blob: a65c482a5fa51f44f54ad84839b26687515275a8 [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
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001031 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1032 psa_get_key_type( &attributes ),
1033 psa_get_key_bits( &attributes ) );
1034 ASSERT_ALLOC( exported, exported_size );
1035
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001036 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001038 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001039 TEST_EQUAL( psa_export_key( key, exported,
1040 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001041 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001042 ok = 1;
1043 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001044 }
1045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001047 exported, exported_size,
1048 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001049 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1050 psa_get_key_bits( &attributes ),
1051 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001052
1053exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001054 /*
1055 * Key attributes may have been returned by psa_get_key_attributes()
1056 * thus reset them as required.
1057 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001058 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001059
1060 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001061 return( ok );
1062}
1063
Ronald Cron5425a212020-08-04 14:58:35 +02001064static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001065{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001067 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001068 uint8_t *exported = NULL;
1069 size_t exported_size = 0;
1070 size_t exported_length = 0;
1071 int ok = 0;
1072
Ronald Cron5425a212020-08-04 14:58:35 +02001073 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001074 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001075 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001076 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1077 psa_get_key_type( &attributes ),
1078 psa_get_key_bits( &attributes ) );
1079 ASSERT_ALLOC( exported, exported_size );
1080
1081 TEST_EQUAL( psa_export_public_key( key, exported,
1082 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001083 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001084 ok = 1;
1085 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001086 }
1087
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001088 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001089 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001090 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1091 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001092 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001093
Ronald Cron5425a212020-08-04 14:58:35 +02001094 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001095 exported, exported_size,
1096 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001097 ok = exported_key_sanity_check( public_type,
1098 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001099 exported, exported_length );
1100
1101exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001102 /*
1103 * Key attributes may have been returned by psa_get_key_attributes()
1104 * thus reset them as required.
1105 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001106 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001107
1108 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001109 return( ok );
1110}
1111
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001112/** Do smoke tests on a key.
1113 *
1114 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1115 * sign/verify, or derivation) that is permitted according to \p usage.
1116 * \p usage and \p alg should correspond to the expected policy on the
1117 * key.
1118 *
1119 * Export the key if permitted by \p usage, and check that the output
1120 * looks sensible. If \p usage forbids export, check that
1121 * \p psa_export_key correctly rejects the attempt. If the key is
1122 * asymmetric, also check \p psa_export_public_key.
1123 *
1124 * If the key fails the tests, this function calls the test framework's
1125 * `test_fail` function and returns false. Otherwise this function returns
1126 * true. Therefore it should be used as follows:
1127 * ```
1128 * if( ! exercise_key( ... ) ) goto exit;
1129 * ```
1130 *
Ronald Cron5425a212020-08-04 14:58:35 +02001131 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001132 * \p alg.
1133 * \param usage The usage flags to assume.
1134 * \param alg The algorithm to exercise.
1135 *
1136 * \retval 0 The key failed the smoke tests.
1137 * \retval 1 The key passed the smoke tests.
1138 */
Ronald Cron5425a212020-08-04 14:58:35 +02001139static int exercise_key( mbedtls_svc_key_id_t key,
Gilles Peskine02b75072018-07-01 22:31:34 +02001140 psa_key_usage_t usage,
1141 psa_algorithm_t alg )
1142{
1143 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001144
Ronald Cron5425a212020-08-04 14:58:35 +02001145 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001146 return( 0 );
1147
Gilles Peskine02b75072018-07-01 22:31:34 +02001148 if( alg == 0 )
1149 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1150 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001151 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001152 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001153 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001154 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001155 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001156 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001157 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001158 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001159 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001160 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001161 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001162 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001163 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001164 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001165 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001166 else
1167 {
1168 char message[40];
1169 mbedtls_snprintf( message, sizeof( message ),
1170 "No code to exercise alg=0x%08lx",
1171 (unsigned long) alg );
1172 test_fail( message, __LINE__, __FILE__ );
1173 ok = 0;
1174 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001175
Ronald Cron5425a212020-08-04 14:58:35 +02001176 ok = ok && exercise_export_key( key, usage );
1177 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001178
Gilles Peskine02b75072018-07-01 22:31:34 +02001179 return( ok );
1180}
1181
Gilles Peskine10df3412018-10-25 22:35:43 +02001182static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1183 psa_algorithm_t alg )
1184{
1185 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1186 {
1187 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001188 PSA_KEY_USAGE_VERIFY_HASH :
1189 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001190 }
1191 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1192 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1193 {
1194 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1195 PSA_KEY_USAGE_ENCRYPT :
1196 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1197 }
1198 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1199 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1200 {
1201 return( PSA_KEY_USAGE_DERIVE );
1202 }
1203 else
1204 {
1205 return( 0 );
1206 }
1207
1208}
Darryl Green0c6575a2018-11-07 16:05:30 +00001209
Ronald Cron5425a212020-08-04 14:58:35 +02001210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001211{
1212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001214 uint8_t buffer[1];
1215 size_t length;
1216 int ok = 0;
1217
Ronald Cronecfb2372020-07-23 17:13:42 +02001218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001223 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001224 TEST_EQUAL(
1225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1226 TEST_EQUAL(
1227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1233
Ronald Cron5425a212020-08-04 14:58:35 +02001234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001235 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001237 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001238 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001239
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001240 ok = 1;
1241
1242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001243 /*
1244 * Key attributes may have been returned by psa_get_key_attributes()
1245 * thus reset them as required.
1246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001248
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001249 return( ok );
1250}
1251
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001252/* Assert that a key isn't reported as having a slot number. */
1253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1255 do \
1256 { \
1257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1258 TEST_EQUAL( psa_get_key_slot_number( \
1259 attributes, \
1260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1261 PSA_ERROR_INVALID_ARGUMENT ); \
1262 } \
1263 while( 0 )
1264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1266 ( (void) 0 )
1267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1268
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001269/* An overapproximation of the amount of storage needed for a key of the
1270 * given type and with the given content. The API doesn't make it easy
1271 * to find a good value for the size. The current implementation doesn't
1272 * care about the value anyway. */
1273#define KEY_BITS_FROM_DATA( type, data ) \
1274 ( data )->len
1275
Darryl Green0c6575a2018-11-07 16:05:30 +00001276typedef enum {
1277 IMPORT_KEY = 0,
1278 GENERATE_KEY = 1,
1279 DERIVE_KEY = 2
1280} generate_method;
1281
Gilles Peskinee59236f2018-01-27 23:32:46 +01001282/* END_HEADER */
1283
1284/* BEGIN_DEPENDENCIES
1285 * depends_on:MBEDTLS_PSA_CRYPTO_C
1286 * END_DEPENDENCIES
1287 */
1288
1289/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001290void static_checks( )
1291{
1292 size_t max_truncated_mac_size =
1293 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1294
1295 /* Check that the length for a truncated MAC always fits in the algorithm
1296 * encoding. The shifted mask is the maximum truncated value. The
1297 * untruncated algorithm may be one byte larger. */
1298 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001299
1300#if defined(MBEDTLS_TEST_DEPRECATED)
1301 /* Check deprecated constants. */
1302 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1303 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1304 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1305 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1306 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1307 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1308 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1309 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001310
Paul Elliott8ff510a2020-06-02 17:19:28 +01001311 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1312 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1314 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1315 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1316 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1317 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1318 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1319 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1320 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1321 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1322 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1323 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1324 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1325 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1326 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1327 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1328 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1329 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1330 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1331 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1332 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1333 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1334 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1335 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1336 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1337 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1338 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1339 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1340 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1341
1342 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1343 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1344 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1345 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1346 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1347 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1348 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1349 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001350
Paul Elliott75e27032020-06-03 15:17:39 +01001351 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1352 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1353 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1354 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1355 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1356
1357 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1358 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001359#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001360}
1361/* END_CASE */
1362
1363/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001364void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001365 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001366 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001367{
Gilles Peskine4747d192019-04-17 15:05:45 +02001368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001369 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001370 psa_key_lifetime_t lifetime = lifetime_arg;
1371 psa_key_usage_t usage_flags = usage_flags_arg;
1372 psa_algorithm_t alg = alg_arg;
1373 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001374 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001375
Ronald Cronecfb2372020-07-23 17:13:42 +02001376 TEST_EQUAL(
1377 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1378 TEST_EQUAL(
1379 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001380 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1381 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1382 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1383 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001384 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001385
Gilles Peskinec87af662019-05-15 16:12:22 +02001386 psa_set_key_id( &attributes, id );
1387 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001388 psa_set_key_usage_flags( &attributes, usage_flags );
1389 psa_set_key_algorithm( &attributes, alg );
1390 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001391 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001392
Ronald Cronecfb2372020-07-23 17:13:42 +02001393 TEST_ASSERT( mbedtls_svc_key_id_equal(
1394 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001395 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1396 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1397 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1398 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001399 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001400
1401 psa_reset_key_attributes( &attributes );
1402
Ronald Cronecfb2372020-07-23 17:13:42 +02001403 TEST_EQUAL(
1404 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1405 TEST_EQUAL(
1406 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001407 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1408 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1409 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1410 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001411 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001412}
1413/* END_CASE */
1414
1415/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001416void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1417 int id2_arg, int owner_id2_arg,
1418 int expected_id_arg, int expected_owner_id_arg,
1419 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001420{
1421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001422 mbedtls_svc_key_id_t id1 =
1423 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001424 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001425 mbedtls_svc_key_id_t id2 =
1426 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001427 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001428 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001429 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1430
1431 if( id1_arg != -1 )
1432 psa_set_key_id( &attributes, id1 );
1433 if( lifetime_arg != -1 )
1434 psa_set_key_lifetime( &attributes, lifetime );
1435 if( id2_arg != -1 )
1436 psa_set_key_id( &attributes, id2 );
1437
Ronald Cronecfb2372020-07-23 17:13:42 +02001438 TEST_ASSERT( mbedtls_svc_key_id_equal(
1439 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001440 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1441}
1442/* END_CASE */
1443
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001444/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1445void slot_number_attribute( )
1446{
1447 psa_key_slot_number_t slot_number = 0xdeadbeef;
1448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1449
1450 /* Initially, there is no slot number. */
1451 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1452 PSA_ERROR_INVALID_ARGUMENT );
1453
1454 /* Test setting a slot number. */
1455 psa_set_key_slot_number( &attributes, 0 );
1456 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1457 TEST_EQUAL( slot_number, 0 );
1458
1459 /* Test changing the slot number. */
1460 psa_set_key_slot_number( &attributes, 42 );
1461 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1462 TEST_EQUAL( slot_number, 42 );
1463
1464 /* Test clearing the slot number. */
1465 psa_clear_key_slot_number( &attributes );
1466 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1467 PSA_ERROR_INVALID_ARGUMENT );
1468
1469 /* Clearing again should have no effect. */
1470 psa_clear_key_slot_number( &attributes );
1471 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1472 PSA_ERROR_INVALID_ARGUMENT );
1473
1474 /* Test that reset clears the slot number. */
1475 psa_set_key_slot_number( &attributes, 42 );
1476 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1477 TEST_EQUAL( slot_number, 42 );
1478 psa_reset_key_attributes( &attributes );
1479 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1480 PSA_ERROR_INVALID_ARGUMENT );
1481}
1482/* END_CASE */
1483
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001484/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001485void import_with_policy( int type_arg,
1486 int usage_arg, int alg_arg,
1487 int expected_status_arg )
1488{
1489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1490 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001491 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001492 psa_key_type_t type = type_arg;
1493 psa_key_usage_t usage = usage_arg;
1494 psa_algorithm_t alg = alg_arg;
1495 psa_status_t expected_status = expected_status_arg;
1496 const uint8_t key_material[16] = {0};
1497 psa_status_t status;
1498
1499 PSA_ASSERT( psa_crypto_init( ) );
1500
1501 psa_set_key_type( &attributes, type );
1502 psa_set_key_usage_flags( &attributes, usage );
1503 psa_set_key_algorithm( &attributes, alg );
1504
1505 status = psa_import_key( &attributes,
1506 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001507 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001508 TEST_EQUAL( status, expected_status );
1509 if( status != PSA_SUCCESS )
1510 goto exit;
1511
Ronald Cron5425a212020-08-04 14:58:35 +02001512 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001513 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1514 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1515 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001516 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001517
Ronald Cron5425a212020-08-04 14:58:35 +02001518 PSA_ASSERT( psa_destroy_key( key ) );
1519 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001520
1521exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001522 /*
1523 * Key attributes may have been returned by psa_get_key_attributes()
1524 * thus reset them as required.
1525 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001526 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001527
1528 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001529 PSA_DONE( );
1530}
1531/* END_CASE */
1532
1533/* BEGIN_CASE */
1534void import_with_data( data_t *data, int type_arg,
1535 int attr_bits_arg,
1536 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001537{
1538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1539 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001540 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001541 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001542 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001543 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545
Gilles Peskine8817f612018-12-18 00:18:46 +01001546 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547
Gilles Peskine4747d192019-04-17 15:05:45 +02001548 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001549 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001550
Ronald Cron5425a212020-08-04 14:58:35 +02001551 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001552 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001553 if( status != PSA_SUCCESS )
1554 goto exit;
1555
Ronald Cron5425a212020-08-04 14:58:35 +02001556 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001557 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001558 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001559 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001560 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001561
Ronald Cron5425a212020-08-04 14:58:35 +02001562 PSA_ASSERT( psa_destroy_key( key ) );
1563 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564
1565exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001566 /*
1567 * Key attributes may have been returned by psa_get_key_attributes()
1568 * thus reset them as required.
1569 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001570 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001571
1572 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001573 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574}
1575/* END_CASE */
1576
1577/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001578void import_large_key( int type_arg, int byte_size_arg,
1579 int expected_status_arg )
1580{
1581 psa_key_type_t type = type_arg;
1582 size_t byte_size = byte_size_arg;
1583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1584 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001585 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001586 psa_status_t status;
1587 uint8_t *buffer = NULL;
1588 size_t buffer_size = byte_size + 1;
1589 size_t n;
1590
1591 /* It would be better to skip the test than fail it if the allocation
1592 * fails, but the test framework doesn't support this yet. */
1593 ASSERT_ALLOC( buffer, buffer_size );
1594 memset( buffer, 'K', byte_size );
1595
1596 PSA_ASSERT( psa_crypto_init( ) );
1597
1598 /* Try importing the key */
1599 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1600 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001601 status = psa_import_key( &attributes, buffer, byte_size, &key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001602 TEST_EQUAL( status, expected_status );
1603
1604 if( status == PSA_SUCCESS )
1605 {
Ronald Cron5425a212020-08-04 14:58:35 +02001606 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001607 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1608 TEST_EQUAL( psa_get_key_bits( &attributes ),
1609 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001610 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001611 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001612 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001613 for( n = 0; n < byte_size; n++ )
1614 TEST_EQUAL( buffer[n], 'K' );
1615 for( n = byte_size; n < buffer_size; n++ )
1616 TEST_EQUAL( buffer[n], 0 );
1617 }
1618
1619exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001620 /*
1621 * Key attributes may have been returned by psa_get_key_attributes()
1622 * thus reset them as required.
1623 */
1624 psa_reset_key_attributes( &attributes );
1625
Ronald Cron5425a212020-08-04 14:58:35 +02001626 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001627 PSA_DONE( );
1628 mbedtls_free( buffer );
1629}
1630/* END_CASE */
1631
1632/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001633void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1634{
Ronald Cron5425a212020-08-04 14:58:35 +02001635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001636 size_t bits = bits_arg;
1637 psa_status_t expected_status = expected_status_arg;
1638 psa_status_t status;
1639 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001640 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001641 size_t buffer_size = /* Slight overapproximations */
1642 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001643 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001644 unsigned char *p;
1645 int ret;
1646 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001648
Gilles Peskine8817f612018-12-18 00:18:46 +01001649 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001650 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001651
1652 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1653 bits, keypair ) ) >= 0 );
1654 length = ret;
1655
1656 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001657 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001658 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001659 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001660
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001661 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001662 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001663
1664exit:
1665 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001666 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001667}
1668/* END_CASE */
1669
1670/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001671void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001672 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001673 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001674 int expected_bits,
1675 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001676 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001677 int canonical_input )
1678{
Ronald Cron5425a212020-08-04 14:58:35 +02001679 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001680 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001681 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001682 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001683 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001684 unsigned char *exported = NULL;
1685 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001686 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001687 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001688 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001689 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001690 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001691
Moran Pekercb088e72018-07-17 17:36:59 +03001692 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001693 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001694 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001695 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001696 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001697
Gilles Peskine4747d192019-04-17 15:05:45 +02001698 psa_set_key_usage_flags( &attributes, usage_arg );
1699 psa_set_key_algorithm( &attributes, alg );
1700 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001701
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001702 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001703 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001704
1705 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001706 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001707 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1708 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001709 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001710
1711 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001712 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001713 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001714
1715 /* The exported length must be set by psa_export_key() to a value between 0
1716 * and export_size. On errors, the exported length must be 0. */
1717 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1718 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1719 TEST_ASSERT( exported_length <= export_size );
1720
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001721 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001722 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001723 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001724 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001725 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001726 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001727 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001728
Ronald Cron5425a212020-08-04 14:58:35 +02001729 if( ! exercise_export_key( key, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001730 goto exit;
1731
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001732 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001733 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001734 else
1735 {
Ronald Cron5425a212020-08-04 14:58:35 +02001736 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001737 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001738 &key2 ) );
1739 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001740 reexported,
1741 export_size,
1742 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001743 ASSERT_COMPARE( exported, exported_length,
1744 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001745 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001746 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001747 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001748
1749destroy:
1750 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001751 PSA_ASSERT( psa_destroy_key( key ) );
1752 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001753
1754exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001755 /*
1756 * Key attributes may have been returned by psa_get_key_attributes()
1757 * thus reset them as required.
1758 */
1759 psa_reset_key_attributes( &got_attributes );
1760
itayzafrir3e02b3b2018-06-12 17:06:52 +03001761 mbedtls_free( exported );
1762 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001763 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001764}
1765/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001766
Moran Pekerf709f4a2018-06-06 17:26:04 +03001767/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001768void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001769 int type_arg,
1770 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001771 int export_size_delta,
1772 int expected_export_status_arg,
1773 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001774{
Ronald Cron5425a212020-08-04 14:58:35 +02001775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001776 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001777 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001778 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001779 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001780 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001781 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001782 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001783 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001784
Gilles Peskine8817f612018-12-18 00:18:46 +01001785 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001786
Gilles Peskine4747d192019-04-17 15:05:45 +02001787 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1788 psa_set_key_algorithm( &attributes, alg );
1789 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001790
1791 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001792 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001793
Gilles Peskine49c25912018-10-29 15:15:31 +01001794 /* Export the public key */
1795 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001796 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001797 exported, export_size,
1798 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001799 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001800 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001801 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001802 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001803 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001804 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001805 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001806 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001807 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001808 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1809 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001810 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001811
1812exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001813 /*
1814 * Key attributes may have been returned by psa_get_key_attributes()
1815 * thus reset them as required.
1816 */
1817 psa_reset_key_attributes( &attributes );
1818
itayzafrir3e02b3b2018-06-12 17:06:52 +03001819 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001820 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001821 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001822}
1823/* END_CASE */
1824
Gilles Peskine20035e32018-02-03 22:44:14 +01001825/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001826void import_and_exercise_key( data_t *data,
1827 int type_arg,
1828 int bits_arg,
1829 int alg_arg )
1830{
Ronald Cron5425a212020-08-04 14:58:35 +02001831 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001832 psa_key_type_t type = type_arg;
1833 size_t bits = bits_arg;
1834 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001835 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001837 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001838
Gilles Peskine8817f612018-12-18 00:18:46 +01001839 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001840
Gilles Peskine4747d192019-04-17 15:05:45 +02001841 psa_set_key_usage_flags( &attributes, usage );
1842 psa_set_key_algorithm( &attributes, alg );
1843 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001844
1845 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001846 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001847
1848 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001849 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001850 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1851 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001852
1853 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02001854 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001855 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001856
Ronald Cron5425a212020-08-04 14:58:35 +02001857 PSA_ASSERT( psa_destroy_key( key ) );
1858 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001859
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001860exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001861 /*
1862 * Key attributes may have been returned by psa_get_key_attributes()
1863 * thus reset them as required.
1864 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001865 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001866
1867 psa_reset_key_attributes( &attributes );
1868 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001869 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001870}
1871/* END_CASE */
1872
1873/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001874void effective_key_attributes( int type_arg, int expected_type_arg,
1875 int bits_arg, int expected_bits_arg,
1876 int usage_arg, int expected_usage_arg,
1877 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001878{
Ronald Cron5425a212020-08-04 14:58:35 +02001879 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001880 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001881 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001882 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001883 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001884 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001885 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001886 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001887 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001889
Gilles Peskine8817f612018-12-18 00:18:46 +01001890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001891
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001892 psa_set_key_usage_flags( &attributes, usage );
1893 psa_set_key_algorithm( &attributes, alg );
1894 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001895 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001896
Ronald Cron5425a212020-08-04 14:58:35 +02001897 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001898 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001899
Ronald Cron5425a212020-08-04 14:58:35 +02001900 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001901 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1902 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1903 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1904 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001905
1906exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001907 /*
1908 * Key attributes may have been returned by psa_get_key_attributes()
1909 * thus reset them as required.
1910 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001911 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001912
1913 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001914 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001915}
1916/* END_CASE */
1917
1918/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001919void check_key_policy( int type_arg, int bits_arg,
1920 int usage_arg, int alg_arg )
1921{
1922 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1923 usage_arg, usage_arg, alg_arg, alg_arg );
1924 goto exit;
1925}
1926/* END_CASE */
1927
1928/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001929void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001930{
1931 /* Test each valid way of initializing the object, except for `= {0}`, as
1932 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1933 * though it's OK by the C standard. We could test for this, but we'd need
1934 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001935 psa_key_attributes_t func = psa_key_attributes_init( );
1936 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1937 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001938
1939 memset( &zero, 0, sizeof( zero ) );
1940
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001941 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1942 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1943 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001944
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001945 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1946 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1947 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1948
1949 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1950 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1951 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1952
1953 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1954 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1955 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1956
1957 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1958 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1959 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001960}
1961/* END_CASE */
1962
1963/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001964void mac_key_policy( int policy_usage,
1965 int policy_alg,
1966 int key_type,
1967 data_t *key_data,
1968 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001969{
Ronald Cron5425a212020-08-04 14:58:35 +02001970 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001971 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001972 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001973 psa_status_t status;
1974 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001975
Gilles Peskine8817f612018-12-18 00:18:46 +01001976 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001977
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001978 psa_set_key_usage_flags( &attributes, policy_usage );
1979 psa_set_key_algorithm( &attributes, policy_alg );
1980 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001981
Gilles Peskine049c7532019-05-15 20:22:09 +02001982 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001983 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001984
Ronald Cron5425a212020-08-04 14:58:35 +02001985 status = psa_mac_sign_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_SIGN_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 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001992
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001994 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001996 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001997 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001998 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001999 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000
2001exit:
2002 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002003 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002004 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005}
2006/* END_CASE */
2007
2008/* BEGIN_CASE */
2009void cipher_key_policy( int policy_usage,
2010 int policy_alg,
2011 int key_type,
2012 data_t *key_data,
2013 int exercise_alg )
2014{
Ronald Cron5425a212020-08-04 14:58:35 +02002015 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002017 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018 psa_status_t status;
2019
Gilles Peskine8817f612018-12-18 00:18:46 +01002020 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002022 psa_set_key_usage_flags( &attributes, policy_usage );
2023 psa_set_key_algorithm( &attributes, policy_alg );
2024 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025
Gilles Peskine049c7532019-05-15 20:22:09 +02002026 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002027 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028
Ronald Cron5425a212020-08-04 14:58:35 +02002029 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 if( policy_alg == exercise_alg &&
2031 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002032 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002034 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035 psa_cipher_abort( &operation );
2036
Ronald Cron5425a212020-08-04 14:58:35 +02002037 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038 if( policy_alg == exercise_alg &&
2039 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002040 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002042 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
2044exit:
2045 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002046 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002047 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002048}
2049/* END_CASE */
2050
2051/* BEGIN_CASE */
2052void aead_key_policy( int policy_usage,
2053 int policy_alg,
2054 int key_type,
2055 data_t *key_data,
2056 int nonce_length_arg,
2057 int tag_length_arg,
2058 int exercise_alg )
2059{
Ronald Cron5425a212020-08-04 14:58:35 +02002060 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002062 psa_status_t status;
2063 unsigned char nonce[16] = {0};
2064 size_t nonce_length = nonce_length_arg;
2065 unsigned char tag[16];
2066 size_t tag_length = tag_length_arg;
2067 size_t output_length;
2068
2069 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
2070 TEST_ASSERT( tag_length <= sizeof( tag ) );
2071
Gilles Peskine8817f612018-12-18 00:18:46 +01002072 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002074 psa_set_key_usage_flags( &attributes, policy_usage );
2075 psa_set_key_algorithm( &attributes, policy_alg );
2076 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077
Gilles Peskine049c7532019-05-15 20:22:09 +02002078 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002079 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002080
Ronald Cron5425a212020-08-04 14:58:35 +02002081 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002082 nonce, nonce_length,
2083 NULL, 0,
2084 NULL, 0,
2085 tag, tag_length,
2086 &output_length );
2087 if( policy_alg == exercise_alg &&
2088 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002089 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002091 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002092
2093 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002094 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002095 nonce, nonce_length,
2096 NULL, 0,
2097 tag, tag_length,
2098 NULL, 0,
2099 &output_length );
2100 if( policy_alg == exercise_alg &&
2101 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002102 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002104 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002105
2106exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002107 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002108 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109}
2110/* END_CASE */
2111
2112/* BEGIN_CASE */
2113void asymmetric_encryption_key_policy( int policy_usage,
2114 int policy_alg,
2115 int key_type,
2116 data_t *key_data,
2117 int exercise_alg )
2118{
Ronald Cron5425a212020-08-04 14:58:35 +02002119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121 psa_status_t status;
2122 size_t key_bits;
2123 size_t buffer_length;
2124 unsigned char *buffer = NULL;
2125 size_t output_length;
2126
Gilles Peskine8817f612018-12-18 00:18:46 +01002127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002129 psa_set_key_usage_flags( &attributes, policy_usage );
2130 psa_set_key_algorithm( &attributes, policy_alg );
2131 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132
Gilles Peskine049c7532019-05-15 20:22:09 +02002133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002134 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002135
Ronald Cron5425a212020-08-04 14:58:35 +02002136 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002137 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002138 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2139 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002140 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002141
Ronald Cron5425a212020-08-04 14:58:35 +02002142 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143 NULL, 0,
2144 NULL, 0,
2145 buffer, buffer_length,
2146 &output_length );
2147 if( policy_alg == exercise_alg &&
2148 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002149 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002150 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002151 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002152
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002153 if( buffer_length != 0 )
2154 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002155 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156 buffer, buffer_length,
2157 NULL, 0,
2158 buffer, buffer_length,
2159 &output_length );
2160 if( policy_alg == exercise_alg &&
2161 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002162 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002163 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002164 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002165
2166exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002167 /*
2168 * Key attributes may have been returned by psa_get_key_attributes()
2169 * thus reset them as required.
2170 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002171 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002172
2173 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002174 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002175 mbedtls_free( buffer );
2176}
2177/* END_CASE */
2178
2179/* BEGIN_CASE */
2180void asymmetric_signature_key_policy( int policy_usage,
2181 int policy_alg,
2182 int key_type,
2183 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002184 int exercise_alg,
2185 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002186{
Ronald Cron5425a212020-08-04 14:58:35 +02002187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002189 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002190 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2191 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2192 * compatible with the policy and `payload_length_arg` is supposed to be
2193 * a valid input length to sign. If `payload_length_arg <= 0`,
2194 * `exercise_alg` is supposed to be forbidden by the policy. */
2195 int compatible_alg = payload_length_arg > 0;
2196 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002197 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002198 size_t signature_length;
2199
Gilles Peskine8817f612018-12-18 00:18:46 +01002200 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002201
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002202 psa_set_key_usage_flags( &attributes, policy_usage );
2203 psa_set_key_algorithm( &attributes, policy_alg );
2204 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002205
Gilles Peskine049c7532019-05-15 20:22:09 +02002206 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002207 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208
Ronald Cron5425a212020-08-04 14:58:35 +02002209 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002210 payload, payload_length,
2211 signature, sizeof( signature ),
2212 &signature_length );
2213 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002214 PSA_ASSERT( status );
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 Peskine76f5c7b2018-07-06 16:53:09 +02002217
2218 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002219 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002220 payload, payload_length,
2221 signature, sizeof( signature ) );
2222 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002223 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002224 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002225 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002226
2227exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002228 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002229 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002230}
2231/* END_CASE */
2232
Janos Follathba3fab92019-06-11 14:50:16 +01002233/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002234void derive_key_policy( int policy_usage,
2235 int policy_alg,
2236 int key_type,
2237 data_t *key_data,
2238 int exercise_alg )
2239{
Ronald Cron5425a212020-08-04 14:58:35 +02002240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002242 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002243 psa_status_t status;
2244
Gilles Peskine8817f612018-12-18 00:18:46 +01002245 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002247 psa_set_key_usage_flags( &attributes, policy_usage );
2248 psa_set_key_algorithm( &attributes, policy_alg );
2249 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002250
Gilles Peskine049c7532019-05-15 20:22:09 +02002251 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002252 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002253
Janos Follathba3fab92019-06-11 14:50:16 +01002254 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2255
2256 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2257 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002258 {
Janos Follathba3fab92019-06-11 14:50:16 +01002259 PSA_ASSERT( psa_key_derivation_input_bytes(
2260 &operation,
2261 PSA_KEY_DERIVATION_INPUT_SEED,
2262 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002263 }
Janos Follathba3fab92019-06-11 14:50:16 +01002264
2265 status = psa_key_derivation_input_key( &operation,
2266 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002267 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002268
Gilles Peskineea0fb492018-07-12 17:17:20 +02002269 if( policy_alg == exercise_alg &&
2270 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002271 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002272 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002273 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002274
2275exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002276 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002277 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002278 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002279}
2280/* END_CASE */
2281
2282/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002283void agreement_key_policy( int policy_usage,
2284 int policy_alg,
2285 int key_type_arg,
2286 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002287 int exercise_alg,
2288 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002289{
Ronald Cron5425a212020-08-04 14:58:35 +02002290 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002291 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002292 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002293 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002294 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002295 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296
Gilles Peskine8817f612018-12-18 00:18:46 +01002297 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002298
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002299 psa_set_key_usage_flags( &attributes, policy_usage );
2300 psa_set_key_algorithm( &attributes, policy_alg );
2301 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002302
Gilles Peskine049c7532019-05-15 20:22:09 +02002303 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002304 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002305
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002306 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002307 status = key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002308
Steven Cooremance48e852020-10-05 16:02:45 +02002309 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002310
2311exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002312 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002313 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002314 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002315}
2316/* END_CASE */
2317
2318/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002319void key_policy_alg2( int key_type_arg, data_t *key_data,
2320 int usage_arg, int alg_arg, int alg2_arg )
2321{
Ronald Cron5425a212020-08-04 14:58:35 +02002322 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002323 psa_key_type_t key_type = key_type_arg;
2324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2325 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2326 psa_key_usage_t usage = usage_arg;
2327 psa_algorithm_t alg = alg_arg;
2328 psa_algorithm_t alg2 = alg2_arg;
2329
2330 PSA_ASSERT( psa_crypto_init( ) );
2331
2332 psa_set_key_usage_flags( &attributes, usage );
2333 psa_set_key_algorithm( &attributes, alg );
2334 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2335 psa_set_key_type( &attributes, key_type );
2336 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002337 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002338
Ronald Cron5425a212020-08-04 14:58:35 +02002339 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002340 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2341 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2342 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2343
Ronald Cron5425a212020-08-04 14:58:35 +02002344 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002345 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002346 if( ! exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002347 goto exit;
2348
2349exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002350 /*
2351 * Key attributes may have been returned by psa_get_key_attributes()
2352 * thus reset them as required.
2353 */
2354 psa_reset_key_attributes( &got_attributes );
2355
Ronald Cron5425a212020-08-04 14:58:35 +02002356 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002357 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002358}
2359/* END_CASE */
2360
2361/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002362void raw_agreement_key_policy( int policy_usage,
2363 int policy_alg,
2364 int key_type_arg,
2365 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002366 int exercise_alg,
2367 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002368{
Ronald Cron5425a212020-08-04 14:58:35 +02002369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002371 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002372 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002373 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002374 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375
2376 PSA_ASSERT( psa_crypto_init( ) );
2377
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002378 psa_set_key_usage_flags( &attributes, policy_usage );
2379 psa_set_key_algorithm( &attributes, policy_alg );
2380 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002381
Gilles Peskine049c7532019-05-15 20:22:09 +02002382 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002383 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002384
Ronald Cron5425a212020-08-04 14:58:35 +02002385 status = raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002386
Steven Cooremance48e852020-10-05 16:02:45 +02002387 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388
2389exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002390 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002391 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002392 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002393}
2394/* END_CASE */
2395
2396/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002397void copy_success( int source_usage_arg,
2398 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002399 int type_arg, data_t *material,
2400 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401 int target_usage_arg,
2402 int target_alg_arg, int target_alg2_arg,
2403 int expected_usage_arg,
2404 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002405{
Gilles Peskineca25db92019-04-19 11:43:08 +02002406 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2407 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002408 psa_key_usage_t expected_usage = expected_usage_arg;
2409 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002410 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002411 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2412 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002413 uint8_t *export_buffer = NULL;
2414
Gilles Peskine57ab7212019-01-28 13:03:09 +01002415 PSA_ASSERT( psa_crypto_init( ) );
2416
Gilles Peskineca25db92019-04-19 11:43:08 +02002417 /* Prepare the source key. */
2418 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2419 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002420 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002421 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002422 PSA_ASSERT( psa_import_key( &source_attributes,
2423 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002424 &source_key ) );
2425 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002426
Gilles Peskineca25db92019-04-19 11:43:08 +02002427 /* Prepare the target attributes. */
2428 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002429 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002430 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002431 /* Set volatile lifetime to reset the key identifier to 0. */
2432 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2433 }
2434
Gilles Peskineca25db92019-04-19 11:43:08 +02002435 if( target_usage_arg != -1 )
2436 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2437 if( target_alg_arg != -1 )
2438 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002439 if( target_alg2_arg != -1 )
2440 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002441
2442 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002443 PSA_ASSERT( psa_copy_key( source_key,
2444 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002445
2446 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002447 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002448
2449 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002450 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002451 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2452 psa_get_key_type( &target_attributes ) );
2453 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2454 psa_get_key_bits( &target_attributes ) );
2455 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2456 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002457 TEST_EQUAL( expected_alg2,
2458 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2460 {
2461 size_t length;
2462 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002463 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002464 material->len, &length ) );
2465 ASSERT_COMPARE( material->x, material->len,
2466 export_buffer, length );
2467 }
Ronald Cron5425a212020-08-04 14:58:35 +02002468 if( ! exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002469 goto exit;
Ronald Cron5425a212020-08-04 14:58:35 +02002470 if( ! exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002471 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002472
Ronald Cron5425a212020-08-04 14:58:35 +02002473 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002474
2475exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002476 /*
2477 * Source and target key attributes may have been returned by
2478 * psa_get_key_attributes() thus reset them as required.
2479 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002480 psa_reset_key_attributes( &source_attributes );
2481 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002482
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002483 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002484 mbedtls_free( export_buffer );
2485}
2486/* END_CASE */
2487
2488/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002489void copy_fail( int source_usage_arg,
2490 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002491 int type_arg, data_t *material,
2492 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002493 int target_usage_arg,
2494 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002495 int expected_status_arg )
2496{
2497 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2498 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002499 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2500 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002501
2502 PSA_ASSERT( psa_crypto_init( ) );
2503
2504 /* Prepare the source key. */
2505 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2506 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002507 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002508 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002509 PSA_ASSERT( psa_import_key( &source_attributes,
2510 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002511 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002512
2513 /* Prepare the target attributes. */
2514 psa_set_key_type( &target_attributes, target_type_arg );
2515 psa_set_key_bits( &target_attributes, target_bits_arg );
2516 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2517 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002518 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002519
2520 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002521 TEST_EQUAL( psa_copy_key( source_key,
2522 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002523 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002524
Ronald Cron5425a212020-08-04 14:58:35 +02002525 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002526
Gilles Peskine4a644642019-05-03 17:14:08 +02002527exit:
2528 psa_reset_key_attributes( &source_attributes );
2529 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002530 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002531}
2532/* END_CASE */
2533
2534/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002535void hash_operation_init( )
2536{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002537 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002538 /* Test each valid way of initializing the object, except for `= {0}`, as
2539 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2540 * though it's OK by the C standard. We could test for this, but we'd need
2541 * to supress the Clang warning for the test. */
2542 psa_hash_operation_t func = psa_hash_operation_init( );
2543 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2544 psa_hash_operation_t zero;
2545
2546 memset( &zero, 0, sizeof( zero ) );
2547
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002548 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002549 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2550 PSA_ERROR_BAD_STATE );
2551 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2552 PSA_ERROR_BAD_STATE );
2553 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2554 PSA_ERROR_BAD_STATE );
2555
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002556 /* A default hash operation should be abortable without error. */
2557 PSA_ASSERT( psa_hash_abort( &func ) );
2558 PSA_ASSERT( psa_hash_abort( &init ) );
2559 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002560}
2561/* END_CASE */
2562
2563/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002564void hash_setup( int alg_arg,
2565 int expected_status_arg )
2566{
2567 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002568 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002569 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002570 psa_status_t status;
2571
Gilles Peskine8817f612018-12-18 00:18:46 +01002572 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002573
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002574 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002575 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002576
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002577 /* Whether setup succeeded or failed, abort must succeed. */
2578 PSA_ASSERT( psa_hash_abort( &operation ) );
2579
2580 /* If setup failed, reproduce the failure, so as to
2581 * test the resulting state of the operation object. */
2582 if( status != PSA_SUCCESS )
2583 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2584
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002585 /* Now the operation object should be reusable. */
2586#if defined(KNOWN_SUPPORTED_HASH_ALG)
2587 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2588 PSA_ASSERT( psa_hash_abort( &operation ) );
2589#endif
2590
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002591exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002592 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002593}
2594/* END_CASE */
2595
2596/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002597void hash_compute_fail( int alg_arg, data_t *input,
2598 int output_size_arg, int expected_status_arg )
2599{
2600 psa_algorithm_t alg = alg_arg;
2601 uint8_t *output = NULL;
2602 size_t output_size = output_size_arg;
2603 size_t output_length = INVALID_EXPORT_LENGTH;
2604 psa_status_t expected_status = expected_status_arg;
2605 psa_status_t status;
2606
2607 ASSERT_ALLOC( output, output_size );
2608
2609 PSA_ASSERT( psa_crypto_init( ) );
2610
2611 status = psa_hash_compute( alg, input->x, input->len,
2612 output, output_size, &output_length );
2613 TEST_EQUAL( status, expected_status );
2614 TEST_ASSERT( output_length <= output_size );
2615
2616exit:
2617 mbedtls_free( output );
2618 PSA_DONE( );
2619}
2620/* END_CASE */
2621
2622/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002623void hash_compare_fail( int alg_arg, data_t *input,
2624 data_t *reference_hash,
2625 int expected_status_arg )
2626{
2627 psa_algorithm_t alg = alg_arg;
2628 psa_status_t expected_status = expected_status_arg;
2629 psa_status_t status;
2630
2631 PSA_ASSERT( psa_crypto_init( ) );
2632
2633 status = psa_hash_compare( alg, input->x, input->len,
2634 reference_hash->x, reference_hash->len );
2635 TEST_EQUAL( status, expected_status );
2636
2637exit:
2638 PSA_DONE( );
2639}
2640/* END_CASE */
2641
2642/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002643void hash_compute_compare( int alg_arg, data_t *input,
2644 data_t *expected_output )
2645{
2646 psa_algorithm_t alg = alg_arg;
2647 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2648 size_t output_length = INVALID_EXPORT_LENGTH;
2649 size_t i;
2650
2651 PSA_ASSERT( psa_crypto_init( ) );
2652
2653 /* Compute with tight buffer */
2654 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002655 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002656 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002657 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002658 ASSERT_COMPARE( output, output_length,
2659 expected_output->x, expected_output->len );
2660
2661 /* Compute with larger buffer */
2662 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2663 output, sizeof( output ),
2664 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002665 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002666 ASSERT_COMPARE( output, output_length,
2667 expected_output->x, expected_output->len );
2668
2669 /* Compare with correct hash */
2670 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2671 output, output_length ) );
2672
2673 /* Compare with trailing garbage */
2674 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2675 output, output_length + 1 ),
2676 PSA_ERROR_INVALID_SIGNATURE );
2677
2678 /* Compare with truncated hash */
2679 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2680 output, output_length - 1 ),
2681 PSA_ERROR_INVALID_SIGNATURE );
2682
2683 /* Compare with corrupted value */
2684 for( i = 0; i < output_length; i++ )
2685 {
2686 test_set_step( i );
2687 output[i] ^= 1;
2688 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2689 output, output_length ),
2690 PSA_ERROR_INVALID_SIGNATURE );
2691 output[i] ^= 1;
2692 }
2693
2694exit:
2695 PSA_DONE( );
2696}
2697/* END_CASE */
2698
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002699/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002700void hash_bad_order( )
2701{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002702 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002703 unsigned char input[] = "";
2704 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002705 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002706 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2707 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2708 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002709 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002710 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002711 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002712
Gilles Peskine8817f612018-12-18 00:18:46 +01002713 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002714
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002715 /* Call setup twice in a row. */
2716 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2717 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2718 PSA_ERROR_BAD_STATE );
2719 PSA_ASSERT( psa_hash_abort( &operation ) );
2720
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002721 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002722 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002723 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002724 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002725
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002726 /* Call update after finish. */
2727 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2728 PSA_ASSERT( psa_hash_finish( &operation,
2729 hash, sizeof( hash ), &hash_len ) );
2730 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002731 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002732 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002733
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002734 /* Call verify without calling setup beforehand. */
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 after finish. */
2741 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2742 PSA_ASSERT( psa_hash_finish( &operation,
2743 hash, sizeof( hash ), &hash_len ) );
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 verify twice in a row. */
2750 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2751 PSA_ASSERT( psa_hash_verify( &operation,
2752 valid_hash, sizeof( valid_hash ) ) );
2753 TEST_EQUAL( psa_hash_verify( &operation,
2754 valid_hash, sizeof( valid_hash ) ),
2755 PSA_ERROR_BAD_STATE );
2756 PSA_ASSERT( psa_hash_abort( &operation ) );
2757
2758 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002759 TEST_EQUAL( psa_hash_finish( &operation,
2760 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002761 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002762 PSA_ASSERT( psa_hash_abort( &operation ) );
2763
2764 /* Call finish twice in a row. */
2765 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2766 PSA_ASSERT( psa_hash_finish( &operation,
2767 hash, sizeof( hash ), &hash_len ) );
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 ) );
2772
2773 /* Call finish after calling verify. */
2774 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2775 PSA_ASSERT( psa_hash_verify( &operation,
2776 valid_hash, sizeof( valid_hash ) ) );
2777 TEST_EQUAL( psa_hash_finish( &operation,
2778 hash, sizeof( hash ), &hash_len ),
2779 PSA_ERROR_BAD_STATE );
2780 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002781
2782exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002783 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002784}
2785/* END_CASE */
2786
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002787/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002788void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002789{
2790 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002791 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2792 * appended to it */
2793 unsigned char hash[] = {
2794 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2795 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2796 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002797 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002798 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002799
Gilles Peskine8817f612018-12-18 00:18:46 +01002800 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002801
itayzafrir27e69452018-11-01 14:26:34 +02002802 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002804 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002805 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002806
itayzafrir27e69452018-11-01 14:26:34 +02002807 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002809 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002810 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002811
itayzafrir27e69452018-11-01 14:26:34 +02002812 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002813 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002814 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002815 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002816
itayzafrirec93d302018-10-18 18:01:10 +03002817exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002818 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002819}
2820/* END_CASE */
2821
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002822/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2823void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002824{
2825 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002826 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002827 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002828 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002829 size_t hash_len;
2830
Gilles Peskine8817f612018-12-18 00:18:46 +01002831 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002832
itayzafrir58028322018-10-25 10:22:01 +03002833 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002834 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002835 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002836 hash, expected_size - 1, &hash_len ),
2837 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002838
2839exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002840 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002841}
2842/* END_CASE */
2843
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002844/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2845void hash_clone_source_state( )
2846{
2847 psa_algorithm_t alg = PSA_ALG_SHA_256;
2848 unsigned char hash[PSA_HASH_MAX_SIZE];
2849 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2850 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2851 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2852 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2853 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2854 size_t hash_len;
2855
2856 PSA_ASSERT( psa_crypto_init( ) );
2857 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2858
2859 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2860 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2861 PSA_ASSERT( psa_hash_finish( &op_finished,
2862 hash, sizeof( hash ), &hash_len ) );
2863 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2864 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2865
2866 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2867 PSA_ERROR_BAD_STATE );
2868
2869 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2870 PSA_ASSERT( psa_hash_finish( &op_init,
2871 hash, sizeof( hash ), &hash_len ) );
2872 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2873 PSA_ASSERT( psa_hash_finish( &op_finished,
2874 hash, sizeof( hash ), &hash_len ) );
2875 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2876 PSA_ASSERT( psa_hash_finish( &op_aborted,
2877 hash, sizeof( hash ), &hash_len ) );
2878
2879exit:
2880 psa_hash_abort( &op_source );
2881 psa_hash_abort( &op_init );
2882 psa_hash_abort( &op_setup );
2883 psa_hash_abort( &op_finished );
2884 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002885 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002886}
2887/* END_CASE */
2888
2889/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2890void hash_clone_target_state( )
2891{
2892 psa_algorithm_t alg = PSA_ALG_SHA_256;
2893 unsigned char hash[PSA_HASH_MAX_SIZE];
2894 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2895 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2896 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2897 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2898 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2899 size_t hash_len;
2900
2901 PSA_ASSERT( psa_crypto_init( ) );
2902
2903 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2904 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2905 PSA_ASSERT( psa_hash_finish( &op_finished,
2906 hash, sizeof( hash ), &hash_len ) );
2907 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2908 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2909
2910 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2911 PSA_ASSERT( psa_hash_finish( &op_target,
2912 hash, sizeof( hash ), &hash_len ) );
2913
2914 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2915 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2916 PSA_ERROR_BAD_STATE );
2917 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2918 PSA_ERROR_BAD_STATE );
2919
2920exit:
2921 psa_hash_abort( &op_target );
2922 psa_hash_abort( &op_init );
2923 psa_hash_abort( &op_setup );
2924 psa_hash_abort( &op_finished );
2925 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002926 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002927}
2928/* END_CASE */
2929
itayzafrir58028322018-10-25 10:22:01 +03002930/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002931void mac_operation_init( )
2932{
Jaeden Amero252ef282019-02-15 14:05:35 +00002933 const uint8_t input[1] = { 0 };
2934
Jaeden Amero769ce272019-01-04 11:48:03 +00002935 /* Test each valid way of initializing the object, except for `= {0}`, as
2936 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2937 * though it's OK by the C standard. We could test for this, but we'd need
2938 * to supress the Clang warning for the test. */
2939 psa_mac_operation_t func = psa_mac_operation_init( );
2940 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2941 psa_mac_operation_t zero;
2942
2943 memset( &zero, 0, sizeof( zero ) );
2944
Jaeden Amero252ef282019-02-15 14:05:35 +00002945 /* A freshly-initialized MAC operation should not be usable. */
2946 TEST_EQUAL( psa_mac_update( &func,
2947 input, sizeof( input ) ),
2948 PSA_ERROR_BAD_STATE );
2949 TEST_EQUAL( psa_mac_update( &init,
2950 input, sizeof( input ) ),
2951 PSA_ERROR_BAD_STATE );
2952 TEST_EQUAL( psa_mac_update( &zero,
2953 input, sizeof( input ) ),
2954 PSA_ERROR_BAD_STATE );
2955
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002956 /* A default MAC operation should be abortable without error. */
2957 PSA_ASSERT( psa_mac_abort( &func ) );
2958 PSA_ASSERT( psa_mac_abort( &init ) );
2959 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002960}
2961/* END_CASE */
2962
2963/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002964void mac_setup( int key_type_arg,
2965 data_t *key,
2966 int alg_arg,
2967 int expected_status_arg )
2968{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002969 psa_key_type_t key_type = key_type_arg;
2970 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002971 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002972 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002973 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2974#if defined(KNOWN_SUPPORTED_MAC_ALG)
2975 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2976#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002977
Gilles Peskine8817f612018-12-18 00:18:46 +01002978 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002979
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002980 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2981 &operation, &status ) )
2982 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002983 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002984
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002985 /* The operation object should be reusable. */
2986#if defined(KNOWN_SUPPORTED_MAC_ALG)
2987 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2988 smoke_test_key_data,
2989 sizeof( smoke_test_key_data ),
2990 KNOWN_SUPPORTED_MAC_ALG,
2991 &operation, &status ) )
2992 goto exit;
2993 TEST_EQUAL( status, PSA_SUCCESS );
2994#endif
2995
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002996exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002997 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002998}
2999/* END_CASE */
3000
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003001/* 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 +00003002void mac_bad_order( )
3003{
Ronald Cron5425a212020-08-04 14:58:35 +02003004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003005 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3006 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003007 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003008 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3009 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3010 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003012 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3013 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3014 size_t sign_mac_length = 0;
3015 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3016 const uint8_t verify_mac[] = {
3017 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3018 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3019 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3020
3021 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003022 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003023 psa_set_key_algorithm( &attributes, alg );
3024 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003025
Ronald Cron5425a212020-08-04 14:58:35 +02003026 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3027 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003028
Jaeden Amero252ef282019-02-15 14:05:35 +00003029 /* Call update without calling setup beforehand. */
3030 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3031 PSA_ERROR_BAD_STATE );
3032 PSA_ASSERT( psa_mac_abort( &operation ) );
3033
3034 /* Call sign finish without calling setup beforehand. */
3035 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3036 &sign_mac_length),
3037 PSA_ERROR_BAD_STATE );
3038 PSA_ASSERT( psa_mac_abort( &operation ) );
3039
3040 /* Call verify finish without calling setup beforehand. */
3041 TEST_EQUAL( psa_mac_verify_finish( &operation,
3042 verify_mac, sizeof( verify_mac ) ),
3043 PSA_ERROR_BAD_STATE );
3044 PSA_ASSERT( psa_mac_abort( &operation ) );
3045
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003046 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003047 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
3048 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003049 PSA_ERROR_BAD_STATE );
3050 PSA_ASSERT( psa_mac_abort( &operation ) );
3051
Jaeden Amero252ef282019-02-15 14:05:35 +00003052 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003053 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003054 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3055 PSA_ASSERT( psa_mac_sign_finish( &operation,
3056 sign_mac, sizeof( sign_mac ),
3057 &sign_mac_length ) );
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 update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003063 PSA_ASSERT( psa_mac_verify_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_verify_finish( &operation,
3066 verify_mac, sizeof( verify_mac ) ) );
3067 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3068 PSA_ERROR_BAD_STATE );
3069 PSA_ASSERT( psa_mac_abort( &operation ) );
3070
3071 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003072 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003073 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3074 PSA_ASSERT( psa_mac_sign_finish( &operation,
3075 sign_mac, sizeof( sign_mac ),
3076 &sign_mac_length ) );
3077 TEST_EQUAL( psa_mac_sign_finish( &operation,
3078 sign_mac, sizeof( sign_mac ),
3079 &sign_mac_length ),
3080 PSA_ERROR_BAD_STATE );
3081 PSA_ASSERT( psa_mac_abort( &operation ) );
3082
3083 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003084 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003085 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3086 PSA_ASSERT( psa_mac_verify_finish( &operation,
3087 verify_mac, sizeof( verify_mac ) ) );
3088 TEST_EQUAL( psa_mac_verify_finish( &operation,
3089 verify_mac, sizeof( verify_mac ) ),
3090 PSA_ERROR_BAD_STATE );
3091 PSA_ASSERT( psa_mac_abort( &operation ) );
3092
3093 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003094 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003095 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3096 TEST_EQUAL( psa_mac_verify_finish( &operation,
3097 verify_mac, sizeof( verify_mac ) ),
3098 PSA_ERROR_BAD_STATE );
3099 PSA_ASSERT( psa_mac_abort( &operation ) );
3100
3101 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003102 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003103 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3104 TEST_EQUAL( psa_mac_sign_finish( &operation,
3105 sign_mac, sizeof( sign_mac ),
3106 &sign_mac_length ),
3107 PSA_ERROR_BAD_STATE );
3108 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003109
Ronald Cron5425a212020-08-04 14:58:35 +02003110 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003111
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003112exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003113 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003114}
3115/* END_CASE */
3116
3117/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003118void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003119 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003120 int alg_arg,
3121 data_t *input,
3122 data_t *expected_mac )
3123{
Ronald Cron5425a212020-08-04 14:58:35 +02003124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003125 psa_key_type_t key_type = key_type_arg;
3126 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003129 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003130 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003131 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003132 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003133 const size_t output_sizes_to_test[] = {
3134 0,
3135 1,
3136 expected_mac->len - 1,
3137 expected_mac->len,
3138 expected_mac->len + 1,
3139 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003140
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003141 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003142 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003143 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003144
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003146
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003147 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003148 psa_set_key_algorithm( &attributes, alg );
3149 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003150
Ronald Cron5425a212020-08-04 14:58:35 +02003151 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3152 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003153
Gilles Peskine8b356b52020-08-25 23:44:59 +02003154 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3155 {
3156 const size_t output_size = output_sizes_to_test[i];
3157 psa_status_t expected_status =
3158 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3159 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003160
Gilles Peskine8b356b52020-08-25 23:44:59 +02003161 test_set_step( output_size );
3162 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003163
Gilles Peskine8b356b52020-08-25 23:44:59 +02003164 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003165 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003166 PSA_ASSERT( psa_mac_update( &operation,
3167 input->x, input->len ) );
3168 TEST_EQUAL( psa_mac_sign_finish( &operation,
3169 actual_mac, output_size,
3170 &mac_length ),
3171 expected_status );
3172 PSA_ASSERT( psa_mac_abort( &operation ) );
3173
3174 if( expected_status == PSA_SUCCESS )
3175 {
3176 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3177 actual_mac, mac_length );
3178 }
3179 mbedtls_free( actual_mac );
3180 actual_mac = NULL;
3181 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003182
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003183exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003184 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003186 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003187 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003188}
3189/* END_CASE */
3190
3191/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003192void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003193 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003194 int alg_arg,
3195 data_t *input,
3196 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003197{
Ronald Cron5425a212020-08-04 14:58:35 +02003198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003199 psa_key_type_t key_type = key_type_arg;
3200 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003201 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003203 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003204
Gilles Peskine69c12672018-06-28 00:07:19 +02003205 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3206
Gilles Peskine8817f612018-12-18 00:18:46 +01003207 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003208
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003209 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003210 psa_set_key_algorithm( &attributes, alg );
3211 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003212
Ronald Cron5425a212020-08-04 14:58:35 +02003213 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3214 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003215
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003216 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003218 PSA_ASSERT( psa_mac_update( &operation,
3219 input->x, input->len ) );
3220 PSA_ASSERT( psa_mac_verify_finish( &operation,
3221 expected_mac->x,
3222 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003223
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003224 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003225 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003226 PSA_ASSERT( psa_mac_update( &operation,
3227 input->x, input->len ) );
3228 TEST_EQUAL( psa_mac_verify_finish( &operation,
3229 expected_mac->x,
3230 expected_mac->len - 1 ),
3231 PSA_ERROR_INVALID_SIGNATURE );
3232
3233 /* Test a MAC that's too long. */
3234 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3235 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003236 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003237 PSA_ASSERT( psa_mac_update( &operation,
3238 input->x, input->len ) );
3239 TEST_EQUAL( psa_mac_verify_finish( &operation,
3240 perturbed_mac,
3241 expected_mac->len + 1 ),
3242 PSA_ERROR_INVALID_SIGNATURE );
3243
3244 /* Test changing one byte. */
3245 for( size_t i = 0; i < expected_mac->len; i++ )
3246 {
3247 test_set_step( i );
3248 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003249 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003250 PSA_ASSERT( psa_mac_update( &operation,
3251 input->x, input->len ) );
3252 TEST_EQUAL( psa_mac_verify_finish( &operation,
3253 perturbed_mac,
3254 expected_mac->len ),
3255 PSA_ERROR_INVALID_SIGNATURE );
3256 perturbed_mac[i] ^= 1;
3257 }
3258
Gilles Peskine8c9def32018-02-08 10:02:12 +01003259exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003260 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003261 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003262 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003263 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003264}
3265/* END_CASE */
3266
3267/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003268void cipher_operation_init( )
3269{
Jaeden Ameroab439972019-02-15 14:12:05 +00003270 const uint8_t input[1] = { 0 };
3271 unsigned char output[1] = { 0 };
3272 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003273 /* Test each valid way of initializing the object, except for `= {0}`, as
3274 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3275 * though it's OK by the C standard. We could test for this, but we'd need
3276 * to supress the Clang warning for the test. */
3277 psa_cipher_operation_t func = psa_cipher_operation_init( );
3278 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3279 psa_cipher_operation_t zero;
3280
3281 memset( &zero, 0, sizeof( zero ) );
3282
Jaeden Ameroab439972019-02-15 14:12:05 +00003283 /* A freshly-initialized cipher operation should not be usable. */
3284 TEST_EQUAL( psa_cipher_update( &func,
3285 input, sizeof( input ),
3286 output, sizeof( output ),
3287 &output_length ),
3288 PSA_ERROR_BAD_STATE );
3289 TEST_EQUAL( psa_cipher_update( &init,
3290 input, sizeof( input ),
3291 output, sizeof( output ),
3292 &output_length ),
3293 PSA_ERROR_BAD_STATE );
3294 TEST_EQUAL( psa_cipher_update( &zero,
3295 input, sizeof( input ),
3296 output, sizeof( output ),
3297 &output_length ),
3298 PSA_ERROR_BAD_STATE );
3299
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003300 /* A default cipher operation should be abortable without error. */
3301 PSA_ASSERT( psa_cipher_abort( &func ) );
3302 PSA_ASSERT( psa_cipher_abort( &init ) );
3303 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003304}
3305/* END_CASE */
3306
3307/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003308void cipher_setup( int key_type_arg,
3309 data_t *key,
3310 int alg_arg,
3311 int expected_status_arg )
3312{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003313 psa_key_type_t key_type = key_type_arg;
3314 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003315 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003316 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003317 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003318#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003319 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3320#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003321
Gilles Peskine8817f612018-12-18 00:18:46 +01003322 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003323
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003324 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3325 &operation, &status ) )
3326 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003327 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003328
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003329 /* The operation object should be reusable. */
3330#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3331 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3332 smoke_test_key_data,
3333 sizeof( smoke_test_key_data ),
3334 KNOWN_SUPPORTED_CIPHER_ALG,
3335 &operation, &status ) )
3336 goto exit;
3337 TEST_EQUAL( status, PSA_SUCCESS );
3338#endif
3339
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003340exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003341 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003342 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003343}
3344/* END_CASE */
3345
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003346/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003347void cipher_bad_order( )
3348{
Ronald Cron5425a212020-08-04 14:58:35 +02003349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003350 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3351 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003353 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003354 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003355 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003356 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3357 0xaa, 0xaa, 0xaa, 0xaa };
3358 const uint8_t text[] = {
3359 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3360 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003361 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003362 size_t length = 0;
3363
3364 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003365 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3366 psa_set_key_algorithm( &attributes, alg );
3367 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003368 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3369 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003370
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003371 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003372 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3373 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003374 PSA_ERROR_BAD_STATE );
3375 PSA_ASSERT( psa_cipher_abort( &operation ) );
3376
3377 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003378 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3379 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003380 PSA_ERROR_BAD_STATE );
3381 PSA_ASSERT( psa_cipher_abort( &operation ) );
3382
Jaeden Ameroab439972019-02-15 14:12:05 +00003383 /* Generate an IV without calling setup beforehand. */
3384 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3385 buffer, sizeof( buffer ),
3386 &length ),
3387 PSA_ERROR_BAD_STATE );
3388 PSA_ASSERT( psa_cipher_abort( &operation ) );
3389
3390 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003391 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003392 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3393 buffer, sizeof( buffer ),
3394 &length ) );
3395 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3396 buffer, sizeof( buffer ),
3397 &length ),
3398 PSA_ERROR_BAD_STATE );
3399 PSA_ASSERT( psa_cipher_abort( &operation ) );
3400
3401 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003402 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003403 PSA_ASSERT( psa_cipher_set_iv( &operation,
3404 iv, sizeof( iv ) ) );
3405 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3406 buffer, sizeof( buffer ),
3407 &length ),
3408 PSA_ERROR_BAD_STATE );
3409 PSA_ASSERT( psa_cipher_abort( &operation ) );
3410
3411 /* Set an IV without calling setup beforehand. */
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 set. */
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_set_iv( &operation,
3420 iv, sizeof( iv ) ) );
3421 TEST_EQUAL( psa_cipher_set_iv( &operation,
3422 iv, sizeof( iv ) ),
3423 PSA_ERROR_BAD_STATE );
3424 PSA_ASSERT( psa_cipher_abort( &operation ) );
3425
3426 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003427 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003428 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3429 buffer, sizeof( buffer ),
3430 &length ) );
3431 TEST_EQUAL( psa_cipher_set_iv( &operation,
3432 iv, sizeof( iv ) ),
3433 PSA_ERROR_BAD_STATE );
3434 PSA_ASSERT( psa_cipher_abort( &operation ) );
3435
3436 /* Call update without calling setup beforehand. */
3437 TEST_EQUAL( psa_cipher_update( &operation,
3438 text, sizeof( text ),
3439 buffer, sizeof( buffer ),
3440 &length ),
3441 PSA_ERROR_BAD_STATE );
3442 PSA_ASSERT( psa_cipher_abort( &operation ) );
3443
3444 /* Call update without an IV where an IV is required. */
3445 TEST_EQUAL( psa_cipher_update( &operation,
3446 text, sizeof( text ),
3447 buffer, sizeof( buffer ),
3448 &length ),
3449 PSA_ERROR_BAD_STATE );
3450 PSA_ASSERT( psa_cipher_abort( &operation ) );
3451
3452 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003453 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003454 PSA_ASSERT( psa_cipher_set_iv( &operation,
3455 iv, sizeof( iv ) ) );
3456 PSA_ASSERT( psa_cipher_finish( &operation,
3457 buffer, sizeof( buffer ), &length ) );
3458 TEST_EQUAL( psa_cipher_update( &operation,
3459 text, sizeof( text ),
3460 buffer, sizeof( buffer ),
3461 &length ),
3462 PSA_ERROR_BAD_STATE );
3463 PSA_ASSERT( psa_cipher_abort( &operation ) );
3464
3465 /* Call finish without calling setup beforehand. */
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 without an IV where an IV is required. */
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 /* Not calling update means we are encrypting an empty buffer, which is OK
3474 * for cipher modes with padding. */
3475 TEST_EQUAL( psa_cipher_finish( &operation,
3476 buffer, sizeof( buffer ), &length ),
3477 PSA_ERROR_BAD_STATE );
3478 PSA_ASSERT( psa_cipher_abort( &operation ) );
3479
3480 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003481 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003482 PSA_ASSERT( psa_cipher_set_iv( &operation,
3483 iv, sizeof( iv ) ) );
3484 PSA_ASSERT( psa_cipher_finish( &operation,
3485 buffer, sizeof( buffer ), &length ) );
3486 TEST_EQUAL( psa_cipher_finish( &operation,
3487 buffer, sizeof( buffer ), &length ),
3488 PSA_ERROR_BAD_STATE );
3489 PSA_ASSERT( psa_cipher_abort( &operation ) );
3490
Ronald Cron5425a212020-08-04 14:58:35 +02003491 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003492
Jaeden Ameroab439972019-02-15 14:12:05 +00003493exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003494 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003495 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003496}
3497/* END_CASE */
3498
3499/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003500void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003501 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003502 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003503 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003504{
Ronald Cron5425a212020-08-04 14:58:35 +02003505 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003506 psa_status_t status;
3507 psa_key_type_t key_type = key_type_arg;
3508 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003509 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003510 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003511 size_t output_buffer_size = 0;
3512 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003513 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003514 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003516
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003518
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003519 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3520 psa_set_key_algorithm( &attributes, alg );
3521 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003522
Ronald Cron5425a212020-08-04 14:58:35 +02003523 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3524 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003525
Ronald Cron5425a212020-08-04 14:58:35 +02003526 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003527
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003528 if( iv->len > 0 )
3529 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003530 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003531 }
3532
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003533 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003534 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003535 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003536
Gilles Peskine8817f612018-12-18 00:18:46 +01003537 PSA_ASSERT( psa_cipher_update( &operation,
3538 input->x, input->len,
3539 output, output_buffer_size,
3540 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003541 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003542 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003543 output + total_output_length,
3544 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003545 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003546 total_output_length += function_output_length;
3547
Gilles Peskinefe11b722018-12-18 00:24:04 +01003548 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003549 if( expected_status == PSA_SUCCESS )
3550 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003551 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003552 ASSERT_COMPARE( expected_output->x, expected_output->len,
3553 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003554 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003555
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003557 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003558 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003559 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003560 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003561}
3562/* END_CASE */
3563
3564/* BEGIN_CASE */
3565void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003566 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003567 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003568 int first_part_size_arg,
3569 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003570 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003571{
Ronald Cron5425a212020-08-04 14:58:35 +02003572 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003573 psa_key_type_t key_type = key_type_arg;
3574 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003575 size_t first_part_size = first_part_size_arg;
3576 size_t output1_length = output1_length_arg;
3577 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003578 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003579 size_t output_buffer_size = 0;
3580 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003581 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003582 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003584
Gilles Peskine8817f612018-12-18 00:18:46 +01003585 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003586
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003587 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3588 psa_set_key_algorithm( &attributes, alg );
3589 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003590
Ronald Cron5425a212020-08-04 14:58:35 +02003591 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3592 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003593
Ronald Cron5425a212020-08-04 14:58:35 +02003594 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003595
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003596 if( iv->len > 0 )
3597 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003598 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003599 }
3600
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003601 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003602 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003603 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003604
Gilles Peskinee0866522019-02-19 19:44:00 +01003605 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003606 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3607 output, output_buffer_size,
3608 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003609 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003610 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003611 PSA_ASSERT( psa_cipher_update( &operation,
3612 input->x + first_part_size,
3613 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003614 output + total_output_length,
3615 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003616 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003617 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003618 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003619 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003620 output + total_output_length,
3621 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003622 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003623 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003624 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003625
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003626 ASSERT_COMPARE( expected_output->x, expected_output->len,
3627 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003628
3629exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003630 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003631 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003632 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003633 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003634}
3635/* END_CASE */
3636
3637/* BEGIN_CASE */
3638void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003639 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003640 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003641 int first_part_size_arg,
3642 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003643 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003644{
Ronald Cron5425a212020-08-04 14:58:35 +02003645 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003646 psa_key_type_t key_type = key_type_arg;
3647 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003648 size_t first_part_size = first_part_size_arg;
3649 size_t output1_length = output1_length_arg;
3650 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003651 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003652 size_t output_buffer_size = 0;
3653 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003654 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003655 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003657
Gilles Peskine8817f612018-12-18 00:18:46 +01003658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003659
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003660 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3661 psa_set_key_algorithm( &attributes, alg );
3662 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003663
Ronald Cron5425a212020-08-04 14:58:35 +02003664 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3665 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003666
Ronald Cron5425a212020-08-04 14:58:35 +02003667 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003668
Steven Cooreman177deba2020-09-07 17:14:14 +02003669 if( iv->len > 0 )
3670 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003671 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003672 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003673
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003674 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003675 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003676 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003677
Gilles Peskinee0866522019-02-19 19:44:00 +01003678 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003679 PSA_ASSERT( psa_cipher_update( &operation,
3680 input->x, first_part_size,
3681 output, output_buffer_size,
3682 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003683 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003684 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003685 PSA_ASSERT( psa_cipher_update( &operation,
3686 input->x + first_part_size,
3687 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003688 output + total_output_length,
3689 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003690 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003691 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003692 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003693 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003694 output + total_output_length,
3695 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003696 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003697 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003698 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003699
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003700 ASSERT_COMPARE( expected_output->x, expected_output->len,
3701 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003702
3703exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003704 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003705 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003707 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003708}
3709/* END_CASE */
3710
Gilles Peskine50e586b2018-06-08 14:28:46 +02003711/* BEGIN_CASE */
3712void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003713 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003714 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003715 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003716{
Ronald Cron5425a212020-08-04 14:58:35 +02003717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003718 psa_status_t status;
3719 psa_key_type_t key_type = key_type_arg;
3720 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003721 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003722 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003723 size_t output_buffer_size = 0;
3724 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003725 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003726 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003727 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003728
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003730
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003731 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3732 psa_set_key_algorithm( &attributes, alg );
3733 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003734
Ronald Cron5425a212020-08-04 14:58:35 +02003735 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3736 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003737
Ronald Cron5425a212020-08-04 14:58:35 +02003738 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003739
Steven Cooreman177deba2020-09-07 17:14:14 +02003740 if( iv->len > 0 )
3741 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003742 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003743 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003744
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003745 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003746 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003747 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003748
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_cipher_update( &operation,
3750 input->x, input->len,
3751 output, output_buffer_size,
3752 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003753 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003754 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003755 output + total_output_length,
3756 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003757 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003758 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003759 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003760
3761 if( expected_status == PSA_SUCCESS )
3762 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003763 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003764 ASSERT_COMPARE( expected_output->x, expected_output->len,
3765 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003766 }
3767
Gilles Peskine50e586b2018-06-08 14:28:46 +02003768exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003769 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003770 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003771 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003772 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003773}
3774/* END_CASE */
3775
Gilles Peskine50e586b2018-06-08 14:28:46 +02003776/* BEGIN_CASE */
3777void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003778 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003779 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003780{
Ronald Cron5425a212020-08-04 14:58:35 +02003781 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003782 psa_key_type_t key_type = key_type_arg;
3783 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003784 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003785 size_t iv_size = 16;
3786 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003787 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003788 size_t output1_size = 0;
3789 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003790 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003791 size_t output2_size = 0;
3792 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003793 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003794 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3795 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003797
Gilles Peskine8817f612018-12-18 00:18:46 +01003798 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003799
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003800 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3801 psa_set_key_algorithm( &attributes, alg );
3802 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003803
Ronald Cron5425a212020-08-04 14:58:35 +02003804 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3805 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003806
Ronald Cron5425a212020-08-04 14:58:35 +02003807 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3808 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003809
Steven Cooreman177deba2020-09-07 17:14:14 +02003810 if( alg != PSA_ALG_ECB_NO_PADDING )
3811 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003812 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3813 iv, iv_size,
3814 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003815 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003816 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003817 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003818 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003819
Gilles Peskine8817f612018-12-18 00:18:46 +01003820 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3821 output1, output1_size,
3822 &output1_length ) );
3823 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003824 output1 + output1_length,
3825 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003826 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003827
Gilles Peskine048b7f02018-06-08 14:20:49 +02003828 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003829
Gilles Peskine8817f612018-12-18 00:18:46 +01003830 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003831
3832 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003833 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003834
Steven Cooreman177deba2020-09-07 17:14:14 +02003835 if( iv_length > 0 )
3836 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003837 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3838 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003839 }
3840
Gilles Peskine8817f612018-12-18 00:18:46 +01003841 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3842 output2, output2_size,
3843 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003844 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003845 PSA_ASSERT( psa_cipher_finish( &operation2,
3846 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003847 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003848 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003849
Gilles Peskine048b7f02018-06-08 14:20:49 +02003850 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003851
Gilles Peskine8817f612018-12-18 00:18:46 +01003852 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003853
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003854 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003855
3856exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003857 psa_cipher_abort( &operation1 );
3858 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003859 mbedtls_free( output1 );
3860 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003861 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003862 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003863}
3864/* END_CASE */
3865
3866/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003867void cipher_verify_output_multipart( int alg_arg,
3868 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003869 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003870 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003871 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003872{
Ronald Cron5425a212020-08-04 14:58:35 +02003873 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003874 psa_key_type_t key_type = key_type_arg;
3875 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003876 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003877 unsigned char iv[16] = {0};
3878 size_t iv_size = 16;
3879 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003880 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003881 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003882 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003883 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003884 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003885 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003886 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003887 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3888 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003890
Gilles Peskine8817f612018-12-18 00:18:46 +01003891 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003892
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3894 psa_set_key_algorithm( &attributes, alg );
3895 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003896
Ronald Cron5425a212020-08-04 14:58:35 +02003897 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3898 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003899
Ronald Cron5425a212020-08-04 14:58:35 +02003900 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3901 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003902
Steven Cooreman177deba2020-09-07 17:14:14 +02003903 if( alg != PSA_ALG_ECB_NO_PADDING )
3904 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003905 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3906 iv, iv_size,
3907 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003908 }
3909
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003910 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003911 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003912 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003913
Gilles Peskinee0866522019-02-19 19:44:00 +01003914 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003915
Gilles Peskine8817f612018-12-18 00:18:46 +01003916 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3917 output1, output1_buffer_size,
3918 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003919 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003920
Gilles Peskine8817f612018-12-18 00:18:46 +01003921 PSA_ASSERT( psa_cipher_update( &operation1,
3922 input->x + first_part_size,
3923 input->len - first_part_size,
3924 output1, output1_buffer_size,
3925 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003926 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003927
Gilles Peskine8817f612018-12-18 00:18:46 +01003928 PSA_ASSERT( psa_cipher_finish( &operation1,
3929 output1 + output1_length,
3930 output1_buffer_size - output1_length,
3931 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003932 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003933
Gilles Peskine8817f612018-12-18 00:18:46 +01003934 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003935
Gilles Peskine048b7f02018-06-08 14:20:49 +02003936 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003937 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003938
Steven Cooreman177deba2020-09-07 17:14:14 +02003939 if( iv_length > 0 )
3940 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003941 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3942 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003943 }
Moran Pekerded84402018-06-06 16:36:50 +03003944
Gilles Peskine8817f612018-12-18 00:18:46 +01003945 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3946 output2, output2_buffer_size,
3947 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003948 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003949
Gilles Peskine8817f612018-12-18 00:18:46 +01003950 PSA_ASSERT( psa_cipher_update( &operation2,
3951 output1 + first_part_size,
3952 output1_length - first_part_size,
3953 output2, output2_buffer_size,
3954 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003955 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003956
Gilles Peskine8817f612018-12-18 00:18:46 +01003957 PSA_ASSERT( psa_cipher_finish( &operation2,
3958 output2 + output2_length,
3959 output2_buffer_size - output2_length,
3960 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003961 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003962
Gilles Peskine8817f612018-12-18 00:18:46 +01003963 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003964
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003965 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003966
3967exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003968 psa_cipher_abort( &operation1 );
3969 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003970 mbedtls_free( output1 );
3971 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003972 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003973 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003974}
3975/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003976
Gilles Peskine20035e32018-02-03 22:44:14 +01003977/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003978void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003979 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003980 data_t *nonce,
3981 data_t *additional_data,
3982 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003983 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003984{
Ronald Cron5425a212020-08-04 14:58:35 +02003985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003986 psa_key_type_t key_type = key_type_arg;
3987 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003988 unsigned char *output_data = NULL;
3989 size_t output_size = 0;
3990 size_t output_length = 0;
3991 unsigned char *output_data2 = NULL;
3992 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003993 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003994 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003996
Gilles Peskine4abf7412018-06-18 16:35:34 +02003997 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003998 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3999 * should be exact. */
4000 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4001 TEST_EQUAL( output_size,
4002 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004003 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004004
Gilles Peskine8817f612018-12-18 00:18:46 +01004005 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004006
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004007 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4008 psa_set_key_algorithm( &attributes, alg );
4009 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004010
Gilles Peskine049c7532019-05-15 20:22:09 +02004011 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004012 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004013
Ronald Cron5425a212020-08-04 14:58:35 +02004014 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004015 nonce->x, nonce->len,
4016 additional_data->x,
4017 additional_data->len,
4018 input_data->x, input_data->len,
4019 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004020 &output_length ),
4021 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004022
4023 if( PSA_SUCCESS == expected_result )
4024 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004025 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004026
Gilles Peskine003a4a92019-05-14 16:09:40 +02004027 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4028 * should be exact. */
4029 TEST_EQUAL( input_data->len,
4030 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
4031
Ronald Cron5425a212020-08-04 14:58:35 +02004032 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004033 nonce->x, nonce->len,
4034 additional_data->x,
4035 additional_data->len,
4036 output_data, output_length,
4037 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004038 &output_length2 ),
4039 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004040
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004041 ASSERT_COMPARE( input_data->x, input_data->len,
4042 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004043 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004044
Gilles Peskinea1cac842018-06-11 19:33:02 +02004045exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004046 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004047 mbedtls_free( output_data );
4048 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004049 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004050}
4051/* END_CASE */
4052
4053/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004054void aead_encrypt( int key_type_arg, data_t *key_data,
4055 int alg_arg,
4056 data_t *nonce,
4057 data_t *additional_data,
4058 data_t *input_data,
4059 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004060{
Ronald Cron5425a212020-08-04 14:58:35 +02004061 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004062 psa_key_type_t key_type = key_type_arg;
4063 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004064 unsigned char *output_data = NULL;
4065 size_t output_size = 0;
4066 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004067 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004068 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004069
Gilles Peskine4abf7412018-06-18 16:35:34 +02004070 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004071 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4072 * should be exact. */
4073 TEST_EQUAL( output_size,
4074 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004075 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004076
Gilles Peskine8817f612018-12-18 00:18:46 +01004077 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004078
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4080 psa_set_key_algorithm( &attributes, alg );
4081 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004082
Gilles Peskine049c7532019-05-15 20:22:09 +02004083 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004084 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004085
Ronald Cron5425a212020-08-04 14:58:35 +02004086 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004087 nonce->x, nonce->len,
4088 additional_data->x, additional_data->len,
4089 input_data->x, input_data->len,
4090 output_data, output_size,
4091 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004092
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004093 ASSERT_COMPARE( expected_result->x, expected_result->len,
4094 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004095
Gilles Peskinea1cac842018-06-11 19:33:02 +02004096exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004097 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004098 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004099 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004100}
4101/* END_CASE */
4102
4103/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004104void aead_decrypt( int key_type_arg, data_t *key_data,
4105 int alg_arg,
4106 data_t *nonce,
4107 data_t *additional_data,
4108 data_t *input_data,
4109 data_t *expected_data,
4110 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004111{
Ronald Cron5425a212020-08-04 14:58:35 +02004112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004113 psa_key_type_t key_type = key_type_arg;
4114 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004115 unsigned char *output_data = NULL;
4116 size_t output_size = 0;
4117 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004118 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004120 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004121
Gilles Peskine003a4a92019-05-14 16:09:40 +02004122 output_size = input_data->len - tag_length;
4123 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4124 * should be exact. */
4125 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4126 TEST_EQUAL( output_size,
4127 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004128 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004129
Gilles Peskine8817f612018-12-18 00:18:46 +01004130 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004131
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004132 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4133 psa_set_key_algorithm( &attributes, alg );
4134 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004135
Gilles Peskine049c7532019-05-15 20:22:09 +02004136 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004137 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004138
Ronald Cron5425a212020-08-04 14:58:35 +02004139 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004140 nonce->x, nonce->len,
4141 additional_data->x,
4142 additional_data->len,
4143 input_data->x, input_data->len,
4144 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004145 &output_length ),
4146 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004147
Gilles Peskine2d277862018-06-18 15:41:12 +02004148 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004149 ASSERT_COMPARE( expected_data->x, expected_data->len,
4150 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004151
Gilles Peskinea1cac842018-06-11 19:33:02 +02004152exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004153 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004154 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004155 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004156}
4157/* END_CASE */
4158
4159/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004160void signature_size( int type_arg,
4161 int bits,
4162 int alg_arg,
4163 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004164{
4165 psa_key_type_t type = type_arg;
4166 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004167 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004168
Gilles Peskinefe11b722018-12-18 00:24:04 +01004169 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004170#if defined(MBEDTLS_TEST_DEPRECATED)
4171 TEST_EQUAL( actual_size,
4172 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4173#endif /* MBEDTLS_TEST_DEPRECATED */
4174
Gilles Peskinee59236f2018-01-27 23:32:46 +01004175exit:
4176 ;
4177}
4178/* END_CASE */
4179
4180/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004181void sign_deterministic( int key_type_arg, data_t *key_data,
4182 int alg_arg, data_t *input_data,
4183 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004184{
Ronald Cron5425a212020-08-04 14:58:35 +02004185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004186 psa_key_type_t key_type = key_type_arg;
4187 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004188 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004189 unsigned char *signature = NULL;
4190 size_t signature_size;
4191 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004193
Gilles Peskine8817f612018-12-18 00:18:46 +01004194 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004195
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004196 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004197 psa_set_key_algorithm( &attributes, alg );
4198 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004199
Gilles Peskine049c7532019-05-15 20:22:09 +02004200 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004201 &key ) );
4202 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004203 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004204
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004205 /* Allocate a buffer which has the size advertized by the
4206 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004207 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004208 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004209 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004210 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004211 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004212
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004213 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004214 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004215 input_data->x, input_data->len,
4216 signature, signature_size,
4217 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004218 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004219 ASSERT_COMPARE( output_data->x, output_data->len,
4220 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004221
Gilles Peskine0627f982019-11-26 19:12:16 +01004222#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004223 memset( signature, 0, signature_size );
4224 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004225 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004226 input_data->x, input_data->len,
4227 signature, signature_size,
4228 &signature_length ) );
4229 ASSERT_COMPARE( output_data->x, output_data->len,
4230 signature, signature_length );
4231#endif /* MBEDTLS_TEST_DEPRECATED */
4232
Gilles Peskine20035e32018-02-03 22:44:14 +01004233exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004234 /*
4235 * Key attributes may have been returned by psa_get_key_attributes()
4236 * thus reset them as required.
4237 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004238 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004239
Ronald Cron5425a212020-08-04 14:58:35 +02004240 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004241 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004242 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004243}
4244/* END_CASE */
4245
4246/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004247void sign_fail( int key_type_arg, data_t *key_data,
4248 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004249 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004250{
Ronald Cron5425a212020-08-04 14:58:35 +02004251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004252 psa_key_type_t key_type = key_type_arg;
4253 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004254 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004255 psa_status_t actual_status;
4256 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004257 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004258 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004259 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004260
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004261 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004262
Gilles Peskine8817f612018-12-18 00:18:46 +01004263 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004264
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004265 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004266 psa_set_key_algorithm( &attributes, alg );
4267 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004268
Gilles Peskine049c7532019-05-15 20:22:09 +02004269 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004270 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004271
Ronald Cron5425a212020-08-04 14:58:35 +02004272 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004273 input_data->x, input_data->len,
4274 signature, signature_size,
4275 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004276 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004277 /* The value of *signature_length is unspecified on error, but
4278 * whatever it is, it should be less than signature_size, so that
4279 * if the caller tries to read *signature_length bytes without
4280 * checking the error code then they don't overflow a buffer. */
4281 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004282
Gilles Peskine895242b2019-11-29 12:15:40 +01004283#if defined(MBEDTLS_TEST_DEPRECATED)
4284 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004285 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004286 input_data->x, input_data->len,
4287 signature, signature_size,
4288 &signature_length ),
4289 expected_status );
4290 TEST_ASSERT( signature_length <= signature_size );
4291#endif /* MBEDTLS_TEST_DEPRECATED */
4292
Gilles Peskine20035e32018-02-03 22:44:14 +01004293exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004294 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004295 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004296 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004297 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004298}
4299/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004300
4301/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004302void sign_verify( int key_type_arg, data_t *key_data,
4303 int alg_arg, data_t *input_data )
4304{
Ronald Cron5425a212020-08-04 14:58:35 +02004305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004306 psa_key_type_t key_type = key_type_arg;
4307 psa_algorithm_t alg = alg_arg;
4308 size_t key_bits;
4309 unsigned char *signature = NULL;
4310 size_t signature_size;
4311 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004313
Gilles Peskine8817f612018-12-18 00:18:46 +01004314 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004315
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004316 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004317 psa_set_key_algorithm( &attributes, alg );
4318 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004319
Gilles Peskine049c7532019-05-15 20:22:09 +02004320 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004321 &key ) );
4322 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004323 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004324
4325 /* Allocate a buffer which has the size advertized by the
4326 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004327 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004328 key_bits, alg );
4329 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004330 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004331 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004332
4333 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004334 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004335 input_data->x, input_data->len,
4336 signature, signature_size,
4337 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004338 /* Check that the signature length looks sensible. */
4339 TEST_ASSERT( signature_length <= signature_size );
4340 TEST_ASSERT( signature_length > 0 );
4341
4342 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004343 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004344 input_data->x, input_data->len,
4345 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004346
4347 if( input_data->len != 0 )
4348 {
4349 /* Flip a bit in the input and verify that the signature is now
4350 * detected as invalid. Flip a bit at the beginning, not at the end,
4351 * because ECDSA may ignore the last few bits of the input. */
4352 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004353 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004354 input_data->x, input_data->len,
4355 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004356 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004357 }
4358
4359exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004360 /*
4361 * Key attributes may have been returned by psa_get_key_attributes()
4362 * thus reset them as required.
4363 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004364 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004365
Ronald Cron5425a212020-08-04 14:58:35 +02004366 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004367 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004368 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004369}
4370/* END_CASE */
4371
4372/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004373void asymmetric_verify( int key_type_arg, data_t *key_data,
4374 int alg_arg, data_t *hash_data,
4375 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004376{
Ronald Cron5425a212020-08-04 14:58:35 +02004377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004378 psa_key_type_t key_type = key_type_arg;
4379 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004380 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004381
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004382 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004383
Gilles Peskine8817f612018-12-18 00:18:46 +01004384 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004385
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004386 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004387 psa_set_key_algorithm( &attributes, alg );
4388 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004389
Gilles Peskine049c7532019-05-15 20:22:09 +02004390 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004391 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004392
Ronald Cron5425a212020-08-04 14:58:35 +02004393 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004394 hash_data->x, hash_data->len,
4395 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004396
4397#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004398 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004399 hash_data->x, hash_data->len,
4400 signature_data->x,
4401 signature_data->len ) );
4402
4403#endif /* MBEDTLS_TEST_DEPRECATED */
4404
itayzafrir5c753392018-05-08 11:18:38 +03004405exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004406 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004407 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004408 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004409}
4410/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004411
4412/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004413void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4414 int alg_arg, data_t *hash_data,
4415 data_t *signature_data,
4416 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004417{
Ronald Cron5425a212020-08-04 14:58:35 +02004418 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004419 psa_key_type_t key_type = key_type_arg;
4420 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004421 psa_status_t actual_status;
4422 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004424
Gilles Peskine8817f612018-12-18 00:18:46 +01004425 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004426
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004427 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004428 psa_set_key_algorithm( &attributes, alg );
4429 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004430
Gilles Peskine049c7532019-05-15 20:22:09 +02004431 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004432 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004433
Ronald Cron5425a212020-08-04 14:58:35 +02004434 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004435 hash_data->x, hash_data->len,
4436 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004437 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004438
Gilles Peskine895242b2019-11-29 12:15:40 +01004439#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004440 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004441 hash_data->x, hash_data->len,
4442 signature_data->x, signature_data->len ),
4443 expected_status );
4444#endif /* MBEDTLS_TEST_DEPRECATED */
4445
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004446exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004447 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004448 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004449 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004450}
4451/* END_CASE */
4452
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004453/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004454void asymmetric_encrypt( int key_type_arg,
4455 data_t *key_data,
4456 int alg_arg,
4457 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004458 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004459 int expected_output_length_arg,
4460 int expected_status_arg )
4461{
Ronald Cron5425a212020-08-04 14:58:35 +02004462 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004463 psa_key_type_t key_type = key_type_arg;
4464 psa_algorithm_t alg = alg_arg;
4465 size_t expected_output_length = expected_output_length_arg;
4466 size_t key_bits;
4467 unsigned char *output = NULL;
4468 size_t output_size;
4469 size_t output_length = ~0;
4470 psa_status_t actual_status;
4471 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004472 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004473
Gilles Peskine8817f612018-12-18 00:18:46 +01004474 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004475
Gilles Peskine656896e2018-06-29 19:12:28 +02004476 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4478 psa_set_key_algorithm( &attributes, alg );
4479 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004480 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004481 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004482
4483 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004484 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004485 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004486 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004487 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004488
4489 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004490 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004491 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004492 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004493 output, output_size,
4494 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004495 TEST_EQUAL( actual_status, expected_status );
4496 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004497
Gilles Peskine68428122018-06-30 18:42:41 +02004498 /* If the label is empty, the test framework puts a non-null pointer
4499 * in label->x. Test that a null pointer works as well. */
4500 if( label->len == 0 )
4501 {
4502 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004503 if( output_size != 0 )
4504 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004505 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004506 input_data->x, input_data->len,
4507 NULL, label->len,
4508 output, output_size,
4509 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004510 TEST_EQUAL( actual_status, expected_status );
4511 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004512 }
4513
Gilles Peskine656896e2018-06-29 19:12:28 +02004514exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004515 /*
4516 * Key attributes may have been returned by psa_get_key_attributes()
4517 * thus reset them as required.
4518 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004519 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004520
Ronald Cron5425a212020-08-04 14:58:35 +02004521 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004522 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004523 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004524}
4525/* END_CASE */
4526
4527/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004528void asymmetric_encrypt_decrypt( int key_type_arg,
4529 data_t *key_data,
4530 int alg_arg,
4531 data_t *input_data,
4532 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004533{
Ronald Cron5425a212020-08-04 14:58:35 +02004534 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004535 psa_key_type_t key_type = key_type_arg;
4536 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004537 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004538 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004539 size_t output_size;
4540 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004541 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004542 size_t output2_size;
4543 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004545
Gilles Peskine8817f612018-12-18 00:18:46 +01004546 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004547
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004548 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4549 psa_set_key_algorithm( &attributes, alg );
4550 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004551
Gilles Peskine049c7532019-05-15 20:22:09 +02004552 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004553 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004554
4555 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004556 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004557 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004558 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004559 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004560 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004561 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004562
Gilles Peskineeebd7382018-06-08 18:11:54 +02004563 /* We test encryption by checking that encrypt-then-decrypt gives back
4564 * the original plaintext because of the non-optional random
4565 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004566 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004567 input_data->x, input_data->len,
4568 label->x, label->len,
4569 output, output_size,
4570 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004571 /* We don't know what ciphertext length to expect, but check that
4572 * it looks sensible. */
4573 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004574
Ronald Cron5425a212020-08-04 14:58:35 +02004575 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004576 output, output_length,
4577 label->x, label->len,
4578 output2, output2_size,
4579 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004580 ASSERT_COMPARE( input_data->x, input_data->len,
4581 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004582
4583exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004584 /*
4585 * Key attributes may have been returned by psa_get_key_attributes()
4586 * thus reset them as required.
4587 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004588 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004589
Ronald Cron5425a212020-08-04 14:58:35 +02004590 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004591 mbedtls_free( output );
4592 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004593 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004594}
4595/* END_CASE */
4596
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004597/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004598void asymmetric_decrypt( int key_type_arg,
4599 data_t *key_data,
4600 int alg_arg,
4601 data_t *input_data,
4602 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004603 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004604{
Ronald Cron5425a212020-08-04 14:58:35 +02004605 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004606 psa_key_type_t key_type = key_type_arg;
4607 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004608 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004609 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004610 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004612
Jaeden Amero412654a2019-02-06 12:57:46 +00004613 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004614 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004615
Gilles Peskine8817f612018-12-18 00:18:46 +01004616 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004617
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004618 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4619 psa_set_key_algorithm( &attributes, alg );
4620 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004621
Gilles Peskine049c7532019-05-15 20:22:09 +02004622 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004623 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004624
Ronald Cron5425a212020-08-04 14:58:35 +02004625 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004626 input_data->x, input_data->len,
4627 label->x, label->len,
4628 output,
4629 output_size,
4630 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004631 ASSERT_COMPARE( expected_data->x, expected_data->len,
4632 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004633
Gilles Peskine68428122018-06-30 18:42:41 +02004634 /* If the label is empty, the test framework puts a non-null pointer
4635 * in label->x. Test that a null pointer works as well. */
4636 if( label->len == 0 )
4637 {
4638 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004639 if( output_size != 0 )
4640 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004641 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004642 input_data->x, input_data->len,
4643 NULL, label->len,
4644 output,
4645 output_size,
4646 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004647 ASSERT_COMPARE( expected_data->x, expected_data->len,
4648 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004649 }
4650
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004651exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004652 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004653 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004654 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004655 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004656}
4657/* END_CASE */
4658
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004659/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004660void asymmetric_decrypt_fail( int key_type_arg,
4661 data_t *key_data,
4662 int alg_arg,
4663 data_t *input_data,
4664 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004665 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004666 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004667{
Ronald Cron5425a212020-08-04 14:58:35 +02004668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004669 psa_key_type_t key_type = key_type_arg;
4670 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004671 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004672 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004673 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004674 psa_status_t actual_status;
4675 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004677
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004678 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004679
Gilles Peskine8817f612018-12-18 00:18:46 +01004680 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004681
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004682 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4683 psa_set_key_algorithm( &attributes, alg );
4684 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004685
Gilles Peskine049c7532019-05-15 20:22:09 +02004686 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004687 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004688
Ronald Cron5425a212020-08-04 14:58:35 +02004689 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004690 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004691 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004692 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004693 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004694 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004695 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004696
Gilles Peskine68428122018-06-30 18:42:41 +02004697 /* If the label is empty, the test framework puts a non-null pointer
4698 * in label->x. Test that a null pointer works as well. */
4699 if( label->len == 0 )
4700 {
4701 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004702 if( output_size != 0 )
4703 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004704 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004705 input_data->x, input_data->len,
4706 NULL, label->len,
4707 output, output_size,
4708 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004709 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004710 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004711 }
4712
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004713exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004714 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004715 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004716 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004717 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004718}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004719/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004720
4721/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004722void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004723{
4724 /* Test each valid way of initializing the object, except for `= {0}`, as
4725 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4726 * though it's OK by the C standard. We could test for this, but we'd need
4727 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004728 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004729 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4730 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4731 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004732
4733 memset( &zero, 0, sizeof( zero ) );
4734
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004736 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004737 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004738 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004739 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004740 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004741 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004742
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004744 PSA_ASSERT( psa_key_derivation_abort(&func) );
4745 PSA_ASSERT( psa_key_derivation_abort(&init) );
4746 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004747}
4748/* END_CASE */
4749
Janos Follath16de4a42019-06-13 16:32:24 +01004750/* BEGIN_CASE */
4751void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004752{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004753 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004754 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004756
Gilles Peskine8817f612018-12-18 00:18:46 +01004757 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004758
Janos Follath16de4a42019-06-13 16:32:24 +01004759 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004760 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004761
4762exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004764 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004765}
4766/* END_CASE */
4767
Janos Follathaf3c2a02019-06-12 12:34:34 +01004768/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004769void derive_set_capacity( int alg_arg, int capacity_arg,
4770 int expected_status_arg )
4771{
4772 psa_algorithm_t alg = alg_arg;
4773 size_t capacity = capacity_arg;
4774 psa_status_t expected_status = expected_status_arg;
4775 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4776
4777 PSA_ASSERT( psa_crypto_init( ) );
4778
4779 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4780
4781 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4782 expected_status );
4783
4784exit:
4785 psa_key_derivation_abort( &operation );
4786 PSA_DONE( );
4787}
4788/* END_CASE */
4789
4790/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004791void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004792 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004793 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004794 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004795 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004796 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004797 int expected_status_arg3,
4798 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004799{
4800 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004801 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4802 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004803 psa_status_t expected_statuses[] = {expected_status_arg1,
4804 expected_status_arg2,
4805 expected_status_arg3};
4806 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004807 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4808 MBEDTLS_SVC_KEY_ID_INIT,
4809 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004810 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4812 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004813 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004814 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004815 psa_status_t expected_output_status = expected_output_status_arg;
4816 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004817
4818 PSA_ASSERT( psa_crypto_init( ) );
4819
4820 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4821 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004822
4823 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4824
4825 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4826 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004827 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004828 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004829 psa_set_key_type( &attributes, key_types[i] );
4830 PSA_ASSERT( psa_import_key( &attributes,
4831 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004832 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004833 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4834 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4835 {
4836 // When taking a private key as secret input, use key agreement
4837 // to add the shared secret to the derivation
Ronald Cron5425a212020-08-04 14:58:35 +02004838 TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004839 expected_statuses[i] );
4840 }
4841 else
4842 {
4843 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004844 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004845 expected_statuses[i] );
4846 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004847 }
4848 else
4849 {
4850 TEST_EQUAL( psa_key_derivation_input_bytes(
4851 &operation, steps[i],
4852 inputs[i]->x, inputs[i]->len ),
4853 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004854 }
4855 }
4856
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004857 if( output_key_type != PSA_KEY_TYPE_NONE )
4858 {
4859 psa_reset_key_attributes( &attributes );
4860 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4861 psa_set_key_bits( &attributes, 8 );
4862 actual_output_status =
4863 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004864 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004865 }
4866 else
4867 {
4868 uint8_t buffer[1];
4869 actual_output_status =
4870 psa_key_derivation_output_bytes( &operation,
4871 buffer, sizeof( buffer ) );
4872 }
4873 TEST_EQUAL( actual_output_status, expected_output_status );
4874
Janos Follathaf3c2a02019-06-12 12:34:34 +01004875exit:
4876 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004877 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4878 psa_destroy_key( keys[i] );
4879 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004880 PSA_DONE( );
4881}
4882/* END_CASE */
4883
Janos Follathd958bb72019-07-03 15:02:16 +01004884/* BEGIN_CASE */
4885void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004886{
Janos Follathd958bb72019-07-03 15:02:16 +01004887 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004888 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004889 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004891 unsigned char input1[] = "Input 1";
4892 size_t input1_length = sizeof( input1 );
4893 unsigned char input2[] = "Input 2";
4894 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004895 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004896 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004897 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4898 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4899 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004900 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004901
Gilles Peskine8817f612018-12-18 00:18:46 +01004902 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004903
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004904 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4905 psa_set_key_algorithm( &attributes, alg );
4906 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004907
Gilles Peskine73676cb2019-05-15 20:15:10 +02004908 PSA_ASSERT( psa_import_key( &attributes,
4909 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004910 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004911
4912 /* valid key derivation */
Ronald Cron5425a212020-08-04 14:58:35 +02004913 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathd958bb72019-07-03 15:02:16 +01004914 input1, input1_length,
4915 input2, input2_length,
4916 capacity ) )
4917 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004920 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004921 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004922
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004924
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004926 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004927
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004928exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004929 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004930 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004931 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004932}
4933/* END_CASE */
4934
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004935/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004936void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004937{
4938 uint8_t output_buffer[16];
4939 size_t buffer_size = 16;
4940 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004941 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
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
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004951
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004952 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4953 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004954 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004955
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004957 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004958
4959exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004961}
4962/* END_CASE */
4963
4964/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004965void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004966 int step1_arg, data_t *input1,
4967 int step2_arg, data_t *input2,
4968 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004969 int requested_capacity_arg,
4970 data_t *expected_output1,
4971 data_t *expected_output2 )
4972{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004973 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004974 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4975 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004976 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4977 MBEDTLS_SVC_KEY_ID_INIT,
4978 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004979 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004980 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004981 uint8_t *expected_outputs[2] =
4982 {expected_output1->x, expected_output2->x};
4983 size_t output_sizes[2] =
4984 {expected_output1->len, expected_output2->len};
4985 size_t output_buffer_size = 0;
4986 uint8_t *output_buffer = NULL;
4987 size_t expected_capacity;
4988 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004990 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004991 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004992
4993 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4994 {
4995 if( output_sizes[i] > output_buffer_size )
4996 output_buffer_size = output_sizes[i];
4997 if( output_sizes[i] == 0 )
4998 expected_outputs[i] = NULL;
4999 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005000 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01005001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005002
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005003 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5004 psa_set_key_algorithm( &attributes, alg );
5005 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005006
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005007 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02005008 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5009 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
5010 requested_capacity ) );
5011 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005012 {
Gilles Peskine1468da72019-05-29 17:35:49 +02005013 switch( steps[i] )
5014 {
5015 case 0:
5016 break;
5017 case PSA_KEY_DERIVATION_INPUT_SECRET:
5018 PSA_ASSERT( psa_import_key( &attributes,
5019 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005020 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005021 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02005022 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02005023 break;
5024 default:
5025 PSA_ASSERT( psa_key_derivation_input_bytes(
5026 &operation, steps[i],
5027 inputs[i]->x, inputs[i]->len ) );
5028 break;
5029 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005030 }
Gilles Peskine1468da72019-05-29 17:35:49 +02005031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005033 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005034 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005035 expected_capacity = requested_capacity;
5036
5037 /* Expansion phase. */
5038 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
5039 {
5040 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005041 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005042 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005043 if( expected_capacity == 0 && output_sizes[i] == 0 )
5044 {
5045 /* Reading 0 bytes when 0 bytes are available can go either way. */
5046 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02005047 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005048 continue;
5049 }
5050 else if( expected_capacity == 0 ||
5051 output_sizes[i] > expected_capacity )
5052 {
5053 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02005054 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005055 expected_capacity = 0;
5056 continue;
5057 }
5058 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005059 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005060 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005061 ASSERT_COMPARE( output_buffer, output_sizes[i],
5062 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005064 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005066 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005067 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005068 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005069 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005070
5071exit:
5072 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005073 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005074 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5075 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005076 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02005077}
5078/* END_CASE */
5079
5080/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02005081void derive_full( int alg_arg,
5082 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01005083 data_t *input1,
5084 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02005085 int requested_capacity_arg )
5086{
Ronald Cron5425a212020-08-04 14:58:35 +02005087 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005088 psa_algorithm_t alg = alg_arg;
5089 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005091 unsigned char output_buffer[16];
5092 size_t expected_capacity = requested_capacity;
5093 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02005095
Gilles Peskine8817f612018-12-18 00:18:46 +01005096 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005097
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005098 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5099 psa_set_key_algorithm( &attributes, alg );
5100 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02005101
Gilles Peskine049c7532019-05-15 20:22:09 +02005102 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005103 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005104
Ronald Cron5425a212020-08-04 14:58:35 +02005105 if( !setup_key_derivation_wrap( &operation, key, alg,
Janos Follathf2815ea2019-07-03 12:41:36 +01005106 input1->x, input1->len,
5107 input2->x, input2->len,
5108 requested_capacity ) )
5109 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005110
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005112 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005113 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005114
5115 /* Expansion phase. */
5116 while( current_capacity > 0 )
5117 {
5118 size_t read_size = sizeof( output_buffer );
5119 if( read_size > current_capacity )
5120 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005122 output_buffer,
5123 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005124 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005126 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005127 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005128 }
5129
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005130 /* Check that the operation refuses to go over capacity. */
5131 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005132 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005133
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005135
5136exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005138 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005139 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005140}
5141/* END_CASE */
5142
Janos Follathe60c9052019-07-03 13:51:30 +01005143/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005144void derive_key_exercise( int alg_arg,
5145 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005146 data_t *input1,
5147 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005148 int derived_type_arg,
5149 int derived_bits_arg,
5150 int derived_usage_arg,
5151 int derived_alg_arg )
5152{
Ronald Cron5425a212020-08-04 14:58:35 +02005153 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5154 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005155 psa_algorithm_t alg = alg_arg;
5156 psa_key_type_t derived_type = derived_type_arg;
5157 size_t derived_bits = derived_bits_arg;
5158 psa_key_usage_t derived_usage = derived_usage_arg;
5159 psa_algorithm_t derived_alg = derived_alg_arg;
5160 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005163 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005164
Gilles Peskine8817f612018-12-18 00:18:46 +01005165 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005166
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005167 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5168 psa_set_key_algorithm( &attributes, alg );
5169 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005170 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005171 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005172
5173 /* Derive a key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005174 if ( setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follathe60c9052019-07-03 13:51:30 +01005175 input1->x, input1->len,
5176 input2->x, input2->len, capacity ) )
5177 goto exit;
5178
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005179 psa_set_key_usage_flags( &attributes, derived_usage );
5180 psa_set_key_algorithm( &attributes, derived_alg );
5181 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005182 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005183 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005184 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005185
5186 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005187 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005188 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5189 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005190
5191 /* Exercise the derived key. */
Ronald Cron5425a212020-08-04 14:58:35 +02005192 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005193 goto exit;
5194
5195exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005196 /*
5197 * Key attributes may have been returned by psa_get_key_attributes()
5198 * thus reset them as required.
5199 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005200 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005201
5202 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005203 psa_destroy_key( base_key );
5204 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005205 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005206}
5207/* END_CASE */
5208
Janos Follath42fd8882019-07-03 14:17:09 +01005209/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005210void derive_key_export( int alg_arg,
5211 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005212 data_t *input1,
5213 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005214 int bytes1_arg,
5215 int bytes2_arg )
5216{
Ronald Cron5425a212020-08-04 14:58:35 +02005217 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5218 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005219 psa_algorithm_t alg = alg_arg;
5220 size_t bytes1 = bytes1_arg;
5221 size_t bytes2 = bytes2_arg;
5222 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005224 uint8_t *output_buffer = NULL;
5225 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005226 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5227 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005228 size_t length;
5229
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005230 ASSERT_ALLOC( output_buffer, capacity );
5231 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005232 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005233
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005234 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5235 psa_set_key_algorithm( &base_attributes, alg );
5236 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005237 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005238 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005239
5240 /* Derive some material and output it. */
Ronald Cron5425a212020-08-04 14:58:35 +02005241 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005242 input1->x, input1->len,
5243 input2->x, input2->len, capacity ) )
5244 goto exit;
5245
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005246 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005247 output_buffer,
5248 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005250
5251 /* Derive the same output again, but this time store it in key objects. */
Ronald Cron5425a212020-08-04 14:58:35 +02005252 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Janos Follath42fd8882019-07-03 14:17:09 +01005253 input1->x, input1->len,
5254 input2->x, input2->len, capacity ) )
5255 goto exit;
5256
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005257 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5258 psa_set_key_algorithm( &derived_attributes, 0 );
5259 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005260 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005262 &derived_key ) );
5263 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005264 export_buffer, bytes1,
5265 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005266 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005267 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005268 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005270 &derived_key ) );
5271 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005272 export_buffer + bytes1, bytes2,
5273 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005274 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005275
5276 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005277 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5278 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005279
5280exit:
5281 mbedtls_free( output_buffer );
5282 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005283 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005284 psa_destroy_key( base_key );
5285 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005286 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005287}
5288/* END_CASE */
5289
5290/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005291void derive_key( int alg_arg,
5292 data_t *key_data, data_t *input1, data_t *input2,
5293 int type_arg, int bits_arg,
5294 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005295{
Ronald Cron5425a212020-08-04 14:58:35 +02005296 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5297 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005298 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005299 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005300 size_t bits = bits_arg;
5301 psa_status_t expected_status = expected_status_arg;
5302 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5303 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5304 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5305
5306 PSA_ASSERT( psa_crypto_init( ) );
5307
5308 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5309 psa_set_key_algorithm( &base_attributes, alg );
5310 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5311 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005312 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005313
Ronald Cron5425a212020-08-04 14:58:35 +02005314 if( !setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec744d992019-07-30 17:26:54 +02005315 input1->x, input1->len,
5316 input2->x, input2->len, SIZE_MAX ) )
5317 goto exit;
5318
5319 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5320 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005321 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005322 psa_set_key_bits( &derived_attributes, bits );
5323 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005324 &derived_key ),
Gilles Peskinec744d992019-07-30 17:26:54 +02005325 expected_status );
5326
5327exit:
5328 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005329 psa_destroy_key( base_key );
5330 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005331 PSA_DONE( );
5332}
5333/* END_CASE */
5334
5335/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005336void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005337 int our_key_type_arg, int our_key_alg_arg,
5338 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005339 int expected_status_arg )
5340{
Ronald Cron5425a212020-08-04 14:58:35 +02005341 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005342 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005343 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005344 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005347 psa_status_t expected_status = expected_status_arg;
5348 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005349
Gilles Peskine8817f612018-12-18 00:18:46 +01005350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005351
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005352 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005353 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005354 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005355 PSA_ASSERT( psa_import_key( &attributes,
5356 our_key_data->x, our_key_data->len,
5357 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005358
Gilles Peskine77f40d82019-04-11 21:27:06 +02005359 /* The tests currently include inputs that should fail at either step.
5360 * Test cases that fail at the setup step should be changed to call
5361 * key_derivation_setup instead, and this function should be renamed
5362 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005363 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005364 if( status == PSA_SUCCESS )
5365 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005366 TEST_EQUAL( psa_key_derivation_key_agreement(
5367 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5368 our_key,
5369 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005370 expected_status );
5371 }
5372 else
5373 {
5374 TEST_ASSERT( status == expected_status );
5375 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005376
5377exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005379 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005380 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005381}
5382/* END_CASE */
5383
5384/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005385void raw_key_agreement( int alg_arg,
5386 int our_key_type_arg, data_t *our_key_data,
5387 data_t *peer_key_data,
5388 data_t *expected_output )
5389{
Ronald Cron5425a212020-08-04 14:58:35 +02005390 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005391 psa_algorithm_t alg = alg_arg;
5392 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005394 unsigned char *output = NULL;
5395 size_t output_length = ~0;
5396
5397 ASSERT_ALLOC( output, expected_output->len );
5398 PSA_ASSERT( psa_crypto_init( ) );
5399
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005400 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5401 psa_set_key_algorithm( &attributes, alg );
5402 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005403 PSA_ASSERT( psa_import_key( &attributes,
5404 our_key_data->x, our_key_data->len,
5405 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005406
Gilles Peskinebe697d82019-05-16 18:00:41 +02005407 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5408 peer_key_data->x, peer_key_data->len,
5409 output, expected_output->len,
5410 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005411 ASSERT_COMPARE( output, output_length,
5412 expected_output->x, expected_output->len );
5413
5414exit:
5415 mbedtls_free( output );
5416 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005417 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005418}
5419/* END_CASE */
5420
5421/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005422void key_agreement_capacity( int alg_arg,
5423 int our_key_type_arg, data_t *our_key_data,
5424 data_t *peer_key_data,
5425 int expected_capacity_arg )
5426{
Ronald Cron5425a212020-08-04 14:58:35 +02005427 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005428 psa_algorithm_t alg = alg_arg;
5429 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005430 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005432 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005433 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005434
Gilles Peskine8817f612018-12-18 00:18:46 +01005435 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005436
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005437 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5438 psa_set_key_algorithm( &attributes, alg );
5439 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005440 PSA_ASSERT( psa_import_key( &attributes,
5441 our_key_data->x, our_key_data->len,
5442 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005443
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005445 PSA_ASSERT( psa_key_derivation_key_agreement(
5446 &operation,
5447 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5448 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005449 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5450 {
5451 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005453 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005454 NULL, 0 ) );
5455 }
Gilles Peskine59685592018-09-18 12:11:34 +02005456
Gilles Peskinebf491972018-10-25 22:36:12 +02005457 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005458 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005460 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005461
Gilles Peskinebf491972018-10-25 22:36:12 +02005462 /* Test the actual capacity by reading the output. */
5463 while( actual_capacity > sizeof( output ) )
5464 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005465 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005466 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005467 actual_capacity -= sizeof( output );
5468 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005469 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005470 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005471 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005472 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005473
Gilles Peskine59685592018-09-18 12:11:34 +02005474exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005475 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005476 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005477 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005478}
5479/* END_CASE */
5480
5481/* BEGIN_CASE */
5482void key_agreement_output( int alg_arg,
5483 int our_key_type_arg, data_t *our_key_data,
5484 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005485 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005486{
Ronald Cron5425a212020-08-04 14:58:35 +02005487 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005488 psa_algorithm_t alg = alg_arg;
5489 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005490 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005491 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005492 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005493
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005494 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5495 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005496
Gilles Peskine8817f612018-12-18 00:18:46 +01005497 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005498
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005499 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5500 psa_set_key_algorithm( &attributes, alg );
5501 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005502 PSA_ASSERT( psa_import_key( &attributes,
5503 our_key_data->x, our_key_data->len,
5504 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005505
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005506 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005507 PSA_ASSERT( psa_key_derivation_key_agreement(
5508 &operation,
5509 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5510 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005511 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5512 {
5513 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005514 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005515 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005516 NULL, 0 ) );
5517 }
Gilles Peskine59685592018-09-18 12:11:34 +02005518
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005520 actual_output,
5521 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005522 ASSERT_COMPARE( actual_output, expected_output1->len,
5523 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005524 if( expected_output2->len != 0 )
5525 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005526 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005527 actual_output,
5528 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005529 ASSERT_COMPARE( actual_output, expected_output2->len,
5530 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005531 }
Gilles Peskine59685592018-09-18 12:11:34 +02005532
5533exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005534 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005535 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005536 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005537 mbedtls_free( actual_output );
5538}
5539/* END_CASE */
5540
5541/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005542void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005543{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005544 size_t bytes = bytes_arg;
5545 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005546 unsigned char *output = NULL;
5547 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005548 size_t i;
5549 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005550
Simon Butcher49f8e312020-03-03 15:51:50 +00005551 TEST_ASSERT( bytes_arg >= 0 );
5552
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005553 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5554 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005555 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005556
Gilles Peskine8817f612018-12-18 00:18:46 +01005557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005558
Gilles Peskinea50d7392018-06-21 10:22:13 +02005559 /* Run several times, to ensure that every output byte will be
5560 * nonzero at least once with overwhelming probability
5561 * (2^(-8*number_of_runs)). */
5562 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005563 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005564 if( bytes != 0 )
5565 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005566 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005567
5568 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005569 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5570 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005571
5572 for( i = 0; i < bytes; i++ )
5573 {
5574 if( output[i] != 0 )
5575 ++changed[i];
5576 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005577 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005578
5579 /* Check that every byte was changed to nonzero at least once. This
5580 * validates that psa_generate_random is overwriting every byte of
5581 * the output buffer. */
5582 for( i = 0; i < bytes; i++ )
5583 {
5584 TEST_ASSERT( changed[i] != 0 );
5585 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005586
5587exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005588 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005589 mbedtls_free( output );
5590 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005591}
5592/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005593
5594/* BEGIN_CASE */
5595void generate_key( int type_arg,
5596 int bits_arg,
5597 int usage_arg,
5598 int alg_arg,
5599 int expected_status_arg )
5600{
Ronald Cron5425a212020-08-04 14:58:35 +02005601 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005602 psa_key_type_t type = type_arg;
5603 psa_key_usage_t usage = usage_arg;
5604 size_t bits = bits_arg;
5605 psa_algorithm_t alg = alg_arg;
5606 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005608 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005609
Gilles Peskine8817f612018-12-18 00:18:46 +01005610 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005611
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005612 psa_set_key_usage_flags( &attributes, usage );
5613 psa_set_key_algorithm( &attributes, alg );
5614 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005615 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005616
5617 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005618 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005619 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005620 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005621
5622 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005623 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005624 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5625 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005626
Gilles Peskine818ca122018-06-20 18:16:48 +02005627 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005628 if( ! exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005629 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005630
5631exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005632 /*
5633 * Key attributes may have been returned by psa_get_key_attributes()
5634 * thus reset them as required.
5635 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005636 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005637
Ronald Cron5425a212020-08-04 14:58:35 +02005638 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005639 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005640}
5641/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005642
Gilles Peskinee56e8782019-04-26 17:34:02 +02005643/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5644void generate_key_rsa( int bits_arg,
5645 data_t *e_arg,
5646 int expected_status_arg )
5647{
Ronald Cron5425a212020-08-04 14:58:35 +02005648 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005649 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005650 size_t bits = bits_arg;
5651 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5652 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5653 psa_status_t expected_status = expected_status_arg;
5654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5655 uint8_t *exported = NULL;
5656 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005657 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005658 size_t exported_length = SIZE_MAX;
5659 uint8_t *e_read_buffer = NULL;
5660 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005661 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005662 size_t e_read_length = SIZE_MAX;
5663
5664 if( e_arg->len == 0 ||
5665 ( e_arg->len == 3 &&
5666 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5667 {
5668 is_default_public_exponent = 1;
5669 e_read_size = 0;
5670 }
5671 ASSERT_ALLOC( e_read_buffer, e_read_size );
5672 ASSERT_ALLOC( exported, exported_size );
5673
5674 PSA_ASSERT( psa_crypto_init( ) );
5675
5676 psa_set_key_usage_flags( &attributes, usage );
5677 psa_set_key_algorithm( &attributes, alg );
5678 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5679 e_arg->x, e_arg->len ) );
5680 psa_set_key_bits( &attributes, bits );
5681
5682 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005683 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005684 if( expected_status != PSA_SUCCESS )
5685 goto exit;
5686
5687 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005688 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005689 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5690 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5691 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5692 e_read_buffer, e_read_size,
5693 &e_read_length ) );
5694 if( is_default_public_exponent )
5695 TEST_EQUAL( e_read_length, 0 );
5696 else
5697 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5698
5699 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005700 if( ! exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005701 goto exit;
5702
5703 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005704 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005705 exported, exported_size,
5706 &exported_length ) );
5707 {
5708 uint8_t *p = exported;
5709 uint8_t *end = exported + exported_length;
5710 size_t len;
5711 /* RSAPublicKey ::= SEQUENCE {
5712 * modulus INTEGER, -- n
5713 * publicExponent INTEGER } -- e
5714 */
5715 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005716 MBEDTLS_ASN1_SEQUENCE |
5717 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005718 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5719 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5720 MBEDTLS_ASN1_INTEGER ) );
5721 if( len >= 1 && p[0] == 0 )
5722 {
5723 ++p;
5724 --len;
5725 }
5726 if( e_arg->len == 0 )
5727 {
5728 TEST_EQUAL( len, 3 );
5729 TEST_EQUAL( p[0], 1 );
5730 TEST_EQUAL( p[1], 0 );
5731 TEST_EQUAL( p[2], 1 );
5732 }
5733 else
5734 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5735 }
5736
5737exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005738 /*
5739 * Key attributes may have been returned by psa_get_key_attributes() or
5740 * set by psa_set_key_domain_parameters() thus reset them as required.
5741 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005742 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005743
Ronald Cron5425a212020-08-04 14:58:35 +02005744 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005745 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005746 mbedtls_free( e_read_buffer );
5747 mbedtls_free( exported );
5748}
5749/* END_CASE */
5750
Darryl Greend49a4992018-06-18 17:27:26 +01005751/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005752void persistent_key_load_key_from_storage( data_t *data,
5753 int type_arg, int bits_arg,
5754 int usage_flags_arg, int alg_arg,
5755 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005756{
Ronald Cron71016a92020-08-28 19:01:50 +02005757 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5760 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005761 psa_key_type_t type = type_arg;
5762 size_t bits = bits_arg;
5763 psa_key_usage_t usage_flags = usage_flags_arg;
5764 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005765 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005766 unsigned char *first_export = NULL;
5767 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005768 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005769 size_t first_exported_length;
5770 size_t second_exported_length;
5771
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005772 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5773 {
5774 ASSERT_ALLOC( first_export, export_size );
5775 ASSERT_ALLOC( second_export, export_size );
5776 }
Darryl Greend49a4992018-06-18 17:27:26 +01005777
Gilles Peskine8817f612018-12-18 00:18:46 +01005778 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005779
Gilles Peskinec87af662019-05-15 16:12:22 +02005780 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005781 psa_set_key_usage_flags( &attributes, usage_flags );
5782 psa_set_key_algorithm( &attributes, alg );
5783 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005784 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005785
Darryl Green0c6575a2018-11-07 16:05:30 +00005786 switch( generation_method )
5787 {
5788 case IMPORT_KEY:
5789 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005790 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005791 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005792 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005793
Darryl Green0c6575a2018-11-07 16:05:30 +00005794 case GENERATE_KEY:
5795 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005796 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005797 break;
5798
5799 case DERIVE_KEY:
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005800#if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005801 {
5802 /* Create base key */
5803 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5804 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5805 psa_set_key_usage_flags( &base_attributes,
5806 PSA_KEY_USAGE_DERIVE );
5807 psa_set_key_algorithm( &base_attributes, derive_alg );
5808 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005809 PSA_ASSERT( psa_import_key( &base_attributes,
5810 data->x, data->len,
5811 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005812 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005813 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005814 PSA_ASSERT( psa_key_derivation_input_key(
5815 &operation,
5816 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005817 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005818 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005819 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005820 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5821 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005822 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005823 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005824 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005825 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005826 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005827#else
5828 TEST_ASSUME( ! "KDF not supported in this configuration" );
5829#endif
5830 break;
5831
5832 default:
5833 TEST_ASSERT( ! "generation_method not implemented in test" );
5834 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005835 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005836 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005837
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005838 /* Export the key if permitted by the key policy. */
5839 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5840 {
Ronald Cron5425a212020-08-04 14:58:35 +02005841 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005842 first_export, export_size,
5843 &first_exported_length ) );
5844 if( generation_method == IMPORT_KEY )
5845 ASSERT_COMPARE( data->x, data->len,
5846 first_export, first_exported_length );
5847 }
Darryl Greend49a4992018-06-18 17:27:26 +01005848
5849 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005850 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005851 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005852 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005853
Darryl Greend49a4992018-06-18 17:27:26 +01005854 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005855 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005856 TEST_ASSERT( mbedtls_svc_key_id_equal(
5857 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005858 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5859 PSA_KEY_LIFETIME_PERSISTENT );
5860 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5861 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5862 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5863 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005864
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005865 /* Export the key again if permitted by the key policy. */
5866 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005867 {
Ronald Cron5425a212020-08-04 14:58:35 +02005868 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005869 second_export, export_size,
5870 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005871 ASSERT_COMPARE( first_export, first_exported_length,
5872 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005873 }
5874
5875 /* Do something with the key according to its type and permitted usage. */
Ronald Cron5425a212020-08-04 14:58:35 +02005876 if( ! exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005877 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005878
5879exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005880 /*
5881 * Key attributes may have been returned by psa_get_key_attributes()
5882 * thus reset them as required.
5883 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005884 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005885
Darryl Greend49a4992018-06-18 17:27:26 +01005886 mbedtls_free( first_export );
5887 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005888 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005889 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005890 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005891 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005892}
5893/* END_CASE */