blob: 5c4d2f597bba1a457b29444239094bbe6cb47958 [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 ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001609 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001610 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1611 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001612 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001613 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001614 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001615
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001616 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001617 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001618 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001619 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001620
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001621 /* Check that update calls abort on error. */
1622 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1623 operation.ctx.mbedtls_ctx.alg = PSA_ALG_XTS;
1624 ASSERT_OPERATION_IS_ACTIVE( operation );
1625 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1626 PSA_ERROR_BAD_STATE );
1627 ASSERT_OPERATION_IS_INACTIVE( operation );
1628 PSA_ASSERT( psa_hash_abort( &operation ) );
1629 ASSERT_OPERATION_IS_INACTIVE( operation );
1630
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001631 /* Call update after finish. */
1632 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1633 PSA_ASSERT( psa_hash_finish( &operation,
1634 hash, sizeof( hash ), &hash_len ) );
1635 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001636 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001637 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001638
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001639 /* Call verify without calling setup beforehand. */
1640 TEST_EQUAL( psa_hash_verify( &operation,
1641 valid_hash, sizeof( valid_hash ) ),
1642 PSA_ERROR_BAD_STATE );
1643 PSA_ASSERT( psa_hash_abort( &operation ) );
1644
1645 /* Call verify after finish. */
1646 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1647 PSA_ASSERT( psa_hash_finish( &operation,
1648 hash, sizeof( hash ), &hash_len ) );
1649 TEST_EQUAL( psa_hash_verify( &operation,
1650 valid_hash, sizeof( valid_hash ) ),
1651 PSA_ERROR_BAD_STATE );
1652 PSA_ASSERT( psa_hash_abort( &operation ) );
1653
1654 /* Call verify twice in a row. */
1655 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001656 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001657 PSA_ASSERT( psa_hash_verify( &operation,
1658 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001659 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001660 TEST_EQUAL( psa_hash_verify( &operation,
1661 valid_hash, sizeof( valid_hash ) ),
1662 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001663 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001664 PSA_ASSERT( psa_hash_abort( &operation ) );
1665
1666 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001667 TEST_EQUAL( psa_hash_finish( &operation,
1668 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001669 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001670 PSA_ASSERT( psa_hash_abort( &operation ) );
1671
1672 /* Call finish twice in a row. */
1673 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1674 PSA_ASSERT( psa_hash_finish( &operation,
1675 hash, sizeof( hash ), &hash_len ) );
1676 TEST_EQUAL( psa_hash_finish( &operation,
1677 hash, sizeof( hash ), &hash_len ),
1678 PSA_ERROR_BAD_STATE );
1679 PSA_ASSERT( psa_hash_abort( &operation ) );
1680
1681 /* Call finish after calling verify. */
1682 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1683 PSA_ASSERT( psa_hash_verify( &operation,
1684 valid_hash, sizeof( valid_hash ) ) );
1685 TEST_EQUAL( psa_hash_finish( &operation,
1686 hash, sizeof( hash ), &hash_len ),
1687 PSA_ERROR_BAD_STATE );
1688 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001689
1690exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001691 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001692}
1693/* END_CASE */
1694
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001695/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001696void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001697{
1698 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001699 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1700 * appended to it */
1701 unsigned char hash[] = {
1702 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1703 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1704 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001705 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001706 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001707
Gilles Peskine8817f612018-12-18 00:18:46 +01001708 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001709
itayzafrir27e69452018-11-01 14:26:34 +02001710 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001711 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001712 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001713 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001714 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001715 ASSERT_OPERATION_IS_INACTIVE( operation );
1716 PSA_ASSERT( psa_hash_abort( &operation ) );
1717 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001718
itayzafrir27e69452018-11-01 14:26:34 +02001719 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001721 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001722 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001723
itayzafrir27e69452018-11-01 14:26:34 +02001724 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001725 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001726 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001727 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001728
itayzafrirec93d302018-10-18 18:01:10 +03001729exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001730 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001731}
1732/* END_CASE */
1733
Ronald Cronee414c72021-03-18 18:50:08 +01001734/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001735void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001736{
1737 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001738 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001739 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001740 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001741 size_t hash_len;
1742
Gilles Peskine8817f612018-12-18 00:18:46 +01001743 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001744
itayzafrir58028322018-10-25 10:22:01 +03001745 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001746 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001747 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001748 hash, expected_size - 1, &hash_len ),
1749 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001750
1751exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001752 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001753}
1754/* END_CASE */
1755
Ronald Cronee414c72021-03-18 18:50:08 +01001756/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001757void hash_clone_source_state( )
1758{
1759 psa_algorithm_t alg = PSA_ALG_SHA_256;
1760 unsigned char hash[PSA_HASH_MAX_SIZE];
1761 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1762 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1763 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1764 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1765 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1766 size_t hash_len;
1767
1768 PSA_ASSERT( psa_crypto_init( ) );
1769 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1770
1771 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1772 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1773 PSA_ASSERT( psa_hash_finish( &op_finished,
1774 hash, sizeof( hash ), &hash_len ) );
1775 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1776 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1777
1778 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1779 PSA_ERROR_BAD_STATE );
1780
1781 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1782 PSA_ASSERT( psa_hash_finish( &op_init,
1783 hash, sizeof( hash ), &hash_len ) );
1784 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1785 PSA_ASSERT( psa_hash_finish( &op_finished,
1786 hash, sizeof( hash ), &hash_len ) );
1787 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1788 PSA_ASSERT( psa_hash_finish( &op_aborted,
1789 hash, sizeof( hash ), &hash_len ) );
1790
1791exit:
1792 psa_hash_abort( &op_source );
1793 psa_hash_abort( &op_init );
1794 psa_hash_abort( &op_setup );
1795 psa_hash_abort( &op_finished );
1796 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001797 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001798}
1799/* END_CASE */
1800
Ronald Cronee414c72021-03-18 18:50:08 +01001801/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001802void hash_clone_target_state( )
1803{
1804 psa_algorithm_t alg = PSA_ALG_SHA_256;
1805 unsigned char hash[PSA_HASH_MAX_SIZE];
1806 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1807 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1808 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1809 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1810 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1811 size_t hash_len;
1812
1813 PSA_ASSERT( psa_crypto_init( ) );
1814
1815 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1816 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1817 PSA_ASSERT( psa_hash_finish( &op_finished,
1818 hash, sizeof( hash ), &hash_len ) );
1819 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1820 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1821
1822 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1823 PSA_ASSERT( psa_hash_finish( &op_target,
1824 hash, sizeof( hash ), &hash_len ) );
1825
1826 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1827 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1828 PSA_ERROR_BAD_STATE );
1829 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1830 PSA_ERROR_BAD_STATE );
1831
1832exit:
1833 psa_hash_abort( &op_target );
1834 psa_hash_abort( &op_init );
1835 psa_hash_abort( &op_setup );
1836 psa_hash_abort( &op_finished );
1837 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001838 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001839}
1840/* END_CASE */
1841
itayzafrir58028322018-10-25 10:22:01 +03001842/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001843void mac_operation_init( )
1844{
Jaeden Amero252ef282019-02-15 14:05:35 +00001845 const uint8_t input[1] = { 0 };
1846
Jaeden Amero769ce272019-01-04 11:48:03 +00001847 /* Test each valid way of initializing the object, except for `= {0}`, as
1848 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1849 * though it's OK by the C standard. We could test for this, but we'd need
1850 * to supress the Clang warning for the test. */
1851 psa_mac_operation_t func = psa_mac_operation_init( );
1852 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1853 psa_mac_operation_t zero;
1854
1855 memset( &zero, 0, sizeof( zero ) );
1856
Jaeden Amero252ef282019-02-15 14:05:35 +00001857 /* A freshly-initialized MAC operation should not be usable. */
1858 TEST_EQUAL( psa_mac_update( &func,
1859 input, sizeof( input ) ),
1860 PSA_ERROR_BAD_STATE );
1861 TEST_EQUAL( psa_mac_update( &init,
1862 input, sizeof( input ) ),
1863 PSA_ERROR_BAD_STATE );
1864 TEST_EQUAL( psa_mac_update( &zero,
1865 input, sizeof( input ) ),
1866 PSA_ERROR_BAD_STATE );
1867
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001868 /* A default MAC operation should be abortable without error. */
1869 PSA_ASSERT( psa_mac_abort( &func ) );
1870 PSA_ASSERT( psa_mac_abort( &init ) );
1871 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001872}
1873/* END_CASE */
1874
1875/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001876void mac_setup( int key_type_arg,
1877 data_t *key,
1878 int alg_arg,
1879 int expected_status_arg )
1880{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001881 psa_key_type_t key_type = key_type_arg;
1882 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001883 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001884 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001885 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1886#if defined(KNOWN_SUPPORTED_MAC_ALG)
1887 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1888#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001889
Gilles Peskine8817f612018-12-18 00:18:46 +01001890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001891
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001892 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1893 &operation, &status ) )
1894 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001895 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001896
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001897 /* The operation object should be reusable. */
1898#if defined(KNOWN_SUPPORTED_MAC_ALG)
1899 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1900 smoke_test_key_data,
1901 sizeof( smoke_test_key_data ),
1902 KNOWN_SUPPORTED_MAC_ALG,
1903 &operation, &status ) )
1904 goto exit;
1905 TEST_EQUAL( status, PSA_SUCCESS );
1906#endif
1907
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001909 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001910}
1911/* END_CASE */
1912
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001913/* 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 +00001914void mac_bad_order( )
1915{
Ronald Cron5425a212020-08-04 14:58:35 +02001916 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001917 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1918 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001919 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001920 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1921 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1922 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001923 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001924 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1925 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1926 size_t sign_mac_length = 0;
1927 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1928 const uint8_t verify_mac[] = {
1929 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1930 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1931 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1932
1933 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001935 psa_set_key_algorithm( &attributes, alg );
1936 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001937
Ronald Cron5425a212020-08-04 14:58:35 +02001938 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1939 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001940
Jaeden Amero252ef282019-02-15 14:05:35 +00001941 /* Call update without calling setup beforehand. */
1942 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1943 PSA_ERROR_BAD_STATE );
1944 PSA_ASSERT( psa_mac_abort( &operation ) );
1945
1946 /* Call sign finish without calling setup beforehand. */
1947 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1948 &sign_mac_length),
1949 PSA_ERROR_BAD_STATE );
1950 PSA_ASSERT( psa_mac_abort( &operation ) );
1951
1952 /* Call verify finish without calling setup beforehand. */
1953 TEST_EQUAL( psa_mac_verify_finish( &operation,
1954 verify_mac, sizeof( verify_mac ) ),
1955 PSA_ERROR_BAD_STATE );
1956 PSA_ASSERT( psa_mac_abort( &operation ) );
1957
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001958 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001959 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001960 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001961 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001962 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001963 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001964 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001965 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001966
Jaeden Amero252ef282019-02-15 14:05:35 +00001967 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001968 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001969 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1970 PSA_ASSERT( psa_mac_sign_finish( &operation,
1971 sign_mac, sizeof( sign_mac ),
1972 &sign_mac_length ) );
1973 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1974 PSA_ERROR_BAD_STATE );
1975 PSA_ASSERT( psa_mac_abort( &operation ) );
1976
1977 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001978 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001979 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1980 PSA_ASSERT( psa_mac_verify_finish( &operation,
1981 verify_mac, sizeof( verify_mac ) ) );
1982 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1983 PSA_ERROR_BAD_STATE );
1984 PSA_ASSERT( psa_mac_abort( &operation ) );
1985
1986 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001987 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001988 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1989 PSA_ASSERT( psa_mac_sign_finish( &operation,
1990 sign_mac, sizeof( sign_mac ),
1991 &sign_mac_length ) );
1992 TEST_EQUAL( psa_mac_sign_finish( &operation,
1993 sign_mac, sizeof( sign_mac ),
1994 &sign_mac_length ),
1995 PSA_ERROR_BAD_STATE );
1996 PSA_ASSERT( psa_mac_abort( &operation ) );
1997
1998 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001999 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002000 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2001 PSA_ASSERT( psa_mac_verify_finish( &operation,
2002 verify_mac, sizeof( verify_mac ) ) );
2003 TEST_EQUAL( psa_mac_verify_finish( &operation,
2004 verify_mac, sizeof( verify_mac ) ),
2005 PSA_ERROR_BAD_STATE );
2006 PSA_ASSERT( psa_mac_abort( &operation ) );
2007
2008 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002009 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002010 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002011 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002012 TEST_EQUAL( psa_mac_verify_finish( &operation,
2013 verify_mac, sizeof( verify_mac ) ),
2014 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002015 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002016 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002017 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002018
2019 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002020 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002021 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002022 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002023 TEST_EQUAL( psa_mac_sign_finish( &operation,
2024 sign_mac, sizeof( sign_mac ),
2025 &sign_mac_length ),
2026 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002027 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002028 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002029 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002030
Ronald Cron5425a212020-08-04 14:58:35 +02002031 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002032
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002033exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002034 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002035}
2036/* END_CASE */
2037
2038/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002040 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002041 int alg_arg,
2042 data_t *input,
2043 data_t *expected_mac )
2044{
Ronald Cron5425a212020-08-04 14:58:35 +02002045 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002046 psa_key_type_t key_type = key_type_arg;
2047 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002048 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002050 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002051 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002052 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002053 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002054 const size_t output_sizes_to_test[] = {
2055 0,
2056 1,
2057 expected_mac->len - 1,
2058 expected_mac->len,
2059 expected_mac->len + 1,
2060 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002061
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002062 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002063 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002064 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002065
Gilles Peskine8817f612018-12-18 00:18:46 +01002066 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002067
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002068 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002069 psa_set_key_algorithm( &attributes, alg );
2070 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002071
Ronald Cron5425a212020-08-04 14:58:35 +02002072 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2073 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002074
Gilles Peskine8b356b52020-08-25 23:44:59 +02002075 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2076 {
2077 const size_t output_size = output_sizes_to_test[i];
2078 psa_status_t expected_status =
2079 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2080 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002081
Chris Jones9634bb12021-01-20 15:56:42 +00002082 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002083 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002084
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002085 /* Calculate the MAC, one-shot case. */
2086 TEST_EQUAL( psa_mac_compute( key, alg,
2087 input->x, input->len,
2088 actual_mac, output_size, &mac_length ),
2089 expected_status );
2090 if( expected_status == PSA_SUCCESS )
2091 {
2092 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2093 actual_mac, mac_length );
2094 }
2095
2096 if( output_size > 0 )
2097 memset( actual_mac, 0, output_size );
2098
2099 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002100 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002101 PSA_ASSERT( psa_mac_update( &operation,
2102 input->x, input->len ) );
2103 TEST_EQUAL( psa_mac_sign_finish( &operation,
2104 actual_mac, output_size,
2105 &mac_length ),
2106 expected_status );
2107 PSA_ASSERT( psa_mac_abort( &operation ) );
2108
2109 if( expected_status == PSA_SUCCESS )
2110 {
2111 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2112 actual_mac, mac_length );
2113 }
2114 mbedtls_free( actual_mac );
2115 actual_mac = NULL;
2116 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002117
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002118exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002119 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002120 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002121 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002122 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002123}
2124/* END_CASE */
2125
2126/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002127void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002128 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002129 int alg_arg,
2130 data_t *input,
2131 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132{
Ronald Cron5425a212020-08-04 14:58:35 +02002133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134 psa_key_type_t key_type = key_type_arg;
2135 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002136 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002137 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002138 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002139
Gilles Peskine69c12672018-06-28 00:07:19 +02002140 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2141
Gilles Peskine8817f612018-12-18 00:18:46 +01002142 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002143
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002144 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002145 psa_set_key_algorithm( &attributes, alg );
2146 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002147
Ronald Cron5425a212020-08-04 14:58:35 +02002148 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2149 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002150
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002151 /* Verify correct MAC, one-shot case. */
2152 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2153 expected_mac->x, expected_mac->len ) );
2154
2155 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002156 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002157 PSA_ASSERT( psa_mac_update( &operation,
2158 input->x, input->len ) );
2159 PSA_ASSERT( psa_mac_verify_finish( &operation,
2160 expected_mac->x,
2161 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002162
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002163 /* Test a MAC that's too short, one-shot case. */
2164 TEST_EQUAL( psa_mac_verify( key, alg,
2165 input->x, input->len,
2166 expected_mac->x,
2167 expected_mac->len - 1 ),
2168 PSA_ERROR_INVALID_SIGNATURE );
2169
2170 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002171 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002172 PSA_ASSERT( psa_mac_update( &operation,
2173 input->x, input->len ) );
2174 TEST_EQUAL( psa_mac_verify_finish( &operation,
2175 expected_mac->x,
2176 expected_mac->len - 1 ),
2177 PSA_ERROR_INVALID_SIGNATURE );
2178
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002179 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002180 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2181 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002182 TEST_EQUAL( psa_mac_verify( key, alg,
2183 input->x, input->len,
2184 perturbed_mac, expected_mac->len + 1 ),
2185 PSA_ERROR_INVALID_SIGNATURE );
2186
2187 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002188 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002189 PSA_ASSERT( psa_mac_update( &operation,
2190 input->x, input->len ) );
2191 TEST_EQUAL( psa_mac_verify_finish( &operation,
2192 perturbed_mac,
2193 expected_mac->len + 1 ),
2194 PSA_ERROR_INVALID_SIGNATURE );
2195
2196 /* Test changing one byte. */
2197 for( size_t i = 0; i < expected_mac->len; i++ )
2198 {
Chris Jones9634bb12021-01-20 15:56:42 +00002199 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002200 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002201
2202 TEST_EQUAL( psa_mac_verify( key, alg,
2203 input->x, input->len,
2204 perturbed_mac, expected_mac->len ),
2205 PSA_ERROR_INVALID_SIGNATURE );
2206
Ronald Cron5425a212020-08-04 14:58:35 +02002207 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002208 PSA_ASSERT( psa_mac_update( &operation,
2209 input->x, input->len ) );
2210 TEST_EQUAL( psa_mac_verify_finish( &operation,
2211 perturbed_mac,
2212 expected_mac->len ),
2213 PSA_ERROR_INVALID_SIGNATURE );
2214 perturbed_mac[i] ^= 1;
2215 }
2216
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002218 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002219 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002220 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002221 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002222}
2223/* END_CASE */
2224
2225/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002226void cipher_operation_init( )
2227{
Jaeden Ameroab439972019-02-15 14:12:05 +00002228 const uint8_t input[1] = { 0 };
2229 unsigned char output[1] = { 0 };
2230 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002231 /* Test each valid way of initializing the object, except for `= {0}`, as
2232 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2233 * though it's OK by the C standard. We could test for this, but we'd need
2234 * to supress the Clang warning for the test. */
2235 psa_cipher_operation_t func = psa_cipher_operation_init( );
2236 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2237 psa_cipher_operation_t zero;
2238
2239 memset( &zero, 0, sizeof( zero ) );
2240
Jaeden Ameroab439972019-02-15 14:12:05 +00002241 /* A freshly-initialized cipher operation should not be usable. */
2242 TEST_EQUAL( psa_cipher_update( &func,
2243 input, sizeof( input ),
2244 output, sizeof( output ),
2245 &output_length ),
2246 PSA_ERROR_BAD_STATE );
2247 TEST_EQUAL( psa_cipher_update( &init,
2248 input, sizeof( input ),
2249 output, sizeof( output ),
2250 &output_length ),
2251 PSA_ERROR_BAD_STATE );
2252 TEST_EQUAL( psa_cipher_update( &zero,
2253 input, sizeof( input ),
2254 output, sizeof( output ),
2255 &output_length ),
2256 PSA_ERROR_BAD_STATE );
2257
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002258 /* A default cipher operation should be abortable without error. */
2259 PSA_ASSERT( psa_cipher_abort( &func ) );
2260 PSA_ASSERT( psa_cipher_abort( &init ) );
2261 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002262}
2263/* END_CASE */
2264
2265/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002266void cipher_setup( int key_type_arg,
2267 data_t *key,
2268 int alg_arg,
2269 int expected_status_arg )
2270{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002271 psa_key_type_t key_type = key_type_arg;
2272 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002273 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002274 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002275 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002276#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002277 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2278#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002279
Gilles Peskine8817f612018-12-18 00:18:46 +01002280 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002281
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002282 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2283 &operation, &status ) )
2284 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002285 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002286
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002287 /* The operation object should be reusable. */
2288#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2289 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2290 smoke_test_key_data,
2291 sizeof( smoke_test_key_data ),
2292 KNOWN_SUPPORTED_CIPHER_ALG,
2293 &operation, &status ) )
2294 goto exit;
2295 TEST_EQUAL( status, PSA_SUCCESS );
2296#endif
2297
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002298exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002299 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002300 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002301}
2302/* END_CASE */
2303
Ronald Cronee414c72021-03-18 18:50:08 +01002304/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002305void cipher_bad_order( )
2306{
Ronald Cron5425a212020-08-04 14:58:35 +02002307 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002308 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2309 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002310 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002311 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002312 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002313 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002314 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2315 0xaa, 0xaa, 0xaa, 0xaa };
2316 const uint8_t text[] = {
2317 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2318 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002319 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002320 size_t length = 0;
2321
2322 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002323 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2324 psa_set_key_algorithm( &attributes, alg );
2325 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002326 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2327 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002328
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002329 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002330 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002331 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002332 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002333 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002335 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002336 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002337
2338 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002339 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002340 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002341 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002342 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002343 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002344 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002345 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002346
Jaeden Ameroab439972019-02-15 14:12:05 +00002347 /* Generate an IV without calling setup beforehand. */
2348 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2349 buffer, sizeof( buffer ),
2350 &length ),
2351 PSA_ERROR_BAD_STATE );
2352 PSA_ASSERT( psa_cipher_abort( &operation ) );
2353
2354 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002355 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002356 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2357 buffer, sizeof( buffer ),
2358 &length ) );
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_generate_iv( &operation,
2361 buffer, sizeof( buffer ),
2362 &length ),
2363 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002364 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002365 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002366 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002367
2368 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002369 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002370 PSA_ASSERT( psa_cipher_set_iv( &operation,
2371 iv, sizeof( iv ) ) );
2372 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2373 buffer, sizeof( buffer ),
2374 &length ),
2375 PSA_ERROR_BAD_STATE );
2376 PSA_ASSERT( psa_cipher_abort( &operation ) );
2377
2378 /* Set an IV without calling setup beforehand. */
2379 TEST_EQUAL( psa_cipher_set_iv( &operation,
2380 iv, sizeof( iv ) ),
2381 PSA_ERROR_BAD_STATE );
2382 PSA_ASSERT( psa_cipher_abort( &operation ) );
2383
2384 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002386 PSA_ASSERT( psa_cipher_set_iv( &operation,
2387 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002388 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002389 TEST_EQUAL( psa_cipher_set_iv( &operation,
2390 iv, sizeof( iv ) ),
2391 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002392 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002393 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002394 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002395
2396 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002397 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002398 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2399 buffer, sizeof( buffer ),
2400 &length ) );
2401 TEST_EQUAL( psa_cipher_set_iv( &operation,
2402 iv, sizeof( iv ) ),
2403 PSA_ERROR_BAD_STATE );
2404 PSA_ASSERT( psa_cipher_abort( &operation ) );
2405
2406 /* Call update without calling setup beforehand. */
2407 TEST_EQUAL( psa_cipher_update( &operation,
2408 text, sizeof( text ),
2409 buffer, sizeof( buffer ),
2410 &length ),
2411 PSA_ERROR_BAD_STATE );
2412 PSA_ASSERT( psa_cipher_abort( &operation ) );
2413
2414 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002415 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002416 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002417 TEST_EQUAL( psa_cipher_update( &operation,
2418 text, sizeof( text ),
2419 buffer, sizeof( buffer ),
2420 &length ),
2421 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002422 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002423 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002424 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002425
2426 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002427 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002428 PSA_ASSERT( psa_cipher_set_iv( &operation,
2429 iv, sizeof( iv ) ) );
2430 PSA_ASSERT( psa_cipher_finish( &operation,
2431 buffer, sizeof( buffer ), &length ) );
2432 TEST_EQUAL( psa_cipher_update( &operation,
2433 text, sizeof( text ),
2434 buffer, sizeof( buffer ),
2435 &length ),
2436 PSA_ERROR_BAD_STATE );
2437 PSA_ASSERT( psa_cipher_abort( &operation ) );
2438
2439 /* Call finish without calling setup beforehand. */
2440 TEST_EQUAL( psa_cipher_finish( &operation,
2441 buffer, sizeof( buffer ), &length ),
2442 PSA_ERROR_BAD_STATE );
2443 PSA_ASSERT( psa_cipher_abort( &operation ) );
2444
2445 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002446 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002447 /* Not calling update means we are encrypting an empty buffer, which is OK
2448 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002449 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002450 TEST_EQUAL( psa_cipher_finish( &operation,
2451 buffer, sizeof( buffer ), &length ),
2452 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002453 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002454 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002455 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002456
2457 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002458 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002459 PSA_ASSERT( psa_cipher_set_iv( &operation,
2460 iv, sizeof( iv ) ) );
2461 PSA_ASSERT( psa_cipher_finish( &operation,
2462 buffer, sizeof( buffer ), &length ) );
2463 TEST_EQUAL( psa_cipher_finish( &operation,
2464 buffer, sizeof( buffer ), &length ),
2465 PSA_ERROR_BAD_STATE );
2466 PSA_ASSERT( psa_cipher_abort( &operation ) );
2467
Ronald Cron5425a212020-08-04 14:58:35 +02002468 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002469
Jaeden Ameroab439972019-02-15 14:12:05 +00002470exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002471 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002472 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002473}
2474/* END_CASE */
2475
2476/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002477void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002478 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002479 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002480 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481{
Ronald Cron5425a212020-08-04 14:58:35 +02002482 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002483 psa_status_t status;
2484 psa_key_type_t key_type = key_type_arg;
2485 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002486 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002487 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002488 size_t output_buffer_size = 0;
2489 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002490 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002491 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002492 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002493
Gilles Peskine8817f612018-12-18 00:18:46 +01002494 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002495
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002496 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2497 psa_set_key_algorithm( &attributes, alg );
2498 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002499
Ronald Cron5425a212020-08-04 14:58:35 +02002500 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2501 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002502
Ronald Cron5425a212020-08-04 14:58:35 +02002503 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002504
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002505 if( iv->len > 0 )
2506 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002507 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002508 }
2509
gabor-mezei-armceface22021-01-21 12:26:17 +01002510 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2511 TEST_ASSERT( output_buffer_size <=
2512 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002513 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002514
Gilles Peskine8817f612018-12-18 00:18:46 +01002515 PSA_ASSERT( psa_cipher_update( &operation,
2516 input->x, input->len,
2517 output, output_buffer_size,
2518 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002519 TEST_ASSERT( function_output_length <=
2520 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2521 TEST_ASSERT( function_output_length <=
2522 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002523 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002524
Gilles Peskine50e586b2018-06-08 14:28:46 +02002525 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002526 ( output_buffer_size == 0 ? NULL :
2527 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002528 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002529 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002530 TEST_ASSERT( function_output_length <=
2531 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2532 TEST_ASSERT( function_output_length <=
2533 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002534 total_output_length += function_output_length;
2535
Gilles Peskinefe11b722018-12-18 00:24:04 +01002536 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002537 if( expected_status == PSA_SUCCESS )
2538 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002539 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002540 ASSERT_COMPARE( expected_output->x, expected_output->len,
2541 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002542 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002543
Gilles Peskine50e586b2018-06-08 14:28:46 +02002544exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002545 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002546 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002547 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002548 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002549}
2550/* END_CASE */
2551
2552/* BEGIN_CASE */
2553void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002554 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002555 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002556 int first_part_size_arg,
2557 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002558 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002559{
Ronald Cron5425a212020-08-04 14:58:35 +02002560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002561 psa_key_type_t key_type = key_type_arg;
2562 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002563 size_t first_part_size = first_part_size_arg;
2564 size_t output1_length = output1_length_arg;
2565 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002566 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002567 size_t output_buffer_size = 0;
2568 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002569 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002570 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002572
Gilles Peskine8817f612018-12-18 00:18:46 +01002573 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002574
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002575 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2576 psa_set_key_algorithm( &attributes, alg );
2577 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002578
Ronald Cron5425a212020-08-04 14:58:35 +02002579 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2580 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002581
Ronald Cron5425a212020-08-04 14:58:35 +02002582 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002584 if( iv->len > 0 )
2585 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002586 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002587 }
2588
gabor-mezei-armceface22021-01-21 12:26:17 +01002589 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2590 TEST_ASSERT( output_buffer_size <=
2591 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002592 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002593
Gilles Peskinee0866522019-02-19 19:44:00 +01002594 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002595 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2596 output, output_buffer_size,
2597 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002598 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002599 TEST_ASSERT( function_output_length <=
2600 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2601 TEST_ASSERT( function_output_length <=
2602 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002603 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002604
Gilles Peskine8817f612018-12-18 00:18:46 +01002605 PSA_ASSERT( psa_cipher_update( &operation,
2606 input->x + first_part_size,
2607 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002608 ( output_buffer_size == 0 ? NULL :
2609 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002610 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002611 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002612 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002613 TEST_ASSERT( function_output_length <=
2614 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2615 alg,
2616 input->len - first_part_size ) );
2617 TEST_ASSERT( function_output_length <=
2618 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002619 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002620
Gilles Peskine8817f612018-12-18 00:18:46 +01002621 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002622 ( output_buffer_size == 0 ? NULL :
2623 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002624 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002626 TEST_ASSERT( function_output_length <=
2627 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2628 TEST_ASSERT( function_output_length <=
2629 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002630 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002631 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002632
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002633 ASSERT_COMPARE( expected_output->x, expected_output->len,
2634 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635
2636exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002637 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002638 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002639 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002640 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002641}
2642/* END_CASE */
2643
2644/* BEGIN_CASE */
2645void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002646 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002647 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002648 int first_part_size_arg,
2649 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002650 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651{
Ronald Cron5425a212020-08-04 14:58:35 +02002652 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002653 psa_key_type_t key_type = key_type_arg;
2654 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002655 size_t first_part_size = first_part_size_arg;
2656 size_t output1_length = output1_length_arg;
2657 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002658 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659 size_t output_buffer_size = 0;
2660 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002661 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002662 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002663 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002664
Gilles Peskine8817f612018-12-18 00:18:46 +01002665 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002667 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2668 psa_set_key_algorithm( &attributes, alg );
2669 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002670
Ronald Cron5425a212020-08-04 14:58:35 +02002671 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2672 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Ronald Cron5425a212020-08-04 14:58:35 +02002674 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675
Steven Cooreman177deba2020-09-07 17:14:14 +02002676 if( iv->len > 0 )
2677 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002678 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002679 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680
gabor-mezei-armceface22021-01-21 12:26:17 +01002681 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2682 TEST_ASSERT( output_buffer_size <=
2683 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002684 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002685
Gilles Peskinee0866522019-02-19 19:44:00 +01002686 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002687 PSA_ASSERT( psa_cipher_update( &operation,
2688 input->x, first_part_size,
2689 output, output_buffer_size,
2690 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002691 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002692 TEST_ASSERT( function_output_length <=
2693 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2694 TEST_ASSERT( function_output_length <=
2695 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002696 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002697
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_cipher_update( &operation,
2699 input->x + first_part_size,
2700 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002701 ( output_buffer_size == 0 ? NULL :
2702 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002703 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002704 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002705 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002706 TEST_ASSERT( function_output_length <=
2707 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2708 alg,
2709 input->len - first_part_size ) );
2710 TEST_ASSERT( function_output_length <=
2711 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002712 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002713
Gilles Peskine8817f612018-12-18 00:18:46 +01002714 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002715 ( output_buffer_size == 0 ? NULL :
2716 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002717 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002718 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002719 TEST_ASSERT( function_output_length <=
2720 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2721 TEST_ASSERT( function_output_length <=
2722 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002723 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002724 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002725
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002726 ASSERT_COMPARE( expected_output->x, expected_output->len,
2727 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002728
2729exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002730 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002731 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002732 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002733 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002734}
2735/* END_CASE */
2736
Gilles Peskine50e586b2018-06-08 14:28:46 +02002737/* BEGIN_CASE */
2738void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002739 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002740 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002741 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002742{
Ronald Cron5425a212020-08-04 14:58:35 +02002743 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744 psa_status_t status;
2745 psa_key_type_t key_type = key_type_arg;
2746 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002747 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002748 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749 size_t output_buffer_size = 0;
2750 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002751 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002752 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754
Gilles Peskine8817f612018-12-18 00:18:46 +01002755 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002756
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002757 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2758 psa_set_key_algorithm( &attributes, alg );
2759 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002760
Ronald Cron5425a212020-08-04 14:58:35 +02002761 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2762 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002763
Ronald Cron5425a212020-08-04 14:58:35 +02002764 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002765
Steven Cooreman177deba2020-09-07 17:14:14 +02002766 if( iv->len > 0 )
2767 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002768 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002769 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770
gabor-mezei-armceface22021-01-21 12:26:17 +01002771 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2772 TEST_ASSERT( output_buffer_size <=
2773 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002774 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_cipher_update( &operation,
2777 input->x, input->len,
2778 output, output_buffer_size,
2779 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002780 TEST_ASSERT( function_output_length <=
2781 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2782 TEST_ASSERT( function_output_length <=
2783 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002784 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002785
Gilles Peskine50e586b2018-06-08 14:28:46 +02002786 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002787 ( output_buffer_size == 0 ? NULL :
2788 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002789 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002790 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002791 TEST_ASSERT( function_output_length <=
2792 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2793 TEST_ASSERT( function_output_length <=
2794 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002795 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002796 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002797
2798 if( expected_status == PSA_SUCCESS )
2799 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002800 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002801 ASSERT_COMPARE( expected_output->x, expected_output->len,
2802 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002803 }
2804
Gilles Peskine50e586b2018-06-08 14:28:46 +02002805exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002806 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002807 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002808 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002809 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810}
2811/* END_CASE */
2812
Gilles Peskine50e586b2018-06-08 14:28:46 +02002813/* BEGIN_CASE */
2814void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002815 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002816 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002817{
Ronald Cron5425a212020-08-04 14:58:35 +02002818 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002819 psa_key_type_t key_type = key_type_arg;
2820 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002821 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002822 size_t iv_size = 16;
2823 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002824 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002825 size_t output1_size = 0;
2826 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002827 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002828 size_t output2_size = 0;
2829 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002830 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002831 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2832 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002833 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002836
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002837 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2838 psa_set_key_algorithm( &attributes, alg );
2839 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002840
Ronald Cron5425a212020-08-04 14:58:35 +02002841 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2842 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002843
Ronald Cron5425a212020-08-04 14:58:35 +02002844 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2845 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002846
Steven Cooreman177deba2020-09-07 17:14:14 +02002847 if( alg != PSA_ALG_ECB_NO_PADDING )
2848 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002849 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2850 iv, iv_size,
2851 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002852 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002853 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2854 TEST_ASSERT( output1_size <=
2855 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002856 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002857
Gilles Peskine8817f612018-12-18 00:18:46 +01002858 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2859 output1, output1_size,
2860 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002861 TEST_ASSERT( output1_length <=
2862 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2863 TEST_ASSERT( output1_length <=
2864 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2865
Gilles Peskine8817f612018-12-18 00:18:46 +01002866 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002867 output1 + output1_length,
2868 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002869 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002870 TEST_ASSERT( function_output_length <=
2871 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2872 TEST_ASSERT( function_output_length <=
2873 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002874
Gilles Peskine048b7f02018-06-08 14:20:49 +02002875 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002876
Gilles Peskine8817f612018-12-18 00:18:46 +01002877 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002878
2879 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002880 TEST_ASSERT( output2_size <=
2881 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2882 TEST_ASSERT( output2_size <=
2883 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002884 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002885
Steven Cooreman177deba2020-09-07 17:14:14 +02002886 if( iv_length > 0 )
2887 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002888 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2889 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002890 }
2891
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2893 output2, output2_size,
2894 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002895 TEST_ASSERT( output2_length <=
2896 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2897 TEST_ASSERT( output2_length <=
2898 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2899
Gilles Peskine048b7f02018-06-08 14:20:49 +02002900 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002901 PSA_ASSERT( psa_cipher_finish( &operation2,
2902 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002903 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002904 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002905 TEST_ASSERT( function_output_length <=
2906 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2907 TEST_ASSERT( function_output_length <=
2908 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002909
Gilles Peskine048b7f02018-06-08 14:20:49 +02002910 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002911
Gilles Peskine8817f612018-12-18 00:18:46 +01002912 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002913
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002914 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002915
2916exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002917 psa_cipher_abort( &operation1 );
2918 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002919 mbedtls_free( output1 );
2920 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002921 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002922 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002923}
2924/* END_CASE */
2925
2926/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927void cipher_verify_output_multipart( int alg_arg,
2928 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002929 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002930 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002931 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002932{
Ronald Cron5425a212020-08-04 14:58:35 +02002933 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002934 psa_key_type_t key_type = key_type_arg;
2935 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002936 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002937 unsigned char iv[16] = {0};
2938 size_t iv_size = 16;
2939 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002940 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002941 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002942 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002943 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002944 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002945 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002946 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002947 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2948 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002950
Gilles Peskine8817f612018-12-18 00:18:46 +01002951 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002952
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002953 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2954 psa_set_key_algorithm( &attributes, alg );
2955 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002956
Ronald Cron5425a212020-08-04 14:58:35 +02002957 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2958 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002959
Ronald Cron5425a212020-08-04 14:58:35 +02002960 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2961 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002962
Steven Cooreman177deba2020-09-07 17:14:14 +02002963 if( alg != PSA_ALG_ECB_NO_PADDING )
2964 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002965 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2966 iv, iv_size,
2967 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002968 }
2969
gabor-mezei-armceface22021-01-21 12:26:17 +01002970 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2971 TEST_ASSERT( output1_buffer_size <=
2972 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002973 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002974
Gilles Peskinee0866522019-02-19 19:44:00 +01002975 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002976
Gilles Peskine8817f612018-12-18 00:18:46 +01002977 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2978 output1, output1_buffer_size,
2979 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002980 TEST_ASSERT( function_output_length <=
2981 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2982 TEST_ASSERT( function_output_length <=
2983 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002984 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002985
Gilles Peskine8817f612018-12-18 00:18:46 +01002986 PSA_ASSERT( psa_cipher_update( &operation1,
2987 input->x + first_part_size,
2988 input->len - first_part_size,
2989 output1, output1_buffer_size,
2990 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002991 TEST_ASSERT( function_output_length <=
2992 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2993 alg,
2994 input->len - first_part_size ) );
2995 TEST_ASSERT( function_output_length <=
2996 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002997 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002998
Gilles Peskine8817f612018-12-18 00:18:46 +01002999 PSA_ASSERT( psa_cipher_finish( &operation1,
3000 output1 + output1_length,
3001 output1_buffer_size - output1_length,
3002 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003003 TEST_ASSERT( function_output_length <=
3004 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3005 TEST_ASSERT( function_output_length <=
3006 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003007 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003008
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003010
Gilles Peskine048b7f02018-06-08 14:20:49 +02003011 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003012 TEST_ASSERT( output2_buffer_size <=
3013 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3014 TEST_ASSERT( output2_buffer_size <=
3015 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003016 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003017
Steven Cooreman177deba2020-09-07 17:14:14 +02003018 if( iv_length > 0 )
3019 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003020 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3021 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003022 }
Moran Pekerded84402018-06-06 16:36:50 +03003023
Gilles Peskine8817f612018-12-18 00:18:46 +01003024 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3025 output2, output2_buffer_size,
3026 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003027 TEST_ASSERT( function_output_length <=
3028 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3029 TEST_ASSERT( function_output_length <=
3030 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003031 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003032
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 PSA_ASSERT( psa_cipher_update( &operation2,
3034 output1 + first_part_size,
3035 output1_length - first_part_size,
3036 output2, output2_buffer_size,
3037 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003038 TEST_ASSERT( function_output_length <=
3039 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3040 alg,
3041 output1_length - first_part_size ) );
3042 TEST_ASSERT( function_output_length <=
3043 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003044 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003045
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 PSA_ASSERT( psa_cipher_finish( &operation2,
3047 output2 + output2_length,
3048 output2_buffer_size - output2_length,
3049 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003050 TEST_ASSERT( function_output_length <=
3051 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3052 TEST_ASSERT( function_output_length <=
3053 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003054 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003055
Gilles Peskine8817f612018-12-18 00:18:46 +01003056 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003057
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003058 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003059
3060exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003061 psa_cipher_abort( &operation1 );
3062 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003063 mbedtls_free( output1 );
3064 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003065 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003066 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003067}
3068/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003069
Gilles Peskine20035e32018-02-03 22:44:14 +01003070/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003071void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003072 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003073 data_t *nonce,
3074 data_t *additional_data,
3075 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003076 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003077{
Ronald Cron5425a212020-08-04 14:58:35 +02003078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003079 psa_key_type_t key_type = key_type_arg;
3080 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003081 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003082 unsigned char *output_data = NULL;
3083 size_t output_size = 0;
3084 size_t output_length = 0;
3085 unsigned char *output_data2 = NULL;
3086 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003087 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003088 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003089 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003090
Gilles Peskine8817f612018-12-18 00:18:46 +01003091 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003092
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3094 psa_set_key_algorithm( &attributes, alg );
3095 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003096
Gilles Peskine049c7532019-05-15 20:22:09 +02003097 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003098 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003099 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3100 key_bits = psa_get_key_bits( &attributes );
3101
3102 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3103 alg );
3104 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3105 * should be exact. */
3106 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3107 expected_result != PSA_ERROR_NOT_SUPPORTED )
3108 {
3109 TEST_EQUAL( output_size,
3110 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3111 TEST_ASSERT( output_size <=
3112 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3113 }
3114 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003115
Steven Cooremanf49478b2021-02-15 15:19:25 +01003116 status = psa_aead_encrypt( key, alg,
3117 nonce->x, nonce->len,
3118 additional_data->x,
3119 additional_data->len,
3120 input_data->x, input_data->len,
3121 output_data, output_size,
3122 &output_length );
3123
3124 /* If the operation is not supported, just skip and not fail in case the
3125 * encryption involves a common limitation of cryptography hardwares and
3126 * an alternative implementation. */
3127 if( status == PSA_ERROR_NOT_SUPPORTED )
3128 {
3129 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3130 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3131 }
3132
3133 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003134
3135 if( PSA_SUCCESS == expected_result )
3136 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003137 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003138
Gilles Peskine003a4a92019-05-14 16:09:40 +02003139 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3140 * should be exact. */
3141 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003142 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003143
gabor-mezei-armceface22021-01-21 12:26:17 +01003144 TEST_ASSERT( input_data->len <=
3145 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3146
Ronald Cron5425a212020-08-04 14:58:35 +02003147 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003148 nonce->x, nonce->len,
3149 additional_data->x,
3150 additional_data->len,
3151 output_data, output_length,
3152 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003153 &output_length2 ),
3154 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003155
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003156 ASSERT_COMPARE( input_data->x, input_data->len,
3157 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003158 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003159
Gilles Peskinea1cac842018-06-11 19:33:02 +02003160exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003161 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003162 mbedtls_free( output_data );
3163 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003164 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003165}
3166/* END_CASE */
3167
3168/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003169void aead_encrypt( int key_type_arg, data_t *key_data,
3170 int alg_arg,
3171 data_t *nonce,
3172 data_t *additional_data,
3173 data_t *input_data,
3174 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003175{
Ronald Cron5425a212020-08-04 14:58:35 +02003176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003177 psa_key_type_t key_type = key_type_arg;
3178 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003179 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003180 unsigned char *output_data = NULL;
3181 size_t output_size = 0;
3182 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003184 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003187
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003188 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3189 psa_set_key_algorithm( &attributes, alg );
3190 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003191
Gilles Peskine049c7532019-05-15 20:22:09 +02003192 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003193 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003194 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3195 key_bits = psa_get_key_bits( &attributes );
3196
3197 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3198 alg );
3199 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3200 * should be exact. */
3201 TEST_EQUAL( output_size,
3202 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3203 TEST_ASSERT( output_size <=
3204 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3205 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003206
Steven Cooremand588ea12021-01-11 19:36:04 +01003207 status = psa_aead_encrypt( key, alg,
3208 nonce->x, nonce->len,
3209 additional_data->x, additional_data->len,
3210 input_data->x, input_data->len,
3211 output_data, output_size,
3212 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003213
Ronald Cron28a45ed2021-02-09 20:35:42 +01003214 /* If the operation is not supported, just skip and not fail in case the
3215 * encryption involves a common limitation of cryptography hardwares and
3216 * an alternative implementation. */
3217 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003218 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003219 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3220 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003221 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003222
3223 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003224 ASSERT_COMPARE( expected_result->x, expected_result->len,
3225 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003226
Gilles Peskinea1cac842018-06-11 19:33:02 +02003227exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003228 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003229 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003230 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003231}
3232/* END_CASE */
3233
3234/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003235void aead_decrypt( int key_type_arg, data_t *key_data,
3236 int alg_arg,
3237 data_t *nonce,
3238 data_t *additional_data,
3239 data_t *input_data,
3240 data_t *expected_data,
3241 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003242{
Ronald Cron5425a212020-08-04 14:58:35 +02003243 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003244 psa_key_type_t key_type = key_type_arg;
3245 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003246 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003247 unsigned char *output_data = NULL;
3248 size_t output_size = 0;
3249 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003251 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003252 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003253
Gilles Peskine8817f612018-12-18 00:18:46 +01003254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003255
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003256 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3257 psa_set_key_algorithm( &attributes, alg );
3258 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003259
Gilles Peskine049c7532019-05-15 20:22:09 +02003260 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003261 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003262 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3263 key_bits = psa_get_key_bits( &attributes );
3264
3265 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3266 alg );
3267 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3268 expected_result != PSA_ERROR_NOT_SUPPORTED )
3269 {
3270 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3271 * should be exact. */
3272 TEST_EQUAL( output_size,
3273 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3274 TEST_ASSERT( output_size <=
3275 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3276 }
3277 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003278
Steven Cooremand588ea12021-01-11 19:36:04 +01003279 status = psa_aead_decrypt( key, alg,
3280 nonce->x, nonce->len,
3281 additional_data->x,
3282 additional_data->len,
3283 input_data->x, input_data->len,
3284 output_data, output_size,
3285 &output_length );
3286
Ronald Cron28a45ed2021-02-09 20:35:42 +01003287 /* If the operation is not supported, just skip and not fail in case the
3288 * decryption involves a common limitation of cryptography hardwares and
3289 * an alternative implementation. */
3290 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003291 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003292 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3293 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003294 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003295
3296 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003297
Gilles Peskine2d277862018-06-18 15:41:12 +02003298 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003299 ASSERT_COMPARE( expected_data->x, expected_data->len,
3300 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003301
Gilles Peskinea1cac842018-06-11 19:33:02 +02003302exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003303 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003304 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003305 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003306}
3307/* END_CASE */
3308
3309/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003310void signature_size( int type_arg,
3311 int bits,
3312 int alg_arg,
3313 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003314{
3315 psa_key_type_t type = type_arg;
3316 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003317 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003318
Gilles Peskinefe11b722018-12-18 00:24:04 +01003319 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003320#if defined(MBEDTLS_TEST_DEPRECATED)
3321 TEST_EQUAL( actual_size,
3322 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3323#endif /* MBEDTLS_TEST_DEPRECATED */
3324
Gilles Peskinee59236f2018-01-27 23:32:46 +01003325exit:
3326 ;
3327}
3328/* END_CASE */
3329
3330/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003331void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3332 int alg_arg, data_t *input_data,
3333 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003334{
Ronald Cron5425a212020-08-04 14:58:35 +02003335 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003336 psa_key_type_t key_type = key_type_arg;
3337 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003338 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003339 unsigned char *signature = NULL;
3340 size_t signature_size;
3341 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003343
Gilles Peskine8817f612018-12-18 00:18:46 +01003344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003345
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_set_key_algorithm( &attributes, alg );
3348 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003349
Gilles Peskine049c7532019-05-15 20:22:09 +02003350 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003351 &key ) );
3352 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003353 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003354
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003355 /* Allocate a buffer which has the size advertized by the
3356 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003357 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003358 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003359 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003360 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003361 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003362
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003363 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003364 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003365 input_data->x, input_data->len,
3366 signature, signature_size,
3367 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003368 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003369 ASSERT_COMPARE( output_data->x, output_data->len,
3370 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003371
Gilles Peskine0627f982019-11-26 19:12:16 +01003372#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003373 memset( signature, 0, signature_size );
3374 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003375 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003376 input_data->x, input_data->len,
3377 signature, signature_size,
3378 &signature_length ) );
3379 ASSERT_COMPARE( output_data->x, output_data->len,
3380 signature, signature_length );
3381#endif /* MBEDTLS_TEST_DEPRECATED */
3382
Gilles Peskine20035e32018-02-03 22:44:14 +01003383exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003384 /*
3385 * Key attributes may have been returned by psa_get_key_attributes()
3386 * thus reset them as required.
3387 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003388 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003389
Ronald Cron5425a212020-08-04 14:58:35 +02003390 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003391 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003392 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003393}
3394/* END_CASE */
3395
3396/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003397void sign_hash_fail( int key_type_arg, data_t *key_data,
3398 int alg_arg, data_t *input_data,
3399 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003400{
Ronald Cron5425a212020-08-04 14:58:35 +02003401 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003402 psa_key_type_t key_type = key_type_arg;
3403 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003404 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003405 psa_status_t actual_status;
3406 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003407 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003408 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003410
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003411 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003412
Gilles Peskine8817f612018-12-18 00:18:46 +01003413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003414
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003416 psa_set_key_algorithm( &attributes, alg );
3417 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003418
Gilles Peskine049c7532019-05-15 20:22:09 +02003419 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003420 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003421
Ronald Cron5425a212020-08-04 14:58:35 +02003422 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003423 input_data->x, input_data->len,
3424 signature, signature_size,
3425 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003426 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003427 /* The value of *signature_length is unspecified on error, but
3428 * whatever it is, it should be less than signature_size, so that
3429 * if the caller tries to read *signature_length bytes without
3430 * checking the error code then they don't overflow a buffer. */
3431 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003432
Gilles Peskine895242b2019-11-29 12:15:40 +01003433#if defined(MBEDTLS_TEST_DEPRECATED)
3434 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003435 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003436 input_data->x, input_data->len,
3437 signature, signature_size,
3438 &signature_length ),
3439 expected_status );
3440 TEST_ASSERT( signature_length <= signature_size );
3441#endif /* MBEDTLS_TEST_DEPRECATED */
3442
Gilles Peskine20035e32018-02-03 22:44:14 +01003443exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003444 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003445 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003446 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003447 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003448}
3449/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003450
3451/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003452void sign_verify_hash( int key_type_arg, data_t *key_data,
3453 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003454{
Ronald Cron5425a212020-08-04 14:58:35 +02003455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003456 psa_key_type_t key_type = key_type_arg;
3457 psa_algorithm_t alg = alg_arg;
3458 size_t key_bits;
3459 unsigned char *signature = NULL;
3460 size_t signature_size;
3461 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003463
Gilles Peskine8817f612018-12-18 00:18:46 +01003464 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003465
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003466 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003467 psa_set_key_algorithm( &attributes, alg );
3468 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003469
Gilles Peskine049c7532019-05-15 20:22:09 +02003470 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003471 &key ) );
3472 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003473 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003474
3475 /* Allocate a buffer which has the size advertized by the
3476 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003477 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003478 key_bits, alg );
3479 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003480 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003481 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003482
3483 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003484 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003485 input_data->x, input_data->len,
3486 signature, signature_size,
3487 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003488 /* Check that the signature length looks sensible. */
3489 TEST_ASSERT( signature_length <= signature_size );
3490 TEST_ASSERT( signature_length > 0 );
3491
3492 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003493 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003494 input_data->x, input_data->len,
3495 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003496
3497 if( input_data->len != 0 )
3498 {
3499 /* Flip a bit in the input and verify that the signature is now
3500 * detected as invalid. Flip a bit at the beginning, not at the end,
3501 * because ECDSA may ignore the last few bits of the input. */
3502 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003503 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003504 input_data->x, input_data->len,
3505 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003506 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003507 }
3508
3509exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003510 /*
3511 * Key attributes may have been returned by psa_get_key_attributes()
3512 * thus reset them as required.
3513 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003514 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003515
Ronald Cron5425a212020-08-04 14:58:35 +02003516 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003517 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003518 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003519}
3520/* END_CASE */
3521
3522/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003523void verify_hash( int key_type_arg, data_t *key_data,
3524 int alg_arg, data_t *hash_data,
3525 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003526{
Ronald Cron5425a212020-08-04 14:58:35 +02003527 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003528 psa_key_type_t key_type = key_type_arg;
3529 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003531
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003532 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003533
Gilles Peskine8817f612018-12-18 00:18:46 +01003534 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003535
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003536 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003537 psa_set_key_algorithm( &attributes, alg );
3538 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003539
Gilles Peskine049c7532019-05-15 20:22:09 +02003540 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003541 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003542
Ronald Cron5425a212020-08-04 14:58:35 +02003543 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003544 hash_data->x, hash_data->len,
3545 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003546
3547#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003548 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003549 hash_data->x, hash_data->len,
3550 signature_data->x,
3551 signature_data->len ) );
3552
3553#endif /* MBEDTLS_TEST_DEPRECATED */
3554
itayzafrir5c753392018-05-08 11:18:38 +03003555exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003556 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003557 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003558 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003559}
3560/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003561
3562/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003563void verify_hash_fail( int key_type_arg, data_t *key_data,
3564 int alg_arg, data_t *hash_data,
3565 data_t *signature_data,
3566 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567{
Ronald Cron5425a212020-08-04 14:58:35 +02003568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569 psa_key_type_t key_type = key_type_arg;
3570 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003571 psa_status_t actual_status;
3572 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003574
Gilles Peskine8817f612018-12-18 00:18:46 +01003575 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003577 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003578 psa_set_key_algorithm( &attributes, alg );
3579 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003580
Gilles Peskine049c7532019-05-15 20:22:09 +02003581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003582 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003583
Ronald Cron5425a212020-08-04 14:58:35 +02003584 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003585 hash_data->x, hash_data->len,
3586 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003587 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588
Gilles Peskine895242b2019-11-29 12:15:40 +01003589#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003590 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003591 hash_data->x, hash_data->len,
3592 signature_data->x, signature_data->len ),
3593 expected_status );
3594#endif /* MBEDTLS_TEST_DEPRECATED */
3595
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003596exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003597 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003598 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003599 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600}
3601/* END_CASE */
3602
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003603/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003604void sign_message_deterministic( int key_type_arg,
3605 data_t *key_data,
3606 int alg_arg,
3607 data_t *input_data,
3608 data_t *output_data )
3609{
3610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3611 psa_key_type_t key_type = key_type_arg;
3612 psa_algorithm_t alg = alg_arg;
3613 size_t key_bits;
3614 unsigned char *signature = NULL;
3615 size_t signature_size;
3616 size_t signature_length = 0xdeadbeef;
3617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3618
3619 PSA_ASSERT( psa_crypto_init( ) );
3620
3621 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3622 psa_set_key_algorithm( &attributes, alg );
3623 psa_set_key_type( &attributes, key_type );
3624
3625 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3626 &key ) );
3627 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3628 key_bits = psa_get_key_bits( &attributes );
3629
3630 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3631 TEST_ASSERT( signature_size != 0 );
3632 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3633 ASSERT_ALLOC( signature, signature_size );
3634
3635 PSA_ASSERT( psa_sign_message( key, alg,
3636 input_data->x, input_data->len,
3637 signature, signature_size,
3638 &signature_length ) );
3639
3640 ASSERT_COMPARE( output_data->x, output_data->len,
3641 signature, signature_length );
3642
3643exit:
3644 psa_reset_key_attributes( &attributes );
3645
3646 psa_destroy_key( key );
3647 mbedtls_free( signature );
3648 PSA_DONE( );
3649
3650}
3651/* END_CASE */
3652
3653/* BEGIN_CASE */
3654void sign_message_fail( int key_type_arg,
3655 data_t *key_data,
3656 int alg_arg,
3657 data_t *input_data,
3658 int signature_size_arg,
3659 int expected_status_arg )
3660{
3661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3662 psa_key_type_t key_type = key_type_arg;
3663 psa_algorithm_t alg = alg_arg;
3664 size_t signature_size = signature_size_arg;
3665 psa_status_t actual_status;
3666 psa_status_t expected_status = expected_status_arg;
3667 unsigned char *signature = NULL;
3668 size_t signature_length = 0xdeadbeef;
3669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3670
3671 ASSERT_ALLOC( signature, signature_size );
3672
3673 PSA_ASSERT( psa_crypto_init( ) );
3674
3675 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3676 psa_set_key_algorithm( &attributes, alg );
3677 psa_set_key_type( &attributes, key_type );
3678
3679 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3680 &key ) );
3681
3682 actual_status = psa_sign_message( key, alg,
3683 input_data->x, input_data->len,
3684 signature, signature_size,
3685 &signature_length );
3686 TEST_EQUAL( actual_status, expected_status );
3687 /* The value of *signature_length is unspecified on error, but
3688 * whatever it is, it should be less than signature_size, so that
3689 * if the caller tries to read *signature_length bytes without
3690 * checking the error code then they don't overflow a buffer. */
3691 TEST_ASSERT( signature_length <= signature_size );
3692
3693exit:
3694 psa_reset_key_attributes( &attributes );
3695 psa_destroy_key( key );
3696 mbedtls_free( signature );
3697 PSA_DONE( );
3698}
3699/* END_CASE */
3700
3701/* BEGIN_CASE */
3702void sign_verify_message( int key_type_arg,
3703 data_t *key_data,
3704 int alg_arg,
3705 data_t *input_data )
3706{
3707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3708 psa_key_type_t key_type = key_type_arg;
3709 psa_algorithm_t alg = alg_arg;
3710 size_t key_bits;
3711 unsigned char *signature = NULL;
3712 size_t signature_size;
3713 size_t signature_length = 0xdeadbeef;
3714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3715
3716 PSA_ASSERT( psa_crypto_init( ) );
3717
3718 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3719 PSA_KEY_USAGE_VERIFY_MESSAGE );
3720 psa_set_key_algorithm( &attributes, alg );
3721 psa_set_key_type( &attributes, key_type );
3722
3723 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3724 &key ) );
3725 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3726 key_bits = psa_get_key_bits( &attributes );
3727
3728 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3729 TEST_ASSERT( signature_size != 0 );
3730 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3731 ASSERT_ALLOC( signature, signature_size );
3732
3733 PSA_ASSERT( psa_sign_message( key, alg,
3734 input_data->x, input_data->len,
3735 signature, signature_size,
3736 &signature_length ) );
3737 TEST_ASSERT( signature_length <= signature_size );
3738 TEST_ASSERT( signature_length > 0 );
3739
3740 PSA_ASSERT( psa_verify_message( key, alg,
3741 input_data->x, input_data->len,
3742 signature, signature_length ) );
3743
3744 if( input_data->len != 0 )
3745 {
3746 /* Flip a bit in the input and verify that the signature is now
3747 * detected as invalid. Flip a bit at the beginning, not at the end,
3748 * because ECDSA may ignore the last few bits of the input. */
3749 input_data->x[0] ^= 1;
3750 TEST_EQUAL( psa_verify_message( key, alg,
3751 input_data->x, input_data->len,
3752 signature, signature_length ),
3753 PSA_ERROR_INVALID_SIGNATURE );
3754 }
3755
3756exit:
3757 psa_reset_key_attributes( &attributes );
3758
3759 psa_destroy_key( key );
3760 mbedtls_free( signature );
3761 PSA_DONE( );
3762}
3763/* END_CASE */
3764
3765/* BEGIN_CASE */
3766void verify_message( int key_type_arg,
3767 data_t *key_data,
3768 int alg_arg,
3769 data_t *input_data,
3770 data_t *signature_data )
3771{
3772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3773 psa_key_type_t key_type = key_type_arg;
3774 psa_algorithm_t alg = alg_arg;
3775 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3776
3777 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3778
3779 PSA_ASSERT( psa_crypto_init( ) );
3780
3781 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3782 psa_set_key_algorithm( &attributes, alg );
3783 psa_set_key_type( &attributes, key_type );
3784
3785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3786 &key ) );
3787
3788 PSA_ASSERT( psa_verify_message( key, alg,
3789 input_data->x, input_data->len,
3790 signature_data->x, signature_data->len ) );
3791
3792exit:
3793 psa_reset_key_attributes( &attributes );
3794 psa_destroy_key( key );
3795 PSA_DONE( );
3796}
3797/* END_CASE */
3798
3799/* BEGIN_CASE */
3800void verify_message_fail( int key_type_arg,
3801 data_t *key_data,
3802 int alg_arg,
3803 data_t *hash_data,
3804 data_t *signature_data,
3805 int expected_status_arg )
3806{
3807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3808 psa_key_type_t key_type = key_type_arg;
3809 psa_algorithm_t alg = alg_arg;
3810 psa_status_t actual_status;
3811 psa_status_t expected_status = expected_status_arg;
3812 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3813
3814 PSA_ASSERT( psa_crypto_init( ) );
3815
3816 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3817 psa_set_key_algorithm( &attributes, alg );
3818 psa_set_key_type( &attributes, key_type );
3819
3820 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3821 &key ) );
3822
3823 actual_status = psa_verify_message( key, alg,
3824 hash_data->x, hash_data->len,
3825 signature_data->x,
3826 signature_data->len );
3827 TEST_EQUAL( actual_status, expected_status );
3828
3829exit:
3830 psa_reset_key_attributes( &attributes );
3831 psa_destroy_key( key );
3832 PSA_DONE( );
3833}
3834/* END_CASE */
3835
3836/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003837void asymmetric_encrypt( int key_type_arg,
3838 data_t *key_data,
3839 int alg_arg,
3840 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003841 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003842 int expected_output_length_arg,
3843 int expected_status_arg )
3844{
Ronald Cron5425a212020-08-04 14:58:35 +02003845 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003846 psa_key_type_t key_type = key_type_arg;
3847 psa_algorithm_t alg = alg_arg;
3848 size_t expected_output_length = expected_output_length_arg;
3849 size_t key_bits;
3850 unsigned char *output = NULL;
3851 size_t output_size;
3852 size_t output_length = ~0;
3853 psa_status_t actual_status;
3854 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003856
Gilles Peskine8817f612018-12-18 00:18:46 +01003857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003858
Gilles Peskine656896e2018-06-29 19:12:28 +02003859 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003860 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3861 psa_set_key_algorithm( &attributes, alg );
3862 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003864 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003865
3866 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003867 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003868 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003869
Gilles Peskine656896e2018-06-29 19:12:28 +02003870 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003871 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003872 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003873
3874 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003875 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003876 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003877 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003878 output, output_size,
3879 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003880 TEST_EQUAL( actual_status, expected_status );
3881 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003882
Gilles Peskine68428122018-06-30 18:42:41 +02003883 /* If the label is empty, the test framework puts a non-null pointer
3884 * in label->x. Test that a null pointer works as well. */
3885 if( label->len == 0 )
3886 {
3887 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003888 if( output_size != 0 )
3889 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003890 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003891 input_data->x, input_data->len,
3892 NULL, label->len,
3893 output, output_size,
3894 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003895 TEST_EQUAL( actual_status, expected_status );
3896 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003897 }
3898
Gilles Peskine656896e2018-06-29 19:12:28 +02003899exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003900 /*
3901 * Key attributes may have been returned by psa_get_key_attributes()
3902 * thus reset them as required.
3903 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003904 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003905
Ronald Cron5425a212020-08-04 14:58:35 +02003906 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003907 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003908 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003909}
3910/* END_CASE */
3911
3912/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003913void asymmetric_encrypt_decrypt( int key_type_arg,
3914 data_t *key_data,
3915 int alg_arg,
3916 data_t *input_data,
3917 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918{
Ronald Cron5425a212020-08-04 14:58:35 +02003919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920 psa_key_type_t key_type = key_type_arg;
3921 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003922 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003923 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003924 size_t output_size;
3925 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003926 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003927 size_t output2_size;
3928 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003930
Gilles Peskine8817f612018-12-18 00:18:46 +01003931 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3934 psa_set_key_algorithm( &attributes, alg );
3935 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003936
Gilles Peskine049c7532019-05-15 20:22:09 +02003937 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003938 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003939
3940 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003941 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003942 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003943
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003944 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003945 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003946 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003947
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003948 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003949 TEST_ASSERT( output2_size <=
3950 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3951 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003952 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003953
Gilles Peskineeebd7382018-06-08 18:11:54 +02003954 /* We test encryption by checking that encrypt-then-decrypt gives back
3955 * the original plaintext because of the non-optional random
3956 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003957 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003958 input_data->x, input_data->len,
3959 label->x, label->len,
3960 output, output_size,
3961 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003962 /* We don't know what ciphertext length to expect, but check that
3963 * it looks sensible. */
3964 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003965
Ronald Cron5425a212020-08-04 14:58:35 +02003966 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 output, output_length,
3968 label->x, label->len,
3969 output2, output2_size,
3970 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003971 ASSERT_COMPARE( input_data->x, input_data->len,
3972 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003973
3974exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003975 /*
3976 * Key attributes may have been returned by psa_get_key_attributes()
3977 * thus reset them as required.
3978 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003979 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003980
Ronald Cron5425a212020-08-04 14:58:35 +02003981 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003982 mbedtls_free( output );
3983 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003984 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985}
3986/* END_CASE */
3987
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003988/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003989void asymmetric_decrypt( int key_type_arg,
3990 data_t *key_data,
3991 int alg_arg,
3992 data_t *input_data,
3993 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003994 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995{
Ronald Cron5425a212020-08-04 14:58:35 +02003996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997 psa_key_type_t key_type = key_type_arg;
3998 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003999 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004001 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004002 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004004
Gilles Peskine8817f612018-12-18 00:18:46 +01004005 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004007 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4008 psa_set_key_algorithm( &attributes, alg );
4009 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004010
Gilles Peskine049c7532019-05-15 20:22:09 +02004011 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004012 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004013
gabor-mezei-armceface22021-01-21 12:26:17 +01004014 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4015 key_bits = psa_get_key_bits( &attributes );
4016
4017 /* Determine the maximum ciphertext length */
4018 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4019 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4020 ASSERT_ALLOC( output, output_size );
4021
Ronald Cron5425a212020-08-04 14:58:35 +02004022 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004023 input_data->x, input_data->len,
4024 label->x, label->len,
4025 output,
4026 output_size,
4027 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004028 ASSERT_COMPARE( expected_data->x, expected_data->len,
4029 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004030
Gilles Peskine68428122018-06-30 18:42:41 +02004031 /* If the label is empty, the test framework puts a non-null pointer
4032 * in label->x. Test that a null pointer works as well. */
4033 if( label->len == 0 )
4034 {
4035 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004036 if( output_size != 0 )
4037 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004038 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004039 input_data->x, input_data->len,
4040 NULL, label->len,
4041 output,
4042 output_size,
4043 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004044 ASSERT_COMPARE( expected_data->x, expected_data->len,
4045 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004046 }
4047
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004048exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004049 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004050 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004051 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004052 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004053}
4054/* END_CASE */
4055
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004056/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004057void asymmetric_decrypt_fail( int key_type_arg,
4058 data_t *key_data,
4059 int alg_arg,
4060 data_t *input_data,
4061 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004062 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004063 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004064{
Ronald Cron5425a212020-08-04 14:58:35 +02004065 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004066 psa_key_type_t key_type = key_type_arg;
4067 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004068 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004069 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004070 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071 psa_status_t actual_status;
4072 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004075 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004076
Gilles Peskine8817f612018-12-18 00:18:46 +01004077 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004078
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4080 psa_set_key_algorithm( &attributes, alg );
4081 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004082
Gilles Peskine049c7532019-05-15 20:22:09 +02004083 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004084 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085
Ronald Cron5425a212020-08-04 14:58:35 +02004086 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004087 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004088 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004089 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004090 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004091 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004092 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004093
Gilles Peskine68428122018-06-30 18:42:41 +02004094 /* If the label is empty, the test framework puts a non-null pointer
4095 * in label->x. Test that a null pointer works as well. */
4096 if( label->len == 0 )
4097 {
4098 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004099 if( output_size != 0 )
4100 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004101 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004102 input_data->x, input_data->len,
4103 NULL, label->len,
4104 output, output_size,
4105 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004106 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004107 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004108 }
4109
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004110exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004111 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004112 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004113 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004114 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004115}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004116/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004117
4118/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004119void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004120{
4121 /* Test each valid way of initializing the object, except for `= {0}`, as
4122 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4123 * though it's OK by the C standard. We could test for this, but we'd need
4124 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004125 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004126 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4127 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4128 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004129
4130 memset( &zero, 0, sizeof( zero ) );
4131
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004132 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004133 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004134 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004135 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004136 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004137 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004138 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004139
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004140 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004141 PSA_ASSERT( psa_key_derivation_abort(&func) );
4142 PSA_ASSERT( psa_key_derivation_abort(&init) );
4143 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004144}
4145/* END_CASE */
4146
Janos Follath16de4a42019-06-13 16:32:24 +01004147/* BEGIN_CASE */
4148void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004149{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004150 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004151 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004152 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004153
Gilles Peskine8817f612018-12-18 00:18:46 +01004154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004155
Janos Follath16de4a42019-06-13 16:32:24 +01004156 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004157 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004158
4159exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004160 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004161 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004162}
4163/* END_CASE */
4164
Janos Follathaf3c2a02019-06-12 12:34:34 +01004165/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004166void derive_set_capacity( int alg_arg, int capacity_arg,
4167 int expected_status_arg )
4168{
4169 psa_algorithm_t alg = alg_arg;
4170 size_t capacity = capacity_arg;
4171 psa_status_t expected_status = expected_status_arg;
4172 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4173
4174 PSA_ASSERT( psa_crypto_init( ) );
4175
4176 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4177
4178 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4179 expected_status );
4180
4181exit:
4182 psa_key_derivation_abort( &operation );
4183 PSA_DONE( );
4184}
4185/* END_CASE */
4186
4187/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004188void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004189 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004190 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004191 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004192 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004193 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004194 int expected_status_arg3,
4195 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004196{
4197 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004198 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4199 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004200 psa_status_t expected_statuses[] = {expected_status_arg1,
4201 expected_status_arg2,
4202 expected_status_arg3};
4203 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004204 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4205 MBEDTLS_SVC_KEY_ID_INIT,
4206 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004207 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4209 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004210 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004211 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004212 psa_status_t expected_output_status = expected_output_status_arg;
4213 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004214
4215 PSA_ASSERT( psa_crypto_init( ) );
4216
4217 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4218 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004219
4220 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4221
4222 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4223 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004224 mbedtls_test_set_step( i );
4225 if( steps[i] == 0 )
4226 {
4227 /* Skip this step */
4228 }
4229 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004230 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004231 psa_set_key_type( &attributes, key_types[i] );
4232 PSA_ASSERT( psa_import_key( &attributes,
4233 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004234 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004235 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4236 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4237 {
4238 // When taking a private key as secret input, use key agreement
4239 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004240 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4241 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004242 expected_statuses[i] );
4243 }
4244 else
4245 {
4246 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004247 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004248 expected_statuses[i] );
4249 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004250 }
4251 else
4252 {
4253 TEST_EQUAL( psa_key_derivation_input_bytes(
4254 &operation, steps[i],
4255 inputs[i]->x, inputs[i]->len ),
4256 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004257 }
4258 }
4259
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004260 if( output_key_type != PSA_KEY_TYPE_NONE )
4261 {
4262 psa_reset_key_attributes( &attributes );
4263 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4264 psa_set_key_bits( &attributes, 8 );
4265 actual_output_status =
4266 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004267 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004268 }
4269 else
4270 {
4271 uint8_t buffer[1];
4272 actual_output_status =
4273 psa_key_derivation_output_bytes( &operation,
4274 buffer, sizeof( buffer ) );
4275 }
4276 TEST_EQUAL( actual_output_status, expected_output_status );
4277
Janos Follathaf3c2a02019-06-12 12:34:34 +01004278exit:
4279 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004280 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4281 psa_destroy_key( keys[i] );
4282 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004283 PSA_DONE( );
4284}
4285/* END_CASE */
4286
Janos Follathd958bb72019-07-03 15:02:16 +01004287/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004288void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004289{
Janos Follathd958bb72019-07-03 15:02:16 +01004290 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004291 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004292 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004293 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004294 unsigned char input1[] = "Input 1";
4295 size_t input1_length = sizeof( input1 );
4296 unsigned char input2[] = "Input 2";
4297 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004298 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004299 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004300 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4301 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4302 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004303 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004304
Gilles Peskine8817f612018-12-18 00:18:46 +01004305 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004306
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004307 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4308 psa_set_key_algorithm( &attributes, alg );
4309 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004310
Gilles Peskine73676cb2019-05-15 20:15:10 +02004311 PSA_ASSERT( psa_import_key( &attributes,
4312 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004313 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004314
4315 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004316 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4317 input1, input1_length,
4318 input2, input2_length,
4319 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004320 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004321
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004322 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004323 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004324 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004325
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004326 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004327
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004328 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004329 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004330
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004331exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004332 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004333 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004334 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004335}
4336/* END_CASE */
4337
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004338/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004339void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004340{
4341 uint8_t output_buffer[16];
4342 size_t buffer_size = 16;
4343 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004344 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004345
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004346 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4347 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004348 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004349
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004350 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004351 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004352
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004354
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004355 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4356 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004357 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004358
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004359 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004360 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004361
4362exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004364}
4365/* END_CASE */
4366
4367/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004368void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004369 int step1_arg, data_t *input1,
4370 int step2_arg, data_t *input2,
4371 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004372 int requested_capacity_arg,
4373 data_t *expected_output1,
4374 data_t *expected_output2 )
4375{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004376 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004377 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4378 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004379 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4380 MBEDTLS_SVC_KEY_ID_INIT,
4381 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004382 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004384 uint8_t *expected_outputs[2] =
4385 {expected_output1->x, expected_output2->x};
4386 size_t output_sizes[2] =
4387 {expected_output1->len, expected_output2->len};
4388 size_t output_buffer_size = 0;
4389 uint8_t *output_buffer = NULL;
4390 size_t expected_capacity;
4391 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004393 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004394 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004395
4396 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4397 {
4398 if( output_sizes[i] > output_buffer_size )
4399 output_buffer_size = output_sizes[i];
4400 if( output_sizes[i] == 0 )
4401 expected_outputs[i] = NULL;
4402 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004403 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004404 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004405
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004406 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4407 psa_set_key_algorithm( &attributes, alg );
4408 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004409
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004410 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004411 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4412 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4413 requested_capacity ) );
4414 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004415 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004416 switch( steps[i] )
4417 {
4418 case 0:
4419 break;
4420 case PSA_KEY_DERIVATION_INPUT_SECRET:
4421 PSA_ASSERT( psa_import_key( &attributes,
4422 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004423 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004424
4425 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4426 {
4427 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4428 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4429 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4430 }
4431
Gilles Peskine1468da72019-05-29 17:35:49 +02004432 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004433 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004434 break;
4435 default:
4436 PSA_ASSERT( psa_key_derivation_input_bytes(
4437 &operation, steps[i],
4438 inputs[i]->x, inputs[i]->len ) );
4439 break;
4440 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004441 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004442
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004444 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004445 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004446 expected_capacity = requested_capacity;
4447
4448 /* Expansion phase. */
4449 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4450 {
4451 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004452 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004453 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004454 if( expected_capacity == 0 && output_sizes[i] == 0 )
4455 {
4456 /* Reading 0 bytes when 0 bytes are available can go either way. */
4457 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004458 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004459 continue;
4460 }
4461 else if( expected_capacity == 0 ||
4462 output_sizes[i] > expected_capacity )
4463 {
4464 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004465 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004466 expected_capacity = 0;
4467 continue;
4468 }
4469 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004470 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004471 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004472 ASSERT_COMPARE( output_buffer, output_sizes[i],
4473 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004474 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004475 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004476 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004477 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004478 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004479 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004481
4482exit:
4483 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004485 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4486 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004487 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004488}
4489/* END_CASE */
4490
4491/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004492void derive_full( int alg_arg,
4493 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004494 data_t *input1,
4495 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004496 int requested_capacity_arg )
4497{
Ronald Cron5425a212020-08-04 14:58:35 +02004498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004499 psa_algorithm_t alg = alg_arg;
4500 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004502 unsigned char output_buffer[16];
4503 size_t expected_capacity = requested_capacity;
4504 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004505 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004506
Gilles Peskine8817f612018-12-18 00:18:46 +01004507 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004508
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004509 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4510 psa_set_key_algorithm( &attributes, alg );
4511 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004512
Gilles Peskine049c7532019-05-15 20:22:09 +02004513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004514 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004515
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004516 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4517 input1->x, input1->len,
4518 input2->x, input2->len,
4519 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004520 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004521
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004522 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004523 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004524 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004525
4526 /* Expansion phase. */
4527 while( current_capacity > 0 )
4528 {
4529 size_t read_size = sizeof( output_buffer );
4530 if( read_size > current_capacity )
4531 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004533 output_buffer,
4534 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004535 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004536 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004537 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004538 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004539 }
4540
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 /* Check that the operation refuses to go over capacity. */
4542 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004543 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004544
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004546
4547exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004549 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004550 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004551}
4552/* END_CASE */
4553
Janos Follathe60c9052019-07-03 13:51:30 +01004554/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004555void derive_key_exercise( int alg_arg,
4556 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004557 data_t *input1,
4558 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004559 int derived_type_arg,
4560 int derived_bits_arg,
4561 int derived_usage_arg,
4562 int derived_alg_arg )
4563{
Ronald Cron5425a212020-08-04 14:58:35 +02004564 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4565 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004566 psa_algorithm_t alg = alg_arg;
4567 psa_key_type_t derived_type = derived_type_arg;
4568 size_t derived_bits = derived_bits_arg;
4569 psa_key_usage_t derived_usage = derived_usage_arg;
4570 psa_algorithm_t derived_alg = derived_alg_arg;
4571 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004572 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004574 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004575
Gilles Peskine8817f612018-12-18 00:18:46 +01004576 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004577
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004578 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4579 psa_set_key_algorithm( &attributes, alg );
4580 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004582 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004583
4584 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004585 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4586 input1->x, input1->len,
4587 input2->x, input2->len,
4588 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004589 goto exit;
4590
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004591 psa_set_key_usage_flags( &attributes, derived_usage );
4592 psa_set_key_algorithm( &attributes, derived_alg );
4593 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004594 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004596 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004597
4598 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004599 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004600 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4601 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004602
4603 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004604 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004605 goto exit;
4606
4607exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004608 /*
4609 * Key attributes may have been returned by psa_get_key_attributes()
4610 * thus reset them as required.
4611 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004612 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004613
4614 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004615 psa_destroy_key( base_key );
4616 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004617 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004618}
4619/* END_CASE */
4620
Janos Follath42fd8882019-07-03 14:17:09 +01004621/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004622void derive_key_export( int alg_arg,
4623 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004624 data_t *input1,
4625 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004626 int bytes1_arg,
4627 int bytes2_arg )
4628{
Ronald Cron5425a212020-08-04 14:58:35 +02004629 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4630 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004631 psa_algorithm_t alg = alg_arg;
4632 size_t bytes1 = bytes1_arg;
4633 size_t bytes2 = bytes2_arg;
4634 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004635 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004636 uint8_t *output_buffer = NULL;
4637 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004638 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4639 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004640 size_t length;
4641
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004642 ASSERT_ALLOC( output_buffer, capacity );
4643 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004645
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004646 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4647 psa_set_key_algorithm( &base_attributes, alg );
4648 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004649 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004650 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004651
4652 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004653 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4654 input1->x, input1->len,
4655 input2->x, input2->len,
4656 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004657 goto exit;
4658
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004660 output_buffer,
4661 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004663
4664 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004665 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4666 input1->x, input1->len,
4667 input2->x, input2->len,
4668 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004669 goto exit;
4670
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004671 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4672 psa_set_key_algorithm( &derived_attributes, 0 );
4673 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004674 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004676 &derived_key ) );
4677 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004678 export_buffer, bytes1,
4679 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004680 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004681 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004682 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004684 &derived_key ) );
4685 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004686 export_buffer + bytes1, bytes2,
4687 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004688 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004689
4690 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004691 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4692 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004693
4694exit:
4695 mbedtls_free( output_buffer );
4696 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004698 psa_destroy_key( base_key );
4699 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004700 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004701}
4702/* END_CASE */
4703
4704/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004705void derive_key( int alg_arg,
4706 data_t *key_data, data_t *input1, data_t *input2,
4707 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004708 int expected_status_arg,
4709 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004710{
Ronald Cron5425a212020-08-04 14:58:35 +02004711 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4712 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004713 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004714 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004715 size_t bits = bits_arg;
4716 psa_status_t expected_status = expected_status_arg;
4717 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4718 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4719 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4720
4721 PSA_ASSERT( psa_crypto_init( ) );
4722
4723 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4724 psa_set_key_algorithm( &base_attributes, alg );
4725 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4726 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004727 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004728
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004729 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4730 input1->x, input1->len,
4731 input2->x, input2->len,
4732 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004733 goto exit;
4734
4735 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4736 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004737 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004738 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004739
4740 psa_status_t status =
4741 psa_key_derivation_output_key( &derived_attributes,
4742 &operation,
4743 &derived_key );
4744 if( is_large_output > 0 )
4745 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4746 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004747
4748exit:
4749 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004750 psa_destroy_key( base_key );
4751 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004752 PSA_DONE( );
4753}
4754/* END_CASE */
4755
4756/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004757void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004758 int our_key_type_arg, int our_key_alg_arg,
4759 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004760 int expected_status_arg )
4761{
Ronald Cron5425a212020-08-04 14:58:35 +02004762 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004763 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004764 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004765 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004768 psa_status_t expected_status = expected_status_arg;
4769 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004770
Gilles Peskine8817f612018-12-18 00:18:46 +01004771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004772
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004773 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004774 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004775 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004776 PSA_ASSERT( psa_import_key( &attributes,
4777 our_key_data->x, our_key_data->len,
4778 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004779
Gilles Peskine77f40d82019-04-11 21:27:06 +02004780 /* The tests currently include inputs that should fail at either step.
4781 * Test cases that fail at the setup step should be changed to call
4782 * key_derivation_setup instead, and this function should be renamed
4783 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004784 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004785 if( status == PSA_SUCCESS )
4786 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004787 TEST_EQUAL( psa_key_derivation_key_agreement(
4788 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4789 our_key,
4790 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004791 expected_status );
4792 }
4793 else
4794 {
4795 TEST_ASSERT( status == expected_status );
4796 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004797
4798exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004800 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004801 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004802}
4803/* END_CASE */
4804
4805/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004806void raw_key_agreement( int alg_arg,
4807 int our_key_type_arg, data_t *our_key_data,
4808 data_t *peer_key_data,
4809 data_t *expected_output )
4810{
Ronald Cron5425a212020-08-04 14:58:35 +02004811 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004812 psa_algorithm_t alg = alg_arg;
4813 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004815 unsigned char *output = NULL;
4816 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004817 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004818
4819 ASSERT_ALLOC( output, expected_output->len );
4820 PSA_ASSERT( psa_crypto_init( ) );
4821
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004822 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4823 psa_set_key_algorithm( &attributes, alg );
4824 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004825 PSA_ASSERT( psa_import_key( &attributes,
4826 our_key_data->x, our_key_data->len,
4827 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004828
gabor-mezei-armceface22021-01-21 12:26:17 +01004829 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4830 key_bits = psa_get_key_bits( &attributes );
4831
Gilles Peskinebe697d82019-05-16 18:00:41 +02004832 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4833 peer_key_data->x, peer_key_data->len,
4834 output, expected_output->len,
4835 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004836 ASSERT_COMPARE( output, output_length,
4837 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004838 TEST_ASSERT( output_length <=
4839 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4840 TEST_ASSERT( output_length <=
4841 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004842
4843exit:
4844 mbedtls_free( output );
4845 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004846 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004847}
4848/* END_CASE */
4849
4850/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004851void key_agreement_capacity( int alg_arg,
4852 int our_key_type_arg, data_t *our_key_data,
4853 data_t *peer_key_data,
4854 int expected_capacity_arg )
4855{
Ronald Cron5425a212020-08-04 14:58:35 +02004856 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004857 psa_algorithm_t alg = alg_arg;
4858 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004859 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004861 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004862 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004863
Gilles Peskine8817f612018-12-18 00:18:46 +01004864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004865
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4867 psa_set_key_algorithm( &attributes, alg );
4868 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004869 PSA_ASSERT( psa_import_key( &attributes,
4870 our_key_data->x, our_key_data->len,
4871 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004872
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004873 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004874 PSA_ASSERT( psa_key_derivation_key_agreement(
4875 &operation,
4876 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4877 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004878 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4879 {
4880 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004882 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004883 NULL, 0 ) );
4884 }
Gilles Peskine59685592018-09-18 12:11:34 +02004885
Gilles Peskinebf491972018-10-25 22:36:12 +02004886 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004887 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004889 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004890
Gilles Peskinebf491972018-10-25 22:36:12 +02004891 /* Test the actual capacity by reading the output. */
4892 while( actual_capacity > sizeof( output ) )
4893 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004895 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004896 actual_capacity -= sizeof( output );
4897 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004899 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004901 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004902
Gilles Peskine59685592018-09-18 12:11:34 +02004903exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004905 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004906 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004907}
4908/* END_CASE */
4909
4910/* BEGIN_CASE */
4911void key_agreement_output( int alg_arg,
4912 int our_key_type_arg, data_t *our_key_data,
4913 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004914 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004915{
Ronald Cron5425a212020-08-04 14:58:35 +02004916 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004917 psa_algorithm_t alg = alg_arg;
4918 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004920 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004921 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004922
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004923 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4924 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004925
Gilles Peskine8817f612018-12-18 00:18:46 +01004926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004927
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004928 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4929 psa_set_key_algorithm( &attributes, alg );
4930 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004931 PSA_ASSERT( psa_import_key( &attributes,
4932 our_key_data->x, our_key_data->len,
4933 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004934
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004936 PSA_ASSERT( psa_key_derivation_key_agreement(
4937 &operation,
4938 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4939 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004940 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4941 {
4942 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004944 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004945 NULL, 0 ) );
4946 }
Gilles Peskine59685592018-09-18 12:11:34 +02004947
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004949 actual_output,
4950 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004951 ASSERT_COMPARE( actual_output, expected_output1->len,
4952 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004953 if( expected_output2->len != 0 )
4954 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004956 actual_output,
4957 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004958 ASSERT_COMPARE( actual_output, expected_output2->len,
4959 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004960 }
Gilles Peskine59685592018-09-18 12:11:34 +02004961
4962exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004964 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004965 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004966 mbedtls_free( actual_output );
4967}
4968/* END_CASE */
4969
4970/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004971void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004972{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004973 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004974 unsigned char *output = NULL;
4975 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004976 size_t i;
4977 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004978
Simon Butcher49f8e312020-03-03 15:51:50 +00004979 TEST_ASSERT( bytes_arg >= 0 );
4980
Gilles Peskine91892022021-02-08 19:50:26 +01004981 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004982 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004983
Gilles Peskine8817f612018-12-18 00:18:46 +01004984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004985
Gilles Peskinea50d7392018-06-21 10:22:13 +02004986 /* Run several times, to ensure that every output byte will be
4987 * nonzero at least once with overwhelming probability
4988 * (2^(-8*number_of_runs)). */
4989 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004990 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004991 if( bytes != 0 )
4992 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004993 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004994
Gilles Peskinea50d7392018-06-21 10:22:13 +02004995 for( i = 0; i < bytes; i++ )
4996 {
4997 if( output[i] != 0 )
4998 ++changed[i];
4999 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005000 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005001
5002 /* Check that every byte was changed to nonzero at least once. This
5003 * validates that psa_generate_random is overwriting every byte of
5004 * the output buffer. */
5005 for( i = 0; i < bytes; i++ )
5006 {
5007 TEST_ASSERT( changed[i] != 0 );
5008 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005009
5010exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005011 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005012 mbedtls_free( output );
5013 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005014}
5015/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005016
5017/* BEGIN_CASE */
5018void generate_key( int type_arg,
5019 int bits_arg,
5020 int usage_arg,
5021 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005022 int expected_status_arg,
5023 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005024{
Ronald Cron5425a212020-08-04 14:58:35 +02005025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005026 psa_key_type_t type = type_arg;
5027 psa_key_usage_t usage = usage_arg;
5028 size_t bits = bits_arg;
5029 psa_algorithm_t alg = alg_arg;
5030 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005032 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005033
Gilles Peskine8817f612018-12-18 00:18:46 +01005034 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005035
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005036 psa_set_key_usage_flags( &attributes, usage );
5037 psa_set_key_algorithm( &attributes, alg );
5038 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005039 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005040
5041 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005042 psa_status_t status = psa_generate_key( &attributes, &key );
5043
5044 if( is_large_key > 0 )
5045 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5046 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005047 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005048 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005049
5050 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005051 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005052 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5053 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005054
Gilles Peskine818ca122018-06-20 18:16:48 +02005055 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005056 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005057 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005058
5059exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005060 /*
5061 * Key attributes may have been returned by psa_get_key_attributes()
5062 * thus reset them as required.
5063 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005064 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005065
Ronald Cron5425a212020-08-04 14:58:35 +02005066 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005067 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005068}
5069/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005070
Ronald Cronee414c72021-03-18 18:50:08 +01005071/* 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 +02005072void generate_key_rsa( int bits_arg,
5073 data_t *e_arg,
5074 int expected_status_arg )
5075{
Ronald Cron5425a212020-08-04 14:58:35 +02005076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005077 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005078 size_t bits = bits_arg;
5079 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5080 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5081 psa_status_t expected_status = expected_status_arg;
5082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5083 uint8_t *exported = NULL;
5084 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005085 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005086 size_t exported_length = SIZE_MAX;
5087 uint8_t *e_read_buffer = NULL;
5088 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005089 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005090 size_t e_read_length = SIZE_MAX;
5091
5092 if( e_arg->len == 0 ||
5093 ( e_arg->len == 3 &&
5094 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5095 {
5096 is_default_public_exponent = 1;
5097 e_read_size = 0;
5098 }
5099 ASSERT_ALLOC( e_read_buffer, e_read_size );
5100 ASSERT_ALLOC( exported, exported_size );
5101
5102 PSA_ASSERT( psa_crypto_init( ) );
5103
5104 psa_set_key_usage_flags( &attributes, usage );
5105 psa_set_key_algorithm( &attributes, alg );
5106 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5107 e_arg->x, e_arg->len ) );
5108 psa_set_key_bits( &attributes, bits );
5109
5110 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005111 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005112 if( expected_status != PSA_SUCCESS )
5113 goto exit;
5114
5115 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005116 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005117 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5118 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5119 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5120 e_read_buffer, e_read_size,
5121 &e_read_length ) );
5122 if( is_default_public_exponent )
5123 TEST_EQUAL( e_read_length, 0 );
5124 else
5125 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5126
5127 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005128 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005129 goto exit;
5130
5131 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005132 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005133 exported, exported_size,
5134 &exported_length ) );
5135 {
5136 uint8_t *p = exported;
5137 uint8_t *end = exported + exported_length;
5138 size_t len;
5139 /* RSAPublicKey ::= SEQUENCE {
5140 * modulus INTEGER, -- n
5141 * publicExponent INTEGER } -- e
5142 */
5143 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005144 MBEDTLS_ASN1_SEQUENCE |
5145 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005146 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005147 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5148 MBEDTLS_ASN1_INTEGER ) );
5149 if( len >= 1 && p[0] == 0 )
5150 {
5151 ++p;
5152 --len;
5153 }
5154 if( e_arg->len == 0 )
5155 {
5156 TEST_EQUAL( len, 3 );
5157 TEST_EQUAL( p[0], 1 );
5158 TEST_EQUAL( p[1], 0 );
5159 TEST_EQUAL( p[2], 1 );
5160 }
5161 else
5162 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5163 }
5164
5165exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005166 /*
5167 * Key attributes may have been returned by psa_get_key_attributes() or
5168 * set by psa_set_key_domain_parameters() thus reset them as required.
5169 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005170 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005171
Ronald Cron5425a212020-08-04 14:58:35 +02005172 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005173 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005174 mbedtls_free( e_read_buffer );
5175 mbedtls_free( exported );
5176}
5177/* END_CASE */
5178
Darryl Greend49a4992018-06-18 17:27:26 +01005179/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005180void persistent_key_load_key_from_storage( data_t *data,
5181 int type_arg, int bits_arg,
5182 int usage_flags_arg, int alg_arg,
5183 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005184{
Ronald Cron71016a92020-08-28 19:01:50 +02005185 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005186 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5188 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005189 psa_key_type_t type = type_arg;
5190 size_t bits = bits_arg;
5191 psa_key_usage_t usage_flags = usage_flags_arg;
5192 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005193 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005194 unsigned char *first_export = NULL;
5195 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005196 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005197 size_t first_exported_length;
5198 size_t second_exported_length;
5199
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005200 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5201 {
5202 ASSERT_ALLOC( first_export, export_size );
5203 ASSERT_ALLOC( second_export, export_size );
5204 }
Darryl Greend49a4992018-06-18 17:27:26 +01005205
Gilles Peskine8817f612018-12-18 00:18:46 +01005206 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005207
Gilles Peskinec87af662019-05-15 16:12:22 +02005208 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005209 psa_set_key_usage_flags( &attributes, usage_flags );
5210 psa_set_key_algorithm( &attributes, alg );
5211 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005212 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005213
Darryl Green0c6575a2018-11-07 16:05:30 +00005214 switch( generation_method )
5215 {
5216 case IMPORT_KEY:
5217 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005218 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005219 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005220 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005221
Darryl Green0c6575a2018-11-07 16:05:30 +00005222 case GENERATE_KEY:
5223 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005224 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005225 break;
5226
5227 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005228#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005229 {
5230 /* Create base key */
5231 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5232 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5233 psa_set_key_usage_flags( &base_attributes,
5234 PSA_KEY_USAGE_DERIVE );
5235 psa_set_key_algorithm( &base_attributes, derive_alg );
5236 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005237 PSA_ASSERT( psa_import_key( &base_attributes,
5238 data->x, data->len,
5239 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005240 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005242 PSA_ASSERT( psa_key_derivation_input_key(
5243 &operation,
5244 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005245 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005246 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005247 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005248 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5249 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005250 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005252 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005253 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005254 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005255#else
5256 TEST_ASSUME( ! "KDF not supported in this configuration" );
5257#endif
5258 break;
5259
5260 default:
5261 TEST_ASSERT( ! "generation_method not implemented in test" );
5262 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005263 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005264 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005265
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005266 /* Export the key if permitted by the key policy. */
5267 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5268 {
Ronald Cron5425a212020-08-04 14:58:35 +02005269 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005270 first_export, export_size,
5271 &first_exported_length ) );
5272 if( generation_method == IMPORT_KEY )
5273 ASSERT_COMPARE( data->x, data->len,
5274 first_export, first_exported_length );
5275 }
Darryl Greend49a4992018-06-18 17:27:26 +01005276
5277 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005278 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005279 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005280 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005281
Darryl Greend49a4992018-06-18 17:27:26 +01005282 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005283 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005284 TEST_ASSERT( mbedtls_svc_key_id_equal(
5285 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005286 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5287 PSA_KEY_LIFETIME_PERSISTENT );
5288 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5289 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5290 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5291 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005292
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005293 /* Export the key again if permitted by the key policy. */
5294 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005295 {
Ronald Cron5425a212020-08-04 14:58:35 +02005296 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005297 second_export, export_size,
5298 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005299 ASSERT_COMPARE( first_export, first_exported_length,
5300 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005301 }
5302
5303 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005304 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005305 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005306
5307exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005308 /*
5309 * Key attributes may have been returned by psa_get_key_attributes()
5310 * thus reset them as required.
5311 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005312 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005313
Darryl Greend49a4992018-06-18 17:27:26 +01005314 mbedtls_free( first_export );
5315 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005317 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005318 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005319 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005320}
5321/* END_CASE */