blob: 0f4e3133351d25ecfc29d404d32b839e849e6e0e [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"
Gilles Peskine01bf6312022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02009/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
10 * uses mbedtls_ctr_drbg internally. */
11#include "mbedtls/ctr_drbg.h"
12
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020013#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020014#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015
Gilles Peskine8e94efe2021-02-13 00:25:53 +010016#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010017#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010018#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010019
Gilles Peskinef6279312021-05-27 13:21:20 +020020/* If this comes up, it's a bug in the test code or in the test data. */
21#define UNUSED 0xdeadbeef
22
Dave Rodgman34b147d2021-06-23 12:49:59 +010023/* Assert that an operation is (not) active.
24 * This serves as a proxy for checking if the operation is aborted. */
25#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
26#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
27
Jaeden Amerof24c7f82018-06-27 17:20:43 +010028/** An invalid export length that will never be set by psa_export_key(). */
29static const size_t INVALID_EXPORT_LENGTH = ~0U;
30
Gilles Peskinea7aa4422018-08-14 15:17:54 +020031/** Test if a buffer contains a constant byte value.
32 *
33 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034 *
35 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037 * \param size Size of the buffer in bytes.
38 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 * \return 1 if the buffer is all-bits-zero.
40 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020041 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020042static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043{
44 size_t i;
45 for( i = 0; i < size; i++ )
46 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020048 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020049 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020050 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020051}
Andrzej Kureke0015962022-01-17 15:29:38 +010052#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020053/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
54static int asn1_write_10x( unsigned char **p,
55 unsigned char *start,
56 size_t bits,
57 unsigned char x )
58{
59 int ret;
60 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020061 if( bits == 0 )
62 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
63 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020064 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030065 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020066 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
67 *p -= len;
68 ( *p )[len-1] = x;
69 if( bits % 8 == 0 )
70 ( *p )[1] |= 1;
71 else
72 ( *p )[0] |= 1 << ( bits % 8 );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
74 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
75 MBEDTLS_ASN1_INTEGER ) );
76 return( len );
77}
78
79static int construct_fake_rsa_key( unsigned char *buffer,
80 size_t buffer_size,
81 unsigned char **p,
82 size_t bits,
83 int keypair )
84{
85 size_t half_bits = ( bits + 1 ) / 2;
86 int ret;
87 int len = 0;
88 /* Construct something that looks like a DER encoding of
89 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
90 * RSAPrivateKey ::= SEQUENCE {
91 * version Version,
92 * modulus INTEGER, -- n
93 * publicExponent INTEGER, -- e
94 * privateExponent INTEGER, -- d
95 * prime1 INTEGER, -- p
96 * prime2 INTEGER, -- q
97 * exponent1 INTEGER, -- d mod (p-1)
98 * exponent2 INTEGER, -- d mod (q-1)
99 * coefficient INTEGER, -- (inverse of q) mod p
100 * otherPrimeInfos OtherPrimeInfos OPTIONAL
101 * }
102 * Or, for a public key, the same structure with only
103 * version, modulus and publicExponent.
104 */
105 *p = buffer + buffer_size;
106 if( keypair )
107 {
108 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
111 asn1_write_10x( p, buffer, half_bits, 1 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
113 asn1_write_10x( p, buffer, half_bits, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* q */
115 asn1_write_10x( p, buffer, half_bits, 1 ) );
116 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
117 asn1_write_10x( p, buffer, half_bits, 3 ) );
118 MBEDTLS_ASN1_CHK_ADD( len, /* d */
119 asn1_write_10x( p, buffer, bits, 1 ) );
120 }
121 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
122 asn1_write_10x( p, buffer, 17, 1 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* n */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 if( keypair )
126 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
127 mbedtls_asn1_write_int( p, buffer, 0 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
129 {
130 const unsigned char tag =
131 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
132 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
133 }
134 return( len );
135}
Andrzej Kureke0015962022-01-17 15:29:38 +0100136#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200137
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100138int exercise_mac_setup( psa_key_type_t key_type,
139 const unsigned char *key_bytes,
140 size_t key_length,
141 psa_algorithm_t alg,
142 psa_mac_operation_t *operation,
143 psa_status_t *status )
144{
Ronald Cron5425a212020-08-04 14:58:35 +0200145 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200146 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100147
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100148 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200149 psa_set_key_algorithm( &attributes, alg );
150 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200151 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100154 /* Whether setup succeeded or failed, abort must succeed. */
155 PSA_ASSERT( psa_mac_abort( operation ) );
156 /* If setup failed, reproduce the failure, so that the caller can
157 * test the resulting state of the operation object. */
158 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 {
Ronald Cron5425a212020-08-04 14:58:35 +0200160 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100161 }
162
Ronald Cron5425a212020-08-04 14:58:35 +0200163 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 return( 1 );
165
166exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200167 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100168 return( 0 );
169}
170
171int exercise_cipher_setup( psa_key_type_t key_type,
172 const unsigned char *key_bytes,
173 size_t key_length,
174 psa_algorithm_t alg,
175 psa_cipher_operation_t *operation,
176 psa_status_t *status )
177{
Ronald Cron5425a212020-08-04 14:58:35 +0200178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
182 psa_set_key_algorithm( &attributes, alg );
183 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200184 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Ronald Cron5425a212020-08-04 14:58:35 +0200186 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 /* Whether setup succeeded or failed, abort must succeed. */
188 PSA_ASSERT( psa_cipher_abort( operation ) );
189 /* If setup failed, reproduce the failure, so that the caller can
190 * test the resulting state of the operation object. */
191 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 {
Ronald Cron5425a212020-08-04 14:58:35 +0200193 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100194 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 }
196
Ronald Cron5425a212020-08-04 14:58:35 +0200197 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100198 return( 1 );
199
200exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200201 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100202 return( 0 );
203}
204
Ronald Cron5425a212020-08-04 14:58:35 +0200205static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200206{
207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200208 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200209 uint8_t buffer[1];
210 size_t length;
211 int ok = 0;
212
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
215 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
216 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200217 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000218 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
221 TEST_EQUAL(
222 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200223 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
226 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
227 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
228
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000230 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200231 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000233 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200235 ok = 1;
236
237exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100238 /*
239 * Key attributes may have been returned by psa_get_key_attributes()
240 * thus reset them as required.
241 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200244 return( ok );
245}
246
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200247/* Assert that a key isn't reported as having a slot number. */
248#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
249#define ASSERT_NO_SLOT_NUMBER( attributes ) \
250 do \
251 { \
252 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
253 TEST_EQUAL( psa_get_key_slot_number( \
254 attributes, \
255 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
256 PSA_ERROR_INVALID_ARGUMENT ); \
257 } \
258 while( 0 )
259#else /* MBEDTLS_PSA_CRYPTO_SE_C */
260#define ASSERT_NO_SLOT_NUMBER( attributes ) \
261 ( (void) 0 )
262#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
263
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100264/* An overapproximation of the amount of storage needed for a key of the
265 * given type and with the given content. The API doesn't make it easy
266 * to find a good value for the size. The current implementation doesn't
267 * care about the value anyway. */
268#define KEY_BITS_FROM_DATA( type, data ) \
269 ( data )->len
270
Darryl Green0c6575a2018-11-07 16:05:30 +0000271typedef enum {
272 IMPORT_KEY = 0,
273 GENERATE_KEY = 1,
274 DERIVE_KEY = 2
275} generate_method;
276
Gilles Peskinee59236f2018-01-27 23:32:46 +0100277/* END_HEADER */
278
279/* BEGIN_DEPENDENCIES
280 * depends_on:MBEDTLS_PSA_CRYPTO_C
281 * END_DEPENDENCIES
282 */
283
284/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200285void static_checks( )
286{
287 size_t max_truncated_mac_size =
288 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
289
290 /* Check that the length for a truncated MAC always fits in the algorithm
291 * encoding. The shifted mask is the maximum truncated value. The
292 * untruncated algorithm may be one byte larger. */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +0200293 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100294
295#if defined(MBEDTLS_TEST_DEPRECATED)
296 /* Check deprecated constants. */
297 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
298 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
299 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
300 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
301 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
302 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
303 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
304 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100305
Paul Elliott8ff510a2020-06-02 17:19:28 +0100306 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
324 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
328 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
329 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
330 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
331 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
333 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
334 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
335 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
336
337 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
338 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
339 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
340 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
341 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
342 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
343 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
344 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100345
Paul Elliott75e27032020-06-03 15:17:39 +0100346 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
347 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
348 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
349 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
350 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
351
352 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
353 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100354#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200355}
356/* END_CASE */
357
358/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200359void import_with_policy( int type_arg,
360 int usage_arg, int alg_arg,
361 int expected_status_arg )
362{
363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
364 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200366 psa_key_type_t type = type_arg;
367 psa_key_usage_t usage = usage_arg;
368 psa_algorithm_t alg = alg_arg;
369 psa_status_t expected_status = expected_status_arg;
370 const uint8_t key_material[16] = {0};
371 psa_status_t status;
372
373 PSA_ASSERT( psa_crypto_init( ) );
374
375 psa_set_key_type( &attributes, type );
376 psa_set_key_usage_flags( &attributes, usage );
377 psa_set_key_algorithm( &attributes, alg );
378
379 status = psa_import_key( &attributes,
380 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200381 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200382 TEST_EQUAL( status, expected_status );
383 if( status != PSA_SUCCESS )
384 goto exit;
385
Ronald Cron5425a212020-08-04 14:58:35 +0200386 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200387 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200388 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200389 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200390 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200391 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200392
Ronald Cron5425a212020-08-04 14:58:35 +0200393 PSA_ASSERT( psa_destroy_key( key ) );
394 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200395
396exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100397 /*
398 * Key attributes may have been returned by psa_get_key_attributes()
399 * thus reset them as required.
400 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200401 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100402
403 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200404 PSA_DONE( );
405}
406/* END_CASE */
407
408/* BEGIN_CASE */
409void import_with_data( data_t *data, int type_arg,
410 int attr_bits_arg,
411 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200412{
413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
414 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200415 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200416 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200417 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200418 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100420
Gilles Peskine8817f612018-12-18 00:18:46 +0100421 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100422
Gilles Peskine4747d192019-04-17 15:05:45 +0200423 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200424 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200425
Ronald Cron5425a212020-08-04 14:58:35 +0200426 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100427 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200428 if( status != PSA_SUCCESS )
429 goto exit;
430
Ronald Cron5425a212020-08-04 14:58:35 +0200431 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200432 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200433 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200434 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200435 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200436
Ronald Cron5425a212020-08-04 14:58:35 +0200437 PSA_ASSERT( psa_destroy_key( key ) );
438 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100439
440exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100441 /*
442 * Key attributes may have been returned by psa_get_key_attributes()
443 * thus reset them as required.
444 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200445 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100446
447 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200448 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100449}
450/* END_CASE */
451
452/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200453void import_large_key( int type_arg, int byte_size_arg,
454 int expected_status_arg )
455{
456 psa_key_type_t type = type_arg;
457 size_t byte_size = byte_size_arg;
458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
459 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200460 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200461 psa_status_t status;
462 uint8_t *buffer = NULL;
463 size_t buffer_size = byte_size + 1;
464 size_t n;
465
Steven Cooreman69967ce2021-01-18 18:01:08 +0100466 /* Skip the test case if the target running the test cannot
Shaun Case0e7791f2021-12-20 21:14:10 -0800467 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +0100468 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200469 memset( buffer, 'K', byte_size );
470
471 PSA_ASSERT( psa_crypto_init( ) );
472
473 /* Try importing the key */
474 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
475 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200476 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100477 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200478 TEST_EQUAL( status, expected_status );
479
480 if( status == PSA_SUCCESS )
481 {
Ronald Cron5425a212020-08-04 14:58:35 +0200482 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200483 TEST_EQUAL( psa_get_key_type( &attributes ), type );
484 TEST_EQUAL( psa_get_key_bits( &attributes ),
485 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200486 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200487 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200488 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200489 for( n = 0; n < byte_size; n++ )
490 TEST_EQUAL( buffer[n], 'K' );
491 for( n = byte_size; n < buffer_size; n++ )
492 TEST_EQUAL( buffer[n], 0 );
493 }
494
495exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100496 /*
497 * Key attributes may have been returned by psa_get_key_attributes()
498 * thus reset them as required.
499 */
500 psa_reset_key_attributes( &attributes );
501
Ronald Cron5425a212020-08-04 14:58:35 +0200502 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200503 PSA_DONE( );
504 mbedtls_free( buffer );
505}
506/* END_CASE */
507
Andrzej Kureke0015962022-01-17 15:29:38 +0100508/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
510{
Ronald Cron5425a212020-08-04 14:58:35 +0200511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200512 size_t bits = bits_arg;
513 psa_status_t expected_status = expected_status_arg;
514 psa_status_t status;
515 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200516 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200517 size_t buffer_size = /* Slight overapproximations */
518 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200519 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200520 unsigned char *p;
521 int ret;
522 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200524
Gilles Peskine8817f612018-12-18 00:18:46 +0100525 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200526 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200527
528 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
529 bits, keypair ) ) >= 0 );
530 length = ret;
531
532 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200533 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200534 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100535 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200536
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200537 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200538 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200539
540exit:
541 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200542 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200543}
544/* END_CASE */
545
546/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300547void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300548 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200549 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550 int expected_bits,
551 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200552 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 int canonical_input )
554{
Ronald Cron5425a212020-08-04 14:58:35 +0200555 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200557 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200558 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560 unsigned char *exported = NULL;
561 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100563 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200566 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567
Moran Pekercb088e72018-07-17 17:36:59 +0300568 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200569 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200571 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100572 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
Gilles Peskine4747d192019-04-17 15:05:45 +0200574 psa_set_key_usage_flags( &attributes, usage_arg );
575 psa_set_key_algorithm( &attributes, alg );
576 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700577
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200579 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100580
581 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200582 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200583 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
584 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200585 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586
587 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200588 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100589 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100590
591 /* The exported length must be set by psa_export_key() to a value between 0
592 * and export_size. On errors, the exported length must be 0. */
593 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
594 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +0200595 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100596
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200597 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200598 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100599 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200600 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100601 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200603 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604
Gilles Peskineea38a922021-02-13 00:05:16 +0100605 /* Run sanity checks on the exported key. For non-canonical inputs,
606 * this validates the canonical representations. For canonical inputs,
607 * this doesn't directly validate the implementation, but it still helps
608 * by cross-validating the test data with the sanity check code. */
609 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200610 goto exit;
611
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100612 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200613 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614 else
615 {
Ronald Cron5425a212020-08-04 14:58:35 +0200616 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200617 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200618 &key2 ) );
619 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100620 reexported,
621 export_size,
622 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200623 ASSERT_COMPARE( exported, exported_length,
624 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200625 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100626 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100627 TEST_ASSERT( exported_length <=
628 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
629 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +0200630 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631
632destroy:
633 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200634 PSA_ASSERT( psa_destroy_key( key ) );
635 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636
637exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100638 /*
639 * Key attributes may have been returned by psa_get_key_attributes()
640 * thus reset them as required.
641 */
642 psa_reset_key_attributes( &got_attributes );
643
itayzafrir3e02b3b2018-06-12 17:06:52 +0300644 mbedtls_free( exported );
645 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200646 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647}
648/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100649
Moran Pekerf709f4a2018-06-06 17:26:04 +0300650/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300651void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200652 int type_arg,
653 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100654 int export_size_delta,
655 int expected_export_status_arg,
656 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300657{
Ronald Cron5425a212020-08-04 14:58:35 +0200658 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300659 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200660 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200661 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300663 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100664 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100665 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300667
Gilles Peskine8817f612018-12-18 00:18:46 +0100668 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300669
Gilles Peskine4747d192019-04-17 15:05:45 +0200670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
671 psa_set_key_algorithm( &attributes, alg );
672 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300673
674 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200675 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300676
Gilles Peskine49c25912018-10-29 15:15:31 +0100677 /* Export the public key */
678 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200679 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200680 exported, export_size,
681 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100682 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100683 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100684 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200685 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100686 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200687 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200688 bits = psa_get_key_bits( &attributes );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +0200689 TEST_LE_U( expected_public_key->len,
690 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
691 TEST_LE_U( expected_public_key->len,
692 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
693 TEST_LE_U( expected_public_key->len,
694 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100695 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
696 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100697 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300698
699exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700 /*
701 * Key attributes may have been returned by psa_get_key_attributes()
702 * thus reset them as required.
703 */
704 psa_reset_key_attributes( &attributes );
705
itayzafrir3e02b3b2018-06-12 17:06:52 +0300706 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200707 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200708 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300709}
710/* END_CASE */
711
Gilles Peskine20035e32018-02-03 22:44:14 +0100712/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200713void import_and_exercise_key( data_t *data,
714 int type_arg,
715 int bits_arg,
716 int alg_arg )
717{
Ronald Cron5425a212020-08-04 14:58:35 +0200718 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200719 psa_key_type_t type = type_arg;
720 size_t bits = bits_arg;
721 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100722 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200724 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200725
Gilles Peskine8817f612018-12-18 00:18:46 +0100726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200727
Gilles Peskine4747d192019-04-17 15:05:45 +0200728 psa_set_key_usage_flags( &attributes, usage );
729 psa_set_key_algorithm( &attributes, alg );
730 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200731
732 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200733 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200734
735 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200737 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
738 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200739
740 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100741 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200742 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200743
Ronald Cron5425a212020-08-04 14:58:35 +0200744 PSA_ASSERT( psa_destroy_key( key ) );
745 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200746
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200747exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100748 /*
749 * Key attributes may have been returned by psa_get_key_attributes()
750 * thus reset them as required.
751 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200752 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100753
754 psa_reset_key_attributes( &attributes );
755 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200756 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200757}
758/* END_CASE */
759
760/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100761void effective_key_attributes( int type_arg, int expected_type_arg,
762 int bits_arg, int expected_bits_arg,
763 int usage_arg, int expected_usage_arg,
764 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200765{
Ronald Cron5425a212020-08-04 14:58:35 +0200766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100767 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100768 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100769 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100770 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200771 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100772 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200773 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100774 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200775 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200776
Gilles Peskine8817f612018-12-18 00:18:46 +0100777 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200778
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200779 psa_set_key_usage_flags( &attributes, usage );
780 psa_set_key_algorithm( &attributes, alg );
781 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100782 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200783
Ronald Cron5425a212020-08-04 14:58:35 +0200784 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100785 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200786
Ronald Cron5425a212020-08-04 14:58:35 +0200787 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100788 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
789 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
790 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
791 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200792
793exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100794 /*
795 * Key attributes may have been returned by psa_get_key_attributes()
796 * thus reset them as required.
797 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200798 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100799
800 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200801 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200802}
803/* END_CASE */
804
805/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100806void check_key_policy( int type_arg, int bits_arg,
807 int usage_arg, int alg_arg )
808{
809 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200810 usage_arg,
811 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200812 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100813 goto exit;
814}
815/* END_CASE */
816
817/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200818void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000819{
820 /* Test each valid way of initializing the object, except for `= {0}`, as
821 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
822 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -0800823 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200824 psa_key_attributes_t func = psa_key_attributes_init( );
825 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
826 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000827
828 memset( &zero, 0, sizeof( zero ) );
829
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200830 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
831 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
832 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000833
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200834 TEST_EQUAL( psa_get_key_type( &func ), 0 );
835 TEST_EQUAL( psa_get_key_type( &init ), 0 );
836 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
837
838 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
839 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
840 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
841
842 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
843 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
844 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
845
846 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
847 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
848 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000849}
850/* END_CASE */
851
852/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200853void mac_key_policy( int policy_usage_arg,
854 int policy_alg_arg,
855 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200856 data_t *key_data,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200857 int exercise_alg_arg,
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200858 int expected_status_sign_arg,
859 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200860{
Ronald Cron5425a212020-08-04 14:58:35 +0200861 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200862 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000863 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200864 psa_key_type_t key_type = key_type_arg;
865 psa_algorithm_t policy_alg = policy_alg_arg;
866 psa_algorithm_t exercise_alg = exercise_alg_arg;
867 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200868 psa_status_t status;
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200869 psa_status_t expected_status_sign = expected_status_sign_arg;
870 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200871 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200872
Gilles Peskine8817f612018-12-18 00:18:46 +0100873 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200874
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200875 psa_set_key_usage_flags( &attributes, policy_usage );
876 psa_set_key_algorithm( &attributes, policy_alg );
877 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200878
Gilles Peskine049c7532019-05-15 20:22:09 +0200879 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200880 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200881
gabor-mezei-arm659af9e2021-06-29 11:06:16 +0200882 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
883 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200884
Ronald Cron5425a212020-08-04 14:58:35 +0200885 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200886 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100887
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200888 /* Calculate the MAC, one-shot case. */
889 uint8_t input[128] = {0};
890 size_t mac_len;
891 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
892 input, 128,
893 mac, PSA_MAC_MAX_SIZE, &mac_len ),
894 expected_status_sign );
895
896 /* Verify correct MAC, one-shot case. */
897 status = psa_mac_verify( key, exercise_alg, input, 128,
898 mac, mac_len );
899
900 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
901 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
902 else
903 TEST_EQUAL( status, expected_status_verify );
904
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200906
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200907 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200908 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200909 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910
911exit:
912 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200913 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200914 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915}
916/* END_CASE */
917
918/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200919void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200920 int policy_alg,
921 int key_type,
922 data_t *key_data,
923 int exercise_alg )
924{
Ronald Cron5425a212020-08-04 14:58:35 +0200925 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000927 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200928 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200929 psa_status_t status;
930
Gilles Peskine8817f612018-12-18 00:18:46 +0100931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200932
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200933 psa_set_key_usage_flags( &attributes, policy_usage );
934 psa_set_key_algorithm( &attributes, policy_alg );
935 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200936
Gilles Peskine049c7532019-05-15 20:22:09 +0200937 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200938 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200939
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200940 /* Check if no key usage flag implication is done */
941 TEST_EQUAL( policy_usage,
942 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200943
Ronald Cron5425a212020-08-04 14:58:35 +0200944 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945 if( policy_alg == exercise_alg &&
946 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100947 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100949 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200950 psa_cipher_abort( &operation );
951
Ronald Cron5425a212020-08-04 14:58:35 +0200952 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200953 if( policy_alg == exercise_alg &&
954 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100955 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100957 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200958
959exit:
960 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200961 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200962 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200963}
964/* END_CASE */
965
966/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200967void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 int policy_alg,
969 int key_type,
970 data_t *key_data,
971 int nonce_length_arg,
972 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100973 int exercise_alg,
974 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200975{
Ronald Cron5425a212020-08-04 14:58:35 +0200976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200977 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200978 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100980 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981 unsigned char nonce[16] = {0};
982 size_t nonce_length = nonce_length_arg;
983 unsigned char tag[16];
984 size_t tag_length = tag_length_arg;
985 size_t output_length;
986
Gilles Peskine47cfdfd2022-04-14 00:12:57 +0200987 TEST_LE_U( nonce_length, sizeof( nonce ) );
988 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989
Gilles Peskine8817f612018-12-18 00:18:46 +0100990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200991
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200992 psa_set_key_usage_flags( &attributes, policy_usage );
993 psa_set_key_algorithm( &attributes, policy_alg );
994 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200995
Gilles Peskine049c7532019-05-15 20:22:09 +0200996 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200997 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200998
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200999 /* Check if no key usage implication is done */
1000 TEST_EQUAL( policy_usage,
1001 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001002
Ronald Cron5425a212020-08-04 14:58:35 +02001003 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001004 nonce, nonce_length,
1005 NULL, 0,
1006 NULL, 0,
1007 tag, tag_length,
1008 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001009 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1010 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001012 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001013
1014 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001015 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016 nonce, nonce_length,
1017 NULL, 0,
1018 tag, tag_length,
1019 NULL, 0,
1020 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001021 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1022 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1023 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001024 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001026 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001027
1028exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001029 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001030 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001031}
1032/* END_CASE */
1033
1034/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001035void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001036 int policy_alg,
1037 int key_type,
1038 data_t *key_data,
1039 int exercise_alg )
1040{
Ronald Cron5425a212020-08-04 14:58:35 +02001041 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001042 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001043 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044 psa_status_t status;
1045 size_t key_bits;
1046 size_t buffer_length;
1047 unsigned char *buffer = NULL;
1048 size_t output_length;
1049
Gilles Peskine8817f612018-12-18 00:18:46 +01001050 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001051
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001052 psa_set_key_usage_flags( &attributes, policy_usage );
1053 psa_set_key_algorithm( &attributes, policy_alg );
1054 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001055
Gilles Peskine049c7532019-05-15 20:22:09 +02001056 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001057 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001058
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001059 /* Check if no key usage implication is done */
1060 TEST_EQUAL( policy_usage,
1061 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001062
Ronald Cron5425a212020-08-04 14:58:35 +02001063 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001064 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001065 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1066 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001067 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068
Ronald Cron5425a212020-08-04 14:58:35 +02001069 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001070 NULL, 0,
1071 NULL, 0,
1072 buffer, buffer_length,
1073 &output_length );
1074 if( policy_alg == exercise_alg &&
1075 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001076 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001078 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001079
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001080 if( buffer_length != 0 )
1081 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001082 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083 buffer, buffer_length,
1084 NULL, 0,
1085 buffer, buffer_length,
1086 &output_length );
1087 if( policy_alg == exercise_alg &&
1088 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001089 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001090 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001091 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001092
1093exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001094 /*
1095 * Key attributes may have been returned by psa_get_key_attributes()
1096 * thus reset them as required.
1097 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001098 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001099
1100 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001101 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001102 mbedtls_free( buffer );
1103}
1104/* END_CASE */
1105
1106/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001107void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001108 int policy_alg,
1109 int key_type,
1110 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001111 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001112 int payload_length_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001113 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001114{
Ronald Cron5425a212020-08-04 14:58:35 +02001115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001117 psa_key_usage_t policy_usage = policy_usage_arg;
1118 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001119 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001120 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1121 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1122 * compatible with the policy and `payload_length_arg` is supposed to be
1123 * a valid input length to sign. If `payload_length_arg <= 0`,
1124 * `exercise_alg` is supposed to be forbidden by the policy. */
1125 int compatible_alg = payload_length_arg > 0;
1126 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001127 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001128 size_t signature_length;
1129
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001130 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001131 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001132 TEST_EQUAL( expected_usage,
1133 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001134
Gilles Peskine8817f612018-12-18 00:18:46 +01001135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001137 psa_set_key_usage_flags( &attributes, policy_usage );
1138 psa_set_key_algorithm( &attributes, policy_alg );
1139 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001140
Gilles Peskine049c7532019-05-15 20:22:09 +02001141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001142 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001143
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001144 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1145
Ronald Cron5425a212020-08-04 14:58:35 +02001146 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001147 payload, payload_length,
1148 signature, sizeof( signature ),
1149 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001150 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001151 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001153 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001154
1155 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001156 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001157 payload, payload_length,
1158 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001159 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001160 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001161 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001162 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001163
Gilles Peskine8cb22c82021-09-22 16:15:05 +02001164 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-arm79df41d2021-06-28 14:53:49 +02001165 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001166 {
1167 status = psa_sign_message( key, exercise_alg,
1168 payload, payload_length,
1169 signature, sizeof( signature ),
1170 &signature_length );
1171 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1172 PSA_ASSERT( status );
1173 else
1174 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1175
1176 memset( signature, 0, sizeof( signature ) );
1177 status = psa_verify_message( key, exercise_alg,
1178 payload, payload_length,
1179 signature, sizeof( signature ) );
1180 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1181 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1182 else
1183 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1184 }
1185
Gilles Peskined5b33222018-06-18 22:20:03 +02001186exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001187 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001188 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001189}
1190/* END_CASE */
1191
Janos Follathba3fab92019-06-11 14:50:16 +01001192/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001193void derive_key_policy( int policy_usage,
1194 int policy_alg,
1195 int key_type,
1196 data_t *key_data,
1197 int exercise_alg )
1198{
Ronald Cron5425a212020-08-04 14:58:35 +02001199 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001201 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001202 psa_status_t status;
1203
Gilles Peskine8817f612018-12-18 00:18:46 +01001204 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001205
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001206 psa_set_key_usage_flags( &attributes, policy_usage );
1207 psa_set_key_algorithm( &attributes, policy_alg );
1208 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001209
Gilles Peskine049c7532019-05-15 20:22:09 +02001210 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001211 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001212
Janos Follathba3fab92019-06-11 14:50:16 +01001213 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1214
1215 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1216 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001217 {
Janos Follathba3fab92019-06-11 14:50:16 +01001218 PSA_ASSERT( psa_key_derivation_input_bytes(
1219 &operation,
1220 PSA_KEY_DERIVATION_INPUT_SEED,
1221 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001222 }
Janos Follathba3fab92019-06-11 14:50:16 +01001223
1224 status = psa_key_derivation_input_key( &operation,
1225 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001226 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001227
Gilles Peskineea0fb492018-07-12 17:17:20 +02001228 if( policy_alg == exercise_alg &&
1229 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001230 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001231 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001232 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001233
1234exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001235 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001236 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001237 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001238}
1239/* END_CASE */
1240
1241/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001242void agreement_key_policy( int policy_usage,
1243 int policy_alg,
1244 int key_type_arg,
1245 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001246 int exercise_alg,
1247 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001248{
Ronald Cron5425a212020-08-04 14:58:35 +02001249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001251 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001252 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001253 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001254 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001255
Gilles Peskine8817f612018-12-18 00:18:46 +01001256 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001257
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001258 psa_set_key_usage_flags( &attributes, policy_usage );
1259 psa_set_key_algorithm( &attributes, policy_alg );
1260 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001261
Gilles Peskine049c7532019-05-15 20:22:09 +02001262 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001263 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001264
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001265 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001266 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001267
Steven Cooremance48e852020-10-05 16:02:45 +02001268 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001269
1270exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001271 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001272 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001273 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001274}
1275/* END_CASE */
1276
1277/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001278void key_policy_alg2( int key_type_arg, data_t *key_data,
1279 int usage_arg, int alg_arg, int alg2_arg )
1280{
Ronald Cron5425a212020-08-04 14:58:35 +02001281 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001282 psa_key_type_t key_type = key_type_arg;
1283 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1284 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1285 psa_key_usage_t usage = usage_arg;
1286 psa_algorithm_t alg = alg_arg;
1287 psa_algorithm_t alg2 = alg2_arg;
1288
1289 PSA_ASSERT( psa_crypto_init( ) );
1290
1291 psa_set_key_usage_flags( &attributes, usage );
1292 psa_set_key_algorithm( &attributes, alg );
1293 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1294 psa_set_key_type( &attributes, key_type );
1295 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001296 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001297
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001298 /* Update the usage flags to obtain implicit usage flags */
1299 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001300 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001301 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1302 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1303 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1304
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001305 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001306 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001307 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001308 goto exit;
1309
1310exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001311 /*
1312 * Key attributes may have been returned by psa_get_key_attributes()
1313 * thus reset them as required.
1314 */
1315 psa_reset_key_attributes( &got_attributes );
1316
Ronald Cron5425a212020-08-04 14:58:35 +02001317 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001318 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001319}
1320/* END_CASE */
1321
1322/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001323void raw_agreement_key_policy( int policy_usage,
1324 int policy_alg,
1325 int key_type_arg,
1326 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001327 int exercise_alg,
1328 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001329{
Ronald Cron5425a212020-08-04 14:58:35 +02001330 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001332 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001333 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001334 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001335 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001336
1337 PSA_ASSERT( psa_crypto_init( ) );
1338
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001339 psa_set_key_usage_flags( &attributes, policy_usage );
1340 psa_set_key_algorithm( &attributes, policy_alg );
1341 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001342
Gilles Peskine049c7532019-05-15 20:22:09 +02001343 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001344 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001345
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001346 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001347
Steven Cooremance48e852020-10-05 16:02:45 +02001348 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001349
1350exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001351 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001352 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001353 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001354}
1355/* END_CASE */
1356
1357/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001358void copy_success( int source_usage_arg,
1359 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001360 int type_arg, data_t *material,
1361 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001362 int target_usage_arg,
1363 int target_alg_arg, int target_alg2_arg,
1364 int expected_usage_arg,
1365 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001366{
Gilles Peskineca25db92019-04-19 11:43:08 +02001367 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1368 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001369 psa_key_usage_t expected_usage = expected_usage_arg;
1370 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001371 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001372 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1373 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001374 uint8_t *export_buffer = NULL;
1375
Gilles Peskine57ab7212019-01-28 13:03:09 +01001376 PSA_ASSERT( psa_crypto_init( ) );
1377
Gilles Peskineca25db92019-04-19 11:43:08 +02001378 /* Prepare the source key. */
1379 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1380 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001381 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001382 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001383 PSA_ASSERT( psa_import_key( &source_attributes,
1384 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001385 &source_key ) );
1386 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001387
Gilles Peskineca25db92019-04-19 11:43:08 +02001388 /* Prepare the target attributes. */
1389 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001390 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001391 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001392 /* Set volatile lifetime to reset the key identifier to 0. */
1393 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1394 }
1395
Gilles Peskineca25db92019-04-19 11:43:08 +02001396 if( target_usage_arg != -1 )
1397 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1398 if( target_alg_arg != -1 )
1399 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001400 if( target_alg2_arg != -1 )
1401 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001402
1403 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001404 PSA_ASSERT( psa_copy_key( source_key,
1405 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001406
1407 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001408 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001409
1410 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001411 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001412 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1413 psa_get_key_type( &target_attributes ) );
1414 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1415 psa_get_key_bits( &target_attributes ) );
1416 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1417 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001418 TEST_EQUAL( expected_alg2,
1419 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001420 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1421 {
1422 size_t length;
1423 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001424 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001425 material->len, &length ) );
1426 ASSERT_COMPARE( material->x, material->len,
1427 export_buffer, length );
1428 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001429
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001430 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001431 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001432 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001433 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001434
Ronald Cron5425a212020-08-04 14:58:35 +02001435 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001436
1437exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001438 /*
1439 * Source and target key attributes may have been returned by
1440 * psa_get_key_attributes() thus reset them as required.
1441 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001442 psa_reset_key_attributes( &source_attributes );
1443 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001444
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001445 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001446 mbedtls_free( export_buffer );
1447}
1448/* END_CASE */
1449
1450/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001451void copy_fail( int source_usage_arg,
1452 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001453 int type_arg, data_t *material,
1454 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001455 int target_usage_arg,
1456 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001457 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001458 int expected_status_arg )
1459{
1460 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1461 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001462 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1463 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001464 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001465
1466 PSA_ASSERT( psa_crypto_init( ) );
1467
1468 /* Prepare the source key. */
1469 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1470 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001471 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001472 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001473 PSA_ASSERT( psa_import_key( &source_attributes,
1474 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001475 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001476
1477 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001478 psa_set_key_id( &target_attributes, key_id );
1479 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001480 psa_set_key_type( &target_attributes, target_type_arg );
1481 psa_set_key_bits( &target_attributes, target_bits_arg );
1482 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1483 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001484 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001485
1486 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001487 TEST_EQUAL( psa_copy_key( source_key,
1488 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001489 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001490
Ronald Cron5425a212020-08-04 14:58:35 +02001491 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001492
Gilles Peskine4a644642019-05-03 17:14:08 +02001493exit:
1494 psa_reset_key_attributes( &source_attributes );
1495 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001496 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001497}
1498/* END_CASE */
1499
1500/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001501void hash_operation_init( )
1502{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001503 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001504 /* Test each valid way of initializing the object, except for `= {0}`, as
1505 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1506 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001507 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001508 psa_hash_operation_t func = psa_hash_operation_init( );
1509 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1510 psa_hash_operation_t zero;
1511
1512 memset( &zero, 0, sizeof( zero ) );
1513
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001514 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001515 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1516 PSA_ERROR_BAD_STATE );
1517 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1518 PSA_ERROR_BAD_STATE );
1519 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1520 PSA_ERROR_BAD_STATE );
1521
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001522 /* A default hash operation should be abortable without error. */
1523 PSA_ASSERT( psa_hash_abort( &func ) );
1524 PSA_ASSERT( psa_hash_abort( &init ) );
1525 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001530void hash_setup( int alg_arg,
1531 int expected_status_arg )
1532{
1533 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001534 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001535 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001536 psa_status_t status;
1537
Gilles Peskine8817f612018-12-18 00:18:46 +01001538 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001539
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001540 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001541 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001542
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001543 /* Whether setup succeeded or failed, abort must succeed. */
1544 PSA_ASSERT( psa_hash_abort( &operation ) );
1545
1546 /* If setup failed, reproduce the failure, so as to
1547 * test the resulting state of the operation object. */
1548 if( status != PSA_SUCCESS )
1549 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1550
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001551 /* Now the operation object should be reusable. */
1552#if defined(KNOWN_SUPPORTED_HASH_ALG)
1553 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1554 PSA_ASSERT( psa_hash_abort( &operation ) );
1555#endif
1556
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001557exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001558 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001559}
1560/* END_CASE */
1561
1562/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001563void hash_compute_fail( int alg_arg, data_t *input,
1564 int output_size_arg, int expected_status_arg )
1565{
1566 psa_algorithm_t alg = alg_arg;
1567 uint8_t *output = NULL;
1568 size_t output_size = output_size_arg;
1569 size_t output_length = INVALID_EXPORT_LENGTH;
1570 psa_status_t expected_status = expected_status_arg;
1571 psa_status_t status;
1572
1573 ASSERT_ALLOC( output, output_size );
1574
1575 PSA_ASSERT( psa_crypto_init( ) );
1576
1577 status = psa_hash_compute( alg, input->x, input->len,
1578 output, output_size, &output_length );
1579 TEST_EQUAL( status, expected_status );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02001580 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001581
1582exit:
1583 mbedtls_free( output );
1584 PSA_DONE( );
1585}
1586/* END_CASE */
1587
1588/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001589void hash_compare_fail( int alg_arg, data_t *input,
1590 data_t *reference_hash,
1591 int expected_status_arg )
1592{
1593 psa_algorithm_t alg = alg_arg;
1594 psa_status_t expected_status = expected_status_arg;
1595 psa_status_t status;
1596
1597 PSA_ASSERT( psa_crypto_init( ) );
1598
1599 status = psa_hash_compare( alg, input->x, input->len,
1600 reference_hash->x, reference_hash->len );
1601 TEST_EQUAL( status, expected_status );
1602
1603exit:
1604 PSA_DONE( );
1605}
1606/* END_CASE */
1607
1608/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001609void hash_compute_compare( int alg_arg, data_t *input,
1610 data_t *expected_output )
1611{
1612 psa_algorithm_t alg = alg_arg;
1613 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1614 size_t output_length = INVALID_EXPORT_LENGTH;
1615 size_t i;
1616
1617 PSA_ASSERT( psa_crypto_init( ) );
1618
1619 /* Compute with tight buffer */
1620 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001621 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001622 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001623 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001624 ASSERT_COMPARE( output, output_length,
1625 expected_output->x, expected_output->len );
1626
1627 /* Compute with larger buffer */
1628 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1629 output, sizeof( output ),
1630 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001631 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001632 ASSERT_COMPARE( output, output_length,
1633 expected_output->x, expected_output->len );
1634
1635 /* Compare with correct hash */
1636 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1637 output, output_length ) );
1638
1639 /* Compare with trailing garbage */
1640 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1641 output, output_length + 1 ),
1642 PSA_ERROR_INVALID_SIGNATURE );
1643
1644 /* Compare with truncated hash */
1645 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1646 output, output_length - 1 ),
1647 PSA_ERROR_INVALID_SIGNATURE );
1648
1649 /* Compare with corrupted value */
1650 for( i = 0; i < output_length; i++ )
1651 {
Chris Jones9634bb12021-01-20 15:56:42 +00001652 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001653 output[i] ^= 1;
1654 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1655 output, output_length ),
1656 PSA_ERROR_INVALID_SIGNATURE );
1657 output[i] ^= 1;
1658 }
1659
1660exit:
1661 PSA_DONE( );
1662}
1663/* END_CASE */
1664
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001665/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001666void hash_bad_order( )
1667{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001668 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001669 unsigned char input[] = "";
1670 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001671 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001672 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1673 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1674 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001675 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001676 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001677 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001678
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001680
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001681 /* Call setup twice in a row. */
1682 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001683 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001684 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1685 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001686 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001687 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001688 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001689
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001690 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001691 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001692 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001693 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001694
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001695 /* Check that update calls abort on error. */
1696 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman54f73512021-06-24 18:14:52 +01001697 operation.id = UINT_MAX;
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001698 ASSERT_OPERATION_IS_ACTIVE( operation );
1699 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1700 PSA_ERROR_BAD_STATE );
1701 ASSERT_OPERATION_IS_INACTIVE( operation );
1702 PSA_ASSERT( psa_hash_abort( &operation ) );
1703 ASSERT_OPERATION_IS_INACTIVE( operation );
1704
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001705 /* Call update after finish. */
1706 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1707 PSA_ASSERT( psa_hash_finish( &operation,
1708 hash, sizeof( hash ), &hash_len ) );
1709 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001710 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001711 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001712
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001713 /* Call verify without calling setup beforehand. */
1714 TEST_EQUAL( psa_hash_verify( &operation,
1715 valid_hash, sizeof( valid_hash ) ),
1716 PSA_ERROR_BAD_STATE );
1717 PSA_ASSERT( psa_hash_abort( &operation ) );
1718
1719 /* Call verify after finish. */
1720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1721 PSA_ASSERT( psa_hash_finish( &operation,
1722 hash, sizeof( hash ), &hash_len ) );
1723 TEST_EQUAL( psa_hash_verify( &operation,
1724 valid_hash, sizeof( valid_hash ) ),
1725 PSA_ERROR_BAD_STATE );
1726 PSA_ASSERT( psa_hash_abort( &operation ) );
1727
1728 /* Call verify twice in a row. */
1729 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001730 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001731 PSA_ASSERT( psa_hash_verify( &operation,
1732 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001733 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001734 TEST_EQUAL( psa_hash_verify( &operation,
1735 valid_hash, sizeof( valid_hash ) ),
1736 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001737 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001738 PSA_ASSERT( psa_hash_abort( &operation ) );
1739
1740 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001741 TEST_EQUAL( psa_hash_finish( &operation,
1742 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001743 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001744 PSA_ASSERT( psa_hash_abort( &operation ) );
1745
1746 /* Call finish twice in a row. */
1747 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1748 PSA_ASSERT( psa_hash_finish( &operation,
1749 hash, sizeof( hash ), &hash_len ) );
1750 TEST_EQUAL( psa_hash_finish( &operation,
1751 hash, sizeof( hash ), &hash_len ),
1752 PSA_ERROR_BAD_STATE );
1753 PSA_ASSERT( psa_hash_abort( &operation ) );
1754
1755 /* Call finish after calling verify. */
1756 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1757 PSA_ASSERT( psa_hash_verify( &operation,
1758 valid_hash, sizeof( valid_hash ) ) );
1759 TEST_EQUAL( psa_hash_finish( &operation,
1760 hash, sizeof( hash ), &hash_len ),
1761 PSA_ERROR_BAD_STATE );
1762 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001763
1764exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001765 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001766}
1767/* END_CASE */
1768
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001769/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001770void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001771{
1772 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001773 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1774 * appended to it */
1775 unsigned char hash[] = {
1776 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1777 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1778 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001779 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001780 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001781
Gilles Peskine8817f612018-12-18 00:18:46 +01001782 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001783
itayzafrir27e69452018-11-01 14:26:34 +02001784 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001785 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001786 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001787 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001788 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001789 ASSERT_OPERATION_IS_INACTIVE( operation );
1790 PSA_ASSERT( psa_hash_abort( &operation ) );
1791 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001792
itayzafrir27e69452018-11-01 14:26:34 +02001793 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001794 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001795 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001796 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001797
itayzafrir27e69452018-11-01 14:26:34 +02001798 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001799 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001800 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001801 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001802
itayzafrirec93d302018-10-18 18:01:10 +03001803exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001804 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001805}
1806/* END_CASE */
1807
Ronald Cronee414c72021-03-18 18:50:08 +01001808/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001809void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001810{
1811 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001812 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001813 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001814 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001815 size_t hash_len;
1816
Gilles Peskine8817f612018-12-18 00:18:46 +01001817 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001818
itayzafrir58028322018-10-25 10:22:01 +03001819 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001820 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001821 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001822 hash, expected_size - 1, &hash_len ),
1823 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001824
1825exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001826 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001827}
1828/* END_CASE */
1829
Ronald Cronee414c72021-03-18 18:50:08 +01001830/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001831void hash_clone_source_state( )
1832{
1833 psa_algorithm_t alg = PSA_ALG_SHA_256;
1834 unsigned char hash[PSA_HASH_MAX_SIZE];
1835 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1836 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1837 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1838 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1839 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1840 size_t hash_len;
1841
1842 PSA_ASSERT( psa_crypto_init( ) );
1843 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1844
1845 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1846 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1847 PSA_ASSERT( psa_hash_finish( &op_finished,
1848 hash, sizeof( hash ), &hash_len ) );
1849 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1850 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1851
1852 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1853 PSA_ERROR_BAD_STATE );
1854
1855 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1856 PSA_ASSERT( psa_hash_finish( &op_init,
1857 hash, sizeof( hash ), &hash_len ) );
1858 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1859 PSA_ASSERT( psa_hash_finish( &op_finished,
1860 hash, sizeof( hash ), &hash_len ) );
1861 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1862 PSA_ASSERT( psa_hash_finish( &op_aborted,
1863 hash, sizeof( hash ), &hash_len ) );
1864
1865exit:
1866 psa_hash_abort( &op_source );
1867 psa_hash_abort( &op_init );
1868 psa_hash_abort( &op_setup );
1869 psa_hash_abort( &op_finished );
1870 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001871 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001872}
1873/* END_CASE */
1874
Ronald Cronee414c72021-03-18 18:50:08 +01001875/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001876void hash_clone_target_state( )
1877{
1878 psa_algorithm_t alg = PSA_ALG_SHA_256;
1879 unsigned char hash[PSA_HASH_MAX_SIZE];
1880 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1881 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1882 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1883 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1884 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1885 size_t hash_len;
1886
1887 PSA_ASSERT( psa_crypto_init( ) );
1888
1889 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1890 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1891 PSA_ASSERT( psa_hash_finish( &op_finished,
1892 hash, sizeof( hash ), &hash_len ) );
1893 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1894 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1895
1896 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1897 PSA_ASSERT( psa_hash_finish( &op_target,
1898 hash, sizeof( hash ), &hash_len ) );
1899
1900 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1901 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1902 PSA_ERROR_BAD_STATE );
1903 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1904 PSA_ERROR_BAD_STATE );
1905
1906exit:
1907 psa_hash_abort( &op_target );
1908 psa_hash_abort( &op_init );
1909 psa_hash_abort( &op_setup );
1910 psa_hash_abort( &op_finished );
1911 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001912 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001913}
1914/* END_CASE */
1915
itayzafrir58028322018-10-25 10:22:01 +03001916/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001917void mac_operation_init( )
1918{
Jaeden Amero252ef282019-02-15 14:05:35 +00001919 const uint8_t input[1] = { 0 };
1920
Jaeden Amero769ce272019-01-04 11:48:03 +00001921 /* Test each valid way of initializing the object, except for `= {0}`, as
1922 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1923 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001924 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00001925 psa_mac_operation_t func = psa_mac_operation_init( );
1926 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1927 psa_mac_operation_t zero;
1928
1929 memset( &zero, 0, sizeof( zero ) );
1930
Jaeden Amero252ef282019-02-15 14:05:35 +00001931 /* A freshly-initialized MAC operation should not be usable. */
1932 TEST_EQUAL( psa_mac_update( &func,
1933 input, sizeof( input ) ),
1934 PSA_ERROR_BAD_STATE );
1935 TEST_EQUAL( psa_mac_update( &init,
1936 input, sizeof( input ) ),
1937 PSA_ERROR_BAD_STATE );
1938 TEST_EQUAL( psa_mac_update( &zero,
1939 input, sizeof( input ) ),
1940 PSA_ERROR_BAD_STATE );
1941
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001942 /* A default MAC operation should be abortable without error. */
1943 PSA_ASSERT( psa_mac_abort( &func ) );
1944 PSA_ASSERT( psa_mac_abort( &init ) );
1945 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001946}
1947/* END_CASE */
1948
1949/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001950void mac_setup( int key_type_arg,
1951 data_t *key,
1952 int alg_arg,
1953 int expected_status_arg )
1954{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001955 psa_key_type_t key_type = key_type_arg;
1956 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001957 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001958 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001959 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1960#if defined(KNOWN_SUPPORTED_MAC_ALG)
1961 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1962#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001963
Gilles Peskine8817f612018-12-18 00:18:46 +01001964 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001965
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001966 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1967 &operation, &status ) )
1968 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001969 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001970
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001971 /* The operation object should be reusable. */
1972#if defined(KNOWN_SUPPORTED_MAC_ALG)
1973 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1974 smoke_test_key_data,
1975 sizeof( smoke_test_key_data ),
1976 KNOWN_SUPPORTED_MAC_ALG,
1977 &operation, &status ) )
1978 goto exit;
1979 TEST_EQUAL( status, PSA_SUCCESS );
1980#endif
1981
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001982exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001983 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001984}
1985/* END_CASE */
1986
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001987/* 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 +00001988void mac_bad_order( )
1989{
Ronald Cron5425a212020-08-04 14:58:35 +02001990 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001991 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1992 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001993 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001994 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1995 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1996 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001998 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1999 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2000 size_t sign_mac_length = 0;
2001 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2002 const uint8_t verify_mac[] = {
2003 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2004 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2005 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2006
2007 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002008 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002009 psa_set_key_algorithm( &attributes, alg );
2010 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002011
Ronald Cron5425a212020-08-04 14:58:35 +02002012 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2013 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002014
Jaeden Amero252ef282019-02-15 14:05:35 +00002015 /* Call update without calling setup beforehand. */
2016 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2017 PSA_ERROR_BAD_STATE );
2018 PSA_ASSERT( psa_mac_abort( &operation ) );
2019
2020 /* Call sign finish without calling setup beforehand. */
2021 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2022 &sign_mac_length),
2023 PSA_ERROR_BAD_STATE );
2024 PSA_ASSERT( psa_mac_abort( &operation ) );
2025
2026 /* Call verify finish without calling setup beforehand. */
2027 TEST_EQUAL( psa_mac_verify_finish( &operation,
2028 verify_mac, sizeof( verify_mac ) ),
2029 PSA_ERROR_BAD_STATE );
2030 PSA_ASSERT( psa_mac_abort( &operation ) );
2031
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002032 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002033 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002034 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002035 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002036 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002037 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002038 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002039 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002040
Jaeden Amero252ef282019-02-15 14:05:35 +00002041 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002042 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002043 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2044 PSA_ASSERT( psa_mac_sign_finish( &operation,
2045 sign_mac, sizeof( sign_mac ),
2046 &sign_mac_length ) );
2047 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2048 PSA_ERROR_BAD_STATE );
2049 PSA_ASSERT( psa_mac_abort( &operation ) );
2050
2051 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002052 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002053 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2054 PSA_ASSERT( psa_mac_verify_finish( &operation,
2055 verify_mac, sizeof( verify_mac ) ) );
2056 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2057 PSA_ERROR_BAD_STATE );
2058 PSA_ASSERT( psa_mac_abort( &operation ) );
2059
2060 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002061 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002062 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2063 PSA_ASSERT( psa_mac_sign_finish( &operation,
2064 sign_mac, sizeof( sign_mac ),
2065 &sign_mac_length ) );
2066 TEST_EQUAL( psa_mac_sign_finish( &operation,
2067 sign_mac, sizeof( sign_mac ),
2068 &sign_mac_length ),
2069 PSA_ERROR_BAD_STATE );
2070 PSA_ASSERT( psa_mac_abort( &operation ) );
2071
2072 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002073 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002074 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2075 PSA_ASSERT( psa_mac_verify_finish( &operation,
2076 verify_mac, sizeof( verify_mac ) ) );
2077 TEST_EQUAL( psa_mac_verify_finish( &operation,
2078 verify_mac, sizeof( verify_mac ) ),
2079 PSA_ERROR_BAD_STATE );
2080 PSA_ASSERT( psa_mac_abort( &operation ) );
2081
2082 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002083 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002084 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002085 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002086 TEST_EQUAL( psa_mac_verify_finish( &operation,
2087 verify_mac, sizeof( verify_mac ) ),
2088 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002089 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002090 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002091 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002092
2093 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002094 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002095 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002096 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002097 TEST_EQUAL( psa_mac_sign_finish( &operation,
2098 sign_mac, sizeof( sign_mac ),
2099 &sign_mac_length ),
2100 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002101 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002102 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002103 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002104
Ronald Cron5425a212020-08-04 14:58:35 +02002105 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002106
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002107exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002108 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002109}
2110/* END_CASE */
2111
2112/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002113void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002114 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002115 int alg_arg,
2116 data_t *input,
2117 data_t *expected_mac )
2118{
Ronald Cron5425a212020-08-04 14:58:35 +02002119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002120 psa_key_type_t key_type = key_type_arg;
2121 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002122 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002124 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002125 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002126 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002127 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002128 const size_t output_sizes_to_test[] = {
2129 0,
2130 1,
2131 expected_mac->len - 1,
2132 expected_mac->len,
2133 expected_mac->len + 1,
2134 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002135
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002136 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002137 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002138 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002139
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002141
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002142 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002143 psa_set_key_algorithm( &attributes, alg );
2144 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002145
Ronald Cron5425a212020-08-04 14:58:35 +02002146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2147 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002148
Gilles Peskine8b356b52020-08-25 23:44:59 +02002149 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2150 {
2151 const size_t output_size = output_sizes_to_test[i];
2152 psa_status_t expected_status =
2153 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2154 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002155
Chris Jones9634bb12021-01-20 15:56:42 +00002156 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002157 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002158
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002159 /* Calculate the MAC, one-shot case. */
2160 TEST_EQUAL( psa_mac_compute( key, alg,
2161 input->x, input->len,
2162 actual_mac, output_size, &mac_length ),
2163 expected_status );
2164 if( expected_status == PSA_SUCCESS )
2165 {
2166 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2167 actual_mac, mac_length );
2168 }
2169
2170 if( output_size > 0 )
2171 memset( actual_mac, 0, output_size );
2172
2173 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002174 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002175 PSA_ASSERT( psa_mac_update( &operation,
2176 input->x, input->len ) );
2177 TEST_EQUAL( psa_mac_sign_finish( &operation,
2178 actual_mac, output_size,
2179 &mac_length ),
2180 expected_status );
2181 PSA_ASSERT( psa_mac_abort( &operation ) );
2182
2183 if( expected_status == PSA_SUCCESS )
2184 {
2185 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2186 actual_mac, mac_length );
2187 }
2188 mbedtls_free( actual_mac );
2189 actual_mac = NULL;
2190 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002191
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002192exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002193 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002194 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002195 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002196 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002197}
2198/* END_CASE */
2199
2200/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002201void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002202 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002203 int alg_arg,
2204 data_t *input,
2205 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002206{
Ronald Cron5425a212020-08-04 14:58:35 +02002207 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002208 psa_key_type_t key_type = key_type_arg;
2209 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002210 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002212 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002213
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002214 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02002215
Gilles Peskine8817f612018-12-18 00:18:46 +01002216 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002219 psa_set_key_algorithm( &attributes, alg );
2220 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002221
Ronald Cron5425a212020-08-04 14:58:35 +02002222 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2223 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002224
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002225 /* Verify correct MAC, one-shot case. */
2226 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2227 expected_mac->x, expected_mac->len ) );
2228
2229 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002230 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002231 PSA_ASSERT( psa_mac_update( &operation,
2232 input->x, input->len ) );
2233 PSA_ASSERT( psa_mac_verify_finish( &operation,
2234 expected_mac->x,
2235 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002236
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002237 /* Test a MAC that's too short, one-shot case. */
2238 TEST_EQUAL( psa_mac_verify( key, alg,
2239 input->x, input->len,
2240 expected_mac->x,
2241 expected_mac->len - 1 ),
2242 PSA_ERROR_INVALID_SIGNATURE );
2243
2244 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002245 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002246 PSA_ASSERT( psa_mac_update( &operation,
2247 input->x, input->len ) );
2248 TEST_EQUAL( psa_mac_verify_finish( &operation,
2249 expected_mac->x,
2250 expected_mac->len - 1 ),
2251 PSA_ERROR_INVALID_SIGNATURE );
2252
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002253 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002254 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2255 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002256 TEST_EQUAL( psa_mac_verify( key, alg,
2257 input->x, input->len,
2258 perturbed_mac, expected_mac->len + 1 ),
2259 PSA_ERROR_INVALID_SIGNATURE );
2260
2261 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002262 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002263 PSA_ASSERT( psa_mac_update( &operation,
2264 input->x, input->len ) );
2265 TEST_EQUAL( psa_mac_verify_finish( &operation,
2266 perturbed_mac,
2267 expected_mac->len + 1 ),
2268 PSA_ERROR_INVALID_SIGNATURE );
2269
2270 /* Test changing one byte. */
2271 for( size_t i = 0; i < expected_mac->len; i++ )
2272 {
Chris Jones9634bb12021-01-20 15:56:42 +00002273 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002274 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002275
2276 TEST_EQUAL( psa_mac_verify( key, alg,
2277 input->x, input->len,
2278 perturbed_mac, expected_mac->len ),
2279 PSA_ERROR_INVALID_SIGNATURE );
2280
Ronald Cron5425a212020-08-04 14:58:35 +02002281 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002282 PSA_ASSERT( psa_mac_update( &operation,
2283 input->x, input->len ) );
2284 TEST_EQUAL( psa_mac_verify_finish( &operation,
2285 perturbed_mac,
2286 expected_mac->len ),
2287 PSA_ERROR_INVALID_SIGNATURE );
2288 perturbed_mac[i] ^= 1;
2289 }
2290
Gilles Peskine8c9def32018-02-08 10:02:12 +01002291exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002292 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002293 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002294 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002295 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296}
2297/* END_CASE */
2298
2299/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002300void cipher_operation_init( )
2301{
Jaeden Ameroab439972019-02-15 14:12:05 +00002302 const uint8_t input[1] = { 0 };
2303 unsigned char output[1] = { 0 };
2304 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002305 /* Test each valid way of initializing the object, except for `= {0}`, as
2306 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2307 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08002308 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002309 psa_cipher_operation_t func = psa_cipher_operation_init( );
2310 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2311 psa_cipher_operation_t zero;
2312
2313 memset( &zero, 0, sizeof( zero ) );
2314
Jaeden Ameroab439972019-02-15 14:12:05 +00002315 /* A freshly-initialized cipher operation should not be usable. */
2316 TEST_EQUAL( psa_cipher_update( &func,
2317 input, sizeof( input ),
2318 output, sizeof( output ),
2319 &output_length ),
2320 PSA_ERROR_BAD_STATE );
2321 TEST_EQUAL( psa_cipher_update( &init,
2322 input, sizeof( input ),
2323 output, sizeof( output ),
2324 &output_length ),
2325 PSA_ERROR_BAD_STATE );
2326 TEST_EQUAL( psa_cipher_update( &zero,
2327 input, sizeof( input ),
2328 output, sizeof( output ),
2329 &output_length ),
2330 PSA_ERROR_BAD_STATE );
2331
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002332 /* A default cipher operation should be abortable without error. */
2333 PSA_ASSERT( psa_cipher_abort( &func ) );
2334 PSA_ASSERT( psa_cipher_abort( &init ) );
2335 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002336}
2337/* END_CASE */
2338
2339/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340void cipher_setup( int key_type_arg,
2341 data_t *key,
2342 int alg_arg,
2343 int expected_status_arg )
2344{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345 psa_key_type_t key_type = key_type_arg;
2346 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002347 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002348 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002349 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002350#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002351 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2352#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002353
Gilles Peskine8817f612018-12-18 00:18:46 +01002354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002355
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002356 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2357 &operation, &status ) )
2358 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002359 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002360
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002361 /* The operation object should be reusable. */
2362#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2363 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2364 smoke_test_key_data,
2365 sizeof( smoke_test_key_data ),
2366 KNOWN_SUPPORTED_CIPHER_ALG,
2367 &operation, &status ) )
2368 goto exit;
2369 TEST_EQUAL( status, PSA_SUCCESS );
2370#endif
2371
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002372exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002373 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002374 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002375}
2376/* END_CASE */
2377
Ronald Cronee414c72021-03-18 18:50:08 +01002378/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002379void cipher_bad_order( )
2380{
Ronald Cron5425a212020-08-04 14:58:35 +02002381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002382 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2383 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002385 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002386 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002387 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002388 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2389 0xaa, 0xaa, 0xaa, 0xaa };
2390 const uint8_t text[] = {
2391 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2392 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002393 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002394 size_t length = 0;
2395
2396 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002397 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2398 psa_set_key_algorithm( &attributes, alg );
2399 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002400 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2401 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002402
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002403 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002404 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002405 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002406 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002407 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002408 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002409 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002410 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002411
2412 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002413 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002414 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002415 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002416 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002417 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002418 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002419 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002420
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 /* Generate an IV without calling setup beforehand. */
2422 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2423 buffer, sizeof( buffer ),
2424 &length ),
2425 PSA_ERROR_BAD_STATE );
2426 PSA_ASSERT( psa_cipher_abort( &operation ) );
2427
2428 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002429 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002430 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2431 buffer, sizeof( buffer ),
2432 &length ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002433 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002434 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2435 buffer, sizeof( buffer ),
2436 &length ),
2437 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002438 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002439 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002440 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002441
2442 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002443 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002444 PSA_ASSERT( psa_cipher_set_iv( &operation,
2445 iv, sizeof( iv ) ) );
2446 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2447 buffer, sizeof( buffer ),
2448 &length ),
2449 PSA_ERROR_BAD_STATE );
2450 PSA_ASSERT( psa_cipher_abort( &operation ) );
2451
2452 /* Set an IV without calling setup beforehand. */
2453 TEST_EQUAL( psa_cipher_set_iv( &operation,
2454 iv, sizeof( iv ) ),
2455 PSA_ERROR_BAD_STATE );
2456 PSA_ASSERT( psa_cipher_abort( &operation ) );
2457
2458 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002459 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002460 PSA_ASSERT( psa_cipher_set_iv( &operation,
2461 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002462 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002463 TEST_EQUAL( psa_cipher_set_iv( &operation,
2464 iv, sizeof( iv ) ),
2465 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002466 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002467 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002468 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002469
2470 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002471 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002472 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2473 buffer, sizeof( buffer ),
2474 &length ) );
2475 TEST_EQUAL( psa_cipher_set_iv( &operation,
2476 iv, sizeof( iv ) ),
2477 PSA_ERROR_BAD_STATE );
2478 PSA_ASSERT( psa_cipher_abort( &operation ) );
2479
2480 /* Call update without calling setup beforehand. */
2481 TEST_EQUAL( psa_cipher_update( &operation,
2482 text, sizeof( text ),
2483 buffer, sizeof( buffer ),
2484 &length ),
2485 PSA_ERROR_BAD_STATE );
2486 PSA_ASSERT( psa_cipher_abort( &operation ) );
2487
2488 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002489 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002490 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002491 TEST_EQUAL( psa_cipher_update( &operation,
2492 text, sizeof( text ),
2493 buffer, sizeof( buffer ),
2494 &length ),
2495 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002496 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002497 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002498 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002499
2500 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002501 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002502 PSA_ASSERT( psa_cipher_set_iv( &operation,
2503 iv, sizeof( iv ) ) );
2504 PSA_ASSERT( psa_cipher_finish( &operation,
2505 buffer, sizeof( buffer ), &length ) );
2506 TEST_EQUAL( psa_cipher_update( &operation,
2507 text, sizeof( text ),
2508 buffer, sizeof( buffer ),
2509 &length ),
2510 PSA_ERROR_BAD_STATE );
2511 PSA_ASSERT( psa_cipher_abort( &operation ) );
2512
2513 /* Call finish without calling setup beforehand. */
2514 TEST_EQUAL( psa_cipher_finish( &operation,
2515 buffer, sizeof( buffer ), &length ),
2516 PSA_ERROR_BAD_STATE );
2517 PSA_ASSERT( psa_cipher_abort( &operation ) );
2518
2519 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002520 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002521 /* Not calling update means we are encrypting an empty buffer, which is OK
2522 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002523 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002524 TEST_EQUAL( psa_cipher_finish( &operation,
2525 buffer, sizeof( buffer ), &length ),
2526 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002527 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002528 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002529 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002530
2531 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002532 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002533 PSA_ASSERT( psa_cipher_set_iv( &operation,
2534 iv, sizeof( iv ) ) );
2535 PSA_ASSERT( psa_cipher_finish( &operation,
2536 buffer, sizeof( buffer ), &length ) );
2537 TEST_EQUAL( psa_cipher_finish( &operation,
2538 buffer, sizeof( buffer ), &length ),
2539 PSA_ERROR_BAD_STATE );
2540 PSA_ASSERT( psa_cipher_abort( &operation ) );
2541
Ronald Cron5425a212020-08-04 14:58:35 +02002542 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002543
Jaeden Ameroab439972019-02-15 14:12:05 +00002544exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002545 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002546 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002547}
2548/* END_CASE */
2549
2550/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002551void cipher_encrypt_fail( int alg_arg,
2552 int key_type_arg,
2553 data_t *key_data,
2554 data_t *input,
2555 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002556{
Ronald Cron5425a212020-08-04 14:58:35 +02002557 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002558 psa_status_t status;
2559 psa_key_type_t key_type = key_type_arg;
2560 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002561 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002562 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002563 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002564 size_t output_length = 0;
2565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2566
2567 if ( PSA_ERROR_BAD_STATE != expected_status )
2568 {
2569 PSA_ASSERT( psa_crypto_init( ) );
2570
2571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2572 psa_set_key_algorithm( &attributes, alg );
2573 psa_set_key_type( &attributes, key_type );
2574
2575 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2576 input->len );
2577 ASSERT_ALLOC( output, output_buffer_size );
2578
2579 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2580 &key ) );
2581 }
2582
2583 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2584 output_buffer_size, &output_length );
2585
2586 TEST_EQUAL( status, expected_status );
2587
2588exit:
2589 mbedtls_free( output );
2590 psa_destroy_key( key );
2591 PSA_DONE( );
2592}
2593/* END_CASE */
2594
2595/* BEGIN_CASE */
Gilles Peskine69d98172022-04-20 17:07:52 +02002596void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
2597 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002598{
2599 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2600 psa_key_type_t key_type = key_type_arg;
2601 psa_algorithm_t alg = alg_arg;
Ronald Cron33c69682021-07-15 09:38:11 +02002602 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2603 uint8_t iv[1] = { 0x5a };
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002604 unsigned char *output = NULL;
2605 size_t output_buffer_size = 0;
Gilles Peskine4da5a852022-04-20 17:09:38 +02002606 size_t output_length, length;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2608
2609 PSA_ASSERT( psa_crypto_init( ) );
2610
Gilles Peskine5f504202022-04-20 16:55:03 +02002611 /* Validate size macros */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002612 TEST_LE_U( ciphertext->len,
2613 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
2614 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine69d98172022-04-20 17:07:52 +02002615 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002616 TEST_LE_U( plaintext->len,
2617 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
2618 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
2619 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine69d98172022-04-20 17:07:52 +02002620
Gilles Peskine5f504202022-04-20 16:55:03 +02002621
2622 /* Set up key and output buffer */
Gilles Peskine69d98172022-04-20 17:07:52 +02002623 psa_set_key_usage_flags( &attributes,
2624 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002625 psa_set_key_algorithm( &attributes, alg );
2626 psa_set_key_type( &attributes, key_type );
Gilles Peskine5f504202022-04-20 16:55:03 +02002627 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2628 &key ) );
Gilles Peskine69d98172022-04-20 17:07:52 +02002629 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2630 plaintext->len );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002631 ASSERT_ALLOC( output, output_buffer_size );
2632
Gilles Peskine5f504202022-04-20 16:55:03 +02002633 /* set_iv() is not allowed */
Ronald Cron33c69682021-07-15 09:38:11 +02002634 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2635 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2636 PSA_ERROR_BAD_STATE );
Gilles Peskine69d98172022-04-20 17:07:52 +02002637 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2638 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2639 PSA_ERROR_BAD_STATE );
Gilles Peskine5f504202022-04-20 16:55:03 +02002640
2641 /* generate_iv() is not allowed */
Ronald Cron33c69682021-07-15 09:38:11 +02002642 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2643 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine4da5a852022-04-20 17:09:38 +02002644 &length ),
Ronald Cron33c69682021-07-15 09:38:11 +02002645 PSA_ERROR_BAD_STATE );
Gilles Peskine69d98172022-04-20 17:07:52 +02002646 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2647 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine4da5a852022-04-20 17:09:38 +02002648 &length ),
Gilles Peskine69d98172022-04-20 17:07:52 +02002649 PSA_ERROR_BAD_STATE );
Ronald Cron33c69682021-07-15 09:38:11 +02002650
Gilles Peskine4da5a852022-04-20 17:09:38 +02002651 /* Multipart encryption */
2652 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2653 output_length = 0;
2654 length = ~0;
2655 PSA_ASSERT( psa_cipher_update( &operation,
2656 plaintext->x, plaintext->len,
2657 output, output_buffer_size,
2658 &length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002659 TEST_LE_U( length, output_buffer_size );
Gilles Peskine4da5a852022-04-20 17:09:38 +02002660 output_length += length;
2661 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine01bf6312022-11-23 14:15:57 +01002662 mbedtls_buffer_offset( output, output_length ),
Gilles Peskine4da5a852022-04-20 17:09:38 +02002663 output_buffer_size - output_length,
2664 &length ) );
2665 output_length += length;
2666 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
2667 output, output_length );
2668
2669 /* Multipart encryption */
2670 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2671 output_length = 0;
2672 length = ~0;
2673 PSA_ASSERT( psa_cipher_update( &operation,
2674 ciphertext->x, ciphertext->len,
2675 output, output_buffer_size,
2676 &length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002677 TEST_LE_U( length, output_buffer_size );
Gilles Peskine4da5a852022-04-20 17:09:38 +02002678 output_length += length;
2679 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine01bf6312022-11-23 14:15:57 +01002680 mbedtls_buffer_offset( output, output_length ),
Gilles Peskine4da5a852022-04-20 17:09:38 +02002681 output_buffer_size - output_length,
2682 &length ) );
2683 output_length += length;
2684 ASSERT_COMPARE( plaintext->x, plaintext->len,
2685 output, output_length );
2686
Gilles Peskine5f504202022-04-20 16:55:03 +02002687 /* One-shot encryption */
Gilles Peskine69d98172022-04-20 17:07:52 +02002688 output_length = ~0;
2689 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
2690 output, output_buffer_size,
2691 &output_length ) );
2692 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
2693 output, output_length );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002694
Gilles Peskine69d98172022-04-20 17:07:52 +02002695 /* One-shot decryption */
2696 output_length = ~0;
2697 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
2698 output, output_buffer_size,
2699 &output_length ) );
2700 ASSERT_COMPARE( plaintext->x, plaintext->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002701 output, output_length );
Gilles Peskine5f504202022-04-20 16:55:03 +02002702
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002703exit:
2704 mbedtls_free( output );
Gilles Peskine5f504202022-04-20 16:55:03 +02002705 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002706 psa_destroy_key( key );
2707 PSA_DONE( );
2708}
2709/* END_CASE */
2710
2711/* BEGIN_CASE */
Paul Elliotted33ef12021-07-14 12:31:21 +01002712void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2713{
2714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2715 psa_algorithm_t alg = alg_arg;
2716 psa_key_type_t key_type = key_type_arg;
2717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2718 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2719 psa_status_t status;
2720
2721 PSA_ASSERT( psa_crypto_init( ) );
2722
2723 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2724 psa_set_key_algorithm( &attributes, alg );
2725 psa_set_key_type( &attributes, key_type );
2726
2727 /* Usage of either of these two size macros would cause divide by zero
2728 * with incorrect key types previously. Input length should be irrelevant
2729 * here. */
2730 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2731 0 );
2732 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2733
2734
2735 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2736 &key ) );
2737
2738 /* Should fail due to invalid alg type (to support invalid key type).
2739 * Encrypt or decrypt will end up in the same place. */
2740 status = psa_cipher_encrypt_setup( &operation, key, alg );
2741
2742 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2743
2744exit:
2745 psa_cipher_abort( &operation );
2746 psa_destroy_key( key );
2747 PSA_DONE( );
2748}
2749/* END_CASE */
2750
2751/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002752void cipher_encrypt_validation( int alg_arg,
2753 int key_type_arg,
2754 data_t *key_data,
2755 data_t *input )
2756{
2757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2758 psa_key_type_t key_type = key_type_arg;
2759 psa_algorithm_t alg = alg_arg;
2760 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2761 unsigned char *output1 = NULL;
2762 size_t output1_buffer_size = 0;
2763 size_t output1_length = 0;
2764 unsigned char *output2 = NULL;
2765 size_t output2_buffer_size = 0;
2766 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002767 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002768 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002769 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770
Gilles Peskine8817f612018-12-18 00:18:46 +01002771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002772
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002773 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2774 psa_set_key_algorithm( &attributes, alg );
2775 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002776
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002777 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2778 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2779 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2780 ASSERT_ALLOC( output1, output1_buffer_size );
2781 ASSERT_ALLOC( output2, output2_buffer_size );
2782
Ronald Cron5425a212020-08-04 14:58:35 +02002783 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2784 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002785
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002786 /* The one-shot cipher encryption uses generated iv so validating
2787 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002788 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2789 output1_buffer_size, &output1_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002790 TEST_LE_U( output1_length,
2791 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2792 TEST_LE_U( output1_length,
2793 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002794
2795 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2796 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002797
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_cipher_update( &operation,
2799 input->x, input->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002800 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002801 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002802 TEST_LE_U( function_output_length,
2803 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2804 TEST_LE_U( function_output_length,
2805 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002806 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002807
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002808 PSA_ASSERT( psa_cipher_finish( &operation,
2809 output2 + output2_length,
2810 output2_buffer_size - output2_length,
2811 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002812 TEST_LE_U( function_output_length,
2813 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2814 TEST_LE_U( function_output_length,
2815 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002816 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002817
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002818 PSA_ASSERT( psa_cipher_abort( &operation ) );
2819 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2820 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002821
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002823 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002824 mbedtls_free( output1 );
2825 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002826 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002827 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828}
2829/* END_CASE */
2830
2831/* BEGIN_CASE */
2832void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002833 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002834 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002835 int first_part_size_arg,
2836 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002837 data_t *expected_output,
2838 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839{
Ronald Cron5425a212020-08-04 14:58:35 +02002840 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841 psa_key_type_t key_type = key_type_arg;
2842 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002843 psa_status_t status;
2844 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002845 size_t first_part_size = first_part_size_arg;
2846 size_t output1_length = output1_length_arg;
2847 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002848 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849 size_t output_buffer_size = 0;
2850 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002851 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002852 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002854
Gilles Peskine8817f612018-12-18 00:18:46 +01002855 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002856
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002857 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2858 psa_set_key_algorithm( &attributes, alg );
2859 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002860
Ronald Cron5425a212020-08-04 14:58:35 +02002861 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2862 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002863
Ronald Cron5425a212020-08-04 14:58:35 +02002864 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002865
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002866 if( iv->len > 0 )
2867 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002868 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002869 }
2870
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002871 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2872 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002873 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002874
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002875 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002876 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2877 output, output_buffer_size,
2878 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002879 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002880 TEST_LE_U( function_output_length,
2881 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2882 TEST_LE_U( function_output_length,
2883 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002884 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002885
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002886 if( first_part_size < input->len )
2887 {
2888 PSA_ASSERT( psa_cipher_update( &operation,
2889 input->x + first_part_size,
2890 input->len - first_part_size,
2891 ( output_buffer_size == 0 ? NULL :
2892 output + total_output_length ),
2893 output_buffer_size - total_output_length,
2894 &function_output_length ) );
2895 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002896 TEST_LE_U( function_output_length,
2897 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2898 alg,
2899 input->len - first_part_size ) );
2900 TEST_LE_U( function_output_length,
2901 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002902 total_output_length += function_output_length;
2903 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002904
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002905 status = psa_cipher_finish( &operation,
2906 ( output_buffer_size == 0 ? NULL :
2907 output + total_output_length ),
2908 output_buffer_size - total_output_length,
2909 &function_output_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002910 TEST_LE_U( function_output_length,
2911 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2912 TEST_LE_U( function_output_length,
2913 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002914 total_output_length += function_output_length;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002915 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002916
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002917 if( expected_status == PSA_SUCCESS )
2918 {
2919 PSA_ASSERT( psa_cipher_abort( &operation ) );
2920
2921 ASSERT_COMPARE( expected_output->x, expected_output->len,
2922 output, total_output_length );
2923 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924
2925exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002926 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002927 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002928 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002929 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002930}
2931/* END_CASE */
2932
2933/* BEGIN_CASE */
2934void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002935 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002936 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002937 int first_part_size_arg,
2938 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002939 data_t *expected_output,
2940 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002941{
Ronald Cron5425a212020-08-04 14:58:35 +02002942 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943 psa_key_type_t key_type = key_type_arg;
2944 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002945 psa_status_t status;
2946 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002947 size_t first_part_size = first_part_size_arg;
2948 size_t output1_length = output1_length_arg;
2949 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002950 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002951 size_t output_buffer_size = 0;
2952 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002953 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002954 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002956
Gilles Peskine8817f612018-12-18 00:18:46 +01002957 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2960 psa_set_key_algorithm( &attributes, alg );
2961 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002962
Ronald Cron5425a212020-08-04 14:58:35 +02002963 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2964 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002965
Ronald Cron5425a212020-08-04 14:58:35 +02002966 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967
Steven Cooreman177deba2020-09-07 17:14:14 +02002968 if( iv->len > 0 )
2969 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002970 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002971 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002973 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2974 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002975 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002977 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002978 PSA_ASSERT( psa_cipher_update( &operation,
2979 input->x, first_part_size,
2980 output, output_buffer_size,
2981 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002982 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002983 TEST_LE_U( function_output_length,
2984 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2985 TEST_LE_U( function_output_length,
2986 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002987 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002988
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002989 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002990 {
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002991 PSA_ASSERT( psa_cipher_update( &operation,
2992 input->x + first_part_size,
2993 input->len - first_part_size,
2994 ( output_buffer_size == 0 ? NULL :
2995 output + total_output_length ),
2996 output_buffer_size - total_output_length,
2997 &function_output_length ) );
2998 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002999 TEST_LE_U( function_output_length,
3000 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3001 alg,
3002 input->len - first_part_size ) );
3003 TEST_LE_U( function_output_length,
3004 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003005 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003006 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003007
Gilles Peskine50e586b2018-06-08 14:28:46 +02003008 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003009 ( output_buffer_size == 0 ? NULL :
3010 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003011 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003012 &function_output_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003013 TEST_LE_U( function_output_length,
3014 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3015 TEST_LE_U( function_output_length,
3016 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003017 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003018 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003019
3020 if( expected_status == PSA_SUCCESS )
3021 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003023
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003024 ASSERT_COMPARE( expected_output->x, expected_output->len,
3025 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003026 }
3027
Gilles Peskine50e586b2018-06-08 14:28:46 +02003028exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003029 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003030 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003031 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003032 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003033}
3034/* END_CASE */
3035
Gilles Peskine50e586b2018-06-08 14:28:46 +02003036/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02003037void cipher_decrypt_fail( int alg_arg,
3038 int key_type_arg,
3039 data_t *key_data,
3040 data_t *iv,
3041 data_t *input_arg,
3042 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003043{
3044 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3045 psa_status_t status;
3046 psa_key_type_t key_type = key_type_arg;
3047 psa_algorithm_t alg = alg_arg;
3048 psa_status_t expected_status = expected_status_arg;
3049 unsigned char *input = NULL;
3050 size_t input_buffer_size = 0;
3051 unsigned char *output = NULL;
3052 size_t output_buffer_size = 0;
3053 size_t output_length = 0;
3054 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3055
3056 if ( PSA_ERROR_BAD_STATE != expected_status )
3057 {
3058 PSA_ASSERT( psa_crypto_init( ) );
3059
3060 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3061 psa_set_key_algorithm( &attributes, alg );
3062 psa_set_key_type( &attributes, key_type );
3063
3064 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3065 &key ) );
3066 }
3067
3068 /* Allocate input buffer and copy the iv and the plaintext */
3069 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3070 if ( input_buffer_size > 0 )
3071 {
3072 ASSERT_ALLOC( input, input_buffer_size );
3073 memcpy( input, iv->x, iv->len );
3074 memcpy( input + iv->len, input_arg->x, input_arg->len );
3075 }
3076
3077 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3078 ASSERT_ALLOC( output, output_buffer_size );
3079
3080 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3081 output_buffer_size, &output_length );
3082 TEST_EQUAL( status, expected_status );
3083
3084exit:
3085 mbedtls_free( input );
3086 mbedtls_free( output );
3087 psa_destroy_key( key );
3088 PSA_DONE( );
3089}
3090/* END_CASE */
3091
3092/* BEGIN_CASE */
3093void cipher_decrypt( int alg_arg,
3094 int key_type_arg,
3095 data_t *key_data,
3096 data_t *iv,
3097 data_t *input_arg,
3098 data_t *expected_output )
3099{
3100 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3101 psa_key_type_t key_type = key_type_arg;
3102 psa_algorithm_t alg = alg_arg;
3103 unsigned char *input = NULL;
3104 size_t input_buffer_size = 0;
3105 unsigned char *output = NULL;
3106 size_t output_buffer_size = 0;
3107 size_t output_length = 0;
3108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3109
3110 PSA_ASSERT( psa_crypto_init( ) );
3111
3112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3113 psa_set_key_algorithm( &attributes, alg );
3114 psa_set_key_type( &attributes, key_type );
3115
3116 /* Allocate input buffer and copy the iv and the plaintext */
3117 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3118 if ( input_buffer_size > 0 )
3119 {
3120 ASSERT_ALLOC( input, input_buffer_size );
3121 memcpy( input, iv->x, iv->len );
3122 memcpy( input + iv->len, input_arg->x, input_arg->len );
3123 }
3124
3125 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3126 ASSERT_ALLOC( output, output_buffer_size );
3127
3128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3129 &key ) );
3130
3131 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3132 output_buffer_size, &output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003133 TEST_LE_U( output_length,
3134 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3135 TEST_LE_U( output_length,
3136 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003137
3138 ASSERT_COMPARE( expected_output->x, expected_output->len,
3139 output, output_length );
3140exit:
3141 mbedtls_free( input );
3142 mbedtls_free( output );
3143 psa_destroy_key( key );
3144 PSA_DONE( );
3145}
3146/* END_CASE */
3147
3148/* BEGIN_CASE */
3149void cipher_verify_output( int alg_arg,
3150 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003151 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003152 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003153{
Ronald Cron5425a212020-08-04 14:58:35 +02003154 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003155 psa_key_type_t key_type = key_type_arg;
3156 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003157 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003158 size_t output1_size = 0;
3159 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003160 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003161 size_t output2_size = 0;
3162 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003164
Gilles Peskine8817f612018-12-18 00:18:46 +01003165 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003166
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003167 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3168 psa_set_key_algorithm( &attributes, alg );
3169 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003170
Ronald Cron5425a212020-08-04 14:58:35 +02003171 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3172 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003173 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003174 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003175
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003176 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3177 output1, output1_size,
3178 &output1_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003179 TEST_LE_U( output1_length,
3180 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3181 TEST_LE_U( output1_length,
3182 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003183
3184 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003185 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003186
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003187 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3188 output2, output2_size,
3189 &output2_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003190 TEST_LE_U( output2_length,
3191 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3192 TEST_LE_U( output2_length,
3193 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003194
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003195 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003196
3197exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003198 mbedtls_free( output1 );
3199 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003200 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003201 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003202}
3203/* END_CASE */
3204
3205/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003206void cipher_verify_output_multipart( int alg_arg,
3207 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003208 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003209 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003210 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003211{
Ronald Cron5425a212020-08-04 14:58:35 +02003212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003213 psa_key_type_t key_type = key_type_arg;
3214 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003215 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003216 unsigned char iv[16] = {0};
3217 size_t iv_size = 16;
3218 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003219 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003220 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003221 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003222 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003223 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003224 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003225 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003226 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3227 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003229
Gilles Peskine8817f612018-12-18 00:18:46 +01003230 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003231
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003232 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3233 psa_set_key_algorithm( &attributes, alg );
3234 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003235
Ronald Cron5425a212020-08-04 14:58:35 +02003236 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3237 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003238
Ronald Cron5425a212020-08-04 14:58:35 +02003239 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3240 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003241
Steven Cooreman177deba2020-09-07 17:14:14 +02003242 if( alg != PSA_ALG_ECB_NO_PADDING )
3243 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003244 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3245 iv, iv_size,
3246 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003247 }
3248
gabor-mezei-armceface22021-01-21 12:26:17 +01003249 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003250 TEST_LE_U( output1_buffer_size,
3251 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003252 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003253
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003254 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003255
Gilles Peskine8817f612018-12-18 00:18:46 +01003256 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3257 output1, output1_buffer_size,
3258 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003259 TEST_LE_U( function_output_length,
3260 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3261 TEST_LE_U( function_output_length,
3262 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003263 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003264
Gilles Peskine8817f612018-12-18 00:18:46 +01003265 PSA_ASSERT( psa_cipher_update( &operation1,
3266 input->x + first_part_size,
3267 input->len - first_part_size,
3268 output1, output1_buffer_size,
3269 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003270 TEST_LE_U( function_output_length,
3271 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3272 alg,
3273 input->len - first_part_size ) );
3274 TEST_LE_U( function_output_length,
3275 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003276 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003277
Gilles Peskine8817f612018-12-18 00:18:46 +01003278 PSA_ASSERT( psa_cipher_finish( &operation1,
3279 output1 + output1_length,
3280 output1_buffer_size - output1_length,
3281 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003282 TEST_LE_U( function_output_length,
3283 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3284 TEST_LE_U( function_output_length,
3285 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003286 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003287
Gilles Peskine8817f612018-12-18 00:18:46 +01003288 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003289
Gilles Peskine048b7f02018-06-08 14:20:49 +02003290 output2_buffer_size = output1_length;
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003291 TEST_LE_U( output2_buffer_size,
3292 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3293 TEST_LE_U( output2_buffer_size,
3294 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003295 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003296
Steven Cooreman177deba2020-09-07 17:14:14 +02003297 if( iv_length > 0 )
3298 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003299 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3300 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003301 }
Moran Pekerded84402018-06-06 16:36:50 +03003302
Gilles Peskine8817f612018-12-18 00:18:46 +01003303 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3304 output2, output2_buffer_size,
3305 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003306 TEST_LE_U( function_output_length,
3307 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3308 TEST_LE_U( function_output_length,
3309 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003310 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003311
Gilles Peskine8817f612018-12-18 00:18:46 +01003312 PSA_ASSERT( psa_cipher_update( &operation2,
3313 output1 + first_part_size,
3314 output1_length - first_part_size,
3315 output2, output2_buffer_size,
3316 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003317 TEST_LE_U( function_output_length,
3318 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3319 alg,
3320 output1_length - first_part_size ) );
3321 TEST_LE_U( function_output_length,
3322 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003323 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003324
Gilles Peskine8817f612018-12-18 00:18:46 +01003325 PSA_ASSERT( psa_cipher_finish( &operation2,
3326 output2 + output2_length,
3327 output2_buffer_size - output2_length,
3328 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003329 TEST_LE_U( function_output_length,
3330 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3331 TEST_LE_U( function_output_length,
3332 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003333 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003334
Gilles Peskine8817f612018-12-18 00:18:46 +01003335 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003336
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003337 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003338
3339exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003340 psa_cipher_abort( &operation1 );
3341 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003342 mbedtls_free( output1 );
3343 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003344 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003345 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003346}
3347/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003348
Gilles Peskine20035e32018-02-03 22:44:14 +01003349/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003350void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003351 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003352 data_t *nonce,
3353 data_t *additional_data,
3354 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003355 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003356{
Ronald Cron5425a212020-08-04 14:58:35 +02003357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358 psa_key_type_t key_type = key_type_arg;
3359 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003360 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003361 unsigned char *output_data = NULL;
3362 size_t output_size = 0;
3363 size_t output_length = 0;
3364 unsigned char *output_data2 = NULL;
3365 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003366 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003367 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003369
Gilles Peskine8817f612018-12-18 00:18:46 +01003370 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003371
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003372 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3373 psa_set_key_algorithm( &attributes, alg );
3374 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003375
Gilles Peskine049c7532019-05-15 20:22:09 +02003376 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003377 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003378 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3379 key_bits = psa_get_key_bits( &attributes );
3380
3381 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3382 alg );
3383 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3384 * should be exact. */
3385 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3386 expected_result != PSA_ERROR_NOT_SUPPORTED )
3387 {
3388 TEST_EQUAL( output_size,
3389 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3390 TEST_ASSERT( output_size <=
3391 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3392 }
3393 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003394
Steven Cooremanf49478b2021-02-15 15:19:25 +01003395 status = psa_aead_encrypt( key, alg,
3396 nonce->x, nonce->len,
3397 additional_data->x,
3398 additional_data->len,
3399 input_data->x, input_data->len,
3400 output_data, output_size,
3401 &output_length );
3402
3403 /* If the operation is not supported, just skip and not fail in case the
3404 * encryption involves a common limitation of cryptography hardwares and
3405 * an alternative implementation. */
3406 if( status == PSA_ERROR_NOT_SUPPORTED )
3407 {
3408 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3409 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3410 }
3411
3412 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003413
3414 if( PSA_SUCCESS == expected_result )
3415 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003416 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417
Gilles Peskine003a4a92019-05-14 16:09:40 +02003418 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3419 * should be exact. */
3420 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003421 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003422
gabor-mezei-armceface22021-01-21 12:26:17 +01003423 TEST_ASSERT( input_data->len <=
3424 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3425
Ronald Cron5425a212020-08-04 14:58:35 +02003426 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003427 nonce->x, nonce->len,
3428 additional_data->x,
3429 additional_data->len,
3430 output_data, output_length,
3431 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003432 &output_length2 ),
3433 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003434
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003435 ASSERT_COMPARE( input_data->x, input_data->len,
3436 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003438
Gilles Peskinea1cac842018-06-11 19:33:02 +02003439exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003440 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003441 mbedtls_free( output_data );
3442 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003443 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444}
3445/* END_CASE */
3446
3447/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003448void aead_encrypt( int key_type_arg, data_t *key_data,
3449 int alg_arg,
3450 data_t *nonce,
3451 data_t *additional_data,
3452 data_t *input_data,
3453 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003454{
Ronald Cron5425a212020-08-04 14:58:35 +02003455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456 psa_key_type_t key_type = key_type_arg;
3457 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003458 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459 unsigned char *output_data = NULL;
3460 size_t output_size = 0;
3461 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003463 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003464
Gilles Peskine8817f612018-12-18 00:18:46 +01003465 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003466
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3468 psa_set_key_algorithm( &attributes, alg );
3469 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskine049c7532019-05-15 20:22:09 +02003471 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003472 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003473 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3474 key_bits = psa_get_key_bits( &attributes );
3475
3476 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3477 alg );
3478 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3479 * should be exact. */
3480 TEST_EQUAL( output_size,
3481 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3482 TEST_ASSERT( output_size <=
3483 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3484 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003485
Steven Cooremand588ea12021-01-11 19:36:04 +01003486 status = psa_aead_encrypt( key, alg,
3487 nonce->x, nonce->len,
3488 additional_data->x, additional_data->len,
3489 input_data->x, input_data->len,
3490 output_data, output_size,
3491 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003492
Ronald Cron28a45ed2021-02-09 20:35:42 +01003493 /* If the operation is not supported, just skip and not fail in case the
3494 * encryption involves a common limitation of cryptography hardwares and
3495 * an alternative implementation. */
3496 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003497 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003498 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3499 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003500 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003501
3502 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003503 ASSERT_COMPARE( expected_result->x, expected_result->len,
3504 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003505
Gilles Peskinea1cac842018-06-11 19:33:02 +02003506exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003507 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003508 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003509 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003510}
3511/* END_CASE */
3512
3513/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003514void aead_decrypt( int key_type_arg, data_t *key_data,
3515 int alg_arg,
3516 data_t *nonce,
3517 data_t *additional_data,
3518 data_t *input_data,
3519 data_t *expected_data,
3520 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003521{
Ronald Cron5425a212020-08-04 14:58:35 +02003522 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003523 psa_key_type_t key_type = key_type_arg;
3524 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003525 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003526 unsigned char *output_data = NULL;
3527 size_t output_size = 0;
3528 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003530 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003531 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003532
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003534
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003535 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3536 psa_set_key_algorithm( &attributes, alg );
3537 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003538
Gilles Peskine049c7532019-05-15 20:22:09 +02003539 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003540 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003541 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3542 key_bits = psa_get_key_bits( &attributes );
3543
3544 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3545 alg );
3546 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3547 expected_result != PSA_ERROR_NOT_SUPPORTED )
3548 {
3549 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3550 * should be exact. */
3551 TEST_EQUAL( output_size,
3552 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3553 TEST_ASSERT( output_size <=
3554 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3555 }
3556 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003557
Steven Cooremand588ea12021-01-11 19:36:04 +01003558 status = psa_aead_decrypt( key, alg,
3559 nonce->x, nonce->len,
3560 additional_data->x,
3561 additional_data->len,
3562 input_data->x, input_data->len,
3563 output_data, output_size,
3564 &output_length );
3565
Ronald Cron28a45ed2021-02-09 20:35:42 +01003566 /* If the operation is not supported, just skip and not fail in case the
3567 * decryption involves a common limitation of cryptography hardwares and
3568 * an alternative implementation. */
3569 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003570 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003571 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3572 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003573 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003574
3575 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003576
Gilles Peskine2d277862018-06-18 15:41:12 +02003577 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003578 ASSERT_COMPARE( expected_data->x, expected_data->len,
3579 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003580
Gilles Peskinea1cac842018-06-11 19:33:02 +02003581exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003582 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003583 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003584 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003585}
3586/* END_CASE */
3587
3588/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003589void signature_size( int type_arg,
3590 int bits,
3591 int alg_arg,
3592 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003593{
3594 psa_key_type_t type = type_arg;
3595 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003596 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003597
Gilles Peskinefe11b722018-12-18 00:24:04 +01003598 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003599#if defined(MBEDTLS_TEST_DEPRECATED)
3600 TEST_EQUAL( actual_size,
3601 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3602#endif /* MBEDTLS_TEST_DEPRECATED */
3603
Gilles Peskinee59236f2018-01-27 23:32:46 +01003604exit:
3605 ;
3606}
3607/* END_CASE */
3608
3609/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003610void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3611 int alg_arg, data_t *input_data,
3612 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003613{
Ronald Cron5425a212020-08-04 14:58:35 +02003614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003615 psa_key_type_t key_type = key_type_arg;
3616 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003617 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003618 unsigned char *signature = NULL;
3619 size_t signature_size;
3620 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003622
Gilles Peskine8817f612018-12-18 00:18:46 +01003623 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003624
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003625 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003626 psa_set_key_algorithm( &attributes, alg );
3627 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003628
Gilles Peskine049c7532019-05-15 20:22:09 +02003629 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003630 &key ) );
3631 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003632 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003633
Shaun Case0e7791f2021-12-20 21:14:10 -08003634 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003635 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003636 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003637 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003638 TEST_ASSERT( signature_size != 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003639 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003640 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003641
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003642 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003643 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003644 input_data->x, input_data->len,
3645 signature, signature_size,
3646 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003647 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003648 ASSERT_COMPARE( output_data->x, output_data->len,
3649 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003650
Gilles Peskine0627f982019-11-26 19:12:16 +01003651#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003652 memset( signature, 0, signature_size );
3653 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003654 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003655 input_data->x, input_data->len,
3656 signature, signature_size,
3657 &signature_length ) );
3658 ASSERT_COMPARE( output_data->x, output_data->len,
3659 signature, signature_length );
3660#endif /* MBEDTLS_TEST_DEPRECATED */
3661
Gilles Peskine20035e32018-02-03 22:44:14 +01003662exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003663 /*
3664 * Key attributes may have been returned by psa_get_key_attributes()
3665 * thus reset them as required.
3666 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003667 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003668
Ronald Cron5425a212020-08-04 14:58:35 +02003669 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003670 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003671 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003672}
3673/* END_CASE */
3674
3675/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003676void sign_hash_fail( int key_type_arg, data_t *key_data,
3677 int alg_arg, data_t *input_data,
3678 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003679{
Ronald Cron5425a212020-08-04 14:58:35 +02003680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003681 psa_key_type_t key_type = key_type_arg;
3682 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003683 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003684 psa_status_t actual_status;
3685 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003686 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003687 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003689
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003690 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003691
Gilles Peskine8817f612018-12-18 00:18:46 +01003692 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003693
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003694 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003695 psa_set_key_algorithm( &attributes, alg );
3696 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003697
Gilles Peskine049c7532019-05-15 20:22:09 +02003698 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003699 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003700
Ronald Cron5425a212020-08-04 14:58:35 +02003701 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003702 input_data->x, input_data->len,
3703 signature, signature_size,
3704 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003705 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003706 /* The value of *signature_length is unspecified on error, but
3707 * whatever it is, it should be less than signature_size, so that
3708 * if the caller tries to read *signature_length bytes without
3709 * checking the error code then they don't overflow a buffer. */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003710 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003711
Gilles Peskine895242b2019-11-29 12:15:40 +01003712#if defined(MBEDTLS_TEST_DEPRECATED)
3713 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003714 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003715 input_data->x, input_data->len,
3716 signature, signature_size,
3717 &signature_length ),
3718 expected_status );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003719 TEST_LE_U( signature_length, signature_size );
Gilles Peskine895242b2019-11-29 12:15:40 +01003720#endif /* MBEDTLS_TEST_DEPRECATED */
3721
Gilles Peskine20035e32018-02-03 22:44:14 +01003722exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003723 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003724 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003725 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003726 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003727}
3728/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003729
3730/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003731void sign_verify_hash( int key_type_arg, data_t *key_data,
3732 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003733{
Ronald Cron5425a212020-08-04 14:58:35 +02003734 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003735 psa_key_type_t key_type = key_type_arg;
3736 psa_algorithm_t alg = alg_arg;
3737 size_t key_bits;
3738 unsigned char *signature = NULL;
3739 size_t signature_size;
3740 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003741 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003742
Gilles Peskine8817f612018-12-18 00:18:46 +01003743 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003744
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003745 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003746 psa_set_key_algorithm( &attributes, alg );
3747 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003748
Gilles Peskine049c7532019-05-15 20:22:09 +02003749 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003750 &key ) );
3751 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003752 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003753
Shaun Case0e7791f2021-12-20 21:14:10 -08003754 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02003755 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003756 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003757 key_bits, alg );
3758 TEST_ASSERT( signature_size != 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003759 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003760 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003761
3762 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003763 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003764 input_data->x, input_data->len,
3765 signature, signature_size,
3766 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003767 /* Check that the signature length looks sensible. */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003768 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003769 TEST_ASSERT( signature_length > 0 );
3770
3771 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003772 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003773 input_data->x, input_data->len,
3774 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003775
3776 if( input_data->len != 0 )
3777 {
3778 /* Flip a bit in the input and verify that the signature is now
3779 * detected as invalid. Flip a bit at the beginning, not at the end,
3780 * because ECDSA may ignore the last few bits of the input. */
3781 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003782 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003783 input_data->x, input_data->len,
3784 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003785 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003786 }
3787
3788exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003789 /*
3790 * Key attributes may have been returned by psa_get_key_attributes()
3791 * thus reset them as required.
3792 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003793 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003794
Ronald Cron5425a212020-08-04 14:58:35 +02003795 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003796 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003797 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003798}
3799/* END_CASE */
3800
3801/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003802void verify_hash( int key_type_arg, data_t *key_data,
3803 int alg_arg, data_t *hash_data,
3804 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003805{
Ronald Cron5425a212020-08-04 14:58:35 +02003806 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003807 psa_key_type_t key_type = key_type_arg;
3808 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003810
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003811 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003812
Gilles Peskine8817f612018-12-18 00:18:46 +01003813 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003814
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003815 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003816 psa_set_key_algorithm( &attributes, alg );
3817 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003818
Gilles Peskine049c7532019-05-15 20:22:09 +02003819 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003820 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003821
Ronald Cron5425a212020-08-04 14:58:35 +02003822 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003823 hash_data->x, hash_data->len,
3824 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003825
3826#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003827 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003828 hash_data->x, hash_data->len,
3829 signature_data->x,
3830 signature_data->len ) );
3831
3832#endif /* MBEDTLS_TEST_DEPRECATED */
3833
itayzafrir5c753392018-05-08 11:18:38 +03003834exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003835 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003836 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003837 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003838}
3839/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003840
3841/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003842void verify_hash_fail( int key_type_arg, data_t *key_data,
3843 int alg_arg, data_t *hash_data,
3844 data_t *signature_data,
3845 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003846{
Ronald Cron5425a212020-08-04 14:58:35 +02003847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003848 psa_key_type_t key_type = key_type_arg;
3849 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003850 psa_status_t actual_status;
3851 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003853
Gilles Peskine8817f612018-12-18 00:18:46 +01003854 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003855
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003856 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003857 psa_set_key_algorithm( &attributes, alg );
3858 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003859
Gilles Peskine049c7532019-05-15 20:22:09 +02003860 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003861 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003862
Ronald Cron5425a212020-08-04 14:58:35 +02003863 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003864 hash_data->x, hash_data->len,
3865 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003866 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867
Gilles Peskine895242b2019-11-29 12:15:40 +01003868#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003869 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003870 hash_data->x, hash_data->len,
3871 signature_data->x, signature_data->len ),
3872 expected_status );
3873#endif /* MBEDTLS_TEST_DEPRECATED */
3874
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003876 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003877 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003878 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003879}
3880/* END_CASE */
3881
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003883void sign_message_deterministic( int key_type_arg,
3884 data_t *key_data,
3885 int alg_arg,
3886 data_t *input_data,
3887 data_t *output_data )
3888{
3889 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3890 psa_key_type_t key_type = key_type_arg;
3891 psa_algorithm_t alg = alg_arg;
3892 size_t key_bits;
3893 unsigned char *signature = NULL;
3894 size_t signature_size;
3895 size_t signature_length = 0xdeadbeef;
3896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3897
3898 PSA_ASSERT( psa_crypto_init( ) );
3899
3900 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3901 psa_set_key_algorithm( &attributes, alg );
3902 psa_set_key_type( &attributes, key_type );
3903
3904 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3905 &key ) );
3906 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3907 key_bits = psa_get_key_bits( &attributes );
3908
3909 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3910 TEST_ASSERT( signature_size != 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003911 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-armabd72582021-04-15 18:19:50 +02003912 ASSERT_ALLOC( signature, signature_size );
3913
3914 PSA_ASSERT( psa_sign_message( key, alg,
3915 input_data->x, input_data->len,
3916 signature, signature_size,
3917 &signature_length ) );
3918
3919 ASSERT_COMPARE( output_data->x, output_data->len,
3920 signature, signature_length );
3921
3922exit:
3923 psa_reset_key_attributes( &attributes );
3924
3925 psa_destroy_key( key );
3926 mbedtls_free( signature );
3927 PSA_DONE( );
3928
3929}
3930/* END_CASE */
3931
3932/* BEGIN_CASE */
3933void sign_message_fail( int key_type_arg,
3934 data_t *key_data,
3935 int alg_arg,
3936 data_t *input_data,
3937 int signature_size_arg,
3938 int expected_status_arg )
3939{
3940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3941 psa_key_type_t key_type = key_type_arg;
3942 psa_algorithm_t alg = alg_arg;
3943 size_t signature_size = signature_size_arg;
3944 psa_status_t actual_status;
3945 psa_status_t expected_status = expected_status_arg;
3946 unsigned char *signature = NULL;
3947 size_t signature_length = 0xdeadbeef;
3948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3949
3950 ASSERT_ALLOC( signature, signature_size );
3951
3952 PSA_ASSERT( psa_crypto_init( ) );
3953
3954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3955 psa_set_key_algorithm( &attributes, alg );
3956 psa_set_key_type( &attributes, key_type );
3957
3958 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3959 &key ) );
3960
3961 actual_status = psa_sign_message( key, alg,
3962 input_data->x, input_data->len,
3963 signature, signature_size,
3964 &signature_length );
3965 TEST_EQUAL( actual_status, expected_status );
3966 /* The value of *signature_length is unspecified on error, but
3967 * whatever it is, it should be less than signature_size, so that
3968 * if the caller tries to read *signature_length bytes without
3969 * checking the error code then they don't overflow a buffer. */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003970 TEST_LE_U( signature_length, signature_size );
gabor-mezei-armabd72582021-04-15 18:19:50 +02003971
3972exit:
3973 psa_reset_key_attributes( &attributes );
3974 psa_destroy_key( key );
3975 mbedtls_free( signature );
3976 PSA_DONE( );
3977}
3978/* END_CASE */
3979
3980/* BEGIN_CASE */
3981void sign_verify_message( int key_type_arg,
3982 data_t *key_data,
3983 int alg_arg,
3984 data_t *input_data )
3985{
3986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3987 psa_key_type_t key_type = key_type_arg;
3988 psa_algorithm_t alg = alg_arg;
3989 size_t key_bits;
3990 unsigned char *signature = NULL;
3991 size_t signature_size;
3992 size_t signature_length = 0xdeadbeef;
3993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3994
3995 PSA_ASSERT( psa_crypto_init( ) );
3996
3997 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3998 PSA_KEY_USAGE_VERIFY_MESSAGE );
3999 psa_set_key_algorithm( &attributes, alg );
4000 psa_set_key_type( &attributes, key_type );
4001
4002 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4003 &key ) );
4004 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4005 key_bits = psa_get_key_bits( &attributes );
4006
4007 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4008 TEST_ASSERT( signature_size != 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004009 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-armabd72582021-04-15 18:19:50 +02004010 ASSERT_ALLOC( signature, signature_size );
4011
4012 PSA_ASSERT( psa_sign_message( key, alg,
4013 input_data->x, input_data->len,
4014 signature, signature_size,
4015 &signature_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004016 TEST_LE_U( signature_length, signature_size );
gabor-mezei-armabd72582021-04-15 18:19:50 +02004017 TEST_ASSERT( signature_length > 0 );
4018
4019 PSA_ASSERT( psa_verify_message( key, alg,
4020 input_data->x, input_data->len,
4021 signature, signature_length ) );
4022
4023 if( input_data->len != 0 )
4024 {
4025 /* Flip a bit in the input and verify that the signature is now
4026 * detected as invalid. Flip a bit at the beginning, not at the end,
4027 * because ECDSA may ignore the last few bits of the input. */
4028 input_data->x[0] ^= 1;
4029 TEST_EQUAL( psa_verify_message( key, alg,
4030 input_data->x, input_data->len,
4031 signature, signature_length ),
4032 PSA_ERROR_INVALID_SIGNATURE );
4033 }
4034
4035exit:
4036 psa_reset_key_attributes( &attributes );
4037
4038 psa_destroy_key( key );
4039 mbedtls_free( signature );
4040 PSA_DONE( );
4041}
4042/* END_CASE */
4043
4044/* BEGIN_CASE */
4045void verify_message( int key_type_arg,
4046 data_t *key_data,
4047 int alg_arg,
4048 data_t *input_data,
4049 data_t *signature_data )
4050{
4051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4052 psa_key_type_t key_type = key_type_arg;
4053 psa_algorithm_t alg = alg_arg;
4054 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4055
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004056 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-armabd72582021-04-15 18:19:50 +02004057
4058 PSA_ASSERT( psa_crypto_init( ) );
4059
4060 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4061 psa_set_key_algorithm( &attributes, alg );
4062 psa_set_key_type( &attributes, key_type );
4063
4064 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4065 &key ) );
4066
4067 PSA_ASSERT( psa_verify_message( key, alg,
4068 input_data->x, input_data->len,
4069 signature_data->x, signature_data->len ) );
4070
4071exit:
4072 psa_reset_key_attributes( &attributes );
4073 psa_destroy_key( key );
4074 PSA_DONE( );
4075}
4076/* END_CASE */
4077
4078/* BEGIN_CASE */
4079void verify_message_fail( int key_type_arg,
4080 data_t *key_data,
4081 int alg_arg,
4082 data_t *hash_data,
4083 data_t *signature_data,
4084 int expected_status_arg )
4085{
4086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4087 psa_key_type_t key_type = key_type_arg;
4088 psa_algorithm_t alg = alg_arg;
4089 psa_status_t actual_status;
4090 psa_status_t expected_status = expected_status_arg;
4091 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4092
4093 PSA_ASSERT( psa_crypto_init( ) );
4094
4095 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4096 psa_set_key_algorithm( &attributes, alg );
4097 psa_set_key_type( &attributes, key_type );
4098
4099 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4100 &key ) );
4101
4102 actual_status = psa_verify_message( key, alg,
4103 hash_data->x, hash_data->len,
4104 signature_data->x,
4105 signature_data->len );
4106 TEST_EQUAL( actual_status, expected_status );
4107
4108exit:
4109 psa_reset_key_attributes( &attributes );
4110 psa_destroy_key( key );
4111 PSA_DONE( );
4112}
4113/* END_CASE */
4114
4115/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004116void asymmetric_encrypt( int key_type_arg,
4117 data_t *key_data,
4118 int alg_arg,
4119 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004120 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004121 int expected_output_length_arg,
4122 int expected_status_arg )
4123{
Ronald Cron5425a212020-08-04 14:58:35 +02004124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004125 psa_key_type_t key_type = key_type_arg;
4126 psa_algorithm_t alg = alg_arg;
4127 size_t expected_output_length = expected_output_length_arg;
4128 size_t key_bits;
4129 unsigned char *output = NULL;
4130 size_t output_size;
4131 size_t output_length = ~0;
4132 psa_status_t actual_status;
4133 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004135
Gilles Peskine8817f612018-12-18 00:18:46 +01004136 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004137
Gilles Peskine656896e2018-06-29 19:12:28 +02004138 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004139 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4140 psa_set_key_algorithm( &attributes, alg );
4141 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004142 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004143 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004144
4145 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004146 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004147 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004148
Gilles Peskine656896e2018-06-29 19:12:28 +02004149 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004150 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004151 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004152
4153 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004154 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004155 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004156 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004157 output, output_size,
4158 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004159 TEST_EQUAL( actual_status, expected_status );
4160 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004161
Gilles Peskine68428122018-06-30 18:42:41 +02004162 /* If the label is empty, the test framework puts a non-null pointer
4163 * in label->x. Test that a null pointer works as well. */
4164 if( label->len == 0 )
4165 {
4166 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004167 if( output_size != 0 )
4168 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004169 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004170 input_data->x, input_data->len,
4171 NULL, label->len,
4172 output, output_size,
4173 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004174 TEST_EQUAL( actual_status, expected_status );
4175 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004176 }
4177
Gilles Peskine656896e2018-06-29 19:12:28 +02004178exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004179 /*
4180 * Key attributes may have been returned by psa_get_key_attributes()
4181 * thus reset them as required.
4182 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004183 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004184
Ronald Cron5425a212020-08-04 14:58:35 +02004185 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004186 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004187 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004188}
4189/* END_CASE */
4190
4191/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004192void asymmetric_encrypt_decrypt( int key_type_arg,
4193 data_t *key_data,
4194 int alg_arg,
4195 data_t *input_data,
4196 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004197{
Ronald Cron5425a212020-08-04 14:58:35 +02004198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199 psa_key_type_t key_type = key_type_arg;
4200 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004201 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004202 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004203 size_t output_size;
4204 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004205 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004206 size_t output2_size;
4207 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004209
Gilles Peskine8817f612018-12-18 00:18:46 +01004210 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004211
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4213 psa_set_key_algorithm( &attributes, alg );
4214 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004215
Gilles Peskine049c7532019-05-15 20:22:09 +02004216 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004217 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004218
4219 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004220 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004221 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004222
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004223 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004224 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004225 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004226
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004227 output2_size = input_data->len;
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004228 TEST_LE_U( output2_size,
4229 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4230 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004231 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004232
Gilles Peskineeebd7382018-06-08 18:11:54 +02004233 /* We test encryption by checking that encrypt-then-decrypt gives back
4234 * the original plaintext because of the non-optional random
4235 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004236 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004237 input_data->x, input_data->len,
4238 label->x, label->len,
4239 output, output_size,
4240 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004241 /* We don't know what ciphertext length to expect, but check that
4242 * it looks sensible. */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004243 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004244
Ronald Cron5425a212020-08-04 14:58:35 +02004245 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004246 output, output_length,
4247 label->x, label->len,
4248 output2, output2_size,
4249 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004250 ASSERT_COMPARE( input_data->x, input_data->len,
4251 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004252
4253exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004254 /*
4255 * Key attributes may have been returned by psa_get_key_attributes()
4256 * thus reset them as required.
4257 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004258 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004259
Ronald Cron5425a212020-08-04 14:58:35 +02004260 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004261 mbedtls_free( output );
4262 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004263 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004264}
4265/* END_CASE */
4266
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004267/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004268void asymmetric_decrypt( int key_type_arg,
4269 data_t *key_data,
4270 int alg_arg,
4271 data_t *input_data,
4272 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004273 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004274{
Ronald Cron5425a212020-08-04 14:58:35 +02004275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004276 psa_key_type_t key_type = key_type_arg;
4277 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004278 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004279 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004280 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004281 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004283
Gilles Peskine8817f612018-12-18 00:18:46 +01004284 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004285
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004286 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4287 psa_set_key_algorithm( &attributes, alg );
4288 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004289
Gilles Peskine049c7532019-05-15 20:22:09 +02004290 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004291 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004292
gabor-mezei-armceface22021-01-21 12:26:17 +01004293 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4294 key_bits = psa_get_key_bits( &attributes );
4295
4296 /* Determine the maximum ciphertext length */
4297 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004298 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01004299 ASSERT_ALLOC( output, output_size );
4300
Ronald Cron5425a212020-08-04 14:58:35 +02004301 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004302 input_data->x, input_data->len,
4303 label->x, label->len,
4304 output,
4305 output_size,
4306 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004307 ASSERT_COMPARE( expected_data->x, expected_data->len,
4308 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004309
Gilles Peskine68428122018-06-30 18:42:41 +02004310 /* If the label is empty, the test framework puts a non-null pointer
4311 * in label->x. Test that a null pointer works as well. */
4312 if( label->len == 0 )
4313 {
4314 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004315 if( output_size != 0 )
4316 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004317 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004318 input_data->x, input_data->len,
4319 NULL, label->len,
4320 output,
4321 output_size,
4322 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004323 ASSERT_COMPARE( expected_data->x, expected_data->len,
4324 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004325 }
4326
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004327exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004328 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004329 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004330 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004331 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004332}
4333/* END_CASE */
4334
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004335/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004336void asymmetric_decrypt_fail( int key_type_arg,
4337 data_t *key_data,
4338 int alg_arg,
4339 data_t *input_data,
4340 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004341 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004342 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004343{
Ronald Cron5425a212020-08-04 14:58:35 +02004344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004345 psa_key_type_t key_type = key_type_arg;
4346 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004347 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004348 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004349 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004350 psa_status_t actual_status;
4351 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004353
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004354 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004355
Gilles Peskine8817f612018-12-18 00:18:46 +01004356 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004357
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004358 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4359 psa_set_key_algorithm( &attributes, alg );
4360 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004361
Gilles Peskine049c7532019-05-15 20:22:09 +02004362 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004363 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004364
Ronald Cron5425a212020-08-04 14:58:35 +02004365 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004366 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004367 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004368 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004369 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004370 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004371 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004372
Gilles Peskine68428122018-06-30 18:42:41 +02004373 /* If the label is empty, the test framework puts a non-null pointer
4374 * in label->x. Test that a null pointer works as well. */
4375 if( label->len == 0 )
4376 {
4377 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004378 if( output_size != 0 )
4379 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004380 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004381 input_data->x, input_data->len,
4382 NULL, label->len,
4383 output, output_size,
4384 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004385 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004386 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004387 }
4388
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004389exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004390 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004391 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004392 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004393 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004394}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004395/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004396
4397/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004398void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004399{
4400 /* Test each valid way of initializing the object, except for `= {0}`, as
4401 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4402 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08004403 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004404 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004405 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4406 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4407 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004408
4409 memset( &zero, 0, sizeof( zero ) );
4410
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004412 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004413 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004414 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004415 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004416 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004417 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004418
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004420 PSA_ASSERT( psa_key_derivation_abort(&func) );
4421 PSA_ASSERT( psa_key_derivation_abort(&init) );
4422 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004423}
4424/* END_CASE */
4425
Janos Follath16de4a42019-06-13 16:32:24 +01004426/* BEGIN_CASE */
4427void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004428{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004429 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004430 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004432
Gilles Peskine8817f612018-12-18 00:18:46 +01004433 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004434
Janos Follath16de4a42019-06-13 16:32:24 +01004435 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004436 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004437
4438exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004440 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004441}
4442/* END_CASE */
4443
Janos Follathaf3c2a02019-06-12 12:34:34 +01004444/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004445void derive_set_capacity( int alg_arg, int capacity_arg,
4446 int expected_status_arg )
4447{
4448 psa_algorithm_t alg = alg_arg;
4449 size_t capacity = capacity_arg;
4450 psa_status_t expected_status = expected_status_arg;
4451 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4452
4453 PSA_ASSERT( psa_crypto_init( ) );
4454
4455 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4456
4457 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4458 expected_status );
4459
4460exit:
4461 psa_key_derivation_abort( &operation );
4462 PSA_DONE( );
4463}
4464/* END_CASE */
4465
4466/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004467void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004468 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004469 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004470 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004471 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004472 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004473 int expected_status_arg3,
4474 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004475{
4476 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004477 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4478 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004479 psa_status_t expected_statuses[] = {expected_status_arg1,
4480 expected_status_arg2,
4481 expected_status_arg3};
4482 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004483 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4484 MBEDTLS_SVC_KEY_ID_INIT,
4485 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004486 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4488 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004489 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004490 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004491 psa_status_t expected_output_status = expected_output_status_arg;
4492 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004493
4494 PSA_ASSERT( psa_crypto_init( ) );
4495
4496 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4497 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004498
4499 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4500
4501 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4502 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004503 mbedtls_test_set_step( i );
4504 if( steps[i] == 0 )
4505 {
4506 /* Skip this step */
4507 }
4508 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004509 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004510 psa_set_key_type( &attributes, key_types[i] );
4511 PSA_ASSERT( psa_import_key( &attributes,
4512 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004513 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004514 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4515 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4516 {
4517 // When taking a private key as secret input, use key agreement
4518 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004519 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4520 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004521 expected_statuses[i] );
4522 }
4523 else
4524 {
4525 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004526 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004527 expected_statuses[i] );
4528 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004529 }
4530 else
4531 {
4532 TEST_EQUAL( psa_key_derivation_input_bytes(
4533 &operation, steps[i],
4534 inputs[i]->x, inputs[i]->len ),
4535 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004536 }
4537 }
4538
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004539 if( output_key_type != PSA_KEY_TYPE_NONE )
4540 {
4541 psa_reset_key_attributes( &attributes );
Dave Rodgmandc4e4b72021-11-16 12:12:49 +00004542 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004543 psa_set_key_bits( &attributes, 8 );
4544 actual_output_status =
4545 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004546 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004547 }
4548 else
4549 {
4550 uint8_t buffer[1];
4551 actual_output_status =
4552 psa_key_derivation_output_bytes( &operation,
4553 buffer, sizeof( buffer ) );
4554 }
4555 TEST_EQUAL( actual_output_status, expected_output_status );
4556
Janos Follathaf3c2a02019-06-12 12:34:34 +01004557exit:
4558 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004559 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4560 psa_destroy_key( keys[i] );
4561 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004562 PSA_DONE( );
4563}
4564/* END_CASE */
4565
Janos Follathd958bb72019-07-03 15:02:16 +01004566/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004567void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004568{
Janos Follathd958bb72019-07-03 15:02:16 +01004569 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004570 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004571 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004572 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004573 unsigned char input1[] = "Input 1";
4574 size_t input1_length = sizeof( input1 );
4575 unsigned char input2[] = "Input 2";
4576 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004577 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004578 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004579 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4580 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4581 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004582 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004583
Gilles Peskine8817f612018-12-18 00:18:46 +01004584 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004585
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004586 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4587 psa_set_key_algorithm( &attributes, alg );
4588 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004589
Gilles Peskine73676cb2019-05-15 20:15:10 +02004590 PSA_ASSERT( psa_import_key( &attributes,
4591 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004592 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004593
4594 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004595 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4596 input1, input1_length,
4597 input2, input2_length,
4598 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004599 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004600
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004601 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004602 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004603 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004604
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004605 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004606
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004608 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004609
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004610exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004611 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004612 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004613 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004614}
4615/* END_CASE */
4616
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004617/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004618void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004619{
4620 uint8_t output_buffer[16];
4621 size_t buffer_size = 16;
4622 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004623 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004624
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004625 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4626 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004627 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004628
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004629 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004630 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004631
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004632 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004633
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004634 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4635 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004636 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004637
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004639 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004640
4641exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004642 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004643}
4644/* END_CASE */
4645
4646/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004647void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004648 int step1_arg, data_t *input1,
4649 int step2_arg, data_t *input2,
4650 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004651 int requested_capacity_arg,
4652 data_t *expected_output1,
4653 data_t *expected_output2 )
4654{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004655 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004656 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4657 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004658 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4659 MBEDTLS_SVC_KEY_ID_INIT,
4660 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004661 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004663 uint8_t *expected_outputs[2] =
4664 {expected_output1->x, expected_output2->x};
4665 size_t output_sizes[2] =
4666 {expected_output1->len, expected_output2->len};
4667 size_t output_buffer_size = 0;
4668 uint8_t *output_buffer = NULL;
4669 size_t expected_capacity;
4670 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004672 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004673 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004674
4675 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4676 {
4677 if( output_sizes[i] > output_buffer_size )
4678 output_buffer_size = output_sizes[i];
4679 if( output_sizes[i] == 0 )
4680 expected_outputs[i] = NULL;
4681 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004682 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004683 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004684
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004685 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4686 psa_set_key_algorithm( &attributes, alg );
4687 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004688
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004689 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004690 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4691 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4692 requested_capacity ) );
4693 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004694 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004695 switch( steps[i] )
4696 {
4697 case 0:
4698 break;
4699 case PSA_KEY_DERIVATION_INPUT_SECRET:
4700 PSA_ASSERT( psa_import_key( &attributes,
4701 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004702 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004703
4704 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4705 {
4706 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4707 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4708 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4709 }
4710
Gilles Peskine1468da72019-05-29 17:35:49 +02004711 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004712 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004713 break;
4714 default:
4715 PSA_ASSERT( psa_key_derivation_input_bytes(
4716 &operation, steps[i],
4717 inputs[i]->x, inputs[i]->len ) );
4718 break;
4719 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004720 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004721
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004723 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004724 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004725 expected_capacity = requested_capacity;
4726
4727 /* Expansion phase. */
4728 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4729 {
4730 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004732 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004733 if( expected_capacity == 0 && output_sizes[i] == 0 )
4734 {
4735 /* Reading 0 bytes when 0 bytes are available can go either way. */
4736 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004737 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004738 continue;
4739 }
4740 else if( expected_capacity == 0 ||
4741 output_sizes[i] > expected_capacity )
4742 {
4743 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004744 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004745 expected_capacity = 0;
4746 continue;
4747 }
4748 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004749 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004750 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004751 ASSERT_COMPARE( output_buffer, output_sizes[i],
4752 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004753 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004754 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004756 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004757 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004758 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004760
4761exit:
4762 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004764 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4765 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004766 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004767}
4768/* END_CASE */
4769
4770/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004771void derive_full( int alg_arg,
4772 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004773 data_t *input1,
4774 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004775 int requested_capacity_arg )
4776{
Ronald Cron5425a212020-08-04 14:58:35 +02004777 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004778 psa_algorithm_t alg = alg_arg;
4779 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004780 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004781 unsigned char output_buffer[16];
4782 size_t expected_capacity = requested_capacity;
4783 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004784 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004785
Gilles Peskine8817f612018-12-18 00:18:46 +01004786 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004787
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004788 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4789 psa_set_key_algorithm( &attributes, alg );
4790 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004791
Gilles Peskine049c7532019-05-15 20:22:09 +02004792 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004793 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004794
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004795 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4796 input1->x, input1->len,
4797 input2->x, input2->len,
4798 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004799 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004800
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004801 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004802 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004803 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004804
4805 /* Expansion phase. */
4806 while( current_capacity > 0 )
4807 {
4808 size_t read_size = sizeof( output_buffer );
4809 if( read_size > current_capacity )
4810 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004812 output_buffer,
4813 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004814 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004816 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004817 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004818 }
4819
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 /* Check that the operation refuses to go over capacity. */
4821 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004822 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004823
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004825
4826exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004828 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004829 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004830}
4831/* END_CASE */
4832
Janos Follathe60c9052019-07-03 13:51:30 +01004833/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004834void derive_key_exercise( int alg_arg,
4835 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004836 data_t *input1,
4837 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004838 int derived_type_arg,
4839 int derived_bits_arg,
4840 int derived_usage_arg,
4841 int derived_alg_arg )
4842{
Ronald Cron5425a212020-08-04 14:58:35 +02004843 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4844 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004845 psa_algorithm_t alg = alg_arg;
4846 psa_key_type_t derived_type = derived_type_arg;
4847 size_t derived_bits = derived_bits_arg;
4848 psa_key_usage_t derived_usage = derived_usage_arg;
4849 psa_algorithm_t derived_alg = derived_alg_arg;
4850 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004851 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004853 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004854
Gilles Peskine8817f612018-12-18 00:18:46 +01004855 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004856
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004857 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4858 psa_set_key_algorithm( &attributes, alg );
4859 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004860 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004861 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004862
4863 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004864 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4865 input1->x, input1->len,
4866 input2->x, input2->len,
4867 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004868 goto exit;
4869
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004870 psa_set_key_usage_flags( &attributes, derived_usage );
4871 psa_set_key_algorithm( &attributes, derived_alg );
4872 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004873 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004874 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004875 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004876
4877 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004878 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004879 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4880 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004881
4882 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004883 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004884 goto exit;
4885
4886exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004887 /*
4888 * Key attributes may have been returned by psa_get_key_attributes()
4889 * thus reset them as required.
4890 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004891 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004892
4893 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004894 psa_destroy_key( base_key );
4895 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004896 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004897}
4898/* END_CASE */
4899
Janos Follath42fd8882019-07-03 14:17:09 +01004900/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004901void derive_key_export( int alg_arg,
4902 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004903 data_t *input1,
4904 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004905 int bytes1_arg,
4906 int bytes2_arg )
4907{
Ronald Cron5425a212020-08-04 14:58:35 +02004908 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4909 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004910 psa_algorithm_t alg = alg_arg;
4911 size_t bytes1 = bytes1_arg;
4912 size_t bytes2 = bytes2_arg;
4913 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004915 uint8_t *output_buffer = NULL;
4916 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004917 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4918 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004919 size_t length;
4920
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004921 ASSERT_ALLOC( output_buffer, capacity );
4922 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004923 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004924
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004925 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4926 psa_set_key_algorithm( &base_attributes, alg );
4927 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004928 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004929 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004930
4931 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004932 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4933 input1->x, input1->len,
4934 input2->x, input2->len,
4935 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004936 goto exit;
4937
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004939 output_buffer,
4940 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004941 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004942
4943 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004944 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4945 input1->x, input1->len,
4946 input2->x, input2->len,
4947 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004948 goto exit;
4949
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004950 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4951 psa_set_key_algorithm( &derived_attributes, 0 );
4952 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004953 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004955 &derived_key ) );
4956 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004957 export_buffer, bytes1,
4958 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004959 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004960 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004961 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004963 &derived_key ) );
4964 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004965 export_buffer + bytes1, bytes2,
4966 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004967 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004968
4969 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004970 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4971 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004972
4973exit:
4974 mbedtls_free( output_buffer );
4975 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004977 psa_destroy_key( base_key );
4978 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004979 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004980}
4981/* END_CASE */
4982
4983/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004984void derive_key( int alg_arg,
4985 data_t *key_data, data_t *input1, data_t *input2,
4986 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004987 int expected_status_arg,
4988 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004989{
Ronald Cron5425a212020-08-04 14:58:35 +02004990 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4991 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004992 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004993 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004994 size_t bits = bits_arg;
4995 psa_status_t expected_status = expected_status_arg;
4996 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4997 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4998 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4999
5000 PSA_ASSERT( psa_crypto_init( ) );
5001
5002 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5003 psa_set_key_algorithm( &base_attributes, alg );
5004 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5005 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005006 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005007
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005008 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5009 input1->x, input1->len,
5010 input2->x, input2->len,
5011 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005012 goto exit;
5013
5014 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5015 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005016 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005017 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005018
5019 psa_status_t status =
5020 psa_key_derivation_output_key( &derived_attributes,
5021 &operation,
5022 &derived_key );
5023 if( is_large_output > 0 )
5024 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5025 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005026
5027exit:
5028 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005029 psa_destroy_key( base_key );
5030 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005031 PSA_DONE( );
5032}
5033/* END_CASE */
5034
5035/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005036void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005037 int our_key_type_arg, int our_key_alg_arg,
5038 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005039 int expected_status_arg )
5040{
Ronald Cron5425a212020-08-04 14:58:35 +02005041 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005042 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005043 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005044 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005047 psa_status_t expected_status = expected_status_arg;
5048 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005049
Gilles Peskine8817f612018-12-18 00:18:46 +01005050 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005051
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005052 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005053 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005054 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005055 PSA_ASSERT( psa_import_key( &attributes,
5056 our_key_data->x, our_key_data->len,
5057 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005058
Gilles Peskine77f40d82019-04-11 21:27:06 +02005059 /* The tests currently include inputs that should fail at either step.
5060 * Test cases that fail at the setup step should be changed to call
5061 * key_derivation_setup instead, and this function should be renamed
5062 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005064 if( status == PSA_SUCCESS )
5065 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005066 TEST_EQUAL( psa_key_derivation_key_agreement(
5067 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5068 our_key,
5069 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005070 expected_status );
5071 }
5072 else
5073 {
5074 TEST_ASSERT( status == expected_status );
5075 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005076
5077exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005078 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005079 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005080 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005081}
5082/* END_CASE */
5083
5084/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005085void raw_key_agreement( int alg_arg,
5086 int our_key_type_arg, data_t *our_key_data,
5087 data_t *peer_key_data,
5088 data_t *expected_output )
5089{
Ronald Cron5425a212020-08-04 14:58:35 +02005090 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005091 psa_algorithm_t alg = alg_arg;
5092 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005094 unsigned char *output = NULL;
5095 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005096 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005097
Gilles Peskinef0cba732019-04-11 22:12:38 +02005098 PSA_ASSERT( psa_crypto_init( ) );
5099
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5101 psa_set_key_algorithm( &attributes, alg );
5102 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005103 PSA_ASSERT( psa_import_key( &attributes,
5104 our_key_data->x, our_key_data->len,
5105 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005106
gabor-mezei-armceface22021-01-21 12:26:17 +01005107 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5108 key_bits = psa_get_key_bits( &attributes );
5109
Gilles Peskine7d150292022-04-13 23:25:52 +02005110 /* Validate size macros */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02005111 TEST_LE_U( expected_output->len,
5112 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5113 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
5114 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine7d150292022-04-13 23:25:52 +02005115
5116 /* Good case with exact output size */
5117 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02005118 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5119 peer_key_data->x, peer_key_data->len,
5120 output, expected_output->len,
5121 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005122 ASSERT_COMPARE( output, output_length,
5123 expected_output->x, expected_output->len );
Gilles Peskine7d150292022-04-13 23:25:52 +02005124 mbedtls_free( output );
5125 output = NULL;
5126 output_length = ~0;
5127
5128 /* Larger buffer */
5129 ASSERT_ALLOC( output, expected_output->len + 1 );
5130 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5131 peer_key_data->x, peer_key_data->len,
5132 output, expected_output->len + 1,
5133 &output_length ) );
5134 ASSERT_COMPARE( output, output_length,
5135 expected_output->x, expected_output->len );
5136 mbedtls_free( output );
5137 output = NULL;
5138 output_length = ~0;
5139
5140 /* Buffer too small */
5141 ASSERT_ALLOC( output, expected_output->len - 1 );
5142 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
5143 peer_key_data->x, peer_key_data->len,
5144 output, expected_output->len - 1,
5145 &output_length ),
5146 PSA_ERROR_BUFFER_TOO_SMALL );
5147 /* Not required by the spec, but good robustness */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02005148 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine7d150292022-04-13 23:25:52 +02005149 mbedtls_free( output );
5150 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005151
5152exit:
5153 mbedtls_free( output );
5154 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005155 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005156}
5157/* END_CASE */
5158
5159/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005160void key_agreement_capacity( int alg_arg,
5161 int our_key_type_arg, data_t *our_key_data,
5162 data_t *peer_key_data,
5163 int expected_capacity_arg )
5164{
Ronald Cron5425a212020-08-04 14:58:35 +02005165 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005166 psa_algorithm_t alg = alg_arg;
5167 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005170 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005171 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005172
Gilles Peskine8817f612018-12-18 00:18:46 +01005173 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005174
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005175 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5176 psa_set_key_algorithm( &attributes, alg );
5177 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005178 PSA_ASSERT( psa_import_key( &attributes,
5179 our_key_data->x, our_key_data->len,
5180 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005183 PSA_ASSERT( psa_key_derivation_key_agreement(
5184 &operation,
5185 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5186 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005187 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5188 {
5189 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005191 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005192 NULL, 0 ) );
5193 }
Gilles Peskine59685592018-09-18 12:11:34 +02005194
Shaun Case0e7791f2021-12-20 21:14:10 -08005195 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005196 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005198 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005199
Gilles Peskinebf491972018-10-25 22:36:12 +02005200 /* Test the actual capacity by reading the output. */
5201 while( actual_capacity > sizeof( output ) )
5202 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005204 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005205 actual_capacity -= sizeof( output );
5206 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005208 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005210 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005211
Gilles Peskine59685592018-09-18 12:11:34 +02005212exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005214 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005215 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005216}
5217/* END_CASE */
5218
5219/* BEGIN_CASE */
5220void key_agreement_output( int alg_arg,
5221 int our_key_type_arg, data_t *our_key_data,
5222 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005223 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005224{
Ronald Cron5425a212020-08-04 14:58:35 +02005225 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005226 psa_algorithm_t alg = alg_arg;
5227 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005228 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005230 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005231
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005232 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5233 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005234
Gilles Peskine8817f612018-12-18 00:18:46 +01005235 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005236
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005237 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5238 psa_set_key_algorithm( &attributes, alg );
5239 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005240 PSA_ASSERT( psa_import_key( &attributes,
5241 our_key_data->x, our_key_data->len,
5242 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005243
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005245 PSA_ASSERT( psa_key_derivation_key_agreement(
5246 &operation,
5247 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5248 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005249 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5250 {
5251 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005253 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005254 NULL, 0 ) );
5255 }
Gilles Peskine59685592018-09-18 12:11:34 +02005256
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005258 actual_output,
5259 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005260 ASSERT_COMPARE( actual_output, expected_output1->len,
5261 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005262 if( expected_output2->len != 0 )
5263 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005265 actual_output,
5266 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005267 ASSERT_COMPARE( actual_output, expected_output2->len,
5268 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005269 }
Gilles Peskine59685592018-09-18 12:11:34 +02005270
5271exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005273 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005274 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005275 mbedtls_free( actual_output );
5276}
5277/* END_CASE */
5278
5279/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005280void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005281{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005282 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005283 unsigned char *output = NULL;
5284 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005285 size_t i;
5286 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005287
Simon Butcher49f8e312020-03-03 15:51:50 +00005288 TEST_ASSERT( bytes_arg >= 0 );
5289
Gilles Peskine91892022021-02-08 19:50:26 +01005290 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005291 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005292
Gilles Peskine8817f612018-12-18 00:18:46 +01005293 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005294
Gilles Peskinea50d7392018-06-21 10:22:13 +02005295 /* Run several times, to ensure that every output byte will be
5296 * nonzero at least once with overwhelming probability
5297 * (2^(-8*number_of_runs)). */
5298 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005299 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005300 if( bytes != 0 )
5301 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005302 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005303
Gilles Peskinea50d7392018-06-21 10:22:13 +02005304 for( i = 0; i < bytes; i++ )
5305 {
5306 if( output[i] != 0 )
5307 ++changed[i];
5308 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005309 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005310
5311 /* Check that every byte was changed to nonzero at least once. This
5312 * validates that psa_generate_random is overwriting every byte of
5313 * the output buffer. */
5314 for( i = 0; i < bytes; i++ )
5315 {
5316 TEST_ASSERT( changed[i] != 0 );
5317 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005318
5319exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005320 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005321 mbedtls_free( output );
5322 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005323}
5324/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005325
5326/* BEGIN_CASE */
5327void generate_key( int type_arg,
5328 int bits_arg,
5329 int usage_arg,
5330 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005331 int expected_status_arg,
5332 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005333{
Ronald Cron5425a212020-08-04 14:58:35 +02005334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005335 psa_key_type_t type = type_arg;
5336 psa_key_usage_t usage = usage_arg;
5337 size_t bits = bits_arg;
5338 psa_algorithm_t alg = alg_arg;
5339 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005341 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005342
Gilles Peskine8817f612018-12-18 00:18:46 +01005343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005344
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005345 psa_set_key_usage_flags( &attributes, usage );
5346 psa_set_key_algorithm( &attributes, alg );
5347 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005348 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005349
5350 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005351 psa_status_t status = psa_generate_key( &attributes, &key );
5352
5353 if( is_large_key > 0 )
5354 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5355 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005356 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005357 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005358
5359 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005360 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005361 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5362 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005363
Gilles Peskine818ca122018-06-20 18:16:48 +02005364 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005365 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005366 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005367
5368exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005369 /*
5370 * Key attributes may have been returned by psa_get_key_attributes()
5371 * thus reset them as required.
5372 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005373 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005374
Ronald Cron5425a212020-08-04 14:58:35 +02005375 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005376 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005377}
5378/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005379
Ronald Cronee414c72021-03-18 18:50:08 +01005380/* 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 +02005381void generate_key_rsa( int bits_arg,
5382 data_t *e_arg,
5383 int expected_status_arg )
5384{
Ronald Cron5425a212020-08-04 14:58:35 +02005385 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005386 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005387 size_t bits = bits_arg;
5388 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5389 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5390 psa_status_t expected_status = expected_status_arg;
5391 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5392 uint8_t *exported = NULL;
5393 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005394 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005395 size_t exported_length = SIZE_MAX;
5396 uint8_t *e_read_buffer = NULL;
5397 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005398 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005399 size_t e_read_length = SIZE_MAX;
5400
5401 if( e_arg->len == 0 ||
5402 ( e_arg->len == 3 &&
5403 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5404 {
5405 is_default_public_exponent = 1;
5406 e_read_size = 0;
5407 }
5408 ASSERT_ALLOC( e_read_buffer, e_read_size );
5409 ASSERT_ALLOC( exported, exported_size );
5410
5411 PSA_ASSERT( psa_crypto_init( ) );
5412
5413 psa_set_key_usage_flags( &attributes, usage );
5414 psa_set_key_algorithm( &attributes, alg );
5415 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5416 e_arg->x, e_arg->len ) );
5417 psa_set_key_bits( &attributes, bits );
5418
5419 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005420 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005421 if( expected_status != PSA_SUCCESS )
5422 goto exit;
5423
5424 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005425 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005426 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5427 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5428 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5429 e_read_buffer, e_read_size,
5430 &e_read_length ) );
5431 if( is_default_public_exponent )
5432 TEST_EQUAL( e_read_length, 0 );
5433 else
5434 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5435
5436 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005437 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005438 goto exit;
5439
5440 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005441 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005442 exported, exported_size,
5443 &exported_length ) );
5444 {
5445 uint8_t *p = exported;
5446 uint8_t *end = exported + exported_length;
5447 size_t len;
5448 /* RSAPublicKey ::= SEQUENCE {
5449 * modulus INTEGER, -- n
5450 * publicExponent INTEGER } -- e
5451 */
5452 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005453 MBEDTLS_ASN1_SEQUENCE |
5454 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005455 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005456 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5457 MBEDTLS_ASN1_INTEGER ) );
5458 if( len >= 1 && p[0] == 0 )
5459 {
5460 ++p;
5461 --len;
5462 }
5463 if( e_arg->len == 0 )
5464 {
5465 TEST_EQUAL( len, 3 );
5466 TEST_EQUAL( p[0], 1 );
5467 TEST_EQUAL( p[1], 0 );
5468 TEST_EQUAL( p[2], 1 );
5469 }
5470 else
5471 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5472 }
5473
5474exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005475 /*
5476 * Key attributes may have been returned by psa_get_key_attributes() or
5477 * set by psa_set_key_domain_parameters() thus reset them as required.
5478 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005479 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005480
Ronald Cron5425a212020-08-04 14:58:35 +02005481 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005482 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005483 mbedtls_free( e_read_buffer );
5484 mbedtls_free( exported );
5485}
5486/* END_CASE */
5487
Darryl Greend49a4992018-06-18 17:27:26 +01005488/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005489void persistent_key_load_key_from_storage( data_t *data,
5490 int type_arg, int bits_arg,
5491 int usage_flags_arg, int alg_arg,
5492 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005493{
Ronald Cron71016a92020-08-28 19:01:50 +02005494 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5497 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005498 psa_key_type_t type = type_arg;
5499 size_t bits = bits_arg;
5500 psa_key_usage_t usage_flags = usage_flags_arg;
5501 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005502 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005503 unsigned char *first_export = NULL;
5504 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005505 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005506 size_t first_exported_length;
5507 size_t second_exported_length;
5508
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005509 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5510 {
5511 ASSERT_ALLOC( first_export, export_size );
5512 ASSERT_ALLOC( second_export, export_size );
5513 }
Darryl Greend49a4992018-06-18 17:27:26 +01005514
Gilles Peskine8817f612018-12-18 00:18:46 +01005515 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005516
Gilles Peskinec87af662019-05-15 16:12:22 +02005517 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005518 psa_set_key_usage_flags( &attributes, usage_flags );
5519 psa_set_key_algorithm( &attributes, alg );
5520 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005521 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005522
Darryl Green0c6575a2018-11-07 16:05:30 +00005523 switch( generation_method )
5524 {
5525 case IMPORT_KEY:
5526 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005527 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005528 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005529 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005530
Darryl Green0c6575a2018-11-07 16:05:30 +00005531 case GENERATE_KEY:
5532 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005533 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005534 break;
5535
5536 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005537#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005538 {
5539 /* Create base key */
5540 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5541 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5542 psa_set_key_usage_flags( &base_attributes,
5543 PSA_KEY_USAGE_DERIVE );
5544 psa_set_key_algorithm( &base_attributes, derive_alg );
5545 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005546 PSA_ASSERT( psa_import_key( &base_attributes,
5547 data->x, data->len,
5548 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005549 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005550 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005551 PSA_ASSERT( psa_key_derivation_input_key(
5552 &operation,
5553 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005554 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005556 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005557 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5558 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005559 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005561 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005562 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005563 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005564#else
5565 TEST_ASSUME( ! "KDF not supported in this configuration" );
5566#endif
5567 break;
5568
5569 default:
5570 TEST_ASSERT( ! "generation_method not implemented in test" );
5571 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005572 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005573 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005574
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005575 /* Export the key if permitted by the key policy. */
5576 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5577 {
Ronald Cron5425a212020-08-04 14:58:35 +02005578 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005579 first_export, export_size,
5580 &first_exported_length ) );
5581 if( generation_method == IMPORT_KEY )
5582 ASSERT_COMPARE( data->x, data->len,
5583 first_export, first_exported_length );
5584 }
Darryl Greend49a4992018-06-18 17:27:26 +01005585
5586 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005587 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005588 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005589 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005590
Darryl Greend49a4992018-06-18 17:27:26 +01005591 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005592 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005593 TEST_ASSERT( mbedtls_svc_key_id_equal(
5594 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005595 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5596 PSA_KEY_LIFETIME_PERSISTENT );
5597 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5598 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005599 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005600 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005601 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005602
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005603 /* Export the key again if permitted by the key policy. */
5604 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005605 {
Ronald Cron5425a212020-08-04 14:58:35 +02005606 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005607 second_export, export_size,
5608 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005609 ASSERT_COMPARE( first_export, first_exported_length,
5610 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005611 }
5612
5613 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005614 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005615 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005616
5617exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005618 /*
5619 * Key attributes may have been returned by psa_get_key_attributes()
5620 * thus reset them as required.
5621 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005622 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005623
Darryl Greend49a4992018-06-18 17:27:26 +01005624 mbedtls_free( first_export );
5625 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005627 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005628 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005629 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005630}
5631/* END_CASE */