blob: a1d5827955c20ab04ed02f09d835d5f65b8c4e8b [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 Peskinec744d992019-07-30 17:26:54 +020012/* Tests that require more than 128kB of RAM plus change have this symbol
13 * as a dependency. Currently we always define this symbol, so the tests
14 * are always executed. In the future we should make this conditional
15 * so that tests that require a lot of memory are skipped on constrained
16 * platforms. */
Gilles Peskine49232e82019-08-07 11:01:30 +020017#define HAVE_RAM_AVAILABLE_128K
Gilles Peskinec744d992019-07-30 17:26:54 +020018
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020019#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020020#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinef426e0f2019-02-25 17:42:03 +010025/* A hash algorithm that is known to be supported.
26 *
27 * This is used in some smoke tests.
28 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010029#if defined(PSA_WANT_ALG_MD2)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010030#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
Gilles Peskined6dc40c2021-01-12 12:55:31 +010031#elif defined(PSA_WANT_ALG_MD4)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
Gilles Peskined6dc40c2021-01-12 12:55:31 +010033#elif defined(PSA_WANT_ALG_MD5)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010034#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
35/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
36 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
37 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
38 * implausible anyway. */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010039#elif defined(PSA_WANT_ALG_SHA_1)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010040#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
Gilles Peskined6dc40c2021-01-12 12:55:31 +010041#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010042#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
Gilles Peskined6dc40c2021-01-12 12:55:31 +010043#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010044#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
Gilles Peskined6dc40c2021-01-12 12:55:31 +010045#elif defined(PSA_WANT_ALG_SHA_512)
46#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
47#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010048#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
49#else
50#undef KNOWN_SUPPORTED_HASH_ALG
51#endif
52
53/* A block cipher that is known to be supported.
54 *
55 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
56 */
57#if defined(MBEDTLS_AES_C)
58#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
59#elif defined(MBEDTLS_ARIA_C)
60#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
61#elif defined(MBEDTLS_CAMELLIA_C)
62#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
63#undef KNOWN_SUPPORTED_BLOCK_CIPHER
64#endif
65
66/* A MAC mode that is known to be supported.
67 *
68 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
69 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
70 *
71 * This is used in some smoke tests.
72 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010073#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010074#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
75#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
76#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
77#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
78#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
79#else
80#undef KNOWN_SUPPORTED_MAC_ALG
81#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
82#endif
83
84/* A cipher algorithm and key type that are known to be supported.
85 *
86 * This is used in some smoke tests.
87 */
88#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
89#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
90#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
91#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
92#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
93#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
94#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
95#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
96#else
97#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
98#endif
99#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
100#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
101#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
102#elif defined(MBEDTLS_RC4_C)
103#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
104#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
105#else
106#undef KNOWN_SUPPORTED_CIPHER_ALG
107#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
108#endif
109
Gilles Peskine667c1112019-12-03 19:03:20 +0100110#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100111int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100112{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100113 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
114 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100115}
116#else
117int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
118{
119 (void) lifetime;
120 return( 0 );
121}
122#endif
123
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200124/** Test if a buffer contains a constant byte value.
125 *
126 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200127 *
128 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200129 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200130 * \param size Size of the buffer in bytes.
131 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200132 * \return 1 if the buffer is all-bits-zero.
133 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200134 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200135static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200136{
137 size_t i;
138 for( i = 0; i < size; i++ )
139 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200140 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200141 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200142 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200143 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200144}
Gilles Peskine818ca122018-06-20 18:16:48 +0200145
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200146/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
147static int asn1_write_10x( unsigned char **p,
148 unsigned char *start,
149 size_t bits,
150 unsigned char x )
151{
152 int ret;
153 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200154 if( bits == 0 )
155 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
156 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200157 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300158 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200159 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
160 *p -= len;
161 ( *p )[len-1] = x;
162 if( bits % 8 == 0 )
163 ( *p )[1] |= 1;
164 else
165 ( *p )[0] |= 1 << ( bits % 8 );
166 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
167 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
168 MBEDTLS_ASN1_INTEGER ) );
169 return( len );
170}
171
172static int construct_fake_rsa_key( unsigned char *buffer,
173 size_t buffer_size,
174 unsigned char **p,
175 size_t bits,
176 int keypair )
177{
178 size_t half_bits = ( bits + 1 ) / 2;
179 int ret;
180 int len = 0;
181 /* Construct something that looks like a DER encoding of
182 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
183 * RSAPrivateKey ::= SEQUENCE {
184 * version Version,
185 * modulus INTEGER, -- n
186 * publicExponent INTEGER, -- e
187 * privateExponent INTEGER, -- d
188 * prime1 INTEGER, -- p
189 * prime2 INTEGER, -- q
190 * exponent1 INTEGER, -- d mod (p-1)
191 * exponent2 INTEGER, -- d mod (q-1)
192 * coefficient INTEGER, -- (inverse of q) mod p
193 * otherPrimeInfos OtherPrimeInfos OPTIONAL
194 * }
195 * Or, for a public key, the same structure with only
196 * version, modulus and publicExponent.
197 */
198 *p = buffer + buffer_size;
199 if( keypair )
200 {
201 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
202 asn1_write_10x( p, buffer, half_bits, 1 ) );
203 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
204 asn1_write_10x( p, buffer, half_bits, 1 ) );
205 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
206 asn1_write_10x( p, buffer, half_bits, 1 ) );
207 MBEDTLS_ASN1_CHK_ADD( len, /* q */
208 asn1_write_10x( p, buffer, half_bits, 1 ) );
209 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
210 asn1_write_10x( p, buffer, half_bits, 3 ) );
211 MBEDTLS_ASN1_CHK_ADD( len, /* d */
212 asn1_write_10x( p, buffer, bits, 1 ) );
213 }
214 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
215 asn1_write_10x( p, buffer, 17, 1 ) );
216 MBEDTLS_ASN1_CHK_ADD( len, /* n */
217 asn1_write_10x( p, buffer, bits, 1 ) );
218 if( keypair )
219 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
220 mbedtls_asn1_write_int( p, buffer, 0 ) );
221 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
222 {
223 const unsigned char tag =
224 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
226 }
227 return( len );
228}
229
Ronald Cron5425a212020-08-04 14:58:35 +0200230int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100231{
232 int ok = 0;
233 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
234 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200235 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100236 psa_key_type_t type;
237 psa_key_type_t bits;
238
239 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
240 lifetime = psa_get_key_lifetime( &attributes );
241 id = psa_get_key_id( &attributes );
242 type = psa_get_key_type( &attributes );
243 bits = psa_get_key_bits( &attributes );
244
245 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200246 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200247 {
248 TEST_ASSERT(
249 ( PSA_KEY_ID_VOLATILE_MIN <=
250 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
251 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
252 PSA_KEY_ID_VOLATILE_MAX ) );
253 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100254 else
255 {
256 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200257 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
258 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100259 }
260#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
261 /* randomly-generated 64-bit constant, should never appear in test data */
262 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
263 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100264 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100265 {
266 /* Mbed Crypto currently always exposes the slot number to
267 * applications. This is not mandated by the PSA specification
268 * and may change in future versions. */
269 TEST_EQUAL( status, 0 );
270 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
271 }
272 else
273 {
274 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
275 }
276#endif
277
278 /* Type and size */
279 TEST_ASSERT( type != 0 );
280 TEST_ASSERT( bits != 0 );
281 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
282 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
283 TEST_ASSERT( bits % 8 == 0 );
284
285 /* MAX macros concerning specific key types */
286 if( PSA_KEY_TYPE_IS_ECC( type ) )
287 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
288 else if( PSA_KEY_TYPE_IS_RSA( type ) )
289 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100290 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100291
292 ok = 1;
293
294exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100295 /*
296 * Key attributes may have been returned by psa_get_key_attributes()
297 * thus reset them as required.
298 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100299 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100300
Gilles Peskine667c1112019-12-03 19:03:20 +0100301 return( ok );
302}
303
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100304int exercise_mac_setup( psa_key_type_t key_type,
305 const unsigned char *key_bytes,
306 size_t key_length,
307 psa_algorithm_t alg,
308 psa_mac_operation_t *operation,
309 psa_status_t *status )
310{
Ronald Cron5425a212020-08-04 14:58:35 +0200311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100313
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100314 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200315 psa_set_key_algorithm( &attributes, alg );
316 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200317 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100318
Ronald Cron5425a212020-08-04 14:58:35 +0200319 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100320 /* Whether setup succeeded or failed, abort must succeed. */
321 PSA_ASSERT( psa_mac_abort( operation ) );
322 /* If setup failed, reproduce the failure, so that the caller can
323 * test the resulting state of the operation object. */
324 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100325 {
Ronald Cron5425a212020-08-04 14:58:35 +0200326 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100327 }
328
Ronald Cron5425a212020-08-04 14:58:35 +0200329 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100330 return( 1 );
331
332exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200333 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100334 return( 0 );
335}
336
337int exercise_cipher_setup( psa_key_type_t key_type,
338 const unsigned char *key_bytes,
339 size_t key_length,
340 psa_algorithm_t alg,
341 psa_cipher_operation_t *operation,
342 psa_status_t *status )
343{
Ronald Cron5425a212020-08-04 14:58:35 +0200344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
348 psa_set_key_algorithm( &attributes, alg );
349 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200350 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351
Ronald Cron5425a212020-08-04 14:58:35 +0200352 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100353 /* Whether setup succeeded or failed, abort must succeed. */
354 PSA_ASSERT( psa_cipher_abort( operation ) );
355 /* If setup failed, reproduce the failure, so that the caller can
356 * test the resulting state of the operation object. */
357 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100358 {
Ronald Cron5425a212020-08-04 14:58:35 +0200359 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100360 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100361 }
362
Ronald Cron5425a212020-08-04 14:58:35 +0200363 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100364 return( 1 );
365
366exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200367 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100368 return( 0 );
369}
370
Ronald Cron5425a212020-08-04 14:58:35 +0200371static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200372 psa_key_usage_t usage,
373 psa_algorithm_t alg )
374{
Jaeden Amero769ce272019-01-04 11:48:03 +0000375 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200376 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200377 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200378 size_t mac_length = sizeof( mac );
379
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100380 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200381 {
Ronald Cron5425a212020-08-04 14:58:35 +0200382 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100383 PSA_ASSERT( psa_mac_update( &operation,
384 input, sizeof( input ) ) );
385 PSA_ASSERT( psa_mac_sign_finish( &operation,
386 mac, sizeof( mac ),
387 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200388 }
389
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100390 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200391 {
392 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100393 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200394 PSA_SUCCESS :
395 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200396 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100397 PSA_ASSERT( psa_mac_update( &operation,
398 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100399 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
400 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200401 }
402
403 return( 1 );
404
405exit:
406 psa_mac_abort( &operation );
407 return( 0 );
408}
409
Ronald Cron5425a212020-08-04 14:58:35 +0200410static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200411 psa_key_usage_t usage,
412 psa_algorithm_t alg )
413{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000414 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200415 unsigned char iv[16] = {0};
416 size_t iv_length = sizeof( iv );
417 const unsigned char plaintext[16] = "Hello, world...";
418 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
419 size_t ciphertext_length = sizeof( ciphertext );
420 unsigned char decrypted[sizeof( ciphertext )];
421 size_t part_length;
422
423 if( usage & PSA_KEY_USAGE_ENCRYPT )
424 {
Ronald Cron5425a212020-08-04 14:58:35 +0200425 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100426 PSA_ASSERT( psa_cipher_generate_iv( &operation,
427 iv, sizeof( iv ),
428 &iv_length ) );
429 PSA_ASSERT( psa_cipher_update( &operation,
430 plaintext, sizeof( plaintext ),
431 ciphertext, sizeof( ciphertext ),
432 &ciphertext_length ) );
433 PSA_ASSERT( psa_cipher_finish( &operation,
434 ciphertext + ciphertext_length,
435 sizeof( ciphertext ) - ciphertext_length,
436 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200437 ciphertext_length += part_length;
438 }
439
440 if( usage & PSA_KEY_USAGE_DECRYPT )
441 {
442 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200443 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200444 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
445 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200447 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200448 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
449 * have this macro yet. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100450 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200451 psa_get_key_type( &attributes ) );
452 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100453 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200454 }
Ronald Cron5425a212020-08-04 14:58:35 +0200455 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100456 PSA_ASSERT( psa_cipher_set_iv( &operation,
457 iv, iv_length ) );
458 PSA_ASSERT( psa_cipher_update( &operation,
459 ciphertext, ciphertext_length,
460 decrypted, sizeof( decrypted ),
461 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200462 status = psa_cipher_finish( &operation,
463 decrypted + part_length,
464 sizeof( decrypted ) - part_length,
465 &part_length );
466 /* For a stream cipher, all inputs are valid. For a block cipher,
467 * if the input is some aribtrary data rather than an actual
468 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200469 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200470 TEST_ASSERT( status == PSA_SUCCESS ||
471 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200472 else
473 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200474 }
475
476 return( 1 );
477
478exit:
479 psa_cipher_abort( &operation );
480 return( 0 );
481}
482
Ronald Cron5425a212020-08-04 14:58:35 +0200483static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200484 psa_key_usage_t usage,
485 psa_algorithm_t alg )
486{
487 unsigned char nonce[16] = {0};
488 size_t nonce_length = sizeof( nonce );
489 unsigned char plaintext[16] = "Hello, world...";
490 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
491 size_t ciphertext_length = sizeof( ciphertext );
492 size_t plaintext_length = sizeof( ciphertext );
493
494 if( usage & PSA_KEY_USAGE_ENCRYPT )
495 {
Ronald Cron5425a212020-08-04 14:58:35 +0200496 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100497 nonce, nonce_length,
498 NULL, 0,
499 plaintext, sizeof( plaintext ),
500 ciphertext, sizeof( ciphertext ),
501 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200502 }
503
504 if( usage & PSA_KEY_USAGE_DECRYPT )
505 {
506 psa_status_t verify_status =
507 ( usage & PSA_KEY_USAGE_ENCRYPT ?
508 PSA_SUCCESS :
509 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200510 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100511 nonce, nonce_length,
512 NULL, 0,
513 ciphertext, ciphertext_length,
514 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100515 &plaintext_length ),
516 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200517 }
518
519 return( 1 );
520
521exit:
522 return( 0 );
523}
524
Ronald Cron5425a212020-08-04 14:58:35 +0200525static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200526 psa_key_usage_t usage,
527 psa_algorithm_t alg )
528{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200529 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
530 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100531 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200532 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100533 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
534
535 /* If the policy allows signing with any hash, just pick one. */
536 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
537 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100538#if defined(KNOWN_SUPPORTED_HASH_ALG)
539 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
540 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100541#else
542 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100543 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100544#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100545 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200546
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100547 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200548 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200549 /* Some algorithms require the payload to have the size of
550 * the hash encoded in the algorithm. Use this input size
551 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200552 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100553 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200554 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100555 payload, payload_length,
556 signature, sizeof( signature ),
557 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200558 }
559
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100560 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200561 {
562 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100563 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200564 PSA_SUCCESS :
565 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200566 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100567 payload, payload_length,
568 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100569 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200570 }
571
572 return( 1 );
573
574exit:
575 return( 0 );
576}
577
Ronald Cron5425a212020-08-04 14:58:35 +0200578static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200579 psa_key_usage_t usage,
580 psa_algorithm_t alg )
581{
582 unsigned char plaintext[256] = "Hello, world...";
583 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
584 size_t ciphertext_length = sizeof( ciphertext );
585 size_t plaintext_length = 16;
586
587 if( usage & PSA_KEY_USAGE_ENCRYPT )
588 {
Ronald Cron5425a212020-08-04 14:58:35 +0200589 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100590 plaintext, plaintext_length,
591 NULL, 0,
592 ciphertext, sizeof( ciphertext ),
593 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200594 }
595
596 if( usage & PSA_KEY_USAGE_DECRYPT )
597 {
598 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200599 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200600 ciphertext, ciphertext_length,
601 NULL, 0,
602 plaintext, sizeof( plaintext ),
603 &plaintext_length );
604 TEST_ASSERT( status == PSA_SUCCESS ||
605 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
606 ( status == PSA_ERROR_INVALID_ARGUMENT ||
607 status == PSA_ERROR_INVALID_PADDING ) ) );
608 }
609
610 return( 1 );
611
612exit:
613 return( 0 );
614}
Gilles Peskine02b75072018-07-01 22:31:34 +0200615
Janos Follathf2815ea2019-07-03 12:41:36 +0100616static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200617 mbedtls_svc_key_id_t key,
Janos Follathf2815ea2019-07-03 12:41:36 +0100618 psa_algorithm_t alg,
619 unsigned char* input1, size_t input1_length,
620 unsigned char* input2, size_t input2_length,
621 size_t capacity )
622{
623 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
624 if( PSA_ALG_IS_HKDF( alg ) )
625 {
626 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
627 PSA_KEY_DERIVATION_INPUT_SALT,
628 input1, input1_length ) );
629 PSA_ASSERT( psa_key_derivation_input_key( operation,
630 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200631 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100632 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
633 PSA_KEY_DERIVATION_INPUT_INFO,
634 input2,
635 input2_length ) );
636 }
637 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
638 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
639 {
640 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
641 PSA_KEY_DERIVATION_INPUT_SEED,
642 input1, input1_length ) );
643 PSA_ASSERT( psa_key_derivation_input_key( operation,
644 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200645 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100646 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
647 PSA_KEY_DERIVATION_INPUT_LABEL,
648 input2, input2_length ) );
649 }
650 else
651 {
652 TEST_ASSERT( ! "Key derivation algorithm not supported" );
653 }
654
Gilles Peskinec744d992019-07-30 17:26:54 +0200655 if( capacity != SIZE_MAX )
656 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100657
658 return( 1 );
659
660exit:
661 return( 0 );
662}
663
664
Ronald Cron5425a212020-08-04 14:58:35 +0200665static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200666 psa_key_usage_t usage,
667 psa_algorithm_t alg )
668{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200669 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100670 unsigned char input1[] = "Input 1";
671 size_t input1_length = sizeof( input1 );
672 unsigned char input2[] = "Input 2";
673 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200674 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100675 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200676
677 if( usage & PSA_KEY_USAGE_DERIVE )
678 {
Ronald Cron5425a212020-08-04 14:58:35 +0200679 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +0100680 input1, input1_length,
681 input2, input2_length, capacity ) )
682 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200683
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200684 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200685 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100686 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200687 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200688 }
689
690 return( 1 );
691
692exit:
693 return( 0 );
694}
695
Gilles Peskinec7998b72018-11-07 18:45:02 +0100696/* We need two keys to exercise key agreement. Exercise the
697 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200698static psa_status_t key_agreement_with_self(
699 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200700 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100701{
702 psa_key_type_t private_key_type;
703 psa_key_type_t public_key_type;
704 size_t key_bits;
705 uint8_t *public_key = NULL;
706 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200707 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200708 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
709 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200710 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100712
Ronald Cron5425a212020-08-04 14:58:35 +0200713 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200714 private_key_type = psa_get_key_type( &attributes );
715 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200716 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100717 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100718 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200719 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100720 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100721
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200722 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200723 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200724 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100725exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100726 /*
727 * Key attributes may have been returned by psa_get_key_attributes()
728 * thus reset them as required.
729 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200730 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100731
732 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100733 return( status );
734}
735
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200736/* We need two keys to exercise key agreement. Exercise the
737 * private key against its own public key. */
738static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
Ronald Cron5425a212020-08-04 14:58:35 +0200739 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. */
Ronald Cron5425a212020-08-04 14:58:35 +0200788 PSA_ASSERT( 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 ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200809 PSA_ASSERT( 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 Peskinedd2f95b2018-08-11 01:22:42 +0200865static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
866 uint8_t *exported, size_t exported_length )
867{
868 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100869 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200870 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100871 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200872
873#if defined(MBEDTLS_DES_C)
874 if( type == PSA_KEY_TYPE_DES )
875 {
876 /* Check the parity bits. */
877 unsigned i;
878 for( i = 0; i < bits / 8; i++ )
879 {
880 unsigned bit_count = 0;
881 unsigned m;
882 for( m = 1; m <= 0x100; m <<= 1 )
883 {
884 if( exported[i] & m )
885 ++bit_count;
886 }
887 TEST_ASSERT( bit_count % 2 != 0 );
888 }
889 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200890 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200891#endif
892
893#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200894 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200895 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200896 uint8_t *p = exported;
897 uint8_t *end = exported + exported_length;
898 size_t len;
899 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200900 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200901 * modulus INTEGER, -- n
902 * publicExponent INTEGER, -- e
903 * privateExponent INTEGER, -- d
904 * prime1 INTEGER, -- p
905 * prime2 INTEGER, -- q
906 * exponent1 INTEGER, -- d mod (p-1)
907 * exponent2 INTEGER, -- d mod (q-1)
908 * coefficient INTEGER, -- (inverse of q) mod p
909 * }
910 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100911 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
912 MBEDTLS_ASN1_SEQUENCE |
913 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
914 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200915 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
916 goto exit;
917 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
918 goto exit;
919 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
920 goto exit;
921 /* Require d to be at least half the size of n. */
922 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
923 goto exit;
924 /* Require p and q to be at most half the size of n, rounded up. */
925 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
926 goto exit;
927 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
928 goto exit;
929 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
930 goto exit;
931 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
932 goto exit;
933 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
934 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100935 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100936 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200937 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200938#endif /* MBEDTLS_RSA_C */
939
940#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200941 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200942 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100943 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100944 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100945 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200946 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200947#endif /* MBEDTLS_ECP_C */
948
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200949 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
950 {
951 uint8_t *p = exported;
952 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200953#if defined(MBEDTLS_RSA_C)
954 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
955 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100956 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200957 /* RSAPublicKey ::= SEQUENCE {
958 * modulus INTEGER, -- n
959 * publicExponent INTEGER } -- e
960 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100961 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
962 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100963 MBEDTLS_ASN1_CONSTRUCTED ),
964 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100965 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200966 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
967 goto exit;
968 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
969 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100970 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200971 }
972 else
973#endif /* MBEDTLS_RSA_C */
974#if defined(MBEDTLS_ECP_C)
975 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
976 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200977 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
978 {
979 /* The representation of an ECC Montgomery public key is
980 * the raw compressed point */
981 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
982 }
983 else
984 {
985 /* The representation of an ECC Weierstrass public key is:
986 * - The byte 0x04;
987 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
988 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
989 * - where m is the bit size associated with the curve.
990 */
991 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
992 TEST_EQUAL( p[0], 4 );
993 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200994 }
995 else
996#endif /* MBEDTLS_ECP_C */
997 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100998 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200999 mbedtls_snprintf( message, sizeof( message ),
1000 "No sanity check for public key type=0x%08lx",
1001 (unsigned long) type );
1002 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +02001003 (void) p;
1004 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001005 return( 0 );
1006 }
1007 }
1008 else
1009
1010 {
1011 /* No sanity checks for other types */
1012 }
1013
1014 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001015
1016exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001017 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001018}
1019
Ronald Cron5425a212020-08-04 14:58:35 +02001020static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +02001021 psa_key_usage_t usage )
1022{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001024 uint8_t *exported = NULL;
1025 size_t exported_size = 0;
1026 size_t exported_length = 0;
1027 int ok = 0;
1028
Ronald Cron5425a212020-08-04 14:58:35 +02001029 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001030
1031 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001032 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001033 {
Ronald Cron5425a212020-08-04 14:58:35 +02001034 TEST_EQUAL( psa_export_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001035 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001036 ok = 1;
1037 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001038 }
1039
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001040 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( psa_get_key_type( &attributes ),
1041 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001042 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001043
Ronald Cron5425a212020-08-04 14:58:35 +02001044 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001045 exported, exported_size,
1046 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001047 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1048 psa_get_key_bits( &attributes ),
1049 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001050
1051exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001052 /*
1053 * Key attributes may have been returned by psa_get_key_attributes()
1054 * thus reset them as required.
1055 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001056 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001057
1058 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001059 return( ok );
1060}
1061
Ronald Cron5425a212020-08-04 14:58:35 +02001062static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001063{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001065 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001066 uint8_t *exported = NULL;
1067 size_t exported_size = 0;
1068 size_t exported_length = 0;
1069 int ok = 0;
1070
Ronald Cron5425a212020-08-04 14:58:35 +02001071 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001072 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001073 {
Ronald Cron5425a212020-08-04 14:58:35 +02001074 TEST_EQUAL( psa_export_public_key( key, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001075 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001076 return( 1 );
1077 }
1078
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001079 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001080 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001081 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1082 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001083 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001084
Ronald Cron5425a212020-08-04 14:58:35 +02001085 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001086 exported, exported_size,
1087 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001088 ok = exported_key_sanity_check( public_type,
1089 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001090 exported, exported_length );
1091
1092exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001093 /*
1094 * Key attributes may have been returned by psa_get_key_attributes()
1095 * thus reset them as required.
1096 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001097 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001098
1099 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001100 return( ok );
1101}
1102
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001103/** Do smoke tests on a key.
1104 *
1105 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1106 * sign/verify, or derivation) that is permitted according to \p usage.
1107 * \p usage and \p alg should correspond to the expected policy on the
1108 * key.
1109 *
1110 * Export the key if permitted by \p usage, and check that the output
1111 * looks sensible. If \p usage forbids export, check that
1112 * \p psa_export_key correctly rejects the attempt. If the key is
1113 * asymmetric, also check \p psa_export_public_key.
1114 *
1115 * If the key fails the tests, this function calls the test framework's
1116 * `test_fail` function and returns false. Otherwise this function returns
1117 * true. Therefore it should be used as follows:
1118 * ```
1119 * if( ! exercise_key( ... ) ) goto exit;
1120 * ```
1121 *
Ronald Cron5425a212020-08-04 14:58:35 +02001122 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001123 * \p alg.
1124 * \param usage The usage flags to assume.
1125 * \param alg The algorithm to exercise.
1126 *
1127 * \retval 0 The key failed the smoke tests.
1128 * \retval 1 The key passed the smoke tests.
1129 */
Ronald Cron5425a212020-08-04 14:58:35 +02001130static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001131 psa_key_usage_t usage,
1132 psa_algorithm_t alg )
1133{
1134 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001135
Ronald Cron5425a212020-08-04 14:58:35 +02001136 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001137 return( 0 );
1138
Gilles Peskine02b75072018-07-01 22:31:34 +02001139 if( alg == 0 )
1140 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1141 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001142 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001143 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001144 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001145 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001146 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001147 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001148 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001149 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001150 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001151 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001152 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001153 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001154 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001155 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001156 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001157 else
1158 {
1159 char message[40];
1160 mbedtls_snprintf( message, sizeof( message ),
1161 "No code to exercise alg=0x%08lx",
1162 (unsigned long) alg );
1163 test_fail( message, __LINE__, __FILE__ );
1164 ok = 0;
1165 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001166
Ronald Cron5425a212020-08-04 14:58:35 +02001167 ok = ok && exercise_export_key( key, usage );
1168 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001169
Gilles Peskine02b75072018-07-01 22:31:34 +02001170 return( ok );
1171}
1172
Gilles Peskine10df3412018-10-25 22:35:43 +02001173static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1174 psa_algorithm_t alg )
1175{
1176 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1177 {
1178 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001179 PSA_KEY_USAGE_VERIFY_HASH :
1180 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001181 }
1182 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1183 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1184 {
1185 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1186 PSA_KEY_USAGE_ENCRYPT :
1187 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1188 }
1189 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1190 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1191 {
1192 return( PSA_KEY_USAGE_DERIVE );
1193 }
1194 else
1195 {
1196 return( 0 );
1197 }
1198
1199}
Darryl Green0c6575a2018-11-07 16:05:30 +00001200
Ronald Cron5425a212020-08-04 14:58:35 +02001201static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001202{
1203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001204 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001205 uint8_t buffer[1];
1206 size_t length;
1207 int ok = 0;
1208
Ronald Cronecfb2372020-07-23 17:13:42 +02001209 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001210 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1211 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1212 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001213 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001214 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001215 TEST_EQUAL(
1216 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1217 TEST_EQUAL(
1218 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001219 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001220 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1221 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1222 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1223 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1224
Ronald Cron5425a212020-08-04 14:58:35 +02001225 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001226 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001227 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001228 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001229 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001230
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001231 ok = 1;
1232
1233exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001234 /*
1235 * Key attributes may have been returned by psa_get_key_attributes()
1236 * thus reset them as required.
1237 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001238 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001239
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001240 return( ok );
1241}
1242
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001243/* Assert that a key isn't reported as having a slot number. */
1244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1245#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1246 do \
1247 { \
1248 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1249 TEST_EQUAL( psa_get_key_slot_number( \
1250 attributes, \
1251 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1252 PSA_ERROR_INVALID_ARGUMENT ); \
1253 } \
1254 while( 0 )
1255#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1256#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1257 ( (void) 0 )
1258#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1259
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001260/* An overapproximation of the amount of storage needed for a key of the
1261 * given type and with the given content. The API doesn't make it easy
1262 * to find a good value for the size. The current implementation doesn't
1263 * care about the value anyway. */
1264#define KEY_BITS_FROM_DATA( type, data ) \
1265 ( data )->len
1266
Darryl Green0c6575a2018-11-07 16:05:30 +00001267typedef enum {
1268 IMPORT_KEY = 0,
1269 GENERATE_KEY = 1,
1270 DERIVE_KEY = 2
1271} generate_method;
1272
Gilles Peskinee59236f2018-01-27 23:32:46 +01001273/* END_HEADER */
1274
1275/* BEGIN_DEPENDENCIES
1276 * depends_on:MBEDTLS_PSA_CRYPTO_C
1277 * END_DEPENDENCIES
1278 */
1279
1280/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001281void static_checks( )
1282{
1283 size_t max_truncated_mac_size =
1284 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1285
1286 /* Check that the length for a truncated MAC always fits in the algorithm
1287 * encoding. The shifted mask is the maximum truncated value. The
1288 * untruncated algorithm may be one byte larger. */
1289 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001290
1291#if defined(MBEDTLS_TEST_DEPRECATED)
1292 /* Check deprecated constants. */
1293 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1294 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1295 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1296 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1297 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1298 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1299 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1300 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001301
Paul Elliott8ff510a2020-06-02 17:19:28 +01001302 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1303 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1304 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1305 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1312 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1327 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1329 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1330 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1331 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1332
1333 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1334 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1335 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1336 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1338 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1339 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1340 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001341
Paul Elliott75e27032020-06-03 15:17:39 +01001342 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1343 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1344 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1345 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1346 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1347
1348 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1349 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001350#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001351}
1352/* END_CASE */
1353
1354/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001355void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001356 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001357 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001358{
Gilles Peskine4747d192019-04-17 15:05:45 +02001359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001360 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001361 psa_key_lifetime_t lifetime = lifetime_arg;
1362 psa_key_usage_t usage_flags = usage_flags_arg;
1363 psa_algorithm_t alg = alg_arg;
1364 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001365 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001366
Ronald Cronecfb2372020-07-23 17:13:42 +02001367 TEST_EQUAL(
1368 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1369 TEST_EQUAL(
1370 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001371 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1372 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1373 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1374 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001375 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001376
Gilles Peskinec87af662019-05-15 16:12:22 +02001377 psa_set_key_id( &attributes, id );
1378 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001379 psa_set_key_usage_flags( &attributes, usage_flags );
1380 psa_set_key_algorithm( &attributes, alg );
1381 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001382 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001383
Ronald Cronecfb2372020-07-23 17:13:42 +02001384 TEST_ASSERT( mbedtls_svc_key_id_equal(
1385 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001386 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1387 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1388 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1389 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001390 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001391
1392 psa_reset_key_attributes( &attributes );
1393
Ronald Cronecfb2372020-07-23 17:13:42 +02001394 TEST_EQUAL(
1395 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1396 TEST_EQUAL(
1397 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001398 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1399 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1400 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1401 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001402 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001403}
1404/* END_CASE */
1405
1406/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001407void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1408 int id2_arg, int owner_id2_arg,
1409 int expected_id_arg, int expected_owner_id_arg,
1410 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001411{
1412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001413 mbedtls_svc_key_id_t id1 =
1414 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001415 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001416 mbedtls_svc_key_id_t id2 =
1417 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001418 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001419 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001420 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1421
1422 if( id1_arg != -1 )
1423 psa_set_key_id( &attributes, id1 );
1424 if( lifetime_arg != -1 )
1425 psa_set_key_lifetime( &attributes, lifetime );
1426 if( id2_arg != -1 )
1427 psa_set_key_id( &attributes, id2 );
1428
Ronald Cronecfb2372020-07-23 17:13:42 +02001429 TEST_ASSERT( mbedtls_svc_key_id_equal(
1430 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001431 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1432}
1433/* END_CASE */
1434
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001435/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1436void slot_number_attribute( )
1437{
1438 psa_key_slot_number_t slot_number = 0xdeadbeef;
1439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1440
1441 /* Initially, there is no slot number. */
1442 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1443 PSA_ERROR_INVALID_ARGUMENT );
1444
1445 /* Test setting a slot number. */
1446 psa_set_key_slot_number( &attributes, 0 );
1447 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1448 TEST_EQUAL( slot_number, 0 );
1449
1450 /* Test changing the slot number. */
1451 psa_set_key_slot_number( &attributes, 42 );
1452 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1453 TEST_EQUAL( slot_number, 42 );
1454
1455 /* Test clearing the slot number. */
1456 psa_clear_key_slot_number( &attributes );
1457 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1458 PSA_ERROR_INVALID_ARGUMENT );
1459
1460 /* Clearing again should have no effect. */
1461 psa_clear_key_slot_number( &attributes );
1462 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1463 PSA_ERROR_INVALID_ARGUMENT );
1464
1465 /* Test that reset clears the slot number. */
1466 psa_set_key_slot_number( &attributes, 42 );
1467 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1468 TEST_EQUAL( slot_number, 42 );
1469 psa_reset_key_attributes( &attributes );
1470 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1471 PSA_ERROR_INVALID_ARGUMENT );
1472}
1473/* END_CASE */
1474
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001475/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001476void import_with_policy( int type_arg,
1477 int usage_arg, int alg_arg,
1478 int expected_status_arg )
1479{
1480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1481 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001482 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001483 psa_key_type_t type = type_arg;
1484 psa_key_usage_t usage = usage_arg;
1485 psa_algorithm_t alg = alg_arg;
1486 psa_status_t expected_status = expected_status_arg;
1487 const uint8_t key_material[16] = {0};
1488 psa_status_t status;
1489
1490 PSA_ASSERT( psa_crypto_init( ) );
1491
1492 psa_set_key_type( &attributes, type );
1493 psa_set_key_usage_flags( &attributes, usage );
1494 psa_set_key_algorithm( &attributes, alg );
1495
1496 status = psa_import_key( &attributes,
1497 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001498 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001499 TEST_EQUAL( status, expected_status );
1500 if( status != PSA_SUCCESS )
1501 goto exit;
1502
Ronald Cron5425a212020-08-04 14:58:35 +02001503 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001504 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1505 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1506 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001507 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001508
Ronald Cron5425a212020-08-04 14:58:35 +02001509 PSA_ASSERT( psa_destroy_key( key ) );
1510 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001511
1512exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001513 /*
1514 * Key attributes may have been returned by psa_get_key_attributes()
1515 * thus reset them as required.
1516 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001517 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001518
1519 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001520 PSA_DONE( );
1521}
1522/* END_CASE */
1523
1524/* BEGIN_CASE */
1525void import_with_data( data_t *data, int type_arg,
1526 int attr_bits_arg,
1527 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001528{
1529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1530 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001531 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001532 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001533 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001534 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001535 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536
Gilles Peskine8817f612018-12-18 00:18:46 +01001537 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001538
Gilles Peskine4747d192019-04-17 15:05:45 +02001539 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001540 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001541
Ronald Cron5425a212020-08-04 14:58:35 +02001542 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001543 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001544 if( status != PSA_SUCCESS )
1545 goto exit;
1546
Ronald Cron5425a212020-08-04 14:58:35 +02001547 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001548 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001549 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001550 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001551 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001552
Ronald Cron5425a212020-08-04 14:58:35 +02001553 PSA_ASSERT( psa_destroy_key( key ) );
1554 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001555
1556exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001557 /*
1558 * Key attributes may have been returned by psa_get_key_attributes()
1559 * thus reset them as required.
1560 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001561 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001562
1563 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001564 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001565}
1566/* END_CASE */
1567
1568/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001569void import_large_key( int type_arg, int byte_size_arg,
1570 int expected_status_arg )
1571{
1572 psa_key_type_t type = type_arg;
1573 size_t byte_size = byte_size_arg;
1574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1575 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001576 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001577 psa_status_t status;
1578 uint8_t *buffer = NULL;
1579 size_t buffer_size = byte_size + 1;
1580 size_t n;
1581
1582 /* It would be better to skip the test than fail it if the allocation
1583 * fails, but the test framework doesn't support this yet. */
1584 ASSERT_ALLOC( buffer, buffer_size );
1585 memset( buffer, 'K', byte_size );
1586
1587 PSA_ASSERT( psa_crypto_init( ) );
1588
1589 /* Try importing the key */
1590 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1591 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001592 status = psa_import_key( &attributes, buffer, byte_size, &key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001593 TEST_EQUAL( status, expected_status );
1594
1595 if( status == PSA_SUCCESS )
1596 {
Ronald Cron5425a212020-08-04 14:58:35 +02001597 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001598 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1599 TEST_EQUAL( psa_get_key_bits( &attributes ),
1600 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001601 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001602 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001603 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001604 for( n = 0; n < byte_size; n++ )
1605 TEST_EQUAL( buffer[n], 'K' );
1606 for( n = byte_size; n < buffer_size; n++ )
1607 TEST_EQUAL( buffer[n], 0 );
1608 }
1609
1610exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001611 /*
1612 * Key attributes may have been returned by psa_get_key_attributes()
1613 * thus reset them as required.
1614 */
1615 psa_reset_key_attributes( &attributes );
1616
Ronald Cron5425a212020-08-04 14:58:35 +02001617 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001618 PSA_DONE( );
1619 mbedtls_free( buffer );
1620}
1621/* END_CASE */
1622
1623/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001624void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1625{
Ronald Cron5425a212020-08-04 14:58:35 +02001626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001627 size_t bits = bits_arg;
1628 psa_status_t expected_status = expected_status_arg;
1629 psa_status_t status;
1630 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001631 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001632 size_t buffer_size = /* Slight overapproximations */
1633 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001634 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001635 unsigned char *p;
1636 int ret;
1637 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001639
Gilles Peskine8817f612018-12-18 00:18:46 +01001640 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001641 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001642
1643 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1644 bits, keypair ) ) >= 0 );
1645 length = ret;
1646
1647 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001648 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001649 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001650 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001651
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001652 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001653 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001654
1655exit:
1656 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001657 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001658}
1659/* END_CASE */
1660
1661/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001662void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001663 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001664 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001665 int expected_bits,
1666 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001667 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001668 int canonical_input )
1669{
Ronald Cron5425a212020-08-04 14:58:35 +02001670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001671 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001672 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001673 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001674 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001675 unsigned char *exported = NULL;
1676 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001677 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001678 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001679 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001681 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001682
Moran Pekercb088e72018-07-17 17:36:59 +03001683 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001684 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001685 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001686 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001688
Gilles Peskine4747d192019-04-17 15:05:45 +02001689 psa_set_key_usage_flags( &attributes, usage_arg );
1690 psa_set_key_algorithm( &attributes, alg );
1691 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001692
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001693 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001694 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001695
1696 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001697 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001698 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1699 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001700 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001701
1702 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001703 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001705
1706 /* The exported length must be set by psa_export_key() to a value between 0
1707 * and export_size. On errors, the exported length must be 0. */
1708 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1709 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1710 TEST_ASSERT( exported_length <= export_size );
1711
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001712 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001713 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001714 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001715 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001716 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001717 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001718 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001719
Ronald Cron5425a212020-08-04 14:58:35 +02001720 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001721 goto exit;
1722
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001723 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001724 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001725 else
1726 {
Ronald Cron5425a212020-08-04 14:58:35 +02001727 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001728 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001729 &key2 ) );
1730 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 reexported,
1732 export_size,
1733 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001734 ASSERT_COMPARE( exported, exported_length,
1735 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001736 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001737 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001738 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001739
1740destroy:
1741 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001742 PSA_ASSERT( psa_destroy_key( key ) );
1743 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001744
1745exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001746 /*
1747 * Key attributes may have been returned by psa_get_key_attributes()
1748 * thus reset them as required.
1749 */
1750 psa_reset_key_attributes( &got_attributes );
1751
itayzafrir3e02b3b2018-06-12 17:06:52 +03001752 mbedtls_free( exported );
1753 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001754 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001755}
1756/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001757
Moran Pekerf709f4a2018-06-06 17:26:04 +03001758/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001759void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001760 int type_arg,
1761 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001762 int export_size_delta,
1763 int expected_export_status_arg,
1764 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001765{
Ronald Cron5425a212020-08-04 14:58:35 +02001766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001767 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001768 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001769 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001770 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001771 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001772 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001773 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001775
Gilles Peskine8817f612018-12-18 00:18:46 +01001776 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001777
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1779 psa_set_key_algorithm( &attributes, alg );
1780 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001781
1782 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001783 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001784
Gilles Peskine49c25912018-10-29 15:15:31 +01001785 /* Export the public key */
1786 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001787 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001788 exported, export_size,
1789 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001790 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001791 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001792 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001793 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001794 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001795 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001796 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001797 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001798 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001799 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1800 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001801 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001802
1803exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001804 /*
1805 * Key attributes may have been returned by psa_get_key_attributes()
1806 * thus reset them as required.
1807 */
1808 psa_reset_key_attributes( &attributes );
1809
itayzafrir3e02b3b2018-06-12 17:06:52 +03001810 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001811 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001812 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001813}
1814/* END_CASE */
1815
Gilles Peskine20035e32018-02-03 22:44:14 +01001816/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001817void import_and_exercise_key( data_t *data,
1818 int type_arg,
1819 int bits_arg,
1820 int alg_arg )
1821{
Ronald Cron5425a212020-08-04 14:58:35 +02001822 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001823 psa_key_type_t type = type_arg;
1824 size_t bits = bits_arg;
1825 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001826 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001828 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001829
Gilles Peskine8817f612018-12-18 00:18:46 +01001830 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001831
Gilles Peskine4747d192019-04-17 15:05:45 +02001832 psa_set_key_usage_flags( &attributes, usage );
1833 psa_set_key_algorithm( &attributes, alg );
1834 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001835
1836 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001837 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001838
1839 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001840 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001841 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1842 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001843
1844 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001845 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001846 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001847
Ronald Cron5425a212020-08-04 14:58:35 +02001848 PSA_ASSERT( psa_destroy_key( key ) );
1849 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001850
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001851exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001852 /*
1853 * Key attributes may have been returned by psa_get_key_attributes()
1854 * thus reset them as required.
1855 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001856 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001857
1858 psa_reset_key_attributes( &attributes );
1859 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001860 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001861}
1862/* END_CASE */
1863
1864/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001865void effective_key_attributes( int type_arg, int expected_type_arg,
1866 int bits_arg, int expected_bits_arg,
1867 int usage_arg, int expected_usage_arg,
1868 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001869{
Ronald Cron5425a212020-08-04 14:58:35 +02001870 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001871 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001872 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001873 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001874 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001875 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001876 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001877 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001878 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001880
Gilles Peskine8817f612018-12-18 00:18:46 +01001881 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001882
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001883 psa_set_key_usage_flags( &attributes, usage );
1884 psa_set_key_algorithm( &attributes, alg );
1885 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001886 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001887
Ronald Cron5425a212020-08-04 14:58:35 +02001888 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001889 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001890
Ronald Cron5425a212020-08-04 14:58:35 +02001891 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001892 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1893 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1894 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1895 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001896
1897exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001898 /*
1899 * Key attributes may have been returned by psa_get_key_attributes()
1900 * thus reset them as required.
1901 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001902 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001903
1904 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001905 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001906}
1907/* END_CASE */
1908
1909/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001910void check_key_policy( int type_arg, int bits_arg,
1911 int usage_arg, int alg_arg )
1912{
1913 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1914 usage_arg, usage_arg, alg_arg, alg_arg );
1915 goto exit;
1916}
1917/* END_CASE */
1918
1919/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001920void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001921{
1922 /* Test each valid way of initializing the object, except for `= {0}`, as
1923 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1924 * though it's OK by the C standard. We could test for this, but we'd need
1925 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001926 psa_key_attributes_t func = psa_key_attributes_init( );
1927 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1928 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001929
1930 memset( &zero, 0, sizeof( zero ) );
1931
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001932 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1933 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1934 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001935
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001936 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1937 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1938 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1939
1940 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1941 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1942 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1943
1944 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1945 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1946 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1947
1948 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1949 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1950 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001951}
1952/* END_CASE */
1953
1954/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955void mac_key_policy( int policy_usage,
1956 int policy_alg,
1957 int key_type,
1958 data_t *key_data,
1959 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001960{
Ronald Cron5425a212020-08-04 14:58:35 +02001961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001963 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001964 psa_status_t status;
1965 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001966
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001968
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001969 psa_set_key_usage_flags( &attributes, policy_usage );
1970 psa_set_key_algorithm( &attributes, policy_alg );
1971 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001972
Gilles Peskine049c7532019-05-15 20:22:09 +02001973 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001974 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001975
Ronald Cron5425a212020-08-04 14:58:35 +02001976 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001978 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001979 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001980 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001981 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001982 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001983
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001985 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001987 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001988 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001989 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001990 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001991
1992exit:
1993 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001994 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001995 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001996}
1997/* END_CASE */
1998
1999/* BEGIN_CASE */
2000void cipher_key_policy( int policy_usage,
2001 int policy_alg,
2002 int key_type,
2003 data_t *key_data,
2004 int exercise_alg )
2005{
Ronald Cron5425a212020-08-04 14:58:35 +02002006 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002008 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009 psa_status_t status;
2010
Gilles Peskine8817f612018-12-18 00:18:46 +01002011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002013 psa_set_key_usage_flags( &attributes, policy_usage );
2014 psa_set_key_algorithm( &attributes, policy_alg );
2015 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016
Gilles Peskine049c7532019-05-15 20:22:09 +02002017 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002018 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019
Ronald Cron5425a212020-08-04 14:58:35 +02002020 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021 if( policy_alg == exercise_alg &&
2022 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002023 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002025 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026 psa_cipher_abort( &operation );
2027
Ronald Cron5425a212020-08-04 14:58:35 +02002028 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029 if( policy_alg == exercise_alg &&
2030 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002031 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002033 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034
2035exit:
2036 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002037 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002038 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039}
2040/* END_CASE */
2041
2042/* BEGIN_CASE */
2043void aead_key_policy( int policy_usage,
2044 int policy_alg,
2045 int key_type,
2046 data_t *key_data,
2047 int nonce_length_arg,
2048 int tag_length_arg,
2049 int exercise_alg )
2050{
Ronald Cron5425a212020-08-04 14:58:35 +02002051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002053 psa_status_t status;
2054 unsigned char nonce[16] = {0};
2055 size_t nonce_length = nonce_length_arg;
2056 unsigned char tag[16];
2057 size_t tag_length = tag_length_arg;
2058 size_t output_length;
2059
2060 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2061 TEST_ASSERT( tag_length <= sizeof( tag ) );
2062
Gilles Peskine8817f612018-12-18 00:18:46 +01002063 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002065 psa_set_key_usage_flags( &attributes, policy_usage );
2066 psa_set_key_algorithm( &attributes, policy_alg );
2067 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068
Gilles Peskine049c7532019-05-15 20:22:09 +02002069 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002070 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002071
Ronald Cron5425a212020-08-04 14:58:35 +02002072 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073 nonce, nonce_length,
2074 NULL, 0,
2075 NULL, 0,
2076 tag, tag_length,
2077 &output_length );
2078 if( policy_alg == exercise_alg &&
2079 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002080 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002082 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083
2084 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002085 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086 nonce, nonce_length,
2087 NULL, 0,
2088 tag, tag_length,
2089 NULL, 0,
2090 &output_length );
2091 if( policy_alg == exercise_alg &&
2092 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002093 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002095 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096
2097exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002098 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002099 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100}
2101/* END_CASE */
2102
2103/* BEGIN_CASE */
2104void asymmetric_encryption_key_policy( int policy_usage,
2105 int policy_alg,
2106 int key_type,
2107 data_t *key_data,
2108 int exercise_alg )
2109{
Ronald Cron5425a212020-08-04 14:58:35 +02002110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112 psa_status_t status;
2113 size_t key_bits;
2114 size_t buffer_length;
2115 unsigned char *buffer = NULL;
2116 size_t output_length;
2117
Gilles Peskine8817f612018-12-18 00:18:46 +01002118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002120 psa_set_key_usage_flags( &attributes, policy_usage );
2121 psa_set_key_algorithm( &attributes, policy_alg );
2122 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123
Gilles Peskine049c7532019-05-15 20:22:09 +02002124 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002125 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002126
Ronald Cron5425a212020-08-04 14:58:35 +02002127 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002128 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002129 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2130 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002131 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132
Ronald Cron5425a212020-08-04 14:58:35 +02002133 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002134 NULL, 0,
2135 NULL, 0,
2136 buffer, buffer_length,
2137 &output_length );
2138 if( policy_alg == exercise_alg &&
2139 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002141 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002142 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002144 if( buffer_length != 0 )
2145 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002146 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147 buffer, buffer_length,
2148 NULL, 0,
2149 buffer, buffer_length,
2150 &output_length );
2151 if( policy_alg == exercise_alg &&
2152 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002153 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002154 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002155 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156
2157exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002158 /*
2159 * Key attributes may have been returned by psa_get_key_attributes()
2160 * thus reset them as required.
2161 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002162 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002163
2164 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002165 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002166 mbedtls_free( buffer );
2167}
2168/* END_CASE */
2169
2170/* BEGIN_CASE */
2171void asymmetric_signature_key_policy( int policy_usage,
2172 int policy_alg,
2173 int key_type,
2174 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002175 int exercise_alg,
2176 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002177{
Ronald Cron5425a212020-08-04 14:58:35 +02002178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002180 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002181 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2182 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2183 * compatible with the policy and `payload_length_arg` is supposed to be
2184 * a valid input length to sign. If `payload_length_arg <= 0`,
2185 * `exercise_alg` is supposed to be forbidden by the policy. */
2186 int compatible_alg = payload_length_arg > 0;
2187 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002188 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002189 size_t signature_length;
2190
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002192
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002193 psa_set_key_usage_flags( &attributes, policy_usage );
2194 psa_set_key_algorithm( &attributes, policy_alg );
2195 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196
Gilles Peskine049c7532019-05-15 20:22:09 +02002197 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002198 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002199
Ronald Cron5425a212020-08-04 14:58:35 +02002200 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002201 payload, payload_length,
2202 signature, sizeof( signature ),
2203 &signature_length );
2204 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002205 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002206 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002207 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208
2209 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002210 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002211 payload, payload_length,
2212 signature, sizeof( signature ) );
2213 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002214 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002215 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002216 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002217
2218exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002219 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002220 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002221}
2222/* END_CASE */
2223
Janos Follathba3fab92019-06-11 14:50:16 +01002224/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002225void derive_key_policy( int policy_usage,
2226 int policy_alg,
2227 int key_type,
2228 data_t *key_data,
2229 int exercise_alg )
2230{
Ronald Cron5425a212020-08-04 14:58:35 +02002231 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002233 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002234 psa_status_t status;
2235
Gilles Peskine8817f612018-12-18 00:18:46 +01002236 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002237
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002238 psa_set_key_usage_flags( &attributes, policy_usage );
2239 psa_set_key_algorithm( &attributes, policy_alg );
2240 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002241
Gilles Peskine049c7532019-05-15 20:22:09 +02002242 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002243 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002244
Janos Follathba3fab92019-06-11 14:50:16 +01002245 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2246
2247 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2248 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002249 {
Janos Follathba3fab92019-06-11 14:50:16 +01002250 PSA_ASSERT( psa_key_derivation_input_bytes(
2251 &operation,
2252 PSA_KEY_DERIVATION_INPUT_SEED,
2253 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002254 }
Janos Follathba3fab92019-06-11 14:50:16 +01002255
2256 status = psa_key_derivation_input_key( &operation,
2257 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002258 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002259
Gilles Peskineea0fb492018-07-12 17:17:20 +02002260 if( policy_alg == exercise_alg &&
2261 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002262 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002263 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002264 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002265
2266exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002267 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002268 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002269 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002270}
2271/* END_CASE */
2272
2273/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002274void agreement_key_policy( int policy_usage,
2275 int policy_alg,
2276 int key_type_arg,
2277 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002278 int exercise_alg,
2279 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002280{
Ronald Cron5425a212020-08-04 14:58:35 +02002281 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002283 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002284 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002285 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002286 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002287
Gilles Peskine8817f612018-12-18 00:18:46 +01002288 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002289
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002290 psa_set_key_usage_flags( &attributes, policy_usage );
2291 psa_set_key_algorithm( &attributes, policy_alg );
2292 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002293
Gilles Peskine049c7532019-05-15 20:22:09 +02002294 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002295 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002297 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002298 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002299
Steven Cooremance48e852020-10-05 16:02:45 +02002300 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002301
2302exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002303 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002304 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002305 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002306}
2307/* END_CASE */
2308
2309/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002310void key_policy_alg2( int key_type_arg, data_t *key_data,
2311 int usage_arg, int alg_arg, int alg2_arg )
2312{
Ronald Cron5425a212020-08-04 14:58:35 +02002313 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002314 psa_key_type_t key_type = key_type_arg;
2315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2316 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2317 psa_key_usage_t usage = usage_arg;
2318 psa_algorithm_t alg = alg_arg;
2319 psa_algorithm_t alg2 = alg2_arg;
2320
2321 PSA_ASSERT( psa_crypto_init( ) );
2322
2323 psa_set_key_usage_flags( &attributes, usage );
2324 psa_set_key_algorithm( &attributes, alg );
2325 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2326 psa_set_key_type( &attributes, key_type );
2327 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002328 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002329
Ronald Cron5425a212020-08-04 14:58:35 +02002330 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002331 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2332 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2333 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2334
Ronald Cron5425a212020-08-04 14:58:35 +02002335 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002336 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002337 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002338 goto exit;
2339
2340exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002341 /*
2342 * Key attributes may have been returned by psa_get_key_attributes()
2343 * thus reset them as required.
2344 */
2345 psa_reset_key_attributes( &got_attributes );
2346
Ronald Cron5425a212020-08-04 14:58:35 +02002347 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002348 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002349}
2350/* END_CASE */
2351
2352/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002353void raw_agreement_key_policy( int policy_usage,
2354 int policy_alg,
2355 int key_type_arg,
2356 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002357 int exercise_alg,
2358 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002359{
Ronald Cron5425a212020-08-04 14:58:35 +02002360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002362 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002363 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002364 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002365 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002366
2367 PSA_ASSERT( psa_crypto_init( ) );
2368
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002369 psa_set_key_usage_flags( &attributes, policy_usage );
2370 psa_set_key_algorithm( &attributes, policy_alg );
2371 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002372
Gilles Peskine049c7532019-05-15 20:22:09 +02002373 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002374 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375
Ronald Cron5425a212020-08-04 14:58:35 +02002376 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002377
Steven Cooremance48e852020-10-05 16:02:45 +02002378 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002379
2380exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002381 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002382 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002383 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002384}
2385/* END_CASE */
2386
2387/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002388void copy_success( int source_usage_arg,
2389 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002390 int type_arg, data_t *material,
2391 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002392 int target_usage_arg,
2393 int target_alg_arg, int target_alg2_arg,
2394 int expected_usage_arg,
2395 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002396{
Gilles Peskineca25db92019-04-19 11:43:08 +02002397 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2398 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002399 psa_key_usage_t expected_usage = expected_usage_arg;
2400 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002402 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2403 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002404 uint8_t *export_buffer = NULL;
2405
Gilles Peskine57ab7212019-01-28 13:03:09 +01002406 PSA_ASSERT( psa_crypto_init( ) );
2407
Gilles Peskineca25db92019-04-19 11:43:08 +02002408 /* Prepare the source key. */
2409 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2410 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002411 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002412 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002413 PSA_ASSERT( psa_import_key( &source_attributes,
2414 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002415 &source_key ) );
2416 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002417
Gilles Peskineca25db92019-04-19 11:43:08 +02002418 /* Prepare the target attributes. */
2419 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002420 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002421 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002422 /* Set volatile lifetime to reset the key identifier to 0. */
2423 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2424 }
2425
Gilles Peskineca25db92019-04-19 11:43:08 +02002426 if( target_usage_arg != -1 )
2427 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2428 if( target_alg_arg != -1 )
2429 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002430 if( target_alg2_arg != -1 )
2431 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002432
2433 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002434 PSA_ASSERT( psa_copy_key( source_key,
2435 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002436
2437 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002438 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002439
2440 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002441 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002442 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2443 psa_get_key_type( &target_attributes ) );
2444 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2445 psa_get_key_bits( &target_attributes ) );
2446 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2447 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002448 TEST_EQUAL( expected_alg2,
2449 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2451 {
2452 size_t length;
2453 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002454 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002455 material->len, &length ) );
2456 ASSERT_COMPARE( material->x, material->len,
2457 export_buffer, length );
2458 }
Ronald Cron5425a212020-08-04 14:58:35 +02002459 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002460 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002461 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002462 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002463
Ronald Cron5425a212020-08-04 14:58:35 +02002464 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002465
2466exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002467 /*
2468 * Source and target key attributes may have been returned by
2469 * psa_get_key_attributes() thus reset them as required.
2470 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002471 psa_reset_key_attributes( &source_attributes );
2472 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002473
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002474 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002475 mbedtls_free( export_buffer );
2476}
2477/* END_CASE */
2478
2479/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002480void copy_fail( int source_usage_arg,
2481 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002482 int type_arg, data_t *material,
2483 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002484 int target_usage_arg,
2485 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002486 int expected_status_arg )
2487{
2488 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2489 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002490 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2491 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002492
2493 PSA_ASSERT( psa_crypto_init( ) );
2494
2495 /* Prepare the source key. */
2496 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2497 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002498 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002499 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002500 PSA_ASSERT( psa_import_key( &source_attributes,
2501 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002502 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002503
2504 /* Prepare the target attributes. */
2505 psa_set_key_type( &target_attributes, target_type_arg );
2506 psa_set_key_bits( &target_attributes, target_bits_arg );
2507 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2508 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002509 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002510
2511 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002512 TEST_EQUAL( psa_copy_key( source_key,
2513 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002514 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002515
Ronald Cron5425a212020-08-04 14:58:35 +02002516 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002517
Gilles Peskine4a644642019-05-03 17:14:08 +02002518exit:
2519 psa_reset_key_attributes( &source_attributes );
2520 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002521 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002522}
2523/* END_CASE */
2524
2525/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002526void hash_operation_init( )
2527{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002528 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002529 /* Test each valid way of initializing the object, except for `= {0}`, as
2530 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2531 * though it's OK by the C standard. We could test for this, but we'd need
2532 * to supress the Clang warning for the test. */
2533 psa_hash_operation_t func = psa_hash_operation_init( );
2534 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2535 psa_hash_operation_t zero;
2536
2537 memset( &zero, 0, sizeof( zero ) );
2538
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002539 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002540 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2541 PSA_ERROR_BAD_STATE );
2542 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2543 PSA_ERROR_BAD_STATE );
2544 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2545 PSA_ERROR_BAD_STATE );
2546
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002547 /* A default hash operation should be abortable without error. */
2548 PSA_ASSERT( psa_hash_abort( &func ) );
2549 PSA_ASSERT( psa_hash_abort( &init ) );
2550 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002551}
2552/* END_CASE */
2553
2554/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002555void hash_setup( int alg_arg,
2556 int expected_status_arg )
2557{
2558 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002559 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002560 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002561 psa_status_t status;
2562
Gilles Peskine8817f612018-12-18 00:18:46 +01002563 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002564
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002565 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002566 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002567
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002568 /* Whether setup succeeded or failed, abort must succeed. */
2569 PSA_ASSERT( psa_hash_abort( &operation ) );
2570
2571 /* If setup failed, reproduce the failure, so as to
2572 * test the resulting state of the operation object. */
2573 if( status != PSA_SUCCESS )
2574 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2575
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002576 /* Now the operation object should be reusable. */
2577#if defined(KNOWN_SUPPORTED_HASH_ALG)
2578 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2579 PSA_ASSERT( psa_hash_abort( &operation ) );
2580#endif
2581
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002582exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002583 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002584}
2585/* END_CASE */
2586
2587/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002588void hash_compute_fail( int alg_arg, data_t *input,
2589 int output_size_arg, int expected_status_arg )
2590{
2591 psa_algorithm_t alg = alg_arg;
2592 uint8_t *output = NULL;
2593 size_t output_size = output_size_arg;
2594 size_t output_length = INVALID_EXPORT_LENGTH;
2595 psa_status_t expected_status = expected_status_arg;
2596 psa_status_t status;
2597
2598 ASSERT_ALLOC( output, output_size );
2599
2600 PSA_ASSERT( psa_crypto_init( ) );
2601
2602 status = psa_hash_compute( alg, input->x, input->len,
2603 output, output_size, &output_length );
2604 TEST_EQUAL( status, expected_status );
2605 TEST_ASSERT( output_length <= output_size );
2606
2607exit:
2608 mbedtls_free( output );
2609 PSA_DONE( );
2610}
2611/* END_CASE */
2612
2613/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002614void hash_compare_fail( int alg_arg, data_t *input,
2615 data_t *reference_hash,
2616 int expected_status_arg )
2617{
2618 psa_algorithm_t alg = alg_arg;
2619 psa_status_t expected_status = expected_status_arg;
2620 psa_status_t status;
2621
2622 PSA_ASSERT( psa_crypto_init( ) );
2623
2624 status = psa_hash_compare( alg, input->x, input->len,
2625 reference_hash->x, reference_hash->len );
2626 TEST_EQUAL( status, expected_status );
2627
2628exit:
2629 PSA_DONE( );
2630}
2631/* END_CASE */
2632
2633/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002634void hash_compute_compare( int alg_arg, data_t *input,
2635 data_t *expected_output )
2636{
2637 psa_algorithm_t alg = alg_arg;
2638 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2639 size_t output_length = INVALID_EXPORT_LENGTH;
2640 size_t i;
2641
2642 PSA_ASSERT( psa_crypto_init( ) );
2643
2644 /* Compute with tight buffer */
2645 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002646 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002647 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002648 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002649 ASSERT_COMPARE( output, output_length,
2650 expected_output->x, expected_output->len );
2651
2652 /* Compute with larger buffer */
2653 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2654 output, sizeof( output ),
2655 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002656 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002657 ASSERT_COMPARE( output, output_length,
2658 expected_output->x, expected_output->len );
2659
2660 /* Compare with correct hash */
2661 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2662 output, output_length ) );
2663
2664 /* Compare with trailing garbage */
2665 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2666 output, output_length + 1 ),
2667 PSA_ERROR_INVALID_SIGNATURE );
2668
2669 /* Compare with truncated hash */
2670 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2671 output, output_length - 1 ),
2672 PSA_ERROR_INVALID_SIGNATURE );
2673
2674 /* Compare with corrupted value */
2675 for( i = 0; i < output_length; i++ )
2676 {
2677 test_set_step( i );
2678 output[i] ^= 1;
2679 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2680 output, output_length ),
2681 PSA_ERROR_INVALID_SIGNATURE );
2682 output[i] ^= 1;
2683 }
2684
2685exit:
2686 PSA_DONE( );
2687}
2688/* END_CASE */
2689
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002690/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002691void hash_bad_order( )
2692{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002693 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002694 unsigned char input[] = "";
2695 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002696 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002697 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2698 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2699 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002700 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002701 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002702 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002703
Gilles Peskine8817f612018-12-18 00:18:46 +01002704 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002705
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002706 /* Call setup twice in a row. */
2707 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2708 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2709 PSA_ERROR_BAD_STATE );
2710 PSA_ASSERT( psa_hash_abort( &operation ) );
2711
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002712 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002713 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002714 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002715 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002716
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002717 /* Call update after finish. */
2718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2719 PSA_ASSERT( psa_hash_finish( &operation,
2720 hash, sizeof( hash ), &hash_len ) );
2721 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002722 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002723 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002724
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002725 /* Call verify without calling setup beforehand. */
2726 TEST_EQUAL( psa_hash_verify( &operation,
2727 valid_hash, sizeof( valid_hash ) ),
2728 PSA_ERROR_BAD_STATE );
2729 PSA_ASSERT( psa_hash_abort( &operation ) );
2730
2731 /* Call verify after finish. */
2732 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2733 PSA_ASSERT( psa_hash_finish( &operation,
2734 hash, sizeof( hash ), &hash_len ) );
2735 TEST_EQUAL( psa_hash_verify( &operation,
2736 valid_hash, sizeof( valid_hash ) ),
2737 PSA_ERROR_BAD_STATE );
2738 PSA_ASSERT( psa_hash_abort( &operation ) );
2739
2740 /* Call verify twice in a row. */
2741 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2742 PSA_ASSERT( psa_hash_verify( &operation,
2743 valid_hash, sizeof( valid_hash ) ) );
2744 TEST_EQUAL( psa_hash_verify( &operation,
2745 valid_hash, sizeof( valid_hash ) ),
2746 PSA_ERROR_BAD_STATE );
2747 PSA_ASSERT( psa_hash_abort( &operation ) );
2748
2749 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002750 TEST_EQUAL( psa_hash_finish( &operation,
2751 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002752 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002753 PSA_ASSERT( psa_hash_abort( &operation ) );
2754
2755 /* Call finish twice in a row. */
2756 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2757 PSA_ASSERT( psa_hash_finish( &operation,
2758 hash, sizeof( hash ), &hash_len ) );
2759 TEST_EQUAL( psa_hash_finish( &operation,
2760 hash, sizeof( hash ), &hash_len ),
2761 PSA_ERROR_BAD_STATE );
2762 PSA_ASSERT( psa_hash_abort( &operation ) );
2763
2764 /* Call finish after calling verify. */
2765 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2766 PSA_ASSERT( psa_hash_verify( &operation,
2767 valid_hash, sizeof( valid_hash ) ) );
2768 TEST_EQUAL( psa_hash_finish( &operation,
2769 hash, sizeof( hash ), &hash_len ),
2770 PSA_ERROR_BAD_STATE );
2771 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002772
2773exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002774 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002775}
2776/* END_CASE */
2777
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002778/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002779void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002780{
2781 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002782 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2783 * appended to it */
2784 unsigned char hash[] = {
2785 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2786 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2787 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002788 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002789 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002790
Gilles Peskine8817f612018-12-18 00:18:46 +01002791 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002792
itayzafrir27e69452018-11-01 14:26:34 +02002793 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002794 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002795 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002796 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002797
itayzafrir27e69452018-11-01 14:26:34 +02002798 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002799 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002800 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002801 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002802
itayzafrir27e69452018-11-01 14:26:34 +02002803 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002804 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002805 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002806 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002807
itayzafrirec93d302018-10-18 18:01:10 +03002808exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002809 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002810}
2811/* END_CASE */
2812
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002813/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2814void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002815{
2816 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002817 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002818 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002819 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002820 size_t hash_len;
2821
Gilles Peskine8817f612018-12-18 00:18:46 +01002822 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002823
itayzafrir58028322018-10-25 10:22:01 +03002824 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002825 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002826 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002827 hash, expected_size - 1, &hash_len ),
2828 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002829
2830exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002831 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002832}
2833/* END_CASE */
2834
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002835/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2836void hash_clone_source_state( )
2837{
2838 psa_algorithm_t alg = PSA_ALG_SHA_256;
2839 unsigned char hash[PSA_HASH_MAX_SIZE];
2840 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2841 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2842 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2843 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2844 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2845 size_t hash_len;
2846
2847 PSA_ASSERT( psa_crypto_init( ) );
2848 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2849
2850 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2851 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2852 PSA_ASSERT( psa_hash_finish( &op_finished,
2853 hash, sizeof( hash ), &hash_len ) );
2854 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2855 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2856
2857 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2858 PSA_ERROR_BAD_STATE );
2859
2860 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2861 PSA_ASSERT( psa_hash_finish( &op_init,
2862 hash, sizeof( hash ), &hash_len ) );
2863 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2864 PSA_ASSERT( psa_hash_finish( &op_finished,
2865 hash, sizeof( hash ), &hash_len ) );
2866 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2867 PSA_ASSERT( psa_hash_finish( &op_aborted,
2868 hash, sizeof( hash ), &hash_len ) );
2869
2870exit:
2871 psa_hash_abort( &op_source );
2872 psa_hash_abort( &op_init );
2873 psa_hash_abort( &op_setup );
2874 psa_hash_abort( &op_finished );
2875 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002876 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002877}
2878/* END_CASE */
2879
2880/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2881void hash_clone_target_state( )
2882{
2883 psa_algorithm_t alg = PSA_ALG_SHA_256;
2884 unsigned char hash[PSA_HASH_MAX_SIZE];
2885 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2886 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2887 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2888 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2889 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2890 size_t hash_len;
2891
2892 PSA_ASSERT( psa_crypto_init( ) );
2893
2894 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2895 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2896 PSA_ASSERT( psa_hash_finish( &op_finished,
2897 hash, sizeof( hash ), &hash_len ) );
2898 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2899 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2900
2901 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2902 PSA_ASSERT( psa_hash_finish( &op_target,
2903 hash, sizeof( hash ), &hash_len ) );
2904
2905 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2906 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2907 PSA_ERROR_BAD_STATE );
2908 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2909 PSA_ERROR_BAD_STATE );
2910
2911exit:
2912 psa_hash_abort( &op_target );
2913 psa_hash_abort( &op_init );
2914 psa_hash_abort( &op_setup );
2915 psa_hash_abort( &op_finished );
2916 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002917 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002918}
2919/* END_CASE */
2920
itayzafrir58028322018-10-25 10:22:01 +03002921/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002922void mac_operation_init( )
2923{
Jaeden Amero252ef282019-02-15 14:05:35 +00002924 const uint8_t input[1] = { 0 };
2925
Jaeden Amero769ce272019-01-04 11:48:03 +00002926 /* Test each valid way of initializing the object, except for `= {0}`, as
2927 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2928 * though it's OK by the C standard. We could test for this, but we'd need
2929 * to supress the Clang warning for the test. */
2930 psa_mac_operation_t func = psa_mac_operation_init( );
2931 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2932 psa_mac_operation_t zero;
2933
2934 memset( &zero, 0, sizeof( zero ) );
2935
Jaeden Amero252ef282019-02-15 14:05:35 +00002936 /* A freshly-initialized MAC operation should not be usable. */
2937 TEST_EQUAL( psa_mac_update( &func,
2938 input, sizeof( input ) ),
2939 PSA_ERROR_BAD_STATE );
2940 TEST_EQUAL( psa_mac_update( &init,
2941 input, sizeof( input ) ),
2942 PSA_ERROR_BAD_STATE );
2943 TEST_EQUAL( psa_mac_update( &zero,
2944 input, sizeof( input ) ),
2945 PSA_ERROR_BAD_STATE );
2946
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002947 /* A default MAC operation should be abortable without error. */
2948 PSA_ASSERT( psa_mac_abort( &func ) );
2949 PSA_ASSERT( psa_mac_abort( &init ) );
2950 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002951}
2952/* END_CASE */
2953
2954/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002955void mac_setup( int key_type_arg,
2956 data_t *key,
2957 int alg_arg,
2958 int expected_status_arg )
2959{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002960 psa_key_type_t key_type = key_type_arg;
2961 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002962 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002963 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002964 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2965#if defined(KNOWN_SUPPORTED_MAC_ALG)
2966 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2967#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002968
Gilles Peskine8817f612018-12-18 00:18:46 +01002969 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002970
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002971 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2972 &operation, &status ) )
2973 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002974 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002975
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002976 /* The operation object should be reusable. */
2977#if defined(KNOWN_SUPPORTED_MAC_ALG)
2978 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2979 smoke_test_key_data,
2980 sizeof( smoke_test_key_data ),
2981 KNOWN_SUPPORTED_MAC_ALG,
2982 &operation, &status ) )
2983 goto exit;
2984 TEST_EQUAL( status, PSA_SUCCESS );
2985#endif
2986
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002987exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002988 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002989}
2990/* END_CASE */
2991
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002992/* 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 +00002993void mac_bad_order( )
2994{
Ronald Cron5425a212020-08-04 14:58:35 +02002995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002996 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2997 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002998 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002999 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3000 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3001 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003003 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3004 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3005 size_t sign_mac_length = 0;
3006 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3007 const uint8_t verify_mac[] = {
3008 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3009 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3010 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3011
3012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003014 psa_set_key_algorithm( &attributes, alg );
3015 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003016
Ronald Cron5425a212020-08-04 14:58:35 +02003017 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3018 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003019
Jaeden Amero252ef282019-02-15 14:05:35 +00003020 /* Call update without calling setup beforehand. */
3021 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3022 PSA_ERROR_BAD_STATE );
3023 PSA_ASSERT( psa_mac_abort( &operation ) );
3024
3025 /* Call sign finish without calling setup beforehand. */
3026 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3027 &sign_mac_length),
3028 PSA_ERROR_BAD_STATE );
3029 PSA_ASSERT( psa_mac_abort( &operation ) );
3030
3031 /* Call verify finish without calling setup beforehand. */
3032 TEST_EQUAL( psa_mac_verify_finish( &operation,
3033 verify_mac, sizeof( verify_mac ) ),
3034 PSA_ERROR_BAD_STATE );
3035 PSA_ASSERT( psa_mac_abort( &operation ) );
3036
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003037 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003038 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3039 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003040 PSA_ERROR_BAD_STATE );
3041 PSA_ASSERT( psa_mac_abort( &operation ) );
3042
Jaeden Amero252ef282019-02-15 14:05:35 +00003043 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003044 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003045 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3046 PSA_ASSERT( psa_mac_sign_finish( &operation,
3047 sign_mac, sizeof( sign_mac ),
3048 &sign_mac_length ) );
3049 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3050 PSA_ERROR_BAD_STATE );
3051 PSA_ASSERT( psa_mac_abort( &operation ) );
3052
3053 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003054 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003055 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3056 PSA_ASSERT( psa_mac_verify_finish( &operation,
3057 verify_mac, sizeof( verify_mac ) ) );
3058 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3059 PSA_ERROR_BAD_STATE );
3060 PSA_ASSERT( psa_mac_abort( &operation ) );
3061
3062 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003063 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003064 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3065 PSA_ASSERT( psa_mac_sign_finish( &operation,
3066 sign_mac, sizeof( sign_mac ),
3067 &sign_mac_length ) );
3068 TEST_EQUAL( psa_mac_sign_finish( &operation,
3069 sign_mac, sizeof( sign_mac ),
3070 &sign_mac_length ),
3071 PSA_ERROR_BAD_STATE );
3072 PSA_ASSERT( psa_mac_abort( &operation ) );
3073
3074 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003075 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003076 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3077 PSA_ASSERT( psa_mac_verify_finish( &operation,
3078 verify_mac, sizeof( verify_mac ) ) );
3079 TEST_EQUAL( psa_mac_verify_finish( &operation,
3080 verify_mac, sizeof( verify_mac ) ),
3081 PSA_ERROR_BAD_STATE );
3082 PSA_ASSERT( psa_mac_abort( &operation ) );
3083
3084 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003085 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003086 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3087 TEST_EQUAL( psa_mac_verify_finish( &operation,
3088 verify_mac, sizeof( verify_mac ) ),
3089 PSA_ERROR_BAD_STATE );
3090 PSA_ASSERT( psa_mac_abort( &operation ) );
3091
3092 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003093 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003094 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3095 TEST_EQUAL( psa_mac_sign_finish( &operation,
3096 sign_mac, sizeof( sign_mac ),
3097 &sign_mac_length ),
3098 PSA_ERROR_BAD_STATE );
3099 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003100
Ronald Cron5425a212020-08-04 14:58:35 +02003101 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003102
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003103exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003104 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003105}
3106/* END_CASE */
3107
3108/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003109void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003110 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003111 int alg_arg,
3112 data_t *input,
3113 data_t *expected_mac )
3114{
Ronald Cron5425a212020-08-04 14:58:35 +02003115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003116 psa_key_type_t key_type = key_type_arg;
3117 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003118 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003120 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003121 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003122 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003123 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003124 const size_t output_sizes_to_test[] = {
3125 0,
3126 1,
3127 expected_mac->len - 1,
3128 expected_mac->len,
3129 expected_mac->len + 1,
3130 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003131
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003132 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003133 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003134 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003135
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003139 psa_set_key_algorithm( &attributes, alg );
3140 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003141
Ronald Cron5425a212020-08-04 14:58:35 +02003142 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3143 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003144
Gilles Peskine8b356b52020-08-25 23:44:59 +02003145 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3146 {
3147 const size_t output_size = output_sizes_to_test[i];
3148 psa_status_t expected_status =
3149 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3150 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003151
Gilles Peskine8b356b52020-08-25 23:44:59 +02003152 test_set_step( output_size );
3153 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003154
Gilles Peskine8b356b52020-08-25 23:44:59 +02003155 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003156 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003157 PSA_ASSERT( psa_mac_update( &operation,
3158 input->x, input->len ) );
3159 TEST_EQUAL( psa_mac_sign_finish( &operation,
3160 actual_mac, output_size,
3161 &mac_length ),
3162 expected_status );
3163 PSA_ASSERT( psa_mac_abort( &operation ) );
3164
3165 if( expected_status == PSA_SUCCESS )
3166 {
3167 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3168 actual_mac, mac_length );
3169 }
3170 mbedtls_free( actual_mac );
3171 actual_mac = NULL;
3172 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003173
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003174exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003175 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003176 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003177 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003178 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003179}
3180/* END_CASE */
3181
3182/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003183void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003184 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003185 int alg_arg,
3186 data_t *input,
3187 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003188{
Ronald Cron5425a212020-08-04 14:58:35 +02003189 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003190 psa_key_type_t key_type = key_type_arg;
3191 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003192 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003194 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003195
Gilles Peskine69c12672018-06-28 00:07:19 +02003196 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3197
Gilles Peskine8817f612018-12-18 00:18:46 +01003198 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003199
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003200 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003201 psa_set_key_algorithm( &attributes, alg );
3202 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003203
Ronald Cron5425a212020-08-04 14:58:35 +02003204 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3205 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003206
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003207 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003208 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_mac_update( &operation,
3210 input->x, input->len ) );
3211 PSA_ASSERT( psa_mac_verify_finish( &operation,
3212 expected_mac->x,
3213 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003214
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003215 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003216 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003217 PSA_ASSERT( psa_mac_update( &operation,
3218 input->x, input->len ) );
3219 TEST_EQUAL( psa_mac_verify_finish( &operation,
3220 expected_mac->x,
3221 expected_mac->len - 1 ),
3222 PSA_ERROR_INVALID_SIGNATURE );
3223
3224 /* Test a MAC that's too long. */
3225 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3226 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003227 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003228 PSA_ASSERT( psa_mac_update( &operation,
3229 input->x, input->len ) );
3230 TEST_EQUAL( psa_mac_verify_finish( &operation,
3231 perturbed_mac,
3232 expected_mac->len + 1 ),
3233 PSA_ERROR_INVALID_SIGNATURE );
3234
3235 /* Test changing one byte. */
3236 for( size_t i = 0; i < expected_mac->len; i++ )
3237 {
3238 test_set_step( i );
3239 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003240 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003241 PSA_ASSERT( psa_mac_update( &operation,
3242 input->x, input->len ) );
3243 TEST_EQUAL( psa_mac_verify_finish( &operation,
3244 perturbed_mac,
3245 expected_mac->len ),
3246 PSA_ERROR_INVALID_SIGNATURE );
3247 perturbed_mac[i] ^= 1;
3248 }
3249
Gilles Peskine8c9def32018-02-08 10:02:12 +01003250exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003251 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003252 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003253 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003254 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003255}
3256/* END_CASE */
3257
3258/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003259void cipher_operation_init( )
3260{
Jaeden Ameroab439972019-02-15 14:12:05 +00003261 const uint8_t input[1] = { 0 };
3262 unsigned char output[1] = { 0 };
3263 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003264 /* Test each valid way of initializing the object, except for `= {0}`, as
3265 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3266 * though it's OK by the C standard. We could test for this, but we'd need
3267 * to supress the Clang warning for the test. */
3268 psa_cipher_operation_t func = psa_cipher_operation_init( );
3269 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3270 psa_cipher_operation_t zero;
3271
3272 memset( &zero, 0, sizeof( zero ) );
3273
Jaeden Ameroab439972019-02-15 14:12:05 +00003274 /* A freshly-initialized cipher operation should not be usable. */
3275 TEST_EQUAL( psa_cipher_update( &func,
3276 input, sizeof( input ),
3277 output, sizeof( output ),
3278 &output_length ),
3279 PSA_ERROR_BAD_STATE );
3280 TEST_EQUAL( psa_cipher_update( &init,
3281 input, sizeof( input ),
3282 output, sizeof( output ),
3283 &output_length ),
3284 PSA_ERROR_BAD_STATE );
3285 TEST_EQUAL( psa_cipher_update( &zero,
3286 input, sizeof( input ),
3287 output, sizeof( output ),
3288 &output_length ),
3289 PSA_ERROR_BAD_STATE );
3290
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003291 /* A default cipher operation should be abortable without error. */
3292 PSA_ASSERT( psa_cipher_abort( &func ) );
3293 PSA_ASSERT( psa_cipher_abort( &init ) );
3294 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003295}
3296/* END_CASE */
3297
3298/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003299void cipher_setup( int key_type_arg,
3300 data_t *key,
3301 int alg_arg,
3302 int expected_status_arg )
3303{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003304 psa_key_type_t key_type = key_type_arg;
3305 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003306 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003307 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003308 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003309#if defined(KNOWN_SUPPORTED_MAC_ALG)
3310 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3311#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003312
Gilles Peskine8817f612018-12-18 00:18:46 +01003313 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003314
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003315 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3316 &operation, &status ) )
3317 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003318 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003319
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003320 /* The operation object should be reusable. */
3321#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3322 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3323 smoke_test_key_data,
3324 sizeof( smoke_test_key_data ),
3325 KNOWN_SUPPORTED_CIPHER_ALG,
3326 &operation, &status ) )
3327 goto exit;
3328 TEST_EQUAL( status, PSA_SUCCESS );
3329#endif
3330
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003331exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003332 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003333 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003334}
3335/* END_CASE */
3336
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003337/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003338void cipher_bad_order( )
3339{
Ronald Cron5425a212020-08-04 14:58:35 +02003340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003341 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3342 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003344 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003345 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003346 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003347 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3348 0xaa, 0xaa, 0xaa, 0xaa };
3349 const uint8_t text[] = {
3350 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3351 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003352 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003353 size_t length = 0;
3354
3355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003356 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3357 psa_set_key_algorithm( &attributes, alg );
3358 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003359 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3360 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003361
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003362 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003363 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3364 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003365 PSA_ERROR_BAD_STATE );
3366 PSA_ASSERT( psa_cipher_abort( &operation ) );
3367
3368 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003369 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3370 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003371 PSA_ERROR_BAD_STATE );
3372 PSA_ASSERT( psa_cipher_abort( &operation ) );
3373
Jaeden Ameroab439972019-02-15 14:12:05 +00003374 /* Generate an IV without calling setup beforehand. */
3375 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3376 buffer, sizeof( buffer ),
3377 &length ),
3378 PSA_ERROR_BAD_STATE );
3379 PSA_ASSERT( psa_cipher_abort( &operation ) );
3380
3381 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003382 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003383 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3384 buffer, sizeof( buffer ),
3385 &length ) );
3386 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3387 buffer, sizeof( buffer ),
3388 &length ),
3389 PSA_ERROR_BAD_STATE );
3390 PSA_ASSERT( psa_cipher_abort( &operation ) );
3391
3392 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003393 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003394 PSA_ASSERT( psa_cipher_set_iv( &operation,
3395 iv, sizeof( iv ) ) );
3396 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3397 buffer, sizeof( buffer ),
3398 &length ),
3399 PSA_ERROR_BAD_STATE );
3400 PSA_ASSERT( psa_cipher_abort( &operation ) );
3401
3402 /* Set an IV without calling setup beforehand. */
3403 TEST_EQUAL( psa_cipher_set_iv( &operation,
3404 iv, sizeof( iv ) ),
3405 PSA_ERROR_BAD_STATE );
3406 PSA_ASSERT( psa_cipher_abort( &operation ) );
3407
3408 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003409 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003410 PSA_ASSERT( psa_cipher_set_iv( &operation,
3411 iv, sizeof( iv ) ) );
3412 TEST_EQUAL( psa_cipher_set_iv( &operation,
3413 iv, sizeof( iv ) ),
3414 PSA_ERROR_BAD_STATE );
3415 PSA_ASSERT( psa_cipher_abort( &operation ) );
3416
3417 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003418 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003419 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3420 buffer, sizeof( buffer ),
3421 &length ) );
3422 TEST_EQUAL( psa_cipher_set_iv( &operation,
3423 iv, sizeof( iv ) ),
3424 PSA_ERROR_BAD_STATE );
3425 PSA_ASSERT( psa_cipher_abort( &operation ) );
3426
3427 /* Call update without calling setup beforehand. */
3428 TEST_EQUAL( psa_cipher_update( &operation,
3429 text, sizeof( text ),
3430 buffer, sizeof( buffer ),
3431 &length ),
3432 PSA_ERROR_BAD_STATE );
3433 PSA_ASSERT( psa_cipher_abort( &operation ) );
3434
3435 /* Call update without an IV where an IV is required. */
3436 TEST_EQUAL( psa_cipher_update( &operation,
3437 text, sizeof( text ),
3438 buffer, sizeof( buffer ),
3439 &length ),
3440 PSA_ERROR_BAD_STATE );
3441 PSA_ASSERT( psa_cipher_abort( &operation ) );
3442
3443 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003444 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003445 PSA_ASSERT( psa_cipher_set_iv( &operation,
3446 iv, sizeof( iv ) ) );
3447 PSA_ASSERT( psa_cipher_finish( &operation,
3448 buffer, sizeof( buffer ), &length ) );
3449 TEST_EQUAL( psa_cipher_update( &operation,
3450 text, sizeof( text ),
3451 buffer, sizeof( buffer ),
3452 &length ),
3453 PSA_ERROR_BAD_STATE );
3454 PSA_ASSERT( psa_cipher_abort( &operation ) );
3455
3456 /* Call finish without calling setup beforehand. */
3457 TEST_EQUAL( psa_cipher_finish( &operation,
3458 buffer, sizeof( buffer ), &length ),
3459 PSA_ERROR_BAD_STATE );
3460 PSA_ASSERT( psa_cipher_abort( &operation ) );
3461
3462 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003463 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003464 /* Not calling update means we are encrypting an empty buffer, which is OK
3465 * for cipher modes with padding. */
3466 TEST_EQUAL( psa_cipher_finish( &operation,
3467 buffer, sizeof( buffer ), &length ),
3468 PSA_ERROR_BAD_STATE );
3469 PSA_ASSERT( psa_cipher_abort( &operation ) );
3470
3471 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003472 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003473 PSA_ASSERT( psa_cipher_set_iv( &operation,
3474 iv, sizeof( iv ) ) );
3475 PSA_ASSERT( psa_cipher_finish( &operation,
3476 buffer, sizeof( buffer ), &length ) );
3477 TEST_EQUAL( psa_cipher_finish( &operation,
3478 buffer, sizeof( buffer ), &length ),
3479 PSA_ERROR_BAD_STATE );
3480 PSA_ASSERT( psa_cipher_abort( &operation ) );
3481
Ronald Cron5425a212020-08-04 14:58:35 +02003482 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003483
Jaeden Ameroab439972019-02-15 14:12:05 +00003484exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003485 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003486 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003487}
3488/* END_CASE */
3489
3490/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003491void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003492 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003493 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003494 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495{
Ronald Cron5425a212020-08-04 14:58:35 +02003496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003497 psa_status_t status;
3498 psa_key_type_t key_type = key_type_arg;
3499 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003500 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003501 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003502 size_t output_buffer_size = 0;
3503 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003504 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003505 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003507
Gilles Peskine8817f612018-12-18 00:18:46 +01003508 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003509
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003510 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3511 psa_set_key_algorithm( &attributes, alg );
3512 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003513
Ronald Cron5425a212020-08-04 14:58:35 +02003514 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3515 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003516
Ronald Cron5425a212020-08-04 14:58:35 +02003517 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003518
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003519 if( iv->len > 0 )
3520 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003521 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003522 }
3523
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003524 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003525 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003526 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003527
Gilles Peskine8817f612018-12-18 00:18:46 +01003528 PSA_ASSERT( psa_cipher_update( &operation,
3529 input->x, input->len,
3530 output, output_buffer_size,
3531 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003532 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003533 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003534 output + total_output_length,
3535 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003536 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003537 total_output_length += function_output_length;
3538
Gilles Peskinefe11b722018-12-18 00:24:04 +01003539 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003540 if( expected_status == PSA_SUCCESS )
3541 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003542 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003543 ASSERT_COMPARE( expected_output->x, expected_output->len,
3544 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003545 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003546
Gilles Peskine50e586b2018-06-08 14:28:46 +02003547exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003548 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003549 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003550 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003551 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003552}
3553/* END_CASE */
3554
3555/* BEGIN_CASE */
3556void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003557 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003558 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003559 int first_part_size_arg,
3560 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003561 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003562{
Ronald Cron5425a212020-08-04 14:58:35 +02003563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003564 psa_key_type_t key_type = key_type_arg;
3565 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003566 size_t first_part_size = first_part_size_arg;
3567 size_t output1_length = output1_length_arg;
3568 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003569 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003570 size_t output_buffer_size = 0;
3571 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003572 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003573 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575
Gilles Peskine8817f612018-12-18 00:18:46 +01003576 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003577
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003578 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3579 psa_set_key_algorithm( &attributes, alg );
3580 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003581
Ronald Cron5425a212020-08-04 14:58:35 +02003582 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3583 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003584
Ronald Cron5425a212020-08-04 14:58:35 +02003585 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003586
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003587 if( iv->len > 0 )
3588 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003589 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003590 }
3591
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003592 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003593 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003594 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003595
Gilles Peskinee0866522019-02-19 19:44:00 +01003596 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003597 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3598 output, output_buffer_size,
3599 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003600 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003601 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003602 PSA_ASSERT( psa_cipher_update( &operation,
3603 input->x + first_part_size,
3604 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003605 output + total_output_length,
3606 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003607 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003608 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003609 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003611 output + total_output_length,
3612 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003614 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003616
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003617 ASSERT_COMPARE( expected_output->x, expected_output->len,
3618 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003619
3620exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003621 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003622 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003623 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003624 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003625}
3626/* END_CASE */
3627
3628/* BEGIN_CASE */
3629void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003630 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003631 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003632 int first_part_size_arg,
3633 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003634 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003635{
Ronald Cron5425a212020-08-04 14:58:35 +02003636 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003637 psa_key_type_t key_type = key_type_arg;
3638 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003639 size_t first_part_size = first_part_size_arg;
3640 size_t output1_length = output1_length_arg;
3641 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003642 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003643 size_t output_buffer_size = 0;
3644 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003645 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003646 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003648
Gilles Peskine8817f612018-12-18 00:18:46 +01003649 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003650
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003651 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3652 psa_set_key_algorithm( &attributes, alg );
3653 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003654
Ronald Cron5425a212020-08-04 14:58:35 +02003655 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3656 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003657
Ronald Cron5425a212020-08-04 14:58:35 +02003658 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003659
Steven Cooreman177deba2020-09-07 17:14:14 +02003660 if( iv->len > 0 )
3661 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003662 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003663 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003664
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003665 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003666 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003667 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003668
Gilles Peskinee0866522019-02-19 19:44:00 +01003669 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003670 PSA_ASSERT( psa_cipher_update( &operation,
3671 input->x, first_part_size,
3672 output, output_buffer_size,
3673 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003674 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003675 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003676 PSA_ASSERT( psa_cipher_update( &operation,
3677 input->x + first_part_size,
3678 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003679 output + total_output_length,
3680 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003681 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003682 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003683 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003684 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003685 output + total_output_length,
3686 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003687 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003688 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003689 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003690
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003691 ASSERT_COMPARE( expected_output->x, expected_output->len,
3692 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003693
3694exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003695 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003696 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003697 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003698 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003699}
3700/* END_CASE */
3701
Gilles Peskine50e586b2018-06-08 14:28:46 +02003702/* BEGIN_CASE */
3703void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003704 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003705 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003706 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003707{
Ronald Cron5425a212020-08-04 14:58:35 +02003708 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003709 psa_status_t status;
3710 psa_key_type_t key_type = key_type_arg;
3711 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003712 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003713 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003714 size_t output_buffer_size = 0;
3715 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003716 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003717 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003719
Gilles Peskine8817f612018-12-18 00:18:46 +01003720 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003721
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003722 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3723 psa_set_key_algorithm( &attributes, alg );
3724 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003725
Ronald Cron5425a212020-08-04 14:58:35 +02003726 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3727 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003728
Ronald Cron5425a212020-08-04 14:58:35 +02003729 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003730
Steven Cooreman177deba2020-09-07 17:14:14 +02003731 if( iv->len > 0 )
3732 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003733 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003734 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003735
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003736 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003737 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003738 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003739
Gilles Peskine8817f612018-12-18 00:18:46 +01003740 PSA_ASSERT( psa_cipher_update( &operation,
3741 input->x, input->len,
3742 output, output_buffer_size,
3743 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003744 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003745 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003746 output + total_output_length,
3747 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003748 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003749 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003750 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003751
3752 if( expected_status == PSA_SUCCESS )
3753 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003754 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003755 ASSERT_COMPARE( expected_output->x, expected_output->len,
3756 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003757 }
3758
Gilles Peskine50e586b2018-06-08 14:28:46 +02003759exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003760 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003761 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003762 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003763 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003764}
3765/* END_CASE */
3766
Gilles Peskine50e586b2018-06-08 14:28:46 +02003767/* BEGIN_CASE */
3768void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003769 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003770 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003771{
Ronald Cron5425a212020-08-04 14:58:35 +02003772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003773 psa_key_type_t key_type = key_type_arg;
3774 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003775 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003776 size_t iv_size = 16;
3777 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003778 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003779 size_t output1_size = 0;
3780 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003781 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003782 size_t output2_size = 0;
3783 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003784 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003785 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3786 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003788
Gilles Peskine8817f612018-12-18 00:18:46 +01003789 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003790
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003791 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3792 psa_set_key_algorithm( &attributes, alg );
3793 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003794
Ronald Cron5425a212020-08-04 14:58:35 +02003795 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3796 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003797
Ronald Cron5425a212020-08-04 14:58:35 +02003798 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3799 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003800
Steven Cooreman177deba2020-09-07 17:14:14 +02003801 if( alg != PSA_ALG_ECB_NO_PADDING )
3802 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003803 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3804 iv, iv_size,
3805 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003806 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003807 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003808 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003809 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003810
Gilles Peskine8817f612018-12-18 00:18:46 +01003811 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3812 output1, output1_size,
3813 &output1_length ) );
3814 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003815 output1 + output1_length,
3816 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003817 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003818
Gilles Peskine048b7f02018-06-08 14:20:49 +02003819 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003820
Gilles Peskine8817f612018-12-18 00:18:46 +01003821 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003822
3823 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003824 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003825
Steven Cooreman177deba2020-09-07 17:14:14 +02003826 if( iv_length > 0 )
3827 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003828 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3829 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003830 }
3831
Gilles Peskine8817f612018-12-18 00:18:46 +01003832 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3833 output2, output2_size,
3834 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003835 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003836 PSA_ASSERT( psa_cipher_finish( &operation2,
3837 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003838 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003839 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003840
Gilles Peskine048b7f02018-06-08 14:20:49 +02003841 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003842
Gilles Peskine8817f612018-12-18 00:18:46 +01003843 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003844
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003845 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003846
3847exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003848 psa_cipher_abort( &operation1 );
3849 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003850 mbedtls_free( output1 );
3851 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003852 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003853 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003854}
3855/* END_CASE */
3856
3857/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003858void cipher_verify_output_multipart( int alg_arg,
3859 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003860 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003861 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003862 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003863{
Ronald Cron5425a212020-08-04 14:58:35 +02003864 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003865 psa_key_type_t key_type = key_type_arg;
3866 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003867 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003868 unsigned char iv[16] = {0};
3869 size_t iv_size = 16;
3870 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003871 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003872 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003873 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003874 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003875 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003876 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003877 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003878 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3879 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003881
Gilles Peskine8817f612018-12-18 00:18:46 +01003882 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003883
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003884 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3885 psa_set_key_algorithm( &attributes, alg );
3886 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003887
Ronald Cron5425a212020-08-04 14:58:35 +02003888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3889 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003890
Ronald Cron5425a212020-08-04 14:58:35 +02003891 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3892 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003893
Steven Cooreman177deba2020-09-07 17:14:14 +02003894 if( alg != PSA_ALG_ECB_NO_PADDING )
3895 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003896 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3897 iv, iv_size,
3898 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003899 }
3900
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003901 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003902 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003903 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003904
Gilles Peskinee0866522019-02-19 19:44:00 +01003905 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003906
Gilles Peskine8817f612018-12-18 00:18:46 +01003907 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3908 output1, output1_buffer_size,
3909 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003910 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003911
Gilles Peskine8817f612018-12-18 00:18:46 +01003912 PSA_ASSERT( psa_cipher_update( &operation1,
3913 input->x + first_part_size,
3914 input->len - first_part_size,
3915 output1, output1_buffer_size,
3916 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003917 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003918
Gilles Peskine8817f612018-12-18 00:18:46 +01003919 PSA_ASSERT( psa_cipher_finish( &operation1,
3920 output1 + output1_length,
3921 output1_buffer_size - output1_length,
3922 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003923 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003924
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003926
Gilles Peskine048b7f02018-06-08 14:20:49 +02003927 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003928 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003929
Steven Cooreman177deba2020-09-07 17:14:14 +02003930 if( iv_length > 0 )
3931 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003932 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3933 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003934 }
Moran Pekerded84402018-06-06 16:36:50 +03003935
Gilles Peskine8817f612018-12-18 00:18:46 +01003936 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3937 output2, output2_buffer_size,
3938 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003939 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003940
Gilles Peskine8817f612018-12-18 00:18:46 +01003941 PSA_ASSERT( psa_cipher_update( &operation2,
3942 output1 + first_part_size,
3943 output1_length - first_part_size,
3944 output2, output2_buffer_size,
3945 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003946 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003947
Gilles Peskine8817f612018-12-18 00:18:46 +01003948 PSA_ASSERT( psa_cipher_finish( &operation2,
3949 output2 + output2_length,
3950 output2_buffer_size - output2_length,
3951 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003952 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003953
Gilles Peskine8817f612018-12-18 00:18:46 +01003954 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003955
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003956 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003957
3958exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003959 psa_cipher_abort( &operation1 );
3960 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003961 mbedtls_free( output1 );
3962 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003963 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003964 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003965}
3966/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003967
Gilles Peskine20035e32018-02-03 22:44:14 +01003968/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003969void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003970 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003971 data_t *nonce,
3972 data_t *additional_data,
3973 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003974 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003975{
Ronald Cron5425a212020-08-04 14:58:35 +02003976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003977 psa_key_type_t key_type = key_type_arg;
3978 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003979 unsigned char *output_data = NULL;
3980 size_t output_size = 0;
3981 size_t output_length = 0;
3982 unsigned char *output_data2 = NULL;
3983 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003984 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003985 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003987
Gilles Peskine4abf7412018-06-18 16:35:34 +02003988 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003989 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3990 * should be exact. */
3991 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3992 TEST_EQUAL( output_size,
3993 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003994 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003995
Gilles Peskine8817f612018-12-18 00:18:46 +01003996 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003997
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003998 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3999 psa_set_key_algorithm( &attributes, alg );
4000 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004001
Gilles Peskine049c7532019-05-15 20:22:09 +02004002 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004003 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004004
Ronald Cron5425a212020-08-04 14:58:35 +02004005 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004006 nonce->x, nonce->len,
4007 additional_data->x,
4008 additional_data->len,
4009 input_data->x, input_data->len,
4010 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004011 &output_length ),
4012 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004013
4014 if( PSA_SUCCESS == expected_result )
4015 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004016 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004017
Gilles Peskine003a4a92019-05-14 16:09:40 +02004018 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4019 * should be exact. */
4020 TEST_EQUAL( input_data->len,
4021 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4022
Ronald Cron5425a212020-08-04 14:58:35 +02004023 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004024 nonce->x, nonce->len,
4025 additional_data->x,
4026 additional_data->len,
4027 output_data, output_length,
4028 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004029 &output_length2 ),
4030 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004031
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004032 ASSERT_COMPARE( input_data->x, input_data->len,
4033 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004034 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004035
Gilles Peskinea1cac842018-06-11 19:33:02 +02004036exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004037 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004038 mbedtls_free( output_data );
4039 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004040 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004041}
4042/* END_CASE */
4043
4044/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004045void aead_encrypt( int key_type_arg, data_t *key_data,
4046 int alg_arg,
4047 data_t *nonce,
4048 data_t *additional_data,
4049 data_t *input_data,
4050 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004051{
Ronald Cron5425a212020-08-04 14:58:35 +02004052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004053 psa_key_type_t key_type = key_type_arg;
4054 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004055 unsigned char *output_data = NULL;
4056 size_t output_size = 0;
4057 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004058 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004060
Gilles Peskine4abf7412018-06-18 16:35:34 +02004061 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004062 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4063 * should be exact. */
4064 TEST_EQUAL( output_size,
4065 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004066 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004067
Gilles Peskine8817f612018-12-18 00:18:46 +01004068 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004069
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004070 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4071 psa_set_key_algorithm( &attributes, alg );
4072 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004073
Gilles Peskine049c7532019-05-15 20:22:09 +02004074 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004075 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004076
Ronald Cron5425a212020-08-04 14:58:35 +02004077 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004078 nonce->x, nonce->len,
4079 additional_data->x, additional_data->len,
4080 input_data->x, input_data->len,
4081 output_data, output_size,
4082 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004083
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004084 ASSERT_COMPARE( expected_result->x, expected_result->len,
4085 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004086
Gilles Peskinea1cac842018-06-11 19:33:02 +02004087exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004088 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004089 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004090 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004091}
4092/* END_CASE */
4093
4094/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004095void aead_decrypt( int key_type_arg, data_t *key_data,
4096 int alg_arg,
4097 data_t *nonce,
4098 data_t *additional_data,
4099 data_t *input_data,
4100 data_t *expected_data,
4101 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004102{
Ronald Cron5425a212020-08-04 14:58:35 +02004103 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004104 psa_key_type_t key_type = key_type_arg;
4105 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004106 unsigned char *output_data = NULL;
4107 size_t output_size = 0;
4108 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004109 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004111 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004112
Gilles Peskine003a4a92019-05-14 16:09:40 +02004113 output_size = input_data->len - tag_length;
4114 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4115 * should be exact. */
4116 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4117 TEST_EQUAL( output_size,
4118 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004119 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004120
Gilles Peskine8817f612018-12-18 00:18:46 +01004121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004122
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004123 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4124 psa_set_key_algorithm( &attributes, alg );
4125 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004126
Gilles Peskine049c7532019-05-15 20:22:09 +02004127 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004128 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004129
Ronald Cron5425a212020-08-04 14:58:35 +02004130 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004131 nonce->x, nonce->len,
4132 additional_data->x,
4133 additional_data->len,
4134 input_data->x, input_data->len,
4135 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004136 &output_length ),
4137 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004138
Gilles Peskine2d277862018-06-18 15:41:12 +02004139 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004140 ASSERT_COMPARE( expected_data->x, expected_data->len,
4141 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004142
Gilles Peskinea1cac842018-06-11 19:33:02 +02004143exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004144 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004145 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004146 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004147}
4148/* END_CASE */
4149
4150/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004151void signature_size( int type_arg,
4152 int bits,
4153 int alg_arg,
4154 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004155{
4156 psa_key_type_t type = type_arg;
4157 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004158 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004159
Gilles Peskinefe11b722018-12-18 00:24:04 +01004160 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004161#if defined(MBEDTLS_TEST_DEPRECATED)
4162 TEST_EQUAL( actual_size,
4163 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4164#endif /* MBEDTLS_TEST_DEPRECATED */
4165
Gilles Peskinee59236f2018-01-27 23:32:46 +01004166exit:
4167 ;
4168}
4169/* END_CASE */
4170
4171/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004172void sign_deterministic( int key_type_arg, data_t *key_data,
4173 int alg_arg, data_t *input_data,
4174 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004175{
Ronald Cron5425a212020-08-04 14:58:35 +02004176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004177 psa_key_type_t key_type = key_type_arg;
4178 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004179 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004180 unsigned char *signature = NULL;
4181 size_t signature_size;
4182 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004184
Gilles Peskine8817f612018-12-18 00:18:46 +01004185 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004186
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004188 psa_set_key_algorithm( &attributes, alg );
4189 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004190
Gilles Peskine049c7532019-05-15 20:22:09 +02004191 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004192 &key ) );
4193 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004194 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004195
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004196 /* Allocate a buffer which has the size advertized by the
4197 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004198 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004199 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004200 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004201 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004202 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004203
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004204 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004205 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004206 input_data->x, input_data->len,
4207 signature, signature_size,
4208 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004209 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004210 ASSERT_COMPARE( output_data->x, output_data->len,
4211 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004212
Gilles Peskine0627f982019-11-26 19:12:16 +01004213#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004214 memset( signature, 0, signature_size );
4215 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004216 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004217 input_data->x, input_data->len,
4218 signature, signature_size,
4219 &signature_length ) );
4220 ASSERT_COMPARE( output_data->x, output_data->len,
4221 signature, signature_length );
4222#endif /* MBEDTLS_TEST_DEPRECATED */
4223
Gilles Peskine20035e32018-02-03 22:44:14 +01004224exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004225 /*
4226 * Key attributes may have been returned by psa_get_key_attributes()
4227 * thus reset them as required.
4228 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004229 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004230
Ronald Cron5425a212020-08-04 14:58:35 +02004231 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004232 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004233 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004234}
4235/* END_CASE */
4236
4237/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004238void sign_fail( int key_type_arg, data_t *key_data,
4239 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004240 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004241{
Ronald Cron5425a212020-08-04 14:58:35 +02004242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004243 psa_key_type_t key_type = key_type_arg;
4244 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004245 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004246 psa_status_t actual_status;
4247 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004248 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004249 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004251
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004252 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004253
Gilles Peskine8817f612018-12-18 00:18:46 +01004254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004255
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004256 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004257 psa_set_key_algorithm( &attributes, alg );
4258 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004259
Gilles Peskine049c7532019-05-15 20:22:09 +02004260 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004261 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004262
Ronald Cron5425a212020-08-04 14:58:35 +02004263 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004264 input_data->x, input_data->len,
4265 signature, signature_size,
4266 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004267 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004268 /* The value of *signature_length is unspecified on error, but
4269 * whatever it is, it should be less than signature_size, so that
4270 * if the caller tries to read *signature_length bytes without
4271 * checking the error code then they don't overflow a buffer. */
4272 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004273
Gilles Peskine895242b2019-11-29 12:15:40 +01004274#if defined(MBEDTLS_TEST_DEPRECATED)
4275 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004276 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004277 input_data->x, input_data->len,
4278 signature, signature_size,
4279 &signature_length ),
4280 expected_status );
4281 TEST_ASSERT( signature_length <= signature_size );
4282#endif /* MBEDTLS_TEST_DEPRECATED */
4283
Gilles Peskine20035e32018-02-03 22:44:14 +01004284exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004285 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004286 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004287 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004288 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004289}
4290/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004291
4292/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004293void sign_verify( int key_type_arg, data_t *key_data,
4294 int alg_arg, data_t *input_data )
4295{
Ronald Cron5425a212020-08-04 14:58:35 +02004296 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004297 psa_key_type_t key_type = key_type_arg;
4298 psa_algorithm_t alg = alg_arg;
4299 size_t key_bits;
4300 unsigned char *signature = NULL;
4301 size_t signature_size;
4302 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004303 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004304
Gilles Peskine8817f612018-12-18 00:18:46 +01004305 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004306
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004307 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004308 psa_set_key_algorithm( &attributes, alg );
4309 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004310
Gilles Peskine049c7532019-05-15 20:22:09 +02004311 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004312 &key ) );
4313 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004314 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004315
4316 /* Allocate a buffer which has the size advertized by the
4317 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004318 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004319 key_bits, alg );
4320 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004321 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004322 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004323
4324 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004325 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004326 input_data->x, input_data->len,
4327 signature, signature_size,
4328 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004329 /* Check that the signature length looks sensible. */
4330 TEST_ASSERT( signature_length <= signature_size );
4331 TEST_ASSERT( signature_length > 0 );
4332
4333 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004334 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004335 input_data->x, input_data->len,
4336 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004337
4338 if( input_data->len != 0 )
4339 {
4340 /* Flip a bit in the input and verify that the signature is now
4341 * detected as invalid. Flip a bit at the beginning, not at the end,
4342 * because ECDSA may ignore the last few bits of the input. */
4343 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004344 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004345 input_data->x, input_data->len,
4346 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004347 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004348 }
4349
4350exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004351 /*
4352 * Key attributes may have been returned by psa_get_key_attributes()
4353 * thus reset them as required.
4354 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004355 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004356
Ronald Cron5425a212020-08-04 14:58:35 +02004357 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004358 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004359 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004360}
4361/* END_CASE */
4362
4363/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004364void asymmetric_verify( int key_type_arg, data_t *key_data,
4365 int alg_arg, data_t *hash_data,
4366 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004367{
Ronald Cron5425a212020-08-04 14:58:35 +02004368 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004369 psa_key_type_t key_type = key_type_arg;
4370 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004373 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004374
Gilles Peskine8817f612018-12-18 00:18:46 +01004375 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004376
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004377 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004378 psa_set_key_algorithm( &attributes, alg );
4379 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004380
Gilles Peskine049c7532019-05-15 20:22:09 +02004381 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004382 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004383
Ronald Cron5425a212020-08-04 14:58:35 +02004384 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004385 hash_data->x, hash_data->len,
4386 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004387
4388#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004389 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004390 hash_data->x, hash_data->len,
4391 signature_data->x,
4392 signature_data->len ) );
4393
4394#endif /* MBEDTLS_TEST_DEPRECATED */
4395
itayzafrir5c753392018-05-08 11:18:38 +03004396exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004397 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004398 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004399 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004400}
4401/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004402
4403/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004404void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4405 int alg_arg, data_t *hash_data,
4406 data_t *signature_data,
4407 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004408{
Ronald Cron5425a212020-08-04 14:58:35 +02004409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004410 psa_key_type_t key_type = key_type_arg;
4411 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004412 psa_status_t actual_status;
4413 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004415
Gilles Peskine8817f612018-12-18 00:18:46 +01004416 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004417
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004418 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004419 psa_set_key_algorithm( &attributes, alg );
4420 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004421
Gilles Peskine049c7532019-05-15 20:22:09 +02004422 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004423 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004424
Ronald Cron5425a212020-08-04 14:58:35 +02004425 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004426 hash_data->x, hash_data->len,
4427 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004428 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004429
Gilles Peskine895242b2019-11-29 12:15:40 +01004430#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004431 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004432 hash_data->x, hash_data->len,
4433 signature_data->x, signature_data->len ),
4434 expected_status );
4435#endif /* MBEDTLS_TEST_DEPRECATED */
4436
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004437exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004438 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004439 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004440 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004441}
4442/* END_CASE */
4443
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004444/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004445void asymmetric_encrypt( int key_type_arg,
4446 data_t *key_data,
4447 int alg_arg,
4448 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004449 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004450 int expected_output_length_arg,
4451 int expected_status_arg )
4452{
Ronald Cron5425a212020-08-04 14:58:35 +02004453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004454 psa_key_type_t key_type = key_type_arg;
4455 psa_algorithm_t alg = alg_arg;
4456 size_t expected_output_length = expected_output_length_arg;
4457 size_t key_bits;
4458 unsigned char *output = NULL;
4459 size_t output_size;
4460 size_t output_length = ~0;
4461 psa_status_t actual_status;
4462 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004464
Gilles Peskine8817f612018-12-18 00:18:46 +01004465 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004466
Gilles Peskine656896e2018-06-29 19:12:28 +02004467 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004468 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4469 psa_set_key_algorithm( &attributes, alg );
4470 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004471 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004472 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004473
4474 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004475 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004476 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004477 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004478 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004479
4480 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004481 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004482 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004483 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004484 output, output_size,
4485 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004486 TEST_EQUAL( actual_status, expected_status );
4487 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004488
Gilles Peskine68428122018-06-30 18:42:41 +02004489 /* If the label is empty, the test framework puts a non-null pointer
4490 * in label->x. Test that a null pointer works as well. */
4491 if( label->len == 0 )
4492 {
4493 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004494 if( output_size != 0 )
4495 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004496 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004497 input_data->x, input_data->len,
4498 NULL, label->len,
4499 output, output_size,
4500 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004501 TEST_EQUAL( actual_status, expected_status );
4502 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004503 }
4504
Gilles Peskine656896e2018-06-29 19:12:28 +02004505exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004506 /*
4507 * Key attributes may have been returned by psa_get_key_attributes()
4508 * thus reset them as required.
4509 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004510 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004511
Ronald Cron5425a212020-08-04 14:58:35 +02004512 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004513 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004514 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004515}
4516/* END_CASE */
4517
4518/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004519void asymmetric_encrypt_decrypt( int key_type_arg,
4520 data_t *key_data,
4521 int alg_arg,
4522 data_t *input_data,
4523 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004524{
Ronald Cron5425a212020-08-04 14:58:35 +02004525 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004526 psa_key_type_t key_type = key_type_arg;
4527 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004528 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004529 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004530 size_t output_size;
4531 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004532 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004533 size_t output2_size;
4534 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004536
Gilles Peskine8817f612018-12-18 00:18:46 +01004537 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004538
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004539 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4540 psa_set_key_algorithm( &attributes, alg );
4541 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004542
Gilles Peskine049c7532019-05-15 20:22:09 +02004543 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004544 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004545
4546 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004547 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004548 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004549 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004550 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004551 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004552 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004553
Gilles Peskineeebd7382018-06-08 18:11:54 +02004554 /* We test encryption by checking that encrypt-then-decrypt gives back
4555 * the original plaintext because of the non-optional random
4556 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004557 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004558 input_data->x, input_data->len,
4559 label->x, label->len,
4560 output, output_size,
4561 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004562 /* We don't know what ciphertext length to expect, but check that
4563 * it looks sensible. */
4564 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004565
Ronald Cron5425a212020-08-04 14:58:35 +02004566 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004567 output, output_length,
4568 label->x, label->len,
4569 output2, output2_size,
4570 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004571 ASSERT_COMPARE( input_data->x, input_data->len,
4572 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004573
4574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004575 /*
4576 * Key attributes may have been returned by psa_get_key_attributes()
4577 * thus reset them as required.
4578 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004579 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004580
Ronald Cron5425a212020-08-04 14:58:35 +02004581 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004582 mbedtls_free( output );
4583 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004584 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004585}
4586/* END_CASE */
4587
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004588/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004589void asymmetric_decrypt( int key_type_arg,
4590 data_t *key_data,
4591 int alg_arg,
4592 data_t *input_data,
4593 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004594 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004595{
Ronald Cron5425a212020-08-04 14:58:35 +02004596 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004597 psa_key_type_t key_type = key_type_arg;
4598 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004599 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004600 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004601 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004603
Jaeden Amero412654a2019-02-06 12:57:46 +00004604 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004605 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004606
Gilles Peskine8817f612018-12-18 00:18:46 +01004607 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004608
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004609 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4610 psa_set_key_algorithm( &attributes, alg );
4611 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004612
Gilles Peskine049c7532019-05-15 20:22:09 +02004613 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004614 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004615
Ronald Cron5425a212020-08-04 14:58:35 +02004616 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004617 input_data->x, input_data->len,
4618 label->x, label->len,
4619 output,
4620 output_size,
4621 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004622 ASSERT_COMPARE( expected_data->x, expected_data->len,
4623 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004624
Gilles Peskine68428122018-06-30 18:42:41 +02004625 /* If the label is empty, the test framework puts a non-null pointer
4626 * in label->x. Test that a null pointer works as well. */
4627 if( label->len == 0 )
4628 {
4629 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004630 if( output_size != 0 )
4631 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004632 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004633 input_data->x, input_data->len,
4634 NULL, label->len,
4635 output,
4636 output_size,
4637 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004638 ASSERT_COMPARE( expected_data->x, expected_data->len,
4639 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004640 }
4641
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004642exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004643 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004644 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004645 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004646 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004647}
4648/* END_CASE */
4649
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004650/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004651void asymmetric_decrypt_fail( int key_type_arg,
4652 data_t *key_data,
4653 int alg_arg,
4654 data_t *input_data,
4655 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004656 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004657 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004658{
Ronald Cron5425a212020-08-04 14:58:35 +02004659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004660 psa_key_type_t key_type = key_type_arg;
4661 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004662 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004663 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004664 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004665 psa_status_t actual_status;
4666 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004668
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004669 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004670
Gilles Peskine8817f612018-12-18 00:18:46 +01004671 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004672
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004673 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4674 psa_set_key_algorithm( &attributes, alg );
4675 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004676
Gilles Peskine049c7532019-05-15 20:22:09 +02004677 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004678 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004679
Ronald Cron5425a212020-08-04 14:58:35 +02004680 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004681 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004682 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004683 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004684 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004685 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004686 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004687
Gilles Peskine68428122018-06-30 18:42:41 +02004688 /* If the label is empty, the test framework puts a non-null pointer
4689 * in label->x. Test that a null pointer works as well. */
4690 if( label->len == 0 )
4691 {
4692 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004693 if( output_size != 0 )
4694 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004695 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004696 input_data->x, input_data->len,
4697 NULL, label->len,
4698 output, output_size,
4699 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004700 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004701 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004702 }
4703
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004704exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004705 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004706 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004707 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004708 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004709}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004710/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004711
4712/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004713void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004714{
4715 /* Test each valid way of initializing the object, except for `= {0}`, as
4716 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4717 * though it's OK by the C standard. We could test for this, but we'd need
4718 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004719 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004720 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4721 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4722 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004723
4724 memset( &zero, 0, sizeof( zero ) );
4725
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004727 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004728 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004729 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004730 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004731 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004732 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004733
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004735 PSA_ASSERT( psa_key_derivation_abort(&func) );
4736 PSA_ASSERT( psa_key_derivation_abort(&init) );
4737 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004738}
4739/* END_CASE */
4740
Janos Follath16de4a42019-06-13 16:32:24 +01004741/* BEGIN_CASE */
4742void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004743{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004744 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004745 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004747
Gilles Peskine8817f612018-12-18 00:18:46 +01004748 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004749
Janos Follath16de4a42019-06-13 16:32:24 +01004750 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004751 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004752
4753exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004754 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004755 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004756}
4757/* END_CASE */
4758
Janos Follathaf3c2a02019-06-12 12:34:34 +01004759/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004760void derive_set_capacity( int alg_arg, int capacity_arg,
4761 int expected_status_arg )
4762{
4763 psa_algorithm_t alg = alg_arg;
4764 size_t capacity = capacity_arg;
4765 psa_status_t expected_status = expected_status_arg;
4766 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4767
4768 PSA_ASSERT( psa_crypto_init( ) );
4769
4770 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4771
4772 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4773 expected_status );
4774
4775exit:
4776 psa_key_derivation_abort( &operation );
4777 PSA_DONE( );
4778}
4779/* END_CASE */
4780
4781/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004782void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004783 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004784 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004785 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004786 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004787 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004788 int expected_status_arg3,
4789 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004790{
4791 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004792 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4793 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004794 psa_status_t expected_statuses[] = {expected_status_arg1,
4795 expected_status_arg2,
4796 expected_status_arg3};
4797 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004798 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4799 MBEDTLS_SVC_KEY_ID_INIT,
4800 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004801 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4803 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004804 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004805 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004806 psa_status_t expected_output_status = expected_output_status_arg;
4807 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004808
4809 PSA_ASSERT( psa_crypto_init( ) );
4810
4811 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4812 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004813
4814 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4815
4816 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4817 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004818 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004819 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004820 psa_set_key_type( &attributes, key_types[i] );
4821 PSA_ASSERT( psa_import_key( &attributes,
4822 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004823 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004824 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4825 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4826 {
4827 // When taking a private key as secret input, use key agreement
4828 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004829 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004830 expected_statuses[i] );
4831 }
4832 else
4833 {
4834 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004835 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004836 expected_statuses[i] );
4837 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004838 }
4839 else
4840 {
4841 TEST_EQUAL( psa_key_derivation_input_bytes(
4842 &operation, steps[i],
4843 inputs[i]->x, inputs[i]->len ),
4844 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004845 }
4846 }
4847
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004848 if( output_key_type != PSA_KEY_TYPE_NONE )
4849 {
4850 psa_reset_key_attributes( &attributes );
4851 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4852 psa_set_key_bits( &attributes, 8 );
4853 actual_output_status =
4854 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004855 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004856 }
4857 else
4858 {
4859 uint8_t buffer[1];
4860 actual_output_status =
4861 psa_key_derivation_output_bytes( &operation,
4862 buffer, sizeof( buffer ) );
4863 }
4864 TEST_EQUAL( actual_output_status, expected_output_status );
4865
Janos Follathaf3c2a02019-06-12 12:34:34 +01004866exit:
4867 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004868 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4869 psa_destroy_key( keys[i] );
4870 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004871 PSA_DONE( );
4872}
4873/* END_CASE */
4874
Janos Follathd958bb72019-07-03 15:02:16 +01004875/* BEGIN_CASE */
4876void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004877{
Janos Follathd958bb72019-07-03 15:02:16 +01004878 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004879 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004880 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004882 unsigned char input1[] = "Input 1";
4883 size_t input1_length = sizeof( input1 );
4884 unsigned char input2[] = "Input 2";
4885 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004886 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004887 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004888 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4889 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4890 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004892
Gilles Peskine8817f612018-12-18 00:18:46 +01004893 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004894
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4896 psa_set_key_algorithm( &attributes, alg );
4897 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004898
Gilles Peskine73676cb2019-05-15 20:15:10 +02004899 PSA_ASSERT( psa_import_key( &attributes,
4900 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004901 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004902
4903 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004904 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004905 input1, input1_length,
4906 input2, input2_length,
4907 capacity ) )
4908 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004909
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004911 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004912 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004913
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004915
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004917 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004918
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004919exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004921 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004922 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004923}
4924/* END_CASE */
4925
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004926/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004927void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004928{
4929 uint8_t output_buffer[16];
4930 size_t buffer_size = 16;
4931 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004933
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004934 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4935 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004936 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004937
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004939 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004940
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004941 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004942
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004943 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4944 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004945 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004946
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004948 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004949
4950exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004951 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004952}
4953/* END_CASE */
4954
4955/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004956void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004957 int step1_arg, data_t *input1,
4958 int step2_arg, data_t *input2,
4959 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004960 int requested_capacity_arg,
4961 data_t *expected_output1,
4962 data_t *expected_output2 )
4963{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004964 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004965 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4966 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004967 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4968 MBEDTLS_SVC_KEY_ID_INIT,
4969 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004970 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004972 uint8_t *expected_outputs[2] =
4973 {expected_output1->x, expected_output2->x};
4974 size_t output_sizes[2] =
4975 {expected_output1->len, expected_output2->len};
4976 size_t output_buffer_size = 0;
4977 uint8_t *output_buffer = NULL;
4978 size_t expected_capacity;
4979 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004981 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004982 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004983
4984 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4985 {
4986 if( output_sizes[i] > output_buffer_size )
4987 output_buffer_size = output_sizes[i];
4988 if( output_sizes[i] == 0 )
4989 expected_outputs[i] = NULL;
4990 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004991 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004992 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004993
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4995 psa_set_key_algorithm( &attributes, alg );
4996 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004997
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004998 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004999 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5000 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5001 requested_capacity ) );
5002 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005003 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005004 switch( steps[i] )
5005 {
5006 case 0:
5007 break;
5008 case PSA_KEY_DERIVATION_INPUT_SECRET:
5009 PSA_ASSERT( psa_import_key( &attributes,
5010 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005011 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005012 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005013 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005014 break;
5015 default:
5016 PSA_ASSERT( psa_key_derivation_input_bytes(
5017 &operation, steps[i],
5018 inputs[i]->x, inputs[i]->len ) );
5019 break;
5020 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005022
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005024 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005025 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005026 expected_capacity = requested_capacity;
5027
5028 /* Expansion phase. */
5029 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5030 {
5031 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005033 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005034 if( expected_capacity == 0 && output_sizes[i] == 0 )
5035 {
5036 /* Reading 0 bytes when 0 bytes are available can go either way. */
5037 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005038 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005039 continue;
5040 }
5041 else if( expected_capacity == 0 ||
5042 output_sizes[i] > expected_capacity )
5043 {
5044 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005045 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005046 expected_capacity = 0;
5047 continue;
5048 }
5049 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005050 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005051 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005052 ASSERT_COMPARE( output_buffer, output_sizes[i],
5053 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005055 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005056 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005057 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005058 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005059 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005061
5062exit:
5063 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005065 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5066 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005067 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005068}
5069/* END_CASE */
5070
5071/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005072void derive_full( int alg_arg,
5073 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005074 data_t *input1,
5075 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005076 int requested_capacity_arg )
5077{
Ronald Cron5425a212020-08-04 14:58:35 +02005078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005079 psa_algorithm_t alg = alg_arg;
5080 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005082 unsigned char output_buffer[16];
5083 size_t expected_capacity = requested_capacity;
5084 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005086
Gilles Peskine8817f612018-12-18 00:18:46 +01005087 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005088
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005089 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5090 psa_set_key_algorithm( &attributes, alg );
5091 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005092
Gilles Peskine049c7532019-05-15 20:22:09 +02005093 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005094 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005095
Ronald Cron5425a212020-08-04 14:58:35 +02005096 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005097 input1->x, input1->len,
5098 input2->x, input2->len,
5099 requested_capacity ) )
5100 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005101
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005102 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005103 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005104 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005105
5106 /* Expansion phase. */
5107 while( current_capacity > 0 )
5108 {
5109 size_t read_size = sizeof( output_buffer );
5110 if( read_size > current_capacity )
5111 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005113 output_buffer,
5114 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005115 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005117 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005118 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005119 }
5120
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 /* Check that the operation refuses to go over capacity. */
5122 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005123 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005124
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005126
5127exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005129 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005130 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005131}
5132/* END_CASE */
5133
Janos Follathe60c9052019-07-03 13:51:30 +01005134/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005135void derive_key_exercise( int alg_arg,
5136 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005137 data_t *input1,
5138 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005139 int derived_type_arg,
5140 int derived_bits_arg,
5141 int derived_usage_arg,
5142 int derived_alg_arg )
5143{
Ronald Cron5425a212020-08-04 14:58:35 +02005144 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5145 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005146 psa_algorithm_t alg = alg_arg;
5147 psa_key_type_t derived_type = derived_type_arg;
5148 size_t derived_bits = derived_bits_arg;
5149 psa_key_usage_t derived_usage = derived_usage_arg;
5150 psa_algorithm_t derived_alg = derived_alg_arg;
5151 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005154 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005155
Gilles Peskine8817f612018-12-18 00:18:46 +01005156 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005157
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5159 psa_set_key_algorithm( &attributes, alg );
5160 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005162 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005163
5164 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005165 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005166 input1->x, input1->len,
5167 input2->x, input2->len, capacity ) )
5168 goto exit;
5169
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005170 psa_set_key_usage_flags( &attributes, derived_usage );
5171 psa_set_key_algorithm( &attributes, derived_alg );
5172 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005173 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005175 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005176
5177 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005178 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005179 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5180 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005181
5182 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005183 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005184 goto exit;
5185
5186exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005187 /*
5188 * Key attributes may have been returned by psa_get_key_attributes()
5189 * thus reset them as required.
5190 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005191 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005192
5193 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005194 psa_destroy_key( base_key );
5195 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005196 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005197}
5198/* END_CASE */
5199
Janos Follath42fd8882019-07-03 14:17:09 +01005200/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005201void derive_key_export( int alg_arg,
5202 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005203 data_t *input1,
5204 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005205 int bytes1_arg,
5206 int bytes2_arg )
5207{
Ronald Cron5425a212020-08-04 14:58:35 +02005208 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5209 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005210 psa_algorithm_t alg = alg_arg;
5211 size_t bytes1 = bytes1_arg;
5212 size_t bytes2 = bytes2_arg;
5213 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005215 uint8_t *output_buffer = NULL;
5216 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005217 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5218 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005219 size_t length;
5220
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005221 ASSERT_ALLOC( output_buffer, capacity );
5222 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005223 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005224
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005225 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5226 psa_set_key_algorithm( &base_attributes, alg );
5227 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005228 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005229 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005230
5231 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005232 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005233 input1->x, input1->len,
5234 input2->x, input2->len, capacity ) )
5235 goto exit;
5236
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005237 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005238 output_buffer,
5239 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005240 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005241
5242 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005243 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005244 input1->x, input1->len,
5245 input2->x, input2->len, capacity ) )
5246 goto exit;
5247
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005248 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5249 psa_set_key_algorithm( &derived_attributes, 0 );
5250 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005251 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005253 &derived_key ) );
5254 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005255 export_buffer, bytes1,
5256 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005257 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005258 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005259 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005261 &derived_key ) );
5262 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005263 export_buffer + bytes1, bytes2,
5264 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005265 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005266
5267 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005268 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5269 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005270
5271exit:
5272 mbedtls_free( output_buffer );
5273 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005275 psa_destroy_key( base_key );
5276 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005277 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005278}
5279/* END_CASE */
5280
5281/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005282void derive_key( int alg_arg,
5283 data_t *key_data, data_t *input1, data_t *input2,
5284 int type_arg, int bits_arg,
5285 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005286{
Ronald Cron5425a212020-08-04 14:58:35 +02005287 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5288 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005289 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005290 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005291 size_t bits = bits_arg;
5292 psa_status_t expected_status = expected_status_arg;
5293 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5294 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5295 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5296
5297 PSA_ASSERT( psa_crypto_init( ) );
5298
5299 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5300 psa_set_key_algorithm( &base_attributes, alg );
5301 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5302 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005303 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005304
Ronald Cron5425a212020-08-04 14:58:35 +02005305 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005306 input1->x, input1->len,
5307 input2->x, input2->len, SIZE_MAX ) )
5308 goto exit;
5309
5310 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5311 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005312 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005313 psa_set_key_bits( &derived_attributes, bits );
5314 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005315 &derived_key ),
Gilles Peskinec744d992019-07-30 17:26:54 +02005316 expected_status );
5317
5318exit:
5319 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005320 psa_destroy_key( base_key );
5321 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005322 PSA_DONE( );
5323}
5324/* END_CASE */
5325
5326/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005327void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005328 int our_key_type_arg, int our_key_alg_arg,
5329 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005330 int expected_status_arg )
5331{
Ronald Cron5425a212020-08-04 14:58:35 +02005332 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005333 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005334 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005335 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005338 psa_status_t expected_status = expected_status_arg;
5339 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005340
Gilles Peskine8817f612018-12-18 00:18:46 +01005341 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005342
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005343 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005344 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005345 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005346 PSA_ASSERT( psa_import_key( &attributes,
5347 our_key_data->x, our_key_data->len,
5348 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005349
Gilles Peskine77f40d82019-04-11 21:27:06 +02005350 /* The tests currently include inputs that should fail at either step.
5351 * Test cases that fail at the setup step should be changed to call
5352 * key_derivation_setup instead, and this function should be renamed
5353 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005355 if( status == PSA_SUCCESS )
5356 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005357 TEST_EQUAL( psa_key_derivation_key_agreement(
5358 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5359 our_key,
5360 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005361 expected_status );
5362 }
5363 else
5364 {
5365 TEST_ASSERT( status == expected_status );
5366 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005367
5368exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005369 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005370 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005371 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005372}
5373/* END_CASE */
5374
5375/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005376void raw_key_agreement( int alg_arg,
5377 int our_key_type_arg, data_t *our_key_data,
5378 data_t *peer_key_data,
5379 data_t *expected_output )
5380{
Ronald Cron5425a212020-08-04 14:58:35 +02005381 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005382 psa_algorithm_t alg = alg_arg;
5383 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005385 unsigned char *output = NULL;
5386 size_t output_length = ~0;
5387
5388 ASSERT_ALLOC( output, expected_output->len );
5389 PSA_ASSERT( psa_crypto_init( ) );
5390
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005391 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5392 psa_set_key_algorithm( &attributes, alg );
5393 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005394 PSA_ASSERT( psa_import_key( &attributes,
5395 our_key_data->x, our_key_data->len,
5396 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005397
Gilles Peskinebe697d82019-05-16 18:00:41 +02005398 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5399 peer_key_data->x, peer_key_data->len,
5400 output, expected_output->len,
5401 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005402 ASSERT_COMPARE( output, output_length,
5403 expected_output->x, expected_output->len );
5404
5405exit:
5406 mbedtls_free( output );
5407 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005408 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005409}
5410/* END_CASE */
5411
5412/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005413void key_agreement_capacity( int alg_arg,
5414 int our_key_type_arg, data_t *our_key_data,
5415 data_t *peer_key_data,
5416 int expected_capacity_arg )
5417{
Ronald Cron5425a212020-08-04 14:58:35 +02005418 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005419 psa_algorithm_t alg = alg_arg;
5420 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005421 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005423 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005424 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005425
Gilles Peskine8817f612018-12-18 00:18:46 +01005426 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005427
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005428 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5429 psa_set_key_algorithm( &attributes, alg );
5430 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005431 PSA_ASSERT( psa_import_key( &attributes,
5432 our_key_data->x, our_key_data->len,
5433 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005434
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005436 PSA_ASSERT( psa_key_derivation_key_agreement(
5437 &operation,
5438 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5439 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005440 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5441 {
5442 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005444 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005445 NULL, 0 ) );
5446 }
Gilles Peskine59685592018-09-18 12:11:34 +02005447
Gilles Peskinebf491972018-10-25 22:36:12 +02005448 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005449 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005450 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005451 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005452
Gilles Peskinebf491972018-10-25 22:36:12 +02005453 /* Test the actual capacity by reading the output. */
5454 while( actual_capacity > sizeof( output ) )
5455 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005456 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005457 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005458 actual_capacity -= sizeof( output );
5459 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005460 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005461 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005462 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005463 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005464
Gilles Peskine59685592018-09-18 12:11:34 +02005465exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005467 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005468 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005469}
5470/* END_CASE */
5471
5472/* BEGIN_CASE */
5473void key_agreement_output( int alg_arg,
5474 int our_key_type_arg, data_t *our_key_data,
5475 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005476 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005477{
Ronald Cron5425a212020-08-04 14:58:35 +02005478 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005479 psa_algorithm_t alg = alg_arg;
5480 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005481 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005483 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005484
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005485 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5486 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005487
Gilles Peskine8817f612018-12-18 00:18:46 +01005488 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005489
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005490 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5491 psa_set_key_algorithm( &attributes, alg );
5492 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005493 PSA_ASSERT( psa_import_key( &attributes,
5494 our_key_data->x, our_key_data->len,
5495 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005496
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005497 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005498 PSA_ASSERT( psa_key_derivation_key_agreement(
5499 &operation,
5500 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5501 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005502 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5503 {
5504 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005505 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005506 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005507 NULL, 0 ) );
5508 }
Gilles Peskine59685592018-09-18 12:11:34 +02005509
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005510 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005511 actual_output,
5512 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005513 ASSERT_COMPARE( actual_output, expected_output1->len,
5514 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005515 if( expected_output2->len != 0 )
5516 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005517 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005518 actual_output,
5519 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005520 ASSERT_COMPARE( actual_output, expected_output2->len,
5521 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005522 }
Gilles Peskine59685592018-09-18 12:11:34 +02005523
5524exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005526 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005527 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005528 mbedtls_free( actual_output );
5529}
5530/* END_CASE */
5531
5532/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005533void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005534{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005535 size_t bytes = bytes_arg;
5536 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005537 unsigned char *output = NULL;
5538 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005539 size_t i;
5540 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005541
Simon Butcher49f8e312020-03-03 15:51:50 +00005542 TEST_ASSERT( bytes_arg >= 0 );
5543
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005544 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5545 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005546 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005547
Gilles Peskine8817f612018-12-18 00:18:46 +01005548 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005549
Gilles Peskinea50d7392018-06-21 10:22:13 +02005550 /* Run several times, to ensure that every output byte will be
5551 * nonzero at least once with overwhelming probability
5552 * (2^(-8*number_of_runs)). */
5553 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005554 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005555 if( bytes != 0 )
5556 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005557 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005558
5559 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005560 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5561 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005562
5563 for( i = 0; i < bytes; i++ )
5564 {
5565 if( output[i] != 0 )
5566 ++changed[i];
5567 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005568 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005569
5570 /* Check that every byte was changed to nonzero at least once. This
5571 * validates that psa_generate_random is overwriting every byte of
5572 * the output buffer. */
5573 for( i = 0; i < bytes; i++ )
5574 {
5575 TEST_ASSERT( changed[i] != 0 );
5576 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005577
5578exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005579 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005580 mbedtls_free( output );
5581 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005582}
5583/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005584
5585/* BEGIN_CASE */
5586void generate_key( int type_arg,
5587 int bits_arg,
5588 int usage_arg,
5589 int alg_arg,
5590 int expected_status_arg )
5591{
Ronald Cron5425a212020-08-04 14:58:35 +02005592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005593 psa_key_type_t type = type_arg;
5594 psa_key_usage_t usage = usage_arg;
5595 size_t bits = bits_arg;
5596 psa_algorithm_t alg = alg_arg;
5597 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005599 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005600
Gilles Peskine8817f612018-12-18 00:18:46 +01005601 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005602
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005603 psa_set_key_usage_flags( &attributes, usage );
5604 psa_set_key_algorithm( &attributes, alg );
5605 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005606 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005607
5608 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005609 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005610 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005611 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005612
5613 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005614 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005615 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5616 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005617
Gilles Peskine818ca122018-06-20 18:16:48 +02005618 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005619 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005620 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005621
5622exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005623 /*
5624 * Key attributes may have been returned by psa_get_key_attributes()
5625 * thus reset them as required.
5626 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005627 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005628
Ronald Cron5425a212020-08-04 14:58:35 +02005629 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005630 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005631}
5632/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005633
Gilles Peskinee56e8782019-04-26 17:34:02 +02005634/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5635void generate_key_rsa( int bits_arg,
5636 data_t *e_arg,
5637 int expected_status_arg )
5638{
Ronald Cron5425a212020-08-04 14:58:35 +02005639 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005640 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005641 size_t bits = bits_arg;
5642 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5643 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5644 psa_status_t expected_status = expected_status_arg;
5645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5646 uint8_t *exported = NULL;
5647 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005648 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005649 size_t exported_length = SIZE_MAX;
5650 uint8_t *e_read_buffer = NULL;
5651 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005652 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005653 size_t e_read_length = SIZE_MAX;
5654
5655 if( e_arg->len == 0 ||
5656 ( e_arg->len == 3 &&
5657 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5658 {
5659 is_default_public_exponent = 1;
5660 e_read_size = 0;
5661 }
5662 ASSERT_ALLOC( e_read_buffer, e_read_size );
5663 ASSERT_ALLOC( exported, exported_size );
5664
5665 PSA_ASSERT( psa_crypto_init( ) );
5666
5667 psa_set_key_usage_flags( &attributes, usage );
5668 psa_set_key_algorithm( &attributes, alg );
5669 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5670 e_arg->x, e_arg->len ) );
5671 psa_set_key_bits( &attributes, bits );
5672
5673 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005674 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005675 if( expected_status != PSA_SUCCESS )
5676 goto exit;
5677
5678 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005679 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005680 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5681 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5682 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5683 e_read_buffer, e_read_size,
5684 &e_read_length ) );
5685 if( is_default_public_exponent )
5686 TEST_EQUAL( e_read_length, 0 );
5687 else
5688 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5689
5690 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005691 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005692 goto exit;
5693
5694 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005695 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005696 exported, exported_size,
5697 &exported_length ) );
5698 {
5699 uint8_t *p = exported;
5700 uint8_t *end = exported + exported_length;
5701 size_t len;
5702 /* RSAPublicKey ::= SEQUENCE {
5703 * modulus INTEGER, -- n
5704 * publicExponent INTEGER } -- e
5705 */
5706 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005707 MBEDTLS_ASN1_SEQUENCE |
5708 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005709 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5710 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5711 MBEDTLS_ASN1_INTEGER ) );
5712 if( len >= 1 && p[0] == 0 )
5713 {
5714 ++p;
5715 --len;
5716 }
5717 if( e_arg->len == 0 )
5718 {
5719 TEST_EQUAL( len, 3 );
5720 TEST_EQUAL( p[0], 1 );
5721 TEST_EQUAL( p[1], 0 );
5722 TEST_EQUAL( p[2], 1 );
5723 }
5724 else
5725 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5726 }
5727
5728exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005729 /*
5730 * Key attributes may have been returned by psa_get_key_attributes() or
5731 * set by psa_set_key_domain_parameters() thus reset them as required.
5732 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005733 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005734
Ronald Cron5425a212020-08-04 14:58:35 +02005735 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005736 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005737 mbedtls_free( e_read_buffer );
5738 mbedtls_free( exported );
5739}
5740/* END_CASE */
5741
Darryl Greend49a4992018-06-18 17:27:26 +01005742/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005743void persistent_key_load_key_from_storage( data_t *data,
5744 int type_arg, int bits_arg,
5745 int usage_flags_arg, int alg_arg,
5746 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005747{
Ronald Cron71016a92020-08-28 19:01:50 +02005748 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005749 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005750 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5751 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005752 psa_key_type_t type = type_arg;
5753 size_t bits = bits_arg;
5754 psa_key_usage_t usage_flags = usage_flags_arg;
5755 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005756 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005757 unsigned char *first_export = NULL;
5758 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005759 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005760 size_t first_exported_length;
5761 size_t second_exported_length;
5762
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005763 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5764 {
5765 ASSERT_ALLOC( first_export, export_size );
5766 ASSERT_ALLOC( second_export, export_size );
5767 }
Darryl Greend49a4992018-06-18 17:27:26 +01005768
Gilles Peskine8817f612018-12-18 00:18:46 +01005769 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005770
Gilles Peskinec87af662019-05-15 16:12:22 +02005771 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005772 psa_set_key_usage_flags( &attributes, usage_flags );
5773 psa_set_key_algorithm( &attributes, alg );
5774 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005775 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005776
Darryl Green0c6575a2018-11-07 16:05:30 +00005777 switch( generation_method )
5778 {
5779 case IMPORT_KEY:
5780 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005781 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005782 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005783 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005784
Darryl Green0c6575a2018-11-07 16:05:30 +00005785 case GENERATE_KEY:
5786 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005787 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005788 break;
5789
5790 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005791#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005792 {
5793 /* Create base key */
5794 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5795 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5796 psa_set_key_usage_flags( &base_attributes,
5797 PSA_KEY_USAGE_DERIVE );
5798 psa_set_key_algorithm( &base_attributes, derive_alg );
5799 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005800 PSA_ASSERT( psa_import_key( &base_attributes,
5801 data->x, data->len,
5802 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005803 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005804 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005805 PSA_ASSERT( psa_key_derivation_input_key(
5806 &operation,
5807 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005808 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005809 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005810 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005811 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5812 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005813 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005814 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005815 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005816 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005817 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005818#else
5819 TEST_ASSUME( ! "KDF not supported in this configuration" );
5820#endif
5821 break;
5822
5823 default:
5824 TEST_ASSERT( ! "generation_method not implemented in test" );
5825 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005826 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005827 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005828
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005829 /* Export the key if permitted by the key policy. */
5830 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5831 {
Ronald Cron5425a212020-08-04 14:58:35 +02005832 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005833 first_export, export_size,
5834 &first_exported_length ) );
5835 if( generation_method == IMPORT_KEY )
5836 ASSERT_COMPARE( data->x, data->len,
5837 first_export, first_exported_length );
5838 }
Darryl Greend49a4992018-06-18 17:27:26 +01005839
5840 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005841 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005842 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005843 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005844
Darryl Greend49a4992018-06-18 17:27:26 +01005845 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005846 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005847 TEST_ASSERT( mbedtls_svc_key_id_equal(
5848 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005849 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5850 PSA_KEY_LIFETIME_PERSISTENT );
5851 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5852 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5853 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5854 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005855
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005856 /* Export the key again if permitted by the key policy. */
5857 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005858 {
Ronald Cron5425a212020-08-04 14:58:35 +02005859 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005860 second_export, export_size,
5861 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005862 ASSERT_COMPARE( first_export, first_exported_length,
5863 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005864 }
5865
5866 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005867 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005868 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005869
5870exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005871 /*
5872 * Key attributes may have been returned by psa_get_key_attributes()
5873 * thus reset them as required.
5874 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005875 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005876
Darryl Greend49a4992018-06-18 17:27:26 +01005877 mbedtls_free( first_export );
5878 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005879 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005880 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005881 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005882 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005883}
5884/* END_CASE */