blob: b4495f0413b2c45eb3b70fac0a2a2b0f20deee80 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskinef6279312021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Dave Rodgman34b147d2021-06-23 12:49:59 +010022/* Assert that an operation is (not) active.
23 * This serves as a proxy for checking if the operation is aborted. */
24#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
25#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
26
Jaeden Amerof24c7f82018-06-27 17:20:43 +010027/** An invalid export length that will never be set by psa_export_key(). */
28static const size_t INVALID_EXPORT_LENGTH = ~0U;
29
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030/** Test if a buffer contains a constant byte value.
31 *
32 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 *
34 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020035 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 * \param size Size of the buffer in bytes.
37 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 * \return 1 if the buffer is all-bits-zero.
39 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042{
43 size_t i;
44 for( i = 0; i < size; i++ )
45 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020046 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020047 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020049 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020050}
Gilles Peskine818ca122018-06-20 18:16:48 +020051
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
53static int asn1_write_10x( unsigned char **p,
54 unsigned char *start,
55 size_t bits,
56 unsigned char x )
57{
58 int ret;
59 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020060 if( bits == 0 )
61 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
62 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020063 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030064 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
66 *p -= len;
67 ( *p )[len-1] = x;
68 if( bits % 8 == 0 )
69 ( *p )[1] |= 1;
70 else
71 ( *p )[0] |= 1 << ( bits % 8 );
72 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
74 MBEDTLS_ASN1_INTEGER ) );
75 return( len );
76}
77
78static int construct_fake_rsa_key( unsigned char *buffer,
79 size_t buffer_size,
80 unsigned char **p,
81 size_t bits,
82 int keypair )
83{
84 size_t half_bits = ( bits + 1 ) / 2;
85 int ret;
86 int len = 0;
87 /* Construct something that looks like a DER encoding of
88 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
89 * RSAPrivateKey ::= SEQUENCE {
90 * version Version,
91 * modulus INTEGER, -- n
92 * publicExponent INTEGER, -- e
93 * privateExponent INTEGER, -- d
94 * prime1 INTEGER, -- p
95 * prime2 INTEGER, -- q
96 * exponent1 INTEGER, -- d mod (p-1)
97 * exponent2 INTEGER, -- d mod (q-1)
98 * coefficient INTEGER, -- (inverse of q) mod p
99 * otherPrimeInfos OtherPrimeInfos OPTIONAL
100 * }
101 * Or, for a public key, the same structure with only
102 * version, modulus and publicExponent.
103 */
104 *p = buffer + buffer_size;
105 if( keypair )
106 {
107 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
112 asn1_write_10x( p, buffer, half_bits, 1 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* q */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
116 asn1_write_10x( p, buffer, half_bits, 3 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* d */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 }
120 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
121 asn1_write_10x( p, buffer, 17, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* n */
123 asn1_write_10x( p, buffer, bits, 1 ) );
124 if( keypair )
125 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
126 mbedtls_asn1_write_int( p, buffer, 0 ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
128 {
129 const unsigned char tag =
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
132 }
133 return( len );
134}
135
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100136int exercise_mac_setup( psa_key_type_t key_type,
137 const unsigned char *key_bytes,
138 size_t key_length,
139 psa_algorithm_t alg,
140 psa_mac_operation_t *operation,
141 psa_status_t *status )
142{
Ronald Cron5425a212020-08-04 14:58:35 +0200143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200147 psa_set_key_algorithm( &attributes, alg );
148 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200149 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150
Ronald Cron5425a212020-08-04 14:58:35 +0200151 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100152 /* Whether setup succeeded or failed, abort must succeed. */
153 PSA_ASSERT( psa_mac_abort( operation ) );
154 /* If setup failed, reproduce the failure, so that the caller can
155 * test the resulting state of the operation object. */
156 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 {
Ronald Cron5425a212020-08-04 14:58:35 +0200158 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 }
160
Ronald Cron5425a212020-08-04 14:58:35 +0200161 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162 return( 1 );
163
164exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200165 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 return( 0 );
167}
168
169int exercise_cipher_setup( psa_key_type_t key_type,
170 const unsigned char *key_bytes,
171 size_t key_length,
172 psa_algorithm_t alg,
173 psa_cipher_operation_t *operation,
174 psa_status_t *status )
175{
Ronald Cron5425a212020-08-04 14:58:35 +0200176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
180 psa_set_key_algorithm( &attributes, alg );
181 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200182 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Ronald Cron5425a212020-08-04 14:58:35 +0200184 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100185 /* Whether setup succeeded or failed, abort must succeed. */
186 PSA_ASSERT( psa_cipher_abort( operation ) );
187 /* If setup failed, reproduce the failure, so that the caller can
188 * test the resulting state of the operation object. */
189 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 {
Ronald Cron5425a212020-08-04 14:58:35 +0200191 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 1 );
197
198exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200199 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 return( 0 );
201}
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204{
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 uint8_t buffer[1];
208 size_t length;
209 int ok = 0;
210
Ronald Cronecfb2372020-07-23 17:13:42 +0200211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000216 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 TEST_EQUAL(
218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
226
Ronald Cron5425a212020-08-04 14:58:35 +0200227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000228 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000231 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233 ok = 1;
234
235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236 /*
237 * Key attributes may have been returned by psa_get_key_attributes()
238 * thus reset them as required.
239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100241
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 return( ok );
243}
244
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200245/* Assert that a key isn't reported as having a slot number. */
246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
248 do \
249 { \
250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
251 TEST_EQUAL( psa_get_key_slot_number( \
252 attributes, \
253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
254 PSA_ERROR_INVALID_ARGUMENT ); \
255 } \
256 while( 0 )
257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
259 ( (void) 0 )
260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
261
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100262/* An overapproximation of the amount of storage needed for a key of the
263 * given type and with the given content. The API doesn't make it easy
264 * to find a good value for the size. The current implementation doesn't
265 * care about the value anyway. */
266#define KEY_BITS_FROM_DATA( type, data ) \
267 ( data )->len
268
Darryl Green0c6575a2018-11-07 16:05:30 +0000269typedef enum {
270 IMPORT_KEY = 0,
271 GENERATE_KEY = 1,
272 DERIVE_KEY = 2
273} generate_method;
274
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275/* END_HEADER */
276
277/* BEGIN_DEPENDENCIES
278 * depends_on:MBEDTLS_PSA_CRYPTO_C
279 * END_DEPENDENCIES
280 */
281
282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200283void static_checks( )
284{
285 size_t max_truncated_mac_size =
286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
287
288 /* Check that the length for a truncated MAC always fits in the algorithm
289 * encoding. The shifted mask is the maximum truncated value. The
290 * untruncated algorithm may be one byte larger. */
291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100292
293#if defined(MBEDTLS_TEST_DEPRECATED)
294 /* Check deprecated constants. */
295 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
296 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
297 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
298 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
299 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
300 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
301 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
302 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100303
Paul Elliott8ff510a2020-06-02 17:19:28 +0100304 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
324 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
328 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
329 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
330 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
331 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
333 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
334
335 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
336 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
337 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
338 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
339 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
340 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
341 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
342 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100343
Paul Elliott75e27032020-06-03 15:17:39 +0100344 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
345 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
347 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
348 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
349
350 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
351 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100352#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200353}
354/* END_CASE */
355
356/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200357void import_with_policy( int type_arg,
358 int usage_arg, int alg_arg,
359 int expected_status_arg )
360{
361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
362 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200363 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200364 psa_key_type_t type = type_arg;
365 psa_key_usage_t usage = usage_arg;
366 psa_algorithm_t alg = alg_arg;
367 psa_status_t expected_status = expected_status_arg;
368 const uint8_t key_material[16] = {0};
369 psa_status_t status;
370
371 PSA_ASSERT( psa_crypto_init( ) );
372
373 psa_set_key_type( &attributes, type );
374 psa_set_key_usage_flags( &attributes, usage );
375 psa_set_key_algorithm( &attributes, alg );
376
377 status = psa_import_key( &attributes,
378 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200379 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380 TEST_EQUAL( status, expected_status );
381 if( status != PSA_SUCCESS )
382 goto exit;
383
Ronald Cron5425a212020-08-04 14:58:35 +0200384 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200385 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
386 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
387 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200388 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200389
Ronald Cron5425a212020-08-04 14:58:35 +0200390 PSA_ASSERT( psa_destroy_key( key ) );
391 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200392
393exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100394 /*
395 * Key attributes may have been returned by psa_get_key_attributes()
396 * thus reset them as required.
397 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200398 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100399
400 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200401 PSA_DONE( );
402}
403/* END_CASE */
404
405/* BEGIN_CASE */
406void import_with_data( data_t *data, int type_arg,
407 int attr_bits_arg,
408 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200409{
410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
411 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200413 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200414 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200415 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100416 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417
Gilles Peskine8817f612018-12-18 00:18:46 +0100418 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419
Gilles Peskine4747d192019-04-17 15:05:45 +0200420 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200421 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200422
Ronald Cron5425a212020-08-04 14:58:35 +0200423 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100424 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200425 if( status != PSA_SUCCESS )
426 goto exit;
427
Ronald Cron5425a212020-08-04 14:58:35 +0200428 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200429 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200430 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200431 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200432 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200433
Ronald Cron5425a212020-08-04 14:58:35 +0200434 PSA_ASSERT( psa_destroy_key( key ) );
435 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100436
437exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100438 /*
439 * Key attributes may have been returned by psa_get_key_attributes()
440 * thus reset them as required.
441 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200442 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100443
444 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200445 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100446}
447/* END_CASE */
448
449/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200450void import_large_key( int type_arg, int byte_size_arg,
451 int expected_status_arg )
452{
453 psa_key_type_t type = type_arg;
454 size_t byte_size = byte_size_arg;
455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
456 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200457 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200458 psa_status_t status;
459 uint8_t *buffer = NULL;
460 size_t buffer_size = byte_size + 1;
461 size_t n;
462
Steven Cooreman69967ce2021-01-18 18:01:08 +0100463 /* Skip the test case if the target running the test cannot
464 * accomodate large keys due to heap size constraints */
465 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200466 memset( buffer, 'K', byte_size );
467
468 PSA_ASSERT( psa_crypto_init( ) );
469
470 /* Try importing the key */
471 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
472 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200473 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100474 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200475 TEST_EQUAL( status, expected_status );
476
477 if( status == PSA_SUCCESS )
478 {
Ronald Cron5425a212020-08-04 14:58:35 +0200479 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200480 TEST_EQUAL( psa_get_key_type( &attributes ), type );
481 TEST_EQUAL( psa_get_key_bits( &attributes ),
482 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200483 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200484 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200485 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200486 for( n = 0; n < byte_size; n++ )
487 TEST_EQUAL( buffer[n], 'K' );
488 for( n = byte_size; n < buffer_size; n++ )
489 TEST_EQUAL( buffer[n], 0 );
490 }
491
492exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100493 /*
494 * Key attributes may have been returned by psa_get_key_attributes()
495 * thus reset them as required.
496 */
497 psa_reset_key_attributes( &attributes );
498
Ronald Cron5425a212020-08-04 14:58:35 +0200499 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200500 PSA_DONE( );
501 mbedtls_free( buffer );
502}
503/* END_CASE */
504
505/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200506void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
507{
Ronald Cron5425a212020-08-04 14:58:35 +0200508 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509 size_t bits = bits_arg;
510 psa_status_t expected_status = expected_status_arg;
511 psa_status_t status;
512 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200513 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200514 size_t buffer_size = /* Slight overapproximations */
515 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200516 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200517 unsigned char *p;
518 int ret;
519 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200521
Gilles Peskine8817f612018-12-18 00:18:46 +0100522 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200523 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200524
525 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
526 bits, keypair ) ) >= 0 );
527 length = ret;
528
529 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200530 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200531 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100532 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200533
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200534 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200535 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200536
537exit:
538 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200539 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200540}
541/* END_CASE */
542
543/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300544void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300545 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200546 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100547 int expected_bits,
548 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200549 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550 int canonical_input )
551{
Ronald Cron5425a212020-08-04 14:58:35 +0200552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200554 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200555 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 unsigned char *exported = NULL;
558 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100560 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100561 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200562 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200563 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564
Moran Pekercb088e72018-07-17 17:36:59 +0300565 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200566 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200568 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570
Gilles Peskine4747d192019-04-17 15:05:45 +0200571 psa_set_key_usage_flags( &attributes, usage_arg );
572 psa_set_key_algorithm( &attributes, alg );
573 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700574
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200576 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100577
578 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200579 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200580 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
581 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200582 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583
584 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200585 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100586 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100587
588 /* The exported length must be set by psa_export_key() to a value between 0
589 * and export_size. On errors, the exported length must be 0. */
590 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
591 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
592 TEST_ASSERT( exported_length <= export_size );
593
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200594 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200595 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200597 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100598 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100599 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200600 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601
Gilles Peskineea38a922021-02-13 00:05:16 +0100602 /* Run sanity checks on the exported key. For non-canonical inputs,
603 * this validates the canonical representations. For canonical inputs,
604 * this doesn't directly validate the implementation, but it still helps
605 * by cross-validating the test data with the sanity check code. */
606 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200607 goto exit;
608
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200610 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100611 else
612 {
Ronald Cron5425a212020-08-04 14:58:35 +0200613 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200614 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200615 &key2 ) );
616 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100617 reexported,
618 export_size,
619 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200620 ASSERT_COMPARE( exported, exported_length,
621 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200622 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100624 TEST_ASSERT( exported_length <=
625 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
626 psa_get_key_bits( &got_attributes ) ) );
627 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628
629destroy:
630 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200631 PSA_ASSERT( psa_destroy_key( key ) );
632 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100633
634exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100635 /*
636 * Key attributes may have been returned by psa_get_key_attributes()
637 * thus reset them as required.
638 */
639 psa_reset_key_attributes( &got_attributes );
640
itayzafrir3e02b3b2018-06-12 17:06:52 +0300641 mbedtls_free( exported );
642 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200643 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100644}
645/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100646
Moran Pekerf709f4a2018-06-06 17:26:04 +0300647/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300648void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200649 int type_arg,
650 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100651 int export_size_delta,
652 int expected_export_status_arg,
653 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300654{
Ronald Cron5425a212020-08-04 14:58:35 +0200655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300656 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200657 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200658 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300659 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100661 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100662 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300664
Gilles Peskine8817f612018-12-18 00:18:46 +0100665 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300666
Gilles Peskine4747d192019-04-17 15:05:45 +0200667 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
668 psa_set_key_algorithm( &attributes, alg );
669 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300670
671 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200672 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300673
Gilles Peskine49c25912018-10-29 15:15:31 +0100674 /* Export the public key */
675 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200676 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200677 exported, export_size,
678 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100679 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100680 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100681 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200682 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100683 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200684 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200685 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100686 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100687 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100688 TEST_ASSERT( expected_public_key->len <=
689 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
690 TEST_ASSERT( expected_public_key->len <=
691 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100692 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
693 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100694 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300695
696exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100697 /*
698 * Key attributes may have been returned by psa_get_key_attributes()
699 * thus reset them as required.
700 */
701 psa_reset_key_attributes( &attributes );
702
itayzafrir3e02b3b2018-06-12 17:06:52 +0300703 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200704 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200705 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300706}
707/* END_CASE */
708
Gilles Peskine20035e32018-02-03 22:44:14 +0100709/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200710void import_and_exercise_key( data_t *data,
711 int type_arg,
712 int bits_arg,
713 int alg_arg )
714{
Ronald Cron5425a212020-08-04 14:58:35 +0200715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200716 psa_key_type_t type = type_arg;
717 size_t bits = bits_arg;
718 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100719 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200721 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200722
Gilles Peskine8817f612018-12-18 00:18:46 +0100723 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200724
Gilles Peskine4747d192019-04-17 15:05:45 +0200725 psa_set_key_usage_flags( &attributes, usage );
726 psa_set_key_algorithm( &attributes, alg );
727 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200728
729 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200730 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200731
732 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200733 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200734 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
735 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736
737 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100738 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200739 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200740
Ronald Cron5425a212020-08-04 14:58:35 +0200741 PSA_ASSERT( psa_destroy_key( key ) );
742 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200743
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200744exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100745 /*
746 * Key attributes may have been returned by psa_get_key_attributes()
747 * thus reset them as required.
748 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200749 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100750
751 psa_reset_key_attributes( &attributes );
752 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200753 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200754}
755/* END_CASE */
756
757/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100758void effective_key_attributes( int type_arg, int expected_type_arg,
759 int bits_arg, int expected_bits_arg,
760 int usage_arg, int expected_usage_arg,
761 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200762{
Ronald Cron5425a212020-08-04 14:58:35 +0200763 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100764 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100765 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100766 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100767 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200768 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100769 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200770 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100771 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200773
Gilles Peskine8817f612018-12-18 00:18:46 +0100774 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200775
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200776 psa_set_key_usage_flags( &attributes, usage );
777 psa_set_key_algorithm( &attributes, alg );
778 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100779 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200780
Ronald Cron5425a212020-08-04 14:58:35 +0200781 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100782 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200783
Ronald Cron5425a212020-08-04 14:58:35 +0200784 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100785 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
786 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
787 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
788 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200789
790exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100791 /*
792 * Key attributes may have been returned by psa_get_key_attributes()
793 * thus reset them as required.
794 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200795 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100796
797 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200798 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200799}
800/* END_CASE */
801
802/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100803void check_key_policy( int type_arg, int bits_arg,
804 int usage_arg, int alg_arg )
805{
806 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
807 usage_arg, usage_arg, alg_arg, alg_arg );
808 goto exit;
809}
810/* END_CASE */
811
812/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200813void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000814{
815 /* Test each valid way of initializing the object, except for `= {0}`, as
816 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
817 * though it's OK by the C standard. We could test for this, but we'd need
818 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200819 psa_key_attributes_t func = psa_key_attributes_init( );
820 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
821 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000822
823 memset( &zero, 0, sizeof( zero ) );
824
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200825 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
826 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
827 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000828
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200829 TEST_EQUAL( psa_get_key_type( &func ), 0 );
830 TEST_EQUAL( psa_get_key_type( &init ), 0 );
831 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
832
833 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
834 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
835 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
836
837 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
838 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
839 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
840
841 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
842 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
843 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000844}
845/* END_CASE */
846
847/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200848void mac_key_policy( int policy_usage,
849 int policy_alg,
850 int key_type,
851 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100852 int exercise_alg,
853 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200854{
Ronald Cron5425a212020-08-04 14:58:35 +0200855 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000857 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200858 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100859 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200861
Gilles Peskine8817f612018-12-18 00:18:46 +0100862 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200863
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200864 psa_set_key_usage_flags( &attributes, policy_usage );
865 psa_set_key_algorithm( &attributes, policy_alg );
866 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200867
Gilles Peskine049c7532019-05-15 20:22:09 +0200868 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200869 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200870
Ronald Cron5425a212020-08-04 14:58:35 +0200871 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100872 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100873 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100874 else
875 TEST_EQUAL( status, expected_status );
876
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200877 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200878
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200879 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200880 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100881 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100882 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100883 else
884 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200885
886exit:
887 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200888 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200889 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200890}
891/* END_CASE */
892
893/* BEGIN_CASE */
894void cipher_key_policy( int policy_usage,
895 int policy_alg,
896 int key_type,
897 data_t *key_data,
898 int exercise_alg )
899{
Ronald Cron5425a212020-08-04 14:58:35 +0200900 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000902 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903 psa_status_t status;
904
Gilles Peskine8817f612018-12-18 00:18:46 +0100905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200906
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200907 psa_set_key_usage_flags( &attributes, policy_usage );
908 psa_set_key_algorithm( &attributes, policy_alg );
909 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910
Gilles Peskine049c7532019-05-15 20:22:09 +0200911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200912 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200913
Ronald Cron5425a212020-08-04 14:58:35 +0200914 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 if( policy_alg == exercise_alg &&
916 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100917 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100919 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200920 psa_cipher_abort( &operation );
921
Ronald Cron5425a212020-08-04 14:58:35 +0200922 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200923 if( policy_alg == exercise_alg &&
924 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100925 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200926 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100927 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928
929exit:
930 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200931 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200932 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200933}
934/* END_CASE */
935
936/* BEGIN_CASE */
937void aead_key_policy( int policy_usage,
938 int policy_alg,
939 int key_type,
940 data_t *key_data,
941 int nonce_length_arg,
942 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100943 int exercise_alg,
944 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945{
Ronald Cron5425a212020-08-04 14:58:35 +0200946 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100949 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200950 unsigned char nonce[16] = {0};
951 size_t nonce_length = nonce_length_arg;
952 unsigned char tag[16];
953 size_t tag_length = tag_length_arg;
954 size_t output_length;
955
956 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
957 TEST_ASSERT( tag_length <= sizeof( tag ) );
958
Gilles Peskine8817f612018-12-18 00:18:46 +0100959 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200961 psa_set_key_usage_flags( &attributes, policy_usage );
962 psa_set_key_algorithm( &attributes, policy_alg );
963 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964
Gilles Peskine049c7532019-05-15 20:22:09 +0200965 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200966 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200967
Ronald Cron5425a212020-08-04 14:58:35 +0200968 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200969 nonce, nonce_length,
970 NULL, 0,
971 NULL, 0,
972 tag, tag_length,
973 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100974 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
975 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100977 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978
979 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200980 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981 nonce, nonce_length,
982 NULL, 0,
983 tag, tag_length,
984 NULL, 0,
985 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100986 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
987 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
988 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100989 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200990 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100991 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992
993exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200994 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200995 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200996}
997/* END_CASE */
998
999/* BEGIN_CASE */
1000void asymmetric_encryption_key_policy( int policy_usage,
1001 int policy_alg,
1002 int key_type,
1003 data_t *key_data,
1004 int exercise_alg )
1005{
Ronald Cron5425a212020-08-04 14:58:35 +02001006 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001008 psa_status_t status;
1009 size_t key_bits;
1010 size_t buffer_length;
1011 unsigned char *buffer = NULL;
1012 size_t output_length;
1013
Gilles Peskine8817f612018-12-18 00:18:46 +01001014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001015
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001016 psa_set_key_usage_flags( &attributes, policy_usage );
1017 psa_set_key_algorithm( &attributes, policy_alg );
1018 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001019
Gilles Peskine049c7532019-05-15 20:22:09 +02001020 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001021 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001022
Ronald Cron5425a212020-08-04 14:58:35 +02001023 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001024 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1026 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001027 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001028
Ronald Cron5425a212020-08-04 14:58:35 +02001029 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001030 NULL, 0,
1031 NULL, 0,
1032 buffer, buffer_length,
1033 &output_length );
1034 if( policy_alg == exercise_alg &&
1035 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001036 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001037 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001038 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001039
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001040 if( buffer_length != 0 )
1041 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001042 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043 buffer, buffer_length,
1044 NULL, 0,
1045 buffer, buffer_length,
1046 &output_length );
1047 if( policy_alg == exercise_alg &&
1048 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001049 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001050 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001051 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001052
1053exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001054 /*
1055 * Key attributes may have been returned by psa_get_key_attributes()
1056 * thus reset them as required.
1057 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001058 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001059
1060 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001061 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001062 mbedtls_free( buffer );
1063}
1064/* END_CASE */
1065
1066/* BEGIN_CASE */
1067void asymmetric_signature_key_policy( int policy_usage,
1068 int policy_alg,
1069 int key_type,
1070 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001071 int exercise_alg,
1072 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001073{
Ronald Cron5425a212020-08-04 14:58:35 +02001074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001076 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001077 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1078 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1079 * compatible with the policy and `payload_length_arg` is supposed to be
1080 * a valid input length to sign. If `payload_length_arg <= 0`,
1081 * `exercise_alg` is supposed to be forbidden by the policy. */
1082 int compatible_alg = payload_length_arg > 0;
1083 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001084 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001085 size_t signature_length;
1086
Gilles Peskine8817f612018-12-18 00:18:46 +01001087 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001089 psa_set_key_usage_flags( &attributes, policy_usage );
1090 psa_set_key_algorithm( &attributes, policy_alg );
1091 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001092
Gilles Peskine049c7532019-05-15 20:22:09 +02001093 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001094 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001095
Ronald Cron5425a212020-08-04 14:58:35 +02001096 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001097 payload, payload_length,
1098 signature, sizeof( signature ),
1099 &signature_length );
1100 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001101 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001102 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001103 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104
1105 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001106 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001107 payload, payload_length,
1108 signature, sizeof( signature ) );
1109 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001110 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001111 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001112 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001113
1114exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001115 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001116 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001117}
1118/* END_CASE */
1119
Janos Follathba3fab92019-06-11 14:50:16 +01001120/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001121void derive_key_policy( int policy_usage,
1122 int policy_alg,
1123 int key_type,
1124 data_t *key_data,
1125 int exercise_alg )
1126{
Ronald Cron5425a212020-08-04 14:58:35 +02001127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001129 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001130 psa_status_t status;
1131
Gilles Peskine8817f612018-12-18 00:18:46 +01001132 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001133
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001134 psa_set_key_usage_flags( &attributes, policy_usage );
1135 psa_set_key_algorithm( &attributes, policy_alg );
1136 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001137
Gilles Peskine049c7532019-05-15 20:22:09 +02001138 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001139 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001140
Janos Follathba3fab92019-06-11 14:50:16 +01001141 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1142
1143 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1144 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001145 {
Janos Follathba3fab92019-06-11 14:50:16 +01001146 PSA_ASSERT( psa_key_derivation_input_bytes(
1147 &operation,
1148 PSA_KEY_DERIVATION_INPUT_SEED,
1149 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001150 }
Janos Follathba3fab92019-06-11 14:50:16 +01001151
1152 status = psa_key_derivation_input_key( &operation,
1153 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001154 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001155
Gilles Peskineea0fb492018-07-12 17:17:20 +02001156 if( policy_alg == exercise_alg &&
1157 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001158 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001159 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001160 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161
1162exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001163 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001164 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001165 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001166}
1167/* END_CASE */
1168
1169/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001170void agreement_key_policy( int policy_usage,
1171 int policy_alg,
1172 int key_type_arg,
1173 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001174 int exercise_alg,
1175 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001176{
Ronald Cron5425a212020-08-04 14:58:35 +02001177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001179 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001180 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001182 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001183
Gilles Peskine8817f612018-12-18 00:18:46 +01001184 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001186 psa_set_key_usage_flags( &attributes, policy_usage );
1187 psa_set_key_algorithm( &attributes, policy_alg );
1188 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001189
Gilles Peskine049c7532019-05-15 20:22:09 +02001190 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001191 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001193 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001194 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001195
Steven Cooremance48e852020-10-05 16:02:45 +02001196 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001197
1198exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001199 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001200 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001201 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001202}
1203/* END_CASE */
1204
1205/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001206void key_policy_alg2( int key_type_arg, data_t *key_data,
1207 int usage_arg, int alg_arg, int alg2_arg )
1208{
Ronald Cron5425a212020-08-04 14:58:35 +02001209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001210 psa_key_type_t key_type = key_type_arg;
1211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1212 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1213 psa_key_usage_t usage = usage_arg;
1214 psa_algorithm_t alg = alg_arg;
1215 psa_algorithm_t alg2 = alg2_arg;
1216
1217 PSA_ASSERT( psa_crypto_init( ) );
1218
1219 psa_set_key_usage_flags( &attributes, usage );
1220 psa_set_key_algorithm( &attributes, alg );
1221 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1222 psa_set_key_type( &attributes, key_type );
1223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001224 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001225
Ronald Cron5425a212020-08-04 14:58:35 +02001226 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001227 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1228 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1229 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1230
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001231 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001232 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001233 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001234 goto exit;
1235
1236exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001237 /*
1238 * Key attributes may have been returned by psa_get_key_attributes()
1239 * thus reset them as required.
1240 */
1241 psa_reset_key_attributes( &got_attributes );
1242
Ronald Cron5425a212020-08-04 14:58:35 +02001243 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001244 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001245}
1246/* END_CASE */
1247
1248/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001249void raw_agreement_key_policy( int policy_usage,
1250 int policy_alg,
1251 int key_type_arg,
1252 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001253 int exercise_alg,
1254 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001255{
Ronald Cron5425a212020-08-04 14:58:35 +02001256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001258 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001259 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001261 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001262
1263 PSA_ASSERT( psa_crypto_init( ) );
1264
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001265 psa_set_key_usage_flags( &attributes, policy_usage );
1266 psa_set_key_algorithm( &attributes, policy_alg );
1267 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001268
Gilles Peskine049c7532019-05-15 20:22:09 +02001269 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001270 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001271
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001272 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001273
Steven Cooremance48e852020-10-05 16:02:45 +02001274 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001275
1276exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001277 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001278 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001279 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001280}
1281/* END_CASE */
1282
1283/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001284void copy_success( int source_usage_arg,
1285 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001286 int type_arg, data_t *material,
1287 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001288 int target_usage_arg,
1289 int target_alg_arg, int target_alg2_arg,
1290 int expected_usage_arg,
1291 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001292{
Gilles Peskineca25db92019-04-19 11:43:08 +02001293 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1294 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001295 psa_key_usage_t expected_usage = expected_usage_arg;
1296 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001297 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001298 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1299 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001300 uint8_t *export_buffer = NULL;
1301
Gilles Peskine57ab7212019-01-28 13:03:09 +01001302 PSA_ASSERT( psa_crypto_init( ) );
1303
Gilles Peskineca25db92019-04-19 11:43:08 +02001304 /* Prepare the source key. */
1305 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1306 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001307 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001308 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001309 PSA_ASSERT( psa_import_key( &source_attributes,
1310 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001311 &source_key ) );
1312 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001313
Gilles Peskineca25db92019-04-19 11:43:08 +02001314 /* Prepare the target attributes. */
1315 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001316 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001317 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001318 /* Set volatile lifetime to reset the key identifier to 0. */
1319 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1320 }
1321
Gilles Peskineca25db92019-04-19 11:43:08 +02001322 if( target_usage_arg != -1 )
1323 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1324 if( target_alg_arg != -1 )
1325 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001326 if( target_alg2_arg != -1 )
1327 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001328
1329 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001330 PSA_ASSERT( psa_copy_key( source_key,
1331 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001332
1333 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001334 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001335
1336 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001337 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001338 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1339 psa_get_key_type( &target_attributes ) );
1340 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1341 psa_get_key_bits( &target_attributes ) );
1342 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1343 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001344 TEST_EQUAL( expected_alg2,
1345 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001346 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1347 {
1348 size_t length;
1349 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001350 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001351 material->len, &length ) );
1352 ASSERT_COMPARE( material->x, material->len,
1353 export_buffer, length );
1354 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001355
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001356 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001357 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001358 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001359 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360
Ronald Cron5425a212020-08-04 14:58:35 +02001361 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001362
1363exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001364 /*
1365 * Source and target key attributes may have been returned by
1366 * psa_get_key_attributes() thus reset them as required.
1367 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001368 psa_reset_key_attributes( &source_attributes );
1369 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001370
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001371 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001372 mbedtls_free( export_buffer );
1373}
1374/* END_CASE */
1375
1376/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001377void copy_fail( int source_usage_arg,
1378 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001379 int type_arg, data_t *material,
1380 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001381 int target_usage_arg,
1382 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001383 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001384 int expected_status_arg )
1385{
1386 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1387 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001388 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1389 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001390 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001391
1392 PSA_ASSERT( psa_crypto_init( ) );
1393
1394 /* Prepare the source key. */
1395 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1396 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001397 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001398 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001399 PSA_ASSERT( psa_import_key( &source_attributes,
1400 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001401 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001402
1403 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001404 psa_set_key_id( &target_attributes, key_id );
1405 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001406 psa_set_key_type( &target_attributes, target_type_arg );
1407 psa_set_key_bits( &target_attributes, target_bits_arg );
1408 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1409 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001410 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001411
1412 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001413 TEST_EQUAL( psa_copy_key( source_key,
1414 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001415 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001416
Ronald Cron5425a212020-08-04 14:58:35 +02001417 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001418
Gilles Peskine4a644642019-05-03 17:14:08 +02001419exit:
1420 psa_reset_key_attributes( &source_attributes );
1421 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001422 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001423}
1424/* END_CASE */
1425
1426/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001427void hash_operation_init( )
1428{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001429 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001430 /* Test each valid way of initializing the object, except for `= {0}`, as
1431 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1432 * though it's OK by the C standard. We could test for this, but we'd need
1433 * to supress the Clang warning for the test. */
1434 psa_hash_operation_t func = psa_hash_operation_init( );
1435 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1436 psa_hash_operation_t zero;
1437
1438 memset( &zero, 0, sizeof( zero ) );
1439
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001440 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001441 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1442 PSA_ERROR_BAD_STATE );
1443 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1444 PSA_ERROR_BAD_STATE );
1445 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1446 PSA_ERROR_BAD_STATE );
1447
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001448 /* A default hash operation should be abortable without error. */
1449 PSA_ASSERT( psa_hash_abort( &func ) );
1450 PSA_ASSERT( psa_hash_abort( &init ) );
1451 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001452}
1453/* END_CASE */
1454
1455/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001456void hash_setup( int alg_arg,
1457 int expected_status_arg )
1458{
1459 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001460 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001461 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001462 psa_status_t status;
1463
Gilles Peskine8817f612018-12-18 00:18:46 +01001464 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001465
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001466 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001467 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001468
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001469 /* Whether setup succeeded or failed, abort must succeed. */
1470 PSA_ASSERT( psa_hash_abort( &operation ) );
1471
1472 /* If setup failed, reproduce the failure, so as to
1473 * test the resulting state of the operation object. */
1474 if( status != PSA_SUCCESS )
1475 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1476
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001477 /* Now the operation object should be reusable. */
1478#if defined(KNOWN_SUPPORTED_HASH_ALG)
1479 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1480 PSA_ASSERT( psa_hash_abort( &operation ) );
1481#endif
1482
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001483exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001484 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001485}
1486/* END_CASE */
1487
1488/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001489void hash_compute_fail( int alg_arg, data_t *input,
1490 int output_size_arg, int expected_status_arg )
1491{
1492 psa_algorithm_t alg = alg_arg;
1493 uint8_t *output = NULL;
1494 size_t output_size = output_size_arg;
1495 size_t output_length = INVALID_EXPORT_LENGTH;
1496 psa_status_t expected_status = expected_status_arg;
1497 psa_status_t status;
1498
1499 ASSERT_ALLOC( output, output_size );
1500
1501 PSA_ASSERT( psa_crypto_init( ) );
1502
1503 status = psa_hash_compute( alg, input->x, input->len,
1504 output, output_size, &output_length );
1505 TEST_EQUAL( status, expected_status );
1506 TEST_ASSERT( output_length <= output_size );
1507
1508exit:
1509 mbedtls_free( output );
1510 PSA_DONE( );
1511}
1512/* END_CASE */
1513
1514/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001515void hash_compare_fail( int alg_arg, data_t *input,
1516 data_t *reference_hash,
1517 int expected_status_arg )
1518{
1519 psa_algorithm_t alg = alg_arg;
1520 psa_status_t expected_status = expected_status_arg;
1521 psa_status_t status;
1522
1523 PSA_ASSERT( psa_crypto_init( ) );
1524
1525 status = psa_hash_compare( alg, input->x, input->len,
1526 reference_hash->x, reference_hash->len );
1527 TEST_EQUAL( status, expected_status );
1528
1529exit:
1530 PSA_DONE( );
1531}
1532/* END_CASE */
1533
1534/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001535void hash_compute_compare( int alg_arg, data_t *input,
1536 data_t *expected_output )
1537{
1538 psa_algorithm_t alg = alg_arg;
1539 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1540 size_t output_length = INVALID_EXPORT_LENGTH;
1541 size_t i;
1542
1543 PSA_ASSERT( psa_crypto_init( ) );
1544
1545 /* Compute with tight buffer */
1546 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001547 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001548 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001549 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001550 ASSERT_COMPARE( output, output_length,
1551 expected_output->x, expected_output->len );
1552
1553 /* Compute with larger buffer */
1554 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1555 output, sizeof( output ),
1556 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001557 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001558 ASSERT_COMPARE( output, output_length,
1559 expected_output->x, expected_output->len );
1560
1561 /* Compare with correct hash */
1562 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1563 output, output_length ) );
1564
1565 /* Compare with trailing garbage */
1566 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1567 output, output_length + 1 ),
1568 PSA_ERROR_INVALID_SIGNATURE );
1569
1570 /* Compare with truncated hash */
1571 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1572 output, output_length - 1 ),
1573 PSA_ERROR_INVALID_SIGNATURE );
1574
1575 /* Compare with corrupted value */
1576 for( i = 0; i < output_length; i++ )
1577 {
Chris Jones9634bb12021-01-20 15:56:42 +00001578 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001579 output[i] ^= 1;
1580 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1581 output, output_length ),
1582 PSA_ERROR_INVALID_SIGNATURE );
1583 output[i] ^= 1;
1584 }
1585
1586exit:
1587 PSA_DONE( );
1588}
1589/* END_CASE */
1590
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001591/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001592void hash_bad_order( )
1593{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001594 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001595 unsigned char input[] = "";
1596 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001597 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001598 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1599 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1600 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001601 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001602 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001603 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001604
Gilles Peskine8817f612018-12-18 00:18:46 +01001605 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001606
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001607 /* Call setup twice in a row. */
1608 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1609 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1610 PSA_ERROR_BAD_STATE );
1611 PSA_ASSERT( psa_hash_abort( &operation ) );
1612
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001613 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001614 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001615 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001616 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001617
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001618 /* Call update after finish. */
1619 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1620 PSA_ASSERT( psa_hash_finish( &operation,
1621 hash, sizeof( hash ), &hash_len ) );
1622 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001623 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001624 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001625
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001626 /* Call verify without calling setup beforehand. */
1627 TEST_EQUAL( psa_hash_verify( &operation,
1628 valid_hash, sizeof( valid_hash ) ),
1629 PSA_ERROR_BAD_STATE );
1630 PSA_ASSERT( psa_hash_abort( &operation ) );
1631
1632 /* Call verify after finish. */
1633 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1634 PSA_ASSERT( psa_hash_finish( &operation,
1635 hash, sizeof( hash ), &hash_len ) );
1636 TEST_EQUAL( psa_hash_verify( &operation,
1637 valid_hash, sizeof( valid_hash ) ),
1638 PSA_ERROR_BAD_STATE );
1639 PSA_ASSERT( psa_hash_abort( &operation ) );
1640
1641 /* Call verify twice in a row. */
1642 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1643 PSA_ASSERT( psa_hash_verify( &operation,
1644 valid_hash, sizeof( valid_hash ) ) );
1645 TEST_EQUAL( psa_hash_verify( &operation,
1646 valid_hash, sizeof( valid_hash ) ),
1647 PSA_ERROR_BAD_STATE );
1648 PSA_ASSERT( psa_hash_abort( &operation ) );
1649
1650 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001651 TEST_EQUAL( psa_hash_finish( &operation,
1652 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001653 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001654 PSA_ASSERT( psa_hash_abort( &operation ) );
1655
1656 /* Call finish twice in a row. */
1657 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1658 PSA_ASSERT( psa_hash_finish( &operation,
1659 hash, sizeof( hash ), &hash_len ) );
1660 TEST_EQUAL( psa_hash_finish( &operation,
1661 hash, sizeof( hash ), &hash_len ),
1662 PSA_ERROR_BAD_STATE );
1663 PSA_ASSERT( psa_hash_abort( &operation ) );
1664
1665 /* Call finish after calling verify. */
1666 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1667 PSA_ASSERT( psa_hash_verify( &operation,
1668 valid_hash, sizeof( valid_hash ) ) );
1669 TEST_EQUAL( psa_hash_finish( &operation,
1670 hash, sizeof( hash ), &hash_len ),
1671 PSA_ERROR_BAD_STATE );
1672 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001673
1674exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001675 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001676}
1677/* END_CASE */
1678
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001679/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001680void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001681{
1682 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001683 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1684 * appended to it */
1685 unsigned char hash[] = {
1686 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1687 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1688 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001689 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001690 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001691
Gilles Peskine8817f612018-12-18 00:18:46 +01001692 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001693
itayzafrir27e69452018-11-01 14:26:34 +02001694 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001696 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001697 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001698
itayzafrir27e69452018-11-01 14:26:34 +02001699 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001700 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001701 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001703
itayzafrir27e69452018-11-01 14:26:34 +02001704 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001706 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001707 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001708
itayzafrirec93d302018-10-18 18:01:10 +03001709exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001710 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001711}
1712/* END_CASE */
1713
Ronald Cronee414c72021-03-18 18:50:08 +01001714/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001715void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001716{
1717 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001718 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001719 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001720 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001721 size_t hash_len;
1722
Gilles Peskine8817f612018-12-18 00:18:46 +01001723 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001724
itayzafrir58028322018-10-25 10:22:01 +03001725 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001727 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001728 hash, expected_size - 1, &hash_len ),
1729 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001730
1731exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001732 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001733}
1734/* END_CASE */
1735
Ronald Cronee414c72021-03-18 18:50:08 +01001736/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001737void hash_clone_source_state( )
1738{
1739 psa_algorithm_t alg = PSA_ALG_SHA_256;
1740 unsigned char hash[PSA_HASH_MAX_SIZE];
1741 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1742 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1743 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1744 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1745 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1746 size_t hash_len;
1747
1748 PSA_ASSERT( psa_crypto_init( ) );
1749 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1750
1751 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1752 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1753 PSA_ASSERT( psa_hash_finish( &op_finished,
1754 hash, sizeof( hash ), &hash_len ) );
1755 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1756 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1757
1758 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1759 PSA_ERROR_BAD_STATE );
1760
1761 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1762 PSA_ASSERT( psa_hash_finish( &op_init,
1763 hash, sizeof( hash ), &hash_len ) );
1764 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1765 PSA_ASSERT( psa_hash_finish( &op_finished,
1766 hash, sizeof( hash ), &hash_len ) );
1767 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1768 PSA_ASSERT( psa_hash_finish( &op_aborted,
1769 hash, sizeof( hash ), &hash_len ) );
1770
1771exit:
1772 psa_hash_abort( &op_source );
1773 psa_hash_abort( &op_init );
1774 psa_hash_abort( &op_setup );
1775 psa_hash_abort( &op_finished );
1776 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001777 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001778}
1779/* END_CASE */
1780
Ronald Cronee414c72021-03-18 18:50:08 +01001781/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001782void hash_clone_target_state( )
1783{
1784 psa_algorithm_t alg = PSA_ALG_SHA_256;
1785 unsigned char hash[PSA_HASH_MAX_SIZE];
1786 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1787 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1788 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1789 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1790 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1791 size_t hash_len;
1792
1793 PSA_ASSERT( psa_crypto_init( ) );
1794
1795 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1796 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1797 PSA_ASSERT( psa_hash_finish( &op_finished,
1798 hash, sizeof( hash ), &hash_len ) );
1799 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1800 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1801
1802 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1803 PSA_ASSERT( psa_hash_finish( &op_target,
1804 hash, sizeof( hash ), &hash_len ) );
1805
1806 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1807 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1808 PSA_ERROR_BAD_STATE );
1809 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1810 PSA_ERROR_BAD_STATE );
1811
1812exit:
1813 psa_hash_abort( &op_target );
1814 psa_hash_abort( &op_init );
1815 psa_hash_abort( &op_setup );
1816 psa_hash_abort( &op_finished );
1817 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001818 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001819}
1820/* END_CASE */
1821
itayzafrir58028322018-10-25 10:22:01 +03001822/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001823void mac_operation_init( )
1824{
Jaeden Amero252ef282019-02-15 14:05:35 +00001825 const uint8_t input[1] = { 0 };
1826
Jaeden Amero769ce272019-01-04 11:48:03 +00001827 /* Test each valid way of initializing the object, except for `= {0}`, as
1828 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1829 * though it's OK by the C standard. We could test for this, but we'd need
1830 * to supress the Clang warning for the test. */
1831 psa_mac_operation_t func = psa_mac_operation_init( );
1832 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1833 psa_mac_operation_t zero;
1834
1835 memset( &zero, 0, sizeof( zero ) );
1836
Jaeden Amero252ef282019-02-15 14:05:35 +00001837 /* A freshly-initialized MAC operation should not be usable. */
1838 TEST_EQUAL( psa_mac_update( &func,
1839 input, sizeof( input ) ),
1840 PSA_ERROR_BAD_STATE );
1841 TEST_EQUAL( psa_mac_update( &init,
1842 input, sizeof( input ) ),
1843 PSA_ERROR_BAD_STATE );
1844 TEST_EQUAL( psa_mac_update( &zero,
1845 input, sizeof( input ) ),
1846 PSA_ERROR_BAD_STATE );
1847
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001848 /* A default MAC operation should be abortable without error. */
1849 PSA_ASSERT( psa_mac_abort( &func ) );
1850 PSA_ASSERT( psa_mac_abort( &init ) );
1851 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001852}
1853/* END_CASE */
1854
1855/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001856void mac_setup( int key_type_arg,
1857 data_t *key,
1858 int alg_arg,
1859 int expected_status_arg )
1860{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001861 psa_key_type_t key_type = key_type_arg;
1862 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001863 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001864 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001865 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1866#if defined(KNOWN_SUPPORTED_MAC_ALG)
1867 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1868#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001869
Gilles Peskine8817f612018-12-18 00:18:46 +01001870 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001871
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001872 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1873 &operation, &status ) )
1874 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001875 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001876
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001877 /* The operation object should be reusable. */
1878#if defined(KNOWN_SUPPORTED_MAC_ALG)
1879 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1880 smoke_test_key_data,
1881 sizeof( smoke_test_key_data ),
1882 KNOWN_SUPPORTED_MAC_ALG,
1883 &operation, &status ) )
1884 goto exit;
1885 TEST_EQUAL( status, PSA_SUCCESS );
1886#endif
1887
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001888exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001889 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001890}
1891/* END_CASE */
1892
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001893/* 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 +00001894void mac_bad_order( )
1895{
Ronald Cron5425a212020-08-04 14:58:35 +02001896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001897 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1898 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001899 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001900 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1901 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1902 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001904 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1905 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1906 size_t sign_mac_length = 0;
1907 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1908 const uint8_t verify_mac[] = {
1909 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1910 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1911 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1912
1913 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001914 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001915 psa_set_key_algorithm( &attributes, alg );
1916 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001917
Ronald Cron5425a212020-08-04 14:58:35 +02001918 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1919 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001920
Jaeden Amero252ef282019-02-15 14:05:35 +00001921 /* Call update without calling setup beforehand. */
1922 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1923 PSA_ERROR_BAD_STATE );
1924 PSA_ASSERT( psa_mac_abort( &operation ) );
1925
1926 /* Call sign finish without calling setup beforehand. */
1927 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1928 &sign_mac_length),
1929 PSA_ERROR_BAD_STATE );
1930 PSA_ASSERT( psa_mac_abort( &operation ) );
1931
1932 /* Call verify finish without calling setup beforehand. */
1933 TEST_EQUAL( psa_mac_verify_finish( &operation,
1934 verify_mac, sizeof( verify_mac ) ),
1935 PSA_ERROR_BAD_STATE );
1936 PSA_ASSERT( psa_mac_abort( &operation ) );
1937
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001938 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001939 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1940 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001941 PSA_ERROR_BAD_STATE );
1942 PSA_ASSERT( psa_mac_abort( &operation ) );
1943
Jaeden Amero252ef282019-02-15 14:05:35 +00001944 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001945 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001946 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1947 PSA_ASSERT( psa_mac_sign_finish( &operation,
1948 sign_mac, sizeof( sign_mac ),
1949 &sign_mac_length ) );
1950 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1951 PSA_ERROR_BAD_STATE );
1952 PSA_ASSERT( psa_mac_abort( &operation ) );
1953
1954 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001955 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001956 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1957 PSA_ASSERT( psa_mac_verify_finish( &operation,
1958 verify_mac, sizeof( verify_mac ) ) );
1959 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1960 PSA_ERROR_BAD_STATE );
1961 PSA_ASSERT( psa_mac_abort( &operation ) );
1962
1963 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001964 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001965 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1966 PSA_ASSERT( psa_mac_sign_finish( &operation,
1967 sign_mac, sizeof( sign_mac ),
1968 &sign_mac_length ) );
1969 TEST_EQUAL( psa_mac_sign_finish( &operation,
1970 sign_mac, sizeof( sign_mac ),
1971 &sign_mac_length ),
1972 PSA_ERROR_BAD_STATE );
1973 PSA_ASSERT( psa_mac_abort( &operation ) );
1974
1975 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001976 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001977 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1978 PSA_ASSERT( psa_mac_verify_finish( &operation,
1979 verify_mac, sizeof( verify_mac ) ) );
1980 TEST_EQUAL( psa_mac_verify_finish( &operation,
1981 verify_mac, sizeof( verify_mac ) ),
1982 PSA_ERROR_BAD_STATE );
1983 PSA_ASSERT( psa_mac_abort( &operation ) );
1984
1985 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001986 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001987 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01001988 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001989 TEST_EQUAL( psa_mac_verify_finish( &operation,
1990 verify_mac, sizeof( verify_mac ) ),
1991 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01001992 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001993 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01001994 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001995
1996 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001998 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01001999 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002000 TEST_EQUAL( psa_mac_sign_finish( &operation,
2001 sign_mac, sizeof( sign_mac ),
2002 &sign_mac_length ),
2003 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002004 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002005 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002006 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002007
Ronald Cron5425a212020-08-04 14:58:35 +02002008 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002009
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002010exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002011 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002012}
2013/* END_CASE */
2014
2015/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002016void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002017 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002018 int alg_arg,
2019 data_t *input,
2020 data_t *expected_mac )
2021{
Ronald Cron5425a212020-08-04 14:58:35 +02002022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002023 psa_key_type_t key_type = key_type_arg;
2024 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002025 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002027 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002028 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002029 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002030 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002031 const size_t output_sizes_to_test[] = {
2032 0,
2033 1,
2034 expected_mac->len - 1,
2035 expected_mac->len,
2036 expected_mac->len + 1,
2037 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002038
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002040 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002041 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002042
Gilles Peskine8817f612018-12-18 00:18:46 +01002043 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002044
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002045 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002046 psa_set_key_algorithm( &attributes, alg );
2047 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002048
Ronald Cron5425a212020-08-04 14:58:35 +02002049 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2050 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002051
Gilles Peskine8b356b52020-08-25 23:44:59 +02002052 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2053 {
2054 const size_t output_size = output_sizes_to_test[i];
2055 psa_status_t expected_status =
2056 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2057 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002058
Chris Jones9634bb12021-01-20 15:56:42 +00002059 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002060 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002061
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002062 /* Calculate the MAC, one-shot case. */
2063 TEST_EQUAL( psa_mac_compute( key, alg,
2064 input->x, input->len,
2065 actual_mac, output_size, &mac_length ),
2066 expected_status );
2067 if( expected_status == PSA_SUCCESS )
2068 {
2069 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2070 actual_mac, mac_length );
2071 }
2072
2073 if( output_size > 0 )
2074 memset( actual_mac, 0, output_size );
2075
2076 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002077 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002078 PSA_ASSERT( psa_mac_update( &operation,
2079 input->x, input->len ) );
2080 TEST_EQUAL( psa_mac_sign_finish( &operation,
2081 actual_mac, output_size,
2082 &mac_length ),
2083 expected_status );
2084 PSA_ASSERT( psa_mac_abort( &operation ) );
2085
2086 if( expected_status == PSA_SUCCESS )
2087 {
2088 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2089 actual_mac, mac_length );
2090 }
2091 mbedtls_free( actual_mac );
2092 actual_mac = NULL;
2093 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002094
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002095exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002096 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002097 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002098 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002099 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002100}
2101/* END_CASE */
2102
2103/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002104void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002105 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002106 int alg_arg,
2107 data_t *input,
2108 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002109{
Ronald Cron5425a212020-08-04 14:58:35 +02002110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002111 psa_key_type_t key_type = key_type_arg;
2112 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002113 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002115 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002116
Gilles Peskine69c12672018-06-28 00:07:19 +02002117 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2118
Gilles Peskine8817f612018-12-18 00:18:46 +01002119 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002120
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002121 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002122 psa_set_key_algorithm( &attributes, alg );
2123 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002124
Ronald Cron5425a212020-08-04 14:58:35 +02002125 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2126 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002127
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002128 /* Verify correct MAC, one-shot case. */
2129 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2130 expected_mac->x, expected_mac->len ) );
2131
2132 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002133 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002134 PSA_ASSERT( psa_mac_update( &operation,
2135 input->x, input->len ) );
2136 PSA_ASSERT( psa_mac_verify_finish( &operation,
2137 expected_mac->x,
2138 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002139
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002140 /* Test a MAC that's too short, one-shot case. */
2141 TEST_EQUAL( psa_mac_verify( key, alg,
2142 input->x, input->len,
2143 expected_mac->x,
2144 expected_mac->len - 1 ),
2145 PSA_ERROR_INVALID_SIGNATURE );
2146
2147 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002148 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002149 PSA_ASSERT( psa_mac_update( &operation,
2150 input->x, input->len ) );
2151 TEST_EQUAL( psa_mac_verify_finish( &operation,
2152 expected_mac->x,
2153 expected_mac->len - 1 ),
2154 PSA_ERROR_INVALID_SIGNATURE );
2155
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002156 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002157 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2158 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002159 TEST_EQUAL( psa_mac_verify( key, alg,
2160 input->x, input->len,
2161 perturbed_mac, expected_mac->len + 1 ),
2162 PSA_ERROR_INVALID_SIGNATURE );
2163
2164 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002165 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002166 PSA_ASSERT( psa_mac_update( &operation,
2167 input->x, input->len ) );
2168 TEST_EQUAL( psa_mac_verify_finish( &operation,
2169 perturbed_mac,
2170 expected_mac->len + 1 ),
2171 PSA_ERROR_INVALID_SIGNATURE );
2172
2173 /* Test changing one byte. */
2174 for( size_t i = 0; i < expected_mac->len; i++ )
2175 {
Chris Jones9634bb12021-01-20 15:56:42 +00002176 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002177 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002178
2179 TEST_EQUAL( psa_mac_verify( key, alg,
2180 input->x, input->len,
2181 perturbed_mac, expected_mac->len ),
2182 PSA_ERROR_INVALID_SIGNATURE );
2183
Ronald Cron5425a212020-08-04 14:58:35 +02002184 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002185 PSA_ASSERT( psa_mac_update( &operation,
2186 input->x, input->len ) );
2187 TEST_EQUAL( psa_mac_verify_finish( &operation,
2188 perturbed_mac,
2189 expected_mac->len ),
2190 PSA_ERROR_INVALID_SIGNATURE );
2191 perturbed_mac[i] ^= 1;
2192 }
2193
Gilles Peskine8c9def32018-02-08 10:02:12 +01002194exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002195 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002196 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002197 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002198 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002199}
2200/* END_CASE */
2201
2202/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002203void cipher_operation_init( )
2204{
Jaeden Ameroab439972019-02-15 14:12:05 +00002205 const uint8_t input[1] = { 0 };
2206 unsigned char output[1] = { 0 };
2207 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002208 /* Test each valid way of initializing the object, except for `= {0}`, as
2209 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2210 * though it's OK by the C standard. We could test for this, but we'd need
2211 * to supress the Clang warning for the test. */
2212 psa_cipher_operation_t func = psa_cipher_operation_init( );
2213 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2214 psa_cipher_operation_t zero;
2215
2216 memset( &zero, 0, sizeof( zero ) );
2217
Jaeden Ameroab439972019-02-15 14:12:05 +00002218 /* A freshly-initialized cipher operation should not be usable. */
2219 TEST_EQUAL( psa_cipher_update( &func,
2220 input, sizeof( input ),
2221 output, sizeof( output ),
2222 &output_length ),
2223 PSA_ERROR_BAD_STATE );
2224 TEST_EQUAL( psa_cipher_update( &init,
2225 input, sizeof( input ),
2226 output, sizeof( output ),
2227 &output_length ),
2228 PSA_ERROR_BAD_STATE );
2229 TEST_EQUAL( psa_cipher_update( &zero,
2230 input, sizeof( input ),
2231 output, sizeof( output ),
2232 &output_length ),
2233 PSA_ERROR_BAD_STATE );
2234
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002235 /* A default cipher operation should be abortable without error. */
2236 PSA_ASSERT( psa_cipher_abort( &func ) );
2237 PSA_ASSERT( psa_cipher_abort( &init ) );
2238 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002239}
2240/* END_CASE */
2241
2242/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002243void cipher_setup( int key_type_arg,
2244 data_t *key,
2245 int alg_arg,
2246 int expected_status_arg )
2247{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248 psa_key_type_t key_type = key_type_arg;
2249 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002250 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002251 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002252 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002253#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002254 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2255#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002256
Gilles Peskine8817f612018-12-18 00:18:46 +01002257 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002258
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002259 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2260 &operation, &status ) )
2261 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002262 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002263
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002264 /* The operation object should be reusable. */
2265#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2266 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2267 smoke_test_key_data,
2268 sizeof( smoke_test_key_data ),
2269 KNOWN_SUPPORTED_CIPHER_ALG,
2270 &operation, &status ) )
2271 goto exit;
2272 TEST_EQUAL( status, PSA_SUCCESS );
2273#endif
2274
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002275exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002276 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002277 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002278}
2279/* END_CASE */
2280
Ronald Cronee414c72021-03-18 18:50:08 +01002281/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002282void cipher_bad_order( )
2283{
Ronald Cron5425a212020-08-04 14:58:35 +02002284 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002285 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2286 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002287 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002288 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002289 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002290 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002291 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2292 0xaa, 0xaa, 0xaa, 0xaa };
2293 const uint8_t text[] = {
2294 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2295 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002296 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002297 size_t length = 0;
2298
2299 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002300 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2301 psa_set_key_algorithm( &attributes, alg );
2302 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002303 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2304 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002305
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002306 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002307 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2308 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002309 PSA_ERROR_BAD_STATE );
2310 PSA_ASSERT( psa_cipher_abort( &operation ) );
2311
2312 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002313 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2314 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002315 PSA_ERROR_BAD_STATE );
2316 PSA_ASSERT( psa_cipher_abort( &operation ) );
2317
Jaeden Ameroab439972019-02-15 14:12:05 +00002318 /* Generate an IV without calling setup beforehand. */
2319 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2320 buffer, sizeof( buffer ),
2321 &length ),
2322 PSA_ERROR_BAD_STATE );
2323 PSA_ASSERT( psa_cipher_abort( &operation ) );
2324
2325 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002326 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002327 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2328 buffer, sizeof( buffer ),
2329 &length ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002330 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002331 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2332 buffer, sizeof( buffer ),
2333 &length ),
2334 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002335 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002336 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002337 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002338
2339 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002340 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002341 PSA_ASSERT( psa_cipher_set_iv( &operation,
2342 iv, sizeof( iv ) ) );
2343 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2344 buffer, sizeof( buffer ),
2345 &length ),
2346 PSA_ERROR_BAD_STATE );
2347 PSA_ASSERT( psa_cipher_abort( &operation ) );
2348
2349 /* Set an IV without calling setup beforehand. */
2350 TEST_EQUAL( psa_cipher_set_iv( &operation,
2351 iv, sizeof( iv ) ),
2352 PSA_ERROR_BAD_STATE );
2353 PSA_ASSERT( psa_cipher_abort( &operation ) );
2354
2355 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002356 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002357 PSA_ASSERT( psa_cipher_set_iv( &operation,
2358 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002359 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002360 TEST_EQUAL( psa_cipher_set_iv( &operation,
2361 iv, sizeof( iv ) ),
2362 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002363 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002364 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002365 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002366
2367 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002368 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002369 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2370 buffer, sizeof( buffer ),
2371 &length ) );
2372 TEST_EQUAL( psa_cipher_set_iv( &operation,
2373 iv, sizeof( iv ) ),
2374 PSA_ERROR_BAD_STATE );
2375 PSA_ASSERT( psa_cipher_abort( &operation ) );
2376
2377 /* Call update without calling setup beforehand. */
2378 TEST_EQUAL( psa_cipher_update( &operation,
2379 text, sizeof( text ),
2380 buffer, sizeof( buffer ),
2381 &length ),
2382 PSA_ERROR_BAD_STATE );
2383 PSA_ASSERT( psa_cipher_abort( &operation ) );
2384
2385 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002386 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002387 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002388 TEST_EQUAL( psa_cipher_update( &operation,
2389 text, sizeof( text ),
2390 buffer, sizeof( buffer ),
2391 &length ),
2392 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002393 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002394 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002395 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002396
2397 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002398 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002399 PSA_ASSERT( psa_cipher_set_iv( &operation,
2400 iv, sizeof( iv ) ) );
2401 PSA_ASSERT( psa_cipher_finish( &operation,
2402 buffer, sizeof( buffer ), &length ) );
2403 TEST_EQUAL( psa_cipher_update( &operation,
2404 text, sizeof( text ),
2405 buffer, sizeof( buffer ),
2406 &length ),
2407 PSA_ERROR_BAD_STATE );
2408 PSA_ASSERT( psa_cipher_abort( &operation ) );
2409
2410 /* Call finish without calling setup beforehand. */
2411 TEST_EQUAL( psa_cipher_finish( &operation,
2412 buffer, sizeof( buffer ), &length ),
2413 PSA_ERROR_BAD_STATE );
2414 PSA_ASSERT( psa_cipher_abort( &operation ) );
2415
2416 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002417 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002418 /* Not calling update means we are encrypting an empty buffer, which is OK
2419 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002420 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002421 TEST_EQUAL( psa_cipher_finish( &operation,
2422 buffer, sizeof( buffer ), &length ),
2423 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002424 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002425 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002426 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002427
2428 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002429 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002430 PSA_ASSERT( psa_cipher_set_iv( &operation,
2431 iv, sizeof( iv ) ) );
2432 PSA_ASSERT( psa_cipher_finish( &operation,
2433 buffer, sizeof( buffer ), &length ) );
2434 TEST_EQUAL( psa_cipher_finish( &operation,
2435 buffer, sizeof( buffer ), &length ),
2436 PSA_ERROR_BAD_STATE );
2437 PSA_ASSERT( psa_cipher_abort( &operation ) );
2438
Ronald Cron5425a212020-08-04 14:58:35 +02002439 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002440
Jaeden Ameroab439972019-02-15 14:12:05 +00002441exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002442 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002443 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444}
2445/* END_CASE */
2446
2447/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002448void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002449 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002450 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002451 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002452{
Ronald Cron5425a212020-08-04 14:58:35 +02002453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002454 psa_status_t status;
2455 psa_key_type_t key_type = key_type_arg;
2456 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002457 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002458 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002459 size_t output_buffer_size = 0;
2460 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002461 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002462 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002464
Gilles Peskine8817f612018-12-18 00:18:46 +01002465 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002466
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2468 psa_set_key_algorithm( &attributes, alg );
2469 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002470
Ronald Cron5425a212020-08-04 14:58:35 +02002471 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2472 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002473
Ronald Cron5425a212020-08-04 14:58:35 +02002474 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002475
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002476 if( iv->len > 0 )
2477 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002478 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002479 }
2480
gabor-mezei-armceface22021-01-21 12:26:17 +01002481 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2482 TEST_ASSERT( output_buffer_size <=
2483 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002484 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002485
Gilles Peskine8817f612018-12-18 00:18:46 +01002486 PSA_ASSERT( psa_cipher_update( &operation,
2487 input->x, input->len,
2488 output, output_buffer_size,
2489 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002490 TEST_ASSERT( function_output_length <=
2491 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2492 TEST_ASSERT( function_output_length <=
2493 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002494 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002495
Gilles Peskine50e586b2018-06-08 14:28:46 +02002496 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002497 ( output_buffer_size == 0 ? NULL :
2498 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002499 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002500 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002501 TEST_ASSERT( function_output_length <=
2502 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2503 TEST_ASSERT( function_output_length <=
2504 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002505 total_output_length += function_output_length;
2506
Gilles Peskinefe11b722018-12-18 00:24:04 +01002507 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002508 if( expected_status == PSA_SUCCESS )
2509 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002510 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002511 ASSERT_COMPARE( expected_output->x, expected_output->len,
2512 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002513 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002514
Gilles Peskine50e586b2018-06-08 14:28:46 +02002515exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002516 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002517 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002518 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002519 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002520}
2521/* END_CASE */
2522
2523/* BEGIN_CASE */
2524void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002525 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002526 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002527 int first_part_size_arg,
2528 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002529 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002530{
Ronald Cron5425a212020-08-04 14:58:35 +02002531 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002532 psa_key_type_t key_type = key_type_arg;
2533 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002534 size_t first_part_size = first_part_size_arg;
2535 size_t output1_length = output1_length_arg;
2536 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002537 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002538 size_t output_buffer_size = 0;
2539 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002540 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002541 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002542 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002543
Gilles Peskine8817f612018-12-18 00:18:46 +01002544 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002545
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002546 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2547 psa_set_key_algorithm( &attributes, alg );
2548 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002549
Ronald Cron5425a212020-08-04 14:58:35 +02002550 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2551 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002552
Ronald Cron5425a212020-08-04 14:58:35 +02002553 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002554
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002555 if( iv->len > 0 )
2556 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002557 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002558 }
2559
gabor-mezei-armceface22021-01-21 12:26:17 +01002560 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2561 TEST_ASSERT( output_buffer_size <=
2562 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002563 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002564
Gilles Peskinee0866522019-02-19 19:44:00 +01002565 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2567 output, output_buffer_size,
2568 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002569 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002570 TEST_ASSERT( function_output_length <=
2571 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2572 TEST_ASSERT( function_output_length <=
2573 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002574 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002575
Gilles Peskine8817f612018-12-18 00:18:46 +01002576 PSA_ASSERT( psa_cipher_update( &operation,
2577 input->x + first_part_size,
2578 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002579 ( output_buffer_size == 0 ? NULL :
2580 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002581 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002582 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002583 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002584 TEST_ASSERT( function_output_length <=
2585 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2586 alg,
2587 input->len - first_part_size ) );
2588 TEST_ASSERT( function_output_length <=
2589 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002590 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002591
Gilles Peskine8817f612018-12-18 00:18:46 +01002592 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002593 ( output_buffer_size == 0 ? NULL :
2594 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002595 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002596 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002597 TEST_ASSERT( function_output_length <=
2598 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2599 TEST_ASSERT( function_output_length <=
2600 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002601 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002602 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002603
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002604 ASSERT_COMPARE( expected_output->x, expected_output->len,
2605 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002606
2607exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002608 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002609 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002610 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002611 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002612}
2613/* END_CASE */
2614
2615/* BEGIN_CASE */
2616void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002617 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002618 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002619 int first_part_size_arg,
2620 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002621 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002622{
Ronald Cron5425a212020-08-04 14:58:35 +02002623 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002624 psa_key_type_t key_type = key_type_arg;
2625 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002626 size_t first_part_size = first_part_size_arg;
2627 size_t output1_length = output1_length_arg;
2628 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002629 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002630 size_t output_buffer_size = 0;
2631 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002632 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002633 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635
Gilles Peskine8817f612018-12-18 00:18:46 +01002636 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002637
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002638 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2639 psa_set_key_algorithm( &attributes, alg );
2640 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002641
Ronald Cron5425a212020-08-04 14:58:35 +02002642 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2643 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644
Ronald Cron5425a212020-08-04 14:58:35 +02002645 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002646
Steven Cooreman177deba2020-09-07 17:14:14 +02002647 if( iv->len > 0 )
2648 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002649 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002650 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651
gabor-mezei-armceface22021-01-21 12:26:17 +01002652 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2653 TEST_ASSERT( output_buffer_size <=
2654 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002655 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002656
Gilles Peskinee0866522019-02-19 19:44:00 +01002657 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002658 PSA_ASSERT( psa_cipher_update( &operation,
2659 input->x, first_part_size,
2660 output, output_buffer_size,
2661 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002662 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002663 TEST_ASSERT( function_output_length <=
2664 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2665 TEST_ASSERT( function_output_length <=
2666 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002667 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002668
Gilles Peskine8817f612018-12-18 00:18:46 +01002669 PSA_ASSERT( psa_cipher_update( &operation,
2670 input->x + first_part_size,
2671 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002672 ( output_buffer_size == 0 ? NULL :
2673 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002674 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002675 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002676 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002677 TEST_ASSERT( function_output_length <=
2678 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2679 alg,
2680 input->len - first_part_size ) );
2681 TEST_ASSERT( function_output_length <=
2682 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002683 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002684
Gilles Peskine8817f612018-12-18 00:18:46 +01002685 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002686 ( output_buffer_size == 0 ? NULL :
2687 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002688 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002690 TEST_ASSERT( function_output_length <=
2691 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2692 TEST_ASSERT( function_output_length <=
2693 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002694 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002695 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002697 ASSERT_COMPARE( expected_output->x, expected_output->len,
2698 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002699
2700exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002701 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002702 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002703 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002704 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705}
2706/* END_CASE */
2707
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708/* BEGIN_CASE */
2709void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002710 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002711 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002712 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713{
Ronald Cron5425a212020-08-04 14:58:35 +02002714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715 psa_status_t status;
2716 psa_key_type_t key_type = key_type_arg;
2717 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002718 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002719 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720 size_t output_buffer_size = 0;
2721 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002722 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002723 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002725
Gilles Peskine8817f612018-12-18 00:18:46 +01002726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002727
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2729 psa_set_key_algorithm( &attributes, alg );
2730 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002731
Ronald Cron5425a212020-08-04 14:58:35 +02002732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2733 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002734
Ronald Cron5425a212020-08-04 14:58:35 +02002735 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736
Steven Cooreman177deba2020-09-07 17:14:14 +02002737 if( iv->len > 0 )
2738 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002739 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002740 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002741
gabor-mezei-armceface22021-01-21 12:26:17 +01002742 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2743 TEST_ASSERT( output_buffer_size <=
2744 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002745 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002746
Gilles Peskine8817f612018-12-18 00:18:46 +01002747 PSA_ASSERT( psa_cipher_update( &operation,
2748 input->x, input->len,
2749 output, output_buffer_size,
2750 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002751 TEST_ASSERT( function_output_length <=
2752 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2753 TEST_ASSERT( function_output_length <=
2754 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002755 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002756
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002758 ( output_buffer_size == 0 ? NULL :
2759 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002760 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002761 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002762 TEST_ASSERT( function_output_length <=
2763 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2764 TEST_ASSERT( function_output_length <=
2765 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002766 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002767 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002768
2769 if( expected_status == PSA_SUCCESS )
2770 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002771 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002772 ASSERT_COMPARE( expected_output->x, expected_output->len,
2773 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002774 }
2775
Gilles Peskine50e586b2018-06-08 14:28:46 +02002776exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002777 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002778 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002779 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002780 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002781}
2782/* END_CASE */
2783
Gilles Peskine50e586b2018-06-08 14:28:46 +02002784/* BEGIN_CASE */
2785void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002786 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002787 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002788{
Ronald Cron5425a212020-08-04 14:58:35 +02002789 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002790 psa_key_type_t key_type = key_type_arg;
2791 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002792 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002793 size_t iv_size = 16;
2794 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002795 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002796 size_t output1_size = 0;
2797 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002798 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002799 size_t output2_size = 0;
2800 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002801 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002802 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2803 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002805
Gilles Peskine8817f612018-12-18 00:18:46 +01002806 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002807
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002808 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2809 psa_set_key_algorithm( &attributes, alg );
2810 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002811
Ronald Cron5425a212020-08-04 14:58:35 +02002812 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2813 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002814
Ronald Cron5425a212020-08-04 14:58:35 +02002815 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2816 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002817
Steven Cooreman177deba2020-09-07 17:14:14 +02002818 if( alg != PSA_ALG_ECB_NO_PADDING )
2819 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002820 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2821 iv, iv_size,
2822 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002823 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002824 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2825 TEST_ASSERT( output1_size <=
2826 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002827 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002828
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2830 output1, output1_size,
2831 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002832 TEST_ASSERT( output1_length <=
2833 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2834 TEST_ASSERT( output1_length <=
2835 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2836
Gilles Peskine8817f612018-12-18 00:18:46 +01002837 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002838 output1 + output1_length,
2839 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002840 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002841 TEST_ASSERT( function_output_length <=
2842 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2843 TEST_ASSERT( function_output_length <=
2844 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002845
Gilles Peskine048b7f02018-06-08 14:20:49 +02002846 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002847
Gilles Peskine8817f612018-12-18 00:18:46 +01002848 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002849
2850 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002851 TEST_ASSERT( output2_size <=
2852 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2853 TEST_ASSERT( output2_size <=
2854 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002855 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002856
Steven Cooreman177deba2020-09-07 17:14:14 +02002857 if( iv_length > 0 )
2858 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002859 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2860 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002861 }
2862
Gilles Peskine8817f612018-12-18 00:18:46 +01002863 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2864 output2, output2_size,
2865 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002866 TEST_ASSERT( output2_length <=
2867 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2868 TEST_ASSERT( output2_length <=
2869 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2870
Gilles Peskine048b7f02018-06-08 14:20:49 +02002871 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002872 PSA_ASSERT( psa_cipher_finish( &operation2,
2873 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002874 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002876 TEST_ASSERT( function_output_length <=
2877 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2878 TEST_ASSERT( function_output_length <=
2879 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002880
Gilles Peskine048b7f02018-06-08 14:20:49 +02002881 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002882
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002884
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002885 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002886
2887exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002888 psa_cipher_abort( &operation1 );
2889 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002890 mbedtls_free( output1 );
2891 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002892 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002893 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002894}
2895/* END_CASE */
2896
2897/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898void cipher_verify_output_multipart( int alg_arg,
2899 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002900 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002901 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002902 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002903{
Ronald Cron5425a212020-08-04 14:58:35 +02002904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002905 psa_key_type_t key_type = key_type_arg;
2906 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002907 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002908 unsigned char iv[16] = {0};
2909 size_t iv_size = 16;
2910 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002911 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002912 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002913 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002914 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002915 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002916 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002917 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002918 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2919 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002920 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002921
Gilles Peskine8817f612018-12-18 00:18:46 +01002922 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002923
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002924 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2925 psa_set_key_algorithm( &attributes, alg );
2926 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002927
Ronald Cron5425a212020-08-04 14:58:35 +02002928 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2929 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002930
Ronald Cron5425a212020-08-04 14:58:35 +02002931 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2932 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002933
Steven Cooreman177deba2020-09-07 17:14:14 +02002934 if( alg != PSA_ALG_ECB_NO_PADDING )
2935 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002936 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2937 iv, iv_size,
2938 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002939 }
2940
gabor-mezei-armceface22021-01-21 12:26:17 +01002941 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2942 TEST_ASSERT( output1_buffer_size <=
2943 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002944 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002945
Gilles Peskinee0866522019-02-19 19:44:00 +01002946 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002947
Gilles Peskine8817f612018-12-18 00:18:46 +01002948 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2949 output1, output1_buffer_size,
2950 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002951 TEST_ASSERT( function_output_length <=
2952 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2953 TEST_ASSERT( function_output_length <=
2954 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002955 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002956
Gilles Peskine8817f612018-12-18 00:18:46 +01002957 PSA_ASSERT( psa_cipher_update( &operation1,
2958 input->x + first_part_size,
2959 input->len - first_part_size,
2960 output1, output1_buffer_size,
2961 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002962 TEST_ASSERT( function_output_length <=
2963 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2964 alg,
2965 input->len - first_part_size ) );
2966 TEST_ASSERT( function_output_length <=
2967 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002968 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002969
Gilles Peskine8817f612018-12-18 00:18:46 +01002970 PSA_ASSERT( psa_cipher_finish( &operation1,
2971 output1 + output1_length,
2972 output1_buffer_size - output1_length,
2973 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002974 TEST_ASSERT( function_output_length <=
2975 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2976 TEST_ASSERT( function_output_length <=
2977 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002978 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002979
Gilles Peskine8817f612018-12-18 00:18:46 +01002980 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002981
Gilles Peskine048b7f02018-06-08 14:20:49 +02002982 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002983 TEST_ASSERT( output2_buffer_size <=
2984 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2985 TEST_ASSERT( output2_buffer_size <=
2986 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002987 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002988
Steven Cooreman177deba2020-09-07 17:14:14 +02002989 if( iv_length > 0 )
2990 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002991 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2992 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002993 }
Moran Pekerded84402018-06-06 16:36:50 +03002994
Gilles Peskine8817f612018-12-18 00:18:46 +01002995 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2996 output2, output2_buffer_size,
2997 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002998 TEST_ASSERT( function_output_length <=
2999 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3000 TEST_ASSERT( function_output_length <=
3001 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003002 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003003
Gilles Peskine8817f612018-12-18 00:18:46 +01003004 PSA_ASSERT( psa_cipher_update( &operation2,
3005 output1 + first_part_size,
3006 output1_length - first_part_size,
3007 output2, output2_buffer_size,
3008 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003009 TEST_ASSERT( function_output_length <=
3010 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3011 alg,
3012 output1_length - first_part_size ) );
3013 TEST_ASSERT( function_output_length <=
3014 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003015 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003016
Gilles Peskine8817f612018-12-18 00:18:46 +01003017 PSA_ASSERT( psa_cipher_finish( &operation2,
3018 output2 + output2_length,
3019 output2_buffer_size - output2_length,
3020 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003021 TEST_ASSERT( function_output_length <=
3022 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3023 TEST_ASSERT( function_output_length <=
3024 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003025 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003026
Gilles Peskine8817f612018-12-18 00:18:46 +01003027 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003028
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003029 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003030
3031exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003032 psa_cipher_abort( &operation1 );
3033 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003034 mbedtls_free( output1 );
3035 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003036 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003037 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003038}
3039/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003040
Gilles Peskine20035e32018-02-03 22:44:14 +01003041/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003042void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003043 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003044 data_t *nonce,
3045 data_t *additional_data,
3046 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003047 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003048{
Ronald Cron5425a212020-08-04 14:58:35 +02003049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003050 psa_key_type_t key_type = key_type_arg;
3051 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003052 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003053 unsigned char *output_data = NULL;
3054 size_t output_size = 0;
3055 size_t output_length = 0;
3056 unsigned char *output_data2 = NULL;
3057 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003058 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003059 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003061
Gilles Peskine8817f612018-12-18 00:18:46 +01003062 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003063
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003064 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3065 psa_set_key_algorithm( &attributes, alg );
3066 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003067
Gilles Peskine049c7532019-05-15 20:22:09 +02003068 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003069 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003070 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3071 key_bits = psa_get_key_bits( &attributes );
3072
3073 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3074 alg );
3075 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3076 * should be exact. */
3077 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3078 expected_result != PSA_ERROR_NOT_SUPPORTED )
3079 {
3080 TEST_EQUAL( output_size,
3081 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3082 TEST_ASSERT( output_size <=
3083 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3084 }
3085 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003086
Steven Cooremanf49478b2021-02-15 15:19:25 +01003087 status = psa_aead_encrypt( key, alg,
3088 nonce->x, nonce->len,
3089 additional_data->x,
3090 additional_data->len,
3091 input_data->x, input_data->len,
3092 output_data, output_size,
3093 &output_length );
3094
3095 /* If the operation is not supported, just skip and not fail in case the
3096 * encryption involves a common limitation of cryptography hardwares and
3097 * an alternative implementation. */
3098 if( status == PSA_ERROR_NOT_SUPPORTED )
3099 {
3100 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3101 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3102 }
3103
3104 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003105
3106 if( PSA_SUCCESS == expected_result )
3107 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003108 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003109
Gilles Peskine003a4a92019-05-14 16:09:40 +02003110 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3111 * should be exact. */
3112 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003113 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003114
gabor-mezei-armceface22021-01-21 12:26:17 +01003115 TEST_ASSERT( input_data->len <=
3116 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3117
Ronald Cron5425a212020-08-04 14:58:35 +02003118 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003119 nonce->x, nonce->len,
3120 additional_data->x,
3121 additional_data->len,
3122 output_data, output_length,
3123 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003124 &output_length2 ),
3125 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003126
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003127 ASSERT_COMPARE( input_data->x, input_data->len,
3128 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003129 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003130
Gilles Peskinea1cac842018-06-11 19:33:02 +02003131exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003132 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003133 mbedtls_free( output_data );
3134 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003135 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003136}
3137/* END_CASE */
3138
3139/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003140void aead_encrypt( int key_type_arg, data_t *key_data,
3141 int alg_arg,
3142 data_t *nonce,
3143 data_t *additional_data,
3144 data_t *input_data,
3145 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003146{
Ronald Cron5425a212020-08-04 14:58:35 +02003147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003148 psa_key_type_t key_type = key_type_arg;
3149 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003150 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003151 unsigned char *output_data = NULL;
3152 size_t output_size = 0;
3153 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003155 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003156
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003158
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003159 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3160 psa_set_key_algorithm( &attributes, alg );
3161 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003162
Gilles Peskine049c7532019-05-15 20:22:09 +02003163 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003164 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003165 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3166 key_bits = psa_get_key_bits( &attributes );
3167
3168 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3169 alg );
3170 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3171 * should be exact. */
3172 TEST_EQUAL( output_size,
3173 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3174 TEST_ASSERT( output_size <=
3175 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3176 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003177
Steven Cooremand588ea12021-01-11 19:36:04 +01003178 status = psa_aead_encrypt( key, alg,
3179 nonce->x, nonce->len,
3180 additional_data->x, additional_data->len,
3181 input_data->x, input_data->len,
3182 output_data, output_size,
3183 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003184
Ronald Cron28a45ed2021-02-09 20:35:42 +01003185 /* If the operation is not supported, just skip and not fail in case the
3186 * encryption involves a common limitation of cryptography hardwares and
3187 * an alternative implementation. */
3188 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003189 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003190 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3191 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003192 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003193
3194 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003195 ASSERT_COMPARE( expected_result->x, expected_result->len,
3196 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003197
Gilles Peskinea1cac842018-06-11 19:33:02 +02003198exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003199 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003200 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003201 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003202}
3203/* END_CASE */
3204
3205/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003206void aead_decrypt( int key_type_arg, data_t *key_data,
3207 int alg_arg,
3208 data_t *nonce,
3209 data_t *additional_data,
3210 data_t *input_data,
3211 data_t *expected_data,
3212 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003213{
Ronald Cron5425a212020-08-04 14:58:35 +02003214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003215 psa_key_type_t key_type = key_type_arg;
3216 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003217 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003218 unsigned char *output_data = NULL;
3219 size_t output_size = 0;
3220 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003222 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003223 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003224
Gilles Peskine8817f612018-12-18 00:18:46 +01003225 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003226
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003227 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3228 psa_set_key_algorithm( &attributes, alg );
3229 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003230
Gilles Peskine049c7532019-05-15 20:22:09 +02003231 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003232 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003233 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3234 key_bits = psa_get_key_bits( &attributes );
3235
3236 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3237 alg );
3238 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3239 expected_result != PSA_ERROR_NOT_SUPPORTED )
3240 {
3241 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3242 * should be exact. */
3243 TEST_EQUAL( output_size,
3244 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3245 TEST_ASSERT( output_size <=
3246 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3247 }
3248 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003249
Steven Cooremand588ea12021-01-11 19:36:04 +01003250 status = psa_aead_decrypt( key, alg,
3251 nonce->x, nonce->len,
3252 additional_data->x,
3253 additional_data->len,
3254 input_data->x, input_data->len,
3255 output_data, output_size,
3256 &output_length );
3257
Ronald Cron28a45ed2021-02-09 20:35:42 +01003258 /* If the operation is not supported, just skip and not fail in case the
3259 * decryption involves a common limitation of cryptography hardwares and
3260 * an alternative implementation. */
3261 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003262 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003263 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3264 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003265 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003266
3267 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003268
Gilles Peskine2d277862018-06-18 15:41:12 +02003269 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003270 ASSERT_COMPARE( expected_data->x, expected_data->len,
3271 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003272
Gilles Peskinea1cac842018-06-11 19:33:02 +02003273exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003274 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003275 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003276 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003277}
3278/* END_CASE */
3279
3280/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003281void signature_size( int type_arg,
3282 int bits,
3283 int alg_arg,
3284 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003285{
3286 psa_key_type_t type = type_arg;
3287 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003288 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003289
Gilles Peskinefe11b722018-12-18 00:24:04 +01003290 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003291#if defined(MBEDTLS_TEST_DEPRECATED)
3292 TEST_EQUAL( actual_size,
3293 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3294#endif /* MBEDTLS_TEST_DEPRECATED */
3295
Gilles Peskinee59236f2018-01-27 23:32:46 +01003296exit:
3297 ;
3298}
3299/* END_CASE */
3300
3301/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003302void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3303 int alg_arg, data_t *input_data,
3304 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003305{
Ronald Cron5425a212020-08-04 14:58:35 +02003306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003307 psa_key_type_t key_type = key_type_arg;
3308 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003309 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003310 unsigned char *signature = NULL;
3311 size_t signature_size;
3312 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003314
Gilles Peskine8817f612018-12-18 00:18:46 +01003315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003316
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003317 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003318 psa_set_key_algorithm( &attributes, alg );
3319 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003320
Gilles Peskine049c7532019-05-15 20:22:09 +02003321 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003322 &key ) );
3323 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003324 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003325
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003326 /* Allocate a buffer which has the size advertized by the
3327 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003328 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003329 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003330 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003331 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003332 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003333
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003334 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003335 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003336 input_data->x, input_data->len,
3337 signature, signature_size,
3338 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003339 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003340 ASSERT_COMPARE( output_data->x, output_data->len,
3341 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003342
Gilles Peskine0627f982019-11-26 19:12:16 +01003343#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003344 memset( signature, 0, signature_size );
3345 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003346 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003347 input_data->x, input_data->len,
3348 signature, signature_size,
3349 &signature_length ) );
3350 ASSERT_COMPARE( output_data->x, output_data->len,
3351 signature, signature_length );
3352#endif /* MBEDTLS_TEST_DEPRECATED */
3353
Gilles Peskine20035e32018-02-03 22:44:14 +01003354exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003355 /*
3356 * Key attributes may have been returned by psa_get_key_attributes()
3357 * thus reset them as required.
3358 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003359 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003360
Ronald Cron5425a212020-08-04 14:58:35 +02003361 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003362 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003363 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003364}
3365/* END_CASE */
3366
3367/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003368void sign_hash_fail( int key_type_arg, data_t *key_data,
3369 int alg_arg, data_t *input_data,
3370 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003371{
Ronald Cron5425a212020-08-04 14:58:35 +02003372 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003373 psa_key_type_t key_type = key_type_arg;
3374 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003375 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003376 psa_status_t actual_status;
3377 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003378 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003379 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003380 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003381
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003382 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003383
Gilles Peskine8817f612018-12-18 00:18:46 +01003384 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003385
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003386 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003387 psa_set_key_algorithm( &attributes, alg );
3388 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003389
Gilles Peskine049c7532019-05-15 20:22:09 +02003390 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003391 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003392
Ronald Cron5425a212020-08-04 14:58:35 +02003393 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003394 input_data->x, input_data->len,
3395 signature, signature_size,
3396 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003397 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003398 /* The value of *signature_length is unspecified on error, but
3399 * whatever it is, it should be less than signature_size, so that
3400 * if the caller tries to read *signature_length bytes without
3401 * checking the error code then they don't overflow a buffer. */
3402 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003403
Gilles Peskine895242b2019-11-29 12:15:40 +01003404#if defined(MBEDTLS_TEST_DEPRECATED)
3405 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003406 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003407 input_data->x, input_data->len,
3408 signature, signature_size,
3409 &signature_length ),
3410 expected_status );
3411 TEST_ASSERT( signature_length <= signature_size );
3412#endif /* MBEDTLS_TEST_DEPRECATED */
3413
Gilles Peskine20035e32018-02-03 22:44:14 +01003414exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003415 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003416 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003417 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003418 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003419}
3420/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003421
3422/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003423void sign_verify_hash( int key_type_arg, data_t *key_data,
3424 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003425{
Ronald Cron5425a212020-08-04 14:58:35 +02003426 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003427 psa_key_type_t key_type = key_type_arg;
3428 psa_algorithm_t alg = alg_arg;
3429 size_t key_bits;
3430 unsigned char *signature = NULL;
3431 size_t signature_size;
3432 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003434
Gilles Peskine8817f612018-12-18 00:18:46 +01003435 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003436
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003437 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003438 psa_set_key_algorithm( &attributes, alg );
3439 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003440
Gilles Peskine049c7532019-05-15 20:22:09 +02003441 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003442 &key ) );
3443 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003444 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003445
3446 /* Allocate a buffer which has the size advertized by the
3447 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003448 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003449 key_bits, alg );
3450 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003451 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003452 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003453
3454 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003455 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003456 input_data->x, input_data->len,
3457 signature, signature_size,
3458 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003459 /* Check that the signature length looks sensible. */
3460 TEST_ASSERT( signature_length <= signature_size );
3461 TEST_ASSERT( signature_length > 0 );
3462
3463 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003464 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003465 input_data->x, input_data->len,
3466 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003467
3468 if( input_data->len != 0 )
3469 {
3470 /* Flip a bit in the input and verify that the signature is now
3471 * detected as invalid. Flip a bit at the beginning, not at the end,
3472 * because ECDSA may ignore the last few bits of the input. */
3473 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003474 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003475 input_data->x, input_data->len,
3476 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003477 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003478 }
3479
3480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003481 /*
3482 * Key attributes may have been returned by psa_get_key_attributes()
3483 * thus reset them as required.
3484 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003485 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003486
Ronald Cron5425a212020-08-04 14:58:35 +02003487 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003488 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003489 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003490}
3491/* END_CASE */
3492
3493/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003494void verify_hash( int key_type_arg, data_t *key_data,
3495 int alg_arg, data_t *hash_data,
3496 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003497{
Ronald Cron5425a212020-08-04 14:58:35 +02003498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003499 psa_key_type_t key_type = key_type_arg;
3500 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003502
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003503 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003504
Gilles Peskine8817f612018-12-18 00:18:46 +01003505 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003506
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003507 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003508 psa_set_key_algorithm( &attributes, alg );
3509 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003510
Gilles Peskine049c7532019-05-15 20:22:09 +02003511 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003512 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003513
Ronald Cron5425a212020-08-04 14:58:35 +02003514 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003515 hash_data->x, hash_data->len,
3516 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003517
3518#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003519 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003520 hash_data->x, hash_data->len,
3521 signature_data->x,
3522 signature_data->len ) );
3523
3524#endif /* MBEDTLS_TEST_DEPRECATED */
3525
itayzafrir5c753392018-05-08 11:18:38 +03003526exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003527 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003528 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003529 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003530}
3531/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532
3533/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003534void verify_hash_fail( int key_type_arg, data_t *key_data,
3535 int alg_arg, data_t *hash_data,
3536 data_t *signature_data,
3537 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003538{
Ronald Cron5425a212020-08-04 14:58:35 +02003539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003540 psa_key_type_t key_type = key_type_arg;
3541 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003542 psa_status_t actual_status;
3543 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003545
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003547
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003548 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003549 psa_set_key_algorithm( &attributes, alg );
3550 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003551
Gilles Peskine049c7532019-05-15 20:22:09 +02003552 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003553 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003554
Ronald Cron5425a212020-08-04 14:58:35 +02003555 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003556 hash_data->x, hash_data->len,
3557 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003558 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003559
Gilles Peskine895242b2019-11-29 12:15:40 +01003560#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003561 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003562 hash_data->x, hash_data->len,
3563 signature_data->x, signature_data->len ),
3564 expected_status );
3565#endif /* MBEDTLS_TEST_DEPRECATED */
3566
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003568 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003569 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003570 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003571}
3572/* END_CASE */
3573
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003574/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003575void sign_message_deterministic( int key_type_arg,
3576 data_t *key_data,
3577 int alg_arg,
3578 data_t *input_data,
3579 data_t *output_data )
3580{
3581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3582 psa_key_type_t key_type = key_type_arg;
3583 psa_algorithm_t alg = alg_arg;
3584 size_t key_bits;
3585 unsigned char *signature = NULL;
3586 size_t signature_size;
3587 size_t signature_length = 0xdeadbeef;
3588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3589
3590 PSA_ASSERT( psa_crypto_init( ) );
3591
3592 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3593 psa_set_key_algorithm( &attributes, alg );
3594 psa_set_key_type( &attributes, key_type );
3595
3596 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3597 &key ) );
3598 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3599 key_bits = psa_get_key_bits( &attributes );
3600
3601 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3602 TEST_ASSERT( signature_size != 0 );
3603 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3604 ASSERT_ALLOC( signature, signature_size );
3605
3606 PSA_ASSERT( psa_sign_message( key, alg,
3607 input_data->x, input_data->len,
3608 signature, signature_size,
3609 &signature_length ) );
3610
3611 ASSERT_COMPARE( output_data->x, output_data->len,
3612 signature, signature_length );
3613
3614exit:
3615 psa_reset_key_attributes( &attributes );
3616
3617 psa_destroy_key( key );
3618 mbedtls_free( signature );
3619 PSA_DONE( );
3620
3621}
3622/* END_CASE */
3623
3624/* BEGIN_CASE */
3625void sign_message_fail( int key_type_arg,
3626 data_t *key_data,
3627 int alg_arg,
3628 data_t *input_data,
3629 int signature_size_arg,
3630 int expected_status_arg )
3631{
3632 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3633 psa_key_type_t key_type = key_type_arg;
3634 psa_algorithm_t alg = alg_arg;
3635 size_t signature_size = signature_size_arg;
3636 psa_status_t actual_status;
3637 psa_status_t expected_status = expected_status_arg;
3638 unsigned char *signature = NULL;
3639 size_t signature_length = 0xdeadbeef;
3640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3641
3642 ASSERT_ALLOC( signature, signature_size );
3643
3644 PSA_ASSERT( psa_crypto_init( ) );
3645
3646 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3647 psa_set_key_algorithm( &attributes, alg );
3648 psa_set_key_type( &attributes, key_type );
3649
3650 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3651 &key ) );
3652
3653 actual_status = psa_sign_message( key, alg,
3654 input_data->x, input_data->len,
3655 signature, signature_size,
3656 &signature_length );
3657 TEST_EQUAL( actual_status, expected_status );
3658 /* The value of *signature_length is unspecified on error, but
3659 * whatever it is, it should be less than signature_size, so that
3660 * if the caller tries to read *signature_length bytes without
3661 * checking the error code then they don't overflow a buffer. */
3662 TEST_ASSERT( signature_length <= signature_size );
3663
3664exit:
3665 psa_reset_key_attributes( &attributes );
3666 psa_destroy_key( key );
3667 mbedtls_free( signature );
3668 PSA_DONE( );
3669}
3670/* END_CASE */
3671
3672/* BEGIN_CASE */
3673void sign_verify_message( int key_type_arg,
3674 data_t *key_data,
3675 int alg_arg,
3676 data_t *input_data )
3677{
3678 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3679 psa_key_type_t key_type = key_type_arg;
3680 psa_algorithm_t alg = alg_arg;
3681 size_t key_bits;
3682 unsigned char *signature = NULL;
3683 size_t signature_size;
3684 size_t signature_length = 0xdeadbeef;
3685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3686
3687 PSA_ASSERT( psa_crypto_init( ) );
3688
3689 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3690 PSA_KEY_USAGE_VERIFY_MESSAGE );
3691 psa_set_key_algorithm( &attributes, alg );
3692 psa_set_key_type( &attributes, key_type );
3693
3694 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3695 &key ) );
3696 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3697 key_bits = psa_get_key_bits( &attributes );
3698
3699 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3700 TEST_ASSERT( signature_size != 0 );
3701 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3702 ASSERT_ALLOC( signature, signature_size );
3703
3704 PSA_ASSERT( psa_sign_message( key, alg,
3705 input_data->x, input_data->len,
3706 signature, signature_size,
3707 &signature_length ) );
3708 TEST_ASSERT( signature_length <= signature_size );
3709 TEST_ASSERT( signature_length > 0 );
3710
3711 PSA_ASSERT( psa_verify_message( key, alg,
3712 input_data->x, input_data->len,
3713 signature, signature_length ) );
3714
3715 if( input_data->len != 0 )
3716 {
3717 /* Flip a bit in the input and verify that the signature is now
3718 * detected as invalid. Flip a bit at the beginning, not at the end,
3719 * because ECDSA may ignore the last few bits of the input. */
3720 input_data->x[0] ^= 1;
3721 TEST_EQUAL( psa_verify_message( key, alg,
3722 input_data->x, input_data->len,
3723 signature, signature_length ),
3724 PSA_ERROR_INVALID_SIGNATURE );
3725 }
3726
3727exit:
3728 psa_reset_key_attributes( &attributes );
3729
3730 psa_destroy_key( key );
3731 mbedtls_free( signature );
3732 PSA_DONE( );
3733}
3734/* END_CASE */
3735
3736/* BEGIN_CASE */
3737void verify_message( int key_type_arg,
3738 data_t *key_data,
3739 int alg_arg,
3740 data_t *input_data,
3741 data_t *signature_data )
3742{
3743 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3744 psa_key_type_t key_type = key_type_arg;
3745 psa_algorithm_t alg = alg_arg;
3746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3747
3748 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3749
3750 PSA_ASSERT( psa_crypto_init( ) );
3751
3752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3753 psa_set_key_algorithm( &attributes, alg );
3754 psa_set_key_type( &attributes, key_type );
3755
3756 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3757 &key ) );
3758
3759 PSA_ASSERT( psa_verify_message( key, alg,
3760 input_data->x, input_data->len,
3761 signature_data->x, signature_data->len ) );
3762
3763exit:
3764 psa_reset_key_attributes( &attributes );
3765 psa_destroy_key( key );
3766 PSA_DONE( );
3767}
3768/* END_CASE */
3769
3770/* BEGIN_CASE */
3771void verify_message_fail( int key_type_arg,
3772 data_t *key_data,
3773 int alg_arg,
3774 data_t *hash_data,
3775 data_t *signature_data,
3776 int expected_status_arg )
3777{
3778 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3779 psa_key_type_t key_type = key_type_arg;
3780 psa_algorithm_t alg = alg_arg;
3781 psa_status_t actual_status;
3782 psa_status_t expected_status = expected_status_arg;
3783 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3784
3785 PSA_ASSERT( psa_crypto_init( ) );
3786
3787 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3788 psa_set_key_algorithm( &attributes, alg );
3789 psa_set_key_type( &attributes, key_type );
3790
3791 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3792 &key ) );
3793
3794 actual_status = psa_verify_message( key, alg,
3795 hash_data->x, hash_data->len,
3796 signature_data->x,
3797 signature_data->len );
3798 TEST_EQUAL( actual_status, expected_status );
3799
3800exit:
3801 psa_reset_key_attributes( &attributes );
3802 psa_destroy_key( key );
3803 PSA_DONE( );
3804}
3805/* END_CASE */
3806
3807/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003808void asymmetric_encrypt( int key_type_arg,
3809 data_t *key_data,
3810 int alg_arg,
3811 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003812 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003813 int expected_output_length_arg,
3814 int expected_status_arg )
3815{
Ronald Cron5425a212020-08-04 14:58:35 +02003816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003817 psa_key_type_t key_type = key_type_arg;
3818 psa_algorithm_t alg = alg_arg;
3819 size_t expected_output_length = expected_output_length_arg;
3820 size_t key_bits;
3821 unsigned char *output = NULL;
3822 size_t output_size;
3823 size_t output_length = ~0;
3824 psa_status_t actual_status;
3825 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003827
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003829
Gilles Peskine656896e2018-06-29 19:12:28 +02003830 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3832 psa_set_key_algorithm( &attributes, alg );
3833 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003834 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003835 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003836
3837 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003838 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003839 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003840
Gilles Peskine656896e2018-06-29 19:12:28 +02003841 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003842 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003843 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003844
3845 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003846 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003847 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003848 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003849 output, output_size,
3850 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003851 TEST_EQUAL( actual_status, expected_status );
3852 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003853
Gilles Peskine68428122018-06-30 18:42:41 +02003854 /* If the label is empty, the test framework puts a non-null pointer
3855 * in label->x. Test that a null pointer works as well. */
3856 if( label->len == 0 )
3857 {
3858 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003859 if( output_size != 0 )
3860 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003861 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003862 input_data->x, input_data->len,
3863 NULL, label->len,
3864 output, output_size,
3865 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003866 TEST_EQUAL( actual_status, expected_status );
3867 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003868 }
3869
Gilles Peskine656896e2018-06-29 19:12:28 +02003870exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003871 /*
3872 * Key attributes may have been returned by psa_get_key_attributes()
3873 * thus reset them as required.
3874 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003875 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003876
Ronald Cron5425a212020-08-04 14:58:35 +02003877 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003878 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003879 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003880}
3881/* END_CASE */
3882
3883/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003884void asymmetric_encrypt_decrypt( int key_type_arg,
3885 data_t *key_data,
3886 int alg_arg,
3887 data_t *input_data,
3888 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003889{
Ronald Cron5425a212020-08-04 14:58:35 +02003890 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003891 psa_key_type_t key_type = key_type_arg;
3892 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003893 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003894 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003895 size_t output_size;
3896 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003897 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003898 size_t output2_size;
3899 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003900 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901
Gilles Peskine8817f612018-12-18 00:18:46 +01003902 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003904 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3905 psa_set_key_algorithm( &attributes, alg );
3906 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003907
Gilles Peskine049c7532019-05-15 20:22:09 +02003908 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003909 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003910
3911 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003912 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003913 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003914
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003915 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003916 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003917 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003918
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003919 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003920 TEST_ASSERT( output2_size <=
3921 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3922 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003923 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003924
Gilles Peskineeebd7382018-06-08 18:11:54 +02003925 /* We test encryption by checking that encrypt-then-decrypt gives back
3926 * the original plaintext because of the non-optional random
3927 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003928 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003929 input_data->x, input_data->len,
3930 label->x, label->len,
3931 output, output_size,
3932 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003933 /* We don't know what ciphertext length to expect, but check that
3934 * it looks sensible. */
3935 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003936
Ronald Cron5425a212020-08-04 14:58:35 +02003937 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003938 output, output_length,
3939 label->x, label->len,
3940 output2, output2_size,
3941 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003942 ASSERT_COMPARE( input_data->x, input_data->len,
3943 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944
3945exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003946 /*
3947 * Key attributes may have been returned by psa_get_key_attributes()
3948 * thus reset them as required.
3949 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003950 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003951
Ronald Cron5425a212020-08-04 14:58:35 +02003952 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003953 mbedtls_free( output );
3954 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003955 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956}
3957/* END_CASE */
3958
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003960void asymmetric_decrypt( int key_type_arg,
3961 data_t *key_data,
3962 int alg_arg,
3963 data_t *input_data,
3964 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003965 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966{
Ronald Cron5425a212020-08-04 14:58:35 +02003967 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968 psa_key_type_t key_type = key_type_arg;
3969 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003970 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003971 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003972 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003973 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975
Gilles Peskine8817f612018-12-18 00:18:46 +01003976 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003978 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3979 psa_set_key_algorithm( &attributes, alg );
3980 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003981
Gilles Peskine049c7532019-05-15 20:22:09 +02003982 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003983 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003984
gabor-mezei-armceface22021-01-21 12:26:17 +01003985 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3986 key_bits = psa_get_key_bits( &attributes );
3987
3988 /* Determine the maximum ciphertext length */
3989 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3990 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3991 ASSERT_ALLOC( output, output_size );
3992
Ronald Cron5425a212020-08-04 14:58:35 +02003993 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003994 input_data->x, input_data->len,
3995 label->x, label->len,
3996 output,
3997 output_size,
3998 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003999 ASSERT_COMPARE( expected_data->x, expected_data->len,
4000 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004001
Gilles Peskine68428122018-06-30 18:42:41 +02004002 /* If the label is empty, the test framework puts a non-null pointer
4003 * in label->x. Test that a null pointer works as well. */
4004 if( label->len == 0 )
4005 {
4006 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004007 if( output_size != 0 )
4008 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004009 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004010 input_data->x, input_data->len,
4011 NULL, label->len,
4012 output,
4013 output_size,
4014 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004015 ASSERT_COMPARE( expected_data->x, expected_data->len,
4016 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004017 }
4018
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004020 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004021 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004022 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004023 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024}
4025/* END_CASE */
4026
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004028void asymmetric_decrypt_fail( int key_type_arg,
4029 data_t *key_data,
4030 int alg_arg,
4031 data_t *input_data,
4032 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004033 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004034 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035{
Ronald Cron5425a212020-08-04 14:58:35 +02004036 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004037 psa_key_type_t key_type = key_type_arg;
4038 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004039 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004040 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004041 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004042 psa_status_t actual_status;
4043 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004045
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004046 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004047
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004049
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004050 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4051 psa_set_key_algorithm( &attributes, alg );
4052 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004053
Gilles Peskine049c7532019-05-15 20:22:09 +02004054 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004055 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004056
Ronald Cron5425a212020-08-04 14:58:35 +02004057 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004058 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004059 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004060 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004061 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004062 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004063 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004064
Gilles Peskine68428122018-06-30 18:42:41 +02004065 /* If the label is empty, the test framework puts a non-null pointer
4066 * in label->x. Test that a null pointer works as well. */
4067 if( label->len == 0 )
4068 {
4069 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004070 if( output_size != 0 )
4071 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004072 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004073 input_data->x, input_data->len,
4074 NULL, label->len,
4075 output, output_size,
4076 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004077 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004078 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004079 }
4080
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004082 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004083 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004084 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004085 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004087/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004088
4089/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004090void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004091{
4092 /* Test each valid way of initializing the object, except for `= {0}`, as
4093 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4094 * though it's OK by the C standard. We could test for this, but we'd need
4095 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004096 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004097 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4098 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4099 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004100
4101 memset( &zero, 0, sizeof( zero ) );
4102
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004103 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004104 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004105 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004106 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004107 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004108 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004109 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004110
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004111 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004112 PSA_ASSERT( psa_key_derivation_abort(&func) );
4113 PSA_ASSERT( psa_key_derivation_abort(&init) );
4114 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004115}
4116/* END_CASE */
4117
Janos Follath16de4a42019-06-13 16:32:24 +01004118/* BEGIN_CASE */
4119void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004120{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004121 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004122 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004123 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004124
Gilles Peskine8817f612018-12-18 00:18:46 +01004125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004126
Janos Follath16de4a42019-06-13 16:32:24 +01004127 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004128 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004129
4130exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004131 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004132 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004133}
4134/* END_CASE */
4135
Janos Follathaf3c2a02019-06-12 12:34:34 +01004136/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004137void derive_set_capacity( int alg_arg, int capacity_arg,
4138 int expected_status_arg )
4139{
4140 psa_algorithm_t alg = alg_arg;
4141 size_t capacity = capacity_arg;
4142 psa_status_t expected_status = expected_status_arg;
4143 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4144
4145 PSA_ASSERT( psa_crypto_init( ) );
4146
4147 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4148
4149 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4150 expected_status );
4151
4152exit:
4153 psa_key_derivation_abort( &operation );
4154 PSA_DONE( );
4155}
4156/* END_CASE */
4157
4158/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004159void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004160 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004161 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004162 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004163 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004164 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004165 int expected_status_arg3,
4166 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004167{
4168 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004169 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4170 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004171 psa_status_t expected_statuses[] = {expected_status_arg1,
4172 expected_status_arg2,
4173 expected_status_arg3};
4174 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004175 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4176 MBEDTLS_SVC_KEY_ID_INIT,
4177 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004178 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4180 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004181 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004182 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004183 psa_status_t expected_output_status = expected_output_status_arg;
4184 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004185
4186 PSA_ASSERT( psa_crypto_init( ) );
4187
4188 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4189 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004190
4191 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4192
4193 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4194 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004195 mbedtls_test_set_step( i );
4196 if( steps[i] == 0 )
4197 {
4198 /* Skip this step */
4199 }
4200 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004201 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004202 psa_set_key_type( &attributes, key_types[i] );
4203 PSA_ASSERT( psa_import_key( &attributes,
4204 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004205 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004206 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4207 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4208 {
4209 // When taking a private key as secret input, use key agreement
4210 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004211 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4212 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004213 expected_statuses[i] );
4214 }
4215 else
4216 {
4217 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004218 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004219 expected_statuses[i] );
4220 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004221 }
4222 else
4223 {
4224 TEST_EQUAL( psa_key_derivation_input_bytes(
4225 &operation, steps[i],
4226 inputs[i]->x, inputs[i]->len ),
4227 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004228 }
4229 }
4230
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004231 if( output_key_type != PSA_KEY_TYPE_NONE )
4232 {
4233 psa_reset_key_attributes( &attributes );
4234 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4235 psa_set_key_bits( &attributes, 8 );
4236 actual_output_status =
4237 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004238 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004239 }
4240 else
4241 {
4242 uint8_t buffer[1];
4243 actual_output_status =
4244 psa_key_derivation_output_bytes( &operation,
4245 buffer, sizeof( buffer ) );
4246 }
4247 TEST_EQUAL( actual_output_status, expected_output_status );
4248
Janos Follathaf3c2a02019-06-12 12:34:34 +01004249exit:
4250 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004251 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4252 psa_destroy_key( keys[i] );
4253 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004254 PSA_DONE( );
4255}
4256/* END_CASE */
4257
Janos Follathd958bb72019-07-03 15:02:16 +01004258/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004259void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004260{
Janos Follathd958bb72019-07-03 15:02:16 +01004261 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004262 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004263 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004265 unsigned char input1[] = "Input 1";
4266 size_t input1_length = sizeof( input1 );
4267 unsigned char input2[] = "Input 2";
4268 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004269 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004270 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004271 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4272 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4273 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004275
Gilles Peskine8817f612018-12-18 00:18:46 +01004276 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004277
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004278 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4279 psa_set_key_algorithm( &attributes, alg );
4280 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004281
Gilles Peskine73676cb2019-05-15 20:15:10 +02004282 PSA_ASSERT( psa_import_key( &attributes,
4283 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004284 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004285
4286 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004287 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4288 input1, input1_length,
4289 input2, input2_length,
4290 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004291 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004292
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004293 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004294 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004295 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004296
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004297 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004298
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004299 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004300 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004301
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004302exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004303 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004304 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004305 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004306}
4307/* END_CASE */
4308
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004309/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004310void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004311{
4312 uint8_t output_buffer[16];
4313 size_t buffer_size = 16;
4314 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004315 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004316
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004317 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4318 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004319 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004320
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004321 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004322 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004323
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004324 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004325
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004326 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4327 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004328 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004329
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004330 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004331 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004332
4333exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004334 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004335}
4336/* END_CASE */
4337
4338/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004339void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004340 int step1_arg, data_t *input1,
4341 int step2_arg, data_t *input2,
4342 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004343 int requested_capacity_arg,
4344 data_t *expected_output1,
4345 data_t *expected_output2 )
4346{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004347 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004348 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4349 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004350 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4351 MBEDTLS_SVC_KEY_ID_INIT,
4352 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004353 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004355 uint8_t *expected_outputs[2] =
4356 {expected_output1->x, expected_output2->x};
4357 size_t output_sizes[2] =
4358 {expected_output1->len, expected_output2->len};
4359 size_t output_buffer_size = 0;
4360 uint8_t *output_buffer = NULL;
4361 size_t expected_capacity;
4362 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004364 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004365 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004366
4367 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4368 {
4369 if( output_sizes[i] > output_buffer_size )
4370 output_buffer_size = output_sizes[i];
4371 if( output_sizes[i] == 0 )
4372 expected_outputs[i] = NULL;
4373 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004374 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004375 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004376
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004377 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4378 psa_set_key_algorithm( &attributes, alg );
4379 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004380
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004381 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004382 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4383 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4384 requested_capacity ) );
4385 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004386 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004387 switch( steps[i] )
4388 {
4389 case 0:
4390 break;
4391 case PSA_KEY_DERIVATION_INPUT_SECRET:
4392 PSA_ASSERT( psa_import_key( &attributes,
4393 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004394 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004395
4396 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4397 {
4398 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4399 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4400 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4401 }
4402
Gilles Peskine1468da72019-05-29 17:35:49 +02004403 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004404 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004405 break;
4406 default:
4407 PSA_ASSERT( psa_key_derivation_input_bytes(
4408 &operation, steps[i],
4409 inputs[i]->x, inputs[i]->len ) );
4410 break;
4411 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004412 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004413
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004415 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004416 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004417 expected_capacity = requested_capacity;
4418
4419 /* Expansion phase. */
4420 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4421 {
4422 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004424 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004425 if( expected_capacity == 0 && output_sizes[i] == 0 )
4426 {
4427 /* Reading 0 bytes when 0 bytes are available can go either way. */
4428 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004429 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004430 continue;
4431 }
4432 else if( expected_capacity == 0 ||
4433 output_sizes[i] > expected_capacity )
4434 {
4435 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004436 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004437 expected_capacity = 0;
4438 continue;
4439 }
4440 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004441 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004442 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004443 ASSERT_COMPARE( output_buffer, output_sizes[i],
4444 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004445 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004446 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004448 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004449 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004450 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004452
4453exit:
4454 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004456 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4457 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004458 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004459}
4460/* END_CASE */
4461
4462/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004463void derive_full( int alg_arg,
4464 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004465 data_t *input1,
4466 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004467 int requested_capacity_arg )
4468{
Ronald Cron5425a212020-08-04 14:58:35 +02004469 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004470 psa_algorithm_t alg = alg_arg;
4471 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004472 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004473 unsigned char output_buffer[16];
4474 size_t expected_capacity = requested_capacity;
4475 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004477
Gilles Peskine8817f612018-12-18 00:18:46 +01004478 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004479
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4481 psa_set_key_algorithm( &attributes, alg );
4482 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004483
Gilles Peskine049c7532019-05-15 20:22:09 +02004484 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004485 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004486
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004487 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4488 input1->x, input1->len,
4489 input2->x, input2->len,
4490 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004491 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004494 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004495 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004496
4497 /* Expansion phase. */
4498 while( current_capacity > 0 )
4499 {
4500 size_t read_size = sizeof( output_buffer );
4501 if( read_size > current_capacity )
4502 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004503 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004504 output_buffer,
4505 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004506 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004507 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004508 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004509 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004510 }
4511
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004512 /* Check that the operation refuses to go over capacity. */
4513 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004514 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004515
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004516 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004517
4518exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004519 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004520 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004521 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004522}
4523/* END_CASE */
4524
Janos Follathe60c9052019-07-03 13:51:30 +01004525/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004526void derive_key_exercise( int alg_arg,
4527 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004528 data_t *input1,
4529 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004530 int derived_type_arg,
4531 int derived_bits_arg,
4532 int derived_usage_arg,
4533 int derived_alg_arg )
4534{
Ronald Cron5425a212020-08-04 14:58:35 +02004535 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4536 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004537 psa_algorithm_t alg = alg_arg;
4538 psa_key_type_t derived_type = derived_type_arg;
4539 size_t derived_bits = derived_bits_arg;
4540 psa_key_usage_t derived_usage = derived_usage_arg;
4541 psa_algorithm_t derived_alg = derived_alg_arg;
4542 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004543 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004545 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004546
Gilles Peskine8817f612018-12-18 00:18:46 +01004547 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004548
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004549 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4550 psa_set_key_algorithm( &attributes, alg );
4551 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004552 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004553 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004554
4555 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004556 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4557 input1->x, input1->len,
4558 input2->x, input2->len,
4559 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004560 goto exit;
4561
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004562 psa_set_key_usage_flags( &attributes, derived_usage );
4563 psa_set_key_algorithm( &attributes, derived_alg );
4564 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004565 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004566 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004567 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004568
4569 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004570 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004571 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4572 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004573
4574 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004575 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004576 goto exit;
4577
4578exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004579 /*
4580 * Key attributes may have been returned by psa_get_key_attributes()
4581 * thus reset them as required.
4582 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004583 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004584
4585 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004586 psa_destroy_key( base_key );
4587 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004588 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004589}
4590/* END_CASE */
4591
Janos Follath42fd8882019-07-03 14:17:09 +01004592/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004593void derive_key_export( int alg_arg,
4594 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004595 data_t *input1,
4596 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004597 int bytes1_arg,
4598 int bytes2_arg )
4599{
Ronald Cron5425a212020-08-04 14:58:35 +02004600 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4601 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004602 psa_algorithm_t alg = alg_arg;
4603 size_t bytes1 = bytes1_arg;
4604 size_t bytes2 = bytes2_arg;
4605 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004606 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004607 uint8_t *output_buffer = NULL;
4608 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004609 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4610 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004611 size_t length;
4612
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004613 ASSERT_ALLOC( output_buffer, capacity );
4614 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004616
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004617 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4618 psa_set_key_algorithm( &base_attributes, alg );
4619 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004620 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004621 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004622
4623 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004624 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4625 input1->x, input1->len,
4626 input2->x, input2->len,
4627 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004628 goto exit;
4629
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004630 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004631 output_buffer,
4632 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004633 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004634
4635 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004636 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4637 input1->x, input1->len,
4638 input2->x, input2->len,
4639 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004640 goto exit;
4641
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004642 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4643 psa_set_key_algorithm( &derived_attributes, 0 );
4644 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004645 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004646 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004647 &derived_key ) );
4648 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004649 export_buffer, bytes1,
4650 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004651 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004652 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004653 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004654 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004655 &derived_key ) );
4656 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004657 export_buffer + bytes1, bytes2,
4658 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004659 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004660
4661 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004662 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4663 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004664
4665exit:
4666 mbedtls_free( output_buffer );
4667 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004669 psa_destroy_key( base_key );
4670 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004671 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004672}
4673/* END_CASE */
4674
4675/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004676void derive_key( int alg_arg,
4677 data_t *key_data, data_t *input1, data_t *input2,
4678 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004679 int expected_status_arg,
4680 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004681{
Ronald Cron5425a212020-08-04 14:58:35 +02004682 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4683 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004684 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004685 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004686 size_t bits = bits_arg;
4687 psa_status_t expected_status = expected_status_arg;
4688 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4689 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4690 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4691
4692 PSA_ASSERT( psa_crypto_init( ) );
4693
4694 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4695 psa_set_key_algorithm( &base_attributes, alg );
4696 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4697 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004698 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004699
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004700 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4701 input1->x, input1->len,
4702 input2->x, input2->len,
4703 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004704 goto exit;
4705
4706 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4707 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004708 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004709 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004710
4711 psa_status_t status =
4712 psa_key_derivation_output_key( &derived_attributes,
4713 &operation,
4714 &derived_key );
4715 if( is_large_output > 0 )
4716 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4717 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004718
4719exit:
4720 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004721 psa_destroy_key( base_key );
4722 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004723 PSA_DONE( );
4724}
4725/* END_CASE */
4726
4727/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004728void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004729 int our_key_type_arg, int our_key_alg_arg,
4730 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004731 int expected_status_arg )
4732{
Ronald Cron5425a212020-08-04 14:58:35 +02004733 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004734 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004735 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004736 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004739 psa_status_t expected_status = expected_status_arg;
4740 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004741
Gilles Peskine8817f612018-12-18 00:18:46 +01004742 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004743
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004744 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004745 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004746 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004747 PSA_ASSERT( psa_import_key( &attributes,
4748 our_key_data->x, our_key_data->len,
4749 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004750
Gilles Peskine77f40d82019-04-11 21:27:06 +02004751 /* The tests currently include inputs that should fail at either step.
4752 * Test cases that fail at the setup step should be changed to call
4753 * key_derivation_setup instead, and this function should be renamed
4754 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004756 if( status == PSA_SUCCESS )
4757 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004758 TEST_EQUAL( psa_key_derivation_key_agreement(
4759 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4760 our_key,
4761 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004762 expected_status );
4763 }
4764 else
4765 {
4766 TEST_ASSERT( status == expected_status );
4767 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004768
4769exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004771 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004772 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004773}
4774/* END_CASE */
4775
4776/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004777void raw_key_agreement( int alg_arg,
4778 int our_key_type_arg, data_t *our_key_data,
4779 data_t *peer_key_data,
4780 data_t *expected_output )
4781{
Ronald Cron5425a212020-08-04 14:58:35 +02004782 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004783 psa_algorithm_t alg = alg_arg;
4784 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004786 unsigned char *output = NULL;
4787 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004788 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004789
4790 ASSERT_ALLOC( output, expected_output->len );
4791 PSA_ASSERT( psa_crypto_init( ) );
4792
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, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004796 PSA_ASSERT( psa_import_key( &attributes,
4797 our_key_data->x, our_key_data->len,
4798 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004799
gabor-mezei-armceface22021-01-21 12:26:17 +01004800 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4801 key_bits = psa_get_key_bits( &attributes );
4802
Gilles Peskinebe697d82019-05-16 18:00:41 +02004803 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4804 peer_key_data->x, peer_key_data->len,
4805 output, expected_output->len,
4806 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004807 ASSERT_COMPARE( output, output_length,
4808 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004809 TEST_ASSERT( output_length <=
4810 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4811 TEST_ASSERT( output_length <=
4812 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004813
4814exit:
4815 mbedtls_free( output );
4816 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004817 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004818}
4819/* END_CASE */
4820
4821/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004822void key_agreement_capacity( int alg_arg,
4823 int our_key_type_arg, data_t *our_key_data,
4824 data_t *peer_key_data,
4825 int expected_capacity_arg )
4826{
Ronald Cron5425a212020-08-04 14:58:35 +02004827 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004828 psa_algorithm_t alg = alg_arg;
4829 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004831 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004832 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004833 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004834
Gilles Peskine8817f612018-12-18 00:18:46 +01004835 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004836
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004837 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4838 psa_set_key_algorithm( &attributes, alg );
4839 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004840 PSA_ASSERT( psa_import_key( &attributes,
4841 our_key_data->x, our_key_data->len,
4842 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004843
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004844 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004845 PSA_ASSERT( psa_key_derivation_key_agreement(
4846 &operation,
4847 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4848 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004849 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4850 {
4851 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004853 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004854 NULL, 0 ) );
4855 }
Gilles Peskine59685592018-09-18 12:11:34 +02004856
Gilles Peskinebf491972018-10-25 22:36:12 +02004857 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004858 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004859 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004860 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004861
Gilles Peskinebf491972018-10-25 22:36:12 +02004862 /* Test the actual capacity by reading the output. */
4863 while( actual_capacity > sizeof( output ) )
4864 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004866 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004867 actual_capacity -= sizeof( output );
4868 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004869 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004870 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004871 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004872 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004873
Gilles Peskine59685592018-09-18 12:11:34 +02004874exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004876 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004877 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004878}
4879/* END_CASE */
4880
4881/* BEGIN_CASE */
4882void key_agreement_output( int alg_arg,
4883 int our_key_type_arg, data_t *our_key_data,
4884 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004885 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004886{
Ronald Cron5425a212020-08-04 14:58:35 +02004887 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004888 psa_algorithm_t alg = alg_arg;
4889 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004892 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004893
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004894 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4895 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004896
Gilles Peskine8817f612018-12-18 00:18:46 +01004897 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004898
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004899 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4900 psa_set_key_algorithm( &attributes, alg );
4901 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004902 PSA_ASSERT( psa_import_key( &attributes,
4903 our_key_data->x, our_key_data->len,
4904 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004905
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004907 PSA_ASSERT( psa_key_derivation_key_agreement(
4908 &operation,
4909 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4910 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004911 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4912 {
4913 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004915 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004916 NULL, 0 ) );
4917 }
Gilles Peskine59685592018-09-18 12:11:34 +02004918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004920 actual_output,
4921 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004922 ASSERT_COMPARE( actual_output, expected_output1->len,
4923 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004924 if( expected_output2->len != 0 )
4925 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004927 actual_output,
4928 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004929 ASSERT_COMPARE( actual_output, expected_output2->len,
4930 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004931 }
Gilles Peskine59685592018-09-18 12:11:34 +02004932
4933exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004935 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004936 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004937 mbedtls_free( actual_output );
4938}
4939/* END_CASE */
4940
4941/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004942void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004943{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004944 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004945 unsigned char *output = NULL;
4946 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004947 size_t i;
4948 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004949
Simon Butcher49f8e312020-03-03 15:51:50 +00004950 TEST_ASSERT( bytes_arg >= 0 );
4951
Gilles Peskine91892022021-02-08 19:50:26 +01004952 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004953 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004954
Gilles Peskine8817f612018-12-18 00:18:46 +01004955 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004956
Gilles Peskinea50d7392018-06-21 10:22:13 +02004957 /* Run several times, to ensure that every output byte will be
4958 * nonzero at least once with overwhelming probability
4959 * (2^(-8*number_of_runs)). */
4960 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004961 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004962 if( bytes != 0 )
4963 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004964 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004965
Gilles Peskinea50d7392018-06-21 10:22:13 +02004966 for( i = 0; i < bytes; i++ )
4967 {
4968 if( output[i] != 0 )
4969 ++changed[i];
4970 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004971 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004972
4973 /* Check that every byte was changed to nonzero at least once. This
4974 * validates that psa_generate_random is overwriting every byte of
4975 * the output buffer. */
4976 for( i = 0; i < bytes; i++ )
4977 {
4978 TEST_ASSERT( changed[i] != 0 );
4979 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004980
4981exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004982 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004983 mbedtls_free( output );
4984 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004985}
4986/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004987
4988/* BEGIN_CASE */
4989void generate_key( int type_arg,
4990 int bits_arg,
4991 int usage_arg,
4992 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004993 int expected_status_arg,
4994 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004995{
Ronald Cron5425a212020-08-04 14:58:35 +02004996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004997 psa_key_type_t type = type_arg;
4998 psa_key_usage_t usage = usage_arg;
4999 size_t bits = bits_arg;
5000 psa_algorithm_t alg = alg_arg;
5001 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005003 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005004
Gilles Peskine8817f612018-12-18 00:18:46 +01005005 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005006
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005007 psa_set_key_usage_flags( &attributes, usage );
5008 psa_set_key_algorithm( &attributes, alg );
5009 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005010 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005011
5012 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005013 psa_status_t status = psa_generate_key( &attributes, &key );
5014
5015 if( is_large_key > 0 )
5016 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5017 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005018 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005019 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005020
5021 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005022 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005023 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5024 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005025
Gilles Peskine818ca122018-06-20 18:16:48 +02005026 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005027 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005028 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005029
5030exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005031 /*
5032 * Key attributes may have been returned by psa_get_key_attributes()
5033 * thus reset them as required.
5034 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005035 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005036
Ronald Cron5425a212020-08-04 14:58:35 +02005037 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005038 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005039}
5040/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005041
Ronald Cronee414c72021-03-18 18:50:08 +01005042/* 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 +02005043void generate_key_rsa( int bits_arg,
5044 data_t *e_arg,
5045 int expected_status_arg )
5046{
Ronald Cron5425a212020-08-04 14:58:35 +02005047 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005048 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005049 size_t bits = bits_arg;
5050 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5051 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5052 psa_status_t expected_status = expected_status_arg;
5053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5054 uint8_t *exported = NULL;
5055 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005056 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005057 size_t exported_length = SIZE_MAX;
5058 uint8_t *e_read_buffer = NULL;
5059 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005060 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005061 size_t e_read_length = SIZE_MAX;
5062
5063 if( e_arg->len == 0 ||
5064 ( e_arg->len == 3 &&
5065 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5066 {
5067 is_default_public_exponent = 1;
5068 e_read_size = 0;
5069 }
5070 ASSERT_ALLOC( e_read_buffer, e_read_size );
5071 ASSERT_ALLOC( exported, exported_size );
5072
5073 PSA_ASSERT( psa_crypto_init( ) );
5074
5075 psa_set_key_usage_flags( &attributes, usage );
5076 psa_set_key_algorithm( &attributes, alg );
5077 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5078 e_arg->x, e_arg->len ) );
5079 psa_set_key_bits( &attributes, bits );
5080
5081 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005082 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005083 if( expected_status != PSA_SUCCESS )
5084 goto exit;
5085
5086 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005087 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005088 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5089 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5090 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5091 e_read_buffer, e_read_size,
5092 &e_read_length ) );
5093 if( is_default_public_exponent )
5094 TEST_EQUAL( e_read_length, 0 );
5095 else
5096 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5097
5098 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005099 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005100 goto exit;
5101
5102 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005103 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005104 exported, exported_size,
5105 &exported_length ) );
5106 {
5107 uint8_t *p = exported;
5108 uint8_t *end = exported + exported_length;
5109 size_t len;
5110 /* RSAPublicKey ::= SEQUENCE {
5111 * modulus INTEGER, -- n
5112 * publicExponent INTEGER } -- e
5113 */
5114 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005115 MBEDTLS_ASN1_SEQUENCE |
5116 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005117 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005118 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5119 MBEDTLS_ASN1_INTEGER ) );
5120 if( len >= 1 && p[0] == 0 )
5121 {
5122 ++p;
5123 --len;
5124 }
5125 if( e_arg->len == 0 )
5126 {
5127 TEST_EQUAL( len, 3 );
5128 TEST_EQUAL( p[0], 1 );
5129 TEST_EQUAL( p[1], 0 );
5130 TEST_EQUAL( p[2], 1 );
5131 }
5132 else
5133 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5134 }
5135
5136exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005137 /*
5138 * Key attributes may have been returned by psa_get_key_attributes() or
5139 * set by psa_set_key_domain_parameters() thus reset them as required.
5140 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005141 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005142
Ronald Cron5425a212020-08-04 14:58:35 +02005143 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005144 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005145 mbedtls_free( e_read_buffer );
5146 mbedtls_free( exported );
5147}
5148/* END_CASE */
5149
Darryl Greend49a4992018-06-18 17:27:26 +01005150/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005151void persistent_key_load_key_from_storage( data_t *data,
5152 int type_arg, int bits_arg,
5153 int usage_flags_arg, int alg_arg,
5154 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005155{
Ronald Cron71016a92020-08-28 19:01:50 +02005156 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5159 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005160 psa_key_type_t type = type_arg;
5161 size_t bits = bits_arg;
5162 psa_key_usage_t usage_flags = usage_flags_arg;
5163 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005165 unsigned char *first_export = NULL;
5166 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005167 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005168 size_t first_exported_length;
5169 size_t second_exported_length;
5170
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005171 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5172 {
5173 ASSERT_ALLOC( first_export, export_size );
5174 ASSERT_ALLOC( second_export, export_size );
5175 }
Darryl Greend49a4992018-06-18 17:27:26 +01005176
Gilles Peskine8817f612018-12-18 00:18:46 +01005177 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005178
Gilles Peskinec87af662019-05-15 16:12:22 +02005179 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005180 psa_set_key_usage_flags( &attributes, usage_flags );
5181 psa_set_key_algorithm( &attributes, alg );
5182 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005183 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005184
Darryl Green0c6575a2018-11-07 16:05:30 +00005185 switch( generation_method )
5186 {
5187 case IMPORT_KEY:
5188 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005189 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005190 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005191 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005192
Darryl Green0c6575a2018-11-07 16:05:30 +00005193 case GENERATE_KEY:
5194 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005195 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005196 break;
5197
5198 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005199#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005200 {
5201 /* Create base key */
5202 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5203 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5204 psa_set_key_usage_flags( &base_attributes,
5205 PSA_KEY_USAGE_DERIVE );
5206 psa_set_key_algorithm( &base_attributes, derive_alg );
5207 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005208 PSA_ASSERT( psa_import_key( &base_attributes,
5209 data->x, data->len,
5210 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005211 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005213 PSA_ASSERT( psa_key_derivation_input_key(
5214 &operation,
5215 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005216 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005218 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005219 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5220 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005221 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005223 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005224 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005225 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005226#else
5227 TEST_ASSUME( ! "KDF not supported in this configuration" );
5228#endif
5229 break;
5230
5231 default:
5232 TEST_ASSERT( ! "generation_method not implemented in test" );
5233 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005234 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005235 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005236
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005237 /* Export the key if permitted by the key policy. */
5238 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5239 {
Ronald Cron5425a212020-08-04 14:58:35 +02005240 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005241 first_export, export_size,
5242 &first_exported_length ) );
5243 if( generation_method == IMPORT_KEY )
5244 ASSERT_COMPARE( data->x, data->len,
5245 first_export, first_exported_length );
5246 }
Darryl Greend49a4992018-06-18 17:27:26 +01005247
5248 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005249 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005250 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005251 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005252
Darryl Greend49a4992018-06-18 17:27:26 +01005253 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005254 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005255 TEST_ASSERT( mbedtls_svc_key_id_equal(
5256 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005257 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5258 PSA_KEY_LIFETIME_PERSISTENT );
5259 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5260 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5261 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5262 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005263
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005264 /* Export the key again if permitted by the key policy. */
5265 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005266 {
Ronald Cron5425a212020-08-04 14:58:35 +02005267 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005268 second_export, export_size,
5269 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005270 ASSERT_COMPARE( first_export, first_exported_length,
5271 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005272 }
5273
5274 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005275 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005276 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005277
5278exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005279 /*
5280 * Key attributes may have been returned by psa_get_key_attributes()
5281 * thus reset them as required.
5282 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005283 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005284
Darryl Greend49a4992018-06-18 17:27:26 +01005285 mbedtls_free( first_export );
5286 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005288 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005289 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005290 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005291}
5292/* END_CASE */