blob: a6a0e42f4943fea82016cbcc2055141c35c4a83d [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskinef6279312021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
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 Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100131int exercise_mac_setup( psa_key_type_t key_type,
132 const unsigned char *key_bytes,
133 size_t key_length,
134 psa_algorithm_t alg,
135 psa_mac_operation_t *operation,
136 psa_status_t *status )
137{
Ronald Cron5425a212020-08-04 14:58:35 +0200138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200142 psa_set_key_algorithm( &attributes, alg );
143 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200144 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Ronald Cron5425a212020-08-04 14:58:35 +0200146 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100147 /* Whether setup succeeded or failed, abort must succeed. */
148 PSA_ASSERT( psa_mac_abort( operation ) );
149 /* If setup failed, reproduce the failure, so that the caller can
150 * test the resulting state of the operation object. */
151 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152 {
Ronald Cron5425a212020-08-04 14:58:35 +0200153 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 }
155
Ronald Cron5425a212020-08-04 14:58:35 +0200156 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 return( 1 );
158
159exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200160 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100161 return( 0 );
162}
163
164int exercise_cipher_setup( psa_key_type_t key_type,
165 const unsigned char *key_bytes,
166 size_t key_length,
167 psa_algorithm_t alg,
168 psa_cipher_operation_t *operation,
169 psa_status_t *status )
170{
Ronald Cron5425a212020-08-04 14:58:35 +0200171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
175 psa_set_key_algorithm( &attributes, alg );
176 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200177 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Ronald Cron5425a212020-08-04 14:58:35 +0200179 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100180 /* Whether setup succeeded or failed, abort must succeed. */
181 PSA_ASSERT( psa_cipher_abort( operation ) );
182 /* If setup failed, reproduce the failure, so that the caller can
183 * test the resulting state of the operation object. */
184 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 {
Ronald Cron5425a212020-08-04 14:58:35 +0200186 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 }
189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191 return( 1 );
192
193exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200194 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 return( 0 );
196}
197
Ronald Cron5425a212020-08-04 14:58:35 +0200198static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199{
200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200201 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200202 uint8_t buffer[1];
203 size_t length;
204 int ok = 0;
205
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
208 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
209 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200210 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000211 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 TEST_EQUAL(
213 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
214 TEST_EQUAL(
215 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200216 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200217 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
218 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
219 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
220 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
221
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200224 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000226 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200227
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200228 ok = 1;
229
230exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100231 /*
232 * Key attributes may have been returned by psa_get_key_attributes()
233 * thus reset them as required.
234 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200235 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 return( ok );
238}
239
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200240/* Assert that a key isn't reported as having a slot number. */
241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
242#define ASSERT_NO_SLOT_NUMBER( attributes ) \
243 do \
244 { \
245 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
246 TEST_EQUAL( psa_get_key_slot_number( \
247 attributes, \
248 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
249 PSA_ERROR_INVALID_ARGUMENT ); \
250 } \
251 while( 0 )
252#else /* MBEDTLS_PSA_CRYPTO_SE_C */
253#define ASSERT_NO_SLOT_NUMBER( attributes ) \
254 ( (void) 0 )
255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
256
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100257/* An overapproximation of the amount of storage needed for a key of the
258 * given type and with the given content. The API doesn't make it easy
259 * to find a good value for the size. The current implementation doesn't
260 * care about the value anyway. */
261#define KEY_BITS_FROM_DATA( type, data ) \
262 ( data )->len
263
Darryl Green0c6575a2018-11-07 16:05:30 +0000264typedef enum {
265 IMPORT_KEY = 0,
266 GENERATE_KEY = 1,
267 DERIVE_KEY = 2
268} generate_method;
269
Gilles Peskinee59236f2018-01-27 23:32:46 +0100270/* END_HEADER */
271
272/* BEGIN_DEPENDENCIES
273 * depends_on:MBEDTLS_PSA_CRYPTO_C
274 * END_DEPENDENCIES
275 */
276
277/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200278void static_checks( )
279{
280 size_t max_truncated_mac_size =
281 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
282
283 /* Check that the length for a truncated MAC always fits in the algorithm
284 * encoding. The shifted mask is the maximum truncated value. The
285 * untruncated algorithm may be one byte larger. */
286 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100287
288#if defined(MBEDTLS_TEST_DEPRECATED)
289 /* Check deprecated constants. */
290 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
291 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
292 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
293 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
294 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
295 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
296 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
297 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100298
Paul Elliott8ff510a2020-06-02 17:19:28 +0100299 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
324 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
328 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
329
330 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
334 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
335 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
336 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
337 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100338
Paul Elliott75e27032020-06-03 15:17:39 +0100339 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
341 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
342 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
344
345 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100347#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200348}
349/* END_CASE */
350
351/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200352void import_with_policy( int type_arg,
353 int usage_arg, int alg_arg,
354 int expected_status_arg )
355{
356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
357 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200359 psa_key_type_t type = type_arg;
360 psa_key_usage_t usage = usage_arg;
361 psa_algorithm_t alg = alg_arg;
362 psa_status_t expected_status = expected_status_arg;
363 const uint8_t key_material[16] = {0};
364 psa_status_t status;
365
366 PSA_ASSERT( psa_crypto_init( ) );
367
368 psa_set_key_type( &attributes, type );
369 psa_set_key_usage_flags( &attributes, usage );
370 psa_set_key_algorithm( &attributes, alg );
371
372 status = psa_import_key( &attributes,
373 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200374 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200375 TEST_EQUAL( status, expected_status );
376 if( status != PSA_SUCCESS )
377 goto exit;
378
Ronald Cron5425a212020-08-04 14:58:35 +0200379 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
381 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
382 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200383 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200384
Ronald Cron5425a212020-08-04 14:58:35 +0200385 PSA_ASSERT( psa_destroy_key( key ) );
386 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200387
388exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100389 /*
390 * Key attributes may have been returned by psa_get_key_attributes()
391 * thus reset them as required.
392 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100394
395 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200396 PSA_DONE( );
397}
398/* END_CASE */
399
400/* BEGIN_CASE */
401void import_with_data( data_t *data, int type_arg,
402 int attr_bits_arg,
403 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200404{
405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
406 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200408 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200409 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200410 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412
Gilles Peskine8817f612018-12-18 00:18:46 +0100413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100414
Gilles Peskine4747d192019-04-17 15:05:45 +0200415 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200416 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200417
Ronald Cron5425a212020-08-04 14:58:35 +0200418 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100419 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200420 if( status != PSA_SUCCESS )
421 goto exit;
422
Ronald Cron5425a212020-08-04 14:58:35 +0200423 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200424 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200425 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200426 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200427 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200428
Ronald Cron5425a212020-08-04 14:58:35 +0200429 PSA_ASSERT( psa_destroy_key( key ) );
430 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200437 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100438
439 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200440 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441}
442/* END_CASE */
443
444/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200445void import_large_key( int type_arg, int byte_size_arg,
446 int expected_status_arg )
447{
448 psa_key_type_t type = type_arg;
449 size_t byte_size = byte_size_arg;
450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
451 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200453 psa_status_t status;
454 uint8_t *buffer = NULL;
455 size_t buffer_size = byte_size + 1;
456 size_t n;
457
Steven Cooreman69967ce2021-01-18 18:01:08 +0100458 /* Skip the test case if the target running the test cannot
459 * accomodate large keys due to heap size constraints */
460 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200461 memset( buffer, 'K', byte_size );
462
463 PSA_ASSERT( psa_crypto_init( ) );
464
465 /* Try importing the key */
466 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
467 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200468 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100469 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200470 TEST_EQUAL( status, expected_status );
471
472 if( status == PSA_SUCCESS )
473 {
Ronald Cron5425a212020-08-04 14:58:35 +0200474 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200475 TEST_EQUAL( psa_get_key_type( &attributes ), type );
476 TEST_EQUAL( psa_get_key_bits( &attributes ),
477 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200478 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200479 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200480 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200481 for( n = 0; n < byte_size; n++ )
482 TEST_EQUAL( buffer[n], 'K' );
483 for( n = byte_size; n < buffer_size; n++ )
484 TEST_EQUAL( buffer[n], 0 );
485 }
486
487exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100488 /*
489 * Key attributes may have been returned by psa_get_key_attributes()
490 * thus reset them as required.
491 */
492 psa_reset_key_attributes( &attributes );
493
Ronald Cron5425a212020-08-04 14:58:35 +0200494 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200495 PSA_DONE( );
496 mbedtls_free( buffer );
497}
498/* END_CASE */
499
500/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200501void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
502{
Ronald Cron5425a212020-08-04 14:58:35 +0200503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200504 size_t bits = bits_arg;
505 psa_status_t expected_status = expected_status_arg;
506 psa_status_t status;
507 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200508 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509 size_t buffer_size = /* Slight overapproximations */
510 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200511 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200512 unsigned char *p;
513 int ret;
514 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516
Gilles Peskine8817f612018-12-18 00:18:46 +0100517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200518 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200519
520 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
521 bits, keypair ) ) >= 0 );
522 length = ret;
523
524 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200525 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200526 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100527 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200528
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200529 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200530 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200531
532exit:
533 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200534 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200535}
536/* END_CASE */
537
538/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300539void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300540 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200541 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 int expected_bits,
543 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200544 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 int canonical_input )
546{
Ronald Cron5425a212020-08-04 14:58:35 +0200547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200549 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200550 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 unsigned char *exported = NULL;
553 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100555 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200558 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559
Moran Pekercb088e72018-07-17 17:36:59 +0300560 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200561 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200563 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100564 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565
Gilles Peskine4747d192019-04-17 15:05:45 +0200566 psa_set_key_usage_flags( &attributes, usage_arg );
567 psa_set_key_algorithm( &attributes, alg );
568 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700569
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572
573 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200574 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200575 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
576 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200577 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578
579 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200580 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100581 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100582
583 /* The exported length must be set by psa_export_key() to a value between 0
584 * and export_size. On errors, the exported length must be 0. */
585 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
586 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
587 TEST_ASSERT( exported_length <= export_size );
588
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200589 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200590 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200592 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100593 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200595 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596
Gilles Peskineea38a922021-02-13 00:05:16 +0100597 /* Run sanity checks on the exported key. For non-canonical inputs,
598 * this validates the canonical representations. For canonical inputs,
599 * this doesn't directly validate the implementation, but it still helps
600 * by cross-validating the test data with the sanity check code. */
601 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200602 goto exit;
603
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200605 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 else
607 {
Ronald Cron5425a212020-08-04 14:58:35 +0200608 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200609 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200610 &key2 ) );
611 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100612 reexported,
613 export_size,
614 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200615 ASSERT_COMPARE( exported, exported_length,
616 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200617 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100619 TEST_ASSERT( exported_length <=
620 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
621 psa_get_key_bits( &got_attributes ) ) );
622 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623
624destroy:
625 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200626 PSA_ASSERT( psa_destroy_key( key ) );
627 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628
629exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100630 /*
631 * Key attributes may have been returned by psa_get_key_attributes()
632 * thus reset them as required.
633 */
634 psa_reset_key_attributes( &got_attributes );
635
itayzafrir3e02b3b2018-06-12 17:06:52 +0300636 mbedtls_free( exported );
637 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200638 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639}
640/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100641
Moran Pekerf709f4a2018-06-06 17:26:04 +0300642/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200644 int type_arg,
645 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100646 int export_size_delta,
647 int expected_export_status_arg,
648 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300649{
Ronald Cron5425a212020-08-04 14:58:35 +0200650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300651 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200652 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200653 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300654 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100656 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100657 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300659
Gilles Peskine8817f612018-12-18 00:18:46 +0100660 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300661
Gilles Peskine4747d192019-04-17 15:05:45 +0200662 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
663 psa_set_key_algorithm( &attributes, alg );
664 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665
666 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200667 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300668
Gilles Peskine49c25912018-10-29 15:15:31 +0100669 /* Export the public key */
670 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200671 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200672 exported, export_size,
673 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100674 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100675 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100676 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200677 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100678 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200679 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200680 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100681 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100682 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100683 TEST_ASSERT( expected_public_key->len <=
684 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
685 TEST_ASSERT( expected_public_key->len <=
686 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100687 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
688 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100689 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300690
691exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100692 /*
693 * Key attributes may have been returned by psa_get_key_attributes()
694 * thus reset them as required.
695 */
696 psa_reset_key_attributes( &attributes );
697
itayzafrir3e02b3b2018-06-12 17:06:52 +0300698 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200699 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200700 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300701}
702/* END_CASE */
703
Gilles Peskine20035e32018-02-03 22:44:14 +0100704/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200705void import_and_exercise_key( data_t *data,
706 int type_arg,
707 int bits_arg,
708 int alg_arg )
709{
Ronald Cron5425a212020-08-04 14:58:35 +0200710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200711 psa_key_type_t type = type_arg;
712 size_t bits = bits_arg;
713 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100714 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200716 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200717
Gilles Peskine8817f612018-12-18 00:18:46 +0100718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200719
Gilles Peskine4747d192019-04-17 15:05:45 +0200720 psa_set_key_usage_flags( &attributes, usage );
721 psa_set_key_algorithm( &attributes, alg );
722 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200723
724 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200726
727 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200729 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
730 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200731
732 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100733 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200734 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200735
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_destroy_key( key ) );
737 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200738
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200739exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100740 /*
741 * Key attributes may have been returned by psa_get_key_attributes()
742 * thus reset them as required.
743 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200744 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100745
746 psa_reset_key_attributes( &attributes );
747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200748 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200749}
750/* END_CASE */
751
752/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100753void effective_key_attributes( int type_arg, int expected_type_arg,
754 int bits_arg, int expected_bits_arg,
755 int usage_arg, int expected_usage_arg,
756 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200757{
Ronald Cron5425a212020-08-04 14:58:35 +0200758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100759 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100760 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100761 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100762 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200763 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100764 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200765 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100766 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200768
Gilles Peskine8817f612018-12-18 00:18:46 +0100769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200770
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200771 psa_set_key_usage_flags( &attributes, usage );
772 psa_set_key_algorithm( &attributes, alg );
773 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100774 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200775
Ronald Cron5425a212020-08-04 14:58:35 +0200776 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100777 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200778
Ronald Cron5425a212020-08-04 14:58:35 +0200779 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100780 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
781 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
782 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
783 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200784
785exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100786 /*
787 * Key attributes may have been returned by psa_get_key_attributes()
788 * thus reset them as required.
789 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200790 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100791
792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200793 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200794}
795/* END_CASE */
796
797/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100798void check_key_policy( int type_arg, int bits_arg,
799 int usage_arg, int alg_arg )
800{
801 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
802 usage_arg, usage_arg, alg_arg, alg_arg );
803 goto exit;
804}
805/* END_CASE */
806
807/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200808void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000809{
810 /* Test each valid way of initializing the object, except for `= {0}`, as
811 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
812 * though it's OK by the C standard. We could test for this, but we'd need
813 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200814 psa_key_attributes_t func = psa_key_attributes_init( );
815 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
816 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000817
818 memset( &zero, 0, sizeof( zero ) );
819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200820 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
821 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
822 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000823
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200824 TEST_EQUAL( psa_get_key_type( &func ), 0 );
825 TEST_EQUAL( psa_get_key_type( &init ), 0 );
826 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
827
828 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
829 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
830 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
831
832 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
833 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
834 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
835
836 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
837 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
838 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000839}
840/* END_CASE */
841
842/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200843void mac_key_policy( int policy_usage,
844 int policy_alg,
845 int key_type,
846 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100847 int exercise_alg,
848 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200849{
Ronald Cron5425a212020-08-04 14:58:35 +0200850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200851 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000852 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100854 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200855 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200856
Gilles Peskine8817f612018-12-18 00:18:46 +0100857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200859 psa_set_key_usage_flags( &attributes, policy_usage );
860 psa_set_key_algorithm( &attributes, policy_alg );
861 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200862
Gilles Peskine049c7532019-05-15 20:22:09 +0200863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200864 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200865
Ronald Cron5425a212020-08-04 14:58:35 +0200866 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100867 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100868 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100869 else
870 TEST_EQUAL( status, expected_status );
871
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200873
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200875 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100876 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100878 else
879 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880
881exit:
882 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200883 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200884 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200885}
886/* END_CASE */
887
888/* BEGIN_CASE */
889void cipher_key_policy( int policy_usage,
890 int policy_alg,
891 int key_type,
892 data_t *key_data,
893 int exercise_alg )
894{
Ronald Cron5425a212020-08-04 14:58:35 +0200895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000897 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898 psa_status_t status;
899
Gilles Peskine8817f612018-12-18 00:18:46 +0100900 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200902 psa_set_key_usage_flags( &attributes, policy_usage );
903 psa_set_key_algorithm( &attributes, policy_alg );
904 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905
Gilles Peskine049c7532019-05-15 20:22:09 +0200906 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200907 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908
Ronald Cron5425a212020-08-04 14:58:35 +0200909 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 if( policy_alg == exercise_alg &&
911 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100912 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200913 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100914 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 psa_cipher_abort( &operation );
916
Ronald Cron5425a212020-08-04 14:58:35 +0200917 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 if( policy_alg == exercise_alg &&
919 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100920 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100922 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200923
924exit:
925 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200926 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200927 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928}
929/* END_CASE */
930
931/* BEGIN_CASE */
932void aead_key_policy( int policy_usage,
933 int policy_alg,
934 int key_type,
935 data_t *key_data,
936 int nonce_length_arg,
937 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100938 int exercise_alg,
939 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940{
Ronald Cron5425a212020-08-04 14:58:35 +0200941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100944 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945 unsigned char nonce[16] = {0};
946 size_t nonce_length = nonce_length_arg;
947 unsigned char tag[16];
948 size_t tag_length = tag_length_arg;
949 size_t output_length;
950
951 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
952 TEST_ASSERT( tag_length <= sizeof( tag ) );
953
Gilles Peskine8817f612018-12-18 00:18:46 +0100954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200956 psa_set_key_usage_flags( &attributes, policy_usage );
957 psa_set_key_algorithm( &attributes, policy_alg );
958 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959
Gilles Peskine049c7532019-05-15 20:22:09 +0200960 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200961 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962
Ronald Cron5425a212020-08-04 14:58:35 +0200963 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 nonce, nonce_length,
965 NULL, 0,
966 NULL, 0,
967 tag, tag_length,
968 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100969 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
970 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200971 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100972 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973
974 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200975 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976 nonce, nonce_length,
977 NULL, 0,
978 tag, tag_length,
979 NULL, 0,
980 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100981 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
982 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
983 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100984 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100986 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200987
988exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200989 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200990 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200991}
992/* END_CASE */
993
994/* BEGIN_CASE */
995void asymmetric_encryption_key_policy( int policy_usage,
996 int policy_alg,
997 int key_type,
998 data_t *key_data,
999 int exercise_alg )
1000{
Ronald Cron5425a212020-08-04 14:58:35 +02001001 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003 psa_status_t status;
1004 size_t key_bits;
1005 size_t buffer_length;
1006 unsigned char *buffer = NULL;
1007 size_t output_length;
1008
Gilles Peskine8817f612018-12-18 00:18:46 +01001009 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001011 psa_set_key_usage_flags( &attributes, policy_usage );
1012 psa_set_key_algorithm( &attributes, policy_alg );
1013 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014
Gilles Peskine049c7532019-05-15 20:22:09 +02001015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001016 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001017
Ronald Cron5425a212020-08-04 14:58:35 +02001018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001019 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001020 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1021 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001022 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001023
Ronald Cron5425a212020-08-04 14:58:35 +02001024 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 NULL, 0,
1026 NULL, 0,
1027 buffer, buffer_length,
1028 &output_length );
1029 if( policy_alg == exercise_alg &&
1030 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001031 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001035 if( buffer_length != 0 )
1036 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001037 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038 buffer, buffer_length,
1039 NULL, 0,
1040 buffer, buffer_length,
1041 &output_length );
1042 if( policy_alg == exercise_alg &&
1043 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001044 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001045 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001046 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001047
1048exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001049 /*
1050 * Key attributes may have been returned by psa_get_key_attributes()
1051 * thus reset them as required.
1052 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001053 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001054
1055 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001056 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001057 mbedtls_free( buffer );
1058}
1059/* END_CASE */
1060
1061/* BEGIN_CASE */
1062void asymmetric_signature_key_policy( int policy_usage,
1063 int policy_alg,
1064 int key_type,
1065 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001066 int exercise_alg,
1067 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068{
Ronald Cron5425a212020-08-04 14:58:35 +02001069 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001071 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001072 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1073 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1074 * compatible with the policy and `payload_length_arg` is supposed to be
1075 * a valid input length to sign. If `payload_length_arg <= 0`,
1076 * `exercise_alg` is supposed to be forbidden by the policy. */
1077 int compatible_alg = payload_length_arg > 0;
1078 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001079 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001080 size_t signature_length;
1081
Gilles Peskine8817f612018-12-18 00:18:46 +01001082 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001084 psa_set_key_usage_flags( &attributes, policy_usage );
1085 psa_set_key_algorithm( &attributes, policy_alg );
1086 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087
Gilles Peskine049c7532019-05-15 20:22:09 +02001088 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001089 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001090
Ronald Cron5425a212020-08-04 14:58:35 +02001091 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001092 payload, payload_length,
1093 signature, sizeof( signature ),
1094 &signature_length );
1095 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001096 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001098 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099
1100 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001101 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001102 payload, payload_length,
1103 signature, sizeof( signature ) );
1104 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001105 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001108
1109exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001110 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001111 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001112}
1113/* END_CASE */
1114
Janos Follathba3fab92019-06-11 14:50:16 +01001115/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001116void derive_key_policy( int policy_usage,
1117 int policy_alg,
1118 int key_type,
1119 data_t *key_data,
1120 int exercise_alg )
1121{
Ronald Cron5425a212020-08-04 14:58:35 +02001122 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001124 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001125 psa_status_t status;
1126
Gilles Peskine8817f612018-12-18 00:18:46 +01001127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001128
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001129 psa_set_key_usage_flags( &attributes, policy_usage );
1130 psa_set_key_algorithm( &attributes, policy_alg );
1131 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001132
Gilles Peskine049c7532019-05-15 20:22:09 +02001133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001134 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001135
Janos Follathba3fab92019-06-11 14:50:16 +01001136 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1137
1138 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1139 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001140 {
Janos Follathba3fab92019-06-11 14:50:16 +01001141 PSA_ASSERT( psa_key_derivation_input_bytes(
1142 &operation,
1143 PSA_KEY_DERIVATION_INPUT_SEED,
1144 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001145 }
Janos Follathba3fab92019-06-11 14:50:16 +01001146
1147 status = psa_key_derivation_input_key( &operation,
1148 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001149 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001150
Gilles Peskineea0fb492018-07-12 17:17:20 +02001151 if( policy_alg == exercise_alg &&
1152 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001153 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001154 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001155 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001156
1157exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001158 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001159 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001160 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161}
1162/* END_CASE */
1163
1164/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001165void agreement_key_policy( int policy_usage,
1166 int policy_alg,
1167 int key_type_arg,
1168 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001169 int exercise_alg,
1170 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001171{
Ronald Cron5425a212020-08-04 14:58:35 +02001172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001174 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001175 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001176 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001177 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001178
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001180
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001181 psa_set_key_usage_flags( &attributes, policy_usage );
1182 psa_set_key_algorithm( &attributes, policy_alg );
1183 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184
Gilles Peskine049c7532019-05-15 20:22:09 +02001185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001186 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001187
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001188 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001189 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001190
Steven Cooremance48e852020-10-05 16:02:45 +02001191 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001192
1193exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001194 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001195 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001196 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001197}
1198/* END_CASE */
1199
1200/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001201void key_policy_alg2( int key_type_arg, data_t *key_data,
1202 int usage_arg, int alg_arg, int alg2_arg )
1203{
Ronald Cron5425a212020-08-04 14:58:35 +02001204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001205 psa_key_type_t key_type = key_type_arg;
1206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1207 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1208 psa_key_usage_t usage = usage_arg;
1209 psa_algorithm_t alg = alg_arg;
1210 psa_algorithm_t alg2 = alg2_arg;
1211
1212 PSA_ASSERT( psa_crypto_init( ) );
1213
1214 psa_set_key_usage_flags( &attributes, usage );
1215 psa_set_key_algorithm( &attributes, alg );
1216 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1217 psa_set_key_type( &attributes, key_type );
1218 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001219 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001220
Ronald Cron5425a212020-08-04 14:58:35 +02001221 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001222 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1223 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1224 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1225
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001226 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001227 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001228 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001229 goto exit;
1230
1231exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001232 /*
1233 * Key attributes may have been returned by psa_get_key_attributes()
1234 * thus reset them as required.
1235 */
1236 psa_reset_key_attributes( &got_attributes );
1237
Ronald Cron5425a212020-08-04 14:58:35 +02001238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001239 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001244void raw_agreement_key_policy( int policy_usage,
1245 int policy_alg,
1246 int key_type_arg,
1247 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001248 int exercise_alg,
1249 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001250{
Ronald Cron5425a212020-08-04 14:58:35 +02001251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001254 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001255 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001256 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001257
1258 PSA_ASSERT( psa_crypto_init( ) );
1259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001260 psa_set_key_usage_flags( &attributes, policy_usage );
1261 psa_set_key_algorithm( &attributes, policy_alg );
1262 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001263
Gilles Peskine049c7532019-05-15 20:22:09 +02001264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001265 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001267 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001268
Steven Cooremance48e852020-10-05 16:02:45 +02001269 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001270
1271exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001272 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001273 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001274 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001275}
1276/* END_CASE */
1277
1278/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001279void copy_success( int source_usage_arg,
1280 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001281 int type_arg, data_t *material,
1282 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001283 int target_usage_arg,
1284 int target_alg_arg, int target_alg2_arg,
1285 int expected_usage_arg,
1286 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001287{
Gilles Peskineca25db92019-04-19 11:43:08 +02001288 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1289 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290 psa_key_usage_t expected_usage = expected_usage_arg;
1291 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001292 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001293 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1294 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001295 uint8_t *export_buffer = NULL;
1296
Gilles Peskine57ab7212019-01-28 13:03:09 +01001297 PSA_ASSERT( psa_crypto_init( ) );
1298
Gilles Peskineca25db92019-04-19 11:43:08 +02001299 /* Prepare the source key. */
1300 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1301 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001302 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001303 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001304 PSA_ASSERT( psa_import_key( &source_attributes,
1305 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001306 &source_key ) );
1307 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001308
Gilles Peskineca25db92019-04-19 11:43:08 +02001309 /* Prepare the target attributes. */
1310 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001311 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001313 /* Set volatile lifetime to reset the key identifier to 0. */
1314 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1315 }
1316
Gilles Peskineca25db92019-04-19 11:43:08 +02001317 if( target_usage_arg != -1 )
1318 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1319 if( target_alg_arg != -1 )
1320 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001321 if( target_alg2_arg != -1 )
1322 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001323
1324 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001325 PSA_ASSERT( psa_copy_key( source_key,
1326 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001327
1328 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001329 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001333 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1334 psa_get_key_type( &target_attributes ) );
1335 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1336 psa_get_key_bits( &target_attributes ) );
1337 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1338 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001339 TEST_EQUAL( expected_alg2,
1340 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001341 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1342 {
1343 size_t length;
1344 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001345 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001346 material->len, &length ) );
1347 ASSERT_COMPARE( material->x, material->len,
1348 export_buffer, length );
1349 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001350
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001351 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001352 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001353 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001354 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355
Ronald Cron5425a212020-08-04 14:58:35 +02001356 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001357
1358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001359 /*
1360 * Source and target key attributes may have been returned by
1361 * psa_get_key_attributes() thus reset them as required.
1362 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001363 psa_reset_key_attributes( &source_attributes );
1364 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001365
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001366 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001367 mbedtls_free( export_buffer );
1368}
1369/* END_CASE */
1370
1371/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001372void copy_fail( int source_usage_arg,
1373 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001374 int type_arg, data_t *material,
1375 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001376 int target_usage_arg,
1377 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001378 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001379 int expected_status_arg )
1380{
1381 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1382 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001383 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1384 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001385 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001386
1387 PSA_ASSERT( psa_crypto_init( ) );
1388
1389 /* Prepare the source key. */
1390 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1391 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001392 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001393 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001394 PSA_ASSERT( psa_import_key( &source_attributes,
1395 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001396 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001397
1398 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001399 psa_set_key_id( &target_attributes, key_id );
1400 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001401 psa_set_key_type( &target_attributes, target_type_arg );
1402 psa_set_key_bits( &target_attributes, target_bits_arg );
1403 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1404 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001405 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001406
1407 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001408 TEST_EQUAL( psa_copy_key( source_key,
1409 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001410 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001411
Ronald Cron5425a212020-08-04 14:58:35 +02001412 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001413
Gilles Peskine4a644642019-05-03 17:14:08 +02001414exit:
1415 psa_reset_key_attributes( &source_attributes );
1416 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001417 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001418}
1419/* END_CASE */
1420
1421/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001422void hash_operation_init( )
1423{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001424 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001425 /* Test each valid way of initializing the object, except for `= {0}`, as
1426 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1427 * though it's OK by the C standard. We could test for this, but we'd need
1428 * to supress the Clang warning for the test. */
1429 psa_hash_operation_t func = psa_hash_operation_init( );
1430 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1431 psa_hash_operation_t zero;
1432
1433 memset( &zero, 0, sizeof( zero ) );
1434
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001435 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001436 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1437 PSA_ERROR_BAD_STATE );
1438 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1439 PSA_ERROR_BAD_STATE );
1440 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1441 PSA_ERROR_BAD_STATE );
1442
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001443 /* A default hash operation should be abortable without error. */
1444 PSA_ASSERT( psa_hash_abort( &func ) );
1445 PSA_ASSERT( psa_hash_abort( &init ) );
1446 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001447}
1448/* END_CASE */
1449
1450/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001451void hash_setup( int alg_arg,
1452 int expected_status_arg )
1453{
1454 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001455 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001456 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001457 psa_status_t status;
1458
Gilles Peskine8817f612018-12-18 00:18:46 +01001459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001461 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001462 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001464 /* Whether setup succeeded or failed, abort must succeed. */
1465 PSA_ASSERT( psa_hash_abort( &operation ) );
1466
1467 /* If setup failed, reproduce the failure, so as to
1468 * test the resulting state of the operation object. */
1469 if( status != PSA_SUCCESS )
1470 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1471
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001472 /* Now the operation object should be reusable. */
1473#if defined(KNOWN_SUPPORTED_HASH_ALG)
1474 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1475 PSA_ASSERT( psa_hash_abort( &operation ) );
1476#endif
1477
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001478exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001479 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001480}
1481/* END_CASE */
1482
1483/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001484void hash_compute_fail( int alg_arg, data_t *input,
1485 int output_size_arg, int expected_status_arg )
1486{
1487 psa_algorithm_t alg = alg_arg;
1488 uint8_t *output = NULL;
1489 size_t output_size = output_size_arg;
1490 size_t output_length = INVALID_EXPORT_LENGTH;
1491 psa_status_t expected_status = expected_status_arg;
1492 psa_status_t status;
1493
1494 ASSERT_ALLOC( output, output_size );
1495
1496 PSA_ASSERT( psa_crypto_init( ) );
1497
1498 status = psa_hash_compute( alg, input->x, input->len,
1499 output, output_size, &output_length );
1500 TEST_EQUAL( status, expected_status );
1501 TEST_ASSERT( output_length <= output_size );
1502
1503exit:
1504 mbedtls_free( output );
1505 PSA_DONE( );
1506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001510void hash_compare_fail( int alg_arg, data_t *input,
1511 data_t *reference_hash,
1512 int expected_status_arg )
1513{
1514 psa_algorithm_t alg = alg_arg;
1515 psa_status_t expected_status = expected_status_arg;
1516 psa_status_t status;
1517
1518 PSA_ASSERT( psa_crypto_init( ) );
1519
1520 status = psa_hash_compare( alg, input->x, input->len,
1521 reference_hash->x, reference_hash->len );
1522 TEST_EQUAL( status, expected_status );
1523
1524exit:
1525 PSA_DONE( );
1526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001530void hash_compute_compare( int alg_arg, data_t *input,
1531 data_t *expected_output )
1532{
1533 psa_algorithm_t alg = alg_arg;
1534 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1535 size_t output_length = INVALID_EXPORT_LENGTH;
1536 size_t i;
1537
1538 PSA_ASSERT( psa_crypto_init( ) );
1539
1540 /* Compute with tight buffer */
1541 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001542 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001543 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001544 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001545 ASSERT_COMPARE( output, output_length,
1546 expected_output->x, expected_output->len );
1547
1548 /* Compute with larger buffer */
1549 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1550 output, sizeof( output ),
1551 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001552 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001553 ASSERT_COMPARE( output, output_length,
1554 expected_output->x, expected_output->len );
1555
1556 /* Compare with correct hash */
1557 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1558 output, output_length ) );
1559
1560 /* Compare with trailing garbage */
1561 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1562 output, output_length + 1 ),
1563 PSA_ERROR_INVALID_SIGNATURE );
1564
1565 /* Compare with truncated hash */
1566 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1567 output, output_length - 1 ),
1568 PSA_ERROR_INVALID_SIGNATURE );
1569
1570 /* Compare with corrupted value */
1571 for( i = 0; i < output_length; i++ )
1572 {
Chris Jones9634bb12021-01-20 15:56:42 +00001573 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001574 output[i] ^= 1;
1575 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1576 output, output_length ),
1577 PSA_ERROR_INVALID_SIGNATURE );
1578 output[i] ^= 1;
1579 }
1580
1581exit:
1582 PSA_DONE( );
1583}
1584/* END_CASE */
1585
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001586/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001587void hash_bad_order( )
1588{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001589 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001590 unsigned char input[] = "";
1591 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001592 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001593 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1594 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1595 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001596 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001597 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001598 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001599
Gilles Peskine8817f612018-12-18 00:18:46 +01001600 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001601
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001602 /* Call setup twice in a row. */
1603 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1604 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1605 PSA_ERROR_BAD_STATE );
1606 PSA_ASSERT( psa_hash_abort( &operation ) );
1607
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001608 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001609 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001610 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001611 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001612
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001613 /* Call update after finish. */
1614 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1615 PSA_ASSERT( psa_hash_finish( &operation,
1616 hash, sizeof( hash ), &hash_len ) );
1617 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001618 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001619 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001620
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001621 /* Call verify without calling setup beforehand. */
1622 TEST_EQUAL( psa_hash_verify( &operation,
1623 valid_hash, sizeof( valid_hash ) ),
1624 PSA_ERROR_BAD_STATE );
1625 PSA_ASSERT( psa_hash_abort( &operation ) );
1626
1627 /* Call verify after finish. */
1628 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1629 PSA_ASSERT( psa_hash_finish( &operation,
1630 hash, sizeof( hash ), &hash_len ) );
1631 TEST_EQUAL( psa_hash_verify( &operation,
1632 valid_hash, sizeof( valid_hash ) ),
1633 PSA_ERROR_BAD_STATE );
1634 PSA_ASSERT( psa_hash_abort( &operation ) );
1635
1636 /* Call verify twice in a row. */
1637 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1638 PSA_ASSERT( psa_hash_verify( &operation,
1639 valid_hash, sizeof( valid_hash ) ) );
1640 TEST_EQUAL( psa_hash_verify( &operation,
1641 valid_hash, sizeof( valid_hash ) ),
1642 PSA_ERROR_BAD_STATE );
1643 PSA_ASSERT( psa_hash_abort( &operation ) );
1644
1645 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001646 TEST_EQUAL( psa_hash_finish( &operation,
1647 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001648 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001649 PSA_ASSERT( psa_hash_abort( &operation ) );
1650
1651 /* Call finish twice in a row. */
1652 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1653 PSA_ASSERT( psa_hash_finish( &operation,
1654 hash, sizeof( hash ), &hash_len ) );
1655 TEST_EQUAL( psa_hash_finish( &operation,
1656 hash, sizeof( hash ), &hash_len ),
1657 PSA_ERROR_BAD_STATE );
1658 PSA_ASSERT( psa_hash_abort( &operation ) );
1659
1660 /* Call finish after calling verify. */
1661 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1662 PSA_ASSERT( psa_hash_verify( &operation,
1663 valid_hash, sizeof( valid_hash ) ) );
1664 TEST_EQUAL( psa_hash_finish( &operation,
1665 hash, sizeof( hash ), &hash_len ),
1666 PSA_ERROR_BAD_STATE );
1667 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001668
1669exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001670 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001671}
1672/* END_CASE */
1673
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001674/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001675void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001676{
1677 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001678 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1679 * appended to it */
1680 unsigned char hash[] = {
1681 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1682 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1683 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001684 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001685 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001686
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001688
itayzafrir27e69452018-11-01 14:26:34 +02001689 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001691 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001692 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001693
itayzafrir27e69452018-11-01 14:26:34 +02001694 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001696 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001697 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001698
itayzafrir27e69452018-11-01 14:26:34 +02001699 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001700 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001701 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001703
itayzafrirec93d302018-10-18 18:01:10 +03001704exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001705 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001706}
1707/* END_CASE */
1708
Ronald Cronee414c72021-03-18 18:50:08 +01001709/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001710void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001711{
1712 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001713 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001714 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001716 size_t hash_len;
1717
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001719
itayzafrir58028322018-10-25 10:22:01 +03001720 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001721 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001722 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001723 hash, expected_size - 1, &hash_len ),
1724 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001725
1726exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001727 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001728}
1729/* END_CASE */
1730
Ronald Cronee414c72021-03-18 18:50:08 +01001731/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001732void hash_clone_source_state( )
1733{
1734 psa_algorithm_t alg = PSA_ALG_SHA_256;
1735 unsigned char hash[PSA_HASH_MAX_SIZE];
1736 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1737 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1738 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1739 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1740 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1741 size_t hash_len;
1742
1743 PSA_ASSERT( psa_crypto_init( ) );
1744 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1745
1746 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1747 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1748 PSA_ASSERT( psa_hash_finish( &op_finished,
1749 hash, sizeof( hash ), &hash_len ) );
1750 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1751 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1752
1753 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1754 PSA_ERROR_BAD_STATE );
1755
1756 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1757 PSA_ASSERT( psa_hash_finish( &op_init,
1758 hash, sizeof( hash ), &hash_len ) );
1759 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1760 PSA_ASSERT( psa_hash_finish( &op_finished,
1761 hash, sizeof( hash ), &hash_len ) );
1762 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1763 PSA_ASSERT( psa_hash_finish( &op_aborted,
1764 hash, sizeof( hash ), &hash_len ) );
1765
1766exit:
1767 psa_hash_abort( &op_source );
1768 psa_hash_abort( &op_init );
1769 psa_hash_abort( &op_setup );
1770 psa_hash_abort( &op_finished );
1771 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001772 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001773}
1774/* END_CASE */
1775
Ronald Cronee414c72021-03-18 18:50:08 +01001776/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001777void hash_clone_target_state( )
1778{
1779 psa_algorithm_t alg = PSA_ALG_SHA_256;
1780 unsigned char hash[PSA_HASH_MAX_SIZE];
1781 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1783 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1784 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1785 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1786 size_t hash_len;
1787
1788 PSA_ASSERT( psa_crypto_init( ) );
1789
1790 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1791 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1792 PSA_ASSERT( psa_hash_finish( &op_finished,
1793 hash, sizeof( hash ), &hash_len ) );
1794 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1795 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1796
1797 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1798 PSA_ASSERT( psa_hash_finish( &op_target,
1799 hash, sizeof( hash ), &hash_len ) );
1800
1801 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1802 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1803 PSA_ERROR_BAD_STATE );
1804 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1805 PSA_ERROR_BAD_STATE );
1806
1807exit:
1808 psa_hash_abort( &op_target );
1809 psa_hash_abort( &op_init );
1810 psa_hash_abort( &op_setup );
1811 psa_hash_abort( &op_finished );
1812 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001813 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001814}
1815/* END_CASE */
1816
itayzafrir58028322018-10-25 10:22:01 +03001817/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001818void mac_operation_init( )
1819{
Jaeden Amero252ef282019-02-15 14:05:35 +00001820 const uint8_t input[1] = { 0 };
1821
Jaeden Amero769ce272019-01-04 11:48:03 +00001822 /* Test each valid way of initializing the object, except for `= {0}`, as
1823 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1824 * though it's OK by the C standard. We could test for this, but we'd need
1825 * to supress the Clang warning for the test. */
1826 psa_mac_operation_t func = psa_mac_operation_init( );
1827 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1828 psa_mac_operation_t zero;
1829
1830 memset( &zero, 0, sizeof( zero ) );
1831
Jaeden Amero252ef282019-02-15 14:05:35 +00001832 /* A freshly-initialized MAC operation should not be usable. */
1833 TEST_EQUAL( psa_mac_update( &func,
1834 input, sizeof( input ) ),
1835 PSA_ERROR_BAD_STATE );
1836 TEST_EQUAL( psa_mac_update( &init,
1837 input, sizeof( input ) ),
1838 PSA_ERROR_BAD_STATE );
1839 TEST_EQUAL( psa_mac_update( &zero,
1840 input, sizeof( input ) ),
1841 PSA_ERROR_BAD_STATE );
1842
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001843 /* A default MAC operation should be abortable without error. */
1844 PSA_ASSERT( psa_mac_abort( &func ) );
1845 PSA_ASSERT( psa_mac_abort( &init ) );
1846 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001847}
1848/* END_CASE */
1849
1850/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001851void mac_setup( int key_type_arg,
1852 data_t *key,
1853 int alg_arg,
1854 int expected_status_arg )
1855{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001856 psa_key_type_t key_type = key_type_arg;
1857 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001858 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001859 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001860 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1861#if defined(KNOWN_SUPPORTED_MAC_ALG)
1862 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1863#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001864
Gilles Peskine8817f612018-12-18 00:18:46 +01001865 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001866
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001867 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1868 &operation, &status ) )
1869 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001870 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001871
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001872 /* The operation object should be reusable. */
1873#if defined(KNOWN_SUPPORTED_MAC_ALG)
1874 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1875 smoke_test_key_data,
1876 sizeof( smoke_test_key_data ),
1877 KNOWN_SUPPORTED_MAC_ALG,
1878 &operation, &status ) )
1879 goto exit;
1880 TEST_EQUAL( status, PSA_SUCCESS );
1881#endif
1882
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001883exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001884 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001885}
1886/* END_CASE */
1887
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001888/* 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 +00001889void mac_bad_order( )
1890{
Ronald Cron5425a212020-08-04 14:58:35 +02001891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001892 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1893 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001894 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001895 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1896 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1897 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001899 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1900 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1901 size_t sign_mac_length = 0;
1902 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1903 const uint8_t verify_mac[] = {
1904 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1905 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1906 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1907
1908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001910 psa_set_key_algorithm( &attributes, alg );
1911 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001912
Ronald Cron5425a212020-08-04 14:58:35 +02001913 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1914 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001915
Jaeden Amero252ef282019-02-15 14:05:35 +00001916 /* Call update without calling setup beforehand. */
1917 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1918 PSA_ERROR_BAD_STATE );
1919 PSA_ASSERT( psa_mac_abort( &operation ) );
1920
1921 /* Call sign finish without calling setup beforehand. */
1922 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1923 &sign_mac_length),
1924 PSA_ERROR_BAD_STATE );
1925 PSA_ASSERT( psa_mac_abort( &operation ) );
1926
1927 /* Call verify finish without calling setup beforehand. */
1928 TEST_EQUAL( psa_mac_verify_finish( &operation,
1929 verify_mac, sizeof( verify_mac ) ),
1930 PSA_ERROR_BAD_STATE );
1931 PSA_ASSERT( psa_mac_abort( &operation ) );
1932
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001933 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001934 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1935 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001936 PSA_ERROR_BAD_STATE );
1937 PSA_ASSERT( psa_mac_abort( &operation ) );
1938
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001940 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001941 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1942 PSA_ASSERT( psa_mac_sign_finish( &operation,
1943 sign_mac, sizeof( sign_mac ),
1944 &sign_mac_length ) );
1945 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1946 PSA_ERROR_BAD_STATE );
1947 PSA_ASSERT( psa_mac_abort( &operation ) );
1948
1949 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001950 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001951 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1952 PSA_ASSERT( psa_mac_verify_finish( &operation,
1953 verify_mac, sizeof( verify_mac ) ) );
1954 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1955 PSA_ERROR_BAD_STATE );
1956 PSA_ASSERT( psa_mac_abort( &operation ) );
1957
1958 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001959 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001960 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1961 PSA_ASSERT( psa_mac_sign_finish( &operation,
1962 sign_mac, sizeof( sign_mac ),
1963 &sign_mac_length ) );
1964 TEST_EQUAL( psa_mac_sign_finish( &operation,
1965 sign_mac, sizeof( sign_mac ),
1966 &sign_mac_length ),
1967 PSA_ERROR_BAD_STATE );
1968 PSA_ASSERT( psa_mac_abort( &operation ) );
1969
1970 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001971 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001972 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1973 PSA_ASSERT( psa_mac_verify_finish( &operation,
1974 verify_mac, sizeof( verify_mac ) ) );
1975 TEST_EQUAL( psa_mac_verify_finish( &operation,
1976 verify_mac, sizeof( verify_mac ) ),
1977 PSA_ERROR_BAD_STATE );
1978 PSA_ASSERT( psa_mac_abort( &operation ) );
1979
1980 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001981 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001982 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1983 TEST_EQUAL( psa_mac_verify_finish( &operation,
1984 verify_mac, sizeof( verify_mac ) ),
1985 PSA_ERROR_BAD_STATE );
1986 PSA_ASSERT( psa_mac_abort( &operation ) );
1987
1988 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001989 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001990 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1991 TEST_EQUAL( psa_mac_sign_finish( &operation,
1992 sign_mac, sizeof( sign_mac ),
1993 &sign_mac_length ),
1994 PSA_ERROR_BAD_STATE );
1995 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001996
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001998
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001999exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002000 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002001}
2002/* END_CASE */
2003
2004/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002006 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002007 int alg_arg,
2008 data_t *input,
2009 data_t *expected_mac )
2010{
Ronald Cron5425a212020-08-04 14:58:35 +02002011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002012 psa_key_type_t key_type = key_type_arg;
2013 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002014 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002016 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002017 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002018 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002019 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002020 const size_t output_sizes_to_test[] = {
2021 0,
2022 1,
2023 expected_mac->len - 1,
2024 expected_mac->len,
2025 expected_mac->len + 1,
2026 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002027
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002028 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002029 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002030 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002031
Gilles Peskine8817f612018-12-18 00:18:46 +01002032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002033
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002034 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002035 psa_set_key_algorithm( &attributes, alg );
2036 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037
Ronald Cron5425a212020-08-04 14:58:35 +02002038 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2039 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002040
Gilles Peskine8b356b52020-08-25 23:44:59 +02002041 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2042 {
2043 const size_t output_size = output_sizes_to_test[i];
2044 psa_status_t expected_status =
2045 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2046 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002047
Chris Jones9634bb12021-01-20 15:56:42 +00002048 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002049 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002050
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002051 /* Calculate the MAC, one-shot case. */
2052 TEST_EQUAL( psa_mac_compute( key, alg,
2053 input->x, input->len,
2054 actual_mac, output_size, &mac_length ),
2055 expected_status );
2056 if( expected_status == PSA_SUCCESS )
2057 {
2058 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2059 actual_mac, mac_length );
2060 }
2061
2062 if( output_size > 0 )
2063 memset( actual_mac, 0, output_size );
2064
2065 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002066 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002067 PSA_ASSERT( psa_mac_update( &operation,
2068 input->x, input->len ) );
2069 TEST_EQUAL( psa_mac_sign_finish( &operation,
2070 actual_mac, output_size,
2071 &mac_length ),
2072 expected_status );
2073 PSA_ASSERT( psa_mac_abort( &operation ) );
2074
2075 if( expected_status == PSA_SUCCESS )
2076 {
2077 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2078 actual_mac, mac_length );
2079 }
2080 mbedtls_free( actual_mac );
2081 actual_mac = NULL;
2082 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002083
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002084exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002085 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002086 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002087 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002088 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002089}
2090/* END_CASE */
2091
2092/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002093void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002094 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002095 int alg_arg,
2096 data_t *input,
2097 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002098{
Ronald Cron5425a212020-08-04 14:58:35 +02002099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002100 psa_key_type_t key_type = key_type_arg;
2101 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002102 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002104 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002105
Gilles Peskine69c12672018-06-28 00:07:19 +02002106 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2107
Gilles Peskine8817f612018-12-18 00:18:46 +01002108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002109
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002110 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002111 psa_set_key_algorithm( &attributes, alg );
2112 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002113
Ronald Cron5425a212020-08-04 14:58:35 +02002114 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2115 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002116
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002117 /* Verify correct MAC, one-shot case. */
2118 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2119 expected_mac->x, expected_mac->len ) );
2120
2121 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002122 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002123 PSA_ASSERT( psa_mac_update( &operation,
2124 input->x, input->len ) );
2125 PSA_ASSERT( psa_mac_verify_finish( &operation,
2126 expected_mac->x,
2127 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002129 /* Test a MAC that's too short, one-shot case. */
2130 TEST_EQUAL( psa_mac_verify( key, alg,
2131 input->x, input->len,
2132 expected_mac->x,
2133 expected_mac->len - 1 ),
2134 PSA_ERROR_INVALID_SIGNATURE );
2135
2136 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002137 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002138 PSA_ASSERT( psa_mac_update( &operation,
2139 input->x, input->len ) );
2140 TEST_EQUAL( psa_mac_verify_finish( &operation,
2141 expected_mac->x,
2142 expected_mac->len - 1 ),
2143 PSA_ERROR_INVALID_SIGNATURE );
2144
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002145 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002146 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2147 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002148 TEST_EQUAL( psa_mac_verify( key, alg,
2149 input->x, input->len,
2150 perturbed_mac, expected_mac->len + 1 ),
2151 PSA_ERROR_INVALID_SIGNATURE );
2152
2153 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002154 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002155 PSA_ASSERT( psa_mac_update( &operation,
2156 input->x, input->len ) );
2157 TEST_EQUAL( psa_mac_verify_finish( &operation,
2158 perturbed_mac,
2159 expected_mac->len + 1 ),
2160 PSA_ERROR_INVALID_SIGNATURE );
2161
2162 /* Test changing one byte. */
2163 for( size_t i = 0; i < expected_mac->len; i++ )
2164 {
Chris Jones9634bb12021-01-20 15:56:42 +00002165 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002166 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002167
2168 TEST_EQUAL( psa_mac_verify( key, alg,
2169 input->x, input->len,
2170 perturbed_mac, expected_mac->len ),
2171 PSA_ERROR_INVALID_SIGNATURE );
2172
Ronald Cron5425a212020-08-04 14:58:35 +02002173 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002174 PSA_ASSERT( psa_mac_update( &operation,
2175 input->x, input->len ) );
2176 TEST_EQUAL( psa_mac_verify_finish( &operation,
2177 perturbed_mac,
2178 expected_mac->len ),
2179 PSA_ERROR_INVALID_SIGNATURE );
2180 perturbed_mac[i] ^= 1;
2181 }
2182
Gilles Peskine8c9def32018-02-08 10:02:12 +01002183exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002184 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002186 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002187 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002188}
2189/* END_CASE */
2190
2191/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002192void cipher_operation_init( )
2193{
Jaeden Ameroab439972019-02-15 14:12:05 +00002194 const uint8_t input[1] = { 0 };
2195 unsigned char output[1] = { 0 };
2196 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002197 /* Test each valid way of initializing the object, except for `= {0}`, as
2198 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2199 * though it's OK by the C standard. We could test for this, but we'd need
2200 * to supress the Clang warning for the test. */
2201 psa_cipher_operation_t func = psa_cipher_operation_init( );
2202 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2203 psa_cipher_operation_t zero;
2204
2205 memset( &zero, 0, sizeof( zero ) );
2206
Jaeden Ameroab439972019-02-15 14:12:05 +00002207 /* A freshly-initialized cipher operation should not be usable. */
2208 TEST_EQUAL( psa_cipher_update( &func,
2209 input, sizeof( input ),
2210 output, sizeof( output ),
2211 &output_length ),
2212 PSA_ERROR_BAD_STATE );
2213 TEST_EQUAL( psa_cipher_update( &init,
2214 input, sizeof( input ),
2215 output, sizeof( output ),
2216 &output_length ),
2217 PSA_ERROR_BAD_STATE );
2218 TEST_EQUAL( psa_cipher_update( &zero,
2219 input, sizeof( input ),
2220 output, sizeof( output ),
2221 &output_length ),
2222 PSA_ERROR_BAD_STATE );
2223
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002224 /* A default cipher operation should be abortable without error. */
2225 PSA_ASSERT( psa_cipher_abort( &func ) );
2226 PSA_ASSERT( psa_cipher_abort( &init ) );
2227 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002228}
2229/* END_CASE */
2230
2231/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002232void cipher_setup( int key_type_arg,
2233 data_t *key,
2234 int alg_arg,
2235 int expected_status_arg )
2236{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002237 psa_key_type_t key_type = key_type_arg;
2238 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002239 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002240 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002242#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002243 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2244#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245
Gilles Peskine8817f612018-12-18 00:18:46 +01002246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002247
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002248 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2249 &operation, &status ) )
2250 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002251 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002252
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002253 /* The operation object should be reusable. */
2254#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2255 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2256 smoke_test_key_data,
2257 sizeof( smoke_test_key_data ),
2258 KNOWN_SUPPORTED_CIPHER_ALG,
2259 &operation, &status ) )
2260 goto exit;
2261 TEST_EQUAL( status, PSA_SUCCESS );
2262#endif
2263
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002265 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002266 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002267}
2268/* END_CASE */
2269
Ronald Cronee414c72021-03-18 18:50:08 +01002270/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002271void cipher_bad_order( )
2272{
Ronald Cron5425a212020-08-04 14:58:35 +02002273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002274 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2275 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002277 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002278 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002279 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002280 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2281 0xaa, 0xaa, 0xaa, 0xaa };
2282 const uint8_t text[] = {
2283 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2284 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002285 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002286 size_t length = 0;
2287
2288 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2290 psa_set_key_algorithm( &attributes, alg );
2291 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002292 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2293 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002294
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002295 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002296 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2297 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002298 PSA_ERROR_BAD_STATE );
2299 PSA_ASSERT( psa_cipher_abort( &operation ) );
2300
2301 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002302 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2303 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002304 PSA_ERROR_BAD_STATE );
2305 PSA_ASSERT( psa_cipher_abort( &operation ) );
2306
Jaeden Ameroab439972019-02-15 14:12:05 +00002307 /* Generate an IV without calling setup beforehand. */
2308 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2309 buffer, sizeof( buffer ),
2310 &length ),
2311 PSA_ERROR_BAD_STATE );
2312 PSA_ASSERT( psa_cipher_abort( &operation ) );
2313
2314 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002315 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002316 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2317 buffer, sizeof( buffer ),
2318 &length ) );
2319 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2320 buffer, sizeof( buffer ),
2321 &length ),
2322 PSA_ERROR_BAD_STATE );
2323 PSA_ASSERT( psa_cipher_abort( &operation ) );
2324
2325 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002326 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002327 PSA_ASSERT( psa_cipher_set_iv( &operation,
2328 iv, sizeof( iv ) ) );
2329 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2330 buffer, sizeof( buffer ),
2331 &length ),
2332 PSA_ERROR_BAD_STATE );
2333 PSA_ASSERT( psa_cipher_abort( &operation ) );
2334
2335 /* Set an IV without calling setup beforehand. */
2336 TEST_EQUAL( psa_cipher_set_iv( &operation,
2337 iv, sizeof( iv ) ),
2338 PSA_ERROR_BAD_STATE );
2339 PSA_ASSERT( psa_cipher_abort( &operation ) );
2340
2341 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002342 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002343 PSA_ASSERT( psa_cipher_set_iv( &operation,
2344 iv, sizeof( iv ) ) );
2345 TEST_EQUAL( psa_cipher_set_iv( &operation,
2346 iv, sizeof( iv ) ),
2347 PSA_ERROR_BAD_STATE );
2348 PSA_ASSERT( psa_cipher_abort( &operation ) );
2349
2350 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002351 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002352 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2353 buffer, sizeof( buffer ),
2354 &length ) );
2355 TEST_EQUAL( psa_cipher_set_iv( &operation,
2356 iv, sizeof( iv ) ),
2357 PSA_ERROR_BAD_STATE );
2358 PSA_ASSERT( psa_cipher_abort( &operation ) );
2359
2360 /* Call update without calling setup beforehand. */
2361 TEST_EQUAL( psa_cipher_update( &operation,
2362 text, sizeof( text ),
2363 buffer, sizeof( buffer ),
2364 &length ),
2365 PSA_ERROR_BAD_STATE );
2366 PSA_ASSERT( psa_cipher_abort( &operation ) );
2367
2368 /* Call update without an IV where an IV is required. */
2369 TEST_EQUAL( psa_cipher_update( &operation,
2370 text, sizeof( text ),
2371 buffer, sizeof( buffer ),
2372 &length ),
2373 PSA_ERROR_BAD_STATE );
2374 PSA_ASSERT( psa_cipher_abort( &operation ) );
2375
2376 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002378 PSA_ASSERT( psa_cipher_set_iv( &operation,
2379 iv, sizeof( iv ) ) );
2380 PSA_ASSERT( psa_cipher_finish( &operation,
2381 buffer, sizeof( buffer ), &length ) );
2382 TEST_EQUAL( psa_cipher_update( &operation,
2383 text, sizeof( text ),
2384 buffer, sizeof( buffer ),
2385 &length ),
2386 PSA_ERROR_BAD_STATE );
2387 PSA_ASSERT( psa_cipher_abort( &operation ) );
2388
2389 /* Call finish without calling setup beforehand. */
2390 TEST_EQUAL( psa_cipher_finish( &operation,
2391 buffer, sizeof( buffer ), &length ),
2392 PSA_ERROR_BAD_STATE );
2393 PSA_ASSERT( psa_cipher_abort( &operation ) );
2394
2395 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002396 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002397 /* Not calling update means we are encrypting an empty buffer, which is OK
2398 * for cipher modes with padding. */
2399 TEST_EQUAL( psa_cipher_finish( &operation,
2400 buffer, sizeof( buffer ), &length ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_cipher_abort( &operation ) );
2403
2404 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002405 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 PSA_ASSERT( psa_cipher_set_iv( &operation,
2407 iv, sizeof( iv ) ) );
2408 PSA_ASSERT( psa_cipher_finish( &operation,
2409 buffer, sizeof( buffer ), &length ) );
2410 TEST_EQUAL( psa_cipher_finish( &operation,
2411 buffer, sizeof( buffer ), &length ),
2412 PSA_ERROR_BAD_STATE );
2413 PSA_ASSERT( psa_cipher_abort( &operation ) );
2414
Ronald Cron5425a212020-08-04 14:58:35 +02002415 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002416
Jaeden Ameroab439972019-02-15 14:12:05 +00002417exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002418 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002419 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002420}
2421/* END_CASE */
2422
2423/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002424void cipher_encrypt_error( int alg_arg,
2425 int key_type_arg,
2426 data_t *key_data,
2427 data_t *input,
2428 int expected_status_arg )
2429{
2430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2431 psa_status_t status;
2432 psa_key_type_t key_type = key_type_arg;
2433 psa_algorithm_t alg = alg_arg;
2434 psa_status_t expected_status = expected_status_arg;
2435 unsigned char *output = NULL;
2436 size_t output_buffer_size = 0;
2437 size_t output_length = 0;
2438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2439
2440 if ( PSA_ERROR_BAD_STATE != expected_status )
2441 {
2442 PSA_ASSERT( psa_crypto_init( ) );
2443
2444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2445 psa_set_key_algorithm( &attributes, alg );
2446 psa_set_key_type( &attributes, key_type );
2447
2448 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2449 input->len );
2450 ASSERT_ALLOC( output, output_buffer_size );
2451
2452 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2453 &key ) );
2454 }
2455
2456 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2457 output_buffer_size, &output_length );
2458
2459 TEST_EQUAL( status, expected_status );
2460
2461exit:
2462 mbedtls_free( output );
2463 psa_destroy_key( key );
2464 PSA_DONE( );
2465}
2466/* END_CASE */
2467
2468/* BEGIN_CASE */
2469void cipher_encrypt_noiv( int alg_arg,
2470 int key_type_arg,
2471 data_t *key_data,
2472 data_t *input,
2473 data_t *expected_output )
2474{
2475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2476 psa_key_type_t key_type = key_type_arg;
2477 psa_algorithm_t alg = alg_arg;
2478 unsigned char *output = NULL;
2479 size_t output_buffer_size = 0;
2480 size_t output_length = 0;
2481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2482
2483 PSA_ASSERT( psa_crypto_init( ) );
2484
2485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2486 psa_set_key_algorithm( &attributes, alg );
2487 psa_set_key_type( &attributes, key_type );
2488
2489 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2490 ASSERT_ALLOC( output, output_buffer_size );
2491
2492 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2493 &key ) );
2494
2495 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2496 output_buffer_size, &output_length ) );
2497 TEST_ASSERT( output_length <=
2498 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2499 TEST_ASSERT( output_length <=
2500 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2501
2502 ASSERT_COMPARE( expected_output->x, expected_output->len,
2503 output, output_length );
2504exit:
2505 mbedtls_free( output );
2506 psa_destroy_key( key );
2507 PSA_DONE( );
2508}
2509/* END_CASE */
2510
2511/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002512void cipher_encrypt_validation( int alg_arg,
2513 int key_type_arg,
2514 data_t *key_data,
2515 data_t *input )
2516{
2517 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2518 psa_key_type_t key_type = key_type_arg;
2519 psa_algorithm_t alg = alg_arg;
2520 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2521 unsigned char *output1 = NULL;
2522 size_t output1_buffer_size = 0;
2523 size_t output1_length = 0;
2524 unsigned char *output2 = NULL;
2525 size_t output2_buffer_size = 0;
2526 size_t output2_length = 0;
2527 size_t function_output_length = 0;
2528 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2530
2531 PSA_ASSERT( psa_crypto_init( ) );
2532
2533 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2534 psa_set_key_algorithm( &attributes, alg );
2535 psa_set_key_type( &attributes, key_type );
2536
2537 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2538 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2539 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2540 ASSERT_ALLOC( output1, output1_buffer_size );
2541 ASSERT_ALLOC( output2, output2_buffer_size );
2542
2543 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2544 &key ) );
2545
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002546 /* The one-shot cipher encryption uses generated iv so validating
2547 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002548 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2549 output1_buffer_size, &output1_length ) );
2550 TEST_ASSERT( output1_length <=
2551 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2552 TEST_ASSERT( output1_length <=
2553 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2554
2555 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2556 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
2557
2558 PSA_ASSERT( psa_cipher_update( &operation,
2559 input->x, input->len,
2560 output2, output2_buffer_size,
2561 &function_output_length ) );
2562 TEST_ASSERT( function_output_length <=
2563 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2564 TEST_ASSERT( function_output_length <=
2565 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2566 output2_length += function_output_length;
2567
2568 PSA_ASSERT( psa_cipher_finish( &operation,
2569 output2 + output2_length,
2570 output2_buffer_size - output2_length,
2571 &function_output_length ) );
2572 TEST_ASSERT( function_output_length <=
2573 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2574 TEST_ASSERT( function_output_length <=
2575 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
2576 output2_length += function_output_length;
2577
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2580 output2, output2_length );
2581
2582exit:
2583 psa_cipher_abort( &operation );
2584 mbedtls_free( output1 );
2585 mbedtls_free( output2 );
2586 psa_destroy_key( key );
2587 PSA_DONE( );
2588}
2589/* END_CASE */
2590
2591/* BEGIN_CASE */
2592void cipher_encrypt_simple_multipart( int alg_arg,
2593 int key_type_arg,
2594 data_t *key_data,
2595 data_t *iv,
2596 data_t *input,
2597 data_t *expected_output,
2598 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002599{
Ronald Cron5425a212020-08-04 14:58:35 +02002600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002601 psa_status_t status;
2602 psa_key_type_t key_type = key_type_arg;
2603 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002604 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002605 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002606 size_t output_buffer_size = 0;
2607 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002608 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002609 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002611
Gilles Peskine8817f612018-12-18 00:18:46 +01002612 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002613
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002614 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2615 psa_set_key_algorithm( &attributes, alg );
2616 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002617
Ronald Cron5425a212020-08-04 14:58:35 +02002618 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2619 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002620
Ronald Cron5425a212020-08-04 14:58:35 +02002621 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002622
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002623 if( iv->len > 0 )
2624 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002625 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002626 }
2627
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002628 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2629 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002630 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002631
Gilles Peskine8817f612018-12-18 00:18:46 +01002632 PSA_ASSERT( psa_cipher_update( &operation,
2633 input->x, input->len,
2634 output, output_buffer_size,
2635 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002636 TEST_ASSERT( function_output_length <=
2637 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2638 TEST_ASSERT( function_output_length <=
2639 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002640 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002641
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002643 ( output_buffer_size == 0 ? NULL :
2644 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002645 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002646 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002647 TEST_ASSERT( function_output_length <=
2648 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2649 TEST_ASSERT( function_output_length <=
2650 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002651 total_output_length += function_output_length;
2652
Gilles Peskinefe11b722018-12-18 00:24:04 +01002653 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654 if( expected_status == PSA_SUCCESS )
2655 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002656 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002657 ASSERT_COMPARE( expected_output->x, expected_output->len,
2658 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002660
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002662 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002663 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002664 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002665 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666}
2667/* END_CASE */
2668
2669/* BEGIN_CASE */
2670void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002671 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002672 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002673 int first_part_size_arg,
2674 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002675 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002676{
Ronald Cron5425a212020-08-04 14:58:35 +02002677 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678 psa_key_type_t key_type = key_type_arg;
2679 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002680 size_t first_part_size = first_part_size_arg;
2681 size_t output1_length = output1_length_arg;
2682 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002683 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684 size_t output_buffer_size = 0;
2685 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002686 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002687 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002689
Gilles Peskine8817f612018-12-18 00:18:46 +01002690 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002691
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002692 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2693 psa_set_key_algorithm( &attributes, alg );
2694 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002695
Ronald Cron5425a212020-08-04 14:58:35 +02002696 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2697 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698
Ronald Cron5425a212020-08-04 14:58:35 +02002699 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002701 if( iv->len > 0 )
2702 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002703 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002704 }
2705
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002706 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2707 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002708 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002709
Gilles Peskinee0866522019-02-19 19:44:00 +01002710 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002711 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2712 output, output_buffer_size,
2713 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002714 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002715 TEST_ASSERT( function_output_length <=
2716 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2717 TEST_ASSERT( function_output_length <=
2718 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002719 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002720
Gilles Peskine8817f612018-12-18 00:18:46 +01002721 PSA_ASSERT( psa_cipher_update( &operation,
2722 input->x + first_part_size,
2723 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002724 ( output_buffer_size == 0 ? NULL :
2725 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002726 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002727 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002728 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002729 TEST_ASSERT( function_output_length <=
2730 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2731 alg,
2732 input->len - first_part_size ) );
2733 TEST_ASSERT( function_output_length <=
2734 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002735 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002736
Gilles Peskine8817f612018-12-18 00:18:46 +01002737 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002738 ( output_buffer_size == 0 ? NULL :
2739 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002740 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002741 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002742 TEST_ASSERT( function_output_length <=
2743 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2744 TEST_ASSERT( function_output_length <=
2745 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002746 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002747 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002748
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002749 ASSERT_COMPARE( expected_output->x, expected_output->len,
2750 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751
2752exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002753 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002754 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002755 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002756 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757}
2758/* END_CASE */
2759
2760/* BEGIN_CASE */
2761void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002762 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002763 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002764 int first_part_size_arg,
2765 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002766 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002767{
Ronald Cron5425a212020-08-04 14:58:35 +02002768 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002769 psa_key_type_t key_type = key_type_arg;
2770 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 size_t first_part_size = first_part_size_arg;
2772 size_t output1_length = output1_length_arg;
2773 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002774 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002775 size_t output_buffer_size = 0;
2776 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002777 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002778 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002779 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002780
Gilles Peskine8817f612018-12-18 00:18:46 +01002781 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002782
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002783 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2784 psa_set_key_algorithm( &attributes, alg );
2785 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002786
Ronald Cron5425a212020-08-04 14:58:35 +02002787 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2788 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002789
Ronald Cron5425a212020-08-04 14:58:35 +02002790 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791
Steven Cooreman177deba2020-09-07 17:14:14 +02002792 if( iv->len > 0 )
2793 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002794 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002795 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002797 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2798 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002799 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002800
Gilles Peskinee0866522019-02-19 19:44:00 +01002801 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_cipher_update( &operation,
2803 input->x, first_part_size,
2804 output, output_buffer_size,
2805 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002806 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002807 TEST_ASSERT( function_output_length <=
2808 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2809 TEST_ASSERT( function_output_length <=
2810 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002811 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002812
Gilles Peskine8817f612018-12-18 00:18:46 +01002813 PSA_ASSERT( psa_cipher_update( &operation,
2814 input->x + first_part_size,
2815 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002816 ( output_buffer_size == 0 ? NULL :
2817 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002818 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002819 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002820 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002821 TEST_ASSERT( function_output_length <=
2822 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2823 alg,
2824 input->len - first_part_size ) );
2825 TEST_ASSERT( function_output_length <=
2826 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002827 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002828
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002830 ( output_buffer_size == 0 ? NULL :
2831 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002832 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002833 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002834 TEST_ASSERT( function_output_length <=
2835 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2836 TEST_ASSERT( function_output_length <=
2837 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002838 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002839 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002840
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002841 ASSERT_COMPARE( expected_output->x, expected_output->len,
2842 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002843
2844exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002845 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002846 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002847 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002848 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849}
2850/* END_CASE */
2851
Gilles Peskine50e586b2018-06-08 14:28:46 +02002852/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002853void cipher_decrypt_simple_multipart( int alg_arg,
2854 int key_type_arg,
2855 data_t *key_data,
2856 data_t *iv,
2857 data_t *input,
2858 data_t *expected_output,
2859 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002860{
Ronald Cron5425a212020-08-04 14:58:35 +02002861 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002862 psa_status_t status;
2863 psa_key_type_t key_type = key_type_arg;
2864 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002865 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002866 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002867 size_t output_buffer_size = 0;
2868 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002869 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002870 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002874
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002875 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2876 psa_set_key_algorithm( &attributes, alg );
2877 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002878
Ronald Cron5425a212020-08-04 14:58:35 +02002879 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2880 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881
Ronald Cron5425a212020-08-04 14:58:35 +02002882 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883
Steven Cooreman177deba2020-09-07 17:14:14 +02002884 if( iv->len > 0 )
2885 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002886 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002887 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002888
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002889 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2890 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002891 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
Gilles Peskine8817f612018-12-18 00:18:46 +01002893 PSA_ASSERT( psa_cipher_update( &operation,
2894 input->x, input->len,
2895 output, output_buffer_size,
2896 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002897 TEST_ASSERT( function_output_length <=
2898 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2899 TEST_ASSERT( function_output_length <=
2900 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002901 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002902
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002904 ( output_buffer_size == 0 ? NULL :
2905 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002906 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002907 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002908 TEST_ASSERT( function_output_length <=
2909 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2910 TEST_ASSERT( function_output_length <=
2911 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002912 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002913 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002914
2915 if( expected_status == PSA_SUCCESS )
2916 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002917 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002918 ASSERT_COMPARE( expected_output->x, expected_output->len,
2919 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920 }
2921
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002923 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002924 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002925 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002926 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927}
2928/* END_CASE */
2929
Gilles Peskine50e586b2018-06-08 14:28:46 +02002930/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002931void cipher_decrypt_error( int alg_arg,
2932 int key_type_arg,
2933 data_t *key_data,
2934 data_t *iv,
2935 data_t *input_arg,
2936 int expected_status_arg )
2937{
2938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2939 psa_status_t status;
2940 psa_key_type_t key_type = key_type_arg;
2941 psa_algorithm_t alg = alg_arg;
2942 psa_status_t expected_status = expected_status_arg;
2943 unsigned char *input = NULL;
2944 size_t input_buffer_size = 0;
2945 unsigned char *output = NULL;
2946 size_t output_buffer_size = 0;
2947 size_t output_length = 0;
2948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2949
2950 if ( PSA_ERROR_BAD_STATE != expected_status )
2951 {
2952 PSA_ASSERT( psa_crypto_init( ) );
2953
2954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2955 psa_set_key_algorithm( &attributes, alg );
2956 psa_set_key_type( &attributes, key_type );
2957
2958 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2959 &key ) );
2960 }
2961
2962 /* Allocate input buffer and copy the iv and the plaintext */
2963 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2964 if ( input_buffer_size > 0 )
2965 {
2966 ASSERT_ALLOC( input, input_buffer_size );
2967 memcpy( input, iv->x, iv->len );
2968 memcpy( input + iv->len, input_arg->x, input_arg->len );
2969 }
2970
2971 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2972 ASSERT_ALLOC( output, output_buffer_size );
2973
2974 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2975 output_buffer_size, &output_length );
2976 TEST_EQUAL( status, expected_status );
2977
2978exit:
2979 mbedtls_free( input );
2980 mbedtls_free( output );
2981 psa_destroy_key( key );
2982 PSA_DONE( );
2983}
2984/* END_CASE */
2985
2986/* BEGIN_CASE */
2987void cipher_decrypt( int alg_arg,
2988 int key_type_arg,
2989 data_t *key_data,
2990 data_t *iv,
2991 data_t *input_arg,
2992 data_t *expected_output )
2993{
2994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2995 psa_key_type_t key_type = key_type_arg;
2996 psa_algorithm_t alg = alg_arg;
2997 unsigned char *input = NULL;
2998 size_t input_buffer_size = 0;
2999 unsigned char *output = NULL;
3000 size_t output_buffer_size = 0;
3001 size_t output_length = 0;
3002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3003
3004 PSA_ASSERT( psa_crypto_init( ) );
3005
3006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3007 psa_set_key_algorithm( &attributes, alg );
3008 psa_set_key_type( &attributes, key_type );
3009
3010 /* Allocate input buffer and copy the iv and the plaintext */
3011 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3012 if ( input_buffer_size > 0 )
3013 {
3014 ASSERT_ALLOC( input, input_buffer_size );
3015 memcpy( input, iv->x, iv->len );
3016 memcpy( input + iv->len, input_arg->x, input_arg->len );
3017 }
3018
3019 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3020 ASSERT_ALLOC( output, output_buffer_size );
3021
3022 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3023 &key ) );
3024
3025 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3026 output_buffer_size, &output_length ) );
3027 TEST_ASSERT( output_length <=
3028 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3029 TEST_ASSERT( output_length <=
3030 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3031
3032 ASSERT_COMPARE( expected_output->x, expected_output->len,
3033 output, output_length );
3034exit:
3035 mbedtls_free( input );
3036 mbedtls_free( output );
3037 psa_destroy_key( key );
3038 PSA_DONE( );
3039}
3040/* END_CASE */
3041
3042/* BEGIN_CASE */
3043void cipher_verify_output( int alg_arg,
3044 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003045 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003046 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003047{
Ronald Cron5425a212020-08-04 14:58:35 +02003048 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003049 psa_key_type_t key_type = key_type_arg;
3050 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003051 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003052 size_t output1_size = 0;
3053 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003054 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003055 size_t output2_size = 0;
3056 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003057 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003058
Gilles Peskine8817f612018-12-18 00:18:46 +01003059 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003060
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003061 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3062 psa_set_key_algorithm( &attributes, alg );
3063 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003064
Ronald Cron5425a212020-08-04 14:58:35 +02003065 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3066 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003067 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003068 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003069
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003070 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3071 output1, output1_size,
3072 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003073 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003074 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003075 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003076 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003077
3078 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003079 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003080
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003081 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3082 output2, output2_size,
3083 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003084 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003085 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003086 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003087 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003088
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003089 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003090
3091exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003092 mbedtls_free( output1 );
3093 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003094 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003095 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003096}
3097/* END_CASE */
3098
3099/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003100void cipher_verify_output_multipart( int alg_arg,
3101 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003102 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003103 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003104 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003105{
Ronald Cron5425a212020-08-04 14:58:35 +02003106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003107 psa_key_type_t key_type = key_type_arg;
3108 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003109 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003110 unsigned char iv[16] = {0};
3111 size_t iv_size = 16;
3112 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003113 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003114 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003115 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003116 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003117 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003118 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003119 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003120 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3121 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003123
Gilles Peskine8817f612018-12-18 00:18:46 +01003124 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003126 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3127 psa_set_key_algorithm( &attributes, alg );
3128 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003129
Ronald Cron5425a212020-08-04 14:58:35 +02003130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3131 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003132
Ronald Cron5425a212020-08-04 14:58:35 +02003133 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3134 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003135
Steven Cooreman177deba2020-09-07 17:14:14 +02003136 if( alg != PSA_ALG_ECB_NO_PADDING )
3137 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003138 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3139 iv, iv_size,
3140 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003141 }
3142
gabor-mezei-armceface22021-01-21 12:26:17 +01003143 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3144 TEST_ASSERT( output1_buffer_size <=
3145 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003146 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003147
Gilles Peskinee0866522019-02-19 19:44:00 +01003148 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003149
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3151 output1, output1_buffer_size,
3152 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003153 TEST_ASSERT( function_output_length <=
3154 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3155 TEST_ASSERT( function_output_length <=
3156 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003157 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003158
Gilles Peskine8817f612018-12-18 00:18:46 +01003159 PSA_ASSERT( psa_cipher_update( &operation1,
3160 input->x + first_part_size,
3161 input->len - first_part_size,
3162 output1, output1_buffer_size,
3163 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003164 TEST_ASSERT( function_output_length <=
3165 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3166 alg,
3167 input->len - first_part_size ) );
3168 TEST_ASSERT( function_output_length <=
3169 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003170 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003171
Gilles Peskine8817f612018-12-18 00:18:46 +01003172 PSA_ASSERT( psa_cipher_finish( &operation1,
3173 output1 + output1_length,
3174 output1_buffer_size - output1_length,
3175 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003176 TEST_ASSERT( function_output_length <=
3177 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3178 TEST_ASSERT( function_output_length <=
3179 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003180 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003183
Gilles Peskine048b7f02018-06-08 14:20:49 +02003184 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003185 TEST_ASSERT( output2_buffer_size <=
3186 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3187 TEST_ASSERT( output2_buffer_size <=
3188 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003189 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003190
Steven Cooreman177deba2020-09-07 17:14:14 +02003191 if( iv_length > 0 )
3192 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003193 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3194 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003195 }
Moran Pekerded84402018-06-06 16:36:50 +03003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3198 output2, output2_buffer_size,
3199 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003200 TEST_ASSERT( function_output_length <=
3201 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3202 TEST_ASSERT( function_output_length <=
3203 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003204 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003205
Gilles Peskine8817f612018-12-18 00:18:46 +01003206 PSA_ASSERT( psa_cipher_update( &operation2,
3207 output1 + first_part_size,
3208 output1_length - first_part_size,
3209 output2, output2_buffer_size,
3210 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3213 alg,
3214 output1_length - first_part_size ) );
3215 TEST_ASSERT( function_output_length <=
3216 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003217 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003218
Gilles Peskine8817f612018-12-18 00:18:46 +01003219 PSA_ASSERT( psa_cipher_finish( &operation2,
3220 output2 + output2_length,
3221 output2_buffer_size - output2_length,
3222 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003223 TEST_ASSERT( function_output_length <=
3224 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3225 TEST_ASSERT( function_output_length <=
3226 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003227 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003228
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003230
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003231 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003232
3233exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003234 psa_cipher_abort( &operation1 );
3235 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003236 mbedtls_free( output1 );
3237 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003239 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003240}
3241/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003242
Gilles Peskine20035e32018-02-03 22:44:14 +01003243/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003244void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003245 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003246 data_t *nonce,
3247 data_t *additional_data,
3248 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003249 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003250{
Ronald Cron5425a212020-08-04 14:58:35 +02003251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252 psa_key_type_t key_type = key_type_arg;
3253 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003254 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255 unsigned char *output_data = NULL;
3256 size_t output_size = 0;
3257 size_t output_length = 0;
3258 unsigned char *output_data2 = NULL;
3259 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003260 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003261 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003262 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003265
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3267 psa_set_key_algorithm( &attributes, alg );
3268 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003269
Gilles Peskine049c7532019-05-15 20:22:09 +02003270 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003271 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003272 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3273 key_bits = psa_get_key_bits( &attributes );
3274
3275 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3276 alg );
3277 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3278 * should be exact. */
3279 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3280 expected_result != PSA_ERROR_NOT_SUPPORTED )
3281 {
3282 TEST_EQUAL( output_size,
3283 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3284 TEST_ASSERT( output_size <=
3285 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3286 }
3287 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003288
Steven Cooremanf49478b2021-02-15 15:19:25 +01003289 status = psa_aead_encrypt( key, alg,
3290 nonce->x, nonce->len,
3291 additional_data->x,
3292 additional_data->len,
3293 input_data->x, input_data->len,
3294 output_data, output_size,
3295 &output_length );
3296
3297 /* If the operation is not supported, just skip and not fail in case the
3298 * encryption involves a common limitation of cryptography hardwares and
3299 * an alternative implementation. */
3300 if( status == PSA_ERROR_NOT_SUPPORTED )
3301 {
3302 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3303 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3304 }
3305
3306 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
3308 if( PSA_SUCCESS == expected_result )
3309 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003310 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
Gilles Peskine003a4a92019-05-14 16:09:40 +02003312 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3313 * should be exact. */
3314 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003315 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003316
gabor-mezei-armceface22021-01-21 12:26:17 +01003317 TEST_ASSERT( input_data->len <=
3318 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3319
Ronald Cron5425a212020-08-04 14:58:35 +02003320 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003321 nonce->x, nonce->len,
3322 additional_data->x,
3323 additional_data->len,
3324 output_data, output_length,
3325 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003326 &output_length2 ),
3327 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003328
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003329 ASSERT_COMPARE( input_data->x, input_data->len,
3330 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003331 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003332
Gilles Peskinea1cac842018-06-11 19:33:02 +02003333exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003334 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003335 mbedtls_free( output_data );
3336 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003337 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003338}
3339/* END_CASE */
3340
3341/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003342void aead_encrypt( int key_type_arg, data_t *key_data,
3343 int alg_arg,
3344 data_t *nonce,
3345 data_t *additional_data,
3346 data_t *input_data,
3347 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003348{
Ronald Cron5425a212020-08-04 14:58:35 +02003349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350 psa_key_type_t key_type = key_type_arg;
3351 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003352 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003353 unsigned char *output_data = NULL;
3354 size_t output_size = 0;
3355 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003357 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3362 psa_set_key_algorithm( &attributes, alg );
3363 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskine049c7532019-05-15 20:22:09 +02003365 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003366 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003367 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3368 key_bits = psa_get_key_bits( &attributes );
3369
3370 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3371 alg );
3372 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3373 * should be exact. */
3374 TEST_EQUAL( output_size,
3375 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3376 TEST_ASSERT( output_size <=
3377 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3378 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379
Steven Cooremand588ea12021-01-11 19:36:04 +01003380 status = psa_aead_encrypt( key, alg,
3381 nonce->x, nonce->len,
3382 additional_data->x, additional_data->len,
3383 input_data->x, input_data->len,
3384 output_data, output_size,
3385 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003386
Ronald Cron28a45ed2021-02-09 20:35:42 +01003387 /* If the operation is not supported, just skip and not fail in case the
3388 * encryption involves a common limitation of cryptography hardwares and
3389 * an alternative implementation. */
3390 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003391 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003392 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3393 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003394 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003395
3396 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003397 ASSERT_COMPARE( expected_result->x, expected_result->len,
3398 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003399
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003401 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003403 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404}
3405/* END_CASE */
3406
3407/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003408void aead_decrypt( int key_type_arg, data_t *key_data,
3409 int alg_arg,
3410 data_t *nonce,
3411 data_t *additional_data,
3412 data_t *input_data,
3413 data_t *expected_data,
3414 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003415{
Ronald Cron5425a212020-08-04 14:58:35 +02003416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417 psa_key_type_t key_type = key_type_arg;
3418 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003419 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420 unsigned char *output_data = NULL;
3421 size_t output_size = 0;
3422 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003424 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003425 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426
Gilles Peskine8817f612018-12-18 00:18:46 +01003427 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003428
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003429 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3430 psa_set_key_algorithm( &attributes, alg );
3431 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003432
Gilles Peskine049c7532019-05-15 20:22:09 +02003433 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003434 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003435 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3436 key_bits = psa_get_key_bits( &attributes );
3437
3438 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3439 alg );
3440 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3441 expected_result != PSA_ERROR_NOT_SUPPORTED )
3442 {
3443 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3444 * should be exact. */
3445 TEST_EQUAL( output_size,
3446 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3447 TEST_ASSERT( output_size <=
3448 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3449 }
3450 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003451
Steven Cooremand588ea12021-01-11 19:36:04 +01003452 status = psa_aead_decrypt( key, alg,
3453 nonce->x, nonce->len,
3454 additional_data->x,
3455 additional_data->len,
3456 input_data->x, input_data->len,
3457 output_data, output_size,
3458 &output_length );
3459
Ronald Cron28a45ed2021-02-09 20:35:42 +01003460 /* If the operation is not supported, just skip and not fail in case the
3461 * decryption involves a common limitation of cryptography hardwares and
3462 * an alternative implementation. */
3463 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003464 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003465 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3466 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003467 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003468
3469 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskine2d277862018-06-18 15:41:12 +02003471 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003472 ASSERT_COMPARE( expected_data->x, expected_data->len,
3473 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003476 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003477 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003478 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479}
3480/* END_CASE */
3481
3482/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003483void signature_size( int type_arg,
3484 int bits,
3485 int alg_arg,
3486 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003487{
3488 psa_key_type_t type = type_arg;
3489 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003490 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003491
Gilles Peskinefe11b722018-12-18 00:24:04 +01003492 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003493#if defined(MBEDTLS_TEST_DEPRECATED)
3494 TEST_EQUAL( actual_size,
3495 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3496#endif /* MBEDTLS_TEST_DEPRECATED */
3497
Gilles Peskinee59236f2018-01-27 23:32:46 +01003498exit:
3499 ;
3500}
3501/* END_CASE */
3502
3503/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003504void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3505 int alg_arg, data_t *input_data,
3506 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003507{
Ronald Cron5425a212020-08-04 14:58:35 +02003508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003509 psa_key_type_t key_type = key_type_arg;
3510 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003511 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003512 unsigned char *signature = NULL;
3513 size_t signature_size;
3514 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003516
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003518
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003519 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003520 psa_set_key_algorithm( &attributes, alg );
3521 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003522
Gilles Peskine049c7532019-05-15 20:22:09 +02003523 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003524 &key ) );
3525 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003526 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003527
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003528 /* Allocate a buffer which has the size advertized by the
3529 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003530 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003531 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003532 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003533 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003534 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003535
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003536 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003537 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003538 input_data->x, input_data->len,
3539 signature, signature_size,
3540 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003541 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003542 ASSERT_COMPARE( output_data->x, output_data->len,
3543 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003544
Gilles Peskine0627f982019-11-26 19:12:16 +01003545#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003546 memset( signature, 0, signature_size );
3547 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003548 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003549 input_data->x, input_data->len,
3550 signature, signature_size,
3551 &signature_length ) );
3552 ASSERT_COMPARE( output_data->x, output_data->len,
3553 signature, signature_length );
3554#endif /* MBEDTLS_TEST_DEPRECATED */
3555
Gilles Peskine20035e32018-02-03 22:44:14 +01003556exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003557 /*
3558 * Key attributes may have been returned by psa_get_key_attributes()
3559 * thus reset them as required.
3560 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003561 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003562
Ronald Cron5425a212020-08-04 14:58:35 +02003563 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003564 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003565 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003566}
3567/* END_CASE */
3568
3569/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003570void sign_hash_fail( int key_type_arg, data_t *key_data,
3571 int alg_arg, data_t *input_data,
3572 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003573{
Ronald Cron5425a212020-08-04 14:58:35 +02003574 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003575 psa_key_type_t key_type = key_type_arg;
3576 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003577 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003578 psa_status_t actual_status;
3579 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003580 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003581 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003582 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003583
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003584 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003585
Gilles Peskine8817f612018-12-18 00:18:46 +01003586 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003587
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003588 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003589 psa_set_key_algorithm( &attributes, alg );
3590 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003591
Gilles Peskine049c7532019-05-15 20:22:09 +02003592 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003593 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003594
Ronald Cron5425a212020-08-04 14:58:35 +02003595 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003596 input_data->x, input_data->len,
3597 signature, signature_size,
3598 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003599 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003600 /* The value of *signature_length is unspecified on error, but
3601 * whatever it is, it should be less than signature_size, so that
3602 * if the caller tries to read *signature_length bytes without
3603 * checking the error code then they don't overflow a buffer. */
3604 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003605
Gilles Peskine895242b2019-11-29 12:15:40 +01003606#if defined(MBEDTLS_TEST_DEPRECATED)
3607 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003608 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003609 input_data->x, input_data->len,
3610 signature, signature_size,
3611 &signature_length ),
3612 expected_status );
3613 TEST_ASSERT( signature_length <= signature_size );
3614#endif /* MBEDTLS_TEST_DEPRECATED */
3615
Gilles Peskine20035e32018-02-03 22:44:14 +01003616exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003617 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003618 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003619 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003620 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003621}
3622/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003623
3624/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003625void sign_verify_hash( int key_type_arg, data_t *key_data,
3626 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003627{
Ronald Cron5425a212020-08-04 14:58:35 +02003628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003629 psa_key_type_t key_type = key_type_arg;
3630 psa_algorithm_t alg = alg_arg;
3631 size_t key_bits;
3632 unsigned char *signature = NULL;
3633 size_t signature_size;
3634 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003636
Gilles Peskine8817f612018-12-18 00:18:46 +01003637 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003638
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003639 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003640 psa_set_key_algorithm( &attributes, alg );
3641 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003642
Gilles Peskine049c7532019-05-15 20:22:09 +02003643 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003644 &key ) );
3645 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003646 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003647
3648 /* Allocate a buffer which has the size advertized by the
3649 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003650 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003651 key_bits, alg );
3652 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003653 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003654 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003655
3656 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003657 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003658 input_data->x, input_data->len,
3659 signature, signature_size,
3660 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003661 /* Check that the signature length looks sensible. */
3662 TEST_ASSERT( signature_length <= signature_size );
3663 TEST_ASSERT( signature_length > 0 );
3664
3665 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003666 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003667 input_data->x, input_data->len,
3668 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003669
3670 if( input_data->len != 0 )
3671 {
3672 /* Flip a bit in the input and verify that the signature is now
3673 * detected as invalid. Flip a bit at the beginning, not at the end,
3674 * because ECDSA may ignore the last few bits of the input. */
3675 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003676 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003677 input_data->x, input_data->len,
3678 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003679 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003680 }
3681
3682exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003683 /*
3684 * Key attributes may have been returned by psa_get_key_attributes()
3685 * thus reset them as required.
3686 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003687 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003688
Ronald Cron5425a212020-08-04 14:58:35 +02003689 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003690 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003691 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003692}
3693/* END_CASE */
3694
3695/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003696void verify_hash( int key_type_arg, data_t *key_data,
3697 int alg_arg, data_t *hash_data,
3698 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003699{
Ronald Cron5425a212020-08-04 14:58:35 +02003700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003701 psa_key_type_t key_type = key_type_arg;
3702 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003704
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003705 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003706
Gilles Peskine8817f612018-12-18 00:18:46 +01003707 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003708
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003709 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003710 psa_set_key_algorithm( &attributes, alg );
3711 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003712
Gilles Peskine049c7532019-05-15 20:22:09 +02003713 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003714 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003715
Ronald Cron5425a212020-08-04 14:58:35 +02003716 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003717 hash_data->x, hash_data->len,
3718 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003719
3720#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003721 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003722 hash_data->x, hash_data->len,
3723 signature_data->x,
3724 signature_data->len ) );
3725
3726#endif /* MBEDTLS_TEST_DEPRECATED */
3727
itayzafrir5c753392018-05-08 11:18:38 +03003728exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003729 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003730 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003731 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003732}
3733/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003734
3735/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003736void verify_hash_fail( int key_type_arg, data_t *key_data,
3737 int alg_arg, data_t *hash_data,
3738 data_t *signature_data,
3739 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003740{
Ronald Cron5425a212020-08-04 14:58:35 +02003741 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003742 psa_key_type_t key_type = key_type_arg;
3743 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003744 psa_status_t actual_status;
3745 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003747
Gilles Peskine8817f612018-12-18 00:18:46 +01003748 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003749
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003750 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003751 psa_set_key_algorithm( &attributes, alg );
3752 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003753
Gilles Peskine049c7532019-05-15 20:22:09 +02003754 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003755 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003756
Ronald Cron5425a212020-08-04 14:58:35 +02003757 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003758 hash_data->x, hash_data->len,
3759 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003760 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003761
Gilles Peskine895242b2019-11-29 12:15:40 +01003762#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003763 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003764 hash_data->x, hash_data->len,
3765 signature_data->x, signature_data->len ),
3766 expected_status );
3767#endif /* MBEDTLS_TEST_DEPRECATED */
3768
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003769exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003770 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003771 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003772 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003773}
3774/* END_CASE */
3775
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003776/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003777void sign_message_deterministic( int key_type_arg,
3778 data_t *key_data,
3779 int alg_arg,
3780 data_t *input_data,
3781 data_t *output_data )
3782{
3783 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3784 psa_key_type_t key_type = key_type_arg;
3785 psa_algorithm_t alg = alg_arg;
3786 size_t key_bits;
3787 unsigned char *signature = NULL;
3788 size_t signature_size;
3789 size_t signature_length = 0xdeadbeef;
3790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3791
3792 PSA_ASSERT( psa_crypto_init( ) );
3793
3794 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3795 psa_set_key_algorithm( &attributes, alg );
3796 psa_set_key_type( &attributes, key_type );
3797
3798 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3799 &key ) );
3800 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3801 key_bits = psa_get_key_bits( &attributes );
3802
3803 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3804 TEST_ASSERT( signature_size != 0 );
3805 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3806 ASSERT_ALLOC( signature, signature_size );
3807
3808 PSA_ASSERT( psa_sign_message( key, alg,
3809 input_data->x, input_data->len,
3810 signature, signature_size,
3811 &signature_length ) );
3812
3813 ASSERT_COMPARE( output_data->x, output_data->len,
3814 signature, signature_length );
3815
3816exit:
3817 psa_reset_key_attributes( &attributes );
3818
3819 psa_destroy_key( key );
3820 mbedtls_free( signature );
3821 PSA_DONE( );
3822
3823}
3824/* END_CASE */
3825
3826/* BEGIN_CASE */
3827void sign_message_fail( int key_type_arg,
3828 data_t *key_data,
3829 int alg_arg,
3830 data_t *input_data,
3831 int signature_size_arg,
3832 int expected_status_arg )
3833{
3834 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3835 psa_key_type_t key_type = key_type_arg;
3836 psa_algorithm_t alg = alg_arg;
3837 size_t signature_size = signature_size_arg;
3838 psa_status_t actual_status;
3839 psa_status_t expected_status = expected_status_arg;
3840 unsigned char *signature = NULL;
3841 size_t signature_length = 0xdeadbeef;
3842 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3843
3844 ASSERT_ALLOC( signature, signature_size );
3845
3846 PSA_ASSERT( psa_crypto_init( ) );
3847
3848 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3849 psa_set_key_algorithm( &attributes, alg );
3850 psa_set_key_type( &attributes, key_type );
3851
3852 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3853 &key ) );
3854
3855 actual_status = psa_sign_message( key, alg,
3856 input_data->x, input_data->len,
3857 signature, signature_size,
3858 &signature_length );
3859 TEST_EQUAL( actual_status, expected_status );
3860 /* The value of *signature_length is unspecified on error, but
3861 * whatever it is, it should be less than signature_size, so that
3862 * if the caller tries to read *signature_length bytes without
3863 * checking the error code then they don't overflow a buffer. */
3864 TEST_ASSERT( signature_length <= signature_size );
3865
3866exit:
3867 psa_reset_key_attributes( &attributes );
3868 psa_destroy_key( key );
3869 mbedtls_free( signature );
3870 PSA_DONE( );
3871}
3872/* END_CASE */
3873
3874/* BEGIN_CASE */
3875void sign_verify_message( int key_type_arg,
3876 data_t *key_data,
3877 int alg_arg,
3878 data_t *input_data )
3879{
3880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3881 psa_key_type_t key_type = key_type_arg;
3882 psa_algorithm_t alg = alg_arg;
3883 size_t key_bits;
3884 unsigned char *signature = NULL;
3885 size_t signature_size;
3886 size_t signature_length = 0xdeadbeef;
3887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3888
3889 PSA_ASSERT( psa_crypto_init( ) );
3890
3891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3892 PSA_KEY_USAGE_VERIFY_MESSAGE );
3893 psa_set_key_algorithm( &attributes, alg );
3894 psa_set_key_type( &attributes, key_type );
3895
3896 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3897 &key ) );
3898 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3899 key_bits = psa_get_key_bits( &attributes );
3900
3901 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3902 TEST_ASSERT( signature_size != 0 );
3903 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3904 ASSERT_ALLOC( signature, signature_size );
3905
3906 PSA_ASSERT( psa_sign_message( key, alg,
3907 input_data->x, input_data->len,
3908 signature, signature_size,
3909 &signature_length ) );
3910 TEST_ASSERT( signature_length <= signature_size );
3911 TEST_ASSERT( signature_length > 0 );
3912
3913 PSA_ASSERT( psa_verify_message( key, alg,
3914 input_data->x, input_data->len,
3915 signature, signature_length ) );
3916
3917 if( input_data->len != 0 )
3918 {
3919 /* Flip a bit in the input and verify that the signature is now
3920 * detected as invalid. Flip a bit at the beginning, not at the end,
3921 * because ECDSA may ignore the last few bits of the input. */
3922 input_data->x[0] ^= 1;
3923 TEST_EQUAL( psa_verify_message( key, alg,
3924 input_data->x, input_data->len,
3925 signature, signature_length ),
3926 PSA_ERROR_INVALID_SIGNATURE );
3927 }
3928
3929exit:
3930 psa_reset_key_attributes( &attributes );
3931
3932 psa_destroy_key( key );
3933 mbedtls_free( signature );
3934 PSA_DONE( );
3935}
3936/* END_CASE */
3937
3938/* BEGIN_CASE */
3939void verify_message( int key_type_arg,
3940 data_t *key_data,
3941 int alg_arg,
3942 data_t *input_data,
3943 data_t *signature_data )
3944{
3945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3946 psa_key_type_t key_type = key_type_arg;
3947 psa_algorithm_t alg = alg_arg;
3948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3949
3950 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3951
3952 PSA_ASSERT( psa_crypto_init( ) );
3953
3954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3955 psa_set_key_algorithm( &attributes, alg );
3956 psa_set_key_type( &attributes, key_type );
3957
3958 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3959 &key ) );
3960
3961 PSA_ASSERT( psa_verify_message( key, alg,
3962 input_data->x, input_data->len,
3963 signature_data->x, signature_data->len ) );
3964
3965exit:
3966 psa_reset_key_attributes( &attributes );
3967 psa_destroy_key( key );
3968 PSA_DONE( );
3969}
3970/* END_CASE */
3971
3972/* BEGIN_CASE */
3973void verify_message_fail( int key_type_arg,
3974 data_t *key_data,
3975 int alg_arg,
3976 data_t *hash_data,
3977 data_t *signature_data,
3978 int expected_status_arg )
3979{
3980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3981 psa_key_type_t key_type = key_type_arg;
3982 psa_algorithm_t alg = alg_arg;
3983 psa_status_t actual_status;
3984 psa_status_t expected_status = expected_status_arg;
3985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3986
3987 PSA_ASSERT( psa_crypto_init( ) );
3988
3989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3990 psa_set_key_algorithm( &attributes, alg );
3991 psa_set_key_type( &attributes, key_type );
3992
3993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3994 &key ) );
3995
3996 actual_status = psa_verify_message( key, alg,
3997 hash_data->x, hash_data->len,
3998 signature_data->x,
3999 signature_data->len );
4000 TEST_EQUAL( actual_status, expected_status );
4001
4002exit:
4003 psa_reset_key_attributes( &attributes );
4004 psa_destroy_key( key );
4005 PSA_DONE( );
4006}
4007/* END_CASE */
4008
4009/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004010void asymmetric_encrypt( int key_type_arg,
4011 data_t *key_data,
4012 int alg_arg,
4013 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004014 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004015 int expected_output_length_arg,
4016 int expected_status_arg )
4017{
Ronald Cron5425a212020-08-04 14:58:35 +02004018 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004019 psa_key_type_t key_type = key_type_arg;
4020 psa_algorithm_t alg = alg_arg;
4021 size_t expected_output_length = expected_output_length_arg;
4022 size_t key_bits;
4023 unsigned char *output = NULL;
4024 size_t output_size;
4025 size_t output_length = ~0;
4026 psa_status_t actual_status;
4027 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004029
Gilles Peskine8817f612018-12-18 00:18:46 +01004030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004031
Gilles Peskine656896e2018-06-29 19:12:28 +02004032 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4034 psa_set_key_algorithm( &attributes, alg );
4035 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004036 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004037 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004038
4039 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004040 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004041 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004042
Gilles Peskine656896e2018-06-29 19:12:28 +02004043 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004044 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004045 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004046
4047 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004048 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004049 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004050 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004051 output, output_size,
4052 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004053 TEST_EQUAL( actual_status, expected_status );
4054 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004055
Gilles Peskine68428122018-06-30 18:42:41 +02004056 /* If the label is empty, the test framework puts a non-null pointer
4057 * in label->x. Test that a null pointer works as well. */
4058 if( label->len == 0 )
4059 {
4060 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004061 if( output_size != 0 )
4062 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004063 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004064 input_data->x, input_data->len,
4065 NULL, label->len,
4066 output, output_size,
4067 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004068 TEST_EQUAL( actual_status, expected_status );
4069 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004070 }
4071
Gilles Peskine656896e2018-06-29 19:12:28 +02004072exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004073 /*
4074 * Key attributes may have been returned by psa_get_key_attributes()
4075 * thus reset them as required.
4076 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004077 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004078
Ronald Cron5425a212020-08-04 14:58:35 +02004079 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004080 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004081 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004082}
4083/* END_CASE */
4084
4085/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004086void asymmetric_encrypt_decrypt( int key_type_arg,
4087 data_t *key_data,
4088 int alg_arg,
4089 data_t *input_data,
4090 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091{
Ronald Cron5425a212020-08-04 14:58:35 +02004092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004093 psa_key_type_t key_type = key_type_arg;
4094 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004095 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004096 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004097 size_t output_size;
4098 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004099 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004100 size_t output2_size;
4101 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004102 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103
Gilles Peskine8817f612018-12-18 00:18:46 +01004104 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004105
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004106 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4107 psa_set_key_algorithm( &attributes, alg );
4108 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004109
Gilles Peskine049c7532019-05-15 20:22:09 +02004110 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004111 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004112
4113 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004114 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004115 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004116
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004117 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004118 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004119 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004120
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004121 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004122 TEST_ASSERT( output2_size <=
4123 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4124 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004125 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004126
Gilles Peskineeebd7382018-06-08 18:11:54 +02004127 /* We test encryption by checking that encrypt-then-decrypt gives back
4128 * the original plaintext because of the non-optional random
4129 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004130 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004131 input_data->x, input_data->len,
4132 label->x, label->len,
4133 output, output_size,
4134 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004135 /* We don't know what ciphertext length to expect, but check that
4136 * it looks sensible. */
4137 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004138
Ronald Cron5425a212020-08-04 14:58:35 +02004139 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004140 output, output_length,
4141 label->x, label->len,
4142 output2, output2_size,
4143 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004144 ASSERT_COMPARE( input_data->x, input_data->len,
4145 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004146
4147exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004148 /*
4149 * Key attributes may have been returned by psa_get_key_attributes()
4150 * thus reset them as required.
4151 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004152 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004153
Ronald Cron5425a212020-08-04 14:58:35 +02004154 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004155 mbedtls_free( output );
4156 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004157 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004158}
4159/* END_CASE */
4160
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004162void asymmetric_decrypt( int key_type_arg,
4163 data_t *key_data,
4164 int alg_arg,
4165 data_t *input_data,
4166 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004167 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004168{
Ronald Cron5425a212020-08-04 14:58:35 +02004169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004170 psa_key_type_t key_type = key_type_arg;
4171 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004172 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004173 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004174 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004175 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004177
Gilles Peskine8817f612018-12-18 00:18:46 +01004178 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4181 psa_set_key_algorithm( &attributes, alg );
4182 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004183
Gilles Peskine049c7532019-05-15 20:22:09 +02004184 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004185 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004186
gabor-mezei-armceface22021-01-21 12:26:17 +01004187 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4188 key_bits = psa_get_key_bits( &attributes );
4189
4190 /* Determine the maximum ciphertext length */
4191 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4192 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4193 ASSERT_ALLOC( output, output_size );
4194
Ronald Cron5425a212020-08-04 14:58:35 +02004195 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004196 input_data->x, input_data->len,
4197 label->x, label->len,
4198 output,
4199 output_size,
4200 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004201 ASSERT_COMPARE( expected_data->x, expected_data->len,
4202 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004203
Gilles Peskine68428122018-06-30 18:42:41 +02004204 /* If the label is empty, the test framework puts a non-null pointer
4205 * in label->x. Test that a null pointer works as well. */
4206 if( label->len == 0 )
4207 {
4208 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004209 if( output_size != 0 )
4210 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004211 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004212 input_data->x, input_data->len,
4213 NULL, label->len,
4214 output,
4215 output_size,
4216 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004217 ASSERT_COMPARE( expected_data->x, expected_data->len,
4218 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004219 }
4220
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004221exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004222 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004223 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004224 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004225 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004226}
4227/* END_CASE */
4228
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004230void asymmetric_decrypt_fail( int key_type_arg,
4231 data_t *key_data,
4232 int alg_arg,
4233 data_t *input_data,
4234 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004235 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004236 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004237{
Ronald Cron5425a212020-08-04 14:58:35 +02004238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004239 psa_key_type_t key_type = key_type_arg;
4240 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004242 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004243 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004244 psa_status_t actual_status;
4245 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004247
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004248 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004249
Gilles Peskine8817f612018-12-18 00:18:46 +01004250 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004251
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004252 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4253 psa_set_key_algorithm( &attributes, alg );
4254 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004255
Gilles Peskine049c7532019-05-15 20:22:09 +02004256 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004257 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004258
Ronald Cron5425a212020-08-04 14:58:35 +02004259 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004260 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004261 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004262 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004263 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004264 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004265 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004266
Gilles Peskine68428122018-06-30 18:42:41 +02004267 /* If the label is empty, the test framework puts a non-null pointer
4268 * in label->x. Test that a null pointer works as well. */
4269 if( label->len == 0 )
4270 {
4271 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004272 if( output_size != 0 )
4273 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004274 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004275 input_data->x, input_data->len,
4276 NULL, label->len,
4277 output, output_size,
4278 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004279 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004280 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004281 }
4282
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004283exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004284 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004285 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004286 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004287 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004288}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004289/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004290
4291/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004292void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004293{
4294 /* Test each valid way of initializing the object, except for `= {0}`, as
4295 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4296 * though it's OK by the C standard. We could test for this, but we'd need
4297 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004298 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004299 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4300 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4301 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004302
4303 memset( &zero, 0, sizeof( zero ) );
4304
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004305 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004306 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004307 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004308 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004309 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004310 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004311 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004312
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004313 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004314 PSA_ASSERT( psa_key_derivation_abort(&func) );
4315 PSA_ASSERT( psa_key_derivation_abort(&init) );
4316 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004317}
4318/* END_CASE */
4319
Janos Follath16de4a42019-06-13 16:32:24 +01004320/* BEGIN_CASE */
4321void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004322{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004323 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004324 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004325 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004326
Gilles Peskine8817f612018-12-18 00:18:46 +01004327 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004328
Janos Follath16de4a42019-06-13 16:32:24 +01004329 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004330 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004331
4332exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004333 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004334 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004335}
4336/* END_CASE */
4337
Janos Follathaf3c2a02019-06-12 12:34:34 +01004338/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004339void derive_set_capacity( int alg_arg, int capacity_arg,
4340 int expected_status_arg )
4341{
4342 psa_algorithm_t alg = alg_arg;
4343 size_t capacity = capacity_arg;
4344 psa_status_t expected_status = expected_status_arg;
4345 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4346
4347 PSA_ASSERT( psa_crypto_init( ) );
4348
4349 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4350
4351 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4352 expected_status );
4353
4354exit:
4355 psa_key_derivation_abort( &operation );
4356 PSA_DONE( );
4357}
4358/* END_CASE */
4359
4360/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004361void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004362 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004363 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004364 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004365 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004366 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004367 int expected_status_arg3,
4368 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004369{
4370 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004371 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4372 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004373 psa_status_t expected_statuses[] = {expected_status_arg1,
4374 expected_status_arg2,
4375 expected_status_arg3};
4376 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004377 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4378 MBEDTLS_SVC_KEY_ID_INIT,
4379 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004380 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4381 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4382 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004383 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004384 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004385 psa_status_t expected_output_status = expected_output_status_arg;
4386 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004387
4388 PSA_ASSERT( psa_crypto_init( ) );
4389
4390 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4391 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004392
4393 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4394
4395 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4396 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004397 mbedtls_test_set_step( i );
4398 if( steps[i] == 0 )
4399 {
4400 /* Skip this step */
4401 }
4402 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004403 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004404 psa_set_key_type( &attributes, key_types[i] );
4405 PSA_ASSERT( psa_import_key( &attributes,
4406 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004407 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004408 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4409 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4410 {
4411 // When taking a private key as secret input, use key agreement
4412 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004413 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4414 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004415 expected_statuses[i] );
4416 }
4417 else
4418 {
4419 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004420 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004421 expected_statuses[i] );
4422 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004423 }
4424 else
4425 {
4426 TEST_EQUAL( psa_key_derivation_input_bytes(
4427 &operation, steps[i],
4428 inputs[i]->x, inputs[i]->len ),
4429 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004430 }
4431 }
4432
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004433 if( output_key_type != PSA_KEY_TYPE_NONE )
4434 {
4435 psa_reset_key_attributes( &attributes );
4436 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4437 psa_set_key_bits( &attributes, 8 );
4438 actual_output_status =
4439 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004440 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004441 }
4442 else
4443 {
4444 uint8_t buffer[1];
4445 actual_output_status =
4446 psa_key_derivation_output_bytes( &operation,
4447 buffer, sizeof( buffer ) );
4448 }
4449 TEST_EQUAL( actual_output_status, expected_output_status );
4450
Janos Follathaf3c2a02019-06-12 12:34:34 +01004451exit:
4452 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004453 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4454 psa_destroy_key( keys[i] );
4455 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004456 PSA_DONE( );
4457}
4458/* END_CASE */
4459
Janos Follathd958bb72019-07-03 15:02:16 +01004460/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004461void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004462{
Janos Follathd958bb72019-07-03 15:02:16 +01004463 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004465 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004466 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004467 unsigned char input1[] = "Input 1";
4468 size_t input1_length = sizeof( input1 );
4469 unsigned char input2[] = "Input 2";
4470 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004471 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004472 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004473 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4474 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4475 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004477
Gilles Peskine8817f612018-12-18 00:18:46 +01004478 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004479
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4481 psa_set_key_algorithm( &attributes, alg );
4482 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004483
Gilles Peskine73676cb2019-05-15 20:15:10 +02004484 PSA_ASSERT( psa_import_key( &attributes,
4485 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004486 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004487
4488 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004489 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4490 input1, input1_length,
4491 input2, input2_length,
4492 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004493 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004496 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004497 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004498
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004499 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004502 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004503
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004504exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004506 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004507 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004508}
4509/* END_CASE */
4510
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004511/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004512void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004513{
4514 uint8_t output_buffer[16];
4515 size_t buffer_size = 16;
4516 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004517 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004518
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004519 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4520 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004521 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004522
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004523 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004524 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004525
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004526 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004527
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004528 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4529 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004530 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004533 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004534
4535exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004536 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004537}
4538/* END_CASE */
4539
4540/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004541void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004542 int step1_arg, data_t *input1,
4543 int step2_arg, data_t *input2,
4544 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004545 int requested_capacity_arg,
4546 data_t *expected_output1,
4547 data_t *expected_output2 )
4548{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004549 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004550 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4551 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004552 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4553 MBEDTLS_SVC_KEY_ID_INIT,
4554 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004555 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004556 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004557 uint8_t *expected_outputs[2] =
4558 {expected_output1->x, expected_output2->x};
4559 size_t output_sizes[2] =
4560 {expected_output1->len, expected_output2->len};
4561 size_t output_buffer_size = 0;
4562 uint8_t *output_buffer = NULL;
4563 size_t expected_capacity;
4564 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004566 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004567 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004568
4569 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4570 {
4571 if( output_sizes[i] > output_buffer_size )
4572 output_buffer_size = output_sizes[i];
4573 if( output_sizes[i] == 0 )
4574 expected_outputs[i] = NULL;
4575 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004576 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004578
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004579 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4580 psa_set_key_algorithm( &attributes, alg );
4581 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004582
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004583 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004584 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4585 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4586 requested_capacity ) );
4587 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004588 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004589 switch( steps[i] )
4590 {
4591 case 0:
4592 break;
4593 case PSA_KEY_DERIVATION_INPUT_SECRET:
4594 PSA_ASSERT( psa_import_key( &attributes,
4595 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004596 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004597
4598 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4599 {
4600 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4601 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4602 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4603 }
4604
Gilles Peskine1468da72019-05-29 17:35:49 +02004605 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004606 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004607 break;
4608 default:
4609 PSA_ASSERT( psa_key_derivation_input_bytes(
4610 &operation, steps[i],
4611 inputs[i]->x, inputs[i]->len ) );
4612 break;
4613 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004614 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004615
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004616 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004617 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004618 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004619 expected_capacity = requested_capacity;
4620
4621 /* Expansion phase. */
4622 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4623 {
4624 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004625 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004626 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004627 if( expected_capacity == 0 && output_sizes[i] == 0 )
4628 {
4629 /* Reading 0 bytes when 0 bytes are available can go either way. */
4630 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004631 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004632 continue;
4633 }
4634 else if( expected_capacity == 0 ||
4635 output_sizes[i] > expected_capacity )
4636 {
4637 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004638 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004639 expected_capacity = 0;
4640 continue;
4641 }
4642 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004643 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004644 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004645 ASSERT_COMPARE( output_buffer, output_sizes[i],
4646 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004648 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004650 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004651 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004652 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004653 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004654
4655exit:
4656 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004657 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004658 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4659 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004660 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004661}
4662/* END_CASE */
4663
4664/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004665void derive_full( int alg_arg,
4666 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004667 data_t *input1,
4668 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004669 int requested_capacity_arg )
4670{
Ronald Cron5425a212020-08-04 14:58:35 +02004671 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004672 psa_algorithm_t alg = alg_arg;
4673 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004675 unsigned char output_buffer[16];
4676 size_t expected_capacity = requested_capacity;
4677 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004679
Gilles Peskine8817f612018-12-18 00:18:46 +01004680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004681
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004682 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4683 psa_set_key_algorithm( &attributes, alg );
4684 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004685
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 ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004688
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004689 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4690 input1->x, input1->len,
4691 input2->x, input2->len,
4692 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004693 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004694
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004695 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004696 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004697 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004698
4699 /* Expansion phase. */
4700 while( current_capacity > 0 )
4701 {
4702 size_t read_size = sizeof( output_buffer );
4703 if( read_size > current_capacity )
4704 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004705 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004706 output_buffer,
4707 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004708 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004710 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004711 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004712 }
4713
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 /* Check that the operation refuses to go over capacity. */
4715 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004716 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004717
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004718 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004719
4720exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004722 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004723 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004724}
4725/* END_CASE */
4726
Janos Follathe60c9052019-07-03 13:51:30 +01004727/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004728void derive_key_exercise( int alg_arg,
4729 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004730 data_t *input1,
4731 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004732 int derived_type_arg,
4733 int derived_bits_arg,
4734 int derived_usage_arg,
4735 int derived_alg_arg )
4736{
Ronald Cron5425a212020-08-04 14:58:35 +02004737 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4738 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004739 psa_algorithm_t alg = alg_arg;
4740 psa_key_type_t derived_type = derived_type_arg;
4741 size_t derived_bits = derived_bits_arg;
4742 psa_key_usage_t derived_usage = derived_usage_arg;
4743 psa_algorithm_t derived_alg = derived_alg_arg;
4744 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004747 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004748
Gilles Peskine8817f612018-12-18 00:18:46 +01004749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004750
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4752 psa_set_key_algorithm( &attributes, alg );
4753 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004754 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004755 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004756
4757 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004758 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4759 input1->x, input1->len,
4760 input2->x, input2->len,
4761 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004762 goto exit;
4763
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004764 psa_set_key_usage_flags( &attributes, derived_usage );
4765 psa_set_key_algorithm( &attributes, derived_alg );
4766 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004767 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004769 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004770
4771 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004772 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004773 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4774 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004775
4776 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004777 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004778 goto exit;
4779
4780exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004781 /*
4782 * Key attributes may have been returned by psa_get_key_attributes()
4783 * thus reset them as required.
4784 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004785 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004786
4787 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004788 psa_destroy_key( base_key );
4789 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004790 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004791}
4792/* END_CASE */
4793
Janos Follath42fd8882019-07-03 14:17:09 +01004794/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004795void derive_key_export( int alg_arg,
4796 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004797 data_t *input1,
4798 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004799 int bytes1_arg,
4800 int bytes2_arg )
4801{
Ronald Cron5425a212020-08-04 14:58:35 +02004802 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4803 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004804 psa_algorithm_t alg = alg_arg;
4805 size_t bytes1 = bytes1_arg;
4806 size_t bytes2 = bytes2_arg;
4807 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004809 uint8_t *output_buffer = NULL;
4810 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004811 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4812 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004813 size_t length;
4814
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004815 ASSERT_ALLOC( output_buffer, capacity );
4816 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004817 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004818
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004819 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4820 psa_set_key_algorithm( &base_attributes, alg );
4821 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004822 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004823 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004824
4825 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004826 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4827 input1->x, input1->len,
4828 input2->x, input2->len,
4829 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004830 goto exit;
4831
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004833 output_buffer,
4834 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004836
4837 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004838 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4839 input1->x, input1->len,
4840 input2->x, input2->len,
4841 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004842 goto exit;
4843
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004844 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4845 psa_set_key_algorithm( &derived_attributes, 0 );
4846 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004847 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004849 &derived_key ) );
4850 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004851 export_buffer, bytes1,
4852 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004853 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004854 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004855 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004857 &derived_key ) );
4858 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004859 export_buffer + bytes1, bytes2,
4860 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004861 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004862
4863 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004864 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4865 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004866
4867exit:
4868 mbedtls_free( output_buffer );
4869 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004871 psa_destroy_key( base_key );
4872 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004873 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004874}
4875/* END_CASE */
4876
4877/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004878void derive_key( int alg_arg,
4879 data_t *key_data, data_t *input1, data_t *input2,
4880 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004881 int expected_status_arg,
4882 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004883{
Ronald Cron5425a212020-08-04 14:58:35 +02004884 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4885 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004886 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004887 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004888 size_t bits = bits_arg;
4889 psa_status_t expected_status = expected_status_arg;
4890 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4891 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4892 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4893
4894 PSA_ASSERT( psa_crypto_init( ) );
4895
4896 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4897 psa_set_key_algorithm( &base_attributes, alg );
4898 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4899 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004900 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004901
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004902 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4903 input1->x, input1->len,
4904 input2->x, input2->len,
4905 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004906 goto exit;
4907
4908 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4909 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004910 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004911 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004912
4913 psa_status_t status =
4914 psa_key_derivation_output_key( &derived_attributes,
4915 &operation,
4916 &derived_key );
4917 if( is_large_output > 0 )
4918 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4919 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004920
4921exit:
4922 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004923 psa_destroy_key( base_key );
4924 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004925 PSA_DONE( );
4926}
4927/* END_CASE */
4928
4929/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004931 int our_key_type_arg, int our_key_alg_arg,
4932 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004933 int expected_status_arg )
4934{
Ronald Cron5425a212020-08-04 14:58:35 +02004935 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004936 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004937 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004938 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004941 psa_status_t expected_status = expected_status_arg;
4942 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004943
Gilles Peskine8817f612018-12-18 00:18:46 +01004944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004945
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004946 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004947 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004948 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004949 PSA_ASSERT( psa_import_key( &attributes,
4950 our_key_data->x, our_key_data->len,
4951 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004952
Gilles Peskine77f40d82019-04-11 21:27:06 +02004953 /* The tests currently include inputs that should fail at either step.
4954 * Test cases that fail at the setup step should be changed to call
4955 * key_derivation_setup instead, and this function should be renamed
4956 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004958 if( status == PSA_SUCCESS )
4959 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004960 TEST_EQUAL( psa_key_derivation_key_agreement(
4961 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4962 our_key,
4963 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004964 expected_status );
4965 }
4966 else
4967 {
4968 TEST_ASSERT( status == expected_status );
4969 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004970
4971exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004973 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004974 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004975}
4976/* END_CASE */
4977
4978/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004979void raw_key_agreement( int alg_arg,
4980 int our_key_type_arg, data_t *our_key_data,
4981 data_t *peer_key_data,
4982 data_t *expected_output )
4983{
Ronald Cron5425a212020-08-04 14:58:35 +02004984 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004985 psa_algorithm_t alg = alg_arg;
4986 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004987 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004988 unsigned char *output = NULL;
4989 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004990 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004991
4992 ASSERT_ALLOC( output, expected_output->len );
4993 PSA_ASSERT( psa_crypto_init( ) );
4994
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004995 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4996 psa_set_key_algorithm( &attributes, alg );
4997 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004998 PSA_ASSERT( psa_import_key( &attributes,
4999 our_key_data->x, our_key_data->len,
5000 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005001
gabor-mezei-armceface22021-01-21 12:26:17 +01005002 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5003 key_bits = psa_get_key_bits( &attributes );
5004
Gilles Peskinebe697d82019-05-16 18:00:41 +02005005 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5006 peer_key_data->x, peer_key_data->len,
5007 output, expected_output->len,
5008 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005009 ASSERT_COMPARE( output, output_length,
5010 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005011 TEST_ASSERT( output_length <=
5012 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5013 TEST_ASSERT( output_length <=
5014 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005015
5016exit:
5017 mbedtls_free( output );
5018 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005019 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005020}
5021/* END_CASE */
5022
5023/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005024void key_agreement_capacity( int alg_arg,
5025 int our_key_type_arg, data_t *our_key_data,
5026 data_t *peer_key_data,
5027 int expected_capacity_arg )
5028{
Ronald Cron5425a212020-08-04 14:58:35 +02005029 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005030 psa_algorithm_t alg = alg_arg;
5031 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005034 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005035 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005036
Gilles Peskine8817f612018-12-18 00:18:46 +01005037 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005038
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005039 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5040 psa_set_key_algorithm( &attributes, alg );
5041 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005042 PSA_ASSERT( psa_import_key( &attributes,
5043 our_key_data->x, our_key_data->len,
5044 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005045
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005047 PSA_ASSERT( psa_key_derivation_key_agreement(
5048 &operation,
5049 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5050 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005051 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5052 {
5053 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005055 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005056 NULL, 0 ) );
5057 }
Gilles Peskine59685592018-09-18 12:11:34 +02005058
Gilles Peskinebf491972018-10-25 22:36:12 +02005059 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005060 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005061 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005062 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005063
Gilles Peskinebf491972018-10-25 22:36:12 +02005064 /* Test the actual capacity by reading the output. */
5065 while( actual_capacity > sizeof( output ) )
5066 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005067 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005068 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005069 actual_capacity -= sizeof( output );
5070 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005071 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005072 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005073 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005074 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005075
Gilles Peskine59685592018-09-18 12:11:34 +02005076exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005077 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005078 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005079 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005080}
5081/* END_CASE */
5082
5083/* BEGIN_CASE */
5084void key_agreement_output( int alg_arg,
5085 int our_key_type_arg, data_t *our_key_data,
5086 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005087 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005088{
Ronald Cron5425a212020-08-04 14:58:35 +02005089 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005090 psa_algorithm_t alg = alg_arg;
5091 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005094 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005095
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005096 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5097 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005098
Gilles Peskine8817f612018-12-18 00:18:46 +01005099 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005100
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5102 psa_set_key_algorithm( &attributes, alg );
5103 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005104 PSA_ASSERT( psa_import_key( &attributes,
5105 our_key_data->x, our_key_data->len,
5106 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005107
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005109 PSA_ASSERT( psa_key_derivation_key_agreement(
5110 &operation,
5111 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5112 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005113 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5114 {
5115 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005117 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005118 NULL, 0 ) );
5119 }
Gilles Peskine59685592018-09-18 12:11:34 +02005120
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005122 actual_output,
5123 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005124 ASSERT_COMPARE( actual_output, expected_output1->len,
5125 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005126 if( expected_output2->len != 0 )
5127 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005128 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005129 actual_output,
5130 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005131 ASSERT_COMPARE( actual_output, expected_output2->len,
5132 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005133 }
Gilles Peskine59685592018-09-18 12:11:34 +02005134
5135exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005137 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005138 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005139 mbedtls_free( actual_output );
5140}
5141/* END_CASE */
5142
5143/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005144void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005145{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005146 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005147 unsigned char *output = NULL;
5148 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005149 size_t i;
5150 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005151
Simon Butcher49f8e312020-03-03 15:51:50 +00005152 TEST_ASSERT( bytes_arg >= 0 );
5153
Gilles Peskine91892022021-02-08 19:50:26 +01005154 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005155 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005156
Gilles Peskine8817f612018-12-18 00:18:46 +01005157 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005158
Gilles Peskinea50d7392018-06-21 10:22:13 +02005159 /* Run several times, to ensure that every output byte will be
5160 * nonzero at least once with overwhelming probability
5161 * (2^(-8*number_of_runs)). */
5162 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005163 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005164 if( bytes != 0 )
5165 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005166 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005167
Gilles Peskinea50d7392018-06-21 10:22:13 +02005168 for( i = 0; i < bytes; i++ )
5169 {
5170 if( output[i] != 0 )
5171 ++changed[i];
5172 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005173 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005174
5175 /* Check that every byte was changed to nonzero at least once. This
5176 * validates that psa_generate_random is overwriting every byte of
5177 * the output buffer. */
5178 for( i = 0; i < bytes; i++ )
5179 {
5180 TEST_ASSERT( changed[i] != 0 );
5181 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005182
5183exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005184 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005185 mbedtls_free( output );
5186 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005187}
5188/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005189
5190/* BEGIN_CASE */
5191void generate_key( int type_arg,
5192 int bits_arg,
5193 int usage_arg,
5194 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005195 int expected_status_arg,
5196 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005197{
Ronald Cron5425a212020-08-04 14:58:35 +02005198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005199 psa_key_type_t type = type_arg;
5200 psa_key_usage_t usage = usage_arg;
5201 size_t bits = bits_arg;
5202 psa_algorithm_t alg = alg_arg;
5203 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005205 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005206
Gilles Peskine8817f612018-12-18 00:18:46 +01005207 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005208
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005209 psa_set_key_usage_flags( &attributes, usage );
5210 psa_set_key_algorithm( &attributes, alg );
5211 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005212 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005213
5214 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005215 psa_status_t status = psa_generate_key( &attributes, &key );
5216
5217 if( is_large_key > 0 )
5218 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5219 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005220 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005221 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005222
5223 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005224 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005225 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5226 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005227
Gilles Peskine818ca122018-06-20 18:16:48 +02005228 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005229 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005230 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005231
5232exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005233 /*
5234 * Key attributes may have been returned by psa_get_key_attributes()
5235 * thus reset them as required.
5236 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005237 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005238
Ronald Cron5425a212020-08-04 14:58:35 +02005239 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005240 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005241}
5242/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005243
Ronald Cronee414c72021-03-18 18:50:08 +01005244/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005245void generate_key_rsa( int bits_arg,
5246 data_t *e_arg,
5247 int expected_status_arg )
5248{
Ronald Cron5425a212020-08-04 14:58:35 +02005249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005250 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005251 size_t bits = bits_arg;
5252 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5253 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5254 psa_status_t expected_status = expected_status_arg;
5255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5256 uint8_t *exported = NULL;
5257 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005258 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005259 size_t exported_length = SIZE_MAX;
5260 uint8_t *e_read_buffer = NULL;
5261 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005262 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005263 size_t e_read_length = SIZE_MAX;
5264
5265 if( e_arg->len == 0 ||
5266 ( e_arg->len == 3 &&
5267 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5268 {
5269 is_default_public_exponent = 1;
5270 e_read_size = 0;
5271 }
5272 ASSERT_ALLOC( e_read_buffer, e_read_size );
5273 ASSERT_ALLOC( exported, exported_size );
5274
5275 PSA_ASSERT( psa_crypto_init( ) );
5276
5277 psa_set_key_usage_flags( &attributes, usage );
5278 psa_set_key_algorithm( &attributes, alg );
5279 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5280 e_arg->x, e_arg->len ) );
5281 psa_set_key_bits( &attributes, bits );
5282
5283 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005284 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005285 if( expected_status != PSA_SUCCESS )
5286 goto exit;
5287
5288 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005289 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005290 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5291 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5292 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5293 e_read_buffer, e_read_size,
5294 &e_read_length ) );
5295 if( is_default_public_exponent )
5296 TEST_EQUAL( e_read_length, 0 );
5297 else
5298 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5299
5300 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005301 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005302 goto exit;
5303
5304 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005305 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005306 exported, exported_size,
5307 &exported_length ) );
5308 {
5309 uint8_t *p = exported;
5310 uint8_t *end = exported + exported_length;
5311 size_t len;
5312 /* RSAPublicKey ::= SEQUENCE {
5313 * modulus INTEGER, -- n
5314 * publicExponent INTEGER } -- e
5315 */
5316 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005317 MBEDTLS_ASN1_SEQUENCE |
5318 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005319 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005320 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5321 MBEDTLS_ASN1_INTEGER ) );
5322 if( len >= 1 && p[0] == 0 )
5323 {
5324 ++p;
5325 --len;
5326 }
5327 if( e_arg->len == 0 )
5328 {
5329 TEST_EQUAL( len, 3 );
5330 TEST_EQUAL( p[0], 1 );
5331 TEST_EQUAL( p[1], 0 );
5332 TEST_EQUAL( p[2], 1 );
5333 }
5334 else
5335 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5336 }
5337
5338exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005339 /*
5340 * Key attributes may have been returned by psa_get_key_attributes() or
5341 * set by psa_set_key_domain_parameters() thus reset them as required.
5342 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005343 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005344
Ronald Cron5425a212020-08-04 14:58:35 +02005345 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005346 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005347 mbedtls_free( e_read_buffer );
5348 mbedtls_free( exported );
5349}
5350/* END_CASE */
5351
Darryl Greend49a4992018-06-18 17:27:26 +01005352/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005353void persistent_key_load_key_from_storage( data_t *data,
5354 int type_arg, int bits_arg,
5355 int usage_flags_arg, int alg_arg,
5356 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005357{
Ronald Cron71016a92020-08-28 19:01:50 +02005358 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5361 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005362 psa_key_type_t type = type_arg;
5363 size_t bits = bits_arg;
5364 psa_key_usage_t usage_flags = usage_flags_arg;
5365 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005366 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005367 unsigned char *first_export = NULL;
5368 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005369 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005370 size_t first_exported_length;
5371 size_t second_exported_length;
5372
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005373 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5374 {
5375 ASSERT_ALLOC( first_export, export_size );
5376 ASSERT_ALLOC( second_export, export_size );
5377 }
Darryl Greend49a4992018-06-18 17:27:26 +01005378
Gilles Peskine8817f612018-12-18 00:18:46 +01005379 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005380
Gilles Peskinec87af662019-05-15 16:12:22 +02005381 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005382 psa_set_key_usage_flags( &attributes, usage_flags );
5383 psa_set_key_algorithm( &attributes, alg );
5384 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005385 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005386
Darryl Green0c6575a2018-11-07 16:05:30 +00005387 switch( generation_method )
5388 {
5389 case IMPORT_KEY:
5390 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005391 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005392 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005393 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005394
Darryl Green0c6575a2018-11-07 16:05:30 +00005395 case GENERATE_KEY:
5396 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005397 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005398 break;
5399
5400 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005401#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005402 {
5403 /* Create base key */
5404 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5405 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5406 psa_set_key_usage_flags( &base_attributes,
5407 PSA_KEY_USAGE_DERIVE );
5408 psa_set_key_algorithm( &base_attributes, derive_alg );
5409 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005410 PSA_ASSERT( psa_import_key( &base_attributes,
5411 data->x, data->len,
5412 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005413 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005414 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005415 PSA_ASSERT( psa_key_derivation_input_key(
5416 &operation,
5417 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005418 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005420 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005421 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5422 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005423 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005425 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005426 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005427 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005428#else
5429 TEST_ASSUME( ! "KDF not supported in this configuration" );
5430#endif
5431 break;
5432
5433 default:
5434 TEST_ASSERT( ! "generation_method not implemented in test" );
5435 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005436 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005437 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005438
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005439 /* Export the key if permitted by the key policy. */
5440 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5441 {
Ronald Cron5425a212020-08-04 14:58:35 +02005442 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005443 first_export, export_size,
5444 &first_exported_length ) );
5445 if( generation_method == IMPORT_KEY )
5446 ASSERT_COMPARE( data->x, data->len,
5447 first_export, first_exported_length );
5448 }
Darryl Greend49a4992018-06-18 17:27:26 +01005449
5450 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005451 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005452 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005453 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005454
Darryl Greend49a4992018-06-18 17:27:26 +01005455 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005456 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005457 TEST_ASSERT( mbedtls_svc_key_id_equal(
5458 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005459 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5460 PSA_KEY_LIFETIME_PERSISTENT );
5461 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5462 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5463 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5464 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005465
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005466 /* Export the key again if permitted by the key policy. */
5467 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005468 {
Ronald Cron5425a212020-08-04 14:58:35 +02005469 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005470 second_export, export_size,
5471 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005472 ASSERT_COMPARE( first_export, first_exported_length,
5473 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005474 }
5475
5476 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005477 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005478 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005479
5480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005481 /*
5482 * Key attributes may have been returned by psa_get_key_attributes()
5483 * thus reset them as required.
5484 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005485 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005486
Darryl Greend49a4992018-06-18 17:27:26 +01005487 mbedtls_free( first_export );
5488 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005489 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005490 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005491 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005492 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005493}
5494/* END_CASE */