blob: 5e32a892a1e2c066b5ff6727e97e6db7b903a7f9 [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
Gilles Peskine8b356b52020-08-25 23:44:59 +02002051 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002052 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002053 PSA_ASSERT( psa_mac_update( &operation,
2054 input->x, input->len ) );
2055 TEST_EQUAL( psa_mac_sign_finish( &operation,
2056 actual_mac, output_size,
2057 &mac_length ),
2058 expected_status );
2059 PSA_ASSERT( psa_mac_abort( &operation ) );
2060
2061 if( expected_status == PSA_SUCCESS )
2062 {
2063 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2064 actual_mac, mac_length );
2065 }
2066 mbedtls_free( actual_mac );
2067 actual_mac = NULL;
2068 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002069
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002070exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002071 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002072 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002073 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002074 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002075}
2076/* END_CASE */
2077
2078/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002079void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002080 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002081 int alg_arg,
2082 data_t *input,
2083 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084{
Ronald Cron5425a212020-08-04 14:58:35 +02002085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002086 psa_key_type_t key_type = key_type_arg;
2087 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002088 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002089 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002090 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002091
Gilles Peskine69c12672018-06-28 00:07:19 +02002092 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2093
Gilles Peskine8817f612018-12-18 00:18:46 +01002094 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002095
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002096 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002097 psa_set_key_algorithm( &attributes, alg );
2098 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002099
Ronald Cron5425a212020-08-04 14:58:35 +02002100 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2101 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002102
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002103 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002104 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002105 PSA_ASSERT( psa_mac_update( &operation,
2106 input->x, input->len ) );
2107 PSA_ASSERT( psa_mac_verify_finish( &operation,
2108 expected_mac->x,
2109 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002110
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002111 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002112 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002113 PSA_ASSERT( psa_mac_update( &operation,
2114 input->x, input->len ) );
2115 TEST_EQUAL( psa_mac_verify_finish( &operation,
2116 expected_mac->x,
2117 expected_mac->len - 1 ),
2118 PSA_ERROR_INVALID_SIGNATURE );
2119
2120 /* Test a MAC that's too long. */
2121 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2122 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002123 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002124 PSA_ASSERT( psa_mac_update( &operation,
2125 input->x, input->len ) );
2126 TEST_EQUAL( psa_mac_verify_finish( &operation,
2127 perturbed_mac,
2128 expected_mac->len + 1 ),
2129 PSA_ERROR_INVALID_SIGNATURE );
2130
2131 /* Test changing one byte. */
2132 for( size_t i = 0; i < expected_mac->len; i++ )
2133 {
Chris Jones9634bb12021-01-20 15:56:42 +00002134 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002135 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002136 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002137 PSA_ASSERT( psa_mac_update( &operation,
2138 input->x, input->len ) );
2139 TEST_EQUAL( psa_mac_verify_finish( &operation,
2140 perturbed_mac,
2141 expected_mac->len ),
2142 PSA_ERROR_INVALID_SIGNATURE );
2143 perturbed_mac[i] ^= 1;
2144 }
2145
Gilles Peskine8c9def32018-02-08 10:02:12 +01002146exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002147 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002148 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002149 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002150 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002151}
2152/* END_CASE */
2153
2154/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002155void cipher_operation_init( )
2156{
Jaeden Ameroab439972019-02-15 14:12:05 +00002157 const uint8_t input[1] = { 0 };
2158 unsigned char output[1] = { 0 };
2159 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002160 /* Test each valid way of initializing the object, except for `= {0}`, as
2161 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2162 * though it's OK by the C standard. We could test for this, but we'd need
2163 * to supress the Clang warning for the test. */
2164 psa_cipher_operation_t func = psa_cipher_operation_init( );
2165 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2166 psa_cipher_operation_t zero;
2167
2168 memset( &zero, 0, sizeof( zero ) );
2169
Jaeden Ameroab439972019-02-15 14:12:05 +00002170 /* A freshly-initialized cipher operation should not be usable. */
2171 TEST_EQUAL( psa_cipher_update( &func,
2172 input, sizeof( input ),
2173 output, sizeof( output ),
2174 &output_length ),
2175 PSA_ERROR_BAD_STATE );
2176 TEST_EQUAL( psa_cipher_update( &init,
2177 input, sizeof( input ),
2178 output, sizeof( output ),
2179 &output_length ),
2180 PSA_ERROR_BAD_STATE );
2181 TEST_EQUAL( psa_cipher_update( &zero,
2182 input, sizeof( input ),
2183 output, sizeof( output ),
2184 &output_length ),
2185 PSA_ERROR_BAD_STATE );
2186
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002187 /* A default cipher operation should be abortable without error. */
2188 PSA_ASSERT( psa_cipher_abort( &func ) );
2189 PSA_ASSERT( psa_cipher_abort( &init ) );
2190 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002191}
2192/* END_CASE */
2193
2194/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002195void cipher_setup( int key_type_arg,
2196 data_t *key,
2197 int alg_arg,
2198 int expected_status_arg )
2199{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002200 psa_key_type_t key_type = key_type_arg;
2201 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002202 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002203 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002204 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002205#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002206 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2207#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002208
Gilles Peskine8817f612018-12-18 00:18:46 +01002209 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002210
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002211 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2212 &operation, &status ) )
2213 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002214 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002215
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002216 /* The operation object should be reusable. */
2217#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2218 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2219 smoke_test_key_data,
2220 sizeof( smoke_test_key_data ),
2221 KNOWN_SUPPORTED_CIPHER_ALG,
2222 &operation, &status ) )
2223 goto exit;
2224 TEST_EQUAL( status, PSA_SUCCESS );
2225#endif
2226
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002227exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002228 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002229 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002230}
2231/* END_CASE */
2232
Ronald Cronee414c72021-03-18 18:50:08 +01002233/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002234void cipher_bad_order( )
2235{
Ronald Cron5425a212020-08-04 14:58:35 +02002236 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002237 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2238 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002240 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002241 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002242 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002243 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2244 0xaa, 0xaa, 0xaa, 0xaa };
2245 const uint8_t text[] = {
2246 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2247 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002248 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002249 size_t length = 0;
2250
2251 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002252 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2253 psa_set_key_algorithm( &attributes, alg );
2254 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002255 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2256 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002257
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002258 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002259 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2260 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002261 PSA_ERROR_BAD_STATE );
2262 PSA_ASSERT( psa_cipher_abort( &operation ) );
2263
2264 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002265 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2266 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002267 PSA_ERROR_BAD_STATE );
2268 PSA_ASSERT( psa_cipher_abort( &operation ) );
2269
Jaeden Ameroab439972019-02-15 14:12:05 +00002270 /* Generate an IV without calling setup beforehand. */
2271 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2272 buffer, sizeof( buffer ),
2273 &length ),
2274 PSA_ERROR_BAD_STATE );
2275 PSA_ASSERT( psa_cipher_abort( &operation ) );
2276
2277 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002278 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002279 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2280 buffer, sizeof( buffer ),
2281 &length ) );
2282 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2283 buffer, sizeof( buffer ),
2284 &length ),
2285 PSA_ERROR_BAD_STATE );
2286 PSA_ASSERT( psa_cipher_abort( &operation ) );
2287
2288 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002289 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002290 PSA_ASSERT( psa_cipher_set_iv( &operation,
2291 iv, sizeof( iv ) ) );
2292 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2293 buffer, sizeof( buffer ),
2294 &length ),
2295 PSA_ERROR_BAD_STATE );
2296 PSA_ASSERT( psa_cipher_abort( &operation ) );
2297
2298 /* Set an IV without calling setup beforehand. */
2299 TEST_EQUAL( psa_cipher_set_iv( &operation,
2300 iv, sizeof( iv ) ),
2301 PSA_ERROR_BAD_STATE );
2302 PSA_ASSERT( psa_cipher_abort( &operation ) );
2303
2304 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002305 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002306 PSA_ASSERT( psa_cipher_set_iv( &operation,
2307 iv, sizeof( iv ) ) );
2308 TEST_EQUAL( psa_cipher_set_iv( &operation,
2309 iv, sizeof( iv ) ),
2310 PSA_ERROR_BAD_STATE );
2311 PSA_ASSERT( psa_cipher_abort( &operation ) );
2312
2313 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002314 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002315 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2316 buffer, sizeof( buffer ),
2317 &length ) );
2318 TEST_EQUAL( psa_cipher_set_iv( &operation,
2319 iv, sizeof( iv ) ),
2320 PSA_ERROR_BAD_STATE );
2321 PSA_ASSERT( psa_cipher_abort( &operation ) );
2322
2323 /* Call update without calling setup beforehand. */
2324 TEST_EQUAL( psa_cipher_update( &operation,
2325 text, sizeof( text ),
2326 buffer, sizeof( buffer ),
2327 &length ),
2328 PSA_ERROR_BAD_STATE );
2329 PSA_ASSERT( psa_cipher_abort( &operation ) );
2330
2331 /* Call update without an IV where an IV is required. */
2332 TEST_EQUAL( psa_cipher_update( &operation,
2333 text, sizeof( text ),
2334 buffer, sizeof( buffer ),
2335 &length ),
2336 PSA_ERROR_BAD_STATE );
2337 PSA_ASSERT( psa_cipher_abort( &operation ) );
2338
2339 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002340 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002341 PSA_ASSERT( psa_cipher_set_iv( &operation,
2342 iv, sizeof( iv ) ) );
2343 PSA_ASSERT( psa_cipher_finish( &operation,
2344 buffer, sizeof( buffer ), &length ) );
2345 TEST_EQUAL( psa_cipher_update( &operation,
2346 text, sizeof( text ),
2347 buffer, sizeof( buffer ),
2348 &length ),
2349 PSA_ERROR_BAD_STATE );
2350 PSA_ASSERT( psa_cipher_abort( &operation ) );
2351
2352 /* Call finish without calling setup beforehand. */
2353 TEST_EQUAL( psa_cipher_finish( &operation,
2354 buffer, sizeof( buffer ), &length ),
2355 PSA_ERROR_BAD_STATE );
2356 PSA_ASSERT( psa_cipher_abort( &operation ) );
2357
2358 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002359 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002360 /* Not calling update means we are encrypting an empty buffer, which is OK
2361 * for cipher modes with padding. */
2362 TEST_EQUAL( psa_cipher_finish( &operation,
2363 buffer, sizeof( buffer ), &length ),
2364 PSA_ERROR_BAD_STATE );
2365 PSA_ASSERT( psa_cipher_abort( &operation ) );
2366
2367 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002368 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002369 PSA_ASSERT( psa_cipher_set_iv( &operation,
2370 iv, sizeof( iv ) ) );
2371 PSA_ASSERT( psa_cipher_finish( &operation,
2372 buffer, sizeof( buffer ), &length ) );
2373 TEST_EQUAL( psa_cipher_finish( &operation,
2374 buffer, sizeof( buffer ), &length ),
2375 PSA_ERROR_BAD_STATE );
2376 PSA_ASSERT( psa_cipher_abort( &operation ) );
2377
Ronald Cron5425a212020-08-04 14:58:35 +02002378 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002379
Jaeden Ameroab439972019-02-15 14:12:05 +00002380exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002381 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002382 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002383}
2384/* END_CASE */
2385
2386/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002388 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002389 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002390 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391{
Ronald Cron5425a212020-08-04 14:58:35 +02002392 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002393 psa_status_t status;
2394 psa_key_type_t key_type = key_type_arg;
2395 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002396 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002397 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002398 size_t output_buffer_size = 0;
2399 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002400 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002401 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002403
Gilles Peskine8817f612018-12-18 00:18:46 +01002404 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002406 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2407 psa_set_key_algorithm( &attributes, alg );
2408 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002409
Ronald Cron5425a212020-08-04 14:58:35 +02002410 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2411 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002412
Ronald Cron5425a212020-08-04 14:58:35 +02002413 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002414
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002415 if( iv->len > 0 )
2416 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002417 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002418 }
2419
gabor-mezei-armceface22021-01-21 12:26:17 +01002420 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2421 TEST_ASSERT( output_buffer_size <=
2422 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002423 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002424
Gilles Peskine8817f612018-12-18 00:18:46 +01002425 PSA_ASSERT( psa_cipher_update( &operation,
2426 input->x, input->len,
2427 output, output_buffer_size,
2428 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002429 TEST_ASSERT( function_output_length <=
2430 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2431 TEST_ASSERT( function_output_length <=
2432 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002433 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002434
Gilles Peskine50e586b2018-06-08 14:28:46 +02002435 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002436 ( output_buffer_size == 0 ? NULL :
2437 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002438 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002439 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002440 TEST_ASSERT( function_output_length <=
2441 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2442 TEST_ASSERT( function_output_length <=
2443 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002444 total_output_length += function_output_length;
2445
Gilles Peskinefe11b722018-12-18 00:24:04 +01002446 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002447 if( expected_status == PSA_SUCCESS )
2448 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002449 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002450 ASSERT_COMPARE( expected_output->x, expected_output->len,
2451 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002452 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002453
Gilles Peskine50e586b2018-06-08 14:28:46 +02002454exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002455 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002456 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002457 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002458 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002459}
2460/* END_CASE */
2461
2462/* BEGIN_CASE */
2463void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002464 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002465 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002466 int first_part_size_arg,
2467 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002468 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002469{
Ronald Cron5425a212020-08-04 14:58:35 +02002470 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002471 psa_key_type_t key_type = key_type_arg;
2472 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002473 size_t first_part_size = first_part_size_arg;
2474 size_t output1_length = output1_length_arg;
2475 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002476 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002477 size_t output_buffer_size = 0;
2478 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002479 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002480 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002482
Gilles Peskine8817f612018-12-18 00:18:46 +01002483 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002484
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002485 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 );
Moran Pekered346952018-07-05 15:22:45 +03002488
Ronald Cron5425a212020-08-04 14:58:35 +02002489 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2490 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002491
Ronald Cron5425a212020-08-04 14:58:35 +02002492 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002493
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002494 if( iv->len > 0 )
2495 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002496 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002497 }
2498
gabor-mezei-armceface22021-01-21 12:26:17 +01002499 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2500 TEST_ASSERT( output_buffer_size <=
2501 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002502 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002503
Gilles Peskinee0866522019-02-19 19:44:00 +01002504 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002505 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2506 output, output_buffer_size,
2507 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002508 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002509 TEST_ASSERT( function_output_length <=
2510 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2511 TEST_ASSERT( function_output_length <=
2512 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002513 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002514
Gilles Peskine8817f612018-12-18 00:18:46 +01002515 PSA_ASSERT( psa_cipher_update( &operation,
2516 input->x + first_part_size,
2517 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002518 ( output_buffer_size == 0 ? NULL :
2519 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002520 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002521 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002522 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002523 TEST_ASSERT( function_output_length <=
2524 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2525 alg,
2526 input->len - first_part_size ) );
2527 TEST_ASSERT( function_output_length <=
2528 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002529 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002530
Gilles Peskine8817f612018-12-18 00:18:46 +01002531 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002532 ( output_buffer_size == 0 ? NULL :
2533 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002534 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002535 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002536 TEST_ASSERT( function_output_length <=
2537 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2538 TEST_ASSERT( function_output_length <=
2539 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002540 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002541 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002542
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002543 ASSERT_COMPARE( expected_output->x, expected_output->len,
2544 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002545
2546exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002547 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002548 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002549 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002550 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002551}
2552/* END_CASE */
2553
2554/* BEGIN_CASE */
2555void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002556 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002557 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002558 int first_part_size_arg,
2559 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002560 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002561{
Ronald Cron5425a212020-08-04 14:58:35 +02002562 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002563 psa_key_type_t key_type = key_type_arg;
2564 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002565 size_t first_part_size = first_part_size_arg;
2566 size_t output1_length = output1_length_arg;
2567 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002568 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002569 size_t output_buffer_size = 0;
2570 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002571 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002572 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002574
Gilles Peskine8817f612018-12-18 00:18:46 +01002575 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002576
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002577 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2578 psa_set_key_algorithm( &attributes, alg );
2579 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002580
Ronald Cron5425a212020-08-04 14:58:35 +02002581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2582 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583
Ronald Cron5425a212020-08-04 14:58:35 +02002584 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002585
Steven Cooreman177deba2020-09-07 17:14:14 +02002586 if( iv->len > 0 )
2587 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002588 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002589 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002590
gabor-mezei-armceface22021-01-21 12:26:17 +01002591 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2592 TEST_ASSERT( output_buffer_size <=
2593 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002594 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002595
Gilles Peskinee0866522019-02-19 19:44:00 +01002596 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002597 PSA_ASSERT( psa_cipher_update( &operation,
2598 input->x, first_part_size,
2599 output, output_buffer_size,
2600 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002601 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002602 TEST_ASSERT( function_output_length <=
2603 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2604 TEST_ASSERT( function_output_length <=
2605 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002606 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002607
Gilles Peskine8817f612018-12-18 00:18:46 +01002608 PSA_ASSERT( psa_cipher_update( &operation,
2609 input->x + first_part_size,
2610 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002611 ( output_buffer_size == 0 ? NULL :
2612 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002613 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002614 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002615 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002616 TEST_ASSERT( function_output_length <=
2617 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2618 alg,
2619 input->len - first_part_size ) );
2620 TEST_ASSERT( function_output_length <=
2621 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002622 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002623
Gilles Peskine8817f612018-12-18 00:18:46 +01002624 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002625 ( output_buffer_size == 0 ? NULL :
2626 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002627 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002628 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002629 TEST_ASSERT( function_output_length <=
2630 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2631 TEST_ASSERT( function_output_length <=
2632 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002633 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002634 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002636 ASSERT_COMPARE( expected_output->x, expected_output->len,
2637 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002638
2639exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002640 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002641 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002642 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002643 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644}
2645/* END_CASE */
2646
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647/* BEGIN_CASE */
2648void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002649 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002650 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002651 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652{
Ronald Cron5425a212020-08-04 14:58:35 +02002653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654 psa_status_t status;
2655 psa_key_type_t key_type = key_type_arg;
2656 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002657 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002658 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659 size_t output_buffer_size = 0;
2660 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002661 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002662 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002664
Gilles Peskine8817f612018-12-18 00:18:46 +01002665 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002667 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2668 psa_set_key_algorithm( &attributes, alg );
2669 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002670
Ronald Cron5425a212020-08-04 14:58:35 +02002671 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2672 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Ronald Cron5425a212020-08-04 14:58:35 +02002674 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
Steven Cooreman177deba2020-09-07 17:14:14 +02002676 if( iv->len > 0 )
2677 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002678 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002679 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680
gabor-mezei-armceface22021-01-21 12:26:17 +01002681 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2682 TEST_ASSERT( output_buffer_size <=
2683 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002684 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685
Gilles Peskine8817f612018-12-18 00:18:46 +01002686 PSA_ASSERT( psa_cipher_update( &operation,
2687 input->x, input->len,
2688 output, output_buffer_size,
2689 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002690 TEST_ASSERT( function_output_length <=
2691 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2692 TEST_ASSERT( function_output_length <=
2693 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002694 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002695
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002697 ( output_buffer_size == 0 ? NULL :
2698 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002699 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002701 TEST_ASSERT( function_output_length <=
2702 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2703 TEST_ASSERT( function_output_length <=
2704 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002705 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002706 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002707
2708 if( expected_status == PSA_SUCCESS )
2709 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002711 ASSERT_COMPARE( expected_output->x, expected_output->len,
2712 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713 }
2714
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002716 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002717 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002718 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002719 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720}
2721/* END_CASE */
2722
Gilles Peskine50e586b2018-06-08 14:28:46 +02002723/* BEGIN_CASE */
2724void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002725 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002726 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002727{
Ronald Cron5425a212020-08-04 14:58:35 +02002728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002729 psa_key_type_t key_type = key_type_arg;
2730 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002731 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002732 size_t iv_size = 16;
2733 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002734 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002735 size_t output1_size = 0;
2736 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002737 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002738 size_t output2_size = 0;
2739 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002740 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002741 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2742 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002744
Gilles Peskine8817f612018-12-18 00:18:46 +01002745 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002746
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002747 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2748 psa_set_key_algorithm( &attributes, alg );
2749 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002750
Ronald Cron5425a212020-08-04 14:58:35 +02002751 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2752 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002753
Ronald Cron5425a212020-08-04 14:58:35 +02002754 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2755 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002756
Steven Cooreman177deba2020-09-07 17:14:14 +02002757 if( alg != PSA_ALG_ECB_NO_PADDING )
2758 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002759 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2760 iv, iv_size,
2761 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002762 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002763 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2764 TEST_ASSERT( output1_size <=
2765 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002766 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002767
Gilles Peskine8817f612018-12-18 00:18:46 +01002768 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2769 output1, output1_size,
2770 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002771 TEST_ASSERT( output1_length <=
2772 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2773 TEST_ASSERT( output1_length <=
2774 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002777 output1 + output1_length,
2778 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002779 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002780 TEST_ASSERT( function_output_length <=
2781 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2782 TEST_ASSERT( function_output_length <=
2783 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002784
Gilles Peskine048b7f02018-06-08 14:20:49 +02002785 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002786
Gilles Peskine8817f612018-12-18 00:18:46 +01002787 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002788
2789 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002790 TEST_ASSERT( output2_size <=
2791 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2792 TEST_ASSERT( output2_size <=
2793 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002794 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002795
Steven Cooreman177deba2020-09-07 17:14:14 +02002796 if( iv_length > 0 )
2797 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002798 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2799 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002800 }
2801
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2803 output2, output2_size,
2804 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002805 TEST_ASSERT( output2_length <=
2806 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2807 TEST_ASSERT( output2_length <=
2808 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2809
Gilles Peskine048b7f02018-06-08 14:20:49 +02002810 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002811 PSA_ASSERT( psa_cipher_finish( &operation2,
2812 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002813 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002814 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002815 TEST_ASSERT( function_output_length <=
2816 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2817 TEST_ASSERT( function_output_length <=
2818 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002819
Gilles Peskine048b7f02018-06-08 14:20:49 +02002820 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002821
Gilles Peskine8817f612018-12-18 00:18:46 +01002822 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002823
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002824 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002825
2826exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002827 psa_cipher_abort( &operation1 );
2828 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002829 mbedtls_free( output1 );
2830 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002831 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002832 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002833}
2834/* END_CASE */
2835
2836/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837void cipher_verify_output_multipart( int alg_arg,
2838 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002839 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002840 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002841 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002842{
Ronald Cron5425a212020-08-04 14:58:35 +02002843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002844 psa_key_type_t key_type = key_type_arg;
2845 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002846 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002847 unsigned char iv[16] = {0};
2848 size_t iv_size = 16;
2849 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002850 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002851 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002852 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002853 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002854 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002855 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002856 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002857 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2858 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002860
Gilles Peskine8817f612018-12-18 00:18:46 +01002861 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002862
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002863 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2864 psa_set_key_algorithm( &attributes, alg );
2865 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002866
Ronald Cron5425a212020-08-04 14:58:35 +02002867 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2868 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002869
Ronald Cron5425a212020-08-04 14:58:35 +02002870 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2871 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002872
Steven Cooreman177deba2020-09-07 17:14:14 +02002873 if( alg != PSA_ALG_ECB_NO_PADDING )
2874 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002875 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2876 iv, iv_size,
2877 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002878 }
2879
gabor-mezei-armceface22021-01-21 12:26:17 +01002880 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2881 TEST_ASSERT( output1_buffer_size <=
2882 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002883 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002884
Gilles Peskinee0866522019-02-19 19:44:00 +01002885 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002886
Gilles Peskine8817f612018-12-18 00:18:46 +01002887 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2888 output1, output1_buffer_size,
2889 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002890 TEST_ASSERT( function_output_length <=
2891 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2892 TEST_ASSERT( function_output_length <=
2893 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002894 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002895
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 PSA_ASSERT( psa_cipher_update( &operation1,
2897 input->x + first_part_size,
2898 input->len - first_part_size,
2899 output1, output1_buffer_size,
2900 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002901 TEST_ASSERT( function_output_length <=
2902 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2903 alg,
2904 input->len - first_part_size ) );
2905 TEST_ASSERT( function_output_length <=
2906 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002907 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002908
Gilles Peskine8817f612018-12-18 00:18:46 +01002909 PSA_ASSERT( psa_cipher_finish( &operation1,
2910 output1 + output1_length,
2911 output1_buffer_size - output1_length,
2912 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002913 TEST_ASSERT( function_output_length <=
2914 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2915 TEST_ASSERT( function_output_length <=
2916 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002917 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002918
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002920
Gilles Peskine048b7f02018-06-08 14:20:49 +02002921 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002922 TEST_ASSERT( output2_buffer_size <=
2923 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2924 TEST_ASSERT( output2_buffer_size <=
2925 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002926 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002927
Steven Cooreman177deba2020-09-07 17:14:14 +02002928 if( iv_length > 0 )
2929 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002930 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2931 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002932 }
Moran Pekerded84402018-06-06 16:36:50 +03002933
Gilles Peskine8817f612018-12-18 00:18:46 +01002934 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2935 output2, output2_buffer_size,
2936 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002937 TEST_ASSERT( function_output_length <=
2938 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002941 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002942
Gilles Peskine8817f612018-12-18 00:18:46 +01002943 PSA_ASSERT( psa_cipher_update( &operation2,
2944 output1 + first_part_size,
2945 output1_length - first_part_size,
2946 output2, output2_buffer_size,
2947 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002948 TEST_ASSERT( function_output_length <=
2949 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2950 alg,
2951 output1_length - first_part_size ) );
2952 TEST_ASSERT( function_output_length <=
2953 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002954 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002955
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_cipher_finish( &operation2,
2957 output2 + output2_length,
2958 output2_buffer_size - output2_length,
2959 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002960 TEST_ASSERT( function_output_length <=
2961 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2962 TEST_ASSERT( function_output_length <=
2963 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002964 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002965
Gilles Peskine8817f612018-12-18 00:18:46 +01002966 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002967
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002968 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002969
2970exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002971 psa_cipher_abort( &operation1 );
2972 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002973 mbedtls_free( output1 );
2974 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002975 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002976 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002977}
2978/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002979
Gilles Peskine20035e32018-02-03 22:44:14 +01002980/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002981void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002982 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002983 data_t *nonce,
2984 data_t *additional_data,
2985 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002986 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002987{
Ronald Cron5425a212020-08-04 14:58:35 +02002988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002989 psa_key_type_t key_type = key_type_arg;
2990 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01002991 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002992 unsigned char *output_data = NULL;
2993 size_t output_size = 0;
2994 size_t output_length = 0;
2995 unsigned char *output_data2 = NULL;
2996 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01002997 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002998 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003000
Gilles Peskine8817f612018-12-18 00:18:46 +01003001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003002
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003003 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3004 psa_set_key_algorithm( &attributes, alg );
3005 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003006
Gilles Peskine049c7532019-05-15 20:22:09 +02003007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003008 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003009 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3010 key_bits = psa_get_key_bits( &attributes );
3011
3012 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3013 alg );
3014 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3015 * should be exact. */
3016 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3017 expected_result != PSA_ERROR_NOT_SUPPORTED )
3018 {
3019 TEST_EQUAL( output_size,
3020 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3021 TEST_ASSERT( output_size <=
3022 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3023 }
3024 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003025
Steven Cooremanf49478b2021-02-15 15:19:25 +01003026 status = psa_aead_encrypt( key, alg,
3027 nonce->x, nonce->len,
3028 additional_data->x,
3029 additional_data->len,
3030 input_data->x, input_data->len,
3031 output_data, output_size,
3032 &output_length );
3033
3034 /* If the operation is not supported, just skip and not fail in case the
3035 * encryption involves a common limitation of cryptography hardwares and
3036 * an alternative implementation. */
3037 if( status == PSA_ERROR_NOT_SUPPORTED )
3038 {
3039 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3040 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3041 }
3042
3043 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003044
3045 if( PSA_SUCCESS == expected_result )
3046 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003047 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003048
Gilles Peskine003a4a92019-05-14 16:09:40 +02003049 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3050 * should be exact. */
3051 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003052 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003053
gabor-mezei-armceface22021-01-21 12:26:17 +01003054 TEST_ASSERT( input_data->len <=
3055 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3056
Ronald Cron5425a212020-08-04 14:58:35 +02003057 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003058 nonce->x, nonce->len,
3059 additional_data->x,
3060 additional_data->len,
3061 output_data, output_length,
3062 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003063 &output_length2 ),
3064 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003065
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003066 ASSERT_COMPARE( input_data->x, input_data->len,
3067 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003068 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003069
Gilles Peskinea1cac842018-06-11 19:33:02 +02003070exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003071 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003072 mbedtls_free( output_data );
3073 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003074 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003075}
3076/* END_CASE */
3077
3078/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003079void aead_encrypt( int key_type_arg, data_t *key_data,
3080 int alg_arg,
3081 data_t *nonce,
3082 data_t *additional_data,
3083 data_t *input_data,
3084 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003085{
Ronald Cron5425a212020-08-04 14:58:35 +02003086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003087 psa_key_type_t key_type = key_type_arg;
3088 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003089 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003090 unsigned char *output_data = NULL;
3091 size_t output_size = 0;
3092 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003094 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003095
Gilles Peskine8817f612018-12-18 00:18:46 +01003096 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003097
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003098 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3099 psa_set_key_algorithm( &attributes, alg );
3100 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003101
Gilles Peskine049c7532019-05-15 20:22:09 +02003102 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003103 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003104 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3105 key_bits = psa_get_key_bits( &attributes );
3106
3107 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3108 alg );
3109 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3110 * should be exact. */
3111 TEST_EQUAL( output_size,
3112 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3113 TEST_ASSERT( output_size <=
3114 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3115 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003116
Steven Cooremand588ea12021-01-11 19:36:04 +01003117 status = psa_aead_encrypt( key, alg,
3118 nonce->x, nonce->len,
3119 additional_data->x, additional_data->len,
3120 input_data->x, input_data->len,
3121 output_data, output_size,
3122 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003123
Ronald Cron28a45ed2021-02-09 20:35:42 +01003124 /* If the operation is not supported, just skip and not fail in case the
3125 * encryption involves a common limitation of cryptography hardwares and
3126 * an alternative implementation. */
3127 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003128 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003129 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3130 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003131 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003132
3133 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003134 ASSERT_COMPARE( expected_result->x, expected_result->len,
3135 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003136
Gilles Peskinea1cac842018-06-11 19:33:02 +02003137exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003138 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003139 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003140 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003141}
3142/* END_CASE */
3143
3144/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003145void aead_decrypt( int key_type_arg, data_t *key_data,
3146 int alg_arg,
3147 data_t *nonce,
3148 data_t *additional_data,
3149 data_t *input_data,
3150 data_t *expected_data,
3151 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003152{
Ronald Cron5425a212020-08-04 14:58:35 +02003153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003154 psa_key_type_t key_type = key_type_arg;
3155 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003156 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003157 unsigned char *output_data = NULL;
3158 size_t output_size = 0;
3159 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003161 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003162 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003165
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003166 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3167 psa_set_key_algorithm( &attributes, alg );
3168 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003169
Gilles Peskine049c7532019-05-15 20:22:09 +02003170 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003171 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003172 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3173 key_bits = psa_get_key_bits( &attributes );
3174
3175 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3176 alg );
3177 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3178 expected_result != PSA_ERROR_NOT_SUPPORTED )
3179 {
3180 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3181 * should be exact. */
3182 TEST_EQUAL( output_size,
3183 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3184 TEST_ASSERT( output_size <=
3185 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3186 }
3187 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003188
Steven Cooremand588ea12021-01-11 19:36:04 +01003189 status = psa_aead_decrypt( key, alg,
3190 nonce->x, nonce->len,
3191 additional_data->x,
3192 additional_data->len,
3193 input_data->x, input_data->len,
3194 output_data, output_size,
3195 &output_length );
3196
Ronald Cron28a45ed2021-02-09 20:35:42 +01003197 /* If the operation is not supported, just skip and not fail in case the
3198 * decryption involves a common limitation of cryptography hardwares and
3199 * an alternative implementation. */
3200 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003201 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003202 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3203 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003204 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003205
3206 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003207
Gilles Peskine2d277862018-06-18 15:41:12 +02003208 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003209 ASSERT_COMPARE( expected_data->x, expected_data->len,
3210 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003211
Gilles Peskinea1cac842018-06-11 19:33:02 +02003212exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003213 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003214 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003215 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003216}
3217/* END_CASE */
3218
3219/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003220void signature_size( int type_arg,
3221 int bits,
3222 int alg_arg,
3223 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003224{
3225 psa_key_type_t type = type_arg;
3226 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003227 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003228
Gilles Peskinefe11b722018-12-18 00:24:04 +01003229 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003230#if defined(MBEDTLS_TEST_DEPRECATED)
3231 TEST_EQUAL( actual_size,
3232 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3233#endif /* MBEDTLS_TEST_DEPRECATED */
3234
Gilles Peskinee59236f2018-01-27 23:32:46 +01003235exit:
3236 ;
3237}
3238/* END_CASE */
3239
3240/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003241void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3242 int alg_arg, data_t *input_data,
3243 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003244{
Ronald Cron5425a212020-08-04 14:58:35 +02003245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003246 psa_key_type_t key_type = key_type_arg;
3247 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003248 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003249 unsigned char *signature = NULL;
3250 size_t signature_size;
3251 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003253
Gilles Peskine8817f612018-12-18 00:18:46 +01003254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003255
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003256 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003257 psa_set_key_algorithm( &attributes, alg );
3258 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003259
Gilles Peskine049c7532019-05-15 20:22:09 +02003260 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003261 &key ) );
3262 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003263 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003264
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003265 /* Allocate a buffer which has the size advertized by the
3266 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003267 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003268 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003269 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003270 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003271 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003272
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003273 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003274 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003275 input_data->x, input_data->len,
3276 signature, signature_size,
3277 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003278 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003279 ASSERT_COMPARE( output_data->x, output_data->len,
3280 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003281
Gilles Peskine0627f982019-11-26 19:12:16 +01003282#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003283 memset( signature, 0, signature_size );
3284 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003285 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003286 input_data->x, input_data->len,
3287 signature, signature_size,
3288 &signature_length ) );
3289 ASSERT_COMPARE( output_data->x, output_data->len,
3290 signature, signature_length );
3291#endif /* MBEDTLS_TEST_DEPRECATED */
3292
Gilles Peskine20035e32018-02-03 22:44:14 +01003293exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003294 /*
3295 * Key attributes may have been returned by psa_get_key_attributes()
3296 * thus reset them as required.
3297 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003298 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003299
Ronald Cron5425a212020-08-04 14:58:35 +02003300 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003301 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003302 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003303}
3304/* END_CASE */
3305
3306/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003307void sign_hash_fail( int key_type_arg, data_t *key_data,
3308 int alg_arg, data_t *input_data,
3309 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003310{
Ronald Cron5425a212020-08-04 14:58:35 +02003311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003312 psa_key_type_t key_type = key_type_arg;
3313 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003314 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003315 psa_status_t actual_status;
3316 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003317 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003318 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003320
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003321 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003322
Gilles Peskine8817f612018-12-18 00:18:46 +01003323 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003324
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003325 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003326 psa_set_key_algorithm( &attributes, alg );
3327 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003328
Gilles Peskine049c7532019-05-15 20:22:09 +02003329 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003330 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003331
Ronald Cron5425a212020-08-04 14:58:35 +02003332 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003333 input_data->x, input_data->len,
3334 signature, signature_size,
3335 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003336 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003337 /* The value of *signature_length is unspecified on error, but
3338 * whatever it is, it should be less than signature_size, so that
3339 * if the caller tries to read *signature_length bytes without
3340 * checking the error code then they don't overflow a buffer. */
3341 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003342
Gilles Peskine895242b2019-11-29 12:15:40 +01003343#if defined(MBEDTLS_TEST_DEPRECATED)
3344 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003345 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003346 input_data->x, input_data->len,
3347 signature, signature_size,
3348 &signature_length ),
3349 expected_status );
3350 TEST_ASSERT( signature_length <= signature_size );
3351#endif /* MBEDTLS_TEST_DEPRECATED */
3352
Gilles Peskine20035e32018-02-03 22:44:14 +01003353exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003354 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003355 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003356 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003357 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003358}
3359/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003360
3361/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003362void sign_verify_hash( int key_type_arg, data_t *key_data,
3363 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003364{
Ronald Cron5425a212020-08-04 14:58:35 +02003365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003366 psa_key_type_t key_type = key_type_arg;
3367 psa_algorithm_t alg = alg_arg;
3368 size_t key_bits;
3369 unsigned char *signature = NULL;
3370 size_t signature_size;
3371 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003373
Gilles Peskine8817f612018-12-18 00:18:46 +01003374 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003375
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003376 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003377 psa_set_key_algorithm( &attributes, alg );
3378 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003379
Gilles Peskine049c7532019-05-15 20:22:09 +02003380 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003381 &key ) );
3382 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003383 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003384
3385 /* Allocate a buffer which has the size advertized by the
3386 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003387 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003388 key_bits, alg );
3389 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003390 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003391 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003392
3393 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003394 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003395 input_data->x, input_data->len,
3396 signature, signature_size,
3397 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003398 /* Check that the signature length looks sensible. */
3399 TEST_ASSERT( signature_length <= signature_size );
3400 TEST_ASSERT( signature_length > 0 );
3401
3402 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003403 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003404 input_data->x, input_data->len,
3405 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003406
3407 if( input_data->len != 0 )
3408 {
3409 /* Flip a bit in the input and verify that the signature is now
3410 * detected as invalid. Flip a bit at the beginning, not at the end,
3411 * because ECDSA may ignore the last few bits of the input. */
3412 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003413 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003414 input_data->x, input_data->len,
3415 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003416 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003417 }
3418
3419exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003420 /*
3421 * Key attributes may have been returned by psa_get_key_attributes()
3422 * thus reset them as required.
3423 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003424 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003425
Ronald Cron5425a212020-08-04 14:58:35 +02003426 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003427 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003428 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003429}
3430/* END_CASE */
3431
3432/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003433void verify_hash( int key_type_arg, data_t *key_data,
3434 int alg_arg, data_t *hash_data,
3435 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003436{
Ronald Cron5425a212020-08-04 14:58:35 +02003437 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003438 psa_key_type_t key_type = key_type_arg;
3439 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003441
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003442 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003443
Gilles Peskine8817f612018-12-18 00:18:46 +01003444 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003445
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003446 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003447 psa_set_key_algorithm( &attributes, alg );
3448 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003449
Gilles Peskine049c7532019-05-15 20:22:09 +02003450 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003451 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003452
Ronald Cron5425a212020-08-04 14:58:35 +02003453 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003454 hash_data->x, hash_data->len,
3455 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003456
3457#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003458 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003459 hash_data->x, hash_data->len,
3460 signature_data->x,
3461 signature_data->len ) );
3462
3463#endif /* MBEDTLS_TEST_DEPRECATED */
3464
itayzafrir5c753392018-05-08 11:18:38 +03003465exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003466 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003467 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003468 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003469}
3470/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003471
3472/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003473void verify_hash_fail( int key_type_arg, data_t *key_data,
3474 int alg_arg, data_t *hash_data,
3475 data_t *signature_data,
3476 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003477{
Ronald Cron5425a212020-08-04 14:58:35 +02003478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003479 psa_key_type_t key_type = key_type_arg;
3480 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003481 psa_status_t actual_status;
3482 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003484
Gilles Peskine8817f612018-12-18 00:18:46 +01003485 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003486
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003487 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003488 psa_set_key_algorithm( &attributes, alg );
3489 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003490
Gilles Peskine049c7532019-05-15 20:22:09 +02003491 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003492 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003493
Ronald Cron5425a212020-08-04 14:58:35 +02003494 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003495 hash_data->x, hash_data->len,
3496 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003497 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003498
Gilles Peskine895242b2019-11-29 12:15:40 +01003499#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003500 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003501 hash_data->x, hash_data->len,
3502 signature_data->x, signature_data->len ),
3503 expected_status );
3504#endif /* MBEDTLS_TEST_DEPRECATED */
3505
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003506exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003507 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003508 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003509 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003510}
3511/* END_CASE */
3512
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003513/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003514void sign_message_deterministic( int key_type_arg,
3515 data_t *key_data,
3516 int alg_arg,
3517 data_t *input_data,
3518 data_t *output_data )
3519{
3520 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3521 psa_key_type_t key_type = key_type_arg;
3522 psa_algorithm_t alg = alg_arg;
3523 size_t key_bits;
3524 unsigned char *signature = NULL;
3525 size_t signature_size;
3526 size_t signature_length = 0xdeadbeef;
3527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3528
3529 PSA_ASSERT( psa_crypto_init( ) );
3530
3531 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3532 psa_set_key_algorithm( &attributes, alg );
3533 psa_set_key_type( &attributes, key_type );
3534
3535 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3536 &key ) );
3537 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3538 key_bits = psa_get_key_bits( &attributes );
3539
3540 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3541 TEST_ASSERT( signature_size != 0 );
3542 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3543 ASSERT_ALLOC( signature, signature_size );
3544
3545 PSA_ASSERT( psa_sign_message( key, alg,
3546 input_data->x, input_data->len,
3547 signature, signature_size,
3548 &signature_length ) );
3549
3550 ASSERT_COMPARE( output_data->x, output_data->len,
3551 signature, signature_length );
3552
3553exit:
3554 psa_reset_key_attributes( &attributes );
3555
3556 psa_destroy_key( key );
3557 mbedtls_free( signature );
3558 PSA_DONE( );
3559
3560}
3561/* END_CASE */
3562
3563/* BEGIN_CASE */
3564void sign_message_fail( int key_type_arg,
3565 data_t *key_data,
3566 int alg_arg,
3567 data_t *input_data,
3568 int signature_size_arg,
3569 int expected_status_arg )
3570{
3571 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3572 psa_key_type_t key_type = key_type_arg;
3573 psa_algorithm_t alg = alg_arg;
3574 size_t signature_size = signature_size_arg;
3575 psa_status_t actual_status;
3576 psa_status_t expected_status = expected_status_arg;
3577 unsigned char *signature = NULL;
3578 size_t signature_length = 0xdeadbeef;
3579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3580
3581 ASSERT_ALLOC( signature, signature_size );
3582
3583 PSA_ASSERT( psa_crypto_init( ) );
3584
3585 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3586 psa_set_key_algorithm( &attributes, alg );
3587 psa_set_key_type( &attributes, key_type );
3588
3589 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3590 &key ) );
3591
3592 actual_status = psa_sign_message( key, alg,
3593 input_data->x, input_data->len,
3594 signature, signature_size,
3595 &signature_length );
3596 TEST_EQUAL( actual_status, expected_status );
3597 /* The value of *signature_length is unspecified on error, but
3598 * whatever it is, it should be less than signature_size, so that
3599 * if the caller tries to read *signature_length bytes without
3600 * checking the error code then they don't overflow a buffer. */
3601 TEST_ASSERT( signature_length <= signature_size );
3602
3603exit:
3604 psa_reset_key_attributes( &attributes );
3605 psa_destroy_key( key );
3606 mbedtls_free( signature );
3607 PSA_DONE( );
3608}
3609/* END_CASE */
3610
3611/* BEGIN_CASE */
3612void sign_verify_message( int key_type_arg,
3613 data_t *key_data,
3614 int alg_arg,
3615 data_t *input_data )
3616{
3617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3618 psa_key_type_t key_type = key_type_arg;
3619 psa_algorithm_t alg = alg_arg;
3620 size_t key_bits;
3621 unsigned char *signature = NULL;
3622 size_t signature_size;
3623 size_t signature_length = 0xdeadbeef;
3624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3625
3626 PSA_ASSERT( psa_crypto_init( ) );
3627
3628 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3629 PSA_KEY_USAGE_VERIFY_MESSAGE );
3630 psa_set_key_algorithm( &attributes, alg );
3631 psa_set_key_type( &attributes, key_type );
3632
3633 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3634 &key ) );
3635 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3636 key_bits = psa_get_key_bits( &attributes );
3637
3638 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3639 TEST_ASSERT( signature_size != 0 );
3640 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3641 ASSERT_ALLOC( signature, signature_size );
3642
3643 PSA_ASSERT( psa_sign_message( key, alg,
3644 input_data->x, input_data->len,
3645 signature, signature_size,
3646 &signature_length ) );
3647 TEST_ASSERT( signature_length <= signature_size );
3648 TEST_ASSERT( signature_length > 0 );
3649
3650 PSA_ASSERT( psa_verify_message( key, alg,
3651 input_data->x, input_data->len,
3652 signature, signature_length ) );
3653
3654 if( input_data->len != 0 )
3655 {
3656 /* Flip a bit in the input and verify that the signature is now
3657 * detected as invalid. Flip a bit at the beginning, not at the end,
3658 * because ECDSA may ignore the last few bits of the input. */
3659 input_data->x[0] ^= 1;
3660 TEST_EQUAL( psa_verify_message( key, alg,
3661 input_data->x, input_data->len,
3662 signature, signature_length ),
3663 PSA_ERROR_INVALID_SIGNATURE );
3664 }
3665
3666exit:
3667 psa_reset_key_attributes( &attributes );
3668
3669 psa_destroy_key( key );
3670 mbedtls_free( signature );
3671 PSA_DONE( );
3672}
3673/* END_CASE */
3674
3675/* BEGIN_CASE */
3676void verify_message( int key_type_arg,
3677 data_t *key_data,
3678 int alg_arg,
3679 data_t *input_data,
3680 data_t *signature_data )
3681{
3682 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3683 psa_key_type_t key_type = key_type_arg;
3684 psa_algorithm_t alg = alg_arg;
3685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3686
3687 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3688
3689 PSA_ASSERT( psa_crypto_init( ) );
3690
3691 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3692 psa_set_key_algorithm( &attributes, alg );
3693 psa_set_key_type( &attributes, key_type );
3694
3695 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3696 &key ) );
3697
3698 PSA_ASSERT( psa_verify_message( key, alg,
3699 input_data->x, input_data->len,
3700 signature_data->x, signature_data->len ) );
3701
3702exit:
3703 psa_reset_key_attributes( &attributes );
3704 psa_destroy_key( key );
3705 PSA_DONE( );
3706}
3707/* END_CASE */
3708
3709/* BEGIN_CASE */
3710void verify_message_fail( int key_type_arg,
3711 data_t *key_data,
3712 int alg_arg,
3713 data_t *hash_data,
3714 data_t *signature_data,
3715 int expected_status_arg )
3716{
3717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3718 psa_key_type_t key_type = key_type_arg;
3719 psa_algorithm_t alg = alg_arg;
3720 psa_status_t actual_status;
3721 psa_status_t expected_status = expected_status_arg;
3722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3723
3724 PSA_ASSERT( psa_crypto_init( ) );
3725
3726 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3727 psa_set_key_algorithm( &attributes, alg );
3728 psa_set_key_type( &attributes, key_type );
3729
3730 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3731 &key ) );
3732
3733 actual_status = psa_verify_message( key, alg,
3734 hash_data->x, hash_data->len,
3735 signature_data->x,
3736 signature_data->len );
3737 TEST_EQUAL( actual_status, expected_status );
3738
3739exit:
3740 psa_reset_key_attributes( &attributes );
3741 psa_destroy_key( key );
3742 PSA_DONE( );
3743}
3744/* END_CASE */
3745
3746/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003747void asymmetric_encrypt( int key_type_arg,
3748 data_t *key_data,
3749 int alg_arg,
3750 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003751 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003752 int expected_output_length_arg,
3753 int expected_status_arg )
3754{
Ronald Cron5425a212020-08-04 14:58:35 +02003755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003756 psa_key_type_t key_type = key_type_arg;
3757 psa_algorithm_t alg = alg_arg;
3758 size_t expected_output_length = expected_output_length_arg;
3759 size_t key_bits;
3760 unsigned char *output = NULL;
3761 size_t output_size;
3762 size_t output_length = ~0;
3763 psa_status_t actual_status;
3764 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003765 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003766
Gilles Peskine8817f612018-12-18 00:18:46 +01003767 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003768
Gilles Peskine656896e2018-06-29 19:12:28 +02003769 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003770 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3771 psa_set_key_algorithm( &attributes, alg );
3772 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003773 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003774 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003775
3776 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003777 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003778 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003779
Gilles Peskine656896e2018-06-29 19:12:28 +02003780 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003781 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003782 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003783
3784 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003785 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003786 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003787 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003788 output, output_size,
3789 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003790 TEST_EQUAL( actual_status, expected_status );
3791 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003792
Gilles Peskine68428122018-06-30 18:42:41 +02003793 /* If the label is empty, the test framework puts a non-null pointer
3794 * in label->x. Test that a null pointer works as well. */
3795 if( label->len == 0 )
3796 {
3797 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003798 if( output_size != 0 )
3799 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003800 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003801 input_data->x, input_data->len,
3802 NULL, label->len,
3803 output, output_size,
3804 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003805 TEST_EQUAL( actual_status, expected_status );
3806 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003807 }
3808
Gilles Peskine656896e2018-06-29 19:12:28 +02003809exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003810 /*
3811 * Key attributes may have been returned by psa_get_key_attributes()
3812 * thus reset them as required.
3813 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003814 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003815
Ronald Cron5425a212020-08-04 14:58:35 +02003816 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003817 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003818 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003819}
3820/* END_CASE */
3821
3822/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003823void asymmetric_encrypt_decrypt( int key_type_arg,
3824 data_t *key_data,
3825 int alg_arg,
3826 data_t *input_data,
3827 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003828{
Ronald Cron5425a212020-08-04 14:58:35 +02003829 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003830 psa_key_type_t key_type = key_type_arg;
3831 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003832 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003833 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003834 size_t output_size;
3835 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003836 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003837 size_t output2_size;
3838 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003840
Gilles Peskine8817f612018-12-18 00:18:46 +01003841 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003842
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3844 psa_set_key_algorithm( &attributes, alg );
3845 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003846
Gilles Peskine049c7532019-05-15 20:22:09 +02003847 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003848 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003849
3850 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003851 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003852 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003853
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003854 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003855 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003856 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003857
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003858 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003859 TEST_ASSERT( output2_size <=
3860 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3861 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003862 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003863
Gilles Peskineeebd7382018-06-08 18:11:54 +02003864 /* We test encryption by checking that encrypt-then-decrypt gives back
3865 * the original plaintext because of the non-optional random
3866 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003867 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003868 input_data->x, input_data->len,
3869 label->x, label->len,
3870 output, output_size,
3871 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003872 /* We don't know what ciphertext length to expect, but check that
3873 * it looks sensible. */
3874 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003875
Ronald Cron5425a212020-08-04 14:58:35 +02003876 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003877 output, output_length,
3878 label->x, label->len,
3879 output2, output2_size,
3880 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003881 ASSERT_COMPARE( input_data->x, input_data->len,
3882 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883
3884exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003885 /*
3886 * Key attributes may have been returned by psa_get_key_attributes()
3887 * thus reset them as required.
3888 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003889 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003890
Ronald Cron5425a212020-08-04 14:58:35 +02003891 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003892 mbedtls_free( output );
3893 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003894 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895}
3896/* END_CASE */
3897
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003899void asymmetric_decrypt( int key_type_arg,
3900 data_t *key_data,
3901 int alg_arg,
3902 data_t *input_data,
3903 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003904 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003905{
Ronald Cron5425a212020-08-04 14:58:35 +02003906 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003907 psa_key_type_t key_type = key_type_arg;
3908 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003909 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003910 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003911 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003912 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003914
Gilles Peskine8817f612018-12-18 00:18:46 +01003915 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003917 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3918 psa_set_key_algorithm( &attributes, alg );
3919 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003920
Gilles Peskine049c7532019-05-15 20:22:09 +02003921 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003922 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003923
gabor-mezei-armceface22021-01-21 12:26:17 +01003924 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3925 key_bits = psa_get_key_bits( &attributes );
3926
3927 /* Determine the maximum ciphertext length */
3928 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3929 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3930 ASSERT_ALLOC( output, output_size );
3931
Ronald Cron5425a212020-08-04 14:58:35 +02003932 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003933 input_data->x, input_data->len,
3934 label->x, label->len,
3935 output,
3936 output_size,
3937 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003938 ASSERT_COMPARE( expected_data->x, expected_data->len,
3939 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940
Gilles Peskine68428122018-06-30 18:42:41 +02003941 /* If the label is empty, the test framework puts a non-null pointer
3942 * in label->x. Test that a null pointer works as well. */
3943 if( label->len == 0 )
3944 {
3945 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003946 if( output_size != 0 )
3947 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003948 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003949 input_data->x, input_data->len,
3950 NULL, label->len,
3951 output,
3952 output_size,
3953 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003954 ASSERT_COMPARE( expected_data->x, expected_data->len,
3955 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003956 }
3957
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003958exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003959 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003960 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003961 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003962 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963}
3964/* END_CASE */
3965
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003967void asymmetric_decrypt_fail( int key_type_arg,
3968 data_t *key_data,
3969 int alg_arg,
3970 data_t *input_data,
3971 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003972 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003973 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003974{
Ronald Cron5425a212020-08-04 14:58:35 +02003975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003976 psa_key_type_t key_type = key_type_arg;
3977 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003979 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003980 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003981 psa_status_t actual_status;
3982 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003983 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003984
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003985 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003986
Gilles Peskine8817f612018-12-18 00:18:46 +01003987 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003988
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3990 psa_set_key_algorithm( &attributes, alg );
3991 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003992
Gilles Peskine049c7532019-05-15 20:22:09 +02003993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003994 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995
Ronald Cron5425a212020-08-04 14:58:35 +02003996 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003997 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003998 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003999 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004000 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004001 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004002 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003
Gilles Peskine68428122018-06-30 18:42:41 +02004004 /* If the label is empty, the test framework puts a non-null pointer
4005 * in label->x. Test that a null pointer works as well. */
4006 if( label->len == 0 )
4007 {
4008 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004009 if( output_size != 0 )
4010 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004011 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004012 input_data->x, input_data->len,
4013 NULL, label->len,
4014 output, output_size,
4015 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004016 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004017 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004018 }
4019
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004020exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004021 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004022 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004023 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004024 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004025}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004026/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004027
4028/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004029void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004030{
4031 /* Test each valid way of initializing the object, except for `= {0}`, as
4032 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4033 * though it's OK by the C standard. We could test for this, but we'd need
4034 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004035 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004036 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4037 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4038 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004039
4040 memset( &zero, 0, sizeof( zero ) );
4041
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004042 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004043 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004044 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004045 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004046 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004047 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004048 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004049
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004050 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004051 PSA_ASSERT( psa_key_derivation_abort(&func) );
4052 PSA_ASSERT( psa_key_derivation_abort(&init) );
4053 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004054}
4055/* END_CASE */
4056
Janos Follath16de4a42019-06-13 16:32:24 +01004057/* BEGIN_CASE */
4058void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004059{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004060 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004061 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004062 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004063
Gilles Peskine8817f612018-12-18 00:18:46 +01004064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004065
Janos Follath16de4a42019-06-13 16:32:24 +01004066 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004067 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004068
4069exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004070 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004071 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004072}
4073/* END_CASE */
4074
Janos Follathaf3c2a02019-06-12 12:34:34 +01004075/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004076void derive_set_capacity( int alg_arg, int capacity_arg,
4077 int expected_status_arg )
4078{
4079 psa_algorithm_t alg = alg_arg;
4080 size_t capacity = capacity_arg;
4081 psa_status_t expected_status = expected_status_arg;
4082 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4083
4084 PSA_ASSERT( psa_crypto_init( ) );
4085
4086 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4087
4088 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4089 expected_status );
4090
4091exit:
4092 psa_key_derivation_abort( &operation );
4093 PSA_DONE( );
4094}
4095/* END_CASE */
4096
4097/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004098void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004099 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004100 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004101 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004102 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004103 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004104 int expected_status_arg3,
4105 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004106{
4107 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004108 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4109 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004110 psa_status_t expected_statuses[] = {expected_status_arg1,
4111 expected_status_arg2,
4112 expected_status_arg3};
4113 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004114 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4115 MBEDTLS_SVC_KEY_ID_INIT,
4116 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004117 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4119 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004120 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004121 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004122 psa_status_t expected_output_status = expected_output_status_arg;
4123 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004124
4125 PSA_ASSERT( psa_crypto_init( ) );
4126
4127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4128 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004129
4130 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4131
4132 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4133 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004134 mbedtls_test_set_step( i );
4135 if( steps[i] == 0 )
4136 {
4137 /* Skip this step */
4138 }
4139 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004140 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004141 psa_set_key_type( &attributes, key_types[i] );
4142 PSA_ASSERT( psa_import_key( &attributes,
4143 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004144 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004145 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4146 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4147 {
4148 // When taking a private key as secret input, use key agreement
4149 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004150 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4151 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004152 expected_statuses[i] );
4153 }
4154 else
4155 {
4156 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004157 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004158 expected_statuses[i] );
4159 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004160 }
4161 else
4162 {
4163 TEST_EQUAL( psa_key_derivation_input_bytes(
4164 &operation, steps[i],
4165 inputs[i]->x, inputs[i]->len ),
4166 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004167 }
4168 }
4169
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004170 if( output_key_type != PSA_KEY_TYPE_NONE )
4171 {
4172 psa_reset_key_attributes( &attributes );
4173 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4174 psa_set_key_bits( &attributes, 8 );
4175 actual_output_status =
4176 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004177 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004178 }
4179 else
4180 {
4181 uint8_t buffer[1];
4182 actual_output_status =
4183 psa_key_derivation_output_bytes( &operation,
4184 buffer, sizeof( buffer ) );
4185 }
4186 TEST_EQUAL( actual_output_status, expected_output_status );
4187
Janos Follathaf3c2a02019-06-12 12:34:34 +01004188exit:
4189 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004190 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4191 psa_destroy_key( keys[i] );
4192 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004193 PSA_DONE( );
4194}
4195/* END_CASE */
4196
Janos Follathd958bb72019-07-03 15:02:16 +01004197/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004198void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004199{
Janos Follathd958bb72019-07-03 15:02:16 +01004200 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004202 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004203 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004204 unsigned char input1[] = "Input 1";
4205 size_t input1_length = sizeof( input1 );
4206 unsigned char input2[] = "Input 2";
4207 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004208 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004209 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004210 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4211 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4212 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004214
Gilles Peskine8817f612018-12-18 00:18:46 +01004215 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004216
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004217 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4218 psa_set_key_algorithm( &attributes, alg );
4219 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004220
Gilles Peskine73676cb2019-05-15 20:15:10 +02004221 PSA_ASSERT( psa_import_key( &attributes,
4222 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004223 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004224
4225 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004226 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4227 input1, input1_length,
4228 input2, input2_length,
4229 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004230 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004231
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004232 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004233 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004234 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004235
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004236 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004237
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004238 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004239 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004240
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004241exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004242 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004243 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004244 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004245}
4246/* END_CASE */
4247
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004248/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004249void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004250{
4251 uint8_t output_buffer[16];
4252 size_t buffer_size = 16;
4253 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004254 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004255
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004256 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4257 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004258 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004259
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004260 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004261 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004262
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004263 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004264
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004265 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4266 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004267 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004269 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004270 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004271
4272exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004273 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004274}
4275/* END_CASE */
4276
4277/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004278void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004279 int step1_arg, data_t *input1,
4280 int step2_arg, data_t *input2,
4281 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004282 int requested_capacity_arg,
4283 data_t *expected_output1,
4284 data_t *expected_output2 )
4285{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004286 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004287 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4288 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004289 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4290 MBEDTLS_SVC_KEY_ID_INIT,
4291 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004292 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004293 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004294 uint8_t *expected_outputs[2] =
4295 {expected_output1->x, expected_output2->x};
4296 size_t output_sizes[2] =
4297 {expected_output1->len, expected_output2->len};
4298 size_t output_buffer_size = 0;
4299 uint8_t *output_buffer = NULL;
4300 size_t expected_capacity;
4301 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004303 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004304 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004305
4306 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4307 {
4308 if( output_sizes[i] > output_buffer_size )
4309 output_buffer_size = output_sizes[i];
4310 if( output_sizes[i] == 0 )
4311 expected_outputs[i] = NULL;
4312 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004313 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004314 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004315
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004316 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4317 psa_set_key_algorithm( &attributes, alg );
4318 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004319
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004320 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004321 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4322 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4323 requested_capacity ) );
4324 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004325 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004326 switch( steps[i] )
4327 {
4328 case 0:
4329 break;
4330 case PSA_KEY_DERIVATION_INPUT_SECRET:
4331 PSA_ASSERT( psa_import_key( &attributes,
4332 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004333 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004334
4335 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4336 {
4337 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4338 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4339 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4340 }
4341
Gilles Peskine1468da72019-05-29 17:35:49 +02004342 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004343 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004344 break;
4345 default:
4346 PSA_ASSERT( psa_key_derivation_input_bytes(
4347 &operation, steps[i],
4348 inputs[i]->x, inputs[i]->len ) );
4349 break;
4350 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004351 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004352
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004354 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004355 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004356 expected_capacity = requested_capacity;
4357
4358 /* Expansion phase. */
4359 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4360 {
4361 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004362 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004363 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004364 if( expected_capacity == 0 && output_sizes[i] == 0 )
4365 {
4366 /* Reading 0 bytes when 0 bytes are available can go either way. */
4367 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004368 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004369 continue;
4370 }
4371 else if( expected_capacity == 0 ||
4372 output_sizes[i] > expected_capacity )
4373 {
4374 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004375 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004376 expected_capacity = 0;
4377 continue;
4378 }
4379 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004380 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004381 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004382 ASSERT_COMPARE( output_buffer, output_sizes[i],
4383 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004385 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004387 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004388 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004389 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004390 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004391
4392exit:
4393 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004395 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4396 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004397 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004398}
4399/* END_CASE */
4400
4401/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004402void derive_full( int alg_arg,
4403 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004404 data_t *input1,
4405 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004406 int requested_capacity_arg )
4407{
Ronald Cron5425a212020-08-04 14:58:35 +02004408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004409 psa_algorithm_t alg = alg_arg;
4410 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004412 unsigned char output_buffer[16];
4413 size_t expected_capacity = requested_capacity;
4414 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004416
Gilles Peskine8817f612018-12-18 00:18:46 +01004417 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004418
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004419 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4420 psa_set_key_algorithm( &attributes, alg );
4421 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004422
Gilles Peskine049c7532019-05-15 20:22:09 +02004423 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004424 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004425
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004426 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4427 input1->x, input1->len,
4428 input2->x, input2->len,
4429 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004430 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004431
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004433 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004434 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004435
4436 /* Expansion phase. */
4437 while( current_capacity > 0 )
4438 {
4439 size_t read_size = sizeof( output_buffer );
4440 if( read_size > current_capacity )
4441 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004443 output_buffer,
4444 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004445 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004446 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004447 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004448 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004449 }
4450
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 /* Check that the operation refuses to go over capacity. */
4452 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004453 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004454
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004456
4457exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004458 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004459 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004460 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004461}
4462/* END_CASE */
4463
Janos Follathe60c9052019-07-03 13:51:30 +01004464/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004465void derive_key_exercise( int alg_arg,
4466 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004467 data_t *input1,
4468 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004469 int derived_type_arg,
4470 int derived_bits_arg,
4471 int derived_usage_arg,
4472 int derived_alg_arg )
4473{
Ronald Cron5425a212020-08-04 14:58:35 +02004474 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4475 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004476 psa_algorithm_t alg = alg_arg;
4477 psa_key_type_t derived_type = derived_type_arg;
4478 size_t derived_bits = derived_bits_arg;
4479 psa_key_usage_t derived_usage = derived_usage_arg;
4480 psa_algorithm_t derived_alg = derived_alg_arg;
4481 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004484 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004485
Gilles Peskine8817f612018-12-18 00:18:46 +01004486 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004487
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004488 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4489 psa_set_key_algorithm( &attributes, alg );
4490 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004491 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004492 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004493
4494 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004495 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4496 input1->x, input1->len,
4497 input2->x, input2->len,
4498 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004499 goto exit;
4500
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004501 psa_set_key_usage_flags( &attributes, derived_usage );
4502 psa_set_key_algorithm( &attributes, derived_alg );
4503 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004504 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004506 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004507
4508 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004509 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004510 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4511 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004512
4513 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004514 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004515 goto exit;
4516
4517exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004518 /*
4519 * Key attributes may have been returned by psa_get_key_attributes()
4520 * thus reset them as required.
4521 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004522 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004523
4524 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004525 psa_destroy_key( base_key );
4526 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004527 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004528}
4529/* END_CASE */
4530
Janos Follath42fd8882019-07-03 14:17:09 +01004531/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004532void derive_key_export( int alg_arg,
4533 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004534 data_t *input1,
4535 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004536 int bytes1_arg,
4537 int bytes2_arg )
4538{
Ronald Cron5425a212020-08-04 14:58:35 +02004539 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4540 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004541 psa_algorithm_t alg = alg_arg;
4542 size_t bytes1 = bytes1_arg;
4543 size_t bytes2 = bytes2_arg;
4544 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004546 uint8_t *output_buffer = NULL;
4547 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004548 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4549 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004550 size_t length;
4551
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004552 ASSERT_ALLOC( output_buffer, capacity );
4553 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004554 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004555
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004556 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4557 psa_set_key_algorithm( &base_attributes, alg );
4558 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004559 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004560 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004561
4562 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004563 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4564 input1->x, input1->len,
4565 input2->x, input2->len,
4566 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004567 goto exit;
4568
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004569 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004570 output_buffer,
4571 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004572 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004573
4574 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004575 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4576 input1->x, input1->len,
4577 input2->x, input2->len,
4578 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004579 goto exit;
4580
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004581 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4582 psa_set_key_algorithm( &derived_attributes, 0 );
4583 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004584 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004585 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004586 &derived_key ) );
4587 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004588 export_buffer, bytes1,
4589 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004590 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004591 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004592 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004594 &derived_key ) );
4595 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004596 export_buffer + bytes1, bytes2,
4597 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004598 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004599
4600 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004601 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4602 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004603
4604exit:
4605 mbedtls_free( output_buffer );
4606 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004608 psa_destroy_key( base_key );
4609 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004610 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004611}
4612/* END_CASE */
4613
4614/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004615void derive_key( int alg_arg,
4616 data_t *key_data, data_t *input1, data_t *input2,
4617 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004618 int expected_status_arg,
4619 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004620{
Ronald Cron5425a212020-08-04 14:58:35 +02004621 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4622 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004623 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004624 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004625 size_t bits = bits_arg;
4626 psa_status_t expected_status = expected_status_arg;
4627 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4628 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4629 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4630
4631 PSA_ASSERT( psa_crypto_init( ) );
4632
4633 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4634 psa_set_key_algorithm( &base_attributes, alg );
4635 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4636 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004637 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004638
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004639 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4640 input1->x, input1->len,
4641 input2->x, input2->len,
4642 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004643 goto exit;
4644
4645 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4646 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004647 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004648 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004649
4650 psa_status_t status =
4651 psa_key_derivation_output_key( &derived_attributes,
4652 &operation,
4653 &derived_key );
4654 if( is_large_output > 0 )
4655 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4656 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004657
4658exit:
4659 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004660 psa_destroy_key( base_key );
4661 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004662 PSA_DONE( );
4663}
4664/* END_CASE */
4665
4666/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004667void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004668 int our_key_type_arg, int our_key_alg_arg,
4669 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004670 int expected_status_arg )
4671{
Ronald Cron5425a212020-08-04 14:58:35 +02004672 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004673 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004674 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004675 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004676 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004678 psa_status_t expected_status = expected_status_arg;
4679 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004680
Gilles Peskine8817f612018-12-18 00:18:46 +01004681 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004682
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004683 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004684 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004685 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004686 PSA_ASSERT( psa_import_key( &attributes,
4687 our_key_data->x, our_key_data->len,
4688 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004689
Gilles Peskine77f40d82019-04-11 21:27:06 +02004690 /* The tests currently include inputs that should fail at either step.
4691 * Test cases that fail at the setup step should be changed to call
4692 * key_derivation_setup instead, and this function should be renamed
4693 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004694 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004695 if( status == PSA_SUCCESS )
4696 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004697 TEST_EQUAL( psa_key_derivation_key_agreement(
4698 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4699 our_key,
4700 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004701 expected_status );
4702 }
4703 else
4704 {
4705 TEST_ASSERT( status == expected_status );
4706 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004707
4708exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004710 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004711 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004712}
4713/* END_CASE */
4714
4715/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004716void raw_key_agreement( int alg_arg,
4717 int our_key_type_arg, data_t *our_key_data,
4718 data_t *peer_key_data,
4719 data_t *expected_output )
4720{
Ronald Cron5425a212020-08-04 14:58:35 +02004721 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004722 psa_algorithm_t alg = alg_arg;
4723 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004725 unsigned char *output = NULL;
4726 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004727 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004728
4729 ASSERT_ALLOC( output, expected_output->len );
4730 PSA_ASSERT( psa_crypto_init( ) );
4731
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004732 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4733 psa_set_key_algorithm( &attributes, alg );
4734 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004735 PSA_ASSERT( psa_import_key( &attributes,
4736 our_key_data->x, our_key_data->len,
4737 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004738
gabor-mezei-armceface22021-01-21 12:26:17 +01004739 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4740 key_bits = psa_get_key_bits( &attributes );
4741
Gilles Peskinebe697d82019-05-16 18:00:41 +02004742 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4743 peer_key_data->x, peer_key_data->len,
4744 output, expected_output->len,
4745 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004746 ASSERT_COMPARE( output, output_length,
4747 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004748 TEST_ASSERT( output_length <=
4749 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4750 TEST_ASSERT( output_length <=
4751 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004752
4753exit:
4754 mbedtls_free( output );
4755 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004756 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004757}
4758/* END_CASE */
4759
4760/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004761void key_agreement_capacity( int alg_arg,
4762 int our_key_type_arg, data_t *our_key_data,
4763 data_t *peer_key_data,
4764 int expected_capacity_arg )
4765{
Ronald Cron5425a212020-08-04 14:58:35 +02004766 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004767 psa_algorithm_t alg = alg_arg;
4768 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004769 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004771 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004772 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004773
Gilles Peskine8817f612018-12-18 00:18:46 +01004774 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004775
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004776 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4777 psa_set_key_algorithm( &attributes, alg );
4778 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004779 PSA_ASSERT( psa_import_key( &attributes,
4780 our_key_data->x, our_key_data->len,
4781 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004782
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004783 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004784 PSA_ASSERT( psa_key_derivation_key_agreement(
4785 &operation,
4786 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4787 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004788 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4789 {
4790 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004791 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004792 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004793 NULL, 0 ) );
4794 }
Gilles Peskine59685592018-09-18 12:11:34 +02004795
Gilles Peskinebf491972018-10-25 22:36:12 +02004796 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004797 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004798 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004799 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004800
Gilles Peskinebf491972018-10-25 22:36:12 +02004801 /* Test the actual capacity by reading the output. */
4802 while( actual_capacity > sizeof( output ) )
4803 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004804 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004805 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004806 actual_capacity -= sizeof( output );
4807 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004809 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004811 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004812
Gilles Peskine59685592018-09-18 12:11:34 +02004813exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004815 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004816 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004817}
4818/* END_CASE */
4819
4820/* BEGIN_CASE */
4821void key_agreement_output( int alg_arg,
4822 int our_key_type_arg, data_t *our_key_data,
4823 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004824 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004825{
Ronald Cron5425a212020-08-04 14:58:35 +02004826 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004827 psa_algorithm_t alg = alg_arg;
4828 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004831 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004832
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004833 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4834 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004835
Gilles Peskine8817f612018-12-18 00:18:46 +01004836 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004837
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004838 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4839 psa_set_key_algorithm( &attributes, alg );
4840 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004841 PSA_ASSERT( psa_import_key( &attributes,
4842 our_key_data->x, our_key_data->len,
4843 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004844
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004846 PSA_ASSERT( psa_key_derivation_key_agreement(
4847 &operation,
4848 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4849 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004850 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4851 {
4852 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004854 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004855 NULL, 0 ) );
4856 }
Gilles Peskine59685592018-09-18 12:11:34 +02004857
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004859 actual_output,
4860 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004861 ASSERT_COMPARE( actual_output, expected_output1->len,
4862 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004863 if( expected_output2->len != 0 )
4864 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004866 actual_output,
4867 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004868 ASSERT_COMPARE( actual_output, expected_output2->len,
4869 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004870 }
Gilles Peskine59685592018-09-18 12:11:34 +02004871
4872exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004873 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004874 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004875 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004876 mbedtls_free( actual_output );
4877}
4878/* END_CASE */
4879
4880/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004881void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004882{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004883 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004884 unsigned char *output = NULL;
4885 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004886 size_t i;
4887 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004888
Simon Butcher49f8e312020-03-03 15:51:50 +00004889 TEST_ASSERT( bytes_arg >= 0 );
4890
Gilles Peskine91892022021-02-08 19:50:26 +01004891 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004892 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004893
Gilles Peskine8817f612018-12-18 00:18:46 +01004894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004895
Gilles Peskinea50d7392018-06-21 10:22:13 +02004896 /* Run several times, to ensure that every output byte will be
4897 * nonzero at least once with overwhelming probability
4898 * (2^(-8*number_of_runs)). */
4899 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004900 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004901 if( bytes != 0 )
4902 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004903 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004904
Gilles Peskinea50d7392018-06-21 10:22:13 +02004905 for( i = 0; i < bytes; i++ )
4906 {
4907 if( output[i] != 0 )
4908 ++changed[i];
4909 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004910 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004911
4912 /* Check that every byte was changed to nonzero at least once. This
4913 * validates that psa_generate_random is overwriting every byte of
4914 * the output buffer. */
4915 for( i = 0; i < bytes; i++ )
4916 {
4917 TEST_ASSERT( changed[i] != 0 );
4918 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004919
4920exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004921 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004922 mbedtls_free( output );
4923 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004924}
4925/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004926
4927/* BEGIN_CASE */
4928void generate_key( int type_arg,
4929 int bits_arg,
4930 int usage_arg,
4931 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004932 int expected_status_arg,
4933 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004934{
Ronald Cron5425a212020-08-04 14:58:35 +02004935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004936 psa_key_type_t type = type_arg;
4937 psa_key_usage_t usage = usage_arg;
4938 size_t bits = bits_arg;
4939 psa_algorithm_t alg = alg_arg;
4940 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004942 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004943
Gilles Peskine8817f612018-12-18 00:18:46 +01004944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004945
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004946 psa_set_key_usage_flags( &attributes, usage );
4947 psa_set_key_algorithm( &attributes, alg );
4948 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004949 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004950
4951 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004952 psa_status_t status = psa_generate_key( &attributes, &key );
4953
4954 if( is_large_key > 0 )
4955 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4956 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004957 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004958 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004959
4960 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004961 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004962 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4963 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004964
Gilles Peskine818ca122018-06-20 18:16:48 +02004965 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004966 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004967 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004968
4969exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004970 /*
4971 * Key attributes may have been returned by psa_get_key_attributes()
4972 * thus reset them as required.
4973 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004974 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004975
Ronald Cron5425a212020-08-04 14:58:35 +02004976 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004977 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004978}
4979/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004980
Ronald Cronee414c72021-03-18 18:50:08 +01004981/* 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 +02004982void generate_key_rsa( int bits_arg,
4983 data_t *e_arg,
4984 int expected_status_arg )
4985{
Ronald Cron5425a212020-08-04 14:58:35 +02004986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004987 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004988 size_t bits = bits_arg;
4989 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4990 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4991 psa_status_t expected_status = expected_status_arg;
4992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4993 uint8_t *exported = NULL;
4994 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004995 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004996 size_t exported_length = SIZE_MAX;
4997 uint8_t *e_read_buffer = NULL;
4998 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004999 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005000 size_t e_read_length = SIZE_MAX;
5001
5002 if( e_arg->len == 0 ||
5003 ( e_arg->len == 3 &&
5004 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5005 {
5006 is_default_public_exponent = 1;
5007 e_read_size = 0;
5008 }
5009 ASSERT_ALLOC( e_read_buffer, e_read_size );
5010 ASSERT_ALLOC( exported, exported_size );
5011
5012 PSA_ASSERT( psa_crypto_init( ) );
5013
5014 psa_set_key_usage_flags( &attributes, usage );
5015 psa_set_key_algorithm( &attributes, alg );
5016 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5017 e_arg->x, e_arg->len ) );
5018 psa_set_key_bits( &attributes, bits );
5019
5020 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005021 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005022 if( expected_status != PSA_SUCCESS )
5023 goto exit;
5024
5025 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005026 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005027 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5028 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5029 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5030 e_read_buffer, e_read_size,
5031 &e_read_length ) );
5032 if( is_default_public_exponent )
5033 TEST_EQUAL( e_read_length, 0 );
5034 else
5035 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5036
5037 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005038 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005039 goto exit;
5040
5041 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005042 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005043 exported, exported_size,
5044 &exported_length ) );
5045 {
5046 uint8_t *p = exported;
5047 uint8_t *end = exported + exported_length;
5048 size_t len;
5049 /* RSAPublicKey ::= SEQUENCE {
5050 * modulus INTEGER, -- n
5051 * publicExponent INTEGER } -- e
5052 */
5053 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005054 MBEDTLS_ASN1_SEQUENCE |
5055 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005056 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005057 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5058 MBEDTLS_ASN1_INTEGER ) );
5059 if( len >= 1 && p[0] == 0 )
5060 {
5061 ++p;
5062 --len;
5063 }
5064 if( e_arg->len == 0 )
5065 {
5066 TEST_EQUAL( len, 3 );
5067 TEST_EQUAL( p[0], 1 );
5068 TEST_EQUAL( p[1], 0 );
5069 TEST_EQUAL( p[2], 1 );
5070 }
5071 else
5072 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5073 }
5074
5075exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005076 /*
5077 * Key attributes may have been returned by psa_get_key_attributes() or
5078 * set by psa_set_key_domain_parameters() thus reset them as required.
5079 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005080 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005081
Ronald Cron5425a212020-08-04 14:58:35 +02005082 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005083 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005084 mbedtls_free( e_read_buffer );
5085 mbedtls_free( exported );
5086}
5087/* END_CASE */
5088
Darryl Greend49a4992018-06-18 17:27:26 +01005089/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005090void persistent_key_load_key_from_storage( data_t *data,
5091 int type_arg, int bits_arg,
5092 int usage_flags_arg, int alg_arg,
5093 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005094{
Ronald Cron71016a92020-08-28 19:01:50 +02005095 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005097 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5098 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005099 psa_key_type_t type = type_arg;
5100 size_t bits = bits_arg;
5101 psa_key_usage_t usage_flags = usage_flags_arg;
5102 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005104 unsigned char *first_export = NULL;
5105 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005106 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005107 size_t first_exported_length;
5108 size_t second_exported_length;
5109
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005110 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5111 {
5112 ASSERT_ALLOC( first_export, export_size );
5113 ASSERT_ALLOC( second_export, export_size );
5114 }
Darryl Greend49a4992018-06-18 17:27:26 +01005115
Gilles Peskine8817f612018-12-18 00:18:46 +01005116 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005117
Gilles Peskinec87af662019-05-15 16:12:22 +02005118 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005119 psa_set_key_usage_flags( &attributes, usage_flags );
5120 psa_set_key_algorithm( &attributes, alg );
5121 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005122 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005123
Darryl Green0c6575a2018-11-07 16:05:30 +00005124 switch( generation_method )
5125 {
5126 case IMPORT_KEY:
5127 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005128 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005129 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005130 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005131
Darryl Green0c6575a2018-11-07 16:05:30 +00005132 case GENERATE_KEY:
5133 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005134 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005135 break;
5136
5137 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005138#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005139 {
5140 /* Create base key */
5141 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5142 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5143 psa_set_key_usage_flags( &base_attributes,
5144 PSA_KEY_USAGE_DERIVE );
5145 psa_set_key_algorithm( &base_attributes, derive_alg );
5146 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005147 PSA_ASSERT( psa_import_key( &base_attributes,
5148 data->x, data->len,
5149 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005150 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005152 PSA_ASSERT( psa_key_derivation_input_key(
5153 &operation,
5154 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005155 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005157 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005158 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5159 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005160 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005162 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005163 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005164 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005165#else
5166 TEST_ASSUME( ! "KDF not supported in this configuration" );
5167#endif
5168 break;
5169
5170 default:
5171 TEST_ASSERT( ! "generation_method not implemented in test" );
5172 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005173 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005174 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005175
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005176 /* Export the key if permitted by the key policy. */
5177 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5178 {
Ronald Cron5425a212020-08-04 14:58:35 +02005179 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005180 first_export, export_size,
5181 &first_exported_length ) );
5182 if( generation_method == IMPORT_KEY )
5183 ASSERT_COMPARE( data->x, data->len,
5184 first_export, first_exported_length );
5185 }
Darryl Greend49a4992018-06-18 17:27:26 +01005186
5187 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005188 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005189 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005190 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005191
Darryl Greend49a4992018-06-18 17:27:26 +01005192 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005193 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005194 TEST_ASSERT( mbedtls_svc_key_id_equal(
5195 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005196 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5197 PSA_KEY_LIFETIME_PERSISTENT );
5198 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5199 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5200 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5201 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005202
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005203 /* Export the key again if permitted by the key policy. */
5204 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005205 {
Ronald Cron5425a212020-08-04 14:58:35 +02005206 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005207 second_export, export_size,
5208 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005209 ASSERT_COMPARE( first_export, first_exported_length,
5210 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005211 }
5212
5213 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005214 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005215 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005216
5217exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005218 /*
5219 * Key attributes may have been returned by psa_get_key_attributes()
5220 * thus reset them as required.
5221 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005222 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005223
Darryl Greend49a4992018-06-18 17:27:26 +01005224 mbedtls_free( first_export );
5225 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005227 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005228 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005229 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005230}
5231/* END_CASE */