blob: 82f17c8d994590de6b872ef2129dff5f55a3ac55 [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 );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200386 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200387 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200388 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200389 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200390
Ronald Cron5425a212020-08-04 14:58:35 +0200391 PSA_ASSERT( psa_destroy_key( key ) );
392 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393
394exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100395 /*
396 * Key attributes may have been returned by psa_get_key_attributes()
397 * thus reset them as required.
398 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200399 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100400
401 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200402 PSA_DONE( );
403}
404/* END_CASE */
405
406/* BEGIN_CASE */
407void import_with_data( data_t *data, int type_arg,
408 int attr_bits_arg,
409 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200410{
411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
412 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200414 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200415 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200416 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418
Gilles Peskine8817f612018-12-18 00:18:46 +0100419 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100420
Gilles Peskine4747d192019-04-17 15:05:45 +0200421 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200422 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200423
Ronald Cron5425a212020-08-04 14:58:35 +0200424 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100425 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200426 if( status != PSA_SUCCESS )
427 goto exit;
428
Ronald Cron5425a212020-08-04 14:58:35 +0200429 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200430 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200431 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200432 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200433 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200434
Ronald Cron5425a212020-08-04 14:58:35 +0200435 PSA_ASSERT( psa_destroy_key( key ) );
436 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100437
438exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100439 /*
440 * Key attributes may have been returned by psa_get_key_attributes()
441 * thus reset them as required.
442 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200443 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100444
445 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200446 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447}
448/* END_CASE */
449
450/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200451void import_large_key( int type_arg, int byte_size_arg,
452 int expected_status_arg )
453{
454 psa_key_type_t type = type_arg;
455 size_t byte_size = byte_size_arg;
456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
457 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200459 psa_status_t status;
460 uint8_t *buffer = NULL;
461 size_t buffer_size = byte_size + 1;
462 size_t n;
463
Steven Cooreman69967ce2021-01-18 18:01:08 +0100464 /* Skip the test case if the target running the test cannot
465 * accomodate large keys due to heap size constraints */
466 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200467 memset( buffer, 'K', byte_size );
468
469 PSA_ASSERT( psa_crypto_init( ) );
470
471 /* Try importing the key */
472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
473 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200474 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100475 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 TEST_EQUAL( status, expected_status );
477
478 if( status == PSA_SUCCESS )
479 {
Ronald Cron5425a212020-08-04 14:58:35 +0200480 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200481 TEST_EQUAL( psa_get_key_type( &attributes ), type );
482 TEST_EQUAL( psa_get_key_bits( &attributes ),
483 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200484 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200485 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200486 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200487 for( n = 0; n < byte_size; n++ )
488 TEST_EQUAL( buffer[n], 'K' );
489 for( n = byte_size; n < buffer_size; n++ )
490 TEST_EQUAL( buffer[n], 0 );
491 }
492
493exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100494 /*
495 * Key attributes may have been returned by psa_get_key_attributes()
496 * thus reset them as required.
497 */
498 psa_reset_key_attributes( &attributes );
499
Ronald Cron5425a212020-08-04 14:58:35 +0200500 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200501 PSA_DONE( );
502 mbedtls_free( buffer );
503}
504/* END_CASE */
505
506/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200507void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
508{
Ronald Cron5425a212020-08-04 14:58:35 +0200509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200510 size_t bits = bits_arg;
511 psa_status_t expected_status = expected_status_arg;
512 psa_status_t status;
513 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200514 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200515 size_t buffer_size = /* Slight overapproximations */
516 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200517 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200518 unsigned char *p;
519 int ret;
520 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200522
Gilles Peskine8817f612018-12-18 00:18:46 +0100523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200524 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200525
526 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
527 bits, keypair ) ) >= 0 );
528 length = ret;
529
530 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200531 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200532 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100533 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200534
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200535 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200536 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200537
538exit:
539 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200540 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200541}
542/* END_CASE */
543
544/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300545void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300546 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200547 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 int expected_bits,
549 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200550 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 int canonical_input )
552{
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200555 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200556 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558 unsigned char *exported = NULL;
559 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100561 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200564 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565
Moran Pekercb088e72018-07-17 17:36:59 +0300566 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200567 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200569 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100570 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571
Gilles Peskine4747d192019-04-17 15:05:45 +0200572 psa_set_key_usage_flags( &attributes, usage_arg );
573 psa_set_key_algorithm( &attributes, alg );
574 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700575
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100576 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200577 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578
579 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200580 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200581 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
582 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200583 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584
585 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200586 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100587 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100588
589 /* The exported length must be set by psa_export_key() to a value between 0
590 * and export_size. On errors, the exported length must be 0. */
591 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
592 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
593 TEST_ASSERT( exported_length <= export_size );
594
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200595 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200596 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200598 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100599 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200601 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602
Gilles Peskineea38a922021-02-13 00:05:16 +0100603 /* Run sanity checks on the exported key. For non-canonical inputs,
604 * this validates the canonical representations. For canonical inputs,
605 * this doesn't directly validate the implementation, but it still helps
606 * by cross-validating the test data with the sanity check code. */
607 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200608 goto exit;
609
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200611 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100612 else
613 {
Ronald Cron5425a212020-08-04 14:58:35 +0200614 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200615 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200616 &key2 ) );
617 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100618 reexported,
619 export_size,
620 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200621 ASSERT_COMPARE( exported, exported_length,
622 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200623 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100625 TEST_ASSERT( exported_length <=
626 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
627 psa_get_key_bits( &got_attributes ) ) );
628 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629
630destroy:
631 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200632 PSA_ASSERT( psa_destroy_key( key ) );
633 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634
635exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100636 /*
637 * Key attributes may have been returned by psa_get_key_attributes()
638 * thus reset them as required.
639 */
640 psa_reset_key_attributes( &got_attributes );
641
itayzafrir3e02b3b2018-06-12 17:06:52 +0300642 mbedtls_free( exported );
643 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200644 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645}
646/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100647
Moran Pekerf709f4a2018-06-06 17:26:04 +0300648/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300649void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200650 int type_arg,
651 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100652 int export_size_delta,
653 int expected_export_status_arg,
654 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655{
Ronald Cron5425a212020-08-04 14:58:35 +0200656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300657 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200658 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200659 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300661 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100662 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100663 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665
Gilles Peskine8817f612018-12-18 00:18:46 +0100666 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300667
Gilles Peskine4747d192019-04-17 15:05:45 +0200668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
669 psa_set_key_algorithm( &attributes, alg );
670 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300671
672 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300674
Gilles Peskine49c25912018-10-29 15:15:31 +0100675 /* Export the public key */
676 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200677 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200678 exported, export_size,
679 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100680 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100681 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100682 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200683 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100684 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100687 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100688 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100689 TEST_ASSERT( expected_public_key->len <=
690 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
691 TEST_ASSERT( expected_public_key->len <=
692 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100693 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
694 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100695 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300696
697exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100698 /*
699 * Key attributes may have been returned by psa_get_key_attributes()
700 * thus reset them as required.
701 */
702 psa_reset_key_attributes( &attributes );
703
itayzafrir3e02b3b2018-06-12 17:06:52 +0300704 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200705 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200706 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300707}
708/* END_CASE */
709
Gilles Peskine20035e32018-02-03 22:44:14 +0100710/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200711void import_and_exercise_key( data_t *data,
712 int type_arg,
713 int bits_arg,
714 int alg_arg )
715{
Ronald Cron5425a212020-08-04 14:58:35 +0200716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200717 psa_key_type_t type = type_arg;
718 size_t bits = bits_arg;
719 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100720 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200722 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200723
Gilles Peskine8817f612018-12-18 00:18:46 +0100724 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200725
Gilles Peskine4747d192019-04-17 15:05:45 +0200726 psa_set_key_usage_flags( &attributes, usage );
727 psa_set_key_algorithm( &attributes, alg );
728 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200729
730 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200731 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
733 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200734 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200735 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
736 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200737
738 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100739 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200740 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200741
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_destroy_key( key ) );
743 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200744
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200745exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100746 /*
747 * Key attributes may have been returned by psa_get_key_attributes()
748 * thus reset them as required.
749 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200750 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100751
752 psa_reset_key_attributes( &attributes );
753 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200754 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200755}
756/* END_CASE */
757
758/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100759void effective_key_attributes( int type_arg, int expected_type_arg,
760 int bits_arg, int expected_bits_arg,
761 int usage_arg, int expected_usage_arg,
762 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200763{
Ronald Cron5425a212020-08-04 14:58:35 +0200764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100765 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100766 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100767 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100768 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200769 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100770 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200771 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100772 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200774
Gilles Peskine8817f612018-12-18 00:18:46 +0100775 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200776
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200777 psa_set_key_usage_flags( &attributes, usage );
778 psa_set_key_algorithm( &attributes, alg );
779 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100780 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200781
Ronald Cron5425a212020-08-04 14:58:35 +0200782 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100783 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200784
Ronald Cron5425a212020-08-04 14:58:35 +0200785 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100786 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
787 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
788 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
789 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200790
791exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100792 /*
793 * Key attributes may have been returned by psa_get_key_attributes()
794 * thus reset them as required.
795 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200796 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100797
798 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200799 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200800}
801/* END_CASE */
802
803/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100804void check_key_policy( int type_arg, int bits_arg,
805 int usage_arg, int alg_arg )
806{
807 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200808 usage_arg,
809 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200810 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100811 goto exit;
812}
813/* END_CASE */
814
815/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200816void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000817{
818 /* Test each valid way of initializing the object, except for `= {0}`, as
819 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
820 * though it's OK by the C standard. We could test for this, but we'd need
821 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200822 psa_key_attributes_t func = psa_key_attributes_init( );
823 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
824 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000825
826 memset( &zero, 0, sizeof( zero ) );
827
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200828 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
829 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
830 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000831
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200832 TEST_EQUAL( psa_get_key_type( &func ), 0 );
833 TEST_EQUAL( psa_get_key_type( &init ), 0 );
834 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
835
836 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
837 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
838 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
839
840 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
841 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
842 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
843
844 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
845 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
846 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000847}
848/* END_CASE */
849
850/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200851void mac_key_policy( int policy_usage_arg,
852 int policy_alg_arg,
853 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200854 data_t *key_data,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200855 int exercise_alg_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100856 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200857{
Ronald Cron5425a212020-08-04 14:58:35 +0200858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000860 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200861 psa_key_type_t key_type = key_type_arg;
862 psa_algorithm_t policy_alg = policy_alg_arg;
863 psa_algorithm_t exercise_alg = exercise_alg_arg;
864 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200865 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100866 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200867 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200868
Gilles Peskine8817f612018-12-18 00:18:46 +0100869 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200870
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200871 psa_set_key_usage_flags( &attributes, policy_usage );
872 psa_set_key_algorithm( &attributes, policy_alg );
873 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200874
Gilles Peskine049c7532019-05-15 20:22:09 +0200875 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200876 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200877
gabor-mezei-arm659af9e2021-06-29 11:06:16 +0200878 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
879 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200880
Ronald Cron5425a212020-08-04 14:58:35 +0200881 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100882 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100883 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100884 else
885 TEST_EQUAL( status, expected_status );
886
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200888
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200889 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200890 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100891 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100892 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100893 else
894 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200895
896exit:
897 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200898 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200899 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200900}
901/* END_CASE */
902
903/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200904void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 int policy_alg,
906 int key_type,
907 data_t *key_data,
908 int exercise_alg )
909{
Ronald Cron5425a212020-08-04 14:58:35 +0200910 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000912 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200913 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200914 psa_status_t status;
915
Gilles Peskine8817f612018-12-18 00:18:46 +0100916 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200917
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200918 psa_set_key_usage_flags( &attributes, policy_usage );
919 psa_set_key_algorithm( &attributes, policy_alg );
920 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921
Gilles Peskine049c7532019-05-15 20:22:09 +0200922 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200923 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200924
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200925 /* Check if no key usage flag implication is done */
926 TEST_EQUAL( policy_usage,
927 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200928
Ronald Cron5425a212020-08-04 14:58:35 +0200929 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930 if( policy_alg == exercise_alg &&
931 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100932 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200933 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100934 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935 psa_cipher_abort( &operation );
936
Ronald Cron5425a212020-08-04 14:58:35 +0200937 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200938 if( policy_alg == exercise_alg &&
939 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100940 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200941 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100942 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943
944exit:
945 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200946 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200947 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948}
949/* END_CASE */
950
951/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200952void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200953 int policy_alg,
954 int key_type,
955 data_t *key_data,
956 int nonce_length_arg,
957 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100958 int exercise_alg,
959 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960{
Ronald Cron5425a212020-08-04 14:58:35 +0200961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200963 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100965 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 unsigned char nonce[16] = {0};
967 size_t nonce_length = nonce_length_arg;
968 unsigned char tag[16];
969 size_t tag_length = tag_length_arg;
970 size_t output_length;
971
972 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
973 TEST_ASSERT( tag_length <= sizeof( tag ) );
974
Gilles Peskine8817f612018-12-18 00:18:46 +0100975 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200977 psa_set_key_usage_flags( &attributes, policy_usage );
978 psa_set_key_algorithm( &attributes, policy_alg );
979 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200980
Gilles Peskine049c7532019-05-15 20:22:09 +0200981 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200982 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200983
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200984 /* Check if no key usage implication is done */
985 TEST_EQUAL( policy_usage,
986 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200987
Ronald Cron5425a212020-08-04 14:58:35 +0200988 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989 nonce, nonce_length,
990 NULL, 0,
991 NULL, 0,
992 tag, tag_length,
993 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100994 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
995 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200996 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100997 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200998
999 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001000 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001 nonce, nonce_length,
1002 NULL, 0,
1003 tag, tag_length,
1004 NULL, 0,
1005 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001006 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1007 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1008 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001009 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001011 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012
1013exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001014 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001015 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016}
1017/* END_CASE */
1018
1019/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001020void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001021 int policy_alg,
1022 int key_type,
1023 data_t *key_data,
1024 int exercise_alg )
1025{
Ronald Cron5425a212020-08-04 14:58:35 +02001026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001028 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001029 psa_status_t status;
1030 size_t key_bits;
1031 size_t buffer_length;
1032 unsigned char *buffer = NULL;
1033 size_t output_length;
1034
Gilles Peskine8817f612018-12-18 00:18:46 +01001035 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001036
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001037 psa_set_key_usage_flags( &attributes, policy_usage );
1038 psa_set_key_algorithm( &attributes, policy_alg );
1039 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001040
Gilles Peskine049c7532019-05-15 20:22:09 +02001041 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001042 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001044 /* Check if no key usage implication is done */
1045 TEST_EQUAL( policy_usage,
1046 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001047
Ronald Cron5425a212020-08-04 14:58:35 +02001048 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001049 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001050 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1051 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001052 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001053
Ronald Cron5425a212020-08-04 14:58:35 +02001054 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001055 NULL, 0,
1056 NULL, 0,
1057 buffer, buffer_length,
1058 &output_length );
1059 if( policy_alg == exercise_alg &&
1060 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001061 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001062 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001063 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001065 if( buffer_length != 0 )
1066 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001067 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068 buffer, buffer_length,
1069 NULL, 0,
1070 buffer, buffer_length,
1071 &output_length );
1072 if( policy_alg == exercise_alg &&
1073 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001074 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001075 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001076 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
1078exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001079 /*
1080 * Key attributes may have been returned by psa_get_key_attributes()
1081 * thus reset them as required.
1082 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001083 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001084
1085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001086 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087 mbedtls_free( buffer );
1088}
1089/* END_CASE */
1090
1091/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001092void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001093 int policy_alg,
1094 int key_type,
1095 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001096 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001097 int payload_length_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001098 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099{
Ronald Cron5425a212020-08-04 14:58:35 +02001100 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001102 psa_key_usage_t policy_usage = policy_usage_arg;
1103 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001105 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1106 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1107 * compatible with the policy and `payload_length_arg` is supposed to be
1108 * a valid input length to sign. If `payload_length_arg <= 0`,
1109 * `exercise_alg` is supposed to be forbidden by the policy. */
1110 int compatible_alg = payload_length_arg > 0;
1111 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001112 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001113 size_t signature_length;
1114
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001115 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001116 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001117 TEST_EQUAL( expected_usage,
1118 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001119
Gilles Peskine8817f612018-12-18 00:18:46 +01001120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001122 psa_set_key_usage_flags( &attributes, policy_usage );
1123 psa_set_key_algorithm( &attributes, policy_alg );
1124 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001125
Gilles Peskine049c7532019-05-15 20:22:09 +02001126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001127 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001128
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001129 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1130
Ronald Cron5425a212020-08-04 14:58:35 +02001131 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001132 payload, payload_length,
1133 signature, sizeof( signature ),
1134 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001135 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001136 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001137 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001138 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139
1140 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001141 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001142 payload, payload_length,
1143 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001144 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001145 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001146 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001147 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001148
gabor-mezei-arm79df41d2021-06-28 14:53:49 +02001149 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1150 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001151 {
1152 status = psa_sign_message( key, exercise_alg,
1153 payload, payload_length,
1154 signature, sizeof( signature ),
1155 &signature_length );
1156 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1157 PSA_ASSERT( status );
1158 else
1159 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1160
1161 memset( signature, 0, sizeof( signature ) );
1162 status = psa_verify_message( key, exercise_alg,
1163 payload, payload_length,
1164 signature, sizeof( signature ) );
1165 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1166 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1167 else
1168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1169 }
1170
Gilles Peskined5b33222018-06-18 22:20:03 +02001171exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001172 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001173 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001174}
1175/* END_CASE */
1176
Janos Follathba3fab92019-06-11 14:50:16 +01001177/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001178void derive_key_policy( int policy_usage,
1179 int policy_alg,
1180 int key_type,
1181 data_t *key_data,
1182 int exercise_alg )
1183{
Ronald Cron5425a212020-08-04 14:58:35 +02001184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001186 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001187 psa_status_t status;
1188
Gilles Peskine8817f612018-12-18 00:18:46 +01001189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001190
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001191 psa_set_key_usage_flags( &attributes, policy_usage );
1192 psa_set_key_algorithm( &attributes, policy_alg );
1193 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001194
Gilles Peskine049c7532019-05-15 20:22:09 +02001195 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001196 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001197
Janos Follathba3fab92019-06-11 14:50:16 +01001198 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1199
1200 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1201 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001202 {
Janos Follathba3fab92019-06-11 14:50:16 +01001203 PSA_ASSERT( psa_key_derivation_input_bytes(
1204 &operation,
1205 PSA_KEY_DERIVATION_INPUT_SEED,
1206 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001207 }
Janos Follathba3fab92019-06-11 14:50:16 +01001208
1209 status = psa_key_derivation_input_key( &operation,
1210 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001211 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001212
Gilles Peskineea0fb492018-07-12 17:17:20 +02001213 if( policy_alg == exercise_alg &&
1214 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001215 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001216 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001217 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001218
1219exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001220 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001221 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001222 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001223}
1224/* END_CASE */
1225
1226/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001227void agreement_key_policy( int policy_usage,
1228 int policy_alg,
1229 int key_type_arg,
1230 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001231 int exercise_alg,
1232 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001233{
Ronald Cron5425a212020-08-04 14:58:35 +02001234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001236 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001237 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001238 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001239 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001240
Gilles Peskine8817f612018-12-18 00:18:46 +01001241 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001242
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001243 psa_set_key_usage_flags( &attributes, policy_usage );
1244 psa_set_key_algorithm( &attributes, policy_alg );
1245 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001246
Gilles Peskine049c7532019-05-15 20:22:09 +02001247 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001248 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001249
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001250 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001251 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001252
Steven Cooremance48e852020-10-05 16:02:45 +02001253 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001254
1255exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001256 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001257 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001258 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001259}
1260/* END_CASE */
1261
1262/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001263void key_policy_alg2( int key_type_arg, data_t *key_data,
1264 int usage_arg, int alg_arg, int alg2_arg )
1265{
Ronald Cron5425a212020-08-04 14:58:35 +02001266 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001267 psa_key_type_t key_type = key_type_arg;
1268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1269 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1270 psa_key_usage_t usage = usage_arg;
1271 psa_algorithm_t alg = alg_arg;
1272 psa_algorithm_t alg2 = alg2_arg;
1273
1274 PSA_ASSERT( psa_crypto_init( ) );
1275
1276 psa_set_key_usage_flags( &attributes, usage );
1277 psa_set_key_algorithm( &attributes, alg );
1278 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1279 psa_set_key_type( &attributes, key_type );
1280 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001281 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001282
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001283 /* Update the usage flags to obtain implicit usage flags */
1284 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001285 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001286 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1287 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1288 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1289
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001290 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001291 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001292 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001293 goto exit;
1294
1295exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001296 /*
1297 * Key attributes may have been returned by psa_get_key_attributes()
1298 * thus reset them as required.
1299 */
1300 psa_reset_key_attributes( &got_attributes );
1301
Ronald Cron5425a212020-08-04 14:58:35 +02001302 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001303 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001304}
1305/* END_CASE */
1306
1307/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001308void raw_agreement_key_policy( int policy_usage,
1309 int policy_alg,
1310 int key_type_arg,
1311 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001312 int exercise_alg,
1313 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001314{
Ronald Cron5425a212020-08-04 14:58:35 +02001315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001317 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001318 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001319 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001320 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001321
1322 PSA_ASSERT( psa_crypto_init( ) );
1323
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001324 psa_set_key_usage_flags( &attributes, policy_usage );
1325 psa_set_key_algorithm( &attributes, policy_alg );
1326 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001327
Gilles Peskine049c7532019-05-15 20:22:09 +02001328 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001329 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001330
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001331 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001332
Steven Cooremance48e852020-10-05 16:02:45 +02001333 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001334
1335exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001336 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001337 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001338 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001339}
1340/* END_CASE */
1341
1342/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001343void copy_success( int source_usage_arg,
1344 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001345 int type_arg, data_t *material,
1346 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001347 int target_usage_arg,
1348 int target_alg_arg, int target_alg2_arg,
1349 int expected_usage_arg,
1350 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001351{
Gilles Peskineca25db92019-04-19 11:43:08 +02001352 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1353 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001354 psa_key_usage_t expected_usage = expected_usage_arg;
1355 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001356 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001357 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1358 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001359 uint8_t *export_buffer = NULL;
1360
Gilles Peskine57ab7212019-01-28 13:03:09 +01001361 PSA_ASSERT( psa_crypto_init( ) );
1362
Gilles Peskineca25db92019-04-19 11:43:08 +02001363 /* Prepare the source key. */
1364 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1365 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001366 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001367 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001368 PSA_ASSERT( psa_import_key( &source_attributes,
1369 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001370 &source_key ) );
1371 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001372
Gilles Peskineca25db92019-04-19 11:43:08 +02001373 /* Prepare the target attributes. */
1374 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001375 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001376 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001377 /* Set volatile lifetime to reset the key identifier to 0. */
1378 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1379 }
1380
Gilles Peskineca25db92019-04-19 11:43:08 +02001381 if( target_usage_arg != -1 )
1382 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1383 if( target_alg_arg != -1 )
1384 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001385 if( target_alg2_arg != -1 )
1386 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001387
1388 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001389 PSA_ASSERT( psa_copy_key( source_key,
1390 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001391
1392 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001393 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001394
1395 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001396 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001397 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1398 psa_get_key_type( &target_attributes ) );
1399 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1400 psa_get_key_bits( &target_attributes ) );
1401 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1402 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001403 TEST_EQUAL( expected_alg2,
1404 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001405 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1406 {
1407 size_t length;
1408 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001409 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001410 material->len, &length ) );
1411 ASSERT_COMPARE( material->x, material->len,
1412 export_buffer, length );
1413 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001414
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001415 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001416 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001417 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001418 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001419
Ronald Cron5425a212020-08-04 14:58:35 +02001420 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001421
1422exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001423 /*
1424 * Source and target key attributes may have been returned by
1425 * psa_get_key_attributes() thus reset them as required.
1426 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001427 psa_reset_key_attributes( &source_attributes );
1428 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001429
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001430 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001431 mbedtls_free( export_buffer );
1432}
1433/* END_CASE */
1434
1435/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001436void copy_fail( int source_usage_arg,
1437 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001438 int type_arg, data_t *material,
1439 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001440 int target_usage_arg,
1441 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001442 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001443 int expected_status_arg )
1444{
1445 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1446 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001447 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1448 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001449 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001450
1451 PSA_ASSERT( psa_crypto_init( ) );
1452
1453 /* Prepare the source key. */
1454 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1455 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001456 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001457 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001458 PSA_ASSERT( psa_import_key( &source_attributes,
1459 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001460 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001461
1462 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001463 psa_set_key_id( &target_attributes, key_id );
1464 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001465 psa_set_key_type( &target_attributes, target_type_arg );
1466 psa_set_key_bits( &target_attributes, target_bits_arg );
1467 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1468 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001469 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001470
1471 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001472 TEST_EQUAL( psa_copy_key( source_key,
1473 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001474 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001475
Ronald Cron5425a212020-08-04 14:58:35 +02001476 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001477
Gilles Peskine4a644642019-05-03 17:14:08 +02001478exit:
1479 psa_reset_key_attributes( &source_attributes );
1480 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001481 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001482}
1483/* END_CASE */
1484
1485/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001486void hash_operation_init( )
1487{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001488 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001489 /* Test each valid way of initializing the object, except for `= {0}`, as
1490 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1491 * though it's OK by the C standard. We could test for this, but we'd need
1492 * to supress the Clang warning for the test. */
1493 psa_hash_operation_t func = psa_hash_operation_init( );
1494 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1495 psa_hash_operation_t zero;
1496
1497 memset( &zero, 0, sizeof( zero ) );
1498
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001499 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001500 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1501 PSA_ERROR_BAD_STATE );
1502 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1503 PSA_ERROR_BAD_STATE );
1504 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1505 PSA_ERROR_BAD_STATE );
1506
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001507 /* A default hash operation should be abortable without error. */
1508 PSA_ASSERT( psa_hash_abort( &func ) );
1509 PSA_ASSERT( psa_hash_abort( &init ) );
1510 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001511}
1512/* END_CASE */
1513
1514/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001515void hash_setup( int alg_arg,
1516 int expected_status_arg )
1517{
1518 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001519 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001520 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001521 psa_status_t status;
1522
Gilles Peskine8817f612018-12-18 00:18:46 +01001523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001524
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001525 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001526 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001527
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001528 /* Whether setup succeeded or failed, abort must succeed. */
1529 PSA_ASSERT( psa_hash_abort( &operation ) );
1530
1531 /* If setup failed, reproduce the failure, so as to
1532 * test the resulting state of the operation object. */
1533 if( status != PSA_SUCCESS )
1534 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1535
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001536 /* Now the operation object should be reusable. */
1537#if defined(KNOWN_SUPPORTED_HASH_ALG)
1538 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1539 PSA_ASSERT( psa_hash_abort( &operation ) );
1540#endif
1541
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001542exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001543 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001544}
1545/* END_CASE */
1546
1547/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001548void hash_compute_fail( int alg_arg, data_t *input,
1549 int output_size_arg, int expected_status_arg )
1550{
1551 psa_algorithm_t alg = alg_arg;
1552 uint8_t *output = NULL;
1553 size_t output_size = output_size_arg;
1554 size_t output_length = INVALID_EXPORT_LENGTH;
1555 psa_status_t expected_status = expected_status_arg;
1556 psa_status_t status;
1557
1558 ASSERT_ALLOC( output, output_size );
1559
1560 PSA_ASSERT( psa_crypto_init( ) );
1561
1562 status = psa_hash_compute( alg, input->x, input->len,
1563 output, output_size, &output_length );
1564 TEST_EQUAL( status, expected_status );
1565 TEST_ASSERT( output_length <= output_size );
1566
1567exit:
1568 mbedtls_free( output );
1569 PSA_DONE( );
1570}
1571/* END_CASE */
1572
1573/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001574void hash_compare_fail( int alg_arg, data_t *input,
1575 data_t *reference_hash,
1576 int expected_status_arg )
1577{
1578 psa_algorithm_t alg = alg_arg;
1579 psa_status_t expected_status = expected_status_arg;
1580 psa_status_t status;
1581
1582 PSA_ASSERT( psa_crypto_init( ) );
1583
1584 status = psa_hash_compare( alg, input->x, input->len,
1585 reference_hash->x, reference_hash->len );
1586 TEST_EQUAL( status, expected_status );
1587
1588exit:
1589 PSA_DONE( );
1590}
1591/* END_CASE */
1592
1593/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001594void hash_compute_compare( int alg_arg, data_t *input,
1595 data_t *expected_output )
1596{
1597 psa_algorithm_t alg = alg_arg;
1598 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1599 size_t output_length = INVALID_EXPORT_LENGTH;
1600 size_t i;
1601
1602 PSA_ASSERT( psa_crypto_init( ) );
1603
1604 /* Compute with tight buffer */
1605 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001606 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001607 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001608 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001609 ASSERT_COMPARE( output, output_length,
1610 expected_output->x, expected_output->len );
1611
1612 /* Compute with larger buffer */
1613 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1614 output, sizeof( output ),
1615 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001616 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001617 ASSERT_COMPARE( output, output_length,
1618 expected_output->x, expected_output->len );
1619
1620 /* Compare with correct hash */
1621 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1622 output, output_length ) );
1623
1624 /* Compare with trailing garbage */
1625 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1626 output, output_length + 1 ),
1627 PSA_ERROR_INVALID_SIGNATURE );
1628
1629 /* Compare with truncated hash */
1630 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1631 output, output_length - 1 ),
1632 PSA_ERROR_INVALID_SIGNATURE );
1633
1634 /* Compare with corrupted value */
1635 for( i = 0; i < output_length; i++ )
1636 {
Chris Jones9634bb12021-01-20 15:56:42 +00001637 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001638 output[i] ^= 1;
1639 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1640 output, output_length ),
1641 PSA_ERROR_INVALID_SIGNATURE );
1642 output[i] ^= 1;
1643 }
1644
1645exit:
1646 PSA_DONE( );
1647}
1648/* END_CASE */
1649
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001650/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001651void hash_bad_order( )
1652{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001653 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001654 unsigned char input[] = "";
1655 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001656 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001657 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1658 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1659 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001660 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001661 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001662 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001663
Gilles Peskine8817f612018-12-18 00:18:46 +01001664 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001665
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001666 /* Call setup twice in a row. */
1667 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001668 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001669 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1670 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001671 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001672 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001673 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001674
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001675 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001676 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001677 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001678 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001679
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001680 /* Check that update calls abort on error. */
1681 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman54f73512021-06-24 18:14:52 +01001682 operation.id = UINT_MAX;
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001683 ASSERT_OPERATION_IS_ACTIVE( operation );
1684 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1685 PSA_ERROR_BAD_STATE );
1686 ASSERT_OPERATION_IS_INACTIVE( operation );
1687 PSA_ASSERT( psa_hash_abort( &operation ) );
1688 ASSERT_OPERATION_IS_INACTIVE( operation );
1689
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001690 /* Call update after finish. */
1691 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1692 PSA_ASSERT( psa_hash_finish( &operation,
1693 hash, sizeof( hash ), &hash_len ) );
1694 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001695 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001696 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001697
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001698 /* Call verify without calling setup beforehand. */
1699 TEST_EQUAL( psa_hash_verify( &operation,
1700 valid_hash, sizeof( valid_hash ) ),
1701 PSA_ERROR_BAD_STATE );
1702 PSA_ASSERT( psa_hash_abort( &operation ) );
1703
1704 /* Call verify after finish. */
1705 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1706 PSA_ASSERT( psa_hash_finish( &operation,
1707 hash, sizeof( hash ), &hash_len ) );
1708 TEST_EQUAL( psa_hash_verify( &operation,
1709 valid_hash, sizeof( valid_hash ) ),
1710 PSA_ERROR_BAD_STATE );
1711 PSA_ASSERT( psa_hash_abort( &operation ) );
1712
1713 /* Call verify twice in a row. */
1714 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001715 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001716 PSA_ASSERT( psa_hash_verify( &operation,
1717 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001718 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001719 TEST_EQUAL( psa_hash_verify( &operation,
1720 valid_hash, sizeof( valid_hash ) ),
1721 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001722 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001723 PSA_ASSERT( psa_hash_abort( &operation ) );
1724
1725 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001726 TEST_EQUAL( psa_hash_finish( &operation,
1727 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001728 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001729 PSA_ASSERT( psa_hash_abort( &operation ) );
1730
1731 /* Call finish twice in a row. */
1732 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1733 PSA_ASSERT( psa_hash_finish( &operation,
1734 hash, sizeof( hash ), &hash_len ) );
1735 TEST_EQUAL( psa_hash_finish( &operation,
1736 hash, sizeof( hash ), &hash_len ),
1737 PSA_ERROR_BAD_STATE );
1738 PSA_ASSERT( psa_hash_abort( &operation ) );
1739
1740 /* Call finish after calling verify. */
1741 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1742 PSA_ASSERT( psa_hash_verify( &operation,
1743 valid_hash, sizeof( valid_hash ) ) );
1744 TEST_EQUAL( psa_hash_finish( &operation,
1745 hash, sizeof( hash ), &hash_len ),
1746 PSA_ERROR_BAD_STATE );
1747 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001748
1749exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001750 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001751}
1752/* END_CASE */
1753
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001754/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001755void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001756{
1757 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001758 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1759 * appended to it */
1760 unsigned char hash[] = {
1761 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1762 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1763 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001764 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001765 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001766
Gilles Peskine8817f612018-12-18 00:18:46 +01001767 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001768
itayzafrir27e69452018-11-01 14:26:34 +02001769 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001771 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001772 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001773 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001774 ASSERT_OPERATION_IS_INACTIVE( operation );
1775 PSA_ASSERT( psa_hash_abort( &operation ) );
1776 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001777
itayzafrir27e69452018-11-01 14:26:34 +02001778 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001780 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001781 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001782
itayzafrir27e69452018-11-01 14:26:34 +02001783 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001784 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001785 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001786 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001787
itayzafrirec93d302018-10-18 18:01:10 +03001788exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001789 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001790}
1791/* END_CASE */
1792
Ronald Cronee414c72021-03-18 18:50:08 +01001793/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001794void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001795{
1796 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001797 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001798 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001799 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001800 size_t hash_len;
1801
Gilles Peskine8817f612018-12-18 00:18:46 +01001802 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001803
itayzafrir58028322018-10-25 10:22:01 +03001804 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001805 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001806 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001807 hash, expected_size - 1, &hash_len ),
1808 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001809
1810exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001811 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001812}
1813/* END_CASE */
1814
Ronald Cronee414c72021-03-18 18:50:08 +01001815/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001816void hash_clone_source_state( )
1817{
1818 psa_algorithm_t alg = PSA_ALG_SHA_256;
1819 unsigned char hash[PSA_HASH_MAX_SIZE];
1820 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1821 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1822 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1823 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1824 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1825 size_t hash_len;
1826
1827 PSA_ASSERT( psa_crypto_init( ) );
1828 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1829
1830 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1831 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1832 PSA_ASSERT( psa_hash_finish( &op_finished,
1833 hash, sizeof( hash ), &hash_len ) );
1834 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1835 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1836
1837 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1838 PSA_ERROR_BAD_STATE );
1839
1840 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1841 PSA_ASSERT( psa_hash_finish( &op_init,
1842 hash, sizeof( hash ), &hash_len ) );
1843 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1844 PSA_ASSERT( psa_hash_finish( &op_finished,
1845 hash, sizeof( hash ), &hash_len ) );
1846 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1847 PSA_ASSERT( psa_hash_finish( &op_aborted,
1848 hash, sizeof( hash ), &hash_len ) );
1849
1850exit:
1851 psa_hash_abort( &op_source );
1852 psa_hash_abort( &op_init );
1853 psa_hash_abort( &op_setup );
1854 psa_hash_abort( &op_finished );
1855 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001856 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001857}
1858/* END_CASE */
1859
Ronald Cronee414c72021-03-18 18:50:08 +01001860/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001861void hash_clone_target_state( )
1862{
1863 psa_algorithm_t alg = PSA_ALG_SHA_256;
1864 unsigned char hash[PSA_HASH_MAX_SIZE];
1865 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1866 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1867 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1868 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1869 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1870 size_t hash_len;
1871
1872 PSA_ASSERT( psa_crypto_init( ) );
1873
1874 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1875 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1876 PSA_ASSERT( psa_hash_finish( &op_finished,
1877 hash, sizeof( hash ), &hash_len ) );
1878 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1879 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1880
1881 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1882 PSA_ASSERT( psa_hash_finish( &op_target,
1883 hash, sizeof( hash ), &hash_len ) );
1884
1885 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1886 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1887 PSA_ERROR_BAD_STATE );
1888 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1889 PSA_ERROR_BAD_STATE );
1890
1891exit:
1892 psa_hash_abort( &op_target );
1893 psa_hash_abort( &op_init );
1894 psa_hash_abort( &op_setup );
1895 psa_hash_abort( &op_finished );
1896 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001897 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001898}
1899/* END_CASE */
1900
itayzafrir58028322018-10-25 10:22:01 +03001901/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001902void mac_operation_init( )
1903{
Jaeden Amero252ef282019-02-15 14:05:35 +00001904 const uint8_t input[1] = { 0 };
1905
Jaeden Amero769ce272019-01-04 11:48:03 +00001906 /* Test each valid way of initializing the object, except for `= {0}`, as
1907 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1908 * though it's OK by the C standard. We could test for this, but we'd need
1909 * to supress the Clang warning for the test. */
1910 psa_mac_operation_t func = psa_mac_operation_init( );
1911 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1912 psa_mac_operation_t zero;
1913
1914 memset( &zero, 0, sizeof( zero ) );
1915
Jaeden Amero252ef282019-02-15 14:05:35 +00001916 /* A freshly-initialized MAC operation should not be usable. */
1917 TEST_EQUAL( psa_mac_update( &func,
1918 input, sizeof( input ) ),
1919 PSA_ERROR_BAD_STATE );
1920 TEST_EQUAL( psa_mac_update( &init,
1921 input, sizeof( input ) ),
1922 PSA_ERROR_BAD_STATE );
1923 TEST_EQUAL( psa_mac_update( &zero,
1924 input, sizeof( input ) ),
1925 PSA_ERROR_BAD_STATE );
1926
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001927 /* A default MAC operation should be abortable without error. */
1928 PSA_ASSERT( psa_mac_abort( &func ) );
1929 PSA_ASSERT( psa_mac_abort( &init ) );
1930 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001931}
1932/* END_CASE */
1933
1934/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001935void mac_setup( int key_type_arg,
1936 data_t *key,
1937 int alg_arg,
1938 int expected_status_arg )
1939{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001940 psa_key_type_t key_type = key_type_arg;
1941 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001942 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001943 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001944 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1945#if defined(KNOWN_SUPPORTED_MAC_ALG)
1946 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1947#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001950
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001951 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1952 &operation, &status ) )
1953 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001954 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001955
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001956 /* The operation object should be reusable. */
1957#if defined(KNOWN_SUPPORTED_MAC_ALG)
1958 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1959 smoke_test_key_data,
1960 sizeof( smoke_test_key_data ),
1961 KNOWN_SUPPORTED_MAC_ALG,
1962 &operation, &status ) )
1963 goto exit;
1964 TEST_EQUAL( status, PSA_SUCCESS );
1965#endif
1966
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001967exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001968 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001969}
1970/* END_CASE */
1971
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001972/* 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 +00001973void mac_bad_order( )
1974{
Ronald Cron5425a212020-08-04 14:58:35 +02001975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001976 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1977 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001978 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001979 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1980 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1981 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001983 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1984 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1985 size_t sign_mac_length = 0;
1986 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1987 const uint8_t verify_mac[] = {
1988 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1989 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1990 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1991
1992 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001993 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001994 psa_set_key_algorithm( &attributes, alg );
1995 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001996
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1998 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001999
Jaeden Amero252ef282019-02-15 14:05:35 +00002000 /* Call update without calling setup beforehand. */
2001 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2002 PSA_ERROR_BAD_STATE );
2003 PSA_ASSERT( psa_mac_abort( &operation ) );
2004
2005 /* Call sign finish without calling setup beforehand. */
2006 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2007 &sign_mac_length),
2008 PSA_ERROR_BAD_STATE );
2009 PSA_ASSERT( psa_mac_abort( &operation ) );
2010
2011 /* Call verify finish without calling setup beforehand. */
2012 TEST_EQUAL( psa_mac_verify_finish( &operation,
2013 verify_mac, sizeof( verify_mac ) ),
2014 PSA_ERROR_BAD_STATE );
2015 PSA_ASSERT( psa_mac_abort( &operation ) );
2016
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002017 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002018 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002019 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002020 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002021 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002022 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002023 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002024 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002025
Jaeden Amero252ef282019-02-15 14:05:35 +00002026 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002027 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002028 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2029 PSA_ASSERT( psa_mac_sign_finish( &operation,
2030 sign_mac, sizeof( sign_mac ),
2031 &sign_mac_length ) );
2032 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2033 PSA_ERROR_BAD_STATE );
2034 PSA_ASSERT( psa_mac_abort( &operation ) );
2035
2036 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002037 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002038 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2039 PSA_ASSERT( psa_mac_verify_finish( &operation,
2040 verify_mac, sizeof( verify_mac ) ) );
2041 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2042 PSA_ERROR_BAD_STATE );
2043 PSA_ASSERT( psa_mac_abort( &operation ) );
2044
2045 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002046 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002047 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2048 PSA_ASSERT( psa_mac_sign_finish( &operation,
2049 sign_mac, sizeof( sign_mac ),
2050 &sign_mac_length ) );
2051 TEST_EQUAL( psa_mac_sign_finish( &operation,
2052 sign_mac, sizeof( sign_mac ),
2053 &sign_mac_length ),
2054 PSA_ERROR_BAD_STATE );
2055 PSA_ASSERT( psa_mac_abort( &operation ) );
2056
2057 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002058 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002059 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2060 PSA_ASSERT( psa_mac_verify_finish( &operation,
2061 verify_mac, sizeof( verify_mac ) ) );
2062 TEST_EQUAL( psa_mac_verify_finish( &operation,
2063 verify_mac, sizeof( verify_mac ) ),
2064 PSA_ERROR_BAD_STATE );
2065 PSA_ASSERT( psa_mac_abort( &operation ) );
2066
2067 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002068 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002069 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002070 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002071 TEST_EQUAL( psa_mac_verify_finish( &operation,
2072 verify_mac, sizeof( verify_mac ) ),
2073 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002074 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002075 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002076 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002077
2078 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002079 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002080 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002081 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002082 TEST_EQUAL( psa_mac_sign_finish( &operation,
2083 sign_mac, sizeof( sign_mac ),
2084 &sign_mac_length ),
2085 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002086 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002087 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002088 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002089
Ronald Cron5425a212020-08-04 14:58:35 +02002090 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002091
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002092exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002093 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002094}
2095/* END_CASE */
2096
2097/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002098void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002099 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002100 int alg_arg,
2101 data_t *input,
2102 data_t *expected_mac )
2103{
Ronald Cron5425a212020-08-04 14:58:35 +02002104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002105 psa_key_type_t key_type = key_type_arg;
2106 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002107 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002109 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002110 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002111 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002112 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002113 const size_t output_sizes_to_test[] = {
2114 0,
2115 1,
2116 expected_mac->len - 1,
2117 expected_mac->len,
2118 expected_mac->len + 1,
2119 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002120
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002121 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002122 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002123 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002124
Gilles Peskine8817f612018-12-18 00:18:46 +01002125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002126
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002128 psa_set_key_algorithm( &attributes, alg );
2129 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002130
Ronald Cron5425a212020-08-04 14:58:35 +02002131 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2132 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002133
Gilles Peskine8b356b52020-08-25 23:44:59 +02002134 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2135 {
2136 const size_t output_size = output_sizes_to_test[i];
2137 psa_status_t expected_status =
2138 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2139 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002140
Chris Jones9634bb12021-01-20 15:56:42 +00002141 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002142 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002143
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002144 /* Calculate the MAC, one-shot case. */
2145 TEST_EQUAL( psa_mac_compute( key, alg,
2146 input->x, input->len,
2147 actual_mac, output_size, &mac_length ),
2148 expected_status );
2149 if( expected_status == PSA_SUCCESS )
2150 {
2151 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2152 actual_mac, mac_length );
2153 }
2154
2155 if( output_size > 0 )
2156 memset( actual_mac, 0, output_size );
2157
2158 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002159 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002160 PSA_ASSERT( psa_mac_update( &operation,
2161 input->x, input->len ) );
2162 TEST_EQUAL( psa_mac_sign_finish( &operation,
2163 actual_mac, output_size,
2164 &mac_length ),
2165 expected_status );
2166 PSA_ASSERT( psa_mac_abort( &operation ) );
2167
2168 if( expected_status == PSA_SUCCESS )
2169 {
2170 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2171 actual_mac, mac_length );
2172 }
2173 mbedtls_free( actual_mac );
2174 actual_mac = NULL;
2175 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002176
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002177exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002178 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002179 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002180 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002181 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002182}
2183/* END_CASE */
2184
2185/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002186void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002187 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002188 int alg_arg,
2189 data_t *input,
2190 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002191{
Ronald Cron5425a212020-08-04 14:58:35 +02002192 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002193 psa_key_type_t key_type = key_type_arg;
2194 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002195 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002197 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198
Gilles Peskine69c12672018-06-28 00:07:19 +02002199 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2200
Gilles Peskine8817f612018-12-18 00:18:46 +01002201 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002202
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002203 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002204 psa_set_key_algorithm( &attributes, alg );
2205 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002206
Ronald Cron5425a212020-08-04 14:58:35 +02002207 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2208 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002209
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002210 /* Verify correct MAC, one-shot case. */
2211 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2212 expected_mac->x, expected_mac->len ) );
2213
2214 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002215 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002216 PSA_ASSERT( psa_mac_update( &operation,
2217 input->x, input->len ) );
2218 PSA_ASSERT( psa_mac_verify_finish( &operation,
2219 expected_mac->x,
2220 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002221
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002222 /* Test a MAC that's too short, one-shot case. */
2223 TEST_EQUAL( psa_mac_verify( key, alg,
2224 input->x, input->len,
2225 expected_mac->x,
2226 expected_mac->len - 1 ),
2227 PSA_ERROR_INVALID_SIGNATURE );
2228
2229 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002230 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002231 PSA_ASSERT( psa_mac_update( &operation,
2232 input->x, input->len ) );
2233 TEST_EQUAL( psa_mac_verify_finish( &operation,
2234 expected_mac->x,
2235 expected_mac->len - 1 ),
2236 PSA_ERROR_INVALID_SIGNATURE );
2237
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002238 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002239 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2240 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002241 TEST_EQUAL( psa_mac_verify( key, alg,
2242 input->x, input->len,
2243 perturbed_mac, expected_mac->len + 1 ),
2244 PSA_ERROR_INVALID_SIGNATURE );
2245
2246 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002247 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002248 PSA_ASSERT( psa_mac_update( &operation,
2249 input->x, input->len ) );
2250 TEST_EQUAL( psa_mac_verify_finish( &operation,
2251 perturbed_mac,
2252 expected_mac->len + 1 ),
2253 PSA_ERROR_INVALID_SIGNATURE );
2254
2255 /* Test changing one byte. */
2256 for( size_t i = 0; i < expected_mac->len; i++ )
2257 {
Chris Jones9634bb12021-01-20 15:56:42 +00002258 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002259 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002260
2261 TEST_EQUAL( psa_mac_verify( key, alg,
2262 input->x, input->len,
2263 perturbed_mac, expected_mac->len ),
2264 PSA_ERROR_INVALID_SIGNATURE );
2265
Ronald Cron5425a212020-08-04 14:58:35 +02002266 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002267 PSA_ASSERT( psa_mac_update( &operation,
2268 input->x, input->len ) );
2269 TEST_EQUAL( psa_mac_verify_finish( &operation,
2270 perturbed_mac,
2271 expected_mac->len ),
2272 PSA_ERROR_INVALID_SIGNATURE );
2273 perturbed_mac[i] ^= 1;
2274 }
2275
Gilles Peskine8c9def32018-02-08 10:02:12 +01002276exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002277 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002278 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002279 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002280 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002281}
2282/* END_CASE */
2283
2284/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002285void cipher_operation_init( )
2286{
Jaeden Ameroab439972019-02-15 14:12:05 +00002287 const uint8_t input[1] = { 0 };
2288 unsigned char output[1] = { 0 };
2289 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002290 /* Test each valid way of initializing the object, except for `= {0}`, as
2291 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2292 * though it's OK by the C standard. We could test for this, but we'd need
2293 * to supress the Clang warning for the test. */
2294 psa_cipher_operation_t func = psa_cipher_operation_init( );
2295 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2296 psa_cipher_operation_t zero;
2297
2298 memset( &zero, 0, sizeof( zero ) );
2299
Jaeden Ameroab439972019-02-15 14:12:05 +00002300 /* A freshly-initialized cipher operation should not be usable. */
2301 TEST_EQUAL( psa_cipher_update( &func,
2302 input, sizeof( input ),
2303 output, sizeof( output ),
2304 &output_length ),
2305 PSA_ERROR_BAD_STATE );
2306 TEST_EQUAL( psa_cipher_update( &init,
2307 input, sizeof( input ),
2308 output, sizeof( output ),
2309 &output_length ),
2310 PSA_ERROR_BAD_STATE );
2311 TEST_EQUAL( psa_cipher_update( &zero,
2312 input, sizeof( input ),
2313 output, sizeof( output ),
2314 &output_length ),
2315 PSA_ERROR_BAD_STATE );
2316
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002317 /* A default cipher operation should be abortable without error. */
2318 PSA_ASSERT( psa_cipher_abort( &func ) );
2319 PSA_ASSERT( psa_cipher_abort( &init ) );
2320 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002321}
2322/* END_CASE */
2323
2324/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002325void cipher_setup( int key_type_arg,
2326 data_t *key,
2327 int alg_arg,
2328 int expected_status_arg )
2329{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002330 psa_key_type_t key_type = key_type_arg;
2331 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002332 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002333 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002335#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002336 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2337#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338
Gilles Peskine8817f612018-12-18 00:18:46 +01002339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002341 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2342 &operation, &status ) )
2343 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002344 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002346 /* The operation object should be reusable. */
2347#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2348 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2349 smoke_test_key_data,
2350 sizeof( smoke_test_key_data ),
2351 KNOWN_SUPPORTED_CIPHER_ALG,
2352 &operation, &status ) )
2353 goto exit;
2354 TEST_EQUAL( status, PSA_SUCCESS );
2355#endif
2356
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002357exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002358 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002359 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002360}
2361/* END_CASE */
2362
Ronald Cronee414c72021-03-18 18:50:08 +01002363/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002364void cipher_bad_order( )
2365{
Ronald Cron5425a212020-08-04 14:58:35 +02002366 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002367 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2368 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002370 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002371 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002372 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002373 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2374 0xaa, 0xaa, 0xaa, 0xaa };
2375 const uint8_t text[] = {
2376 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2377 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002378 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002379 size_t length = 0;
2380
2381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002382 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2383 psa_set_key_algorithm( &attributes, alg );
2384 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2386 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002387
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002388 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002389 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002390 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002391 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002392 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002393 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002394 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002395 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002396
2397 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002398 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002399 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002400 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002401 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002402 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002403 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002404 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002405
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 /* Generate an IV without calling setup beforehand. */
2407 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2408 buffer, sizeof( buffer ),
2409 &length ),
2410 PSA_ERROR_BAD_STATE );
2411 PSA_ASSERT( psa_cipher_abort( &operation ) );
2412
2413 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002414 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2416 buffer, sizeof( buffer ),
2417 &length ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002418 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002419 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2420 buffer, sizeof( buffer ),
2421 &length ),
2422 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002423 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002425 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002426
2427 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002428 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002429 PSA_ASSERT( psa_cipher_set_iv( &operation,
2430 iv, sizeof( iv ) ) );
2431 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2432 buffer, sizeof( buffer ),
2433 &length ),
2434 PSA_ERROR_BAD_STATE );
2435 PSA_ASSERT( psa_cipher_abort( &operation ) );
2436
2437 /* Set an IV without calling setup beforehand. */
2438 TEST_EQUAL( psa_cipher_set_iv( &operation,
2439 iv, sizeof( iv ) ),
2440 PSA_ERROR_BAD_STATE );
2441 PSA_ASSERT( psa_cipher_abort( &operation ) );
2442
2443 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002444 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002445 PSA_ASSERT( psa_cipher_set_iv( &operation,
2446 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002447 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002448 TEST_EQUAL( psa_cipher_set_iv( &operation,
2449 iv, sizeof( iv ) ),
2450 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002451 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002452 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002453 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002454
2455 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002456 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002457 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2458 buffer, sizeof( buffer ),
2459 &length ) );
2460 TEST_EQUAL( psa_cipher_set_iv( &operation,
2461 iv, sizeof( iv ) ),
2462 PSA_ERROR_BAD_STATE );
2463 PSA_ASSERT( psa_cipher_abort( &operation ) );
2464
2465 /* Call update without calling setup beforehand. */
2466 TEST_EQUAL( psa_cipher_update( &operation,
2467 text, sizeof( text ),
2468 buffer, sizeof( buffer ),
2469 &length ),
2470 PSA_ERROR_BAD_STATE );
2471 PSA_ASSERT( psa_cipher_abort( &operation ) );
2472
2473 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002474 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002475 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002476 TEST_EQUAL( psa_cipher_update( &operation,
2477 text, sizeof( text ),
2478 buffer, sizeof( buffer ),
2479 &length ),
2480 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002481 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002482 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002483 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002484
2485 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002486 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002487 PSA_ASSERT( psa_cipher_set_iv( &operation,
2488 iv, sizeof( iv ) ) );
2489 PSA_ASSERT( psa_cipher_finish( &operation,
2490 buffer, sizeof( buffer ), &length ) );
2491 TEST_EQUAL( psa_cipher_update( &operation,
2492 text, sizeof( text ),
2493 buffer, sizeof( buffer ),
2494 &length ),
2495 PSA_ERROR_BAD_STATE );
2496 PSA_ASSERT( psa_cipher_abort( &operation ) );
2497
2498 /* Call finish without calling setup beforehand. */
2499 TEST_EQUAL( psa_cipher_finish( &operation,
2500 buffer, sizeof( buffer ), &length ),
2501 PSA_ERROR_BAD_STATE );
2502 PSA_ASSERT( psa_cipher_abort( &operation ) );
2503
2504 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002505 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002506 /* Not calling update means we are encrypting an empty buffer, which is OK
2507 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002508 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002509 TEST_EQUAL( psa_cipher_finish( &operation,
2510 buffer, sizeof( buffer ), &length ),
2511 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002512 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002513 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002514 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002515
2516 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002517 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002518 PSA_ASSERT( psa_cipher_set_iv( &operation,
2519 iv, sizeof( iv ) ) );
2520 PSA_ASSERT( psa_cipher_finish( &operation,
2521 buffer, sizeof( buffer ), &length ) );
2522 TEST_EQUAL( psa_cipher_finish( &operation,
2523 buffer, sizeof( buffer ), &length ),
2524 PSA_ERROR_BAD_STATE );
2525 PSA_ASSERT( psa_cipher_abort( &operation ) );
2526
Ronald Cron5425a212020-08-04 14:58:35 +02002527 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002528
Jaeden Ameroab439972019-02-15 14:12:05 +00002529exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002530 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002531 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002532}
2533/* END_CASE */
2534
2535/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002536void cipher_encrypt_fail( int alg_arg,
2537 int key_type_arg,
2538 data_t *key_data,
2539 data_t *input,
2540 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002541{
Ronald Cron5425a212020-08-04 14:58:35 +02002542 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002543 psa_status_t status;
2544 psa_key_type_t key_type = key_type_arg;
2545 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002546 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002547 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002548 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002549 size_t output_length = 0;
2550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2551
2552 if ( PSA_ERROR_BAD_STATE != expected_status )
2553 {
2554 PSA_ASSERT( psa_crypto_init( ) );
2555
2556 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2557 psa_set_key_algorithm( &attributes, alg );
2558 psa_set_key_type( &attributes, key_type );
2559
2560 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2561 input->len );
2562 ASSERT_ALLOC( output, output_buffer_size );
2563
2564 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2565 &key ) );
2566 }
2567
2568 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2569 output_buffer_size, &output_length );
2570
2571 TEST_EQUAL( status, expected_status );
2572
2573exit:
2574 mbedtls_free( output );
2575 psa_destroy_key( key );
2576 PSA_DONE( );
2577}
2578/* END_CASE */
2579
2580/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002581void cipher_encrypt_alg_without_iv( int alg_arg,
2582 int key_type_arg,
2583 data_t *key_data,
2584 data_t *input,
2585 data_t *expected_output )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002586{
2587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2588 psa_key_type_t key_type = key_type_arg;
2589 psa_algorithm_t alg = alg_arg;
2590 unsigned char *output = NULL;
2591 size_t output_buffer_size = 0;
2592 size_t output_length = 0;
2593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2594
2595 PSA_ASSERT( psa_crypto_init( ) );
2596
2597 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2598 psa_set_key_algorithm( &attributes, alg );
2599 psa_set_key_type( &attributes, key_type );
2600
2601 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2602 ASSERT_ALLOC( output, output_buffer_size );
2603
2604 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2605 &key ) );
2606
2607 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2608 output_buffer_size, &output_length ) );
2609 TEST_ASSERT( output_length <=
2610 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2611 TEST_ASSERT( output_length <=
2612 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2613
2614 ASSERT_COMPARE( expected_output->x, expected_output->len,
2615 output, output_length );
2616exit:
2617 mbedtls_free( output );
2618 psa_destroy_key( key );
2619 PSA_DONE( );
2620}
2621/* END_CASE */
2622
2623/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002624void cipher_encrypt_validation( int alg_arg,
2625 int key_type_arg,
2626 data_t *key_data,
2627 data_t *input )
2628{
2629 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2630 psa_key_type_t key_type = key_type_arg;
2631 psa_algorithm_t alg = alg_arg;
2632 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2633 unsigned char *output1 = NULL;
2634 size_t output1_buffer_size = 0;
2635 size_t output1_length = 0;
2636 unsigned char *output2 = NULL;
2637 size_t output2_buffer_size = 0;
2638 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002640 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642
Gilles Peskine8817f612018-12-18 00:18:46 +01002643 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002645 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2646 psa_set_key_algorithm( &attributes, alg );
2647 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002648
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002649 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2650 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2651 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2652 ASSERT_ALLOC( output1, output1_buffer_size );
2653 ASSERT_ALLOC( output2, output2_buffer_size );
2654
Ronald Cron5425a212020-08-04 14:58:35 +02002655 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2656 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002658 /* The one-shot cipher encryption uses generated iv so validating
2659 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002660 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2661 output1_buffer_size, &output1_length ) );
2662 TEST_ASSERT( output1_length <=
2663 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2664 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002665 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002666
2667 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2668 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002669
Gilles Peskine8817f612018-12-18 00:18:46 +01002670 PSA_ASSERT( psa_cipher_update( &operation,
2671 input->x, input->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002672 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002673 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002674 TEST_ASSERT( function_output_length <=
2675 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2676 TEST_ASSERT( function_output_length <=
2677 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002678 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002679
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002680 PSA_ASSERT( psa_cipher_finish( &operation,
2681 output2 + output2_length,
2682 output2_buffer_size - output2_length,
2683 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002684 TEST_ASSERT( function_output_length <=
2685 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2686 TEST_ASSERT( function_output_length <=
2687 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002688 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002689
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002690 PSA_ASSERT( psa_cipher_abort( &operation ) );
2691 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2692 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002693
Gilles Peskine50e586b2018-06-08 14:28:46 +02002694exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002695 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002696 mbedtls_free( output1 );
2697 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002698 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002699 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700}
2701/* END_CASE */
2702
2703/* BEGIN_CASE */
2704void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002705 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002706 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002707 int first_part_size_arg,
2708 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002709 data_t *expected_output,
2710 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002711{
Ronald Cron5425a212020-08-04 14:58:35 +02002712 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713 psa_key_type_t key_type = key_type_arg;
2714 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002715 psa_status_t status;
2716 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002717 size_t first_part_size = first_part_size_arg;
2718 size_t output1_length = output1_length_arg;
2719 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002720 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002721 size_t output_buffer_size = 0;
2722 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002723 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002724 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002726
Gilles Peskine8817f612018-12-18 00:18:46 +01002727 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002728
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002729 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2730 psa_set_key_algorithm( &attributes, alg );
2731 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002732
Ronald Cron5425a212020-08-04 14:58:35 +02002733 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2734 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002735
Ronald Cron5425a212020-08-04 14:58:35 +02002736 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002737
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002738 if( iv->len > 0 )
2739 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002740 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002741 }
2742
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002743 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2744 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002745 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002746
Gilles Peskinee0866522019-02-19 19:44:00 +01002747 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002748 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2749 output, output_buffer_size,
2750 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002751 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002752 TEST_ASSERT( function_output_length <=
2753 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2754 TEST_ASSERT( function_output_length <=
2755 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002756 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002757
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002758 if( first_part_size < input->len )
2759 {
2760 PSA_ASSERT( psa_cipher_update( &operation,
2761 input->x + first_part_size,
2762 input->len - first_part_size,
2763 ( output_buffer_size == 0 ? NULL :
2764 output + total_output_length ),
2765 output_buffer_size - total_output_length,
2766 &function_output_length ) );
2767 TEST_ASSERT( function_output_length == output2_length );
2768 TEST_ASSERT( function_output_length <=
2769 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2770 alg,
2771 input->len - first_part_size ) );
2772 TEST_ASSERT( function_output_length <=
2773 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2774 total_output_length += function_output_length;
2775 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002776
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002777 status = psa_cipher_finish( &operation,
2778 ( output_buffer_size == 0 ? NULL :
2779 output + total_output_length ),
2780 output_buffer_size - total_output_length,
2781 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002782 TEST_ASSERT( function_output_length <=
2783 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2784 TEST_ASSERT( function_output_length <=
2785 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002786 total_output_length += function_output_length;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002787 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002789 if( expected_status == PSA_SUCCESS )
2790 {
2791 PSA_ASSERT( psa_cipher_abort( &operation ) );
2792
2793 ASSERT_COMPARE( expected_output->x, expected_output->len,
2794 output, total_output_length );
2795 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796
2797exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002798 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002799 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002800 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002801 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002802}
2803/* END_CASE */
2804
2805/* BEGIN_CASE */
2806void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002807 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002808 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002809 int first_part_size_arg,
2810 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002811 data_t *expected_output,
2812 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002813{
Ronald Cron5425a212020-08-04 14:58:35 +02002814 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815 psa_key_type_t key_type = key_type_arg;
2816 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002817 psa_status_t status;
2818 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002819 size_t first_part_size = first_part_size_arg;
2820 size_t output1_length = output1_length_arg;
2821 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002823 size_t output_buffer_size = 0;
2824 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002825 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002826 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2832 psa_set_key_algorithm( &attributes, alg );
2833 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002834
Ronald Cron5425a212020-08-04 14:58:35 +02002835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2836 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837
Ronald Cron5425a212020-08-04 14:58:35 +02002838 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002839
Steven Cooreman177deba2020-09-07 17:14:14 +02002840 if( iv->len > 0 )
2841 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002842 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002843 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002845 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2846 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002847 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002848
Gilles Peskinee0866522019-02-19 19:44:00 +01002849 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002850 PSA_ASSERT( psa_cipher_update( &operation,
2851 input->x, first_part_size,
2852 output, output_buffer_size,
2853 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002854 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002855 TEST_ASSERT( function_output_length <=
2856 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2857 TEST_ASSERT( function_output_length <=
2858 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002859 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002860
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002861 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002862 {
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002863 PSA_ASSERT( psa_cipher_update( &operation,
2864 input->x + first_part_size,
2865 input->len - first_part_size,
2866 ( output_buffer_size == 0 ? NULL :
2867 output + total_output_length ),
2868 output_buffer_size - total_output_length,
2869 &function_output_length ) );
2870 TEST_ASSERT( function_output_length == output2_length );
2871 TEST_ASSERT( function_output_length <=
2872 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2873 alg,
2874 input->len - first_part_size ) );
2875 TEST_ASSERT( function_output_length <=
2876 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2877 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002878 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002879
Gilles Peskine50e586b2018-06-08 14:28:46 +02002880 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002881 ( output_buffer_size == 0 ? NULL :
2882 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002883 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002885 TEST_ASSERT( function_output_length <=
2886 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2887 TEST_ASSERT( function_output_length <=
2888 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002889 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002890 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002891
2892 if( expected_status == PSA_SUCCESS )
2893 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002894 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002895
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002896 ASSERT_COMPARE( expected_output->x, expected_output->len,
2897 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898 }
2899
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002901 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002902 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002903 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002904 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002905}
2906/* END_CASE */
2907
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002909void cipher_decrypt_fail( int alg_arg,
2910 int key_type_arg,
2911 data_t *key_data,
2912 data_t *iv,
2913 data_t *input_arg,
2914 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002915{
2916 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2917 psa_status_t status;
2918 psa_key_type_t key_type = key_type_arg;
2919 psa_algorithm_t alg = alg_arg;
2920 psa_status_t expected_status = expected_status_arg;
2921 unsigned char *input = NULL;
2922 size_t input_buffer_size = 0;
2923 unsigned char *output = NULL;
2924 size_t output_buffer_size = 0;
2925 size_t output_length = 0;
2926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2927
2928 if ( PSA_ERROR_BAD_STATE != expected_status )
2929 {
2930 PSA_ASSERT( psa_crypto_init( ) );
2931
2932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2933 psa_set_key_algorithm( &attributes, alg );
2934 psa_set_key_type( &attributes, key_type );
2935
2936 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2937 &key ) );
2938 }
2939
2940 /* Allocate input buffer and copy the iv and the plaintext */
2941 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2942 if ( input_buffer_size > 0 )
2943 {
2944 ASSERT_ALLOC( input, input_buffer_size );
2945 memcpy( input, iv->x, iv->len );
2946 memcpy( input + iv->len, input_arg->x, input_arg->len );
2947 }
2948
2949 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2950 ASSERT_ALLOC( output, output_buffer_size );
2951
2952 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2953 output_buffer_size, &output_length );
2954 TEST_EQUAL( status, expected_status );
2955
2956exit:
2957 mbedtls_free( input );
2958 mbedtls_free( output );
2959 psa_destroy_key( key );
2960 PSA_DONE( );
2961}
2962/* END_CASE */
2963
2964/* BEGIN_CASE */
2965void cipher_decrypt( int alg_arg,
2966 int key_type_arg,
2967 data_t *key_data,
2968 data_t *iv,
2969 data_t *input_arg,
2970 data_t *expected_output )
2971{
2972 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2973 psa_key_type_t key_type = key_type_arg;
2974 psa_algorithm_t alg = alg_arg;
2975 unsigned char *input = NULL;
2976 size_t input_buffer_size = 0;
2977 unsigned char *output = NULL;
2978 size_t output_buffer_size = 0;
2979 size_t output_length = 0;
2980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2981
2982 PSA_ASSERT( psa_crypto_init( ) );
2983
2984 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2985 psa_set_key_algorithm( &attributes, alg );
2986 psa_set_key_type( &attributes, key_type );
2987
2988 /* Allocate input buffer and copy the iv and the plaintext */
2989 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2990 if ( input_buffer_size > 0 )
2991 {
2992 ASSERT_ALLOC( input, input_buffer_size );
2993 memcpy( input, iv->x, iv->len );
2994 memcpy( input + iv->len, input_arg->x, input_arg->len );
2995 }
2996
2997 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2998 ASSERT_ALLOC( output, output_buffer_size );
2999
3000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3001 &key ) );
3002
3003 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3004 output_buffer_size, &output_length ) );
3005 TEST_ASSERT( output_length <=
3006 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3007 TEST_ASSERT( output_length <=
3008 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3009
3010 ASSERT_COMPARE( expected_output->x, expected_output->len,
3011 output, output_length );
3012exit:
3013 mbedtls_free( input );
3014 mbedtls_free( output );
3015 psa_destroy_key( key );
3016 PSA_DONE( );
3017}
3018/* END_CASE */
3019
3020/* BEGIN_CASE */
3021void cipher_verify_output( int alg_arg,
3022 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003023 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003024 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003025{
Ronald Cron5425a212020-08-04 14:58:35 +02003026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003027 psa_key_type_t key_type = key_type_arg;
3028 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003029 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003030 size_t output1_size = 0;
3031 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003032 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003033 size_t output2_size = 0;
3034 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003036
Gilles Peskine8817f612018-12-18 00:18:46 +01003037 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003038
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003039 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3040 psa_set_key_algorithm( &attributes, alg );
3041 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003042
Ronald Cron5425a212020-08-04 14:58:35 +02003043 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3044 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003045 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003046 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003047
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003048 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3049 output1, output1_size,
3050 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003051 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003052 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003053 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003054 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003055
3056 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003057 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003058
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003059 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3060 output2, output2_size,
3061 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003062 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003063 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003064 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003065 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003066
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003067 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003068
3069exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003070 mbedtls_free( output1 );
3071 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003072 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003073 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003074}
3075/* END_CASE */
3076
3077/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003078void cipher_verify_output_multipart( int alg_arg,
3079 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003080 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003081 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003082 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003083{
Ronald Cron5425a212020-08-04 14:58:35 +02003084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003085 psa_key_type_t key_type = key_type_arg;
3086 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003087 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003088 unsigned char iv[16] = {0};
3089 size_t iv_size = 16;
3090 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003091 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003092 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003093 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003094 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003095 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003096 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003097 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003098 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3099 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003101
Gilles Peskine8817f612018-12-18 00:18:46 +01003102 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003103
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003104 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3105 psa_set_key_algorithm( &attributes, alg );
3106 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003107
Ronald Cron5425a212020-08-04 14:58:35 +02003108 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3109 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003110
Ronald Cron5425a212020-08-04 14:58:35 +02003111 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3112 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003113
Steven Cooreman177deba2020-09-07 17:14:14 +02003114 if( alg != PSA_ALG_ECB_NO_PADDING )
3115 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003116 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3117 iv, iv_size,
3118 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003119 }
3120
gabor-mezei-armceface22021-01-21 12:26:17 +01003121 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3122 TEST_ASSERT( output1_buffer_size <=
3123 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003124 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003125
Gilles Peskinee0866522019-02-19 19:44:00 +01003126 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003127
Gilles Peskine8817f612018-12-18 00:18:46 +01003128 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3129 output1, output1_buffer_size,
3130 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003131 TEST_ASSERT( function_output_length <=
3132 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3133 TEST_ASSERT( function_output_length <=
3134 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003135 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003136
Gilles Peskine8817f612018-12-18 00:18:46 +01003137 PSA_ASSERT( psa_cipher_update( &operation1,
3138 input->x + first_part_size,
3139 input->len - first_part_size,
3140 output1, output1_buffer_size,
3141 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003142 TEST_ASSERT( function_output_length <=
3143 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3144 alg,
3145 input->len - first_part_size ) );
3146 TEST_ASSERT( function_output_length <=
3147 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003148 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003149
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_cipher_finish( &operation1,
3151 output1 + output1_length,
3152 output1_buffer_size - output1_length,
3153 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003154 TEST_ASSERT( function_output_length <=
3155 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3156 TEST_ASSERT( function_output_length <=
3157 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003158 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003159
Gilles Peskine8817f612018-12-18 00:18:46 +01003160 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003161
Gilles Peskine048b7f02018-06-08 14:20:49 +02003162 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003163 TEST_ASSERT( output2_buffer_size <=
3164 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3165 TEST_ASSERT( output2_buffer_size <=
3166 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003167 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003168
Steven Cooreman177deba2020-09-07 17:14:14 +02003169 if( iv_length > 0 )
3170 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003171 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3172 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003173 }
Moran Pekerded84402018-06-06 16:36:50 +03003174
Gilles Peskine8817f612018-12-18 00:18:46 +01003175 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3176 output2, output2_buffer_size,
3177 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003178 TEST_ASSERT( function_output_length <=
3179 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3180 TEST_ASSERT( function_output_length <=
3181 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003182 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003183
Gilles Peskine8817f612018-12-18 00:18:46 +01003184 PSA_ASSERT( psa_cipher_update( &operation2,
3185 output1 + first_part_size,
3186 output1_length - first_part_size,
3187 output2, output2_buffer_size,
3188 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003189 TEST_ASSERT( function_output_length <=
3190 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3191 alg,
3192 output1_length - first_part_size ) );
3193 TEST_ASSERT( function_output_length <=
3194 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003195 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_finish( &operation2,
3198 output2 + output2_length,
3199 output2_buffer_size - output2_length,
3200 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003201 TEST_ASSERT( function_output_length <=
3202 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3203 TEST_ASSERT( function_output_length <=
3204 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003205 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003206
Gilles Peskine8817f612018-12-18 00:18:46 +01003207 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003208
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003209 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003210
3211exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003212 psa_cipher_abort( &operation1 );
3213 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003214 mbedtls_free( output1 );
3215 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003216 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003217 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003218}
3219/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003220
Gilles Peskine20035e32018-02-03 22:44:14 +01003221/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003222void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003223 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003224 data_t *nonce,
3225 data_t *additional_data,
3226 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003227 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003228{
Ronald Cron5425a212020-08-04 14:58:35 +02003229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003230 psa_key_type_t key_type = key_type_arg;
3231 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003232 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003233 unsigned char *output_data = NULL;
3234 size_t output_size = 0;
3235 size_t output_length = 0;
3236 unsigned char *output_data2 = NULL;
3237 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003238 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003239 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003241
Gilles Peskine8817f612018-12-18 00:18:46 +01003242 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003243
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003244 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3245 psa_set_key_algorithm( &attributes, alg );
3246 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003247
Gilles Peskine049c7532019-05-15 20:22:09 +02003248 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003249 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003250 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3251 key_bits = psa_get_key_bits( &attributes );
3252
3253 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3254 alg );
3255 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3256 * should be exact. */
3257 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3258 expected_result != PSA_ERROR_NOT_SUPPORTED )
3259 {
3260 TEST_EQUAL( output_size,
3261 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3262 TEST_ASSERT( output_size <=
3263 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3264 }
3265 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003266
Steven Cooremanf49478b2021-02-15 15:19:25 +01003267 status = psa_aead_encrypt( key, alg,
3268 nonce->x, nonce->len,
3269 additional_data->x,
3270 additional_data->len,
3271 input_data->x, input_data->len,
3272 output_data, output_size,
3273 &output_length );
3274
3275 /* If the operation is not supported, just skip and not fail in case the
3276 * encryption involves a common limitation of cryptography hardwares and
3277 * an alternative implementation. */
3278 if( status == PSA_ERROR_NOT_SUPPORTED )
3279 {
3280 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3281 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3282 }
3283
3284 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003285
3286 if( PSA_SUCCESS == expected_result )
3287 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003288 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003289
Gilles Peskine003a4a92019-05-14 16:09:40 +02003290 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3291 * should be exact. */
3292 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003293 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003294
gabor-mezei-armceface22021-01-21 12:26:17 +01003295 TEST_ASSERT( input_data->len <=
3296 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3297
Ronald Cron5425a212020-08-04 14:58:35 +02003298 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003299 nonce->x, nonce->len,
3300 additional_data->x,
3301 additional_data->len,
3302 output_data, output_length,
3303 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003304 &output_length2 ),
3305 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003306
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003307 ASSERT_COMPARE( input_data->x, input_data->len,
3308 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003309 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003310
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003312 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003313 mbedtls_free( output_data );
3314 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003315 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003316}
3317/* END_CASE */
3318
3319/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003320void aead_encrypt( int key_type_arg, data_t *key_data,
3321 int alg_arg,
3322 data_t *nonce,
3323 data_t *additional_data,
3324 data_t *input_data,
3325 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003326{
Ronald Cron5425a212020-08-04 14:58:35 +02003327 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003328 psa_key_type_t key_type = key_type_arg;
3329 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003330 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003331 unsigned char *output_data = NULL;
3332 size_t output_size = 0;
3333 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003335 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336
Gilles Peskine8817f612018-12-18 00:18:46 +01003337 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003338
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3340 psa_set_key_algorithm( &attributes, alg );
3341 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342
Gilles Peskine049c7532019-05-15 20:22:09 +02003343 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003344 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003345 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3346 key_bits = psa_get_key_bits( &attributes );
3347
3348 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3349 alg );
3350 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3351 * should be exact. */
3352 TEST_EQUAL( output_size,
3353 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3354 TEST_ASSERT( output_size <=
3355 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3356 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003357
Steven Cooremand588ea12021-01-11 19:36:04 +01003358 status = psa_aead_encrypt( key, alg,
3359 nonce->x, nonce->len,
3360 additional_data->x, additional_data->len,
3361 input_data->x, input_data->len,
3362 output_data, output_size,
3363 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Ronald Cron28a45ed2021-02-09 20:35:42 +01003365 /* If the operation is not supported, just skip and not fail in case the
3366 * encryption involves a common limitation of cryptography hardwares and
3367 * an alternative implementation. */
3368 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003369 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003370 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3371 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003372 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003373
3374 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003375 ASSERT_COMPARE( expected_result->x, expected_result->len,
3376 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003377
Gilles Peskinea1cac842018-06-11 19:33:02 +02003378exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003379 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003381 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003382}
3383/* END_CASE */
3384
3385/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003386void aead_decrypt( int key_type_arg, data_t *key_data,
3387 int alg_arg,
3388 data_t *nonce,
3389 data_t *additional_data,
3390 data_t *input_data,
3391 data_t *expected_data,
3392 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393{
Ronald Cron5425a212020-08-04 14:58:35 +02003394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003395 psa_key_type_t key_type = key_type_arg;
3396 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003397 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398 unsigned char *output_data = NULL;
3399 size_t output_size = 0;
3400 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003402 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003403 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404
Gilles Peskine8817f612018-12-18 00:18:46 +01003405 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003407 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3408 psa_set_key_algorithm( &attributes, alg );
3409 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003410
Gilles Peskine049c7532019-05-15 20:22:09 +02003411 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003412 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003413 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3414 key_bits = psa_get_key_bits( &attributes );
3415
3416 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3417 alg );
3418 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3419 expected_result != PSA_ERROR_NOT_SUPPORTED )
3420 {
3421 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3422 * should be exact. */
3423 TEST_EQUAL( output_size,
3424 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3425 TEST_ASSERT( output_size <=
3426 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3427 }
3428 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003429
Steven Cooremand588ea12021-01-11 19:36:04 +01003430 status = psa_aead_decrypt( key, alg,
3431 nonce->x, nonce->len,
3432 additional_data->x,
3433 additional_data->len,
3434 input_data->x, input_data->len,
3435 output_data, output_size,
3436 &output_length );
3437
Ronald Cron28a45ed2021-02-09 20:35:42 +01003438 /* If the operation is not supported, just skip and not fail in case the
3439 * decryption involves a common limitation of cryptography hardwares and
3440 * an alternative implementation. */
3441 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003442 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003443 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3444 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003445 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003446
3447 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003448
Gilles Peskine2d277862018-06-18 15:41:12 +02003449 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003450 ASSERT_COMPARE( expected_data->x, expected_data->len,
3451 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003452
Gilles Peskinea1cac842018-06-11 19:33:02 +02003453exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003454 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003455 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003456 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003457}
3458/* END_CASE */
3459
3460/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003461void signature_size( int type_arg,
3462 int bits,
3463 int alg_arg,
3464 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003465{
3466 psa_key_type_t type = type_arg;
3467 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003468 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003469
Gilles Peskinefe11b722018-12-18 00:24:04 +01003470 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003471#if defined(MBEDTLS_TEST_DEPRECATED)
3472 TEST_EQUAL( actual_size,
3473 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3474#endif /* MBEDTLS_TEST_DEPRECATED */
3475
Gilles Peskinee59236f2018-01-27 23:32:46 +01003476exit:
3477 ;
3478}
3479/* END_CASE */
3480
3481/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003482void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3483 int alg_arg, data_t *input_data,
3484 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003485{
Ronald Cron5425a212020-08-04 14:58:35 +02003486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003487 psa_key_type_t key_type = key_type_arg;
3488 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003489 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003490 unsigned char *signature = NULL;
3491 size_t signature_size;
3492 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003494
Gilles Peskine8817f612018-12-18 00:18:46 +01003495 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003496
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003497 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003498 psa_set_key_algorithm( &attributes, alg );
3499 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003500
Gilles Peskine049c7532019-05-15 20:22:09 +02003501 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003502 &key ) );
3503 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003504 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003505
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003506 /* Allocate a buffer which has the size advertized by the
3507 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003508 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003509 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003510 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003511 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003512 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003513
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003514 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003515 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003516 input_data->x, input_data->len,
3517 signature, signature_size,
3518 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003519 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003520 ASSERT_COMPARE( output_data->x, output_data->len,
3521 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003522
Gilles Peskine0627f982019-11-26 19:12:16 +01003523#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003524 memset( signature, 0, signature_size );
3525 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003526 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003527 input_data->x, input_data->len,
3528 signature, signature_size,
3529 &signature_length ) );
3530 ASSERT_COMPARE( output_data->x, output_data->len,
3531 signature, signature_length );
3532#endif /* MBEDTLS_TEST_DEPRECATED */
3533
Gilles Peskine20035e32018-02-03 22:44:14 +01003534exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003535 /*
3536 * Key attributes may have been returned by psa_get_key_attributes()
3537 * thus reset them as required.
3538 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003539 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003540
Ronald Cron5425a212020-08-04 14:58:35 +02003541 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003542 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003543 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003544}
3545/* END_CASE */
3546
3547/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003548void sign_hash_fail( int key_type_arg, data_t *key_data,
3549 int alg_arg, data_t *input_data,
3550 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003551{
Ronald Cron5425a212020-08-04 14:58:35 +02003552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003553 psa_key_type_t key_type = key_type_arg;
3554 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003555 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003556 psa_status_t actual_status;
3557 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003558 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003559 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003561
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003562 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003563
Gilles Peskine8817f612018-12-18 00:18:46 +01003564 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003565
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003566 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003567 psa_set_key_algorithm( &attributes, alg );
3568 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003569
Gilles Peskine049c7532019-05-15 20:22:09 +02003570 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003571 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003572
Ronald Cron5425a212020-08-04 14:58:35 +02003573 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003574 input_data->x, input_data->len,
3575 signature, signature_size,
3576 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003577 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003578 /* The value of *signature_length is unspecified on error, but
3579 * whatever it is, it should be less than signature_size, so that
3580 * if the caller tries to read *signature_length bytes without
3581 * checking the error code then they don't overflow a buffer. */
3582 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003583
Gilles Peskine895242b2019-11-29 12:15:40 +01003584#if defined(MBEDTLS_TEST_DEPRECATED)
3585 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003586 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003587 input_data->x, input_data->len,
3588 signature, signature_size,
3589 &signature_length ),
3590 expected_status );
3591 TEST_ASSERT( signature_length <= signature_size );
3592#endif /* MBEDTLS_TEST_DEPRECATED */
3593
Gilles Peskine20035e32018-02-03 22:44:14 +01003594exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003595 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003596 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003597 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003598 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003599}
3600/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003601
3602/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003603void sign_verify_hash( int key_type_arg, data_t *key_data,
3604 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003605{
Ronald Cron5425a212020-08-04 14:58:35 +02003606 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003607 psa_key_type_t key_type = key_type_arg;
3608 psa_algorithm_t alg = alg_arg;
3609 size_t key_bits;
3610 unsigned char *signature = NULL;
3611 size_t signature_size;
3612 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003614
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003616
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003618 psa_set_key_algorithm( &attributes, alg );
3619 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003620
Gilles Peskine049c7532019-05-15 20:22:09 +02003621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003622 &key ) );
3623 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003624 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003625
3626 /* Allocate a buffer which has the size advertized by the
3627 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003628 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003629 key_bits, alg );
3630 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003631 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003632 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003633
3634 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003635 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003636 input_data->x, input_data->len,
3637 signature, signature_size,
3638 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003639 /* Check that the signature length looks sensible. */
3640 TEST_ASSERT( signature_length <= signature_size );
3641 TEST_ASSERT( signature_length > 0 );
3642
3643 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003644 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003645 input_data->x, input_data->len,
3646 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003647
3648 if( input_data->len != 0 )
3649 {
3650 /* Flip a bit in the input and verify that the signature is now
3651 * detected as invalid. Flip a bit at the beginning, not at the end,
3652 * because ECDSA may ignore the last few bits of the input. */
3653 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003654 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003655 input_data->x, input_data->len,
3656 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003657 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003658 }
3659
3660exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003661 /*
3662 * Key attributes may have been returned by psa_get_key_attributes()
3663 * thus reset them as required.
3664 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003665 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003666
Ronald Cron5425a212020-08-04 14:58:35 +02003667 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003668 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003669 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003670}
3671/* END_CASE */
3672
3673/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003674void verify_hash( int key_type_arg, data_t *key_data,
3675 int alg_arg, data_t *hash_data,
3676 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003677{
Ronald Cron5425a212020-08-04 14:58:35 +02003678 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003679 psa_key_type_t key_type = key_type_arg;
3680 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003682
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003683 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003684
Gilles Peskine8817f612018-12-18 00:18:46 +01003685 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003686
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003687 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003688 psa_set_key_algorithm( &attributes, alg );
3689 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003690
Gilles Peskine049c7532019-05-15 20:22:09 +02003691 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003692 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003693
Ronald Cron5425a212020-08-04 14:58:35 +02003694 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003695 hash_data->x, hash_data->len,
3696 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003697
3698#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003699 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003700 hash_data->x, hash_data->len,
3701 signature_data->x,
3702 signature_data->len ) );
3703
3704#endif /* MBEDTLS_TEST_DEPRECATED */
3705
itayzafrir5c753392018-05-08 11:18:38 +03003706exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003707 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003708 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003709 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003710}
3711/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003712
3713/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003714void verify_hash_fail( int key_type_arg, data_t *key_data,
3715 int alg_arg, data_t *hash_data,
3716 data_t *signature_data,
3717 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003718{
Ronald Cron5425a212020-08-04 14:58:35 +02003719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003720 psa_key_type_t key_type = key_type_arg;
3721 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003722 psa_status_t actual_status;
3723 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003725
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003729 psa_set_key_algorithm( &attributes, alg );
3730 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003731
Gilles Peskine049c7532019-05-15 20:22:09 +02003732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003733 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003734
Ronald Cron5425a212020-08-04 14:58:35 +02003735 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003736 hash_data->x, hash_data->len,
3737 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003738 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003739
Gilles Peskine895242b2019-11-29 12:15:40 +01003740#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003741 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003742 hash_data->x, hash_data->len,
3743 signature_data->x, signature_data->len ),
3744 expected_status );
3745#endif /* MBEDTLS_TEST_DEPRECATED */
3746
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003747exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003748 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003749 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003750 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003751}
3752/* END_CASE */
3753
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003754/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003755void sign_message_deterministic( int key_type_arg,
3756 data_t *key_data,
3757 int alg_arg,
3758 data_t *input_data,
3759 data_t *output_data )
3760{
3761 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3762 psa_key_type_t key_type = key_type_arg;
3763 psa_algorithm_t alg = alg_arg;
3764 size_t key_bits;
3765 unsigned char *signature = NULL;
3766 size_t signature_size;
3767 size_t signature_length = 0xdeadbeef;
3768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3769
3770 PSA_ASSERT( psa_crypto_init( ) );
3771
3772 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3773 psa_set_key_algorithm( &attributes, alg );
3774 psa_set_key_type( &attributes, key_type );
3775
3776 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3777 &key ) );
3778 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3779 key_bits = psa_get_key_bits( &attributes );
3780
3781 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3782 TEST_ASSERT( signature_size != 0 );
3783 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3784 ASSERT_ALLOC( signature, signature_size );
3785
3786 PSA_ASSERT( psa_sign_message( key, alg,
3787 input_data->x, input_data->len,
3788 signature, signature_size,
3789 &signature_length ) );
3790
3791 ASSERT_COMPARE( output_data->x, output_data->len,
3792 signature, signature_length );
3793
3794exit:
3795 psa_reset_key_attributes( &attributes );
3796
3797 psa_destroy_key( key );
3798 mbedtls_free( signature );
3799 PSA_DONE( );
3800
3801}
3802/* END_CASE */
3803
3804/* BEGIN_CASE */
3805void sign_message_fail( int key_type_arg,
3806 data_t *key_data,
3807 int alg_arg,
3808 data_t *input_data,
3809 int signature_size_arg,
3810 int expected_status_arg )
3811{
3812 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3813 psa_key_type_t key_type = key_type_arg;
3814 psa_algorithm_t alg = alg_arg;
3815 size_t signature_size = signature_size_arg;
3816 psa_status_t actual_status;
3817 psa_status_t expected_status = expected_status_arg;
3818 unsigned char *signature = NULL;
3819 size_t signature_length = 0xdeadbeef;
3820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3821
3822 ASSERT_ALLOC( signature, signature_size );
3823
3824 PSA_ASSERT( psa_crypto_init( ) );
3825
3826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3827 psa_set_key_algorithm( &attributes, alg );
3828 psa_set_key_type( &attributes, key_type );
3829
3830 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3831 &key ) );
3832
3833 actual_status = psa_sign_message( key, alg,
3834 input_data->x, input_data->len,
3835 signature, signature_size,
3836 &signature_length );
3837 TEST_EQUAL( actual_status, expected_status );
3838 /* The value of *signature_length is unspecified on error, but
3839 * whatever it is, it should be less than signature_size, so that
3840 * if the caller tries to read *signature_length bytes without
3841 * checking the error code then they don't overflow a buffer. */
3842 TEST_ASSERT( signature_length <= signature_size );
3843
3844exit:
3845 psa_reset_key_attributes( &attributes );
3846 psa_destroy_key( key );
3847 mbedtls_free( signature );
3848 PSA_DONE( );
3849}
3850/* END_CASE */
3851
3852/* BEGIN_CASE */
3853void sign_verify_message( int key_type_arg,
3854 data_t *key_data,
3855 int alg_arg,
3856 data_t *input_data )
3857{
3858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3859 psa_key_type_t key_type = key_type_arg;
3860 psa_algorithm_t alg = alg_arg;
3861 size_t key_bits;
3862 unsigned char *signature = NULL;
3863 size_t signature_size;
3864 size_t signature_length = 0xdeadbeef;
3865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3866
3867 PSA_ASSERT( psa_crypto_init( ) );
3868
3869 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3870 PSA_KEY_USAGE_VERIFY_MESSAGE );
3871 psa_set_key_algorithm( &attributes, alg );
3872 psa_set_key_type( &attributes, key_type );
3873
3874 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3875 &key ) );
3876 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3877 key_bits = psa_get_key_bits( &attributes );
3878
3879 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3880 TEST_ASSERT( signature_size != 0 );
3881 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3882 ASSERT_ALLOC( signature, signature_size );
3883
3884 PSA_ASSERT( psa_sign_message( key, alg,
3885 input_data->x, input_data->len,
3886 signature, signature_size,
3887 &signature_length ) );
3888 TEST_ASSERT( signature_length <= signature_size );
3889 TEST_ASSERT( signature_length > 0 );
3890
3891 PSA_ASSERT( psa_verify_message( key, alg,
3892 input_data->x, input_data->len,
3893 signature, signature_length ) );
3894
3895 if( input_data->len != 0 )
3896 {
3897 /* Flip a bit in the input and verify that the signature is now
3898 * detected as invalid. Flip a bit at the beginning, not at the end,
3899 * because ECDSA may ignore the last few bits of the input. */
3900 input_data->x[0] ^= 1;
3901 TEST_EQUAL( psa_verify_message( key, alg,
3902 input_data->x, input_data->len,
3903 signature, signature_length ),
3904 PSA_ERROR_INVALID_SIGNATURE );
3905 }
3906
3907exit:
3908 psa_reset_key_attributes( &attributes );
3909
3910 psa_destroy_key( key );
3911 mbedtls_free( signature );
3912 PSA_DONE( );
3913}
3914/* END_CASE */
3915
3916/* BEGIN_CASE */
3917void verify_message( int key_type_arg,
3918 data_t *key_data,
3919 int alg_arg,
3920 data_t *input_data,
3921 data_t *signature_data )
3922{
3923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3924 psa_key_type_t key_type = key_type_arg;
3925 psa_algorithm_t alg = alg_arg;
3926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3927
3928 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3929
3930 PSA_ASSERT( psa_crypto_init( ) );
3931
3932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3933 psa_set_key_algorithm( &attributes, alg );
3934 psa_set_key_type( &attributes, key_type );
3935
3936 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3937 &key ) );
3938
3939 PSA_ASSERT( psa_verify_message( key, alg,
3940 input_data->x, input_data->len,
3941 signature_data->x, signature_data->len ) );
3942
3943exit:
3944 psa_reset_key_attributes( &attributes );
3945 psa_destroy_key( key );
3946 PSA_DONE( );
3947}
3948/* END_CASE */
3949
3950/* BEGIN_CASE */
3951void verify_message_fail( int key_type_arg,
3952 data_t *key_data,
3953 int alg_arg,
3954 data_t *hash_data,
3955 data_t *signature_data,
3956 int expected_status_arg )
3957{
3958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3959 psa_key_type_t key_type = key_type_arg;
3960 psa_algorithm_t alg = alg_arg;
3961 psa_status_t actual_status;
3962 psa_status_t expected_status = expected_status_arg;
3963 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3964
3965 PSA_ASSERT( psa_crypto_init( ) );
3966
3967 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3968 psa_set_key_algorithm( &attributes, alg );
3969 psa_set_key_type( &attributes, key_type );
3970
3971 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3972 &key ) );
3973
3974 actual_status = psa_verify_message( key, alg,
3975 hash_data->x, hash_data->len,
3976 signature_data->x,
3977 signature_data->len );
3978 TEST_EQUAL( actual_status, expected_status );
3979
3980exit:
3981 psa_reset_key_attributes( &attributes );
3982 psa_destroy_key( key );
3983 PSA_DONE( );
3984}
3985/* END_CASE */
3986
3987/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003988void asymmetric_encrypt( int key_type_arg,
3989 data_t *key_data,
3990 int alg_arg,
3991 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003992 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003993 int expected_output_length_arg,
3994 int expected_status_arg )
3995{
Ronald Cron5425a212020-08-04 14:58:35 +02003996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003997 psa_key_type_t key_type = key_type_arg;
3998 psa_algorithm_t alg = alg_arg;
3999 size_t expected_output_length = expected_output_length_arg;
4000 size_t key_bits;
4001 unsigned char *output = NULL;
4002 size_t output_size;
4003 size_t output_length = ~0;
4004 psa_status_t actual_status;
4005 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004007
Gilles Peskine8817f612018-12-18 00:18:46 +01004008 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004009
Gilles Peskine656896e2018-06-29 19:12:28 +02004010 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004011 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4012 psa_set_key_algorithm( &attributes, alg );
4013 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004014 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004015 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004016
4017 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004019 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004020
Gilles Peskine656896e2018-06-29 19:12:28 +02004021 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004022 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004023 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004024
4025 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004026 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004027 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004028 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004029 output, output_size,
4030 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004031 TEST_EQUAL( actual_status, expected_status );
4032 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004033
Gilles Peskine68428122018-06-30 18:42:41 +02004034 /* If the label is empty, the test framework puts a non-null pointer
4035 * in label->x. Test that a null pointer works as well. */
4036 if( label->len == 0 )
4037 {
4038 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004039 if( output_size != 0 )
4040 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004041 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004042 input_data->x, input_data->len,
4043 NULL, label->len,
4044 output, output_size,
4045 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004046 TEST_EQUAL( actual_status, expected_status );
4047 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004048 }
4049
Gilles Peskine656896e2018-06-29 19:12:28 +02004050exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004051 /*
4052 * Key attributes may have been returned by psa_get_key_attributes()
4053 * thus reset them as required.
4054 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004055 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004056
Ronald Cron5425a212020-08-04 14:58:35 +02004057 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004058 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004059 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004060}
4061/* END_CASE */
4062
4063/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004064void asymmetric_encrypt_decrypt( int key_type_arg,
4065 data_t *key_data,
4066 int alg_arg,
4067 data_t *input_data,
4068 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004069{
Ronald Cron5425a212020-08-04 14:58:35 +02004070 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071 psa_key_type_t key_type = key_type_arg;
4072 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004073 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004074 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004075 size_t output_size;
4076 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004077 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004078 size_t output2_size;
4079 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081
Gilles Peskine8817f612018-12-18 00:18:46 +01004082 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004084 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4085 psa_set_key_algorithm( &attributes, alg );
4086 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004087
Gilles Peskine049c7532019-05-15 20:22:09 +02004088 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004089 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004090
4091 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004092 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004093 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004094
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004095 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004096 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004097 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004098
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004099 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004100 TEST_ASSERT( output2_size <=
4101 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4102 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004103 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004104
Gilles Peskineeebd7382018-06-08 18:11:54 +02004105 /* We test encryption by checking that encrypt-then-decrypt gives back
4106 * the original plaintext because of the non-optional random
4107 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004108 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004109 input_data->x, input_data->len,
4110 label->x, label->len,
4111 output, output_size,
4112 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004113 /* We don't know what ciphertext length to expect, but check that
4114 * it looks sensible. */
4115 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004116
Ronald Cron5425a212020-08-04 14:58:35 +02004117 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004118 output, output_length,
4119 label->x, label->len,
4120 output2, output2_size,
4121 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004122 ASSERT_COMPARE( input_data->x, input_data->len,
4123 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004124
4125exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004126 /*
4127 * Key attributes may have been returned by psa_get_key_attributes()
4128 * thus reset them as required.
4129 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004130 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004131
Ronald Cron5425a212020-08-04 14:58:35 +02004132 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004133 mbedtls_free( output );
4134 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004135 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004136}
4137/* END_CASE */
4138
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004139/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004140void asymmetric_decrypt( int key_type_arg,
4141 data_t *key_data,
4142 int alg_arg,
4143 data_t *input_data,
4144 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004145 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004146{
Ronald Cron5425a212020-08-04 14:58:35 +02004147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004148 psa_key_type_t key_type = key_type_arg;
4149 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004150 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004151 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004152 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004153 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004155
Gilles Peskine8817f612018-12-18 00:18:46 +01004156 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004158 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4159 psa_set_key_algorithm( &attributes, alg );
4160 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004161
Gilles Peskine049c7532019-05-15 20:22:09 +02004162 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004163 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164
gabor-mezei-armceface22021-01-21 12:26:17 +01004165 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4166 key_bits = psa_get_key_bits( &attributes );
4167
4168 /* Determine the maximum ciphertext length */
4169 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4170 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4171 ASSERT_ALLOC( output, output_size );
4172
Ronald Cron5425a212020-08-04 14:58:35 +02004173 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004174 input_data->x, input_data->len,
4175 label->x, label->len,
4176 output,
4177 output_size,
4178 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004179 ASSERT_COMPARE( expected_data->x, expected_data->len,
4180 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181
Gilles Peskine68428122018-06-30 18:42:41 +02004182 /* If the label is empty, the test framework puts a non-null pointer
4183 * in label->x. Test that a null pointer works as well. */
4184 if( label->len == 0 )
4185 {
4186 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004187 if( output_size != 0 )
4188 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004189 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004190 input_data->x, input_data->len,
4191 NULL, label->len,
4192 output,
4193 output_size,
4194 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004195 ASSERT_COMPARE( expected_data->x, expected_data->len,
4196 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004197 }
4198
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004200 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004201 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004202 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004203 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004204}
4205/* END_CASE */
4206
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004207/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004208void asymmetric_decrypt_fail( int key_type_arg,
4209 data_t *key_data,
4210 int alg_arg,
4211 data_t *input_data,
4212 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004213 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004214 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215{
Ronald Cron5425a212020-08-04 14:58:35 +02004216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004217 psa_key_type_t key_type = key_type_arg;
4218 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004219 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004220 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004221 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004222 psa_status_t actual_status;
4223 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004224 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004225
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004226 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004227
Gilles Peskine8817f612018-12-18 00:18:46 +01004228 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004230 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4231 psa_set_key_algorithm( &attributes, alg );
4232 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004233
Gilles Peskine049c7532019-05-15 20:22:09 +02004234 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004235 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004236
Ronald Cron5425a212020-08-04 14:58:35 +02004237 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004238 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004239 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004240 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004241 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004242 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004243 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004244
Gilles Peskine68428122018-06-30 18:42:41 +02004245 /* If the label is empty, the test framework puts a non-null pointer
4246 * in label->x. Test that a null pointer works as well. */
4247 if( label->len == 0 )
4248 {
4249 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004250 if( output_size != 0 )
4251 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004252 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004253 input_data->x, input_data->len,
4254 NULL, label->len,
4255 output, output_size,
4256 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004257 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004258 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004259 }
4260
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004261exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004262 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004263 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004264 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004265 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004266}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004267/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004268
4269/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004270void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004271{
4272 /* Test each valid way of initializing the object, except for `= {0}`, as
4273 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4274 * though it's OK by the C standard. We could test for this, but we'd need
4275 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004276 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004277 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4278 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4279 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004280
4281 memset( &zero, 0, sizeof( zero ) );
4282
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004283 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004284 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004285 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004286 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004287 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004288 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004289 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004290
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004291 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004292 PSA_ASSERT( psa_key_derivation_abort(&func) );
4293 PSA_ASSERT( psa_key_derivation_abort(&init) );
4294 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004295}
4296/* END_CASE */
4297
Janos Follath16de4a42019-06-13 16:32:24 +01004298/* BEGIN_CASE */
4299void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004300{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004301 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004302 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004303 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004304
Gilles Peskine8817f612018-12-18 00:18:46 +01004305 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004306
Janos Follath16de4a42019-06-13 16:32:24 +01004307 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004308 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004309
4310exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004311 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004312 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004313}
4314/* END_CASE */
4315
Janos Follathaf3c2a02019-06-12 12:34:34 +01004316/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004317void derive_set_capacity( int alg_arg, int capacity_arg,
4318 int expected_status_arg )
4319{
4320 psa_algorithm_t alg = alg_arg;
4321 size_t capacity = capacity_arg;
4322 psa_status_t expected_status = expected_status_arg;
4323 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4324
4325 PSA_ASSERT( psa_crypto_init( ) );
4326
4327 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4328
4329 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4330 expected_status );
4331
4332exit:
4333 psa_key_derivation_abort( &operation );
4334 PSA_DONE( );
4335}
4336/* END_CASE */
4337
4338/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004339void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004340 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004341 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004342 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004343 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004344 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004345 int expected_status_arg3,
4346 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004347{
4348 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004349 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4350 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004351 psa_status_t expected_statuses[] = {expected_status_arg1,
4352 expected_status_arg2,
4353 expected_status_arg3};
4354 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004355 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4356 MBEDTLS_SVC_KEY_ID_INIT,
4357 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004358 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4360 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004361 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004362 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004363 psa_status_t expected_output_status = expected_output_status_arg;
4364 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004365
4366 PSA_ASSERT( psa_crypto_init( ) );
4367
4368 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4369 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004370
4371 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4372
4373 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4374 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004375 mbedtls_test_set_step( i );
4376 if( steps[i] == 0 )
4377 {
4378 /* Skip this step */
4379 }
4380 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004381 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004382 psa_set_key_type( &attributes, key_types[i] );
4383 PSA_ASSERT( psa_import_key( &attributes,
4384 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004385 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004386 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4387 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4388 {
4389 // When taking a private key as secret input, use key agreement
4390 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004391 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4392 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004393 expected_statuses[i] );
4394 }
4395 else
4396 {
4397 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004398 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004399 expected_statuses[i] );
4400 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004401 }
4402 else
4403 {
4404 TEST_EQUAL( psa_key_derivation_input_bytes(
4405 &operation, steps[i],
4406 inputs[i]->x, inputs[i]->len ),
4407 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004408 }
4409 }
4410
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004411 if( output_key_type != PSA_KEY_TYPE_NONE )
4412 {
4413 psa_reset_key_attributes( &attributes );
4414 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4415 psa_set_key_bits( &attributes, 8 );
4416 actual_output_status =
4417 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004418 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004419 }
4420 else
4421 {
4422 uint8_t buffer[1];
4423 actual_output_status =
4424 psa_key_derivation_output_bytes( &operation,
4425 buffer, sizeof( buffer ) );
4426 }
4427 TEST_EQUAL( actual_output_status, expected_output_status );
4428
Janos Follathaf3c2a02019-06-12 12:34:34 +01004429exit:
4430 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004431 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4432 psa_destroy_key( keys[i] );
4433 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004434 PSA_DONE( );
4435}
4436/* END_CASE */
4437
Janos Follathd958bb72019-07-03 15:02:16 +01004438/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004439void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004440{
Janos Follathd958bb72019-07-03 15:02:16 +01004441 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004443 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004445 unsigned char input1[] = "Input 1";
4446 size_t input1_length = sizeof( input1 );
4447 unsigned char input2[] = "Input 2";
4448 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004449 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004450 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004451 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4452 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4453 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004455
Gilles Peskine8817f612018-12-18 00:18:46 +01004456 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004457
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004458 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4459 psa_set_key_algorithm( &attributes, alg );
4460 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004461
Gilles Peskine73676cb2019-05-15 20:15:10 +02004462 PSA_ASSERT( psa_import_key( &attributes,
4463 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004464 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004465
4466 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004467 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4468 input1, input1_length,
4469 input2, input2_length,
4470 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004471 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004472
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004474 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004475 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004476
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004477 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004478
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004479 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004480 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004481
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004482exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004483 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004484 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004485 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004486}
4487/* END_CASE */
4488
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004489/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004490void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004491{
4492 uint8_t output_buffer[16];
4493 size_t buffer_size = 16;
4494 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004496
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004497 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4498 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004499 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004502 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004503
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004505
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004506 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4507 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004508 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004509
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004510 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004511 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004512
4513exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004514 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004515}
4516/* END_CASE */
4517
4518/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004519void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004520 int step1_arg, data_t *input1,
4521 int step2_arg, data_t *input2,
4522 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004523 int requested_capacity_arg,
4524 data_t *expected_output1,
4525 data_t *expected_output2 )
4526{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004527 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004528 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4529 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004530 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4531 MBEDTLS_SVC_KEY_ID_INIT,
4532 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004533 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004534 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004535 uint8_t *expected_outputs[2] =
4536 {expected_output1->x, expected_output2->x};
4537 size_t output_sizes[2] =
4538 {expected_output1->len, expected_output2->len};
4539 size_t output_buffer_size = 0;
4540 uint8_t *output_buffer = NULL;
4541 size_t expected_capacity;
4542 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004543 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004544 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004545 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004546
4547 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4548 {
4549 if( output_sizes[i] > output_buffer_size )
4550 output_buffer_size = output_sizes[i];
4551 if( output_sizes[i] == 0 )
4552 expected_outputs[i] = NULL;
4553 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004554 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004556
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004557 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4558 psa_set_key_algorithm( &attributes, alg );
4559 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004560
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004561 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004562 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4563 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4564 requested_capacity ) );
4565 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004566 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004567 switch( steps[i] )
4568 {
4569 case 0:
4570 break;
4571 case PSA_KEY_DERIVATION_INPUT_SECRET:
4572 PSA_ASSERT( psa_import_key( &attributes,
4573 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004574 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004575
4576 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4577 {
4578 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4579 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4580 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4581 }
4582
Gilles Peskine1468da72019-05-29 17:35:49 +02004583 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004584 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004585 break;
4586 default:
4587 PSA_ASSERT( psa_key_derivation_input_bytes(
4588 &operation, steps[i],
4589 inputs[i]->x, inputs[i]->len ) );
4590 break;
4591 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004592 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004593
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004595 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004596 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004597 expected_capacity = requested_capacity;
4598
4599 /* Expansion phase. */
4600 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4601 {
4602 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004603 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004604 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004605 if( expected_capacity == 0 && output_sizes[i] == 0 )
4606 {
4607 /* Reading 0 bytes when 0 bytes are available can go either way. */
4608 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004609 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004610 continue;
4611 }
4612 else if( expected_capacity == 0 ||
4613 output_sizes[i] > expected_capacity )
4614 {
4615 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004616 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004617 expected_capacity = 0;
4618 continue;
4619 }
4620 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004621 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004622 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004623 ASSERT_COMPARE( output_buffer, output_sizes[i],
4624 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004625 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004626 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004627 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004628 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004629 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004630 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004631 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004632
4633exit:
4634 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004635 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004636 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4637 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004638 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004639}
4640/* END_CASE */
4641
4642/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004643void derive_full( int alg_arg,
4644 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004645 data_t *input1,
4646 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004647 int requested_capacity_arg )
4648{
Ronald Cron5425a212020-08-04 14:58:35 +02004649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004650 psa_algorithm_t alg = alg_arg;
4651 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004652 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004653 unsigned char output_buffer[16];
4654 size_t expected_capacity = requested_capacity;
4655 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004657
Gilles Peskine8817f612018-12-18 00:18:46 +01004658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004659
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004660 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4661 psa_set_key_algorithm( &attributes, alg );
4662 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004663
Gilles Peskine049c7532019-05-15 20:22:09 +02004664 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004665 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004666
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004667 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4668 input1->x, input1->len,
4669 input2->x, input2->len,
4670 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004671 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004672
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004673 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004674 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004675 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004676
4677 /* Expansion phase. */
4678 while( current_capacity > 0 )
4679 {
4680 size_t read_size = sizeof( output_buffer );
4681 if( read_size > current_capacity )
4682 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004684 output_buffer,
4685 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004686 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004687 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004688 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004689 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004690 }
4691
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004692 /* Check that the operation refuses to go over capacity. */
4693 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004694 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004695
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004696 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004697
4698exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004701 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004702}
4703/* END_CASE */
4704
Janos Follathe60c9052019-07-03 13:51:30 +01004705/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004706void derive_key_exercise( int alg_arg,
4707 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004708 data_t *input1,
4709 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004710 int derived_type_arg,
4711 int derived_bits_arg,
4712 int derived_usage_arg,
4713 int derived_alg_arg )
4714{
Ronald Cron5425a212020-08-04 14:58:35 +02004715 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4716 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004717 psa_algorithm_t alg = alg_arg;
4718 psa_key_type_t derived_type = derived_type_arg;
4719 size_t derived_bits = derived_bits_arg;
4720 psa_key_usage_t derived_usage = derived_usage_arg;
4721 psa_algorithm_t derived_alg = derived_alg_arg;
4722 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004723 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004725 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004726
Gilles Peskine8817f612018-12-18 00:18:46 +01004727 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004728
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004729 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4730 psa_set_key_algorithm( &attributes, alg );
4731 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004733 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004734
4735 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004736 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4737 input1->x, input1->len,
4738 input2->x, input2->len,
4739 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004740 goto exit;
4741
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004742 psa_set_key_usage_flags( &attributes, derived_usage );
4743 psa_set_key_algorithm( &attributes, derived_alg );
4744 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004745 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004747 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004748
4749 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004750 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004751 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4752 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004753
4754 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004755 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004756 goto exit;
4757
4758exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004759 /*
4760 * Key attributes may have been returned by psa_get_key_attributes()
4761 * thus reset them as required.
4762 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004763 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004764
4765 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004766 psa_destroy_key( base_key );
4767 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004768 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004769}
4770/* END_CASE */
4771
Janos Follath42fd8882019-07-03 14:17:09 +01004772/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004773void derive_key_export( int alg_arg,
4774 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004775 data_t *input1,
4776 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004777 int bytes1_arg,
4778 int bytes2_arg )
4779{
Ronald Cron5425a212020-08-04 14:58:35 +02004780 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4781 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004782 psa_algorithm_t alg = alg_arg;
4783 size_t bytes1 = bytes1_arg;
4784 size_t bytes2 = bytes2_arg;
4785 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004786 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004787 uint8_t *output_buffer = NULL;
4788 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004789 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4790 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004791 size_t length;
4792
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004793 ASSERT_ALLOC( output_buffer, capacity );
4794 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004795 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004796
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004797 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4798 psa_set_key_algorithm( &base_attributes, alg );
4799 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004800 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004801 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004802
4803 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004804 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4805 input1->x, input1->len,
4806 input2->x, input2->len,
4807 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004808 goto exit;
4809
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004811 output_buffer,
4812 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004813 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004814
4815 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004816 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4817 input1->x, input1->len,
4818 input2->x, input2->len,
4819 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004820 goto exit;
4821
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004822 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4823 psa_set_key_algorithm( &derived_attributes, 0 );
4824 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004825 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004827 &derived_key ) );
4828 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004829 export_buffer, bytes1,
4830 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004831 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004832 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004833 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004835 &derived_key ) );
4836 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004837 export_buffer + bytes1, bytes2,
4838 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004839 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004840
4841 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004842 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4843 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004844
4845exit:
4846 mbedtls_free( output_buffer );
4847 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004849 psa_destroy_key( base_key );
4850 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004851 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004852}
4853/* END_CASE */
4854
4855/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004856void derive_key( int alg_arg,
4857 data_t *key_data, data_t *input1, data_t *input2,
4858 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004859 int expected_status_arg,
4860 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004861{
Ronald Cron5425a212020-08-04 14:58:35 +02004862 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4863 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004864 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004865 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004866 size_t bits = bits_arg;
4867 psa_status_t expected_status = expected_status_arg;
4868 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4869 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4870 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4871
4872 PSA_ASSERT( psa_crypto_init( ) );
4873
4874 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4875 psa_set_key_algorithm( &base_attributes, alg );
4876 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4877 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004878 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004879
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004880 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4881 input1->x, input1->len,
4882 input2->x, input2->len,
4883 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004884 goto exit;
4885
4886 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4887 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004888 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004889 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004890
4891 psa_status_t status =
4892 psa_key_derivation_output_key( &derived_attributes,
4893 &operation,
4894 &derived_key );
4895 if( is_large_output > 0 )
4896 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4897 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004898
4899exit:
4900 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004901 psa_destroy_key( base_key );
4902 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004903 PSA_DONE( );
4904}
4905/* END_CASE */
4906
4907/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004908void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004909 int our_key_type_arg, int our_key_alg_arg,
4910 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004911 int expected_status_arg )
4912{
Ronald Cron5425a212020-08-04 14:58:35 +02004913 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004914 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004915 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004916 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004918 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004919 psa_status_t expected_status = expected_status_arg;
4920 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004921
Gilles Peskine8817f612018-12-18 00:18:46 +01004922 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004923
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004924 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004925 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004926 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004927 PSA_ASSERT( psa_import_key( &attributes,
4928 our_key_data->x, our_key_data->len,
4929 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930
Gilles Peskine77f40d82019-04-11 21:27:06 +02004931 /* The tests currently include inputs that should fail at either step.
4932 * Test cases that fail at the setup step should be changed to call
4933 * key_derivation_setup instead, and this function should be renamed
4934 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004936 if( status == PSA_SUCCESS )
4937 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004938 TEST_EQUAL( psa_key_derivation_key_agreement(
4939 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4940 our_key,
4941 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004942 expected_status );
4943 }
4944 else
4945 {
4946 TEST_ASSERT( status == expected_status );
4947 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004948
4949exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004951 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004952 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004953}
4954/* END_CASE */
4955
4956/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004957void raw_key_agreement( int alg_arg,
4958 int our_key_type_arg, data_t *our_key_data,
4959 data_t *peer_key_data,
4960 data_t *expected_output )
4961{
Ronald Cron5425a212020-08-04 14:58:35 +02004962 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004963 psa_algorithm_t alg = alg_arg;
4964 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004966 unsigned char *output = NULL;
4967 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004968 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004969
4970 ASSERT_ALLOC( output, expected_output->len );
4971 PSA_ASSERT( psa_crypto_init( ) );
4972
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004973 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4974 psa_set_key_algorithm( &attributes, alg );
4975 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004976 PSA_ASSERT( psa_import_key( &attributes,
4977 our_key_data->x, our_key_data->len,
4978 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004979
gabor-mezei-armceface22021-01-21 12:26:17 +01004980 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4981 key_bits = psa_get_key_bits( &attributes );
4982
Gilles Peskinebe697d82019-05-16 18:00:41 +02004983 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4984 peer_key_data->x, peer_key_data->len,
4985 output, expected_output->len,
4986 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004987 ASSERT_COMPARE( output, output_length,
4988 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004989 TEST_ASSERT( output_length <=
4990 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4991 TEST_ASSERT( output_length <=
4992 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004993
4994exit:
4995 mbedtls_free( output );
4996 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004997 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004998}
4999/* END_CASE */
5000
5001/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005002void key_agreement_capacity( int alg_arg,
5003 int our_key_type_arg, data_t *our_key_data,
5004 data_t *peer_key_data,
5005 int expected_capacity_arg )
5006{
Ronald Cron5425a212020-08-04 14:58:35 +02005007 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005008 psa_algorithm_t alg = alg_arg;
5009 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005010 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005012 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005013 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005014
Gilles Peskine8817f612018-12-18 00:18:46 +01005015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005016
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005017 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5018 psa_set_key_algorithm( &attributes, alg );
5019 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005020 PSA_ASSERT( psa_import_key( &attributes,
5021 our_key_data->x, our_key_data->len,
5022 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005023
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005025 PSA_ASSERT( psa_key_derivation_key_agreement(
5026 &operation,
5027 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5028 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005029 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5030 {
5031 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005033 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005034 NULL, 0 ) );
5035 }
Gilles Peskine59685592018-09-18 12:11:34 +02005036
Gilles Peskinebf491972018-10-25 22:36:12 +02005037 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005038 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005039 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005040 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005041
Gilles Peskinebf491972018-10-25 22:36:12 +02005042 /* Test the actual capacity by reading the output. */
5043 while( actual_capacity > sizeof( output ) )
5044 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005046 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005047 actual_capacity -= sizeof( output );
5048 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005049 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005050 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005051 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005052 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005053
Gilles Peskine59685592018-09-18 12:11:34 +02005054exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005056 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005057 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005058}
5059/* END_CASE */
5060
5061/* BEGIN_CASE */
5062void key_agreement_output( int alg_arg,
5063 int our_key_type_arg, data_t *our_key_data,
5064 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005065 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005066{
Ronald Cron5425a212020-08-04 14:58:35 +02005067 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005068 psa_algorithm_t alg = alg_arg;
5069 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005070 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005072 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005073
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005074 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5075 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005076
Gilles Peskine8817f612018-12-18 00:18:46 +01005077 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005078
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5080 psa_set_key_algorithm( &attributes, alg );
5081 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005082 PSA_ASSERT( psa_import_key( &attributes,
5083 our_key_data->x, our_key_data->len,
5084 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005085
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005087 PSA_ASSERT( psa_key_derivation_key_agreement(
5088 &operation,
5089 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5090 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005091 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5092 {
5093 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005095 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005096 NULL, 0 ) );
5097 }
Gilles Peskine59685592018-09-18 12:11:34 +02005098
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005099 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005100 actual_output,
5101 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005102 ASSERT_COMPARE( actual_output, expected_output1->len,
5103 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005104 if( expected_output2->len != 0 )
5105 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005107 actual_output,
5108 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005109 ASSERT_COMPARE( actual_output, expected_output2->len,
5110 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005111 }
Gilles Peskine59685592018-09-18 12:11:34 +02005112
5113exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005114 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005115 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005116 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005117 mbedtls_free( actual_output );
5118}
5119/* END_CASE */
5120
5121/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005122void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005123{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005124 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005125 unsigned char *output = NULL;
5126 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005127 size_t i;
5128 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005129
Simon Butcher49f8e312020-03-03 15:51:50 +00005130 TEST_ASSERT( bytes_arg >= 0 );
5131
Gilles Peskine91892022021-02-08 19:50:26 +01005132 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005133 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005134
Gilles Peskine8817f612018-12-18 00:18:46 +01005135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005136
Gilles Peskinea50d7392018-06-21 10:22:13 +02005137 /* Run several times, to ensure that every output byte will be
5138 * nonzero at least once with overwhelming probability
5139 * (2^(-8*number_of_runs)). */
5140 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005141 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005142 if( bytes != 0 )
5143 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005144 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005145
Gilles Peskinea50d7392018-06-21 10:22:13 +02005146 for( i = 0; i < bytes; i++ )
5147 {
5148 if( output[i] != 0 )
5149 ++changed[i];
5150 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005151 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005152
5153 /* Check that every byte was changed to nonzero at least once. This
5154 * validates that psa_generate_random is overwriting every byte of
5155 * the output buffer. */
5156 for( i = 0; i < bytes; i++ )
5157 {
5158 TEST_ASSERT( changed[i] != 0 );
5159 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005160
5161exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005162 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005163 mbedtls_free( output );
5164 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005165}
5166/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005167
5168/* BEGIN_CASE */
5169void generate_key( int type_arg,
5170 int bits_arg,
5171 int usage_arg,
5172 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005173 int expected_status_arg,
5174 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005175{
Ronald Cron5425a212020-08-04 14:58:35 +02005176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005177 psa_key_type_t type = type_arg;
5178 psa_key_usage_t usage = usage_arg;
5179 size_t bits = bits_arg;
5180 psa_algorithm_t alg = alg_arg;
5181 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005183 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005184
Gilles Peskine8817f612018-12-18 00:18:46 +01005185 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005186
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005187 psa_set_key_usage_flags( &attributes, usage );
5188 psa_set_key_algorithm( &attributes, alg );
5189 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005190 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005191
5192 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005193 psa_status_t status = psa_generate_key( &attributes, &key );
5194
5195 if( is_large_key > 0 )
5196 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5197 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005198 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005199 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005200
5201 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005202 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005203 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5204 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005205
Gilles Peskine818ca122018-06-20 18:16:48 +02005206 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005207 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005208 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005209
5210exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005211 /*
5212 * Key attributes may have been returned by psa_get_key_attributes()
5213 * thus reset them as required.
5214 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005215 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005216
Ronald Cron5425a212020-08-04 14:58:35 +02005217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005218 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005219}
5220/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005221
Ronald Cronee414c72021-03-18 18:50:08 +01005222/* 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 +02005223void generate_key_rsa( int bits_arg,
5224 data_t *e_arg,
5225 int expected_status_arg )
5226{
Ronald Cron5425a212020-08-04 14:58:35 +02005227 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005228 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005229 size_t bits = bits_arg;
5230 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5231 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5232 psa_status_t expected_status = expected_status_arg;
5233 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5234 uint8_t *exported = NULL;
5235 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005236 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005237 size_t exported_length = SIZE_MAX;
5238 uint8_t *e_read_buffer = NULL;
5239 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005240 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005241 size_t e_read_length = SIZE_MAX;
5242
5243 if( e_arg->len == 0 ||
5244 ( e_arg->len == 3 &&
5245 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5246 {
5247 is_default_public_exponent = 1;
5248 e_read_size = 0;
5249 }
5250 ASSERT_ALLOC( e_read_buffer, e_read_size );
5251 ASSERT_ALLOC( exported, exported_size );
5252
5253 PSA_ASSERT( psa_crypto_init( ) );
5254
5255 psa_set_key_usage_flags( &attributes, usage );
5256 psa_set_key_algorithm( &attributes, alg );
5257 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5258 e_arg->x, e_arg->len ) );
5259 psa_set_key_bits( &attributes, bits );
5260
5261 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005262 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005263 if( expected_status != PSA_SUCCESS )
5264 goto exit;
5265
5266 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005267 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005268 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5269 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5270 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5271 e_read_buffer, e_read_size,
5272 &e_read_length ) );
5273 if( is_default_public_exponent )
5274 TEST_EQUAL( e_read_length, 0 );
5275 else
5276 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5277
5278 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005279 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005280 goto exit;
5281
5282 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005283 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005284 exported, exported_size,
5285 &exported_length ) );
5286 {
5287 uint8_t *p = exported;
5288 uint8_t *end = exported + exported_length;
5289 size_t len;
5290 /* RSAPublicKey ::= SEQUENCE {
5291 * modulus INTEGER, -- n
5292 * publicExponent INTEGER } -- e
5293 */
5294 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005295 MBEDTLS_ASN1_SEQUENCE |
5296 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005297 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005298 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5299 MBEDTLS_ASN1_INTEGER ) );
5300 if( len >= 1 && p[0] == 0 )
5301 {
5302 ++p;
5303 --len;
5304 }
5305 if( e_arg->len == 0 )
5306 {
5307 TEST_EQUAL( len, 3 );
5308 TEST_EQUAL( p[0], 1 );
5309 TEST_EQUAL( p[1], 0 );
5310 TEST_EQUAL( p[2], 1 );
5311 }
5312 else
5313 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5314 }
5315
5316exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005317 /*
5318 * Key attributes may have been returned by psa_get_key_attributes() or
5319 * set by psa_set_key_domain_parameters() thus reset them as required.
5320 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005321 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005322
Ronald Cron5425a212020-08-04 14:58:35 +02005323 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005324 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005325 mbedtls_free( e_read_buffer );
5326 mbedtls_free( exported );
5327}
5328/* END_CASE */
5329
Darryl Greend49a4992018-06-18 17:27:26 +01005330/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005331void persistent_key_load_key_from_storage( data_t *data,
5332 int type_arg, int bits_arg,
5333 int usage_flags_arg, int alg_arg,
5334 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005335{
Ronald Cron71016a92020-08-28 19:01:50 +02005336 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5339 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005340 psa_key_type_t type = type_arg;
5341 size_t bits = bits_arg;
5342 psa_key_usage_t usage_flags = usage_flags_arg;
5343 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005345 unsigned char *first_export = NULL;
5346 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005347 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005348 size_t first_exported_length;
5349 size_t second_exported_length;
5350
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005351 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5352 {
5353 ASSERT_ALLOC( first_export, export_size );
5354 ASSERT_ALLOC( second_export, export_size );
5355 }
Darryl Greend49a4992018-06-18 17:27:26 +01005356
Gilles Peskine8817f612018-12-18 00:18:46 +01005357 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005358
Gilles Peskinec87af662019-05-15 16:12:22 +02005359 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005360 psa_set_key_usage_flags( &attributes, usage_flags );
5361 psa_set_key_algorithm( &attributes, alg );
5362 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005363 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005364
Darryl Green0c6575a2018-11-07 16:05:30 +00005365 switch( generation_method )
5366 {
5367 case IMPORT_KEY:
5368 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005369 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005370 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005371 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005372
Darryl Green0c6575a2018-11-07 16:05:30 +00005373 case GENERATE_KEY:
5374 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005375 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005376 break;
5377
5378 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005379#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005380 {
5381 /* Create base key */
5382 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5383 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5384 psa_set_key_usage_flags( &base_attributes,
5385 PSA_KEY_USAGE_DERIVE );
5386 psa_set_key_algorithm( &base_attributes, derive_alg );
5387 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005388 PSA_ASSERT( psa_import_key( &base_attributes,
5389 data->x, data->len,
5390 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005391 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005393 PSA_ASSERT( psa_key_derivation_input_key(
5394 &operation,
5395 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005396 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005398 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005399 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5400 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005401 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005402 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005403 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005404 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005405 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005406#else
5407 TEST_ASSUME( ! "KDF not supported in this configuration" );
5408#endif
5409 break;
5410
5411 default:
5412 TEST_ASSERT( ! "generation_method not implemented in test" );
5413 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005414 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005415 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005416
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005417 /* Export the key if permitted by the key policy. */
5418 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5419 {
Ronald Cron5425a212020-08-04 14:58:35 +02005420 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005421 first_export, export_size,
5422 &first_exported_length ) );
5423 if( generation_method == IMPORT_KEY )
5424 ASSERT_COMPARE( data->x, data->len,
5425 first_export, first_exported_length );
5426 }
Darryl Greend49a4992018-06-18 17:27:26 +01005427
5428 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005429 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005430 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005431 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005432
Darryl Greend49a4992018-06-18 17:27:26 +01005433 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005434 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005435 TEST_ASSERT( mbedtls_svc_key_id_equal(
5436 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005437 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5438 PSA_KEY_LIFETIME_PERSISTENT );
5439 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5440 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005441 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005442 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005443 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005444
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005445 /* Export the key again if permitted by the key policy. */
5446 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005447 {
Ronald Cron5425a212020-08-04 14:58:35 +02005448 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005449 second_export, export_size,
5450 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005451 ASSERT_COMPARE( first_export, first_exported_length,
5452 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005453 }
5454
5455 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005456 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005457 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005458
5459exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005460 /*
5461 * Key attributes may have been returned by psa_get_key_attributes()
5462 * thus reset them as required.
5463 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005464 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005465
Darryl Greend49a4992018-06-18 17:27:26 +01005466 mbedtls_free( first_export );
5467 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005469 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005470 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005471 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005472}
5473/* END_CASE */