blob: 5f489f97c3f16c430c015faf4f47c84a9d4e644f [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 Peskined3ad55e2022-11-11 16:37:16 +0100453/* Construct and attempt to import a large unstructured key. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200454void import_large_key( int type_arg, int byte_size_arg,
455 int expected_status_arg )
456{
457 psa_key_type_t type = type_arg;
458 size_t byte_size = byte_size_arg;
459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
460 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200461 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200462 psa_status_t status;
463 uint8_t *buffer = NULL;
464 size_t buffer_size = byte_size + 1;
465 size_t n;
466
Steven Cooreman69967ce2021-01-18 18:01:08 +0100467 /* Skip the test case if the target running the test cannot
Shaun Case0e7791f2021-12-20 21:14:10 -0800468 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +0100469 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200470 memset( buffer, 'K', byte_size );
471
472 PSA_ASSERT( psa_crypto_init( ) );
473
474 /* Try importing the key */
475 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
476 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200477 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100478 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200479 TEST_EQUAL( status, expected_status );
480
481 if( status == PSA_SUCCESS )
482 {
Ronald Cron5425a212020-08-04 14:58:35 +0200483 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200484 TEST_EQUAL( psa_get_key_type( &attributes ), type );
485 TEST_EQUAL( psa_get_key_bits( &attributes ),
486 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200487 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200488 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200489 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200490 for( n = 0; n < byte_size; n++ )
491 TEST_EQUAL( buffer[n], 'K' );
492 for( n = byte_size; n < buffer_size; n++ )
493 TEST_EQUAL( buffer[n], 0 );
494 }
495
496exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100497 /*
498 * Key attributes may have been returned by psa_get_key_attributes()
499 * thus reset them as required.
500 */
501 psa_reset_key_attributes( &attributes );
502
Ronald Cron5425a212020-08-04 14:58:35 +0200503 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200504 PSA_DONE( );
505 mbedtls_free( buffer );
506}
507/* END_CASE */
508
Andrzej Kureke0015962022-01-17 15:29:38 +0100509/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100510/* Import an RSA key with a valid structure (but not valid numbers
511 * inside, beyond having sensible size and parity). This is expected to
512 * fail for large keys. */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200513void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
514{
Ronald Cron5425a212020-08-04 14:58:35 +0200515 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516 size_t bits = bits_arg;
517 psa_status_t expected_status = expected_status_arg;
518 psa_status_t status;
519 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200520 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200521 size_t buffer_size = /* Slight overapproximations */
522 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200523 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200524 unsigned char *p;
525 int ret;
526 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200528
Gilles Peskine8817f612018-12-18 00:18:46 +0100529 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200530 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200531
532 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
533 bits, keypair ) ) >= 0 );
534 length = ret;
535
536 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200537 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200538 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100539 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200540
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200541 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200542 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200543
544exit:
545 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200546 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200547}
548/* END_CASE */
549
550/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300551void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300552 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200553 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 int expected_bits,
555 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200556 int expected_export_status_arg,
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100557 /*whether reexport must give the original input exactly*/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558 int canonical_input )
559{
Ronald Cron5425a212020-08-04 14:58:35 +0200560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100561 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200562 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200563 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565 unsigned char *exported = NULL;
566 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100568 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200571 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572
Moran Pekercb088e72018-07-17 17:36:59 +0300573 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200574 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200576 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578
Gilles Peskine4747d192019-04-17 15:05:45 +0200579 psa_set_key_usage_flags( &attributes, usage_arg );
580 psa_set_key_algorithm( &attributes, alg );
581 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700582
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200584 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100585
586 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200587 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200588 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
589 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200590 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591
592 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200593 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100594 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100595
596 /* The exported length must be set by psa_export_key() to a value between 0
597 * and export_size. On errors, the exported length must be 0. */
598 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
599 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +0200600 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100601
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200602 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200603 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200605 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100606 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200608 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609
Gilles Peskineea38a922021-02-13 00:05:16 +0100610 /* Run sanity checks on the exported key. For non-canonical inputs,
611 * this validates the canonical representations. For canonical inputs,
612 * this doesn't directly validate the implementation, but it still helps
613 * by cross-validating the test data with the sanity check code. */
614 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200615 goto exit;
616
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100617 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200618 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 else
620 {
Ronald Cron5425a212020-08-04 14:58:35 +0200621 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200622 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200623 &key2 ) );
624 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100625 reexported,
626 export_size,
627 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200628 ASSERT_COMPARE( exported, exported_length,
629 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200630 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100632 TEST_ASSERT( exported_length <=
633 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
634 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +0200635 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636
637destroy:
638 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200639 PSA_ASSERT( psa_destroy_key( key ) );
640 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641
642exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100643 /*
644 * Key attributes may have been returned by psa_get_key_attributes()
645 * thus reset them as required.
646 */
647 psa_reset_key_attributes( &got_attributes );
648
itayzafrir3e02b3b2018-06-12 17:06:52 +0300649 mbedtls_free( exported );
650 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200651 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652}
653/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100654
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300656void import_export_public_key( data_t *data,
Gilles Peskined3ad55e2022-11-11 16:37:16 +0100657 int type_arg, // key pair or public key
Gilles Peskine2d277862018-06-18 15:41:12 +0200658 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100659 int export_size_delta,
660 int expected_export_status_arg,
661 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662{
Ronald Cron5425a212020-08-04 14:58:35 +0200663 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300664 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200665 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200666 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300667 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300668 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100669 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100670 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300672
Gilles Peskine8817f612018-12-18 00:18:46 +0100673 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300674
Gilles Peskine4747d192019-04-17 15:05:45 +0200675 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
676 psa_set_key_algorithm( &attributes, alg );
677 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300678
679 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200680 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300681
Gilles Peskine49c25912018-10-29 15:15:31 +0100682 /* Export the public key */
683 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200684 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200685 exported, export_size,
686 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100687 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100688 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100689 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200690 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100691 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200692 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200693 bits = psa_get_key_bits( &attributes );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +0200694 TEST_LE_U( expected_public_key->len,
695 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
696 TEST_LE_U( expected_public_key->len,
697 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
698 TEST_LE_U( expected_public_key->len,
699 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100700 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
701 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100702 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300703
704exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100705 /*
706 * Key attributes may have been returned by psa_get_key_attributes()
707 * thus reset them as required.
708 */
709 psa_reset_key_attributes( &attributes );
710
itayzafrir3e02b3b2018-06-12 17:06:52 +0300711 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200712 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200713 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300714}
715/* END_CASE */
716
Gilles Peskine20035e32018-02-03 22:44:14 +0100717/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200718void import_and_exercise_key( data_t *data,
719 int type_arg,
720 int bits_arg,
721 int alg_arg )
722{
Ronald Cron5425a212020-08-04 14:58:35 +0200723 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200724 psa_key_type_t type = type_arg;
725 size_t bits = bits_arg;
726 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100727 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200729 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200730
Gilles Peskine8817f612018-12-18 00:18:46 +0100731 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
Gilles Peskine4747d192019-04-17 15:05:45 +0200733 psa_set_key_usage_flags( &attributes, usage );
734 psa_set_key_algorithm( &attributes, alg );
735 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736
737 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200738 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200739
740 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200741 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200742 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
743 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200744
745 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100746 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200747 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200748
Ronald Cron5425a212020-08-04 14:58:35 +0200749 PSA_ASSERT( psa_destroy_key( key ) );
750 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200751
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200752exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100753 /*
754 * Key attributes may have been returned by psa_get_key_attributes()
755 * thus reset them as required.
756 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200757 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100758
759 psa_reset_key_attributes( &attributes );
760 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200761 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200762}
763/* END_CASE */
764
765/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100766void effective_key_attributes( int type_arg, int expected_type_arg,
767 int bits_arg, int expected_bits_arg,
768 int usage_arg, int expected_usage_arg,
769 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200770{
Ronald Cron5425a212020-08-04 14:58:35 +0200771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100772 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100773 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100774 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100775 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200776 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100777 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200778 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100779 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200781
Gilles Peskine8817f612018-12-18 00:18:46 +0100782 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200783
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200784 psa_set_key_usage_flags( &attributes, usage );
785 psa_set_key_algorithm( &attributes, alg );
786 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100787 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200788
Ronald Cron5425a212020-08-04 14:58:35 +0200789 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100790 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200791
Ronald Cron5425a212020-08-04 14:58:35 +0200792 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100793 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
794 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
795 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
796 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200797
798exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100799 /*
800 * Key attributes may have been returned by psa_get_key_attributes()
801 * thus reset them as required.
802 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200803 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100804
805 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200806 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200807}
808/* END_CASE */
809
810/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100811void check_key_policy( int type_arg, int bits_arg,
812 int usage_arg, int alg_arg )
813{
814 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200815 usage_arg,
816 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200817 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100818 goto exit;
819}
820/* END_CASE */
821
822/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200823void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000824{
825 /* Test each valid way of initializing the object, except for `= {0}`, as
826 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
827 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -0800828 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200829 psa_key_attributes_t func = psa_key_attributes_init( );
830 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
831 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000832
833 memset( &zero, 0, sizeof( zero ) );
834
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200835 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
836 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
837 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000838
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200839 TEST_EQUAL( psa_get_key_type( &func ), 0 );
840 TEST_EQUAL( psa_get_key_type( &init ), 0 );
841 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
842
843 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
844 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
845 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
846
847 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
848 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
849 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
850
851 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
852 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
853 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000854}
855/* END_CASE */
856
857/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200858void mac_key_policy( int policy_usage_arg,
859 int policy_alg_arg,
860 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200861 data_t *key_data,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200862 int exercise_alg_arg,
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200863 int expected_status_sign_arg,
864 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200865{
Ronald Cron5425a212020-08-04 14:58:35 +0200866 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000868 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200869 psa_key_type_t key_type = key_type_arg;
870 psa_algorithm_t policy_alg = policy_alg_arg;
871 psa_algorithm_t exercise_alg = exercise_alg_arg;
872 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200873 psa_status_t status;
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200874 psa_status_t expected_status_sign = expected_status_sign_arg;
875 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200876 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200877
Gilles Peskine8817f612018-12-18 00:18:46 +0100878 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200879
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200880 psa_set_key_usage_flags( &attributes, policy_usage );
881 psa_set_key_algorithm( &attributes, policy_alg );
882 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200883
Gilles Peskine049c7532019-05-15 20:22:09 +0200884 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200885 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200886
gabor-mezei-arm659af9e2021-06-29 11:06:16 +0200887 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
888 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200889
Ronald Cron5425a212020-08-04 14:58:35 +0200890 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200891 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100892
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200893 /* Calculate the MAC, one-shot case. */
894 uint8_t input[128] = {0};
895 size_t mac_len;
896 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
897 input, 128,
898 mac, PSA_MAC_MAX_SIZE, &mac_len ),
899 expected_status_sign );
900
901 /* Verify correct MAC, one-shot case. */
902 status = psa_mac_verify( key, exercise_alg, input, 128,
903 mac, mac_len );
904
905 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
906 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
907 else
908 TEST_EQUAL( status, expected_status_verify );
909
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200911
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200913 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200914 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915
916exit:
917 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200918 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200919 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200920}
921/* END_CASE */
922
923/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200924void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200925 int policy_alg,
926 int key_type,
927 data_t *key_data,
928 int exercise_alg )
929{
Ronald Cron5425a212020-08-04 14:58:35 +0200930 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000932 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200933 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200934 psa_status_t status;
935
Gilles Peskine8817f612018-12-18 00:18:46 +0100936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200938 psa_set_key_usage_flags( &attributes, policy_usage );
939 psa_set_key_algorithm( &attributes, policy_alg );
940 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200941
Gilles Peskine049c7532019-05-15 20:22:09 +0200942 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200943 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200944
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200945 /* Check if no key usage flag implication is done */
946 TEST_EQUAL( policy_usage,
947 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200948
Ronald Cron5425a212020-08-04 14:58:35 +0200949 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200950 if( policy_alg == exercise_alg &&
951 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100952 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200953 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100954 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955 psa_cipher_abort( &operation );
956
Ronald Cron5425a212020-08-04 14:58:35 +0200957 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200958 if( policy_alg == exercise_alg &&
959 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100960 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100962 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200963
964exit:
965 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200966 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200967 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968}
969/* END_CASE */
970
971/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200972void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973 int policy_alg,
974 int key_type,
975 data_t *key_data,
976 int nonce_length_arg,
977 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100978 int exercise_alg,
979 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200980{
Ronald Cron5425a212020-08-04 14:58:35 +0200981 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200983 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200984 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100985 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200986 unsigned char nonce[16] = {0};
987 size_t nonce_length = nonce_length_arg;
988 unsigned char tag[16];
989 size_t tag_length = tag_length_arg;
990 size_t output_length;
991
Gilles Peskine47cfdfd2022-04-14 00:12:57 +0200992 TEST_LE_U( nonce_length, sizeof( nonce ) );
993 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200994
Gilles Peskine8817f612018-12-18 00:18:46 +0100995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200996
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200997 psa_set_key_usage_flags( &attributes, policy_usage );
998 psa_set_key_algorithm( &attributes, policy_alg );
999 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001000
Gilles Peskine049c7532019-05-15 20:22:09 +02001001 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001002 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001004 /* Check if no key usage implication is done */
1005 TEST_EQUAL( policy_usage,
1006 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001007
Ronald Cron5425a212020-08-04 14:58:35 +02001008 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001009 nonce, nonce_length,
1010 NULL, 0,
1011 NULL, 0,
1012 tag, tag_length,
1013 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001014 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1015 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001017 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001018
1019 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001020 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001021 nonce, nonce_length,
1022 NULL, 0,
1023 tag, tag_length,
1024 NULL, 0,
1025 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001026 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1027 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1028 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001029 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001030 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001031 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032
1033exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001034 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001035 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001036}
1037/* END_CASE */
1038
1039/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001040void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001041 int policy_alg,
1042 int key_type,
1043 data_t *key_data,
1044 int exercise_alg )
1045{
Ronald Cron5425a212020-08-04 14:58:35 +02001046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001048 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001049 psa_status_t status;
1050 size_t key_bits;
1051 size_t buffer_length;
1052 unsigned char *buffer = NULL;
1053 size_t output_length;
1054
Gilles Peskine8817f612018-12-18 00:18:46 +01001055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001056
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001057 psa_set_key_usage_flags( &attributes, policy_usage );
1058 psa_set_key_algorithm( &attributes, policy_alg );
1059 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001060
Gilles Peskine049c7532019-05-15 20:22:09 +02001061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001062 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001063
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001064 /* Check if no key usage implication is done */
1065 TEST_EQUAL( policy_usage,
1066 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001067
Ronald Cron5425a212020-08-04 14:58:35 +02001068 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001069 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001070 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1071 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001072 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001073
Ronald Cron5425a212020-08-04 14:58:35 +02001074 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001075 NULL, 0,
1076 NULL, 0,
1077 buffer, buffer_length,
1078 &output_length );
1079 if( policy_alg == exercise_alg &&
1080 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001081 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001082 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001083 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001084
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001085 if( buffer_length != 0 )
1086 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001087 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088 buffer, buffer_length,
1089 NULL, 0,
1090 buffer, buffer_length,
1091 &output_length );
1092 if( policy_alg == exercise_alg &&
1093 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001094 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001095 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001096 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097
1098exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001099 /*
1100 * Key attributes may have been returned by psa_get_key_attributes()
1101 * thus reset them as required.
1102 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001103 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001104
1105 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001106 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001107 mbedtls_free( buffer );
1108}
1109/* END_CASE */
1110
1111/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001112void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001113 int policy_alg,
1114 int key_type,
1115 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001116 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001117 int payload_length_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001118 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001119{
Ronald Cron5425a212020-08-04 14:58:35 +02001120 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001122 psa_key_usage_t policy_usage = policy_usage_arg;
1123 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001124 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001125 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1126 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1127 * compatible with the policy and `payload_length_arg` is supposed to be
1128 * a valid input length to sign. If `payload_length_arg <= 0`,
1129 * `exercise_alg` is supposed to be forbidden by the policy. */
1130 int compatible_alg = payload_length_arg > 0;
1131 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001132 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 size_t signature_length;
1134
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001135 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001136 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001137 TEST_EQUAL( expected_usage,
1138 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001139
Gilles Peskine8817f612018-12-18 00:18:46 +01001140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001142 psa_set_key_usage_flags( &attributes, policy_usage );
1143 psa_set_key_algorithm( &attributes, policy_alg );
1144 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001145
Gilles Peskine049c7532019-05-15 20:22:09 +02001146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001147 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001148
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001149 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1150
Ronald Cron5425a212020-08-04 14:58:35 +02001151 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001152 payload, payload_length,
1153 signature, sizeof( signature ),
1154 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001155 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001156 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001157 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001158 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159
1160 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001161 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001162 payload, payload_length,
1163 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001164 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001165 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001166 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001167 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001168
Gilles Peskine8cb22c82021-09-22 16:15:05 +02001169 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-arm79df41d2021-06-28 14:53:49 +02001170 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001171 {
1172 status = psa_sign_message( key, exercise_alg,
1173 payload, payload_length,
1174 signature, sizeof( signature ),
1175 &signature_length );
1176 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1177 PSA_ASSERT( status );
1178 else
1179 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1180
1181 memset( signature, 0, sizeof( signature ) );
1182 status = psa_verify_message( key, exercise_alg,
1183 payload, payload_length,
1184 signature, sizeof( signature ) );
1185 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1186 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1187 else
1188 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1189 }
1190
Gilles Peskined5b33222018-06-18 22:20:03 +02001191exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001192 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001193 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001194}
1195/* END_CASE */
1196
Janos Follathba3fab92019-06-11 14:50:16 +01001197/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001198void derive_key_policy( int policy_usage,
1199 int policy_alg,
1200 int key_type,
1201 data_t *key_data,
1202 int exercise_alg )
1203{
Ronald Cron5425a212020-08-04 14:58:35 +02001204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001206 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001207 psa_status_t status;
1208
Gilles Peskine8817f612018-12-18 00:18:46 +01001209 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001210
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001211 psa_set_key_usage_flags( &attributes, policy_usage );
1212 psa_set_key_algorithm( &attributes, policy_alg );
1213 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001214
Gilles Peskine049c7532019-05-15 20:22:09 +02001215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001216 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001217
Janos Follathba3fab92019-06-11 14:50:16 +01001218 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1219
1220 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1221 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001222 {
Janos Follathba3fab92019-06-11 14:50:16 +01001223 PSA_ASSERT( psa_key_derivation_input_bytes(
1224 &operation,
1225 PSA_KEY_DERIVATION_INPUT_SEED,
1226 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001227 }
Janos Follathba3fab92019-06-11 14:50:16 +01001228
1229 status = psa_key_derivation_input_key( &operation,
1230 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001231 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001232
Gilles Peskineea0fb492018-07-12 17:17:20 +02001233 if( policy_alg == exercise_alg &&
1234 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001235 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001236 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001237 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001238
1239exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001240 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001241 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001242 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001243}
1244/* END_CASE */
1245
1246/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001247void agreement_key_policy( int policy_usage,
1248 int policy_alg,
1249 int key_type_arg,
1250 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001251 int exercise_alg,
1252 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001253{
Ronald Cron5425a212020-08-04 14:58:35 +02001254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001256 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001258 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001260
Gilles Peskine8817f612018-12-18 00:18:46 +01001261 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001262
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001263 psa_set_key_usage_flags( &attributes, policy_usage );
1264 psa_set_key_algorithm( &attributes, policy_alg );
1265 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001266
Gilles Peskine049c7532019-05-15 20:22:09 +02001267 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001268 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001269
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001270 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001271 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001272
Steven Cooremance48e852020-10-05 16:02:45 +02001273 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001274
1275exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001276 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001277 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001278 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001279}
1280/* END_CASE */
1281
1282/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001283void key_policy_alg2( int key_type_arg, data_t *key_data,
1284 int usage_arg, int alg_arg, int alg2_arg )
1285{
Ronald Cron5425a212020-08-04 14:58:35 +02001286 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001287 psa_key_type_t key_type = key_type_arg;
1288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1289 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1290 psa_key_usage_t usage = usage_arg;
1291 psa_algorithm_t alg = alg_arg;
1292 psa_algorithm_t alg2 = alg2_arg;
1293
1294 PSA_ASSERT( psa_crypto_init( ) );
1295
1296 psa_set_key_usage_flags( &attributes, usage );
1297 psa_set_key_algorithm( &attributes, alg );
1298 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1299 psa_set_key_type( &attributes, key_type );
1300 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001301 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001302
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001303 /* Update the usage flags to obtain implicit usage flags */
1304 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001305 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001306 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1307 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1308 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1309
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001310 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001311 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001312 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001313 goto exit;
1314
1315exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001316 /*
1317 * Key attributes may have been returned by psa_get_key_attributes()
1318 * thus reset them as required.
1319 */
1320 psa_reset_key_attributes( &got_attributes );
1321
Ronald Cron5425a212020-08-04 14:58:35 +02001322 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001323 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001324}
1325/* END_CASE */
1326
1327/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001328void raw_agreement_key_policy( int policy_usage,
1329 int policy_alg,
1330 int key_type_arg,
1331 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001332 int exercise_alg,
1333 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001334{
Ronald Cron5425a212020-08-04 14:58:35 +02001335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001336 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001337 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001338 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001339 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001340 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001341
1342 PSA_ASSERT( psa_crypto_init( ) );
1343
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001344 psa_set_key_usage_flags( &attributes, policy_usage );
1345 psa_set_key_algorithm( &attributes, policy_alg );
1346 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001347
Gilles Peskine049c7532019-05-15 20:22:09 +02001348 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001349 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001350
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001351 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001352
Steven Cooremance48e852020-10-05 16:02:45 +02001353 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001354
1355exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001356 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001357 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001358 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001359}
1360/* END_CASE */
1361
1362/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001363void copy_success( int source_usage_arg,
1364 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001365 int type_arg, data_t *material,
1366 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001367 int target_usage_arg,
1368 int target_alg_arg, int target_alg2_arg,
1369 int expected_usage_arg,
1370 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001371{
Gilles Peskineca25db92019-04-19 11:43:08 +02001372 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1373 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001374 psa_key_usage_t expected_usage = expected_usage_arg;
1375 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001376 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001377 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1378 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001379 uint8_t *export_buffer = NULL;
1380
Gilles Peskine57ab7212019-01-28 13:03:09 +01001381 PSA_ASSERT( psa_crypto_init( ) );
1382
Gilles Peskineca25db92019-04-19 11:43:08 +02001383 /* Prepare the source key. */
1384 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1385 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001386 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001387 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001388 PSA_ASSERT( psa_import_key( &source_attributes,
1389 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001390 &source_key ) );
1391 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001392
Gilles Peskineca25db92019-04-19 11:43:08 +02001393 /* Prepare the target attributes. */
1394 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001395 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001396 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001397 /* Set volatile lifetime to reset the key identifier to 0. */
1398 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1399 }
1400
Gilles Peskineca25db92019-04-19 11:43:08 +02001401 if( target_usage_arg != -1 )
1402 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1403 if( target_alg_arg != -1 )
1404 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001405 if( target_alg2_arg != -1 )
1406 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001407
1408 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001409 PSA_ASSERT( psa_copy_key( source_key,
1410 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001411
1412 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001413 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001414
1415 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001416 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001417 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1418 psa_get_key_type( &target_attributes ) );
1419 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1420 psa_get_key_bits( &target_attributes ) );
1421 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1422 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001423 TEST_EQUAL( expected_alg2,
1424 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001425 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1426 {
1427 size_t length;
1428 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001429 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001430 material->len, &length ) );
1431 ASSERT_COMPARE( material->x, material->len,
1432 export_buffer, length );
1433 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001434
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001435 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001436 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001437 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001438 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001439
Ronald Cron5425a212020-08-04 14:58:35 +02001440 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001441
1442exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001443 /*
1444 * Source and target key attributes may have been returned by
1445 * psa_get_key_attributes() thus reset them as required.
1446 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001447 psa_reset_key_attributes( &source_attributes );
1448 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001449
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001450 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001451 mbedtls_free( export_buffer );
1452}
1453/* END_CASE */
1454
1455/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001456void copy_fail( int source_usage_arg,
1457 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001458 int type_arg, data_t *material,
1459 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001460 int target_usage_arg,
1461 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001462 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001463 int expected_status_arg )
1464{
1465 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1466 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001467 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1468 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001469 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001470
1471 PSA_ASSERT( psa_crypto_init( ) );
1472
1473 /* Prepare the source key. */
1474 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1475 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001476 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001477 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001478 PSA_ASSERT( psa_import_key( &source_attributes,
1479 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001480 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001481
1482 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001483 psa_set_key_id( &target_attributes, key_id );
1484 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001485 psa_set_key_type( &target_attributes, target_type_arg );
1486 psa_set_key_bits( &target_attributes, target_bits_arg );
1487 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1488 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001489 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001490
1491 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001492 TEST_EQUAL( psa_copy_key( source_key,
1493 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001494 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001495
Ronald Cron5425a212020-08-04 14:58:35 +02001496 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001497
Gilles Peskine4a644642019-05-03 17:14:08 +02001498exit:
1499 psa_reset_key_attributes( &source_attributes );
1500 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001501 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001502}
1503/* END_CASE */
1504
1505/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001506void hash_operation_init( )
1507{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001508 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001509 /* Test each valid way of initializing the object, except for `= {0}`, as
1510 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1511 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001512 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001513 psa_hash_operation_t func = psa_hash_operation_init( );
1514 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1515 psa_hash_operation_t zero;
1516
1517 memset( &zero, 0, sizeof( zero ) );
1518
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001519 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001520 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1521 PSA_ERROR_BAD_STATE );
1522 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1523 PSA_ERROR_BAD_STATE );
1524 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1525 PSA_ERROR_BAD_STATE );
1526
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001527 /* A default hash operation should be abortable without error. */
1528 PSA_ASSERT( psa_hash_abort( &func ) );
1529 PSA_ASSERT( psa_hash_abort( &init ) );
1530 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001531}
1532/* END_CASE */
1533
1534/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001535void hash_setup( int alg_arg,
1536 int expected_status_arg )
1537{
1538 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001539 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001540 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001541 psa_status_t status;
1542
Gilles Peskine8817f612018-12-18 00:18:46 +01001543 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001544
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001545 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001546 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001547
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001548 /* Whether setup succeeded or failed, abort must succeed. */
1549 PSA_ASSERT( psa_hash_abort( &operation ) );
1550
1551 /* If setup failed, reproduce the failure, so as to
1552 * test the resulting state of the operation object. */
1553 if( status != PSA_SUCCESS )
1554 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1555
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001556 /* Now the operation object should be reusable. */
1557#if defined(KNOWN_SUPPORTED_HASH_ALG)
1558 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1559 PSA_ASSERT( psa_hash_abort( &operation ) );
1560#endif
1561
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001562exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001563 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001564}
1565/* END_CASE */
1566
1567/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001568void hash_compute_fail( int alg_arg, data_t *input,
1569 int output_size_arg, int expected_status_arg )
1570{
1571 psa_algorithm_t alg = alg_arg;
1572 uint8_t *output = NULL;
1573 size_t output_size = output_size_arg;
1574 size_t output_length = INVALID_EXPORT_LENGTH;
1575 psa_status_t expected_status = expected_status_arg;
1576 psa_status_t status;
1577
1578 ASSERT_ALLOC( output, output_size );
1579
1580 PSA_ASSERT( psa_crypto_init( ) );
1581
1582 status = psa_hash_compute( alg, input->x, input->len,
1583 output, output_size, &output_length );
1584 TEST_EQUAL( status, expected_status );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02001585 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001586
1587exit:
1588 mbedtls_free( output );
1589 PSA_DONE( );
1590}
1591/* END_CASE */
1592
1593/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001594void hash_compare_fail( int alg_arg, data_t *input,
1595 data_t *reference_hash,
1596 int expected_status_arg )
1597{
1598 psa_algorithm_t alg = alg_arg;
1599 psa_status_t expected_status = expected_status_arg;
1600 psa_status_t status;
1601
1602 PSA_ASSERT( psa_crypto_init( ) );
1603
1604 status = psa_hash_compare( alg, input->x, input->len,
1605 reference_hash->x, reference_hash->len );
1606 TEST_EQUAL( status, expected_status );
1607
1608exit:
1609 PSA_DONE( );
1610}
1611/* END_CASE */
1612
1613/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001614void hash_compute_compare( int alg_arg, data_t *input,
1615 data_t *expected_output )
1616{
1617 psa_algorithm_t alg = alg_arg;
1618 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1619 size_t output_length = INVALID_EXPORT_LENGTH;
1620 size_t i;
1621
1622 PSA_ASSERT( psa_crypto_init( ) );
1623
1624 /* Compute with tight buffer */
1625 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001626 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001627 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001628 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001629 ASSERT_COMPARE( output, output_length,
1630 expected_output->x, expected_output->len );
1631
1632 /* Compute with larger buffer */
1633 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1634 output, sizeof( output ),
1635 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001636 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001637 ASSERT_COMPARE( output, output_length,
1638 expected_output->x, expected_output->len );
1639
1640 /* Compare with correct hash */
1641 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1642 output, output_length ) );
1643
1644 /* Compare with trailing garbage */
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 truncated hash */
1650 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1651 output, output_length - 1 ),
1652 PSA_ERROR_INVALID_SIGNATURE );
1653
1654 /* Compare with corrupted value */
1655 for( i = 0; i < output_length; i++ )
1656 {
Chris Jones9634bb12021-01-20 15:56:42 +00001657 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001658 output[i] ^= 1;
1659 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1660 output, output_length ),
1661 PSA_ERROR_INVALID_SIGNATURE );
1662 output[i] ^= 1;
1663 }
1664
1665exit:
1666 PSA_DONE( );
1667}
1668/* END_CASE */
1669
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001670/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001671void hash_bad_order( )
1672{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001673 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001674 unsigned char input[] = "";
1675 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001676 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001677 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1678 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1679 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001680 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001681 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001682 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001683
Gilles Peskine8817f612018-12-18 00:18:46 +01001684 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001685
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001686 /* Call setup twice in a row. */
1687 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001688 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001689 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1690 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001691 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001692 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001693 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001694
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001695 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001696 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001697 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001698 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001699
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001700 /* Check that update calls abort on error. */
1701 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman54f73512021-06-24 18:14:52 +01001702 operation.id = UINT_MAX;
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001703 ASSERT_OPERATION_IS_ACTIVE( operation );
1704 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1705 PSA_ERROR_BAD_STATE );
1706 ASSERT_OPERATION_IS_INACTIVE( operation );
1707 PSA_ASSERT( psa_hash_abort( &operation ) );
1708 ASSERT_OPERATION_IS_INACTIVE( operation );
1709
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001710 /* Call update after finish. */
1711 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1712 PSA_ASSERT( psa_hash_finish( &operation,
1713 hash, sizeof( hash ), &hash_len ) );
1714 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001715 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001716 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001717
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001718 /* Call verify without calling setup beforehand. */
1719 TEST_EQUAL( psa_hash_verify( &operation,
1720 valid_hash, sizeof( valid_hash ) ),
1721 PSA_ERROR_BAD_STATE );
1722 PSA_ASSERT( psa_hash_abort( &operation ) );
1723
1724 /* Call verify after finish. */
1725 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1726 PSA_ASSERT( psa_hash_finish( &operation,
1727 hash, sizeof( hash ), &hash_len ) );
1728 TEST_EQUAL( psa_hash_verify( &operation,
1729 valid_hash, sizeof( valid_hash ) ),
1730 PSA_ERROR_BAD_STATE );
1731 PSA_ASSERT( psa_hash_abort( &operation ) );
1732
1733 /* Call verify twice in a row. */
1734 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001735 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001736 PSA_ASSERT( psa_hash_verify( &operation,
1737 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001738 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001739 TEST_EQUAL( psa_hash_verify( &operation,
1740 valid_hash, sizeof( valid_hash ) ),
1741 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001742 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001743 PSA_ASSERT( psa_hash_abort( &operation ) );
1744
1745 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001746 TEST_EQUAL( psa_hash_finish( &operation,
1747 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001748 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001749 PSA_ASSERT( psa_hash_abort( &operation ) );
1750
1751 /* Call finish twice in a row. */
1752 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1753 PSA_ASSERT( psa_hash_finish( &operation,
1754 hash, sizeof( hash ), &hash_len ) );
1755 TEST_EQUAL( psa_hash_finish( &operation,
1756 hash, sizeof( hash ), &hash_len ),
1757 PSA_ERROR_BAD_STATE );
1758 PSA_ASSERT( psa_hash_abort( &operation ) );
1759
1760 /* Call finish after calling verify. */
1761 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1762 PSA_ASSERT( psa_hash_verify( &operation,
1763 valid_hash, sizeof( valid_hash ) ) );
1764 TEST_EQUAL( psa_hash_finish( &operation,
1765 hash, sizeof( hash ), &hash_len ),
1766 PSA_ERROR_BAD_STATE );
1767 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001768
1769exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001770 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001771}
1772/* END_CASE */
1773
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001774/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001775void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001776{
1777 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001778 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1779 * appended to it */
1780 unsigned char hash[] = {
1781 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1782 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1783 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001784 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001785 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001786
Gilles Peskine8817f612018-12-18 00:18:46 +01001787 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001788
itayzafrir27e69452018-11-01 14:26:34 +02001789 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001790 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001791 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001792 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001793 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001794 ASSERT_OPERATION_IS_INACTIVE( operation );
1795 PSA_ASSERT( psa_hash_abort( &operation ) );
1796 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001797
itayzafrir27e69452018-11-01 14:26:34 +02001798 /* psa_hash_verify with a non-matching hash */
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 + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001801 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001802
itayzafrir27e69452018-11-01 14:26:34 +02001803 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001804 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001805 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001806 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001807
itayzafrirec93d302018-10-18 18:01:10 +03001808exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001809 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001810}
1811/* END_CASE */
1812
Ronald Cronee414c72021-03-18 18:50:08 +01001813/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001814void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001815{
1816 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001817 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001818 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001819 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001820 size_t hash_len;
1821
Gilles Peskine8817f612018-12-18 00:18:46 +01001822 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001823
itayzafrir58028322018-10-25 10:22:01 +03001824 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001825 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001826 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001827 hash, expected_size - 1, &hash_len ),
1828 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001829
1830exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001831 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001832}
1833/* END_CASE */
1834
Ronald Cronee414c72021-03-18 18:50:08 +01001835/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001836void hash_clone_source_state( )
1837{
1838 psa_algorithm_t alg = PSA_ALG_SHA_256;
1839 unsigned char hash[PSA_HASH_MAX_SIZE];
1840 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1841 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1842 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1843 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1844 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1845 size_t hash_len;
1846
1847 PSA_ASSERT( psa_crypto_init( ) );
1848 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1849
1850 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1851 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1852 PSA_ASSERT( psa_hash_finish( &op_finished,
1853 hash, sizeof( hash ), &hash_len ) );
1854 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1855 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1856
1857 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1858 PSA_ERROR_BAD_STATE );
1859
1860 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1861 PSA_ASSERT( psa_hash_finish( &op_init,
1862 hash, sizeof( hash ), &hash_len ) );
1863 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1864 PSA_ASSERT( psa_hash_finish( &op_finished,
1865 hash, sizeof( hash ), &hash_len ) );
1866 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1867 PSA_ASSERT( psa_hash_finish( &op_aborted,
1868 hash, sizeof( hash ), &hash_len ) );
1869
1870exit:
1871 psa_hash_abort( &op_source );
1872 psa_hash_abort( &op_init );
1873 psa_hash_abort( &op_setup );
1874 psa_hash_abort( &op_finished );
1875 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001876 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001877}
1878/* END_CASE */
1879
Ronald Cronee414c72021-03-18 18:50:08 +01001880/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001881void hash_clone_target_state( )
1882{
1883 psa_algorithm_t alg = PSA_ALG_SHA_256;
1884 unsigned char hash[PSA_HASH_MAX_SIZE];
1885 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1886 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1887 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1888 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1889 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1890 size_t hash_len;
1891
1892 PSA_ASSERT( psa_crypto_init( ) );
1893
1894 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1895 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1896 PSA_ASSERT( psa_hash_finish( &op_finished,
1897 hash, sizeof( hash ), &hash_len ) );
1898 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1899 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1900
1901 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1902 PSA_ASSERT( psa_hash_finish( &op_target,
1903 hash, sizeof( hash ), &hash_len ) );
1904
1905 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1906 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1907 PSA_ERROR_BAD_STATE );
1908 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1909 PSA_ERROR_BAD_STATE );
1910
1911exit:
1912 psa_hash_abort( &op_target );
1913 psa_hash_abort( &op_init );
1914 psa_hash_abort( &op_setup );
1915 psa_hash_abort( &op_finished );
1916 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001917 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001918}
1919/* END_CASE */
1920
itayzafrir58028322018-10-25 10:22:01 +03001921/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001922void mac_operation_init( )
1923{
Jaeden Amero252ef282019-02-15 14:05:35 +00001924 const uint8_t input[1] = { 0 };
1925
Jaeden Amero769ce272019-01-04 11:48:03 +00001926 /* Test each valid way of initializing the object, except for `= {0}`, as
1927 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1928 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08001929 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00001930 psa_mac_operation_t func = psa_mac_operation_init( );
1931 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1932 psa_mac_operation_t zero;
1933
1934 memset( &zero, 0, sizeof( zero ) );
1935
Jaeden Amero252ef282019-02-15 14:05:35 +00001936 /* A freshly-initialized MAC operation should not be usable. */
1937 TEST_EQUAL( psa_mac_update( &func,
1938 input, sizeof( input ) ),
1939 PSA_ERROR_BAD_STATE );
1940 TEST_EQUAL( psa_mac_update( &init,
1941 input, sizeof( input ) ),
1942 PSA_ERROR_BAD_STATE );
1943 TEST_EQUAL( psa_mac_update( &zero,
1944 input, sizeof( input ) ),
1945 PSA_ERROR_BAD_STATE );
1946
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001947 /* A default MAC operation should be abortable without error. */
1948 PSA_ASSERT( psa_mac_abort( &func ) );
1949 PSA_ASSERT( psa_mac_abort( &init ) );
1950 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001951}
1952/* END_CASE */
1953
1954/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001955void mac_setup( int key_type_arg,
1956 data_t *key,
1957 int alg_arg,
1958 int expected_status_arg )
1959{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001960 psa_key_type_t key_type = key_type_arg;
1961 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001962 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001963 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001964 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1965#if defined(KNOWN_SUPPORTED_MAC_ALG)
1966 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1967#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001968
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001970
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001971 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1972 &operation, &status ) )
1973 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001974 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001975
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001976 /* The operation object should be reusable. */
1977#if defined(KNOWN_SUPPORTED_MAC_ALG)
1978 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1979 smoke_test_key_data,
1980 sizeof( smoke_test_key_data ),
1981 KNOWN_SUPPORTED_MAC_ALG,
1982 &operation, &status ) )
1983 goto exit;
1984 TEST_EQUAL( status, PSA_SUCCESS );
1985#endif
1986
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001987exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001988 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001989}
1990/* END_CASE */
1991
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001992/* 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 +00001993void mac_bad_order( )
1994{
Ronald Cron5425a212020-08-04 14:58:35 +02001995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001996 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1997 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001998 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001999 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2000 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2001 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002003 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2004 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2005 size_t sign_mac_length = 0;
2006 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2007 const uint8_t verify_mac[] = {
2008 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2009 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2010 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2011
2012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002014 psa_set_key_algorithm( &attributes, alg );
2015 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002016
Ronald Cron5425a212020-08-04 14:58:35 +02002017 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2018 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002019
Jaeden Amero252ef282019-02-15 14:05:35 +00002020 /* Call update without calling setup beforehand. */
2021 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2022 PSA_ERROR_BAD_STATE );
2023 PSA_ASSERT( psa_mac_abort( &operation ) );
2024
2025 /* Call sign finish without calling setup beforehand. */
2026 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2027 &sign_mac_length),
2028 PSA_ERROR_BAD_STATE );
2029 PSA_ASSERT( psa_mac_abort( &operation ) );
2030
2031 /* Call verify finish without calling setup beforehand. */
2032 TEST_EQUAL( psa_mac_verify_finish( &operation,
2033 verify_mac, sizeof( verify_mac ) ),
2034 PSA_ERROR_BAD_STATE );
2035 PSA_ASSERT( psa_mac_abort( &operation ) );
2036
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002037 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002038 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002039 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002040 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002041 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002042 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002043 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002044 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002045
Jaeden Amero252ef282019-02-15 14:05:35 +00002046 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002047 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002048 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2049 PSA_ASSERT( psa_mac_sign_finish( &operation,
2050 sign_mac, sizeof( sign_mac ),
2051 &sign_mac_length ) );
2052 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2053 PSA_ERROR_BAD_STATE );
2054 PSA_ASSERT( psa_mac_abort( &operation ) );
2055
2056 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002057 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002058 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2059 PSA_ASSERT( psa_mac_verify_finish( &operation,
2060 verify_mac, sizeof( verify_mac ) ) );
2061 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2062 PSA_ERROR_BAD_STATE );
2063 PSA_ASSERT( psa_mac_abort( &operation ) );
2064
2065 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002066 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002067 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2068 PSA_ASSERT( psa_mac_sign_finish( &operation,
2069 sign_mac, sizeof( sign_mac ),
2070 &sign_mac_length ) );
2071 TEST_EQUAL( psa_mac_sign_finish( &operation,
2072 sign_mac, sizeof( sign_mac ),
2073 &sign_mac_length ),
2074 PSA_ERROR_BAD_STATE );
2075 PSA_ASSERT( psa_mac_abort( &operation ) );
2076
2077 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002078 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002079 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2080 PSA_ASSERT( psa_mac_verify_finish( &operation,
2081 verify_mac, sizeof( verify_mac ) ) );
2082 TEST_EQUAL( psa_mac_verify_finish( &operation,
2083 verify_mac, sizeof( verify_mac ) ),
2084 PSA_ERROR_BAD_STATE );
2085 PSA_ASSERT( psa_mac_abort( &operation ) );
2086
2087 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002088 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002089 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002090 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002091 TEST_EQUAL( psa_mac_verify_finish( &operation,
2092 verify_mac, sizeof( verify_mac ) ),
2093 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002094 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002095 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002096 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002097
2098 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002099 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002100 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002101 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002102 TEST_EQUAL( psa_mac_sign_finish( &operation,
2103 sign_mac, sizeof( sign_mac ),
2104 &sign_mac_length ),
2105 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002106 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002107 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002108 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002109
Ronald Cron5425a212020-08-04 14:58:35 +02002110 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002111
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002113 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002114}
2115/* END_CASE */
2116
2117/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002118void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002119 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002120 int alg_arg,
2121 data_t *input,
2122 data_t *expected_mac )
2123{
Ronald Cron5425a212020-08-04 14:58:35 +02002124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002125 psa_key_type_t key_type = key_type_arg;
2126 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002129 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002130 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002131 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002132 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002133 const size_t output_sizes_to_test[] = {
2134 0,
2135 1,
2136 expected_mac->len - 1,
2137 expected_mac->len,
2138 expected_mac->len + 1,
2139 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002140
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002141 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002142 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002143 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002144
Gilles Peskine8817f612018-12-18 00:18:46 +01002145 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002146
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002147 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002148 psa_set_key_algorithm( &attributes, alg );
2149 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002150
Ronald Cron5425a212020-08-04 14:58:35 +02002151 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2152 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002153
Gilles Peskine8b356b52020-08-25 23:44:59 +02002154 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2155 {
2156 const size_t output_size = output_sizes_to_test[i];
2157 psa_status_t expected_status =
2158 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2159 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002160
Chris Jones9634bb12021-01-20 15:56:42 +00002161 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002162 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002163
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002164 /* Calculate the MAC, one-shot case. */
2165 TEST_EQUAL( psa_mac_compute( key, alg,
2166 input->x, input->len,
2167 actual_mac, output_size, &mac_length ),
2168 expected_status );
2169 if( expected_status == PSA_SUCCESS )
2170 {
2171 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2172 actual_mac, mac_length );
2173 }
2174
2175 if( output_size > 0 )
2176 memset( actual_mac, 0, output_size );
2177
2178 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002179 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002180 PSA_ASSERT( psa_mac_update( &operation,
2181 input->x, input->len ) );
2182 TEST_EQUAL( psa_mac_sign_finish( &operation,
2183 actual_mac, output_size,
2184 &mac_length ),
2185 expected_status );
2186 PSA_ASSERT( psa_mac_abort( &operation ) );
2187
2188 if( expected_status == PSA_SUCCESS )
2189 {
2190 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2191 actual_mac, mac_length );
2192 }
2193 mbedtls_free( actual_mac );
2194 actual_mac = NULL;
2195 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002196
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002197exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002198 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002199 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002200 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002201 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002202}
2203/* END_CASE */
2204
2205/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002206void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002207 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002208 int alg_arg,
2209 data_t *input,
2210 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002211{
Ronald Cron5425a212020-08-04 14:58:35 +02002212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002213 psa_key_type_t key_type = key_type_arg;
2214 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002215 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002217 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002218
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002219 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02002220
Gilles Peskine8817f612018-12-18 00:18:46 +01002221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002222
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002223 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002224 psa_set_key_algorithm( &attributes, alg );
2225 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002226
Ronald Cron5425a212020-08-04 14:58:35 +02002227 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2228 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002229
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002230 /* Verify correct MAC, one-shot case. */
2231 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2232 expected_mac->x, expected_mac->len ) );
2233
2234 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002235 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002236 PSA_ASSERT( psa_mac_update( &operation,
2237 input->x, input->len ) );
2238 PSA_ASSERT( psa_mac_verify_finish( &operation,
2239 expected_mac->x,
2240 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002241
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002242 /* Test a MAC that's too short, one-shot case. */
2243 TEST_EQUAL( psa_mac_verify( key, alg,
2244 input->x, input->len,
2245 expected_mac->x,
2246 expected_mac->len - 1 ),
2247 PSA_ERROR_INVALID_SIGNATURE );
2248
2249 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002250 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002251 PSA_ASSERT( psa_mac_update( &operation,
2252 input->x, input->len ) );
2253 TEST_EQUAL( psa_mac_verify_finish( &operation,
2254 expected_mac->x,
2255 expected_mac->len - 1 ),
2256 PSA_ERROR_INVALID_SIGNATURE );
2257
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002258 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002259 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2260 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002261 TEST_EQUAL( psa_mac_verify( key, alg,
2262 input->x, input->len,
2263 perturbed_mac, expected_mac->len + 1 ),
2264 PSA_ERROR_INVALID_SIGNATURE );
2265
2266 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002267 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002268 PSA_ASSERT( psa_mac_update( &operation,
2269 input->x, input->len ) );
2270 TEST_EQUAL( psa_mac_verify_finish( &operation,
2271 perturbed_mac,
2272 expected_mac->len + 1 ),
2273 PSA_ERROR_INVALID_SIGNATURE );
2274
2275 /* Test changing one byte. */
2276 for( size_t i = 0; i < expected_mac->len; i++ )
2277 {
Chris Jones9634bb12021-01-20 15:56:42 +00002278 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002279 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002280
2281 TEST_EQUAL( psa_mac_verify( key, alg,
2282 input->x, input->len,
2283 perturbed_mac, expected_mac->len ),
2284 PSA_ERROR_INVALID_SIGNATURE );
2285
Ronald Cron5425a212020-08-04 14:58:35 +02002286 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002287 PSA_ASSERT( psa_mac_update( &operation,
2288 input->x, input->len ) );
2289 TEST_EQUAL( psa_mac_verify_finish( &operation,
2290 perturbed_mac,
2291 expected_mac->len ),
2292 PSA_ERROR_INVALID_SIGNATURE );
2293 perturbed_mac[i] ^= 1;
2294 }
2295
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002297 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002298 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002299 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002300 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002301}
2302/* END_CASE */
2303
2304/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002305void cipher_operation_init( )
2306{
Jaeden Ameroab439972019-02-15 14:12:05 +00002307 const uint8_t input[1] = { 0 };
2308 unsigned char output[1] = { 0 };
2309 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002310 /* Test each valid way of initializing the object, except for `= {0}`, as
2311 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2312 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08002313 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002314 psa_cipher_operation_t func = psa_cipher_operation_init( );
2315 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2316 psa_cipher_operation_t zero;
2317
2318 memset( &zero, 0, sizeof( zero ) );
2319
Jaeden Ameroab439972019-02-15 14:12:05 +00002320 /* A freshly-initialized cipher operation should not be usable. */
2321 TEST_EQUAL( psa_cipher_update( &func,
2322 input, sizeof( input ),
2323 output, sizeof( output ),
2324 &output_length ),
2325 PSA_ERROR_BAD_STATE );
2326 TEST_EQUAL( psa_cipher_update( &init,
2327 input, sizeof( input ),
2328 output, sizeof( output ),
2329 &output_length ),
2330 PSA_ERROR_BAD_STATE );
2331 TEST_EQUAL( psa_cipher_update( &zero,
2332 input, sizeof( input ),
2333 output, sizeof( output ),
2334 &output_length ),
2335 PSA_ERROR_BAD_STATE );
2336
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002337 /* A default cipher operation should be abortable without error. */
2338 PSA_ASSERT( psa_cipher_abort( &func ) );
2339 PSA_ASSERT( psa_cipher_abort( &init ) );
2340 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002341}
2342/* END_CASE */
2343
2344/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345void cipher_setup( int key_type_arg,
2346 data_t *key,
2347 int alg_arg,
2348 int expected_status_arg )
2349{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002350 psa_key_type_t key_type = key_type_arg;
2351 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002352 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002353 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002354 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002355#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002356 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2357#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002358
Gilles Peskine8817f612018-12-18 00:18:46 +01002359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002360
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002361 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2362 &operation, &status ) )
2363 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002364 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002365
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002366 /* The operation object should be reusable. */
2367#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2368 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2369 smoke_test_key_data,
2370 sizeof( smoke_test_key_data ),
2371 KNOWN_SUPPORTED_CIPHER_ALG,
2372 &operation, &status ) )
2373 goto exit;
2374 TEST_EQUAL( status, PSA_SUCCESS );
2375#endif
2376
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002377exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002378 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002379 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002380}
2381/* END_CASE */
2382
Ronald Cronee414c72021-03-18 18:50:08 +01002383/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002384void cipher_bad_order( )
2385{
Ronald Cron5425a212020-08-04 14:58:35 +02002386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002387 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2388 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002390 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002391 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002392 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002393 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2394 0xaa, 0xaa, 0xaa, 0xaa };
2395 const uint8_t text[] = {
2396 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2397 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002398 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002399 size_t length = 0;
2400
2401 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002402 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2403 psa_set_key_algorithm( &attributes, alg );
2404 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002405 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2406 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002407
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002408 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002409 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002410 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002411 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002412 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002413 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002414 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002415 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002416
2417 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002418 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002419 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002420 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002421 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002422 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002423 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002424 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002425
Jaeden Ameroab439972019-02-15 14:12:05 +00002426 /* Generate an IV without calling setup beforehand. */
2427 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2428 buffer, sizeof( buffer ),
2429 &length ),
2430 PSA_ERROR_BAD_STATE );
2431 PSA_ASSERT( psa_cipher_abort( &operation ) );
2432
2433 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002434 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002435 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2436 buffer, sizeof( buffer ),
2437 &length ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002438 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002439 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2440 buffer, sizeof( buffer ),
2441 &length ),
2442 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002443 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002444 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002445 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002446
2447 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002448 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002449 PSA_ASSERT( psa_cipher_set_iv( &operation,
2450 iv, sizeof( iv ) ) );
2451 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2452 buffer, sizeof( buffer ),
2453 &length ),
2454 PSA_ERROR_BAD_STATE );
2455 PSA_ASSERT( psa_cipher_abort( &operation ) );
2456
2457 /* Set an IV without calling setup beforehand. */
2458 TEST_EQUAL( psa_cipher_set_iv( &operation,
2459 iv, sizeof( iv ) ),
2460 PSA_ERROR_BAD_STATE );
2461 PSA_ASSERT( psa_cipher_abort( &operation ) );
2462
2463 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002464 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002465 PSA_ASSERT( psa_cipher_set_iv( &operation,
2466 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002467 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002468 TEST_EQUAL( psa_cipher_set_iv( &operation,
2469 iv, sizeof( iv ) ),
2470 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002471 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002472 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002473 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002474
2475 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002476 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002477 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2478 buffer, sizeof( buffer ),
2479 &length ) );
2480 TEST_EQUAL( psa_cipher_set_iv( &operation,
2481 iv, sizeof( iv ) ),
2482 PSA_ERROR_BAD_STATE );
2483 PSA_ASSERT( psa_cipher_abort( &operation ) );
2484
2485 /* Call update without calling setup beforehand. */
2486 TEST_EQUAL( psa_cipher_update( &operation,
2487 text, sizeof( text ),
2488 buffer, sizeof( buffer ),
2489 &length ),
2490 PSA_ERROR_BAD_STATE );
2491 PSA_ASSERT( psa_cipher_abort( &operation ) );
2492
2493 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002494 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002495 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002496 TEST_EQUAL( psa_cipher_update( &operation,
2497 text, sizeof( text ),
2498 buffer, sizeof( buffer ),
2499 &length ),
2500 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002501 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002502 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002503 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002504
2505 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002506 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002507 PSA_ASSERT( psa_cipher_set_iv( &operation,
2508 iv, sizeof( iv ) ) );
2509 PSA_ASSERT( psa_cipher_finish( &operation,
2510 buffer, sizeof( buffer ), &length ) );
2511 TEST_EQUAL( psa_cipher_update( &operation,
2512 text, sizeof( text ),
2513 buffer, sizeof( buffer ),
2514 &length ),
2515 PSA_ERROR_BAD_STATE );
2516 PSA_ASSERT( psa_cipher_abort( &operation ) );
2517
2518 /* Call finish without calling setup beforehand. */
2519 TEST_EQUAL( psa_cipher_finish( &operation,
2520 buffer, sizeof( buffer ), &length ),
2521 PSA_ERROR_BAD_STATE );
2522 PSA_ASSERT( psa_cipher_abort( &operation ) );
2523
2524 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002525 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002526 /* Not calling update means we are encrypting an empty buffer, which is OK
2527 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002528 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002529 TEST_EQUAL( psa_cipher_finish( &operation,
2530 buffer, sizeof( buffer ), &length ),
2531 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002532 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002533 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002534 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002535
2536 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002537 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002538 PSA_ASSERT( psa_cipher_set_iv( &operation,
2539 iv, sizeof( iv ) ) );
2540 PSA_ASSERT( psa_cipher_finish( &operation,
2541 buffer, sizeof( buffer ), &length ) );
2542 TEST_EQUAL( psa_cipher_finish( &operation,
2543 buffer, sizeof( buffer ), &length ),
2544 PSA_ERROR_BAD_STATE );
2545 PSA_ASSERT( psa_cipher_abort( &operation ) );
2546
Ronald Cron5425a212020-08-04 14:58:35 +02002547 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002548
Jaeden Ameroab439972019-02-15 14:12:05 +00002549exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002550 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002551 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002552}
2553/* END_CASE */
2554
2555/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002556void cipher_encrypt_fail( int alg_arg,
2557 int key_type_arg,
2558 data_t *key_data,
2559 data_t *input,
2560 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002561{
Ronald Cron5425a212020-08-04 14:58:35 +02002562 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002563 psa_status_t status;
2564 psa_key_type_t key_type = key_type_arg;
2565 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002566 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002567 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002568 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002569 size_t output_length = 0;
2570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2571
2572 if ( PSA_ERROR_BAD_STATE != expected_status )
2573 {
2574 PSA_ASSERT( psa_crypto_init( ) );
2575
2576 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2577 psa_set_key_algorithm( &attributes, alg );
2578 psa_set_key_type( &attributes, key_type );
2579
2580 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2581 input->len );
2582 ASSERT_ALLOC( output, output_buffer_size );
2583
2584 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2585 &key ) );
2586 }
2587
2588 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2589 output_buffer_size, &output_length );
2590
2591 TEST_EQUAL( status, expected_status );
2592
2593exit:
2594 mbedtls_free( output );
2595 psa_destroy_key( key );
2596 PSA_DONE( );
2597}
2598/* END_CASE */
2599
2600/* BEGIN_CASE */
Gilles Peskine69d98172022-04-20 17:07:52 +02002601void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
2602 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002603{
2604 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2605 psa_key_type_t key_type = key_type_arg;
2606 psa_algorithm_t alg = alg_arg;
Ronald Cron33c69682021-07-15 09:38:11 +02002607 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2608 uint8_t iv[1] = { 0x5a };
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002609 unsigned char *output = NULL;
2610 size_t output_buffer_size = 0;
Gilles Peskine4da5a852022-04-20 17:09:38 +02002611 size_t output_length, length;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2613
2614 PSA_ASSERT( psa_crypto_init( ) );
2615
Gilles Peskine5f504202022-04-20 16:55:03 +02002616 /* Validate size macros */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002617 TEST_LE_U( ciphertext->len,
2618 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
2619 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine69d98172022-04-20 17:07:52 +02002620 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002621 TEST_LE_U( plaintext->len,
2622 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
2623 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
2624 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine69d98172022-04-20 17:07:52 +02002625
Gilles Peskine5f504202022-04-20 16:55:03 +02002626
2627 /* Set up key and output buffer */
Gilles Peskine69d98172022-04-20 17:07:52 +02002628 psa_set_key_usage_flags( &attributes,
2629 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002630 psa_set_key_algorithm( &attributes, alg );
2631 psa_set_key_type( &attributes, key_type );
Gilles Peskine5f504202022-04-20 16:55:03 +02002632 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2633 &key ) );
Gilles Peskine69d98172022-04-20 17:07:52 +02002634 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2635 plaintext->len );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002636 ASSERT_ALLOC( output, output_buffer_size );
2637
Gilles Peskine5f504202022-04-20 16:55:03 +02002638 /* set_iv() is not allowed */
Ronald Cron33c69682021-07-15 09:38:11 +02002639 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2640 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2641 PSA_ERROR_BAD_STATE );
Gilles Peskine69d98172022-04-20 17:07:52 +02002642 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2643 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2644 PSA_ERROR_BAD_STATE );
Gilles Peskine5f504202022-04-20 16:55:03 +02002645
2646 /* generate_iv() is not allowed */
Ronald Cron33c69682021-07-15 09:38:11 +02002647 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2648 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine4da5a852022-04-20 17:09:38 +02002649 &length ),
Ronald Cron33c69682021-07-15 09:38:11 +02002650 PSA_ERROR_BAD_STATE );
Gilles Peskine69d98172022-04-20 17:07:52 +02002651 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2652 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine4da5a852022-04-20 17:09:38 +02002653 &length ),
Gilles Peskine69d98172022-04-20 17:07:52 +02002654 PSA_ERROR_BAD_STATE );
Ronald Cron33c69682021-07-15 09:38:11 +02002655
Gilles Peskine4da5a852022-04-20 17:09:38 +02002656 /* Multipart encryption */
2657 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2658 output_length = 0;
2659 length = ~0;
2660 PSA_ASSERT( psa_cipher_update( &operation,
2661 plaintext->x, plaintext->len,
2662 output, output_buffer_size,
2663 &length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002664 TEST_LE_U( length, output_buffer_size );
Gilles Peskine4da5a852022-04-20 17:09:38 +02002665 output_length += length;
2666 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine01bf6312022-11-23 14:15:57 +01002667 mbedtls_buffer_offset( output, output_length ),
Gilles Peskine4da5a852022-04-20 17:09:38 +02002668 output_buffer_size - output_length,
2669 &length ) );
2670 output_length += length;
2671 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
2672 output, output_length );
2673
2674 /* Multipart encryption */
2675 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2676 output_length = 0;
2677 length = ~0;
2678 PSA_ASSERT( psa_cipher_update( &operation,
2679 ciphertext->x, ciphertext->len,
2680 output, output_buffer_size,
2681 &length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002682 TEST_LE_U( length, output_buffer_size );
Gilles Peskine4da5a852022-04-20 17:09:38 +02002683 output_length += length;
2684 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine01bf6312022-11-23 14:15:57 +01002685 mbedtls_buffer_offset( output, output_length ),
Gilles Peskine4da5a852022-04-20 17:09:38 +02002686 output_buffer_size - output_length,
2687 &length ) );
2688 output_length += length;
2689 ASSERT_COMPARE( plaintext->x, plaintext->len,
2690 output, output_length );
2691
Gilles Peskine5f504202022-04-20 16:55:03 +02002692 /* One-shot encryption */
Gilles Peskine69d98172022-04-20 17:07:52 +02002693 output_length = ~0;
2694 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
2695 output, output_buffer_size,
2696 &output_length ) );
2697 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
2698 output, output_length );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002699
Gilles Peskine69d98172022-04-20 17:07:52 +02002700 /* One-shot decryption */
2701 output_length = ~0;
2702 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
2703 output, output_buffer_size,
2704 &output_length ) );
2705 ASSERT_COMPARE( plaintext->x, plaintext->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002706 output, output_length );
Gilles Peskine5f504202022-04-20 16:55:03 +02002707
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002708exit:
2709 mbedtls_free( output );
Gilles Peskine5f504202022-04-20 16:55:03 +02002710 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002711 psa_destroy_key( key );
2712 PSA_DONE( );
2713}
2714/* END_CASE */
2715
2716/* BEGIN_CASE */
Paul Elliotted33ef12021-07-14 12:31:21 +01002717void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2718{
2719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2720 psa_algorithm_t alg = alg_arg;
2721 psa_key_type_t key_type = key_type_arg;
2722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2723 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2724 psa_status_t status;
2725
2726 PSA_ASSERT( psa_crypto_init( ) );
2727
2728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2729 psa_set_key_algorithm( &attributes, alg );
2730 psa_set_key_type( &attributes, key_type );
2731
2732 /* Usage of either of these two size macros would cause divide by zero
2733 * with incorrect key types previously. Input length should be irrelevant
2734 * here. */
2735 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2736 0 );
2737 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2738
2739
2740 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2741 &key ) );
2742
2743 /* Should fail due to invalid alg type (to support invalid key type).
2744 * Encrypt or decrypt will end up in the same place. */
2745 status = psa_cipher_encrypt_setup( &operation, key, alg );
2746
2747 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2748
2749exit:
2750 psa_cipher_abort( &operation );
2751 psa_destroy_key( key );
2752 PSA_DONE( );
2753}
2754/* END_CASE */
2755
2756/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002757void cipher_encrypt_validation( int alg_arg,
2758 int key_type_arg,
2759 data_t *key_data,
2760 data_t *input )
2761{
2762 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2763 psa_key_type_t key_type = key_type_arg;
2764 psa_algorithm_t alg = alg_arg;
2765 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2766 unsigned char *output1 = NULL;
2767 size_t output1_buffer_size = 0;
2768 size_t output1_length = 0;
2769 unsigned char *output2 = NULL;
2770 size_t output2_buffer_size = 0;
2771 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002772 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002773 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002777
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2779 psa_set_key_algorithm( &attributes, alg );
2780 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002781
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002782 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2783 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2784 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2785 ASSERT_ALLOC( output1, output1_buffer_size );
2786 ASSERT_ALLOC( output2, output2_buffer_size );
2787
Ronald Cron5425a212020-08-04 14:58:35 +02002788 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2789 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002790
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002791 /* The one-shot cipher encryption uses generated iv so validating
2792 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002793 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2794 output1_buffer_size, &output1_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002795 TEST_LE_U( output1_length,
2796 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2797 TEST_LE_U( output1_length,
2798 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002799
2800 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2801 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002802
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_cipher_update( &operation,
2804 input->x, input->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002805 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002806 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002807 TEST_LE_U( function_output_length,
2808 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2809 TEST_LE_U( function_output_length,
2810 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002811 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002812
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002813 PSA_ASSERT( psa_cipher_finish( &operation,
2814 output2 + output2_length,
2815 output2_buffer_size - output2_length,
2816 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002817 TEST_LE_U( function_output_length,
2818 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2819 TEST_LE_U( function_output_length,
2820 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002821 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002822
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002823 PSA_ASSERT( psa_cipher_abort( &operation ) );
2824 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2825 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002826
Gilles Peskine50e586b2018-06-08 14:28:46 +02002827exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002828 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002829 mbedtls_free( output1 );
2830 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002831 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002832 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002833}
2834/* END_CASE */
2835
2836/* BEGIN_CASE */
2837void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002838 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002839 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002840 int first_part_size_arg,
2841 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002842 data_t *expected_output,
2843 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844{
Ronald Cron5425a212020-08-04 14:58:35 +02002845 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002846 psa_key_type_t key_type = key_type_arg;
2847 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002848 psa_status_t status;
2849 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002850 size_t first_part_size = first_part_size_arg;
2851 size_t output1_length = output1_length_arg;
2852 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002853 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002854 size_t output_buffer_size = 0;
2855 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002856 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002857 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002859
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002861
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2863 psa_set_key_algorithm( &attributes, alg );
2864 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002865
Ronald Cron5425a212020-08-04 14:58:35 +02002866 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2867 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002868
Ronald Cron5425a212020-08-04 14:58:35 +02002869 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002870
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002871 if( iv->len > 0 )
2872 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002873 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002874 }
2875
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002876 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2877 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002878 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002879
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002880 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002881 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2882 output, output_buffer_size,
2883 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002884 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002885 TEST_LE_U( function_output_length,
2886 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2887 TEST_LE_U( function_output_length,
2888 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002889 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002890
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002891 if( first_part_size < input->len )
2892 {
2893 PSA_ASSERT( psa_cipher_update( &operation,
2894 input->x + first_part_size,
2895 input->len - first_part_size,
2896 ( output_buffer_size == 0 ? NULL :
2897 output + total_output_length ),
2898 output_buffer_size - total_output_length,
2899 &function_output_length ) );
2900 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002901 TEST_LE_U( function_output_length,
2902 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2903 alg,
2904 input->len - first_part_size ) );
2905 TEST_LE_U( function_output_length,
2906 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002907 total_output_length += function_output_length;
2908 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002909
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002910 status = psa_cipher_finish( &operation,
2911 ( output_buffer_size == 0 ? NULL :
2912 output + total_output_length ),
2913 output_buffer_size - total_output_length,
2914 &function_output_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002915 TEST_LE_U( function_output_length,
2916 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2917 TEST_LE_U( function_output_length,
2918 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002919 total_output_length += function_output_length;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002920 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002922 if( expected_status == PSA_SUCCESS )
2923 {
2924 PSA_ASSERT( psa_cipher_abort( &operation ) );
2925
2926 ASSERT_COMPARE( expected_output->x, expected_output->len,
2927 output, total_output_length );
2928 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929
2930exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002931 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002932 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002933 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002934 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002935}
2936/* END_CASE */
2937
2938/* BEGIN_CASE */
2939void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002940 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002941 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002942 int first_part_size_arg,
2943 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002944 data_t *expected_output,
2945 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946{
Ronald Cron5425a212020-08-04 14:58:35 +02002947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948 psa_key_type_t key_type = key_type_arg;
2949 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002950 psa_status_t status;
2951 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002952 size_t first_part_size = first_part_size_arg;
2953 size_t output1_length = output1_length_arg;
2954 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002955 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002956 size_t output_buffer_size = 0;
2957 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002958 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002959 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961
Gilles Peskine8817f612018-12-18 00:18:46 +01002962 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002964 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2965 psa_set_key_algorithm( &attributes, alg );
2966 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002967
Ronald Cron5425a212020-08-04 14:58:35 +02002968 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2969 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002970
Ronald Cron5425a212020-08-04 14:58:35 +02002971 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972
Steven Cooreman177deba2020-09-07 17:14:14 +02002973 if( iv->len > 0 )
2974 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002975 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002976 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002977
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002978 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2979 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002980 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002981
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002982 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002983 PSA_ASSERT( psa_cipher_update( &operation,
2984 input->x, first_part_size,
2985 output, output_buffer_size,
2986 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002987 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02002988 TEST_LE_U( function_output_length,
2989 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2990 TEST_LE_U( function_output_length,
2991 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002992 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002993
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002994 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002995 {
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002996 PSA_ASSERT( psa_cipher_update( &operation,
2997 input->x + first_part_size,
2998 input->len - first_part_size,
2999 ( output_buffer_size == 0 ? NULL :
3000 output + total_output_length ),
3001 output_buffer_size - total_output_length,
3002 &function_output_length ) );
3003 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003004 TEST_LE_U( function_output_length,
3005 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3006 alg,
3007 input->len - first_part_size ) );
3008 TEST_LE_U( function_output_length,
3009 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003010 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003011 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003012
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003014 ( output_buffer_size == 0 ? NULL :
3015 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003016 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003017 &function_output_length );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003018 TEST_LE_U( function_output_length,
3019 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3020 TEST_LE_U( function_output_length,
3021 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003022 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003023 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003024
3025 if( expected_status == PSA_SUCCESS )
3026 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003027 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02003028
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003029 ASSERT_COMPARE( expected_output->x, expected_output->len,
3030 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003031 }
3032
Gilles Peskine50e586b2018-06-08 14:28:46 +02003033exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003034 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003035 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003036 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003037 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003038}
3039/* END_CASE */
3040
Gilles Peskine50e586b2018-06-08 14:28:46 +02003041/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02003042void cipher_decrypt_fail( int alg_arg,
3043 int key_type_arg,
3044 data_t *key_data,
3045 data_t *iv,
3046 data_t *input_arg,
3047 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003048{
3049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3050 psa_status_t status;
3051 psa_key_type_t key_type = key_type_arg;
3052 psa_algorithm_t alg = alg_arg;
3053 psa_status_t expected_status = expected_status_arg;
3054 unsigned char *input = NULL;
3055 size_t input_buffer_size = 0;
3056 unsigned char *output = NULL;
3057 size_t output_buffer_size = 0;
3058 size_t output_length = 0;
3059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3060
3061 if ( PSA_ERROR_BAD_STATE != expected_status )
3062 {
3063 PSA_ASSERT( psa_crypto_init( ) );
3064
3065 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3066 psa_set_key_algorithm( &attributes, alg );
3067 psa_set_key_type( &attributes, key_type );
3068
3069 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3070 &key ) );
3071 }
3072
3073 /* Allocate input buffer and copy the iv and the plaintext */
3074 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3075 if ( input_buffer_size > 0 )
3076 {
3077 ASSERT_ALLOC( input, input_buffer_size );
3078 memcpy( input, iv->x, iv->len );
3079 memcpy( input + iv->len, input_arg->x, input_arg->len );
3080 }
3081
3082 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3083 ASSERT_ALLOC( output, output_buffer_size );
3084
3085 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3086 output_buffer_size, &output_length );
3087 TEST_EQUAL( status, expected_status );
3088
3089exit:
3090 mbedtls_free( input );
3091 mbedtls_free( output );
3092 psa_destroy_key( key );
3093 PSA_DONE( );
3094}
3095/* END_CASE */
3096
3097/* BEGIN_CASE */
3098void cipher_decrypt( int alg_arg,
3099 int key_type_arg,
3100 data_t *key_data,
3101 data_t *iv,
3102 data_t *input_arg,
3103 data_t *expected_output )
3104{
3105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3106 psa_key_type_t key_type = key_type_arg;
3107 psa_algorithm_t alg = alg_arg;
3108 unsigned char *input = NULL;
3109 size_t input_buffer_size = 0;
3110 unsigned char *output = NULL;
3111 size_t output_buffer_size = 0;
3112 size_t output_length = 0;
3113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3114
3115 PSA_ASSERT( psa_crypto_init( ) );
3116
3117 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3118 psa_set_key_algorithm( &attributes, alg );
3119 psa_set_key_type( &attributes, key_type );
3120
3121 /* Allocate input buffer and copy the iv and the plaintext */
3122 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3123 if ( input_buffer_size > 0 )
3124 {
3125 ASSERT_ALLOC( input, input_buffer_size );
3126 memcpy( input, iv->x, iv->len );
3127 memcpy( input + iv->len, input_arg->x, input_arg->len );
3128 }
3129
3130 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3131 ASSERT_ALLOC( output, output_buffer_size );
3132
3133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3134 &key ) );
3135
3136 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3137 output_buffer_size, &output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003138 TEST_LE_U( output_length,
3139 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3140 TEST_LE_U( output_length,
3141 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003142
3143 ASSERT_COMPARE( expected_output->x, expected_output->len,
3144 output, output_length );
3145exit:
3146 mbedtls_free( input );
3147 mbedtls_free( output );
3148 psa_destroy_key( key );
3149 PSA_DONE( );
3150}
3151/* END_CASE */
3152
3153/* BEGIN_CASE */
3154void cipher_verify_output( int alg_arg,
3155 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003156 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003157 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003158{
Ronald Cron5425a212020-08-04 14:58:35 +02003159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003160 psa_key_type_t key_type = key_type_arg;
3161 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003162 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003163 size_t output1_size = 0;
3164 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003165 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003166 size_t output2_size = 0;
3167 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003169
Gilles Peskine8817f612018-12-18 00:18:46 +01003170 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003171
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3173 psa_set_key_algorithm( &attributes, alg );
3174 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003175
Ronald Cron5425a212020-08-04 14:58:35 +02003176 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3177 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003178 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003179 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003180
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003181 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3182 output1, output1_size,
3183 &output1_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003184 TEST_LE_U( output1_length,
3185 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3186 TEST_LE_U( output1_length,
3187 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003188
3189 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003190 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003191
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003192 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3193 output2, output2_size,
3194 &output2_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003195 TEST_LE_U( output2_length,
3196 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3197 TEST_LE_U( output2_length,
3198 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003199
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003200 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003201
3202exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003203 mbedtls_free( output1 );
3204 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003205 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003206 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003207}
3208/* END_CASE */
3209
3210/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003211void cipher_verify_output_multipart( int alg_arg,
3212 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003213 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003214 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003215 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003216{
Ronald Cron5425a212020-08-04 14:58:35 +02003217 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003218 psa_key_type_t key_type = key_type_arg;
3219 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003220 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003221 unsigned char iv[16] = {0};
3222 size_t iv_size = 16;
3223 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003224 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003225 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003226 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003227 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003228 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003229 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003230 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003231 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3232 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003233 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003234
Gilles Peskine8817f612018-12-18 00:18:46 +01003235 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003236
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003237 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3238 psa_set_key_algorithm( &attributes, alg );
3239 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003240
Ronald Cron5425a212020-08-04 14:58:35 +02003241 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3242 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003243
Ronald Cron5425a212020-08-04 14:58:35 +02003244 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3245 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003246
Steven Cooreman177deba2020-09-07 17:14:14 +02003247 if( alg != PSA_ALG_ECB_NO_PADDING )
3248 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003249 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3250 iv, iv_size,
3251 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003252 }
3253
gabor-mezei-armceface22021-01-21 12:26:17 +01003254 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003255 TEST_LE_U( output1_buffer_size,
3256 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003257 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003258
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003259 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003260
Gilles Peskine8817f612018-12-18 00:18:46 +01003261 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3262 output1, output1_buffer_size,
3263 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003264 TEST_LE_U( function_output_length,
3265 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3266 TEST_LE_U( function_output_length,
3267 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003268 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003269
Gilles Peskine8817f612018-12-18 00:18:46 +01003270 PSA_ASSERT( psa_cipher_update( &operation1,
3271 input->x + first_part_size,
3272 input->len - first_part_size,
3273 output1, output1_buffer_size,
3274 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003275 TEST_LE_U( function_output_length,
3276 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3277 alg,
3278 input->len - first_part_size ) );
3279 TEST_LE_U( function_output_length,
3280 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003281 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003282
Gilles Peskine8817f612018-12-18 00:18:46 +01003283 PSA_ASSERT( psa_cipher_finish( &operation1,
3284 output1 + output1_length,
3285 output1_buffer_size - output1_length,
3286 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003287 TEST_LE_U( function_output_length,
3288 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3289 TEST_LE_U( function_output_length,
3290 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003291 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003292
Gilles Peskine8817f612018-12-18 00:18:46 +01003293 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003294
Gilles Peskine048b7f02018-06-08 14:20:49 +02003295 output2_buffer_size = output1_length;
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003296 TEST_LE_U( output2_buffer_size,
3297 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3298 TEST_LE_U( output2_buffer_size,
3299 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003300 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003301
Steven Cooreman177deba2020-09-07 17:14:14 +02003302 if( iv_length > 0 )
3303 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003304 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3305 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003306 }
Moran Pekerded84402018-06-06 16:36:50 +03003307
Gilles Peskine8817f612018-12-18 00:18:46 +01003308 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3309 output2, output2_buffer_size,
3310 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003311 TEST_LE_U( function_output_length,
3312 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3313 TEST_LE_U( function_output_length,
3314 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003315 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003316
Gilles Peskine8817f612018-12-18 00:18:46 +01003317 PSA_ASSERT( psa_cipher_update( &operation2,
3318 output1 + first_part_size,
3319 output1_length - first_part_size,
3320 output2, output2_buffer_size,
3321 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003322 TEST_LE_U( function_output_length,
3323 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3324 alg,
3325 output1_length - first_part_size ) );
3326 TEST_LE_U( function_output_length,
3327 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003328 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003329
Gilles Peskine8817f612018-12-18 00:18:46 +01003330 PSA_ASSERT( psa_cipher_finish( &operation2,
3331 output2 + output2_length,
3332 output2_buffer_size - output2_length,
3333 &function_output_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003334 TEST_LE_U( function_output_length,
3335 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3336 TEST_LE_U( function_output_length,
3337 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003338 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003339
Gilles Peskine8817f612018-12-18 00:18:46 +01003340 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003341
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003342 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003343
3344exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003345 psa_cipher_abort( &operation1 );
3346 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003347 mbedtls_free( output1 );
3348 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003349 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003350 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003351}
3352/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003353
Gilles Peskine20035e32018-02-03 22:44:14 +01003354/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003355void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003356 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003357 data_t *nonce,
3358 data_t *additional_data,
3359 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003360 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003361{
Ronald Cron5425a212020-08-04 14:58:35 +02003362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003363 psa_key_type_t key_type = key_type_arg;
3364 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003365 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003366 unsigned char *output_data = NULL;
3367 size_t output_size = 0;
3368 size_t output_length = 0;
3369 unsigned char *output_data2 = NULL;
3370 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003371 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003372 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374
Gilles Peskine8817f612018-12-18 00:18:46 +01003375 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003376
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003377 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3378 psa_set_key_algorithm( &attributes, alg );
3379 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380
Gilles Peskine049c7532019-05-15 20:22:09 +02003381 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003382 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003383 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3384 key_bits = psa_get_key_bits( &attributes );
3385
3386 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3387 alg );
3388 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3389 * should be exact. */
3390 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3391 expected_result != PSA_ERROR_NOT_SUPPORTED )
3392 {
3393 TEST_EQUAL( output_size,
3394 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3395 TEST_ASSERT( output_size <=
3396 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3397 }
3398 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003399
Steven Cooremanf49478b2021-02-15 15:19:25 +01003400 status = psa_aead_encrypt( key, alg,
3401 nonce->x, nonce->len,
3402 additional_data->x,
3403 additional_data->len,
3404 input_data->x, input_data->len,
3405 output_data, output_size,
3406 &output_length );
3407
3408 /* If the operation is not supported, just skip and not fail in case the
3409 * encryption involves a common limitation of cryptography hardwares and
3410 * an alternative implementation. */
3411 if( status == PSA_ERROR_NOT_SUPPORTED )
3412 {
3413 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3414 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3415 }
3416
3417 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
3419 if( PSA_SUCCESS == expected_result )
3420 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003421 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003422
Gilles Peskine003a4a92019-05-14 16:09:40 +02003423 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3424 * should be exact. */
3425 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003426 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003427
gabor-mezei-armceface22021-01-21 12:26:17 +01003428 TEST_ASSERT( input_data->len <=
3429 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3430
Ronald Cron5425a212020-08-04 14:58:35 +02003431 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003432 nonce->x, nonce->len,
3433 additional_data->x,
3434 additional_data->len,
3435 output_data, output_length,
3436 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003437 &output_length2 ),
3438 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003439
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003440 ASSERT_COMPARE( input_data->x, input_data->len,
3441 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003443
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003445 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446 mbedtls_free( output_data );
3447 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003448 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003449}
3450/* END_CASE */
3451
3452/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003453void aead_encrypt( int key_type_arg, data_t *key_data,
3454 int alg_arg,
3455 data_t *nonce,
3456 data_t *additional_data,
3457 data_t *input_data,
3458 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459{
Ronald Cron5425a212020-08-04 14:58:35 +02003460 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461 psa_key_type_t key_type = key_type_arg;
3462 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003463 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003464 unsigned char *output_data = NULL;
3465 size_t output_size = 0;
3466 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003468 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003469
Gilles Peskine8817f612018-12-18 00:18:46 +01003470 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003471
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3473 psa_set_key_algorithm( &attributes, alg );
3474 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475
Gilles Peskine049c7532019-05-15 20:22:09 +02003476 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003477 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003478 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3479 key_bits = psa_get_key_bits( &attributes );
3480
3481 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3482 alg );
3483 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3484 * should be exact. */
3485 TEST_EQUAL( output_size,
3486 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3487 TEST_ASSERT( output_size <=
3488 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3489 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003490
Steven Cooremand588ea12021-01-11 19:36:04 +01003491 status = psa_aead_encrypt( key, alg,
3492 nonce->x, nonce->len,
3493 additional_data->x, additional_data->len,
3494 input_data->x, input_data->len,
3495 output_data, output_size,
3496 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003497
Ronald Cron28a45ed2021-02-09 20:35:42 +01003498 /* If the operation is not supported, just skip and not fail in case the
3499 * encryption involves a common limitation of cryptography hardwares and
3500 * an alternative implementation. */
3501 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003502 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003503 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3504 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003505 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003506
3507 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003508 ASSERT_COMPARE( expected_result->x, expected_result->len,
3509 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003510
Gilles Peskinea1cac842018-06-11 19:33:02 +02003511exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003512 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003513 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003514 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003515}
3516/* END_CASE */
3517
3518/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003519void aead_decrypt( int key_type_arg, data_t *key_data,
3520 int alg_arg,
3521 data_t *nonce,
3522 data_t *additional_data,
3523 data_t *input_data,
3524 data_t *expected_data,
3525 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003526{
Ronald Cron5425a212020-08-04 14:58:35 +02003527 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003528 psa_key_type_t key_type = key_type_arg;
3529 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003530 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003531 unsigned char *output_data = NULL;
3532 size_t output_size = 0;
3533 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003534 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003535 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003536 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003537
Gilles Peskine8817f612018-12-18 00:18:46 +01003538 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003539
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003540 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3541 psa_set_key_algorithm( &attributes, alg );
3542 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003543
Gilles Peskine049c7532019-05-15 20:22:09 +02003544 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003545 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003546 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3547 key_bits = psa_get_key_bits( &attributes );
3548
3549 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3550 alg );
3551 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3552 expected_result != PSA_ERROR_NOT_SUPPORTED )
3553 {
3554 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3555 * should be exact. */
3556 TEST_EQUAL( output_size,
3557 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3558 TEST_ASSERT( output_size <=
3559 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3560 }
3561 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003562
Steven Cooremand588ea12021-01-11 19:36:04 +01003563 status = psa_aead_decrypt( key, alg,
3564 nonce->x, nonce->len,
3565 additional_data->x,
3566 additional_data->len,
3567 input_data->x, input_data->len,
3568 output_data, output_size,
3569 &output_length );
3570
Ronald Cron28a45ed2021-02-09 20:35:42 +01003571 /* If the operation is not supported, just skip and not fail in case the
3572 * decryption involves a common limitation of cryptography hardwares and
3573 * an alternative implementation. */
3574 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003575 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003576 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3577 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003578 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003579
3580 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003581
Gilles Peskine2d277862018-06-18 15:41:12 +02003582 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003583 ASSERT_COMPARE( expected_data->x, expected_data->len,
3584 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003585
Gilles Peskinea1cac842018-06-11 19:33:02 +02003586exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003587 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003588 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003589 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003590}
3591/* END_CASE */
3592
3593/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003594void signature_size( int type_arg,
3595 int bits,
3596 int alg_arg,
3597 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003598{
3599 psa_key_type_t type = type_arg;
3600 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003601 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003602
Gilles Peskinefe11b722018-12-18 00:24:04 +01003603 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003604#if defined(MBEDTLS_TEST_DEPRECATED)
3605 TEST_EQUAL( actual_size,
3606 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3607#endif /* MBEDTLS_TEST_DEPRECATED */
3608
Gilles Peskinee59236f2018-01-27 23:32:46 +01003609exit:
3610 ;
3611}
3612/* END_CASE */
3613
3614/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003615void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3616 int alg_arg, data_t *input_data,
3617 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003618{
Ronald Cron5425a212020-08-04 14:58:35 +02003619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003620 psa_key_type_t key_type = key_type_arg;
3621 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003622 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003623 unsigned char *signature = NULL;
3624 size_t signature_size;
3625 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003627
Gilles Peskine8817f612018-12-18 00:18:46 +01003628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003629
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003631 psa_set_key_algorithm( &attributes, alg );
3632 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003633
Gilles Peskine049c7532019-05-15 20:22:09 +02003634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003635 &key ) );
3636 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003637 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003638
Shaun Case0e7791f2021-12-20 21:14:10 -08003639 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003640 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003641 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003642 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003643 TEST_ASSERT( signature_size != 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003644 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003645 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003646
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003647 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003648 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003649 input_data->x, input_data->len,
3650 signature, signature_size,
3651 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003652 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003653 ASSERT_COMPARE( output_data->x, output_data->len,
3654 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003655
Gilles Peskine0627f982019-11-26 19:12:16 +01003656#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003657 memset( signature, 0, signature_size );
3658 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003659 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003660 input_data->x, input_data->len,
3661 signature, signature_size,
3662 &signature_length ) );
3663 ASSERT_COMPARE( output_data->x, output_data->len,
3664 signature, signature_length );
3665#endif /* MBEDTLS_TEST_DEPRECATED */
3666
Gilles Peskine20035e32018-02-03 22:44:14 +01003667exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003668 /*
3669 * Key attributes may have been returned by psa_get_key_attributes()
3670 * thus reset them as required.
3671 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003672 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003673
Ronald Cron5425a212020-08-04 14:58:35 +02003674 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003675 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003676 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003677}
3678/* END_CASE */
3679
3680/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003681void sign_hash_fail( int key_type_arg, data_t *key_data,
3682 int alg_arg, data_t *input_data,
3683 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003684{
Ronald Cron5425a212020-08-04 14:58:35 +02003685 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003686 psa_key_type_t key_type = key_type_arg;
3687 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003688 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003689 psa_status_t actual_status;
3690 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003691 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003692 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003693 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003694
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003695 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003696
Gilles Peskine8817f612018-12-18 00:18:46 +01003697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003698
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003700 psa_set_key_algorithm( &attributes, alg );
3701 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003702
Gilles Peskine049c7532019-05-15 20:22:09 +02003703 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003704 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003705
Ronald Cron5425a212020-08-04 14:58:35 +02003706 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003707 input_data->x, input_data->len,
3708 signature, signature_size,
3709 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003710 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003711 /* The value of *signature_length is unspecified on error, but
3712 * whatever it is, it should be less than signature_size, so that
3713 * if the caller tries to read *signature_length bytes without
3714 * checking the error code then they don't overflow a buffer. */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003715 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003716
Gilles Peskine895242b2019-11-29 12:15:40 +01003717#if defined(MBEDTLS_TEST_DEPRECATED)
3718 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003719 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003720 input_data->x, input_data->len,
3721 signature, signature_size,
3722 &signature_length ),
3723 expected_status );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003724 TEST_LE_U( signature_length, signature_size );
Gilles Peskine895242b2019-11-29 12:15:40 +01003725#endif /* MBEDTLS_TEST_DEPRECATED */
3726
Gilles Peskine20035e32018-02-03 22:44:14 +01003727exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003728 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003729 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003730 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003731 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003732}
3733/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003734
3735/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003736void sign_verify_hash( int key_type_arg, data_t *key_data,
3737 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003738{
Ronald Cron5425a212020-08-04 14:58:35 +02003739 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003740 psa_key_type_t key_type = key_type_arg;
3741 psa_algorithm_t alg = alg_arg;
3742 size_t key_bits;
3743 unsigned char *signature = NULL;
3744 size_t signature_size;
3745 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003747
Gilles Peskine8817f612018-12-18 00:18:46 +01003748 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003749
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003750 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003751 psa_set_key_algorithm( &attributes, alg );
3752 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003753
Gilles Peskine049c7532019-05-15 20:22:09 +02003754 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003755 &key ) );
3756 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003757 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003758
Shaun Case0e7791f2021-12-20 21:14:10 -08003759 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02003760 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003761 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003762 key_bits, alg );
3763 TEST_ASSERT( signature_size != 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003764 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003765 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003766
3767 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003768 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003769 input_data->x, input_data->len,
3770 signature, signature_size,
3771 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003772 /* Check that the signature length looks sensible. */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003773 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003774 TEST_ASSERT( signature_length > 0 );
3775
3776 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003777 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003778 input_data->x, input_data->len,
3779 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003780
3781 if( input_data->len != 0 )
3782 {
3783 /* Flip a bit in the input and verify that the signature is now
3784 * detected as invalid. Flip a bit at the beginning, not at the end,
3785 * because ECDSA may ignore the last few bits of the input. */
3786 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003787 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003788 input_data->x, input_data->len,
3789 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003790 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003791 }
3792
3793exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003794 /*
3795 * Key attributes may have been returned by psa_get_key_attributes()
3796 * thus reset them as required.
3797 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003798 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003799
Ronald Cron5425a212020-08-04 14:58:35 +02003800 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003801 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003802 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003803}
3804/* END_CASE */
3805
3806/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003807void verify_hash( int key_type_arg, data_t *key_data,
3808 int alg_arg, data_t *hash_data,
3809 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003810{
Ronald Cron5425a212020-08-04 14:58:35 +02003811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003812 psa_key_type_t key_type = key_type_arg;
3813 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003815
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003816 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003817
Gilles Peskine8817f612018-12-18 00:18:46 +01003818 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003819
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003820 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003821 psa_set_key_algorithm( &attributes, alg );
3822 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003823
Gilles Peskine049c7532019-05-15 20:22:09 +02003824 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003825 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003826
Ronald Cron5425a212020-08-04 14:58:35 +02003827 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003828 hash_data->x, hash_data->len,
3829 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003830
3831#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003832 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003833 hash_data->x, hash_data->len,
3834 signature_data->x,
3835 signature_data->len ) );
3836
3837#endif /* MBEDTLS_TEST_DEPRECATED */
3838
itayzafrir5c753392018-05-08 11:18:38 +03003839exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003840 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003841 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003842 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003843}
3844/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003845
3846/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003847void verify_hash_fail( int key_type_arg, data_t *key_data,
3848 int alg_arg, data_t *hash_data,
3849 data_t *signature_data,
3850 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003851{
Ronald Cron5425a212020-08-04 14:58:35 +02003852 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003853 psa_key_type_t key_type = key_type_arg;
3854 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003855 psa_status_t actual_status;
3856 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003857 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003858
Gilles Peskine8817f612018-12-18 00:18:46 +01003859 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003860
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003861 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003862 psa_set_key_algorithm( &attributes, alg );
3863 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003864
Gilles Peskine049c7532019-05-15 20:22:09 +02003865 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003866 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867
Ronald Cron5425a212020-08-04 14:58:35 +02003868 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003869 hash_data->x, hash_data->len,
3870 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003871 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872
Gilles Peskine895242b2019-11-29 12:15:40 +01003873#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003874 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003875 hash_data->x, hash_data->len,
3876 signature_data->x, signature_data->len ),
3877 expected_status );
3878#endif /* MBEDTLS_TEST_DEPRECATED */
3879
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003881 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003882 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003883 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884}
3885/* END_CASE */
3886
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003888void sign_message_deterministic( int key_type_arg,
3889 data_t *key_data,
3890 int alg_arg,
3891 data_t *input_data,
3892 data_t *output_data )
3893{
3894 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3895 psa_key_type_t key_type = key_type_arg;
3896 psa_algorithm_t alg = alg_arg;
3897 size_t key_bits;
3898 unsigned char *signature = NULL;
3899 size_t signature_size;
3900 size_t signature_length = 0xdeadbeef;
3901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3902
3903 PSA_ASSERT( psa_crypto_init( ) );
3904
3905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3906 psa_set_key_algorithm( &attributes, alg );
3907 psa_set_key_type( &attributes, key_type );
3908
3909 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3910 &key ) );
3911 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3912 key_bits = psa_get_key_bits( &attributes );
3913
3914 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3915 TEST_ASSERT( signature_size != 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003916 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-armabd72582021-04-15 18:19:50 +02003917 ASSERT_ALLOC( signature, signature_size );
3918
3919 PSA_ASSERT( psa_sign_message( key, alg,
3920 input_data->x, input_data->len,
3921 signature, signature_size,
3922 &signature_length ) );
3923
3924 ASSERT_COMPARE( output_data->x, output_data->len,
3925 signature, signature_length );
3926
3927exit:
3928 psa_reset_key_attributes( &attributes );
3929
3930 psa_destroy_key( key );
3931 mbedtls_free( signature );
3932 PSA_DONE( );
3933
3934}
3935/* END_CASE */
3936
3937/* BEGIN_CASE */
3938void sign_message_fail( int key_type_arg,
3939 data_t *key_data,
3940 int alg_arg,
3941 data_t *input_data,
3942 int signature_size_arg,
3943 int expected_status_arg )
3944{
3945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3946 psa_key_type_t key_type = key_type_arg;
3947 psa_algorithm_t alg = alg_arg;
3948 size_t signature_size = signature_size_arg;
3949 psa_status_t actual_status;
3950 psa_status_t expected_status = expected_status_arg;
3951 unsigned char *signature = NULL;
3952 size_t signature_length = 0xdeadbeef;
3953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3954
3955 ASSERT_ALLOC( signature, signature_size );
3956
3957 PSA_ASSERT( psa_crypto_init( ) );
3958
3959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3960 psa_set_key_algorithm( &attributes, alg );
3961 psa_set_key_type( &attributes, key_type );
3962
3963 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3964 &key ) );
3965
3966 actual_status = psa_sign_message( key, alg,
3967 input_data->x, input_data->len,
3968 signature, signature_size,
3969 &signature_length );
3970 TEST_EQUAL( actual_status, expected_status );
3971 /* The value of *signature_length is unspecified on error, but
3972 * whatever it is, it should be less than signature_size, so that
3973 * if the caller tries to read *signature_length bytes without
3974 * checking the error code then they don't overflow a buffer. */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02003975 TEST_LE_U( signature_length, signature_size );
gabor-mezei-armabd72582021-04-15 18:19:50 +02003976
3977exit:
3978 psa_reset_key_attributes( &attributes );
3979 psa_destroy_key( key );
3980 mbedtls_free( signature );
3981 PSA_DONE( );
3982}
3983/* END_CASE */
3984
3985/* BEGIN_CASE */
3986void sign_verify_message( int key_type_arg,
3987 data_t *key_data,
3988 int alg_arg,
3989 data_t *input_data )
3990{
3991 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3992 psa_key_type_t key_type = key_type_arg;
3993 psa_algorithm_t alg = alg_arg;
3994 size_t key_bits;
3995 unsigned char *signature = NULL;
3996 size_t signature_size;
3997 size_t signature_length = 0xdeadbeef;
3998 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3999
4000 PSA_ASSERT( psa_crypto_init( ) );
4001
4002 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
4003 PSA_KEY_USAGE_VERIFY_MESSAGE );
4004 psa_set_key_algorithm( &attributes, alg );
4005 psa_set_key_type( &attributes, key_type );
4006
4007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4008 &key ) );
4009 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4010 key_bits = psa_get_key_bits( &attributes );
4011
4012 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
4013 TEST_ASSERT( signature_size != 0 );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004014 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-armabd72582021-04-15 18:19:50 +02004015 ASSERT_ALLOC( signature, signature_size );
4016
4017 PSA_ASSERT( psa_sign_message( key, alg,
4018 input_data->x, input_data->len,
4019 signature, signature_size,
4020 &signature_length ) );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004021 TEST_LE_U( signature_length, signature_size );
gabor-mezei-armabd72582021-04-15 18:19:50 +02004022 TEST_ASSERT( signature_length > 0 );
4023
4024 PSA_ASSERT( psa_verify_message( key, alg,
4025 input_data->x, input_data->len,
4026 signature, signature_length ) );
4027
4028 if( input_data->len != 0 )
4029 {
4030 /* Flip a bit in the input and verify that the signature is now
4031 * detected as invalid. Flip a bit at the beginning, not at the end,
4032 * because ECDSA may ignore the last few bits of the input. */
4033 input_data->x[0] ^= 1;
4034 TEST_EQUAL( psa_verify_message( key, alg,
4035 input_data->x, input_data->len,
4036 signature, signature_length ),
4037 PSA_ERROR_INVALID_SIGNATURE );
4038 }
4039
4040exit:
4041 psa_reset_key_attributes( &attributes );
4042
4043 psa_destroy_key( key );
4044 mbedtls_free( signature );
4045 PSA_DONE( );
4046}
4047/* END_CASE */
4048
4049/* BEGIN_CASE */
4050void verify_message( int key_type_arg,
4051 data_t *key_data,
4052 int alg_arg,
4053 data_t *input_data,
4054 data_t *signature_data )
4055{
4056 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4057 psa_key_type_t key_type = key_type_arg;
4058 psa_algorithm_t alg = alg_arg;
4059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4060
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004061 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-armabd72582021-04-15 18:19:50 +02004062
4063 PSA_ASSERT( psa_crypto_init( ) );
4064
4065 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4066 psa_set_key_algorithm( &attributes, alg );
4067 psa_set_key_type( &attributes, key_type );
4068
4069 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4070 &key ) );
4071
4072 PSA_ASSERT( psa_verify_message( key, alg,
4073 input_data->x, input_data->len,
4074 signature_data->x, signature_data->len ) );
4075
4076exit:
4077 psa_reset_key_attributes( &attributes );
4078 psa_destroy_key( key );
4079 PSA_DONE( );
4080}
4081/* END_CASE */
4082
4083/* BEGIN_CASE */
4084void verify_message_fail( int key_type_arg,
4085 data_t *key_data,
4086 int alg_arg,
4087 data_t *hash_data,
4088 data_t *signature_data,
4089 int expected_status_arg )
4090{
4091 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4092 psa_key_type_t key_type = key_type_arg;
4093 psa_algorithm_t alg = alg_arg;
4094 psa_status_t actual_status;
4095 psa_status_t expected_status = expected_status_arg;
4096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4097
4098 PSA_ASSERT( psa_crypto_init( ) );
4099
4100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4101 psa_set_key_algorithm( &attributes, alg );
4102 psa_set_key_type( &attributes, key_type );
4103
4104 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4105 &key ) );
4106
4107 actual_status = psa_verify_message( key, alg,
4108 hash_data->x, hash_data->len,
4109 signature_data->x,
4110 signature_data->len );
4111 TEST_EQUAL( actual_status, expected_status );
4112
4113exit:
4114 psa_reset_key_attributes( &attributes );
4115 psa_destroy_key( key );
4116 PSA_DONE( );
4117}
4118/* END_CASE */
4119
4120/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004121void asymmetric_encrypt( int key_type_arg,
4122 data_t *key_data,
4123 int alg_arg,
4124 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004125 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004126 int expected_output_length_arg,
4127 int expected_status_arg )
4128{
Ronald Cron5425a212020-08-04 14:58:35 +02004129 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004130 psa_key_type_t key_type = key_type_arg;
4131 psa_algorithm_t alg = alg_arg;
4132 size_t expected_output_length = expected_output_length_arg;
4133 size_t key_bits;
4134 unsigned char *output = NULL;
4135 size_t output_size;
4136 size_t output_length = ~0;
4137 psa_status_t actual_status;
4138 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004140
Gilles Peskine8817f612018-12-18 00:18:46 +01004141 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004142
Gilles Peskine656896e2018-06-29 19:12:28 +02004143 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004144 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4145 psa_set_key_algorithm( &attributes, alg );
4146 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004147 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004148 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004149
4150 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004151 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004152 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004153
Gilles Peskine656896e2018-06-29 19:12:28 +02004154 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004155 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004156 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004157
4158 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004159 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004160 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004161 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004162 output, output_size,
4163 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004164 TEST_EQUAL( actual_status, expected_status );
4165 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004166
Gilles Peskine68428122018-06-30 18:42:41 +02004167 /* If the label is empty, the test framework puts a non-null pointer
4168 * in label->x. Test that a null pointer works as well. */
4169 if( label->len == 0 )
4170 {
4171 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004172 if( output_size != 0 )
4173 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004174 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004175 input_data->x, input_data->len,
4176 NULL, label->len,
4177 output, output_size,
4178 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004179 TEST_EQUAL( actual_status, expected_status );
4180 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004181 }
4182
Gilles Peskine656896e2018-06-29 19:12:28 +02004183exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004184 /*
4185 * Key attributes may have been returned by psa_get_key_attributes()
4186 * thus reset them as required.
4187 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004188 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004189
Ronald Cron5425a212020-08-04 14:58:35 +02004190 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004191 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004192 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004193}
4194/* END_CASE */
4195
4196/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004197void asymmetric_encrypt_decrypt( int key_type_arg,
4198 data_t *key_data,
4199 int alg_arg,
4200 data_t *input_data,
4201 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004202{
Ronald Cron5425a212020-08-04 14:58:35 +02004203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004204 psa_key_type_t key_type = key_type_arg;
4205 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004206 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004207 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004208 size_t output_size;
4209 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004210 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004211 size_t output2_size;
4212 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004214
Gilles Peskine8817f612018-12-18 00:18:46 +01004215 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004216
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004217 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4218 psa_set_key_algorithm( &attributes, alg );
4219 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004220
Gilles Peskine049c7532019-05-15 20:22:09 +02004221 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004222 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004223
4224 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004225 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004226 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004227
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004228 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004229 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004230 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004231
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004232 output2_size = input_data->len;
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004233 TEST_LE_U( output2_size,
4234 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4235 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004236 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004237
Gilles Peskineeebd7382018-06-08 18:11:54 +02004238 /* We test encryption by checking that encrypt-then-decrypt gives back
4239 * the original plaintext because of the non-optional random
4240 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004241 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004242 input_data->x, input_data->len,
4243 label->x, label->len,
4244 output, output_size,
4245 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004246 /* We don't know what ciphertext length to expect, but check that
4247 * it looks sensible. */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004248 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004249
Ronald Cron5425a212020-08-04 14:58:35 +02004250 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004251 output, output_length,
4252 label->x, label->len,
4253 output2, output2_size,
4254 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004255 ASSERT_COMPARE( input_data->x, input_data->len,
4256 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257
4258exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004259 /*
4260 * Key attributes may have been returned by psa_get_key_attributes()
4261 * thus reset them as required.
4262 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004263 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004264
Ronald Cron5425a212020-08-04 14:58:35 +02004265 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004266 mbedtls_free( output );
4267 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004268 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004269}
4270/* END_CASE */
4271
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004272/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004273void asymmetric_decrypt( int key_type_arg,
4274 data_t *key_data,
4275 int alg_arg,
4276 data_t *input_data,
4277 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004278 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004279{
Ronald Cron5425a212020-08-04 14:58:35 +02004280 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004281 psa_key_type_t key_type = key_type_arg;
4282 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004283 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004284 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004285 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004286 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004287 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004288
Gilles Peskine8817f612018-12-18 00:18:46 +01004289 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004290
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004291 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4292 psa_set_key_algorithm( &attributes, alg );
4293 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004294
Gilles Peskine049c7532019-05-15 20:22:09 +02004295 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004296 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004297
gabor-mezei-armceface22021-01-21 12:26:17 +01004298 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4299 key_bits = psa_get_key_bits( &attributes );
4300
4301 /* Determine the maximum ciphertext length */
4302 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004303 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01004304 ASSERT_ALLOC( output, output_size );
4305
Ronald Cron5425a212020-08-04 14:58:35 +02004306 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004307 input_data->x, input_data->len,
4308 label->x, label->len,
4309 output,
4310 output_size,
4311 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004312 ASSERT_COMPARE( expected_data->x, expected_data->len,
4313 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004314
Gilles Peskine68428122018-06-30 18:42:41 +02004315 /* If the label is empty, the test framework puts a non-null pointer
4316 * in label->x. Test that a null pointer works as well. */
4317 if( label->len == 0 )
4318 {
4319 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004320 if( output_size != 0 )
4321 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004322 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004323 input_data->x, input_data->len,
4324 NULL, label->len,
4325 output,
4326 output_size,
4327 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004328 ASSERT_COMPARE( expected_data->x, expected_data->len,
4329 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004330 }
4331
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004332exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004333 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004334 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004335 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004336 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337}
4338/* END_CASE */
4339
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004341void asymmetric_decrypt_fail( int key_type_arg,
4342 data_t *key_data,
4343 int alg_arg,
4344 data_t *input_data,
4345 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004346 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004347 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004348{
Ronald Cron5425a212020-08-04 14:58:35 +02004349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004350 psa_key_type_t key_type = key_type_arg;
4351 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004352 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004353 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004354 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004355 psa_status_t actual_status;
4356 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004358
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004359 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004360
Gilles Peskine8817f612018-12-18 00:18:46 +01004361 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004362
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4364 psa_set_key_algorithm( &attributes, alg );
4365 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004366
Gilles Peskine049c7532019-05-15 20:22:09 +02004367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004368 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004369
Ronald Cron5425a212020-08-04 14:58:35 +02004370 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004371 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004372 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004373 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004374 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004375 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004376 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004377
Gilles Peskine68428122018-06-30 18:42:41 +02004378 /* If the label is empty, the test framework puts a non-null pointer
4379 * in label->x. Test that a null pointer works as well. */
4380 if( label->len == 0 )
4381 {
4382 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004383 if( output_size != 0 )
4384 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004385 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004386 input_data->x, input_data->len,
4387 NULL, label->len,
4388 output, output_size,
4389 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004390 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02004391 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004392 }
4393
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004394exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004395 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004396 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004397 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004398 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004399}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004400/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004401
4402/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004403void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004404{
4405 /* Test each valid way of initializing the object, except for `= {0}`, as
4406 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4407 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case0e7791f2021-12-20 21:14:10 -08004408 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004409 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004410 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4411 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4412 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004413
4414 memset( &zero, 0, sizeof( zero ) );
4415
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004417 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004418 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004419 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004420 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004421 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004422 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004423
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004425 PSA_ASSERT( psa_key_derivation_abort(&func) );
4426 PSA_ASSERT( psa_key_derivation_abort(&init) );
4427 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004428}
4429/* END_CASE */
4430
Janos Follath16de4a42019-06-13 16:32:24 +01004431/* BEGIN_CASE */
4432void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004433{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004434 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004435 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004437
Gilles Peskine8817f612018-12-18 00:18:46 +01004438 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004439
Janos Follath16de4a42019-06-13 16:32:24 +01004440 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004441 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004442
4443exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004445 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004446}
4447/* END_CASE */
4448
Janos Follathaf3c2a02019-06-12 12:34:34 +01004449/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004450void derive_set_capacity( int alg_arg, int capacity_arg,
4451 int expected_status_arg )
4452{
4453 psa_algorithm_t alg = alg_arg;
4454 size_t capacity = capacity_arg;
4455 psa_status_t expected_status = expected_status_arg;
4456 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4457
4458 PSA_ASSERT( psa_crypto_init( ) );
4459
4460 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4461
4462 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4463 expected_status );
4464
4465exit:
4466 psa_key_derivation_abort( &operation );
4467 PSA_DONE( );
4468}
4469/* END_CASE */
4470
4471/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004472void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004473 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004474 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004475 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004476 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004477 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004478 int expected_status_arg3,
4479 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004480{
4481 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004482 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4483 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004484 psa_status_t expected_statuses[] = {expected_status_arg1,
4485 expected_status_arg2,
4486 expected_status_arg3};
4487 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004488 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4489 MBEDTLS_SVC_KEY_ID_INIT,
4490 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004491 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4492 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4493 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004494 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004495 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004496 psa_status_t expected_output_status = expected_output_status_arg;
4497 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004498
4499 PSA_ASSERT( psa_crypto_init( ) );
4500
4501 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4502 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004503
4504 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4505
4506 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4507 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004508 mbedtls_test_set_step( i );
4509 if( steps[i] == 0 )
4510 {
4511 /* Skip this step */
4512 }
4513 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004514 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004515 psa_set_key_type( &attributes, key_types[i] );
4516 PSA_ASSERT( psa_import_key( &attributes,
4517 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004518 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004519 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4520 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4521 {
4522 // When taking a private key as secret input, use key agreement
4523 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004524 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4525 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004526 expected_statuses[i] );
4527 }
4528 else
4529 {
4530 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004531 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004532 expected_statuses[i] );
4533 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004534 }
4535 else
4536 {
4537 TEST_EQUAL( psa_key_derivation_input_bytes(
4538 &operation, steps[i],
4539 inputs[i]->x, inputs[i]->len ),
4540 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004541 }
4542 }
4543
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004544 if( output_key_type != PSA_KEY_TYPE_NONE )
4545 {
4546 psa_reset_key_attributes( &attributes );
Dave Rodgmandc4e4b72021-11-16 12:12:49 +00004547 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004548 psa_set_key_bits( &attributes, 8 );
4549 actual_output_status =
4550 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004551 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004552 }
4553 else
4554 {
4555 uint8_t buffer[1];
4556 actual_output_status =
4557 psa_key_derivation_output_bytes( &operation,
4558 buffer, sizeof( buffer ) );
4559 }
4560 TEST_EQUAL( actual_output_status, expected_output_status );
4561
Janos Follathaf3c2a02019-06-12 12:34:34 +01004562exit:
4563 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004564 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4565 psa_destroy_key( keys[i] );
4566 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004567 PSA_DONE( );
4568}
4569/* END_CASE */
4570
Janos Follathd958bb72019-07-03 15:02:16 +01004571/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004572void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004573{
Janos Follathd958bb72019-07-03 15:02:16 +01004574 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004575 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004576 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004578 unsigned char input1[] = "Input 1";
4579 size_t input1_length = sizeof( input1 );
4580 unsigned char input2[] = "Input 2";
4581 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004582 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004583 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004584 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4585 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4586 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004588
Gilles Peskine8817f612018-12-18 00:18:46 +01004589 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004590
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004591 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4592 psa_set_key_algorithm( &attributes, alg );
4593 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004594
Gilles Peskine73676cb2019-05-15 20:15:10 +02004595 PSA_ASSERT( psa_import_key( &attributes,
4596 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004597 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004598
4599 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004600 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4601 input1, input1_length,
4602 input2, input2_length,
4603 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004604 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004605
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004606 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004607 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004608 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004609
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004611
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004613 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004614
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004615exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004616 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004617 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004618 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004619}
4620/* END_CASE */
4621
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004622/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004623void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004624{
4625 uint8_t output_buffer[16];
4626 size_t buffer_size = 16;
4627 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004629
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004630 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4631 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004632 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004633
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004634 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004635 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004636
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004637 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004638
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004639 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4640 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004641 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004642
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004643 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004644 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004645
4646exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004648}
4649/* END_CASE */
4650
4651/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004652void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004653 int step1_arg, data_t *input1,
4654 int step2_arg, data_t *input2,
4655 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004656 int requested_capacity_arg,
4657 data_t *expected_output1,
4658 data_t *expected_output2 )
4659{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004660 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004661 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4662 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004663 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4664 MBEDTLS_SVC_KEY_ID_INIT,
4665 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004666 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004668 uint8_t *expected_outputs[2] =
4669 {expected_output1->x, expected_output2->x};
4670 size_t output_sizes[2] =
4671 {expected_output1->len, expected_output2->len};
4672 size_t output_buffer_size = 0;
4673 uint8_t *output_buffer = NULL;
4674 size_t expected_capacity;
4675 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004677 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004678 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004679
4680 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4681 {
4682 if( output_sizes[i] > output_buffer_size )
4683 output_buffer_size = output_sizes[i];
4684 if( output_sizes[i] == 0 )
4685 expected_outputs[i] = NULL;
4686 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004687 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004688 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004689
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004690 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4691 psa_set_key_algorithm( &attributes, alg );
4692 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004693
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004694 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004695 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4696 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4697 requested_capacity ) );
4698 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004699 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004700 switch( steps[i] )
4701 {
4702 case 0:
4703 break;
4704 case PSA_KEY_DERIVATION_INPUT_SECRET:
4705 PSA_ASSERT( psa_import_key( &attributes,
4706 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004707 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004708
4709 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4710 {
4711 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4712 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4713 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4714 }
4715
Gilles Peskine1468da72019-05-29 17:35:49 +02004716 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004717 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004718 break;
4719 default:
4720 PSA_ASSERT( psa_key_derivation_input_bytes(
4721 &operation, steps[i],
4722 inputs[i]->x, inputs[i]->len ) );
4723 break;
4724 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004725 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004726
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004727 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004728 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004729 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004730 expected_capacity = requested_capacity;
4731
4732 /* Expansion phase. */
4733 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4734 {
4735 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004737 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004738 if( expected_capacity == 0 && output_sizes[i] == 0 )
4739 {
4740 /* Reading 0 bytes when 0 bytes are available can go either way. */
4741 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004742 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004743 continue;
4744 }
4745 else if( expected_capacity == 0 ||
4746 output_sizes[i] > expected_capacity )
4747 {
4748 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004749 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004750 expected_capacity = 0;
4751 continue;
4752 }
4753 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004754 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004755 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004756 ASSERT_COMPARE( output_buffer, output_sizes[i],
4757 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004759 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004761 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004762 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004763 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004764 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004765
4766exit:
4767 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004769 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4770 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004771 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004772}
4773/* END_CASE */
4774
4775/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004776void derive_full( int alg_arg,
4777 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004778 data_t *input1,
4779 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004780 int requested_capacity_arg )
4781{
Ronald Cron5425a212020-08-04 14:58:35 +02004782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004783 psa_algorithm_t alg = alg_arg;
4784 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004785 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004786 unsigned char output_buffer[16];
4787 size_t expected_capacity = requested_capacity;
4788 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004790
Gilles Peskine8817f612018-12-18 00:18:46 +01004791 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004792
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004793 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4794 psa_set_key_algorithm( &attributes, alg );
4795 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004796
Gilles Peskine049c7532019-05-15 20:22:09 +02004797 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004798 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004799
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004800 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4801 input1->x, input1->len,
4802 input2->x, input2->len,
4803 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004804 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004805
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004807 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004808 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004809
4810 /* Expansion phase. */
4811 while( current_capacity > 0 )
4812 {
4813 size_t read_size = sizeof( output_buffer );
4814 if( read_size > current_capacity )
4815 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004816 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004817 output_buffer,
4818 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004819 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004821 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004822 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004823 }
4824
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825 /* Check that the operation refuses to go over capacity. */
4826 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004827 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004828
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004830
4831exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004833 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004834 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004835}
4836/* END_CASE */
4837
Janos Follathe60c9052019-07-03 13:51:30 +01004838/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004839void derive_key_exercise( int alg_arg,
4840 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004841 data_t *input1,
4842 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004843 int derived_type_arg,
4844 int derived_bits_arg,
4845 int derived_usage_arg,
4846 int derived_alg_arg )
4847{
Ronald Cron5425a212020-08-04 14:58:35 +02004848 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4849 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004850 psa_algorithm_t alg = alg_arg;
4851 psa_key_type_t derived_type = derived_type_arg;
4852 size_t derived_bits = derived_bits_arg;
4853 psa_key_usage_t derived_usage = derived_usage_arg;
4854 psa_algorithm_t derived_alg = derived_alg_arg;
4855 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004857 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004858 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004859
Gilles Peskine8817f612018-12-18 00:18:46 +01004860 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004861
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4863 psa_set_key_algorithm( &attributes, alg );
4864 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004865 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004866 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004867
4868 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004869 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4870 input1->x, input1->len,
4871 input2->x, input2->len,
4872 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004873 goto exit;
4874
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004875 psa_set_key_usage_flags( &attributes, derived_usage );
4876 psa_set_key_algorithm( &attributes, derived_alg );
4877 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004878 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004880 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004881
4882 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004883 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004884 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4885 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004886
4887 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004888 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004889 goto exit;
4890
4891exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004892 /*
4893 * Key attributes may have been returned by psa_get_key_attributes()
4894 * thus reset them as required.
4895 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004896 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004897
4898 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004899 psa_destroy_key( base_key );
4900 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004901 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004902}
4903/* END_CASE */
4904
Janos Follath42fd8882019-07-03 14:17:09 +01004905/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004906void derive_key_export( int alg_arg,
4907 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004908 data_t *input1,
4909 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004910 int bytes1_arg,
4911 int bytes2_arg )
4912{
Ronald Cron5425a212020-08-04 14:58:35 +02004913 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4914 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004915 psa_algorithm_t alg = alg_arg;
4916 size_t bytes1 = bytes1_arg;
4917 size_t bytes2 = bytes2_arg;
4918 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004920 uint8_t *output_buffer = NULL;
4921 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004922 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4923 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004924 size_t length;
4925
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004926 ASSERT_ALLOC( output_buffer, capacity );
4927 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004928 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004929
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004930 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4931 psa_set_key_algorithm( &base_attributes, alg );
4932 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004933 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004934 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004935
4936 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004937 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4938 input1->x, input1->len,
4939 input2->x, input2->len,
4940 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004941 goto exit;
4942
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004944 output_buffer,
4945 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004946 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004947
4948 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004949 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4950 input1->x, input1->len,
4951 input2->x, input2->len,
4952 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004953 goto exit;
4954
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004955 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4956 psa_set_key_algorithm( &derived_attributes, 0 );
4957 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004958 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004959 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004960 &derived_key ) );
4961 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004962 export_buffer, bytes1,
4963 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004964 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004965 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004966 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004968 &derived_key ) );
4969 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004970 export_buffer + bytes1, bytes2,
4971 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004972 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004973
4974 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004975 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4976 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004977
4978exit:
4979 mbedtls_free( output_buffer );
4980 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004982 psa_destroy_key( base_key );
4983 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004984 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004985}
4986/* END_CASE */
4987
4988/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004989void derive_key( int alg_arg,
4990 data_t *key_data, data_t *input1, data_t *input2,
4991 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004992 int expected_status_arg,
4993 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004994{
Ronald Cron5425a212020-08-04 14:58:35 +02004995 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4996 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004997 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004998 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004999 size_t bits = bits_arg;
5000 psa_status_t expected_status = expected_status_arg;
5001 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5002 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5003 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5004
5005 PSA_ASSERT( psa_crypto_init( ) );
5006
5007 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5008 psa_set_key_algorithm( &base_attributes, alg );
5009 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5010 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005011 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005012
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005013 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5014 input1->x, input1->len,
5015 input2->x, input2->len,
5016 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005017 goto exit;
5018
5019 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5020 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005021 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005022 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005023
5024 psa_status_t status =
5025 psa_key_derivation_output_key( &derived_attributes,
5026 &operation,
5027 &derived_key );
5028 if( is_large_output > 0 )
5029 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5030 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005031
5032exit:
5033 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005034 psa_destroy_key( base_key );
5035 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005036 PSA_DONE( );
5037}
5038/* END_CASE */
5039
5040/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005041void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005042 int our_key_type_arg, int our_key_alg_arg,
5043 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005044 int expected_status_arg )
5045{
Ronald Cron5425a212020-08-04 14:58:35 +02005046 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005047 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005048 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005049 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005052 psa_status_t expected_status = expected_status_arg;
5053 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005054
Gilles Peskine8817f612018-12-18 00:18:46 +01005055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005056
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005057 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005058 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005059 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005060 PSA_ASSERT( psa_import_key( &attributes,
5061 our_key_data->x, our_key_data->len,
5062 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005063
Gilles Peskine77f40d82019-04-11 21:27:06 +02005064 /* The tests currently include inputs that should fail at either step.
5065 * Test cases that fail at the setup step should be changed to call
5066 * key_derivation_setup instead, and this function should be renamed
5067 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005068 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005069 if( status == PSA_SUCCESS )
5070 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005071 TEST_EQUAL( psa_key_derivation_key_agreement(
5072 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5073 our_key,
5074 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005075 expected_status );
5076 }
5077 else
5078 {
5079 TEST_ASSERT( status == expected_status );
5080 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005081
5082exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005084 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005085 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005086}
5087/* END_CASE */
5088
5089/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005090void raw_key_agreement( int alg_arg,
5091 int our_key_type_arg, data_t *our_key_data,
5092 data_t *peer_key_data,
5093 data_t *expected_output )
5094{
Ronald Cron5425a212020-08-04 14:58:35 +02005095 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005096 psa_algorithm_t alg = alg_arg;
5097 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005099 unsigned char *output = NULL;
5100 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005101 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005102
Gilles Peskinef0cba732019-04-11 22:12:38 +02005103 PSA_ASSERT( psa_crypto_init( ) );
5104
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005105 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5106 psa_set_key_algorithm( &attributes, alg );
5107 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005108 PSA_ASSERT( psa_import_key( &attributes,
5109 our_key_data->x, our_key_data->len,
5110 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005111
gabor-mezei-armceface22021-01-21 12:26:17 +01005112 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5113 key_bits = psa_get_key_bits( &attributes );
5114
Gilles Peskine7d150292022-04-13 23:25:52 +02005115 /* Validate size macros */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02005116 TEST_LE_U( expected_output->len,
5117 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5118 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
5119 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine7d150292022-04-13 23:25:52 +02005120
5121 /* Good case with exact output size */
5122 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02005123 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5124 peer_key_data->x, peer_key_data->len,
5125 output, expected_output->len,
5126 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005127 ASSERT_COMPARE( output, output_length,
5128 expected_output->x, expected_output->len );
Gilles Peskine7d150292022-04-13 23:25:52 +02005129 mbedtls_free( output );
5130 output = NULL;
5131 output_length = ~0;
5132
5133 /* Larger buffer */
5134 ASSERT_ALLOC( output, expected_output->len + 1 );
5135 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5136 peer_key_data->x, peer_key_data->len,
5137 output, expected_output->len + 1,
5138 &output_length ) );
5139 ASSERT_COMPARE( output, output_length,
5140 expected_output->x, expected_output->len );
5141 mbedtls_free( output );
5142 output = NULL;
5143 output_length = ~0;
5144
5145 /* Buffer too small */
5146 ASSERT_ALLOC( output, expected_output->len - 1 );
5147 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
5148 peer_key_data->x, peer_key_data->len,
5149 output, expected_output->len - 1,
5150 &output_length ),
5151 PSA_ERROR_BUFFER_TOO_SMALL );
5152 /* Not required by the spec, but good robustness */
Gilles Peskine47cfdfd2022-04-14 00:12:57 +02005153 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine7d150292022-04-13 23:25:52 +02005154 mbedtls_free( output );
5155 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005156
5157exit:
5158 mbedtls_free( output );
5159 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005160 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005161}
5162/* END_CASE */
5163
5164/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005165void key_agreement_capacity( int alg_arg,
5166 int our_key_type_arg, data_t *our_key_data,
5167 data_t *peer_key_data,
5168 int expected_capacity_arg )
5169{
Ronald Cron5425a212020-08-04 14:58:35 +02005170 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005171 psa_algorithm_t alg = alg_arg;
5172 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005173 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005175 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005176 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005177
Gilles Peskine8817f612018-12-18 00:18:46 +01005178 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005179
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5181 psa_set_key_algorithm( &attributes, alg );
5182 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005183 PSA_ASSERT( psa_import_key( &attributes,
5184 our_key_data->x, our_key_data->len,
5185 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005186
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005187 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005188 PSA_ASSERT( psa_key_derivation_key_agreement(
5189 &operation,
5190 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5191 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005192 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5193 {
5194 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005195 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005196 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005197 NULL, 0 ) );
5198 }
Gilles Peskine59685592018-09-18 12:11:34 +02005199
Shaun Case0e7791f2021-12-20 21:14:10 -08005200 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005201 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005203 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005204
Gilles Peskinebf491972018-10-25 22:36:12 +02005205 /* Test the actual capacity by reading the output. */
5206 while( actual_capacity > sizeof( output ) )
5207 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005209 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005210 actual_capacity -= sizeof( output );
5211 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005213 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005215 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005216
Gilles Peskine59685592018-09-18 12:11:34 +02005217exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005219 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005220 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005221}
5222/* END_CASE */
5223
5224/* BEGIN_CASE */
5225void key_agreement_output( int alg_arg,
5226 int our_key_type_arg, data_t *our_key_data,
5227 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005228 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005229{
Ronald Cron5425a212020-08-04 14:58:35 +02005230 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005231 psa_algorithm_t alg = alg_arg;
5232 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005233 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005235 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005236
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005237 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5238 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005239
Gilles Peskine8817f612018-12-18 00:18:46 +01005240 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005241
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005242 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5243 psa_set_key_algorithm( &attributes, alg );
5244 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005245 PSA_ASSERT( psa_import_key( &attributes,
5246 our_key_data->x, our_key_data->len,
5247 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005248
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005250 PSA_ASSERT( psa_key_derivation_key_agreement(
5251 &operation,
5252 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5253 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005254 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5255 {
5256 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005258 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005259 NULL, 0 ) );
5260 }
Gilles Peskine59685592018-09-18 12:11:34 +02005261
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005262 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005263 actual_output,
5264 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005265 ASSERT_COMPARE( actual_output, expected_output1->len,
5266 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005267 if( expected_output2->len != 0 )
5268 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005270 actual_output,
5271 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005272 ASSERT_COMPARE( actual_output, expected_output2->len,
5273 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005274 }
Gilles Peskine59685592018-09-18 12:11:34 +02005275
5276exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005278 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005279 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005280 mbedtls_free( actual_output );
5281}
5282/* END_CASE */
5283
5284/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005285void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005286{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005287 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005288 unsigned char *output = NULL;
5289 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005290 size_t i;
5291 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005292
Simon Butcher49f8e312020-03-03 15:51:50 +00005293 TEST_ASSERT( bytes_arg >= 0 );
5294
Gilles Peskine91892022021-02-08 19:50:26 +01005295 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005296 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005297
Gilles Peskine8817f612018-12-18 00:18:46 +01005298 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005299
Gilles Peskinea50d7392018-06-21 10:22:13 +02005300 /* Run several times, to ensure that every output byte will be
5301 * nonzero at least once with overwhelming probability
5302 * (2^(-8*number_of_runs)). */
5303 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005304 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005305 if( bytes != 0 )
5306 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005307 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005308
Gilles Peskinea50d7392018-06-21 10:22:13 +02005309 for( i = 0; i < bytes; i++ )
5310 {
5311 if( output[i] != 0 )
5312 ++changed[i];
5313 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005314 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005315
5316 /* Check that every byte was changed to nonzero at least once. This
5317 * validates that psa_generate_random is overwriting every byte of
5318 * the output buffer. */
5319 for( i = 0; i < bytes; i++ )
5320 {
5321 TEST_ASSERT( changed[i] != 0 );
5322 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005323
5324exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005325 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005326 mbedtls_free( output );
5327 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005328}
5329/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005330
5331/* BEGIN_CASE */
5332void generate_key( int type_arg,
5333 int bits_arg,
5334 int usage_arg,
5335 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005336 int expected_status_arg,
5337 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005338{
Ronald Cron5425a212020-08-04 14:58:35 +02005339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005340 psa_key_type_t type = type_arg;
5341 psa_key_usage_t usage = usage_arg;
5342 size_t bits = bits_arg;
5343 psa_algorithm_t alg = alg_arg;
5344 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005346 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005347
Gilles Peskine8817f612018-12-18 00:18:46 +01005348 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005349
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005350 psa_set_key_usage_flags( &attributes, usage );
5351 psa_set_key_algorithm( &attributes, alg );
5352 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005353 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005354
5355 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005356 psa_status_t status = psa_generate_key( &attributes, &key );
5357
5358 if( is_large_key > 0 )
5359 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5360 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005361 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005362 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005363
5364 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005365 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005366 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5367 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005368
Gilles Peskine818ca122018-06-20 18:16:48 +02005369 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005370 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005371 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005372
5373exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005374 /*
5375 * Key attributes may have been returned by psa_get_key_attributes()
5376 * thus reset them as required.
5377 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005378 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005379
Ronald Cron5425a212020-08-04 14:58:35 +02005380 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005381 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005382}
5383/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005384
Ronald Cronee414c72021-03-18 18:50:08 +01005385/* 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 +02005386void generate_key_rsa( int bits_arg,
5387 data_t *e_arg,
5388 int expected_status_arg )
5389{
Ronald Cron5425a212020-08-04 14:58:35 +02005390 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005391 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005392 size_t bits = bits_arg;
5393 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5394 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5395 psa_status_t expected_status = expected_status_arg;
5396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5397 uint8_t *exported = NULL;
5398 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005399 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005400 size_t exported_length = SIZE_MAX;
5401 uint8_t *e_read_buffer = NULL;
5402 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005403 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005404 size_t e_read_length = SIZE_MAX;
5405
5406 if( e_arg->len == 0 ||
5407 ( e_arg->len == 3 &&
5408 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5409 {
5410 is_default_public_exponent = 1;
5411 e_read_size = 0;
5412 }
5413 ASSERT_ALLOC( e_read_buffer, e_read_size );
5414 ASSERT_ALLOC( exported, exported_size );
5415
5416 PSA_ASSERT( psa_crypto_init( ) );
5417
5418 psa_set_key_usage_flags( &attributes, usage );
5419 psa_set_key_algorithm( &attributes, alg );
5420 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5421 e_arg->x, e_arg->len ) );
5422 psa_set_key_bits( &attributes, bits );
5423
5424 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005425 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005426 if( expected_status != PSA_SUCCESS )
5427 goto exit;
5428
5429 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005430 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005431 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5432 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5433 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5434 e_read_buffer, e_read_size,
5435 &e_read_length ) );
5436 if( is_default_public_exponent )
5437 TEST_EQUAL( e_read_length, 0 );
5438 else
5439 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5440
5441 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005442 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005443 goto exit;
5444
5445 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005446 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005447 exported, exported_size,
5448 &exported_length ) );
5449 {
5450 uint8_t *p = exported;
5451 uint8_t *end = exported + exported_length;
5452 size_t len;
5453 /* RSAPublicKey ::= SEQUENCE {
5454 * modulus INTEGER, -- n
5455 * publicExponent INTEGER } -- e
5456 */
5457 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005458 MBEDTLS_ASN1_SEQUENCE |
5459 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005460 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005461 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5462 MBEDTLS_ASN1_INTEGER ) );
5463 if( len >= 1 && p[0] == 0 )
5464 {
5465 ++p;
5466 --len;
5467 }
5468 if( e_arg->len == 0 )
5469 {
5470 TEST_EQUAL( len, 3 );
5471 TEST_EQUAL( p[0], 1 );
5472 TEST_EQUAL( p[1], 0 );
5473 TEST_EQUAL( p[2], 1 );
5474 }
5475 else
5476 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5477 }
5478
5479exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005480 /*
5481 * Key attributes may have been returned by psa_get_key_attributes() or
5482 * set by psa_set_key_domain_parameters() thus reset them as required.
5483 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005484 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005485
Ronald Cron5425a212020-08-04 14:58:35 +02005486 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005487 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005488 mbedtls_free( e_read_buffer );
5489 mbedtls_free( exported );
5490}
5491/* END_CASE */
5492
Darryl Greend49a4992018-06-18 17:27:26 +01005493/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005494void persistent_key_load_key_from_storage( data_t *data,
5495 int type_arg, int bits_arg,
5496 int usage_flags_arg, int alg_arg,
5497 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005498{
Ronald Cron71016a92020-08-28 19:01:50 +02005499 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005501 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5502 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005503 psa_key_type_t type = type_arg;
5504 size_t bits = bits_arg;
5505 psa_key_usage_t usage_flags = usage_flags_arg;
5506 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005507 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005508 unsigned char *first_export = NULL;
5509 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005510 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005511 size_t first_exported_length;
5512 size_t second_exported_length;
5513
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005514 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5515 {
5516 ASSERT_ALLOC( first_export, export_size );
5517 ASSERT_ALLOC( second_export, export_size );
5518 }
Darryl Greend49a4992018-06-18 17:27:26 +01005519
Gilles Peskine8817f612018-12-18 00:18:46 +01005520 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005521
Gilles Peskinec87af662019-05-15 16:12:22 +02005522 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005523 psa_set_key_usage_flags( &attributes, usage_flags );
5524 psa_set_key_algorithm( &attributes, alg );
5525 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005526 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005527
Darryl Green0c6575a2018-11-07 16:05:30 +00005528 switch( generation_method )
5529 {
5530 case IMPORT_KEY:
5531 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005532 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005533 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005534 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005535
Darryl Green0c6575a2018-11-07 16:05:30 +00005536 case GENERATE_KEY:
5537 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005538 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005539 break;
5540
5541 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005542#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005543 {
5544 /* Create base key */
5545 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5546 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5547 psa_set_key_usage_flags( &base_attributes,
5548 PSA_KEY_USAGE_DERIVE );
5549 psa_set_key_algorithm( &base_attributes, derive_alg );
5550 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005551 PSA_ASSERT( psa_import_key( &base_attributes,
5552 data->x, data->len,
5553 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005554 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005556 PSA_ASSERT( psa_key_derivation_input_key(
5557 &operation,
5558 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005559 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005561 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005562 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5563 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005564 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005566 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005567 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005568 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005569#else
5570 TEST_ASSUME( ! "KDF not supported in this configuration" );
5571#endif
5572 break;
5573
5574 default:
5575 TEST_ASSERT( ! "generation_method not implemented in test" );
5576 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005577 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005578 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005579
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005580 /* Export the key if permitted by the key policy. */
5581 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5582 {
Ronald Cron5425a212020-08-04 14:58:35 +02005583 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005584 first_export, export_size,
5585 &first_exported_length ) );
5586 if( generation_method == IMPORT_KEY )
5587 ASSERT_COMPARE( data->x, data->len,
5588 first_export, first_exported_length );
5589 }
Darryl Greend49a4992018-06-18 17:27:26 +01005590
5591 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005592 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005593 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005594 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005595
Darryl Greend49a4992018-06-18 17:27:26 +01005596 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005597 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005598 TEST_ASSERT( mbedtls_svc_key_id_equal(
5599 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005600 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5601 PSA_KEY_LIFETIME_PERSISTENT );
5602 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5603 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005604 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005605 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005606 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005607
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005608 /* Export the key again if permitted by the key policy. */
5609 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005610 {
Ronald Cron5425a212020-08-04 14:58:35 +02005611 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005612 second_export, export_size,
5613 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005614 ASSERT_COMPARE( first_export, first_exported_length,
5615 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005616 }
5617
5618 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005619 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005620 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005621
5622exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005623 /*
5624 * Key attributes may have been returned by psa_get_key_attributes()
5625 * thus reset them as required.
5626 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005627 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005628
Darryl Greend49a4992018-06-18 17:27:26 +01005629 mbedtls_free( first_export );
5630 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005631 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005632 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005633 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005634 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005635}
5636/* END_CASE */