blob: 163b32a907be87ed89eb60bd007e8e0901481e8e [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,
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200856 int expected_status_sign_arg,
857 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200858{
Ronald Cron5425a212020-08-04 14:58:35 +0200859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000861 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200862 psa_key_type_t key_type = key_type_arg;
863 psa_algorithm_t policy_alg = policy_alg_arg;
864 psa_algorithm_t exercise_alg = exercise_alg_arg;
865 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200866 psa_status_t status;
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200867 psa_status_t expected_status_sign = expected_status_sign_arg;
868 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200870
Gilles Peskine8817f612018-12-18 00:18:46 +0100871 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200872
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200873 psa_set_key_usage_flags( &attributes, policy_usage );
874 psa_set_key_algorithm( &attributes, policy_alg );
875 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200876
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200879
gabor-mezei-arm659af9e2021-06-29 11:06:16 +0200880 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
881 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200882
Ronald Cron5425a212020-08-04 14:58:35 +0200883 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200884 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100885
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200886 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200887
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200888 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200889 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200890 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200891
892exit:
893 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200894 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200895 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200896}
897/* END_CASE */
898
899/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200900void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901 int policy_alg,
902 int key_type,
903 data_t *key_data,
904 int exercise_alg )
905{
Ronald Cron5425a212020-08-04 14:58:35 +0200906 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000908 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200909 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 psa_status_t status;
911
Gilles Peskine8817f612018-12-18 00:18:46 +0100912 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200913
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200914 psa_set_key_usage_flags( &attributes, policy_usage );
915 psa_set_key_algorithm( &attributes, policy_alg );
916 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200917
Gilles Peskine049c7532019-05-15 20:22:09 +0200918 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200919 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200920
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200921 /* Check if no key usage flag implication is done */
922 TEST_EQUAL( policy_usage,
923 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200924
Ronald Cron5425a212020-08-04 14:58:35 +0200925 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200926 if( policy_alg == exercise_alg &&
927 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100928 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200929 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100930 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200931 psa_cipher_abort( &operation );
932
Ronald Cron5425a212020-08-04 14:58:35 +0200933 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200934 if( policy_alg == exercise_alg &&
935 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100936 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100938 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200939
940exit:
941 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200942 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200943 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200944}
945/* END_CASE */
946
947/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200948void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949 int policy_alg,
950 int key_type,
951 data_t *key_data,
952 int nonce_length_arg,
953 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100954 int exercise_alg,
955 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956{
Ronald Cron5425a212020-08-04 14:58:35 +0200957 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200958 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200959 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100961 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962 unsigned char nonce[16] = {0};
963 size_t nonce_length = nonce_length_arg;
964 unsigned char tag[16];
965 size_t tag_length = tag_length_arg;
966 size_t output_length;
967
968 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
969 TEST_ASSERT( tag_length <= sizeof( tag ) );
970
Gilles Peskine8817f612018-12-18 00:18:46 +0100971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200972
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200973 psa_set_key_usage_flags( &attributes, policy_usage );
974 psa_set_key_algorithm( &attributes, policy_alg );
975 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976
Gilles Peskine049c7532019-05-15 20:22:09 +0200977 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200978 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200980 /* Check if no key usage implication is done */
981 TEST_EQUAL( policy_usage,
982 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200983
Ronald Cron5425a212020-08-04 14:58:35 +0200984 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985 nonce, nonce_length,
986 NULL, 0,
987 NULL, 0,
988 tag, tag_length,
989 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100990 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
991 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100993 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200994
995 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200996 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997 nonce, nonce_length,
998 NULL, 0,
999 tag, tag_length,
1000 NULL, 0,
1001 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001002 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1003 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1004 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001005 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001006 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001007 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001008
1009exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001010 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001011 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012}
1013/* END_CASE */
1014
1015/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001016void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001017 int policy_alg,
1018 int key_type,
1019 data_t *key_data,
1020 int exercise_alg )
1021{
Ronald Cron5425a212020-08-04 14:58:35 +02001022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001024 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 psa_status_t status;
1026 size_t key_bits;
1027 size_t buffer_length;
1028 unsigned char *buffer = NULL;
1029 size_t output_length;
1030
Gilles Peskine8817f612018-12-18 00:18:46 +01001031 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001033 psa_set_key_usage_flags( &attributes, policy_usage );
1034 psa_set_key_algorithm( &attributes, policy_alg );
1035 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001036
Gilles Peskine049c7532019-05-15 20:22:09 +02001037 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001038 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001039
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001040 /* Check if no key usage implication is done */
1041 TEST_EQUAL( policy_usage,
1042 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001043
Ronald Cron5425a212020-08-04 14:58:35 +02001044 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001045 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001046 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1047 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001048 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001049
Ronald Cron5425a212020-08-04 14:58:35 +02001050 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001051 NULL, 0,
1052 NULL, 0,
1053 buffer, buffer_length,
1054 &output_length );
1055 if( policy_alg == exercise_alg &&
1056 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001057 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001058 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001059 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001060
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001061 if( buffer_length != 0 )
1062 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001063 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064 buffer, buffer_length,
1065 NULL, 0,
1066 buffer, buffer_length,
1067 &output_length );
1068 if( policy_alg == exercise_alg &&
1069 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001070 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001071 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001072 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001073
1074exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001075 /*
1076 * Key attributes may have been returned by psa_get_key_attributes()
1077 * thus reset them as required.
1078 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001079 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001080
1081 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001082 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083 mbedtls_free( buffer );
1084}
1085/* END_CASE */
1086
1087/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001088void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001089 int policy_alg,
1090 int key_type,
1091 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001092 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001093 int payload_length_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001094 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001095{
Ronald Cron5425a212020-08-04 14:58:35 +02001096 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001098 psa_key_usage_t policy_usage = policy_usage_arg;
1099 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001101 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1102 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1103 * compatible with the policy and `payload_length_arg` is supposed to be
1104 * a valid input length to sign. If `payload_length_arg <= 0`,
1105 * `exercise_alg` is supposed to be forbidden by the policy. */
1106 int compatible_alg = payload_length_arg > 0;
1107 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001108 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001109 size_t signature_length;
1110
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001111 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001112 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001113 TEST_EQUAL( expected_usage,
1114 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001115
Gilles Peskine8817f612018-12-18 00:18:46 +01001116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001117
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001118 psa_set_key_usage_flags( &attributes, policy_usage );
1119 psa_set_key_algorithm( &attributes, policy_alg );
1120 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001121
Gilles Peskine049c7532019-05-15 20:22:09 +02001122 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001123 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001124
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001125 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1126
Ronald Cron5425a212020-08-04 14:58:35 +02001127 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001128 payload, payload_length,
1129 signature, sizeof( signature ),
1130 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001131 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001132 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001134 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001135
1136 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001137 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001138 payload, payload_length,
1139 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001140 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001141 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001142 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001143 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001144
gabor-mezei-arm79df41d2021-06-28 14:53:49 +02001145 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1146 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001147 {
1148 status = psa_sign_message( key, exercise_alg,
1149 payload, payload_length,
1150 signature, sizeof( signature ),
1151 &signature_length );
1152 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1153 PSA_ASSERT( status );
1154 else
1155 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1156
1157 memset( signature, 0, sizeof( signature ) );
1158 status = psa_verify_message( key, exercise_alg,
1159 payload, payload_length,
1160 signature, sizeof( signature ) );
1161 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1162 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1163 else
1164 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1165 }
1166
Gilles Peskined5b33222018-06-18 22:20:03 +02001167exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001168 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001169 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001170}
1171/* END_CASE */
1172
Janos Follathba3fab92019-06-11 14:50:16 +01001173/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001174void derive_key_policy( int policy_usage,
1175 int policy_alg,
1176 int key_type,
1177 data_t *key_data,
1178 int exercise_alg )
1179{
Ronald Cron5425a212020-08-04 14:58:35 +02001180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001182 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001183 psa_status_t status;
1184
Gilles Peskine8817f612018-12-18 00:18:46 +01001185 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001186
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001187 psa_set_key_usage_flags( &attributes, policy_usage );
1188 psa_set_key_algorithm( &attributes, policy_alg );
1189 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001190
Gilles Peskine049c7532019-05-15 20:22:09 +02001191 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001192 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001193
Janos Follathba3fab92019-06-11 14:50:16 +01001194 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1195
1196 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1197 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001198 {
Janos Follathba3fab92019-06-11 14:50:16 +01001199 PSA_ASSERT( psa_key_derivation_input_bytes(
1200 &operation,
1201 PSA_KEY_DERIVATION_INPUT_SEED,
1202 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001203 }
Janos Follathba3fab92019-06-11 14:50:16 +01001204
1205 status = psa_key_derivation_input_key( &operation,
1206 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001207 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001208
Gilles Peskineea0fb492018-07-12 17:17:20 +02001209 if( policy_alg == exercise_alg &&
1210 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001211 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001212 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001213 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001214
1215exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001216 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001217 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001218 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001219}
1220/* END_CASE */
1221
1222/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001223void agreement_key_policy( int policy_usage,
1224 int policy_alg,
1225 int key_type_arg,
1226 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001227 int exercise_alg,
1228 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001229{
Ronald Cron5425a212020-08-04 14:58:35 +02001230 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001232 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001233 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001234 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001235 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001236
Gilles Peskine8817f612018-12-18 00:18:46 +01001237 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001238
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_set_key_usage_flags( &attributes, policy_usage );
1240 psa_set_key_algorithm( &attributes, policy_alg );
1241 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001242
Gilles Peskine049c7532019-05-15 20:22:09 +02001243 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001244 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001245
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001246 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001247 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001248
Steven Cooremance48e852020-10-05 16:02:45 +02001249 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001250
1251exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001252 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001253 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001254 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001255}
1256/* END_CASE */
1257
1258/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001259void key_policy_alg2( int key_type_arg, data_t *key_data,
1260 int usage_arg, int alg_arg, int alg2_arg )
1261{
Ronald Cron5425a212020-08-04 14:58:35 +02001262 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001263 psa_key_type_t key_type = key_type_arg;
1264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1265 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1266 psa_key_usage_t usage = usage_arg;
1267 psa_algorithm_t alg = alg_arg;
1268 psa_algorithm_t alg2 = alg2_arg;
1269
1270 PSA_ASSERT( psa_crypto_init( ) );
1271
1272 psa_set_key_usage_flags( &attributes, usage );
1273 psa_set_key_algorithm( &attributes, alg );
1274 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1275 psa_set_key_type( &attributes, key_type );
1276 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001277 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001278
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001279 /* Update the usage flags to obtain implicit usage flags */
1280 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001281 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001282 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1283 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1284 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1285
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001286 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001287 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001288 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001289 goto exit;
1290
1291exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001292 /*
1293 * Key attributes may have been returned by psa_get_key_attributes()
1294 * thus reset them as required.
1295 */
1296 psa_reset_key_attributes( &got_attributes );
1297
Ronald Cron5425a212020-08-04 14:58:35 +02001298 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001299 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001300}
1301/* END_CASE */
1302
1303/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001304void raw_agreement_key_policy( int policy_usage,
1305 int policy_alg,
1306 int key_type_arg,
1307 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001308 int exercise_alg,
1309 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001310{
Ronald Cron5425a212020-08-04 14:58:35 +02001311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001313 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001314 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001315 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001316 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001317
1318 PSA_ASSERT( psa_crypto_init( ) );
1319
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001320 psa_set_key_usage_flags( &attributes, policy_usage );
1321 psa_set_key_algorithm( &attributes, policy_alg );
1322 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001323
Gilles Peskine049c7532019-05-15 20:22:09 +02001324 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001325 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001326
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001327 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001328
Steven Cooremance48e852020-10-05 16:02:45 +02001329 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001330
1331exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001332 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001333 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001334 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001335}
1336/* END_CASE */
1337
1338/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001339void copy_success( int source_usage_arg,
1340 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001341 int type_arg, data_t *material,
1342 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001343 int target_usage_arg,
1344 int target_alg_arg, int target_alg2_arg,
1345 int expected_usage_arg,
1346 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001347{
Gilles Peskineca25db92019-04-19 11:43:08 +02001348 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1349 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001350 psa_key_usage_t expected_usage = expected_usage_arg;
1351 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001352 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001353 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1354 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355 uint8_t *export_buffer = NULL;
1356
Gilles Peskine57ab7212019-01-28 13:03:09 +01001357 PSA_ASSERT( psa_crypto_init( ) );
1358
Gilles Peskineca25db92019-04-19 11:43:08 +02001359 /* Prepare the source key. */
1360 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1361 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001362 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001363 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001364 PSA_ASSERT( psa_import_key( &source_attributes,
1365 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001366 &source_key ) );
1367 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001368
Gilles Peskineca25db92019-04-19 11:43:08 +02001369 /* Prepare the target attributes. */
1370 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001371 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001372 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001373 /* Set volatile lifetime to reset the key identifier to 0. */
1374 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1375 }
1376
Gilles Peskineca25db92019-04-19 11:43:08 +02001377 if( target_usage_arg != -1 )
1378 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1379 if( target_alg_arg != -1 )
1380 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001381 if( target_alg2_arg != -1 )
1382 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001383
1384 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001385 PSA_ASSERT( psa_copy_key( source_key,
1386 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001387
1388 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001389 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001390
1391 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001392 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001393 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1394 psa_get_key_type( &target_attributes ) );
1395 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1396 psa_get_key_bits( &target_attributes ) );
1397 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1398 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001399 TEST_EQUAL( expected_alg2,
1400 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001401 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1402 {
1403 size_t length;
1404 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001405 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001406 material->len, &length ) );
1407 ASSERT_COMPARE( material->x, material->len,
1408 export_buffer, length );
1409 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001410
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001411 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001412 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001413 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001414 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001415
Ronald Cron5425a212020-08-04 14:58:35 +02001416 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001417
1418exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001419 /*
1420 * Source and target key attributes may have been returned by
1421 * psa_get_key_attributes() thus reset them as required.
1422 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001423 psa_reset_key_attributes( &source_attributes );
1424 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001425
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001426 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001427 mbedtls_free( export_buffer );
1428}
1429/* END_CASE */
1430
1431/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001432void copy_fail( int source_usage_arg,
1433 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001434 int type_arg, data_t *material,
1435 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001436 int target_usage_arg,
1437 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001438 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001439 int expected_status_arg )
1440{
1441 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1442 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001443 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1444 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001445 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001446
1447 PSA_ASSERT( psa_crypto_init( ) );
1448
1449 /* Prepare the source key. */
1450 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1451 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001452 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001453 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001454 PSA_ASSERT( psa_import_key( &source_attributes,
1455 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001456 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001457
1458 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001459 psa_set_key_id( &target_attributes, key_id );
1460 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001461 psa_set_key_type( &target_attributes, target_type_arg );
1462 psa_set_key_bits( &target_attributes, target_bits_arg );
1463 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1464 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001465 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001466
1467 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001468 TEST_EQUAL( psa_copy_key( source_key,
1469 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001470 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001471
Ronald Cron5425a212020-08-04 14:58:35 +02001472 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001473
Gilles Peskine4a644642019-05-03 17:14:08 +02001474exit:
1475 psa_reset_key_attributes( &source_attributes );
1476 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001477 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001478}
1479/* END_CASE */
1480
1481/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001482void hash_operation_init( )
1483{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001484 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001485 /* Test each valid way of initializing the object, except for `= {0}`, as
1486 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1487 * though it's OK by the C standard. We could test for this, but we'd need
1488 * to supress the Clang warning for the test. */
1489 psa_hash_operation_t func = psa_hash_operation_init( );
1490 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1491 psa_hash_operation_t zero;
1492
1493 memset( &zero, 0, sizeof( zero ) );
1494
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001495 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001496 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1497 PSA_ERROR_BAD_STATE );
1498 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1499 PSA_ERROR_BAD_STATE );
1500 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1501 PSA_ERROR_BAD_STATE );
1502
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001503 /* A default hash operation should be abortable without error. */
1504 PSA_ASSERT( psa_hash_abort( &func ) );
1505 PSA_ASSERT( psa_hash_abort( &init ) );
1506 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001507}
1508/* END_CASE */
1509
1510/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001511void hash_setup( int alg_arg,
1512 int expected_status_arg )
1513{
1514 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001515 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001516 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001517 psa_status_t status;
1518
Gilles Peskine8817f612018-12-18 00:18:46 +01001519 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001520
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001521 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001522 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001523
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001524 /* Whether setup succeeded or failed, abort must succeed. */
1525 PSA_ASSERT( psa_hash_abort( &operation ) );
1526
1527 /* If setup failed, reproduce the failure, so as to
1528 * test the resulting state of the operation object. */
1529 if( status != PSA_SUCCESS )
1530 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1531
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001532 /* Now the operation object should be reusable. */
1533#if defined(KNOWN_SUPPORTED_HASH_ALG)
1534 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1535 PSA_ASSERT( psa_hash_abort( &operation ) );
1536#endif
1537
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001538exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001539 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001540}
1541/* END_CASE */
1542
1543/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001544void hash_compute_fail( int alg_arg, data_t *input,
1545 int output_size_arg, int expected_status_arg )
1546{
1547 psa_algorithm_t alg = alg_arg;
1548 uint8_t *output = NULL;
1549 size_t output_size = output_size_arg;
1550 size_t output_length = INVALID_EXPORT_LENGTH;
1551 psa_status_t expected_status = expected_status_arg;
1552 psa_status_t status;
1553
1554 ASSERT_ALLOC( output, output_size );
1555
1556 PSA_ASSERT( psa_crypto_init( ) );
1557
1558 status = psa_hash_compute( alg, input->x, input->len,
1559 output, output_size, &output_length );
1560 TEST_EQUAL( status, expected_status );
1561 TEST_ASSERT( output_length <= output_size );
1562
1563exit:
1564 mbedtls_free( output );
1565 PSA_DONE( );
1566}
1567/* END_CASE */
1568
1569/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001570void hash_compare_fail( int alg_arg, data_t *input,
1571 data_t *reference_hash,
1572 int expected_status_arg )
1573{
1574 psa_algorithm_t alg = alg_arg;
1575 psa_status_t expected_status = expected_status_arg;
1576 psa_status_t status;
1577
1578 PSA_ASSERT( psa_crypto_init( ) );
1579
1580 status = psa_hash_compare( alg, input->x, input->len,
1581 reference_hash->x, reference_hash->len );
1582 TEST_EQUAL( status, expected_status );
1583
1584exit:
1585 PSA_DONE( );
1586}
1587/* END_CASE */
1588
1589/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001590void hash_compute_compare( int alg_arg, data_t *input,
1591 data_t *expected_output )
1592{
1593 psa_algorithm_t alg = alg_arg;
1594 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1595 size_t output_length = INVALID_EXPORT_LENGTH;
1596 size_t i;
1597
1598 PSA_ASSERT( psa_crypto_init( ) );
1599
1600 /* Compute with tight buffer */
1601 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001602 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001603 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001604 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001605 ASSERT_COMPARE( output, output_length,
1606 expected_output->x, expected_output->len );
1607
1608 /* Compute with larger buffer */
1609 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1610 output, sizeof( output ),
1611 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001612 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001613 ASSERT_COMPARE( output, output_length,
1614 expected_output->x, expected_output->len );
1615
1616 /* Compare with correct hash */
1617 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1618 output, output_length ) );
1619
1620 /* Compare with trailing garbage */
1621 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1622 output, output_length + 1 ),
1623 PSA_ERROR_INVALID_SIGNATURE );
1624
1625 /* Compare with truncated hash */
1626 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1627 output, output_length - 1 ),
1628 PSA_ERROR_INVALID_SIGNATURE );
1629
1630 /* Compare with corrupted value */
1631 for( i = 0; i < output_length; i++ )
1632 {
Chris Jones9634bb12021-01-20 15:56:42 +00001633 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001634 output[i] ^= 1;
1635 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1636 output, output_length ),
1637 PSA_ERROR_INVALID_SIGNATURE );
1638 output[i] ^= 1;
1639 }
1640
1641exit:
1642 PSA_DONE( );
1643}
1644/* END_CASE */
1645
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001646/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001647void hash_bad_order( )
1648{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001649 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001650 unsigned char input[] = "";
1651 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001652 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001653 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1654 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1655 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001656 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001657 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001658 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001659
Gilles Peskine8817f612018-12-18 00:18:46 +01001660 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001661
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001662 /* Call setup twice in a row. */
1663 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001664 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001665 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1666 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001667 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001668 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001669 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001670
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001671 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001672 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001673 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001674 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001675
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001676 /* Check that update calls abort on error. */
1677 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman54f73512021-06-24 18:14:52 +01001678 operation.id = UINT_MAX;
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001679 ASSERT_OPERATION_IS_ACTIVE( operation );
1680 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1681 PSA_ERROR_BAD_STATE );
1682 ASSERT_OPERATION_IS_INACTIVE( operation );
1683 PSA_ASSERT( psa_hash_abort( &operation ) );
1684 ASSERT_OPERATION_IS_INACTIVE( operation );
1685
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001686 /* Call update after finish. */
1687 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1688 PSA_ASSERT( psa_hash_finish( &operation,
1689 hash, sizeof( hash ), &hash_len ) );
1690 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001691 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001692 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001693
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001694 /* Call verify without calling setup beforehand. */
1695 TEST_EQUAL( psa_hash_verify( &operation,
1696 valid_hash, sizeof( valid_hash ) ),
1697 PSA_ERROR_BAD_STATE );
1698 PSA_ASSERT( psa_hash_abort( &operation ) );
1699
1700 /* Call verify after finish. */
1701 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1702 PSA_ASSERT( psa_hash_finish( &operation,
1703 hash, sizeof( hash ), &hash_len ) );
1704 TEST_EQUAL( psa_hash_verify( &operation,
1705 valid_hash, sizeof( valid_hash ) ),
1706 PSA_ERROR_BAD_STATE );
1707 PSA_ASSERT( psa_hash_abort( &operation ) );
1708
1709 /* Call verify twice in a row. */
1710 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001711 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001712 PSA_ASSERT( psa_hash_verify( &operation,
1713 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001714 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001715 TEST_EQUAL( psa_hash_verify( &operation,
1716 valid_hash, sizeof( valid_hash ) ),
1717 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001718 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001719 PSA_ASSERT( psa_hash_abort( &operation ) );
1720
1721 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001722 TEST_EQUAL( psa_hash_finish( &operation,
1723 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001724 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001725 PSA_ASSERT( psa_hash_abort( &operation ) );
1726
1727 /* Call finish twice in a row. */
1728 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1729 PSA_ASSERT( psa_hash_finish( &operation,
1730 hash, sizeof( hash ), &hash_len ) );
1731 TEST_EQUAL( psa_hash_finish( &operation,
1732 hash, sizeof( hash ), &hash_len ),
1733 PSA_ERROR_BAD_STATE );
1734 PSA_ASSERT( psa_hash_abort( &operation ) );
1735
1736 /* Call finish after calling verify. */
1737 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1738 PSA_ASSERT( psa_hash_verify( &operation,
1739 valid_hash, sizeof( valid_hash ) ) );
1740 TEST_EQUAL( psa_hash_finish( &operation,
1741 hash, sizeof( hash ), &hash_len ),
1742 PSA_ERROR_BAD_STATE );
1743 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001744
1745exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001746 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001747}
1748/* END_CASE */
1749
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001750/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001751void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001752{
1753 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001754 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1755 * appended to it */
1756 unsigned char hash[] = {
1757 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1758 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1759 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001760 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001761 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001762
Gilles Peskine8817f612018-12-18 00:18:46 +01001763 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001764
itayzafrir27e69452018-11-01 14:26:34 +02001765 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001766 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001767 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001768 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001769 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001770 ASSERT_OPERATION_IS_INACTIVE( operation );
1771 PSA_ASSERT( psa_hash_abort( &operation ) );
1772 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001773
itayzafrir27e69452018-11-01 14:26:34 +02001774 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001775 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001776 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001777 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001778
itayzafrir27e69452018-11-01 14:26:34 +02001779 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001781 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001782 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001783
itayzafrirec93d302018-10-18 18:01:10 +03001784exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001785 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001786}
1787/* END_CASE */
1788
Ronald Cronee414c72021-03-18 18:50:08 +01001789/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001790void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001791{
1792 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001793 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001794 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001795 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001796 size_t hash_len;
1797
Gilles Peskine8817f612018-12-18 00:18:46 +01001798 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001799
itayzafrir58028322018-10-25 10:22:01 +03001800 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001801 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001802 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001803 hash, expected_size - 1, &hash_len ),
1804 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001805
1806exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001807 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001808}
1809/* END_CASE */
1810
Ronald Cronee414c72021-03-18 18:50:08 +01001811/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001812void hash_clone_source_state( )
1813{
1814 psa_algorithm_t alg = PSA_ALG_SHA_256;
1815 unsigned char hash[PSA_HASH_MAX_SIZE];
1816 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1817 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1818 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1819 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1820 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1821 size_t hash_len;
1822
1823 PSA_ASSERT( psa_crypto_init( ) );
1824 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1825
1826 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1827 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1828 PSA_ASSERT( psa_hash_finish( &op_finished,
1829 hash, sizeof( hash ), &hash_len ) );
1830 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1831 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1832
1833 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1834 PSA_ERROR_BAD_STATE );
1835
1836 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1837 PSA_ASSERT( psa_hash_finish( &op_init,
1838 hash, sizeof( hash ), &hash_len ) );
1839 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1840 PSA_ASSERT( psa_hash_finish( &op_finished,
1841 hash, sizeof( hash ), &hash_len ) );
1842 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1843 PSA_ASSERT( psa_hash_finish( &op_aborted,
1844 hash, sizeof( hash ), &hash_len ) );
1845
1846exit:
1847 psa_hash_abort( &op_source );
1848 psa_hash_abort( &op_init );
1849 psa_hash_abort( &op_setup );
1850 psa_hash_abort( &op_finished );
1851 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001852 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001853}
1854/* END_CASE */
1855
Ronald Cronee414c72021-03-18 18:50:08 +01001856/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001857void hash_clone_target_state( )
1858{
1859 psa_algorithm_t alg = PSA_ALG_SHA_256;
1860 unsigned char hash[PSA_HASH_MAX_SIZE];
1861 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1862 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1863 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1864 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1865 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1866 size_t hash_len;
1867
1868 PSA_ASSERT( psa_crypto_init( ) );
1869
1870 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1871 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1872 PSA_ASSERT( psa_hash_finish( &op_finished,
1873 hash, sizeof( hash ), &hash_len ) );
1874 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1875 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1876
1877 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1878 PSA_ASSERT( psa_hash_finish( &op_target,
1879 hash, sizeof( hash ), &hash_len ) );
1880
1881 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1882 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1883 PSA_ERROR_BAD_STATE );
1884 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1885 PSA_ERROR_BAD_STATE );
1886
1887exit:
1888 psa_hash_abort( &op_target );
1889 psa_hash_abort( &op_init );
1890 psa_hash_abort( &op_setup );
1891 psa_hash_abort( &op_finished );
1892 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001893 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001894}
1895/* END_CASE */
1896
itayzafrir58028322018-10-25 10:22:01 +03001897/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001898void mac_operation_init( )
1899{
Jaeden Amero252ef282019-02-15 14:05:35 +00001900 const uint8_t input[1] = { 0 };
1901
Jaeden Amero769ce272019-01-04 11:48:03 +00001902 /* Test each valid way of initializing the object, except for `= {0}`, as
1903 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1904 * though it's OK by the C standard. We could test for this, but we'd need
1905 * to supress the Clang warning for the test. */
1906 psa_mac_operation_t func = psa_mac_operation_init( );
1907 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1908 psa_mac_operation_t zero;
1909
1910 memset( &zero, 0, sizeof( zero ) );
1911
Jaeden Amero252ef282019-02-15 14:05:35 +00001912 /* A freshly-initialized MAC operation should not be usable. */
1913 TEST_EQUAL( psa_mac_update( &func,
1914 input, sizeof( input ) ),
1915 PSA_ERROR_BAD_STATE );
1916 TEST_EQUAL( psa_mac_update( &init,
1917 input, sizeof( input ) ),
1918 PSA_ERROR_BAD_STATE );
1919 TEST_EQUAL( psa_mac_update( &zero,
1920 input, sizeof( input ) ),
1921 PSA_ERROR_BAD_STATE );
1922
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001923 /* A default MAC operation should be abortable without error. */
1924 PSA_ASSERT( psa_mac_abort( &func ) );
1925 PSA_ASSERT( psa_mac_abort( &init ) );
1926 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001927}
1928/* END_CASE */
1929
1930/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001931void mac_setup( int key_type_arg,
1932 data_t *key,
1933 int alg_arg,
1934 int expected_status_arg )
1935{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001936 psa_key_type_t key_type = key_type_arg;
1937 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001938 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001939 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001940 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1941#if defined(KNOWN_SUPPORTED_MAC_ALG)
1942 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1943#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944
Gilles Peskine8817f612018-12-18 00:18:46 +01001945 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001946
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001947 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1948 &operation, &status ) )
1949 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001950 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001951
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001952 /* The operation object should be reusable. */
1953#if defined(KNOWN_SUPPORTED_MAC_ALG)
1954 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1955 smoke_test_key_data,
1956 sizeof( smoke_test_key_data ),
1957 KNOWN_SUPPORTED_MAC_ALG,
1958 &operation, &status ) )
1959 goto exit;
1960 TEST_EQUAL( status, PSA_SUCCESS );
1961#endif
1962
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001963exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001964 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001965}
1966/* END_CASE */
1967
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001968/* 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 +00001969void mac_bad_order( )
1970{
Ronald Cron5425a212020-08-04 14:58:35 +02001971 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001972 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1973 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001974 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001975 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1976 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1977 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001978 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001979 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1980 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1981 size_t sign_mac_length = 0;
1982 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1983 const uint8_t verify_mac[] = {
1984 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1985 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1986 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1987
1988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001990 psa_set_key_algorithm( &attributes, alg );
1991 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001992
Ronald Cron5425a212020-08-04 14:58:35 +02001993 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1994 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001995
Jaeden Amero252ef282019-02-15 14:05:35 +00001996 /* Call update without calling setup beforehand. */
1997 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1998 PSA_ERROR_BAD_STATE );
1999 PSA_ASSERT( psa_mac_abort( &operation ) );
2000
2001 /* Call sign finish without calling setup beforehand. */
2002 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2003 &sign_mac_length),
2004 PSA_ERROR_BAD_STATE );
2005 PSA_ASSERT( psa_mac_abort( &operation ) );
2006
2007 /* Call verify finish without calling setup beforehand. */
2008 TEST_EQUAL( psa_mac_verify_finish( &operation,
2009 verify_mac, sizeof( verify_mac ) ),
2010 PSA_ERROR_BAD_STATE );
2011 PSA_ASSERT( psa_mac_abort( &operation ) );
2012
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002013 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002014 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002015 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002016 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002017 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002018 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002019 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002020 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002021
Jaeden Amero252ef282019-02-15 14:05:35 +00002022 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002023 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002024 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2025 PSA_ASSERT( psa_mac_sign_finish( &operation,
2026 sign_mac, sizeof( sign_mac ),
2027 &sign_mac_length ) );
2028 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2029 PSA_ERROR_BAD_STATE );
2030 PSA_ASSERT( psa_mac_abort( &operation ) );
2031
2032 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002033 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002034 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2035 PSA_ASSERT( psa_mac_verify_finish( &operation,
2036 verify_mac, sizeof( verify_mac ) ) );
2037 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2038 PSA_ERROR_BAD_STATE );
2039 PSA_ASSERT( psa_mac_abort( &operation ) );
2040
2041 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002042 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002043 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2044 PSA_ASSERT( psa_mac_sign_finish( &operation,
2045 sign_mac, sizeof( sign_mac ),
2046 &sign_mac_length ) );
2047 TEST_EQUAL( psa_mac_sign_finish( &operation,
2048 sign_mac, sizeof( sign_mac ),
2049 &sign_mac_length ),
2050 PSA_ERROR_BAD_STATE );
2051 PSA_ASSERT( psa_mac_abort( &operation ) );
2052
2053 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002054 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002055 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2056 PSA_ASSERT( psa_mac_verify_finish( &operation,
2057 verify_mac, sizeof( verify_mac ) ) );
2058 TEST_EQUAL( psa_mac_verify_finish( &operation,
2059 verify_mac, sizeof( verify_mac ) ),
2060 PSA_ERROR_BAD_STATE );
2061 PSA_ASSERT( psa_mac_abort( &operation ) );
2062
2063 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002064 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002065 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002066 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002067 TEST_EQUAL( psa_mac_verify_finish( &operation,
2068 verify_mac, sizeof( verify_mac ) ),
2069 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002070 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002071 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002072 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002073
2074 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002075 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002076 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002077 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002078 TEST_EQUAL( psa_mac_sign_finish( &operation,
2079 sign_mac, sizeof( sign_mac ),
2080 &sign_mac_length ),
2081 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002082 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002083 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002084 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002085
Ronald Cron5425a212020-08-04 14:58:35 +02002086 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002087
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002088exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002089 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002090}
2091/* END_CASE */
2092
2093/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002094void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002095 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002096 int alg_arg,
2097 data_t *input,
2098 data_t *expected_mac )
2099{
Ronald Cron5425a212020-08-04 14:58:35 +02002100 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002101 psa_key_type_t key_type = key_type_arg;
2102 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002103 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002105 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002106 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002107 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002108 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002109 const size_t output_sizes_to_test[] = {
2110 0,
2111 1,
2112 expected_mac->len - 1,
2113 expected_mac->len,
2114 expected_mac->len + 1,
2115 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002116
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002117 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002118 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002119 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002120
Gilles Peskine8817f612018-12-18 00:18:46 +01002121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002122
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002123 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002124 psa_set_key_algorithm( &attributes, alg );
2125 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002126
Ronald Cron5425a212020-08-04 14:58:35 +02002127 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2128 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002129
Gilles Peskine8b356b52020-08-25 23:44:59 +02002130 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2131 {
2132 const size_t output_size = output_sizes_to_test[i];
2133 psa_status_t expected_status =
2134 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2135 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002136
Chris Jones9634bb12021-01-20 15:56:42 +00002137 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002138 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002139
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002140 /* Calculate the MAC, one-shot case. */
2141 TEST_EQUAL( psa_mac_compute( key, alg,
2142 input->x, input->len,
2143 actual_mac, output_size, &mac_length ),
2144 expected_status );
2145 if( expected_status == PSA_SUCCESS )
2146 {
2147 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2148 actual_mac, mac_length );
2149 }
2150
2151 if( output_size > 0 )
2152 memset( actual_mac, 0, output_size );
2153
2154 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002155 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002156 PSA_ASSERT( psa_mac_update( &operation,
2157 input->x, input->len ) );
2158 TEST_EQUAL( psa_mac_sign_finish( &operation,
2159 actual_mac, output_size,
2160 &mac_length ),
2161 expected_status );
2162 PSA_ASSERT( psa_mac_abort( &operation ) );
2163
2164 if( expected_status == PSA_SUCCESS )
2165 {
2166 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2167 actual_mac, mac_length );
2168 }
2169 mbedtls_free( actual_mac );
2170 actual_mac = NULL;
2171 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002172
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002173exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002174 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002176 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002177 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002178}
2179/* END_CASE */
2180
2181/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002182void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002183 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002184 int alg_arg,
2185 data_t *input,
2186 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002187{
Ronald Cron5425a212020-08-04 14:58:35 +02002188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002189 psa_key_type_t key_type = key_type_arg;
2190 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002191 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002193 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002194
Gilles Peskine69c12672018-06-28 00:07:19 +02002195 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2196
Gilles Peskine8817f612018-12-18 00:18:46 +01002197 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002199 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002200 psa_set_key_algorithm( &attributes, alg );
2201 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002202
Ronald Cron5425a212020-08-04 14:58:35 +02002203 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2204 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002205
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002206 /* Verify correct MAC, one-shot case. */
2207 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2208 expected_mac->x, expected_mac->len ) );
2209
2210 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002211 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002212 PSA_ASSERT( psa_mac_update( &operation,
2213 input->x, input->len ) );
2214 PSA_ASSERT( psa_mac_verify_finish( &operation,
2215 expected_mac->x,
2216 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002218 /* Test a MAC that's too short, one-shot case. */
2219 TEST_EQUAL( psa_mac_verify( key, alg,
2220 input->x, input->len,
2221 expected_mac->x,
2222 expected_mac->len - 1 ),
2223 PSA_ERROR_INVALID_SIGNATURE );
2224
2225 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002226 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002227 PSA_ASSERT( psa_mac_update( &operation,
2228 input->x, input->len ) );
2229 TEST_EQUAL( psa_mac_verify_finish( &operation,
2230 expected_mac->x,
2231 expected_mac->len - 1 ),
2232 PSA_ERROR_INVALID_SIGNATURE );
2233
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002234 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002235 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2236 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002237 TEST_EQUAL( psa_mac_verify( key, alg,
2238 input->x, input->len,
2239 perturbed_mac, expected_mac->len + 1 ),
2240 PSA_ERROR_INVALID_SIGNATURE );
2241
2242 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002243 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002244 PSA_ASSERT( psa_mac_update( &operation,
2245 input->x, input->len ) );
2246 TEST_EQUAL( psa_mac_verify_finish( &operation,
2247 perturbed_mac,
2248 expected_mac->len + 1 ),
2249 PSA_ERROR_INVALID_SIGNATURE );
2250
2251 /* Test changing one byte. */
2252 for( size_t i = 0; i < expected_mac->len; i++ )
2253 {
Chris Jones9634bb12021-01-20 15:56:42 +00002254 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002255 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002256
2257 TEST_EQUAL( psa_mac_verify( key, alg,
2258 input->x, input->len,
2259 perturbed_mac, expected_mac->len ),
2260 PSA_ERROR_INVALID_SIGNATURE );
2261
Ronald Cron5425a212020-08-04 14:58:35 +02002262 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002263 PSA_ASSERT( psa_mac_update( &operation,
2264 input->x, input->len ) );
2265 TEST_EQUAL( psa_mac_verify_finish( &operation,
2266 perturbed_mac,
2267 expected_mac->len ),
2268 PSA_ERROR_INVALID_SIGNATURE );
2269 perturbed_mac[i] ^= 1;
2270 }
2271
Gilles Peskine8c9def32018-02-08 10:02:12 +01002272exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002273 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002274 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002275 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002276 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002277}
2278/* END_CASE */
2279
2280/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002281void cipher_operation_init( )
2282{
Jaeden Ameroab439972019-02-15 14:12:05 +00002283 const uint8_t input[1] = { 0 };
2284 unsigned char output[1] = { 0 };
2285 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002286 /* Test each valid way of initializing the object, except for `= {0}`, as
2287 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2288 * though it's OK by the C standard. We could test for this, but we'd need
2289 * to supress the Clang warning for the test. */
2290 psa_cipher_operation_t func = psa_cipher_operation_init( );
2291 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2292 psa_cipher_operation_t zero;
2293
2294 memset( &zero, 0, sizeof( zero ) );
2295
Jaeden Ameroab439972019-02-15 14:12:05 +00002296 /* A freshly-initialized cipher operation should not be usable. */
2297 TEST_EQUAL( psa_cipher_update( &func,
2298 input, sizeof( input ),
2299 output, sizeof( output ),
2300 &output_length ),
2301 PSA_ERROR_BAD_STATE );
2302 TEST_EQUAL( psa_cipher_update( &init,
2303 input, sizeof( input ),
2304 output, sizeof( output ),
2305 &output_length ),
2306 PSA_ERROR_BAD_STATE );
2307 TEST_EQUAL( psa_cipher_update( &zero,
2308 input, sizeof( input ),
2309 output, sizeof( output ),
2310 &output_length ),
2311 PSA_ERROR_BAD_STATE );
2312
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002313 /* A default cipher operation should be abortable without error. */
2314 PSA_ASSERT( psa_cipher_abort( &func ) );
2315 PSA_ASSERT( psa_cipher_abort( &init ) );
2316 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002317}
2318/* END_CASE */
2319
2320/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002321void cipher_setup( int key_type_arg,
2322 data_t *key,
2323 int alg_arg,
2324 int expected_status_arg )
2325{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002326 psa_key_type_t key_type = key_type_arg;
2327 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002328 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002329 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002330 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002331#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002332 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2333#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334
Gilles Peskine8817f612018-12-18 00:18:46 +01002335 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002336
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002337 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2338 &operation, &status ) )
2339 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002340 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002341
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002342 /* The operation object should be reusable. */
2343#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2344 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2345 smoke_test_key_data,
2346 sizeof( smoke_test_key_data ),
2347 KNOWN_SUPPORTED_CIPHER_ALG,
2348 &operation, &status ) )
2349 goto exit;
2350 TEST_EQUAL( status, PSA_SUCCESS );
2351#endif
2352
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002353exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002354 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002355 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002356}
2357/* END_CASE */
2358
Ronald Cronee414c72021-03-18 18:50:08 +01002359/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002360void cipher_bad_order( )
2361{
Ronald Cron5425a212020-08-04 14:58:35 +02002362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002363 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2364 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002366 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002367 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002368 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002369 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2370 0xaa, 0xaa, 0xaa, 0xaa };
2371 const uint8_t text[] = {
2372 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2373 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002374 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002375 size_t length = 0;
2376
2377 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002378 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2379 psa_set_key_algorithm( &attributes, alg );
2380 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002381 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2382 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002383
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002384 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002386 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002387 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002388 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002389 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002390 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002391 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002392
2393 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002394 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002395 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002396 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002397 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002398 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002399 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002400 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002401
Jaeden Ameroab439972019-02-15 14:12:05 +00002402 /* Generate an IV without calling setup beforehand. */
2403 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2404 buffer, sizeof( buffer ),
2405 &length ),
2406 PSA_ERROR_BAD_STATE );
2407 PSA_ASSERT( psa_cipher_abort( &operation ) );
2408
2409 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002410 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002411 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2412 buffer, sizeof( buffer ),
2413 &length ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002414 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2416 buffer, sizeof( buffer ),
2417 &length ),
2418 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002419 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002420 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002421 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002422
2423 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002424 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002425 PSA_ASSERT( psa_cipher_set_iv( &operation,
2426 iv, sizeof( iv ) ) );
2427 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2428 buffer, sizeof( buffer ),
2429 &length ),
2430 PSA_ERROR_BAD_STATE );
2431 PSA_ASSERT( psa_cipher_abort( &operation ) );
2432
2433 /* Set an IV without calling setup beforehand. */
2434 TEST_EQUAL( psa_cipher_set_iv( &operation,
2435 iv, sizeof( iv ) ),
2436 PSA_ERROR_BAD_STATE );
2437 PSA_ASSERT( psa_cipher_abort( &operation ) );
2438
2439 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002440 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002441 PSA_ASSERT( psa_cipher_set_iv( &operation,
2442 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002443 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002444 TEST_EQUAL( psa_cipher_set_iv( &operation,
2445 iv, sizeof( iv ) ),
2446 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002447 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002448 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002449 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002450
2451 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002452 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002453 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2454 buffer, sizeof( buffer ),
2455 &length ) );
2456 TEST_EQUAL( psa_cipher_set_iv( &operation,
2457 iv, sizeof( iv ) ),
2458 PSA_ERROR_BAD_STATE );
2459 PSA_ASSERT( psa_cipher_abort( &operation ) );
2460
2461 /* Call update without calling setup beforehand. */
2462 TEST_EQUAL( psa_cipher_update( &operation,
2463 text, sizeof( text ),
2464 buffer, sizeof( buffer ),
2465 &length ),
2466 PSA_ERROR_BAD_STATE );
2467 PSA_ASSERT( psa_cipher_abort( &operation ) );
2468
2469 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002470 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002471 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002472 TEST_EQUAL( psa_cipher_update( &operation,
2473 text, sizeof( text ),
2474 buffer, sizeof( buffer ),
2475 &length ),
2476 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002477 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002478 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002479 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002480
2481 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002482 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002483 PSA_ASSERT( psa_cipher_set_iv( &operation,
2484 iv, sizeof( iv ) ) );
2485 PSA_ASSERT( psa_cipher_finish( &operation,
2486 buffer, sizeof( buffer ), &length ) );
2487 TEST_EQUAL( psa_cipher_update( &operation,
2488 text, sizeof( text ),
2489 buffer, sizeof( buffer ),
2490 &length ),
2491 PSA_ERROR_BAD_STATE );
2492 PSA_ASSERT( psa_cipher_abort( &operation ) );
2493
2494 /* Call finish without calling setup beforehand. */
2495 TEST_EQUAL( psa_cipher_finish( &operation,
2496 buffer, sizeof( buffer ), &length ),
2497 PSA_ERROR_BAD_STATE );
2498 PSA_ASSERT( psa_cipher_abort( &operation ) );
2499
2500 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002501 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002502 /* Not calling update means we are encrypting an empty buffer, which is OK
2503 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002504 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002505 TEST_EQUAL( psa_cipher_finish( &operation,
2506 buffer, sizeof( buffer ), &length ),
2507 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002508 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002509 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002510 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002511
2512 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002513 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002514 PSA_ASSERT( psa_cipher_set_iv( &operation,
2515 iv, sizeof( iv ) ) );
2516 PSA_ASSERT( psa_cipher_finish( &operation,
2517 buffer, sizeof( buffer ), &length ) );
2518 TEST_EQUAL( psa_cipher_finish( &operation,
2519 buffer, sizeof( buffer ), &length ),
2520 PSA_ERROR_BAD_STATE );
2521 PSA_ASSERT( psa_cipher_abort( &operation ) );
2522
Ronald Cron5425a212020-08-04 14:58:35 +02002523 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002524
Jaeden Ameroab439972019-02-15 14:12:05 +00002525exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002526 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002527 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002528}
2529/* END_CASE */
2530
2531/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002532void cipher_encrypt_fail( int alg_arg,
2533 int key_type_arg,
2534 data_t *key_data,
2535 data_t *input,
2536 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002537{
Ronald Cron5425a212020-08-04 14:58:35 +02002538 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002539 psa_status_t status;
2540 psa_key_type_t key_type = key_type_arg;
2541 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002542 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002543 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002544 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002545 size_t output_length = 0;
2546 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2547
2548 if ( PSA_ERROR_BAD_STATE != expected_status )
2549 {
2550 PSA_ASSERT( psa_crypto_init( ) );
2551
2552 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2553 psa_set_key_algorithm( &attributes, alg );
2554 psa_set_key_type( &attributes, key_type );
2555
2556 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2557 input->len );
2558 ASSERT_ALLOC( output, output_buffer_size );
2559
2560 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2561 &key ) );
2562 }
2563
2564 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2565 output_buffer_size, &output_length );
2566
2567 TEST_EQUAL( status, expected_status );
2568
2569exit:
2570 mbedtls_free( output );
2571 psa_destroy_key( key );
2572 PSA_DONE( );
2573}
2574/* END_CASE */
2575
2576/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002577void cipher_encrypt_alg_without_iv( int alg_arg,
2578 int key_type_arg,
2579 data_t *key_data,
2580 data_t *input,
2581 data_t *expected_output )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002582{
2583 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2584 psa_key_type_t key_type = key_type_arg;
2585 psa_algorithm_t alg = alg_arg;
2586 unsigned char *output = NULL;
2587 size_t output_buffer_size = 0;
2588 size_t output_length = 0;
2589 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2590
2591 PSA_ASSERT( psa_crypto_init( ) );
2592
2593 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2594 psa_set_key_algorithm( &attributes, alg );
2595 psa_set_key_type( &attributes, key_type );
2596
2597 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2598 ASSERT_ALLOC( output, output_buffer_size );
2599
2600 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2601 &key ) );
2602
2603 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2604 output_buffer_size, &output_length ) );
2605 TEST_ASSERT( output_length <=
2606 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2607 TEST_ASSERT( output_length <=
2608 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2609
2610 ASSERT_COMPARE( expected_output->x, expected_output->len,
2611 output, output_length );
2612exit:
2613 mbedtls_free( output );
2614 psa_destroy_key( key );
2615 PSA_DONE( );
2616}
2617/* END_CASE */
2618
2619/* BEGIN_CASE */
Paul Elliotted33ef12021-07-14 12:31:21 +01002620void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2621{
2622 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2623 psa_algorithm_t alg = alg_arg;
2624 psa_key_type_t key_type = key_type_arg;
2625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2626 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2627 psa_status_t status;
2628
2629 PSA_ASSERT( psa_crypto_init( ) );
2630
2631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2632 psa_set_key_algorithm( &attributes, alg );
2633 psa_set_key_type( &attributes, key_type );
2634
2635 /* Usage of either of these two size macros would cause divide by zero
2636 * with incorrect key types previously. Input length should be irrelevant
2637 * here. */
2638 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2639 0 );
2640 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2641
2642
2643 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2644 &key ) );
2645
2646 /* Should fail due to invalid alg type (to support invalid key type).
2647 * Encrypt or decrypt will end up in the same place. */
2648 status = psa_cipher_encrypt_setup( &operation, key, alg );
2649
2650 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2651
2652exit:
2653 psa_cipher_abort( &operation );
2654 psa_destroy_key( key );
2655 PSA_DONE( );
2656}
2657/* END_CASE */
2658
2659/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002660void cipher_encrypt_validation( int alg_arg,
2661 int key_type_arg,
2662 data_t *key_data,
2663 data_t *input )
2664{
2665 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2666 psa_key_type_t key_type = key_type_arg;
2667 psa_algorithm_t alg = alg_arg;
2668 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2669 unsigned char *output1 = NULL;
2670 size_t output1_buffer_size = 0;
2671 size_t output1_length = 0;
2672 unsigned char *output2 = NULL;
2673 size_t output2_buffer_size = 0;
2674 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002675 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002676 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
Gilles Peskine8817f612018-12-18 00:18:46 +01002679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2682 psa_set_key_algorithm( &attributes, alg );
2683 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002684
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002685 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2686 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2687 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2688 ASSERT_ALLOC( output1, output1_buffer_size );
2689 ASSERT_ALLOC( output2, output2_buffer_size );
2690
Ronald Cron5425a212020-08-04 14:58:35 +02002691 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2692 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002693
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002694 /* The one-shot cipher encryption uses generated iv so validating
2695 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002696 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2697 output1_buffer_size, &output1_length ) );
2698 TEST_ASSERT( output1_length <=
2699 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2700 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002701 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002702
2703 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2704 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705
Gilles Peskine8817f612018-12-18 00:18:46 +01002706 PSA_ASSERT( psa_cipher_update( &operation,
2707 input->x, input->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002708 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002709 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002710 TEST_ASSERT( function_output_length <=
2711 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2712 TEST_ASSERT( function_output_length <=
2713 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002714 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002715
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002716 PSA_ASSERT( psa_cipher_finish( &operation,
2717 output2 + output2_length,
2718 output2_buffer_size - output2_length,
2719 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002720 TEST_ASSERT( function_output_length <=
2721 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2722 TEST_ASSERT( function_output_length <=
2723 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002724 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002725
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002726 PSA_ASSERT( psa_cipher_abort( &operation ) );
2727 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2728 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002729
Gilles Peskine50e586b2018-06-08 14:28:46 +02002730exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002731 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002732 mbedtls_free( output1 );
2733 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002734 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002735 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736}
2737/* END_CASE */
2738
2739/* BEGIN_CASE */
2740void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002741 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002742 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002743 int first_part_size_arg,
2744 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002745 data_t *expected_output,
2746 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747{
Ronald Cron5425a212020-08-04 14:58:35 +02002748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749 psa_key_type_t key_type = key_type_arg;
2750 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002751 psa_status_t status;
2752 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002753 size_t first_part_size = first_part_size_arg;
2754 size_t output1_length = output1_length_arg;
2755 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002756 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757 size_t output_buffer_size = 0;
2758 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002759 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002760 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002762
Gilles Peskine8817f612018-12-18 00:18:46 +01002763 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002764
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002765 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2766 psa_set_key_algorithm( &attributes, alg );
2767 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002768
Ronald Cron5425a212020-08-04 14:58:35 +02002769 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2770 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002771
Ronald Cron5425a212020-08-04 14:58:35 +02002772 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002773
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002774 if( iv->len > 0 )
2775 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002776 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002777 }
2778
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002779 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2780 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002781 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002782
Gilles Peskinee0866522019-02-19 19:44:00 +01002783 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2785 output, output_buffer_size,
2786 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002787 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002788 TEST_ASSERT( function_output_length <=
2789 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2790 TEST_ASSERT( function_output_length <=
2791 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002792 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002793
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002794 if( first_part_size < input->len )
2795 {
2796 PSA_ASSERT( psa_cipher_update( &operation,
2797 input->x + first_part_size,
2798 input->len - first_part_size,
2799 ( output_buffer_size == 0 ? NULL :
2800 output + total_output_length ),
2801 output_buffer_size - total_output_length,
2802 &function_output_length ) );
2803 TEST_ASSERT( function_output_length == output2_length );
2804 TEST_ASSERT( function_output_length <=
2805 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2806 alg,
2807 input->len - first_part_size ) );
2808 TEST_ASSERT( function_output_length <=
2809 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2810 total_output_length += function_output_length;
2811 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002812
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002813 status = psa_cipher_finish( &operation,
2814 ( output_buffer_size == 0 ? NULL :
2815 output + total_output_length ),
2816 output_buffer_size - total_output_length,
2817 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002818 TEST_ASSERT( function_output_length <=
2819 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2820 TEST_ASSERT( function_output_length <=
2821 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002822 total_output_length += function_output_length;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002823 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002824
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002825 if( expected_status == PSA_SUCCESS )
2826 {
2827 PSA_ASSERT( psa_cipher_abort( &operation ) );
2828
2829 ASSERT_COMPARE( expected_output->x, expected_output->len,
2830 output, total_output_length );
2831 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832
2833exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002834 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002835 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002836 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002837 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002838}
2839/* END_CASE */
2840
2841/* BEGIN_CASE */
2842void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002843 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002844 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002845 int first_part_size_arg,
2846 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002847 data_t *expected_output,
2848 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849{
Ronald Cron5425a212020-08-04 14:58:35 +02002850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002851 psa_key_type_t key_type = key_type_arg;
2852 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002853 psa_status_t status;
2854 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002855 size_t first_part_size = first_part_size_arg;
2856 size_t output1_length = output1_length_arg;
2857 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002858 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002859 size_t output_buffer_size = 0;
2860 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002861 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002862 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002863 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002864
Gilles Peskine8817f612018-12-18 00:18:46 +01002865 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002866
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002867 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2868 psa_set_key_algorithm( &attributes, alg );
2869 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002870
Ronald Cron5425a212020-08-04 14:58:35 +02002871 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2872 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002873
Ronald Cron5425a212020-08-04 14:58:35 +02002874 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875
Steven Cooreman177deba2020-09-07 17:14:14 +02002876 if( iv->len > 0 )
2877 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002878 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002879 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002880
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002881 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2882 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002883 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884
Gilles Peskinee0866522019-02-19 19:44:00 +01002885 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002886 PSA_ASSERT( psa_cipher_update( &operation,
2887 input->x, first_part_size,
2888 output, output_buffer_size,
2889 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002890 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002891 TEST_ASSERT( function_output_length <=
2892 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2893 TEST_ASSERT( function_output_length <=
2894 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002895 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002896
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002897 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002898 {
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002899 PSA_ASSERT( psa_cipher_update( &operation,
2900 input->x + first_part_size,
2901 input->len - first_part_size,
2902 ( output_buffer_size == 0 ? NULL :
2903 output + total_output_length ),
2904 output_buffer_size - total_output_length,
2905 &function_output_length ) );
2906 TEST_ASSERT( function_output_length == output2_length );
2907 TEST_ASSERT( function_output_length <=
2908 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2909 alg,
2910 input->len - first_part_size ) );
2911 TEST_ASSERT( function_output_length <=
2912 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2913 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002914 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915
Gilles Peskine50e586b2018-06-08 14:28:46 +02002916 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002917 ( output_buffer_size == 0 ? NULL :
2918 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002919 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002921 TEST_ASSERT( function_output_length <=
2922 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2923 TEST_ASSERT( function_output_length <=
2924 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002925 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002926 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927
2928 if( expected_status == PSA_SUCCESS )
2929 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002930 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002931
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002932 ASSERT_COMPARE( expected_output->x, expected_output->len,
2933 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934 }
2935
Gilles Peskine50e586b2018-06-08 14:28:46 +02002936exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002937 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002938 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002939 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002940 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002941}
2942/* END_CASE */
2943
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002945void cipher_decrypt_fail( int alg_arg,
2946 int key_type_arg,
2947 data_t *key_data,
2948 data_t *iv,
2949 data_t *input_arg,
2950 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002951{
2952 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2953 psa_status_t status;
2954 psa_key_type_t key_type = key_type_arg;
2955 psa_algorithm_t alg = alg_arg;
2956 psa_status_t expected_status = expected_status_arg;
2957 unsigned char *input = NULL;
2958 size_t input_buffer_size = 0;
2959 unsigned char *output = NULL;
2960 size_t output_buffer_size = 0;
2961 size_t output_length = 0;
2962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2963
2964 if ( PSA_ERROR_BAD_STATE != expected_status )
2965 {
2966 PSA_ASSERT( psa_crypto_init( ) );
2967
2968 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2969 psa_set_key_algorithm( &attributes, alg );
2970 psa_set_key_type( &attributes, key_type );
2971
2972 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2973 &key ) );
2974 }
2975
2976 /* Allocate input buffer and copy the iv and the plaintext */
2977 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2978 if ( input_buffer_size > 0 )
2979 {
2980 ASSERT_ALLOC( input, input_buffer_size );
2981 memcpy( input, iv->x, iv->len );
2982 memcpy( input + iv->len, input_arg->x, input_arg->len );
2983 }
2984
2985 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2986 ASSERT_ALLOC( output, output_buffer_size );
2987
2988 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2989 output_buffer_size, &output_length );
2990 TEST_EQUAL( status, expected_status );
2991
2992exit:
2993 mbedtls_free( input );
2994 mbedtls_free( output );
2995 psa_destroy_key( key );
2996 PSA_DONE( );
2997}
2998/* END_CASE */
2999
3000/* BEGIN_CASE */
3001void cipher_decrypt( int alg_arg,
3002 int key_type_arg,
3003 data_t *key_data,
3004 data_t *iv,
3005 data_t *input_arg,
3006 data_t *expected_output )
3007{
3008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3009 psa_key_type_t key_type = key_type_arg;
3010 psa_algorithm_t alg = alg_arg;
3011 unsigned char *input = NULL;
3012 size_t input_buffer_size = 0;
3013 unsigned char *output = NULL;
3014 size_t output_buffer_size = 0;
3015 size_t output_length = 0;
3016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3017
3018 PSA_ASSERT( psa_crypto_init( ) );
3019
3020 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3021 psa_set_key_algorithm( &attributes, alg );
3022 psa_set_key_type( &attributes, key_type );
3023
3024 /* Allocate input buffer and copy the iv and the plaintext */
3025 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3026 if ( input_buffer_size > 0 )
3027 {
3028 ASSERT_ALLOC( input, input_buffer_size );
3029 memcpy( input, iv->x, iv->len );
3030 memcpy( input + iv->len, input_arg->x, input_arg->len );
3031 }
3032
3033 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3034 ASSERT_ALLOC( output, output_buffer_size );
3035
3036 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3037 &key ) );
3038
3039 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3040 output_buffer_size, &output_length ) );
3041 TEST_ASSERT( output_length <=
3042 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3043 TEST_ASSERT( output_length <=
3044 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3045
3046 ASSERT_COMPARE( expected_output->x, expected_output->len,
3047 output, output_length );
3048exit:
3049 mbedtls_free( input );
3050 mbedtls_free( output );
3051 psa_destroy_key( key );
3052 PSA_DONE( );
3053}
3054/* END_CASE */
3055
3056/* BEGIN_CASE */
3057void cipher_verify_output( int alg_arg,
3058 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003059 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003060 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003061{
Ronald Cron5425a212020-08-04 14:58:35 +02003062 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003063 psa_key_type_t key_type = key_type_arg;
3064 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003065 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003066 size_t output1_size = 0;
3067 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003068 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003069 size_t output2_size = 0;
3070 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003072
Gilles Peskine8817f612018-12-18 00:18:46 +01003073 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003074
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003075 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3076 psa_set_key_algorithm( &attributes, alg );
3077 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003078
Ronald Cron5425a212020-08-04 14:58:35 +02003079 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3080 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003081 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003082 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003083
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003084 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3085 output1, output1_size,
3086 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003087 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003088 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003089 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003090 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003091
3092 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003093 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003094
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003095 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3096 output2, output2_size,
3097 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003098 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003099 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003100 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003101 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003102
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003103 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003104
3105exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003106 mbedtls_free( output1 );
3107 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003108 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003109 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003110}
3111/* END_CASE */
3112
3113/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003114void cipher_verify_output_multipart( int alg_arg,
3115 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003116 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003117 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003118 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003119{
Ronald Cron5425a212020-08-04 14:58:35 +02003120 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003121 psa_key_type_t key_type = key_type_arg;
3122 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003123 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003124 unsigned char iv[16] = {0};
3125 size_t iv_size = 16;
3126 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003127 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003128 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003129 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003130 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003131 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003132 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003133 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003134 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3135 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003137
Gilles Peskine8817f612018-12-18 00:18:46 +01003138 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003139
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3141 psa_set_key_algorithm( &attributes, alg );
3142 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003143
Ronald Cron5425a212020-08-04 14:58:35 +02003144 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3145 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003146
Ronald Cron5425a212020-08-04 14:58:35 +02003147 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3148 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003149
Steven Cooreman177deba2020-09-07 17:14:14 +02003150 if( alg != PSA_ALG_ECB_NO_PADDING )
3151 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003152 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3153 iv, iv_size,
3154 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003155 }
3156
gabor-mezei-armceface22021-01-21 12:26:17 +01003157 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3158 TEST_ASSERT( output1_buffer_size <=
3159 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003160 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003161
Gilles Peskinee0866522019-02-19 19:44:00 +01003162 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3165 output1, output1_buffer_size,
3166 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003167 TEST_ASSERT( function_output_length <=
3168 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3169 TEST_ASSERT( function_output_length <=
3170 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003171 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003172
Gilles Peskine8817f612018-12-18 00:18:46 +01003173 PSA_ASSERT( psa_cipher_update( &operation1,
3174 input->x + first_part_size,
3175 input->len - first_part_size,
3176 output1, output1_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,
3180 alg,
3181 input->len - first_part_size ) );
3182 TEST_ASSERT( function_output_length <=
3183 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003184 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_cipher_finish( &operation1,
3187 output1 + output1_length,
3188 output1_buffer_size - output1_length,
3189 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003190 TEST_ASSERT( function_output_length <=
3191 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3192 TEST_ASSERT( function_output_length <=
3193 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003194 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003195
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003197
Gilles Peskine048b7f02018-06-08 14:20:49 +02003198 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003199 TEST_ASSERT( output2_buffer_size <=
3200 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3201 TEST_ASSERT( output2_buffer_size <=
3202 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003203 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003204
Steven Cooreman177deba2020-09-07 17:14:14 +02003205 if( iv_length > 0 )
3206 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003207 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3208 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003209 }
Moran Pekerded84402018-06-06 16:36:50 +03003210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3212 output2, output2_buffer_size,
3213 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003214 TEST_ASSERT( function_output_length <=
3215 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3216 TEST_ASSERT( function_output_length <=
3217 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003218 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_cipher_update( &operation2,
3221 output1 + first_part_size,
3222 output1_length - first_part_size,
3223 output2, output2_buffer_size,
3224 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003225 TEST_ASSERT( function_output_length <=
3226 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3227 alg,
3228 output1_length - first_part_size ) );
3229 TEST_ASSERT( function_output_length <=
3230 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003231 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003232
Gilles Peskine8817f612018-12-18 00:18:46 +01003233 PSA_ASSERT( psa_cipher_finish( &operation2,
3234 output2 + output2_length,
3235 output2_buffer_size - output2_length,
3236 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003237 TEST_ASSERT( function_output_length <=
3238 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3239 TEST_ASSERT( function_output_length <=
3240 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003241 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003242
Gilles Peskine8817f612018-12-18 00:18:46 +01003243 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003244
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003245 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003246
3247exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003248 psa_cipher_abort( &operation1 );
3249 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003250 mbedtls_free( output1 );
3251 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003252 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003253 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003254}
3255/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003256
Gilles Peskine20035e32018-02-03 22:44:14 +01003257/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003258void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003259 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003260 data_t *nonce,
3261 data_t *additional_data,
3262 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003263 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003264{
Ronald Cron5425a212020-08-04 14:58:35 +02003265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003266 psa_key_type_t key_type = key_type_arg;
3267 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003268 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003269 unsigned char *output_data = NULL;
3270 size_t output_size = 0;
3271 size_t output_length = 0;
3272 unsigned char *output_data2 = NULL;
3273 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003274 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003275 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003277
Gilles Peskine8817f612018-12-18 00:18:46 +01003278 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003279
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003280 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3281 psa_set_key_algorithm( &attributes, alg );
3282 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003283
Gilles Peskine049c7532019-05-15 20:22:09 +02003284 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003285 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003286 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3287 key_bits = psa_get_key_bits( &attributes );
3288
3289 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3290 alg );
3291 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3292 * should be exact. */
3293 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3294 expected_result != PSA_ERROR_NOT_SUPPORTED )
3295 {
3296 TEST_EQUAL( output_size,
3297 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3298 TEST_ASSERT( output_size <=
3299 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3300 }
3301 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003302
Steven Cooremanf49478b2021-02-15 15:19:25 +01003303 status = psa_aead_encrypt( key, alg,
3304 nonce->x, nonce->len,
3305 additional_data->x,
3306 additional_data->len,
3307 input_data->x, input_data->len,
3308 output_data, output_size,
3309 &output_length );
3310
3311 /* If the operation is not supported, just skip and not fail in case the
3312 * encryption involves a common limitation of cryptography hardwares and
3313 * an alternative implementation. */
3314 if( status == PSA_ERROR_NOT_SUPPORTED )
3315 {
3316 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3317 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3318 }
3319
3320 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003321
3322 if( PSA_SUCCESS == expected_result )
3323 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003324 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003325
Gilles Peskine003a4a92019-05-14 16:09:40 +02003326 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3327 * should be exact. */
3328 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003329 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003330
gabor-mezei-armceface22021-01-21 12:26:17 +01003331 TEST_ASSERT( input_data->len <=
3332 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3333
Ronald Cron5425a212020-08-04 14:58:35 +02003334 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003335 nonce->x, nonce->len,
3336 additional_data->x,
3337 additional_data->len,
3338 output_data, output_length,
3339 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003340 &output_length2 ),
3341 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003342
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003343 ASSERT_COMPARE( input_data->x, input_data->len,
3344 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003345 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003346
Gilles Peskinea1cac842018-06-11 19:33:02 +02003347exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003348 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349 mbedtls_free( output_data );
3350 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003351 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003352}
3353/* END_CASE */
3354
3355/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003356void aead_encrypt( int key_type_arg, data_t *key_data,
3357 int alg_arg,
3358 data_t *nonce,
3359 data_t *additional_data,
3360 data_t *input_data,
3361 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362{
Ronald Cron5425a212020-08-04 14:58:35 +02003363 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364 psa_key_type_t key_type = key_type_arg;
3365 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003366 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003367 unsigned char *output_data = NULL;
3368 size_t output_size = 0;
3369 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003371 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003372
Gilles Peskine8817f612018-12-18 00:18:46 +01003373 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003375 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3376 psa_set_key_algorithm( &attributes, alg );
3377 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003378
Gilles Peskine049c7532019-05-15 20:22:09 +02003379 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003380 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003381 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3382 key_bits = psa_get_key_bits( &attributes );
3383
3384 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3385 alg );
3386 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3387 * should be exact. */
3388 TEST_EQUAL( output_size,
3389 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3390 TEST_ASSERT( output_size <=
3391 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3392 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393
Steven Cooremand588ea12021-01-11 19:36:04 +01003394 status = psa_aead_encrypt( key, alg,
3395 nonce->x, nonce->len,
3396 additional_data->x, additional_data->len,
3397 input_data->x, input_data->len,
3398 output_data, output_size,
3399 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400
Ronald Cron28a45ed2021-02-09 20:35:42 +01003401 /* If the operation is not supported, just skip and not fail in case the
3402 * encryption involves a common limitation of cryptography hardwares and
3403 * an alternative implementation. */
3404 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003405 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003406 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3407 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003408 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003409
3410 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003411 ASSERT_COMPARE( expected_result->x, expected_result->len,
3412 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003413
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003415 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003417 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418}
3419/* END_CASE */
3420
3421/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003422void aead_decrypt( int key_type_arg, data_t *key_data,
3423 int alg_arg,
3424 data_t *nonce,
3425 data_t *additional_data,
3426 data_t *input_data,
3427 data_t *expected_data,
3428 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003429{
Ronald Cron5425a212020-08-04 14:58:35 +02003430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003431 psa_key_type_t key_type = key_type_arg;
3432 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003433 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434 unsigned char *output_data = NULL;
3435 size_t output_size = 0;
3436 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003438 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003439 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003440
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3444 psa_set_key_algorithm( &attributes, alg );
3445 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446
Gilles Peskine049c7532019-05-15 20:22:09 +02003447 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003448 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003449 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3450 key_bits = psa_get_key_bits( &attributes );
3451
3452 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3453 alg );
3454 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3455 expected_result != PSA_ERROR_NOT_SUPPORTED )
3456 {
3457 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3458 * should be exact. */
3459 TEST_EQUAL( output_size,
3460 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3461 TEST_ASSERT( output_size <=
3462 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3463 }
3464 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465
Steven Cooremand588ea12021-01-11 19:36:04 +01003466 status = psa_aead_decrypt( key, alg,
3467 nonce->x, nonce->len,
3468 additional_data->x,
3469 additional_data->len,
3470 input_data->x, input_data->len,
3471 output_data, output_size,
3472 &output_length );
3473
Ronald Cron28a45ed2021-02-09 20:35:42 +01003474 /* If the operation is not supported, just skip and not fail in case the
3475 * decryption involves a common limitation of cryptography hardwares and
3476 * an alternative implementation. */
3477 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003478 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003479 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3480 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003481 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003482
3483 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003484
Gilles Peskine2d277862018-06-18 15:41:12 +02003485 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003486 ASSERT_COMPARE( expected_data->x, expected_data->len,
3487 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003488
Gilles Peskinea1cac842018-06-11 19:33:02 +02003489exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003490 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003491 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003492 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003493}
3494/* END_CASE */
3495
3496/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003497void signature_size( int type_arg,
3498 int bits,
3499 int alg_arg,
3500 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003501{
3502 psa_key_type_t type = type_arg;
3503 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003504 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003505
Gilles Peskinefe11b722018-12-18 00:24:04 +01003506 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003507#if defined(MBEDTLS_TEST_DEPRECATED)
3508 TEST_EQUAL( actual_size,
3509 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3510#endif /* MBEDTLS_TEST_DEPRECATED */
3511
Gilles Peskinee59236f2018-01-27 23:32:46 +01003512exit:
3513 ;
3514}
3515/* END_CASE */
3516
3517/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003518void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3519 int alg_arg, data_t *input_data,
3520 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003521{
Ronald Cron5425a212020-08-04 14:58:35 +02003522 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003523 psa_key_type_t key_type = key_type_arg;
3524 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003525 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003526 unsigned char *signature = NULL;
3527 size_t signature_size;
3528 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003530
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003532
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003533 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003534 psa_set_key_algorithm( &attributes, alg );
3535 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003536
Gilles Peskine049c7532019-05-15 20:22:09 +02003537 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003538 &key ) );
3539 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003540 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003541
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003542 /* Allocate a buffer which has the size advertized by the
3543 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003544 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003545 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003546 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003547 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003548 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003549
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003550 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003551 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003552 input_data->x, input_data->len,
3553 signature, signature_size,
3554 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003555 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003556 ASSERT_COMPARE( output_data->x, output_data->len,
3557 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003558
Gilles Peskine0627f982019-11-26 19:12:16 +01003559#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003560 memset( signature, 0, signature_size );
3561 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003562 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003563 input_data->x, input_data->len,
3564 signature, signature_size,
3565 &signature_length ) );
3566 ASSERT_COMPARE( output_data->x, output_data->len,
3567 signature, signature_length );
3568#endif /* MBEDTLS_TEST_DEPRECATED */
3569
Gilles Peskine20035e32018-02-03 22:44:14 +01003570exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003571 /*
3572 * Key attributes may have been returned by psa_get_key_attributes()
3573 * thus reset them as required.
3574 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003575 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003576
Ronald Cron5425a212020-08-04 14:58:35 +02003577 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003578 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003579 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003580}
3581/* END_CASE */
3582
3583/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003584void sign_hash_fail( int key_type_arg, data_t *key_data,
3585 int alg_arg, data_t *input_data,
3586 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003587{
Ronald Cron5425a212020-08-04 14:58:35 +02003588 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003589 psa_key_type_t key_type = key_type_arg;
3590 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003591 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003592 psa_status_t actual_status;
3593 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003594 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003595 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003597
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003598 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003599
Gilles Peskine8817f612018-12-18 00:18:46 +01003600 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003601
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003602 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003603 psa_set_key_algorithm( &attributes, alg );
3604 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003605
Gilles Peskine049c7532019-05-15 20:22:09 +02003606 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003607 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003608
Ronald Cron5425a212020-08-04 14:58:35 +02003609 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003610 input_data->x, input_data->len,
3611 signature, signature_size,
3612 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003613 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003614 /* The value of *signature_length is unspecified on error, but
3615 * whatever it is, it should be less than signature_size, so that
3616 * if the caller tries to read *signature_length bytes without
3617 * checking the error code then they don't overflow a buffer. */
3618 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003619
Gilles Peskine895242b2019-11-29 12:15:40 +01003620#if defined(MBEDTLS_TEST_DEPRECATED)
3621 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003622 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003623 input_data->x, input_data->len,
3624 signature, signature_size,
3625 &signature_length ),
3626 expected_status );
3627 TEST_ASSERT( signature_length <= signature_size );
3628#endif /* MBEDTLS_TEST_DEPRECATED */
3629
Gilles Peskine20035e32018-02-03 22:44:14 +01003630exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003631 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003632 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003633 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003634 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003635}
3636/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003637
3638/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003639void sign_verify_hash( int key_type_arg, data_t *key_data,
3640 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003641{
Ronald Cron5425a212020-08-04 14:58:35 +02003642 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003643 psa_key_type_t key_type = key_type_arg;
3644 psa_algorithm_t alg = alg_arg;
3645 size_t key_bits;
3646 unsigned char *signature = NULL;
3647 size_t signature_size;
3648 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003650
Gilles Peskine8817f612018-12-18 00:18:46 +01003651 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003652
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003653 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003654 psa_set_key_algorithm( &attributes, alg );
3655 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003656
Gilles Peskine049c7532019-05-15 20:22:09 +02003657 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003658 &key ) );
3659 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003660 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003661
3662 /* Allocate a buffer which has the size advertized by the
3663 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003664 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003665 key_bits, alg );
3666 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003667 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003668 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003669
3670 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003671 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003672 input_data->x, input_data->len,
3673 signature, signature_size,
3674 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003675 /* Check that the signature length looks sensible. */
3676 TEST_ASSERT( signature_length <= signature_size );
3677 TEST_ASSERT( signature_length > 0 );
3678
3679 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003680 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003681 input_data->x, input_data->len,
3682 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003683
3684 if( input_data->len != 0 )
3685 {
3686 /* Flip a bit in the input and verify that the signature is now
3687 * detected as invalid. Flip a bit at the beginning, not at the end,
3688 * because ECDSA may ignore the last few bits of the input. */
3689 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003690 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003691 input_data->x, input_data->len,
3692 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003693 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003694 }
3695
3696exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003697 /*
3698 * Key attributes may have been returned by psa_get_key_attributes()
3699 * thus reset them as required.
3700 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003701 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003702
Ronald Cron5425a212020-08-04 14:58:35 +02003703 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003704 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003705 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003706}
3707/* END_CASE */
3708
3709/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003710void verify_hash( int key_type_arg, data_t *key_data,
3711 int alg_arg, data_t *hash_data,
3712 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003713{
Ronald Cron5425a212020-08-04 14:58:35 +02003714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003715 psa_key_type_t key_type = key_type_arg;
3716 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003718
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003719 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003720
Gilles Peskine8817f612018-12-18 00:18:46 +01003721 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003722
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003723 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003724 psa_set_key_algorithm( &attributes, alg );
3725 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003726
Gilles Peskine049c7532019-05-15 20:22:09 +02003727 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003728 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003729
Ronald Cron5425a212020-08-04 14:58:35 +02003730 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003731 hash_data->x, hash_data->len,
3732 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003733
3734#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003735 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003736 hash_data->x, hash_data->len,
3737 signature_data->x,
3738 signature_data->len ) );
3739
3740#endif /* MBEDTLS_TEST_DEPRECATED */
3741
itayzafrir5c753392018-05-08 11:18:38 +03003742exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003743 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003744 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003745 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003746}
3747/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003748
3749/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003750void verify_hash_fail( int key_type_arg, data_t *key_data,
3751 int alg_arg, data_t *hash_data,
3752 data_t *signature_data,
3753 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003754{
Ronald Cron5425a212020-08-04 14:58:35 +02003755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003756 psa_key_type_t key_type = key_type_arg;
3757 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003758 psa_status_t actual_status;
3759 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003761
Gilles Peskine8817f612018-12-18 00:18:46 +01003762 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003763
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003764 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003765 psa_set_key_algorithm( &attributes, alg );
3766 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003767
Gilles Peskine049c7532019-05-15 20:22:09 +02003768 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003769 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003770
Ronald Cron5425a212020-08-04 14:58:35 +02003771 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003772 hash_data->x, hash_data->len,
3773 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003774 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003775
Gilles Peskine895242b2019-11-29 12:15:40 +01003776#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003777 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003778 hash_data->x, hash_data->len,
3779 signature_data->x, signature_data->len ),
3780 expected_status );
3781#endif /* MBEDTLS_TEST_DEPRECATED */
3782
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003783exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003784 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003785 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003786 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003787}
3788/* END_CASE */
3789
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003790/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003791void sign_message_deterministic( int key_type_arg,
3792 data_t *key_data,
3793 int alg_arg,
3794 data_t *input_data,
3795 data_t *output_data )
3796{
3797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3798 psa_key_type_t key_type = key_type_arg;
3799 psa_algorithm_t alg = alg_arg;
3800 size_t key_bits;
3801 unsigned char *signature = NULL;
3802 size_t signature_size;
3803 size_t signature_length = 0xdeadbeef;
3804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3805
3806 PSA_ASSERT( psa_crypto_init( ) );
3807
3808 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3809 psa_set_key_algorithm( &attributes, alg );
3810 psa_set_key_type( &attributes, key_type );
3811
3812 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3813 &key ) );
3814 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3815 key_bits = psa_get_key_bits( &attributes );
3816
3817 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3818 TEST_ASSERT( signature_size != 0 );
3819 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3820 ASSERT_ALLOC( signature, signature_size );
3821
3822 PSA_ASSERT( psa_sign_message( key, alg,
3823 input_data->x, input_data->len,
3824 signature, signature_size,
3825 &signature_length ) );
3826
3827 ASSERT_COMPARE( output_data->x, output_data->len,
3828 signature, signature_length );
3829
3830exit:
3831 psa_reset_key_attributes( &attributes );
3832
3833 psa_destroy_key( key );
3834 mbedtls_free( signature );
3835 PSA_DONE( );
3836
3837}
3838/* END_CASE */
3839
3840/* BEGIN_CASE */
3841void sign_message_fail( int key_type_arg,
3842 data_t *key_data,
3843 int alg_arg,
3844 data_t *input_data,
3845 int signature_size_arg,
3846 int expected_status_arg )
3847{
3848 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3849 psa_key_type_t key_type = key_type_arg;
3850 psa_algorithm_t alg = alg_arg;
3851 size_t signature_size = signature_size_arg;
3852 psa_status_t actual_status;
3853 psa_status_t expected_status = expected_status_arg;
3854 unsigned char *signature = NULL;
3855 size_t signature_length = 0xdeadbeef;
3856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3857
3858 ASSERT_ALLOC( signature, signature_size );
3859
3860 PSA_ASSERT( psa_crypto_init( ) );
3861
3862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3863 psa_set_key_algorithm( &attributes, alg );
3864 psa_set_key_type( &attributes, key_type );
3865
3866 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3867 &key ) );
3868
3869 actual_status = psa_sign_message( key, alg,
3870 input_data->x, input_data->len,
3871 signature, signature_size,
3872 &signature_length );
3873 TEST_EQUAL( actual_status, expected_status );
3874 /* The value of *signature_length is unspecified on error, but
3875 * whatever it is, it should be less than signature_size, so that
3876 * if the caller tries to read *signature_length bytes without
3877 * checking the error code then they don't overflow a buffer. */
3878 TEST_ASSERT( signature_length <= signature_size );
3879
3880exit:
3881 psa_reset_key_attributes( &attributes );
3882 psa_destroy_key( key );
3883 mbedtls_free( signature );
3884 PSA_DONE( );
3885}
3886/* END_CASE */
3887
3888/* BEGIN_CASE */
3889void sign_verify_message( int key_type_arg,
3890 data_t *key_data,
3891 int alg_arg,
3892 data_t *input_data )
3893{
3894 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3895 psa_key_type_t key_type = key_type_arg;
3896 psa_algorithm_t alg = alg_arg;
3897 size_t key_bits;
3898 unsigned char *signature = NULL;
3899 size_t signature_size;
3900 size_t signature_length = 0xdeadbeef;
3901 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3902
3903 PSA_ASSERT( psa_crypto_init( ) );
3904
3905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3906 PSA_KEY_USAGE_VERIFY_MESSAGE );
3907 psa_set_key_algorithm( &attributes, alg );
3908 psa_set_key_type( &attributes, key_type );
3909
3910 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3911 &key ) );
3912 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3913 key_bits = psa_get_key_bits( &attributes );
3914
3915 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3916 TEST_ASSERT( signature_size != 0 );
3917 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3918 ASSERT_ALLOC( signature, signature_size );
3919
3920 PSA_ASSERT( psa_sign_message( key, alg,
3921 input_data->x, input_data->len,
3922 signature, signature_size,
3923 &signature_length ) );
3924 TEST_ASSERT( signature_length <= signature_size );
3925 TEST_ASSERT( signature_length > 0 );
3926
3927 PSA_ASSERT( psa_verify_message( key, alg,
3928 input_data->x, input_data->len,
3929 signature, signature_length ) );
3930
3931 if( input_data->len != 0 )
3932 {
3933 /* Flip a bit in the input and verify that the signature is now
3934 * detected as invalid. Flip a bit at the beginning, not at the end,
3935 * because ECDSA may ignore the last few bits of the input. */
3936 input_data->x[0] ^= 1;
3937 TEST_EQUAL( psa_verify_message( key, alg,
3938 input_data->x, input_data->len,
3939 signature, signature_length ),
3940 PSA_ERROR_INVALID_SIGNATURE );
3941 }
3942
3943exit:
3944 psa_reset_key_attributes( &attributes );
3945
3946 psa_destroy_key( key );
3947 mbedtls_free( signature );
3948 PSA_DONE( );
3949}
3950/* END_CASE */
3951
3952/* BEGIN_CASE */
3953void verify_message( int key_type_arg,
3954 data_t *key_data,
3955 int alg_arg,
3956 data_t *input_data,
3957 data_t *signature_data )
3958{
3959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3960 psa_key_type_t key_type = key_type_arg;
3961 psa_algorithm_t alg = alg_arg;
3962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3963
3964 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3965
3966 PSA_ASSERT( psa_crypto_init( ) );
3967
3968 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3969 psa_set_key_algorithm( &attributes, alg );
3970 psa_set_key_type( &attributes, key_type );
3971
3972 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3973 &key ) );
3974
3975 PSA_ASSERT( psa_verify_message( key, alg,
3976 input_data->x, input_data->len,
3977 signature_data->x, signature_data->len ) );
3978
3979exit:
3980 psa_reset_key_attributes( &attributes );
3981 psa_destroy_key( key );
3982 PSA_DONE( );
3983}
3984/* END_CASE */
3985
3986/* BEGIN_CASE */
3987void verify_message_fail( int key_type_arg,
3988 data_t *key_data,
3989 int alg_arg,
3990 data_t *hash_data,
3991 data_t *signature_data,
3992 int expected_status_arg )
3993{
3994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3995 psa_key_type_t key_type = key_type_arg;
3996 psa_algorithm_t alg = alg_arg;
3997 psa_status_t actual_status;
3998 psa_status_t expected_status = expected_status_arg;
3999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4000
4001 PSA_ASSERT( psa_crypto_init( ) );
4002
4003 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4004 psa_set_key_algorithm( &attributes, alg );
4005 psa_set_key_type( &attributes, key_type );
4006
4007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4008 &key ) );
4009
4010 actual_status = psa_verify_message( key, alg,
4011 hash_data->x, hash_data->len,
4012 signature_data->x,
4013 signature_data->len );
4014 TEST_EQUAL( actual_status, expected_status );
4015
4016exit:
4017 psa_reset_key_attributes( &attributes );
4018 psa_destroy_key( key );
4019 PSA_DONE( );
4020}
4021/* END_CASE */
4022
4023/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004024void asymmetric_encrypt( int key_type_arg,
4025 data_t *key_data,
4026 int alg_arg,
4027 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004028 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004029 int expected_output_length_arg,
4030 int expected_status_arg )
4031{
Ronald Cron5425a212020-08-04 14:58:35 +02004032 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004033 psa_key_type_t key_type = key_type_arg;
4034 psa_algorithm_t alg = alg_arg;
4035 size_t expected_output_length = expected_output_length_arg;
4036 size_t key_bits;
4037 unsigned char *output = NULL;
4038 size_t output_size;
4039 size_t output_length = ~0;
4040 psa_status_t actual_status;
4041 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004042 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004043
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004045
Gilles Peskine656896e2018-06-29 19:12:28 +02004046 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004047 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4048 psa_set_key_algorithm( &attributes, alg );
4049 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004050 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004051 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004052
4053 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004054 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004055 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004056
Gilles Peskine656896e2018-06-29 19:12:28 +02004057 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004058 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004059 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004060
4061 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004062 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004063 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004064 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004065 output, output_size,
4066 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004067 TEST_EQUAL( actual_status, expected_status );
4068 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004069
Gilles Peskine68428122018-06-30 18:42:41 +02004070 /* If the label is empty, the test framework puts a non-null pointer
4071 * in label->x. Test that a null pointer works as well. */
4072 if( label->len == 0 )
4073 {
4074 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004075 if( output_size != 0 )
4076 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004077 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004078 input_data->x, input_data->len,
4079 NULL, label->len,
4080 output, output_size,
4081 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004082 TEST_EQUAL( actual_status, expected_status );
4083 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004084 }
4085
Gilles Peskine656896e2018-06-29 19:12:28 +02004086exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004087 /*
4088 * Key attributes may have been returned by psa_get_key_attributes()
4089 * thus reset them as required.
4090 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004091 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004092
Ronald Cron5425a212020-08-04 14:58:35 +02004093 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004094 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004095 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004096}
4097/* END_CASE */
4098
4099/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004100void asymmetric_encrypt_decrypt( int key_type_arg,
4101 data_t *key_data,
4102 int alg_arg,
4103 data_t *input_data,
4104 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004105{
Ronald Cron5425a212020-08-04 14:58:35 +02004106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004107 psa_key_type_t key_type = key_type_arg;
4108 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004109 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004110 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004111 size_t output_size;
4112 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004113 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004114 size_t output2_size;
4115 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004117
Gilles Peskine8817f612018-12-18 00:18:46 +01004118 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004119
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4121 psa_set_key_algorithm( &attributes, alg );
4122 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004123
Gilles Peskine049c7532019-05-15 20:22:09 +02004124 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004125 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004126
4127 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004128 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004129 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004130
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004131 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004132 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004133 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004134
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004135 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004136 TEST_ASSERT( output2_size <=
4137 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4138 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004139 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004140
Gilles Peskineeebd7382018-06-08 18:11:54 +02004141 /* We test encryption by checking that encrypt-then-decrypt gives back
4142 * the original plaintext because of the non-optional random
4143 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004144 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004145 input_data->x, input_data->len,
4146 label->x, label->len,
4147 output, output_size,
4148 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004149 /* We don't know what ciphertext length to expect, but check that
4150 * it looks sensible. */
4151 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004152
Ronald Cron5425a212020-08-04 14:58:35 +02004153 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004154 output, output_length,
4155 label->x, label->len,
4156 output2, output2_size,
4157 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004158 ASSERT_COMPARE( input_data->x, input_data->len,
4159 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160
4161exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004162 /*
4163 * Key attributes may have been returned by psa_get_key_attributes()
4164 * thus reset them as required.
4165 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004166 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004167
Ronald Cron5425a212020-08-04 14:58:35 +02004168 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004169 mbedtls_free( output );
4170 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004171 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004172}
4173/* END_CASE */
4174
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004175/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004176void asymmetric_decrypt( int key_type_arg,
4177 data_t *key_data,
4178 int alg_arg,
4179 data_t *input_data,
4180 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004181 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004182{
Ronald Cron5425a212020-08-04 14:58:35 +02004183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004184 psa_key_type_t key_type = key_type_arg;
4185 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004186 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004187 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004188 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004189 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004191
Gilles Peskine8817f612018-12-18 00:18:46 +01004192 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004193
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004194 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4195 psa_set_key_algorithm( &attributes, alg );
4196 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004197
Gilles Peskine049c7532019-05-15 20:22:09 +02004198 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004199 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004200
gabor-mezei-armceface22021-01-21 12:26:17 +01004201 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4202 key_bits = psa_get_key_bits( &attributes );
4203
4204 /* Determine the maximum ciphertext length */
4205 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4206 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4207 ASSERT_ALLOC( output, output_size );
4208
Ronald Cron5425a212020-08-04 14:58:35 +02004209 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004210 input_data->x, input_data->len,
4211 label->x, label->len,
4212 output,
4213 output_size,
4214 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004215 ASSERT_COMPARE( expected_data->x, expected_data->len,
4216 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004217
Gilles Peskine68428122018-06-30 18:42:41 +02004218 /* If the label is empty, the test framework puts a non-null pointer
4219 * in label->x. Test that a null pointer works as well. */
4220 if( label->len == 0 )
4221 {
4222 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004223 if( output_size != 0 )
4224 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004225 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004226 input_data->x, input_data->len,
4227 NULL, label->len,
4228 output,
4229 output_size,
4230 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004231 ASSERT_COMPARE( expected_data->x, expected_data->len,
4232 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004233 }
4234
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004235exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004236 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004237 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004238 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004239 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004240}
4241/* END_CASE */
4242
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004243/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004244void asymmetric_decrypt_fail( int key_type_arg,
4245 data_t *key_data,
4246 int alg_arg,
4247 data_t *input_data,
4248 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004249 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004250 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004251{
Ronald Cron5425a212020-08-04 14:58:35 +02004252 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004253 psa_key_type_t key_type = key_type_arg;
4254 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004255 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004256 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004257 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004258 psa_status_t actual_status;
4259 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004261
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004262 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004263
Gilles Peskine8817f612018-12-18 00:18:46 +01004264 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004265
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4267 psa_set_key_algorithm( &attributes, alg );
4268 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004269
Gilles Peskine049c7532019-05-15 20:22:09 +02004270 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004271 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004272
Ronald Cron5425a212020-08-04 14:58:35 +02004273 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004274 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004275 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004276 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004277 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004278 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004279 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004280
Gilles Peskine68428122018-06-30 18:42:41 +02004281 /* If the label is empty, the test framework puts a non-null pointer
4282 * in label->x. Test that a null pointer works as well. */
4283 if( label->len == 0 )
4284 {
4285 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004286 if( output_size != 0 )
4287 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004288 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004289 input_data->x, input_data->len,
4290 NULL, label->len,
4291 output, output_size,
4292 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004293 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004294 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004295 }
4296
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004297exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004298 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004299 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004300 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004301 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004302}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004303/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004304
4305/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004306void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004307{
4308 /* Test each valid way of initializing the object, except for `= {0}`, as
4309 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4310 * though it's OK by the C standard. We could test for this, but we'd need
4311 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004312 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004313 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4314 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4315 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004316
4317 memset( &zero, 0, sizeof( zero ) );
4318
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004319 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004320 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004321 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004322 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004323 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004324 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004325 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004326
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004327 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004328 PSA_ASSERT( psa_key_derivation_abort(&func) );
4329 PSA_ASSERT( psa_key_derivation_abort(&init) );
4330 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004331}
4332/* END_CASE */
4333
Janos Follath16de4a42019-06-13 16:32:24 +01004334/* BEGIN_CASE */
4335void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004336{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004337 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004338 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004339 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004340
Gilles Peskine8817f612018-12-18 00:18:46 +01004341 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004342
Janos Follath16de4a42019-06-13 16:32:24 +01004343 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004344 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004345
4346exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004347 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004348 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004349}
4350/* END_CASE */
4351
Janos Follathaf3c2a02019-06-12 12:34:34 +01004352/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004353void derive_set_capacity( int alg_arg, int capacity_arg,
4354 int expected_status_arg )
4355{
4356 psa_algorithm_t alg = alg_arg;
4357 size_t capacity = capacity_arg;
4358 psa_status_t expected_status = expected_status_arg;
4359 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4360
4361 PSA_ASSERT( psa_crypto_init( ) );
4362
4363 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4364
4365 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4366 expected_status );
4367
4368exit:
4369 psa_key_derivation_abort( &operation );
4370 PSA_DONE( );
4371}
4372/* END_CASE */
4373
4374/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004375void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004376 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004377 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004378 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004379 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004380 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004381 int expected_status_arg3,
4382 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004383{
4384 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004385 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4386 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004387 psa_status_t expected_statuses[] = {expected_status_arg1,
4388 expected_status_arg2,
4389 expected_status_arg3};
4390 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004391 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4392 MBEDTLS_SVC_KEY_ID_INIT,
4393 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004394 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4396 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004397 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004398 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004399 psa_status_t expected_output_status = expected_output_status_arg;
4400 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004401
4402 PSA_ASSERT( psa_crypto_init( ) );
4403
4404 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4405 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004406
4407 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4408
4409 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4410 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004411 mbedtls_test_set_step( i );
4412 if( steps[i] == 0 )
4413 {
4414 /* Skip this step */
4415 }
4416 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004417 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004418 psa_set_key_type( &attributes, key_types[i] );
4419 PSA_ASSERT( psa_import_key( &attributes,
4420 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004421 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004422 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4423 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4424 {
4425 // When taking a private key as secret input, use key agreement
4426 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004427 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4428 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004429 expected_statuses[i] );
4430 }
4431 else
4432 {
4433 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004434 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004435 expected_statuses[i] );
4436 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004437 }
4438 else
4439 {
4440 TEST_EQUAL( psa_key_derivation_input_bytes(
4441 &operation, steps[i],
4442 inputs[i]->x, inputs[i]->len ),
4443 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004444 }
4445 }
4446
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004447 if( output_key_type != PSA_KEY_TYPE_NONE )
4448 {
4449 psa_reset_key_attributes( &attributes );
4450 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4451 psa_set_key_bits( &attributes, 8 );
4452 actual_output_status =
4453 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004454 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004455 }
4456 else
4457 {
4458 uint8_t buffer[1];
4459 actual_output_status =
4460 psa_key_derivation_output_bytes( &operation,
4461 buffer, sizeof( buffer ) );
4462 }
4463 TEST_EQUAL( actual_output_status, expected_output_status );
4464
Janos Follathaf3c2a02019-06-12 12:34:34 +01004465exit:
4466 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004467 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4468 psa_destroy_key( keys[i] );
4469 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004470 PSA_DONE( );
4471}
4472/* END_CASE */
4473
Janos Follathd958bb72019-07-03 15:02:16 +01004474/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004475void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004476{
Janos Follathd958bb72019-07-03 15:02:16 +01004477 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004479 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004481 unsigned char input1[] = "Input 1";
4482 size_t input1_length = sizeof( input1 );
4483 unsigned char input2[] = "Input 2";
4484 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004485 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004486 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004487 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4488 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4489 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004491
Gilles Peskine8817f612018-12-18 00:18:46 +01004492 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004493
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004494 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4495 psa_set_key_algorithm( &attributes, alg );
4496 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004497
Gilles Peskine73676cb2019-05-15 20:15:10 +02004498 PSA_ASSERT( psa_import_key( &attributes,
4499 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004500 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004501
4502 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004503 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4504 input1, input1_length,
4505 input2, input2_length,
4506 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004507 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004508
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004509 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004510 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004511 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004512
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004514
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004515 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004516 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004517
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004518exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004519 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004520 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004521 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004522}
4523/* END_CASE */
4524
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004525/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004526void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004527{
4528 uint8_t output_buffer[16];
4529 size_t buffer_size = 16;
4530 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004531 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004532
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004533 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4534 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004535 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004536
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004538 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004539
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004540 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004541
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004542 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4543 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004544 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004545
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004546 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004547 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004548
4549exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004550 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004551}
4552/* END_CASE */
4553
4554/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004555void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004556 int step1_arg, data_t *input1,
4557 int step2_arg, data_t *input2,
4558 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004559 int requested_capacity_arg,
4560 data_t *expected_output1,
4561 data_t *expected_output2 )
4562{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004563 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004564 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4565 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004566 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4567 MBEDTLS_SVC_KEY_ID_INIT,
4568 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004569 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004570 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004571 uint8_t *expected_outputs[2] =
4572 {expected_output1->x, expected_output2->x};
4573 size_t output_sizes[2] =
4574 {expected_output1->len, expected_output2->len};
4575 size_t output_buffer_size = 0;
4576 uint8_t *output_buffer = NULL;
4577 size_t expected_capacity;
4578 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004580 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004581 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004582
4583 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4584 {
4585 if( output_sizes[i] > output_buffer_size )
4586 output_buffer_size = output_sizes[i];
4587 if( output_sizes[i] == 0 )
4588 expected_outputs[i] = NULL;
4589 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004590 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004591 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004592
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004593 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4594 psa_set_key_algorithm( &attributes, alg );
4595 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004596
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004597 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004598 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4599 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4600 requested_capacity ) );
4601 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004602 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004603 switch( steps[i] )
4604 {
4605 case 0:
4606 break;
4607 case PSA_KEY_DERIVATION_INPUT_SECRET:
4608 PSA_ASSERT( psa_import_key( &attributes,
4609 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004610 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004611
4612 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4613 {
4614 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4615 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4616 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4617 }
4618
Gilles Peskine1468da72019-05-29 17:35:49 +02004619 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004620 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004621 break;
4622 default:
4623 PSA_ASSERT( psa_key_derivation_input_bytes(
4624 &operation, steps[i],
4625 inputs[i]->x, inputs[i]->len ) );
4626 break;
4627 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004628 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004629
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004630 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004631 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004632 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004633 expected_capacity = requested_capacity;
4634
4635 /* Expansion phase. */
4636 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4637 {
4638 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004640 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004641 if( expected_capacity == 0 && output_sizes[i] == 0 )
4642 {
4643 /* Reading 0 bytes when 0 bytes are available can go either way. */
4644 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004645 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004646 continue;
4647 }
4648 else if( expected_capacity == 0 ||
4649 output_sizes[i] > expected_capacity )
4650 {
4651 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004652 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004653 expected_capacity = 0;
4654 continue;
4655 }
4656 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004657 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004658 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004659 ASSERT_COMPARE( output_buffer, output_sizes[i],
4660 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004661 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004662 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004664 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004665 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004666 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004668
4669exit:
4670 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004672 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4673 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004674 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004675}
4676/* END_CASE */
4677
4678/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004679void derive_full( int alg_arg,
4680 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004681 data_t *input1,
4682 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004683 int requested_capacity_arg )
4684{
Ronald Cron5425a212020-08-04 14:58:35 +02004685 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004686 psa_algorithm_t alg = alg_arg;
4687 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004689 unsigned char output_buffer[16];
4690 size_t expected_capacity = requested_capacity;
4691 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004693
Gilles Peskine8817f612018-12-18 00:18:46 +01004694 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004695
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004696 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4697 psa_set_key_algorithm( &attributes, alg );
4698 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004699
Gilles Peskine049c7532019-05-15 20:22:09 +02004700 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004701 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004702
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004703 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4704 input1->x, input1->len,
4705 input2->x, input2->len,
4706 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004707 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004708
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004709 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004710 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004711 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004712
4713 /* Expansion phase. */
4714 while( current_capacity > 0 )
4715 {
4716 size_t read_size = sizeof( output_buffer );
4717 if( read_size > current_capacity )
4718 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004719 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004720 output_buffer,
4721 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004722 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004723 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004724 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004725 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004726 }
4727
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 /* Check that the operation refuses to go over capacity. */
4729 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004730 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004731
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004733
4734exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004736 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004737 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004738}
4739/* END_CASE */
4740
Janos Follathe60c9052019-07-03 13:51:30 +01004741/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004742void derive_key_exercise( int alg_arg,
4743 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004744 data_t *input1,
4745 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004746 int derived_type_arg,
4747 int derived_bits_arg,
4748 int derived_usage_arg,
4749 int derived_alg_arg )
4750{
Ronald Cron5425a212020-08-04 14:58:35 +02004751 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4752 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004753 psa_algorithm_t alg = alg_arg;
4754 psa_key_type_t derived_type = derived_type_arg;
4755 size_t derived_bits = derived_bits_arg;
4756 psa_key_usage_t derived_usage = derived_usage_arg;
4757 psa_algorithm_t derived_alg = derived_alg_arg;
4758 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004761 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004762
Gilles Peskine8817f612018-12-18 00:18:46 +01004763 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004764
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004765 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4766 psa_set_key_algorithm( &attributes, alg );
4767 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004768 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004769 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004770
4771 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004772 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4773 input1->x, input1->len,
4774 input2->x, input2->len,
4775 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004776 goto exit;
4777
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004778 psa_set_key_usage_flags( &attributes, derived_usage );
4779 psa_set_key_algorithm( &attributes, derived_alg );
4780 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004781 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004783 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004784
4785 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004786 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004787 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4788 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004789
4790 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004791 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004792 goto exit;
4793
4794exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004795 /*
4796 * Key attributes may have been returned by psa_get_key_attributes()
4797 * thus reset them as required.
4798 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004799 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004800
4801 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004802 psa_destroy_key( base_key );
4803 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004804 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004805}
4806/* END_CASE */
4807
Janos Follath42fd8882019-07-03 14:17:09 +01004808/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004809void derive_key_export( int alg_arg,
4810 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004811 data_t *input1,
4812 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004813 int bytes1_arg,
4814 int bytes2_arg )
4815{
Ronald Cron5425a212020-08-04 14:58:35 +02004816 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4817 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004818 psa_algorithm_t alg = alg_arg;
4819 size_t bytes1 = bytes1_arg;
4820 size_t bytes2 = bytes2_arg;
4821 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004823 uint8_t *output_buffer = NULL;
4824 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004825 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4826 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004827 size_t length;
4828
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004829 ASSERT_ALLOC( output_buffer, capacity );
4830 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004831 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004832
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004833 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4834 psa_set_key_algorithm( &base_attributes, alg );
4835 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004836 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004837 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004838
4839 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004840 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4841 input1->x, input1->len,
4842 input2->x, input2->len,
4843 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004844 goto exit;
4845
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004847 output_buffer,
4848 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004850
4851 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004852 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4853 input1->x, input1->len,
4854 input2->x, input2->len,
4855 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004856 goto exit;
4857
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004858 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4859 psa_set_key_algorithm( &derived_attributes, 0 );
4860 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004861 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004863 &derived_key ) );
4864 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004865 export_buffer, bytes1,
4866 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004867 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004868 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004869 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004871 &derived_key ) );
4872 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004873 export_buffer + bytes1, bytes2,
4874 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004875 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004876
4877 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004878 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4879 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004880
4881exit:
4882 mbedtls_free( output_buffer );
4883 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004884 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004885 psa_destroy_key( base_key );
4886 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004887 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004888}
4889/* END_CASE */
4890
4891/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004892void derive_key( int alg_arg,
4893 data_t *key_data, data_t *input1, data_t *input2,
4894 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004895 int expected_status_arg,
4896 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004897{
Ronald Cron5425a212020-08-04 14:58:35 +02004898 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4899 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004900 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004901 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004902 size_t bits = bits_arg;
4903 psa_status_t expected_status = expected_status_arg;
4904 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4905 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4906 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4907
4908 PSA_ASSERT( psa_crypto_init( ) );
4909
4910 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4911 psa_set_key_algorithm( &base_attributes, alg );
4912 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4913 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004914 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004915
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004916 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4917 input1->x, input1->len,
4918 input2->x, input2->len,
4919 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004920 goto exit;
4921
4922 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4923 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004924 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004925 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004926
4927 psa_status_t status =
4928 psa_key_derivation_output_key( &derived_attributes,
4929 &operation,
4930 &derived_key );
4931 if( is_large_output > 0 )
4932 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4933 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004934
4935exit:
4936 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004937 psa_destroy_key( base_key );
4938 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004939 PSA_DONE( );
4940}
4941/* END_CASE */
4942
4943/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004944void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004945 int our_key_type_arg, int our_key_alg_arg,
4946 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004947 int expected_status_arg )
4948{
Ronald Cron5425a212020-08-04 14:58:35 +02004949 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004950 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004951 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004952 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004955 psa_status_t expected_status = expected_status_arg;
4956 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004957
Gilles Peskine8817f612018-12-18 00:18:46 +01004958 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004959
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004960 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004961 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004962 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004963 PSA_ASSERT( psa_import_key( &attributes,
4964 our_key_data->x, our_key_data->len,
4965 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004966
Gilles Peskine77f40d82019-04-11 21:27:06 +02004967 /* The tests currently include inputs that should fail at either step.
4968 * Test cases that fail at the setup step should be changed to call
4969 * key_derivation_setup instead, and this function should be renamed
4970 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004972 if( status == PSA_SUCCESS )
4973 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004974 TEST_EQUAL( psa_key_derivation_key_agreement(
4975 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4976 our_key,
4977 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004978 expected_status );
4979 }
4980 else
4981 {
4982 TEST_ASSERT( status == expected_status );
4983 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004984
4985exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004987 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004988 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004989}
4990/* END_CASE */
4991
4992/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004993void raw_key_agreement( int alg_arg,
4994 int our_key_type_arg, data_t *our_key_data,
4995 data_t *peer_key_data,
4996 data_t *expected_output )
4997{
Ronald Cron5425a212020-08-04 14:58:35 +02004998 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004999 psa_algorithm_t alg = alg_arg;
5000 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005002 unsigned char *output = NULL;
5003 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005004 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005005
5006 ASSERT_ALLOC( output, expected_output->len );
5007 PSA_ASSERT( psa_crypto_init( ) );
5008
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005009 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5010 psa_set_key_algorithm( &attributes, alg );
5011 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005012 PSA_ASSERT( psa_import_key( &attributes,
5013 our_key_data->x, our_key_data->len,
5014 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005015
gabor-mezei-armceface22021-01-21 12:26:17 +01005016 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5017 key_bits = psa_get_key_bits( &attributes );
5018
Gilles Peskinebe697d82019-05-16 18:00:41 +02005019 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5020 peer_key_data->x, peer_key_data->len,
5021 output, expected_output->len,
5022 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005023 ASSERT_COMPARE( output, output_length,
5024 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005025 TEST_ASSERT( output_length <=
5026 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5027 TEST_ASSERT( output_length <=
5028 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005029
5030exit:
5031 mbedtls_free( output );
5032 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005033 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005034}
5035/* END_CASE */
5036
5037/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005038void key_agreement_capacity( int alg_arg,
5039 int our_key_type_arg, data_t *our_key_data,
5040 data_t *peer_key_data,
5041 int expected_capacity_arg )
5042{
Ronald Cron5425a212020-08-04 14:58:35 +02005043 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005044 psa_algorithm_t alg = alg_arg;
5045 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005048 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005049 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005050
Gilles Peskine8817f612018-12-18 00:18:46 +01005051 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005052
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005053 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5054 psa_set_key_algorithm( &attributes, alg );
5055 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005056 PSA_ASSERT( psa_import_key( &attributes,
5057 our_key_data->x, our_key_data->len,
5058 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005059
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005061 PSA_ASSERT( psa_key_derivation_key_agreement(
5062 &operation,
5063 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5064 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005065 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5066 {
5067 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005068 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005069 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005070 NULL, 0 ) );
5071 }
Gilles Peskine59685592018-09-18 12:11:34 +02005072
Gilles Peskinebf491972018-10-25 22:36:12 +02005073 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005074 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005075 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005076 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005077
Gilles Peskinebf491972018-10-25 22:36:12 +02005078 /* Test the actual capacity by reading the output. */
5079 while( actual_capacity > sizeof( output ) )
5080 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005082 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005083 actual_capacity -= sizeof( output );
5084 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005085 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005086 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005087 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005088 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005089
Gilles Peskine59685592018-09-18 12:11:34 +02005090exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005092 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005093 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005094}
5095/* END_CASE */
5096
5097/* BEGIN_CASE */
5098void key_agreement_output( int alg_arg,
5099 int our_key_type_arg, data_t *our_key_data,
5100 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005101 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005102{
Ronald Cron5425a212020-08-04 14:58:35 +02005103 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005104 psa_algorithm_t alg = alg_arg;
5105 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005106 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005108 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005109
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005110 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5111 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005112
Gilles Peskine8817f612018-12-18 00:18:46 +01005113 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005114
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005115 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5116 psa_set_key_algorithm( &attributes, alg );
5117 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005118 PSA_ASSERT( psa_import_key( &attributes,
5119 our_key_data->x, our_key_data->len,
5120 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005121
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005122 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005123 PSA_ASSERT( psa_key_derivation_key_agreement(
5124 &operation,
5125 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5126 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005127 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5128 {
5129 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005130 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005131 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005132 NULL, 0 ) );
5133 }
Gilles Peskine59685592018-09-18 12:11:34 +02005134
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005136 actual_output,
5137 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005138 ASSERT_COMPARE( actual_output, expected_output1->len,
5139 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005140 if( expected_output2->len != 0 )
5141 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005143 actual_output,
5144 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005145 ASSERT_COMPARE( actual_output, expected_output2->len,
5146 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005147 }
Gilles Peskine59685592018-09-18 12:11:34 +02005148
5149exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005151 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005152 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005153 mbedtls_free( actual_output );
5154}
5155/* END_CASE */
5156
5157/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005158void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005159{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005160 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005161 unsigned char *output = NULL;
5162 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005163 size_t i;
5164 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005165
Simon Butcher49f8e312020-03-03 15:51:50 +00005166 TEST_ASSERT( bytes_arg >= 0 );
5167
Gilles Peskine91892022021-02-08 19:50:26 +01005168 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005169 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005170
Gilles Peskine8817f612018-12-18 00:18:46 +01005171 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005172
Gilles Peskinea50d7392018-06-21 10:22:13 +02005173 /* Run several times, to ensure that every output byte will be
5174 * nonzero at least once with overwhelming probability
5175 * (2^(-8*number_of_runs)). */
5176 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005177 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005178 if( bytes != 0 )
5179 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005180 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005181
Gilles Peskinea50d7392018-06-21 10:22:13 +02005182 for( i = 0; i < bytes; i++ )
5183 {
5184 if( output[i] != 0 )
5185 ++changed[i];
5186 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005187 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005188
5189 /* Check that every byte was changed to nonzero at least once. This
5190 * validates that psa_generate_random is overwriting every byte of
5191 * the output buffer. */
5192 for( i = 0; i < bytes; i++ )
5193 {
5194 TEST_ASSERT( changed[i] != 0 );
5195 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005196
5197exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005198 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005199 mbedtls_free( output );
5200 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005201}
5202/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005203
5204/* BEGIN_CASE */
5205void generate_key( int type_arg,
5206 int bits_arg,
5207 int usage_arg,
5208 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005209 int expected_status_arg,
5210 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005211{
Ronald Cron5425a212020-08-04 14:58:35 +02005212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005213 psa_key_type_t type = type_arg;
5214 psa_key_usage_t usage = usage_arg;
5215 size_t bits = bits_arg;
5216 psa_algorithm_t alg = alg_arg;
5217 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005218 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005219 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005220
Gilles Peskine8817f612018-12-18 00:18:46 +01005221 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005222
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005223 psa_set_key_usage_flags( &attributes, usage );
5224 psa_set_key_algorithm( &attributes, alg );
5225 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005226 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005227
5228 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005229 psa_status_t status = psa_generate_key( &attributes, &key );
5230
5231 if( is_large_key > 0 )
5232 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5233 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005234 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005235 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005236
5237 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005238 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005239 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5240 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005241
Gilles Peskine818ca122018-06-20 18:16:48 +02005242 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005243 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005244 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005245
5246exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005247 /*
5248 * Key attributes may have been returned by psa_get_key_attributes()
5249 * thus reset them as required.
5250 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005251 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005252
Ronald Cron5425a212020-08-04 14:58:35 +02005253 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005254 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005255}
5256/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005257
Ronald Cronee414c72021-03-18 18:50:08 +01005258/* 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 +02005259void generate_key_rsa( int bits_arg,
5260 data_t *e_arg,
5261 int expected_status_arg )
5262{
Ronald Cron5425a212020-08-04 14:58:35 +02005263 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005264 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005265 size_t bits = bits_arg;
5266 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5267 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5268 psa_status_t expected_status = expected_status_arg;
5269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5270 uint8_t *exported = NULL;
5271 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005272 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005273 size_t exported_length = SIZE_MAX;
5274 uint8_t *e_read_buffer = NULL;
5275 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005276 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005277 size_t e_read_length = SIZE_MAX;
5278
5279 if( e_arg->len == 0 ||
5280 ( e_arg->len == 3 &&
5281 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5282 {
5283 is_default_public_exponent = 1;
5284 e_read_size = 0;
5285 }
5286 ASSERT_ALLOC( e_read_buffer, e_read_size );
5287 ASSERT_ALLOC( exported, exported_size );
5288
5289 PSA_ASSERT( psa_crypto_init( ) );
5290
5291 psa_set_key_usage_flags( &attributes, usage );
5292 psa_set_key_algorithm( &attributes, alg );
5293 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5294 e_arg->x, e_arg->len ) );
5295 psa_set_key_bits( &attributes, bits );
5296
5297 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005298 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005299 if( expected_status != PSA_SUCCESS )
5300 goto exit;
5301
5302 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005303 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005304 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5305 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5306 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5307 e_read_buffer, e_read_size,
5308 &e_read_length ) );
5309 if( is_default_public_exponent )
5310 TEST_EQUAL( e_read_length, 0 );
5311 else
5312 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5313
5314 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005315 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005316 goto exit;
5317
5318 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005319 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005320 exported, exported_size,
5321 &exported_length ) );
5322 {
5323 uint8_t *p = exported;
5324 uint8_t *end = exported + exported_length;
5325 size_t len;
5326 /* RSAPublicKey ::= SEQUENCE {
5327 * modulus INTEGER, -- n
5328 * publicExponent INTEGER } -- e
5329 */
5330 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005331 MBEDTLS_ASN1_SEQUENCE |
5332 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005333 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005334 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5335 MBEDTLS_ASN1_INTEGER ) );
5336 if( len >= 1 && p[0] == 0 )
5337 {
5338 ++p;
5339 --len;
5340 }
5341 if( e_arg->len == 0 )
5342 {
5343 TEST_EQUAL( len, 3 );
5344 TEST_EQUAL( p[0], 1 );
5345 TEST_EQUAL( p[1], 0 );
5346 TEST_EQUAL( p[2], 1 );
5347 }
5348 else
5349 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5350 }
5351
5352exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005353 /*
5354 * Key attributes may have been returned by psa_get_key_attributes() or
5355 * set by psa_set_key_domain_parameters() thus reset them as required.
5356 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005357 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005358
Ronald Cron5425a212020-08-04 14:58:35 +02005359 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005360 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005361 mbedtls_free( e_read_buffer );
5362 mbedtls_free( exported );
5363}
5364/* END_CASE */
5365
Darryl Greend49a4992018-06-18 17:27:26 +01005366/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005367void persistent_key_load_key_from_storage( data_t *data,
5368 int type_arg, int bits_arg,
5369 int usage_flags_arg, int alg_arg,
5370 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005371{
Ronald Cron71016a92020-08-28 19:01:50 +02005372 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005374 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5375 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005376 psa_key_type_t type = type_arg;
5377 size_t bits = bits_arg;
5378 psa_key_usage_t usage_flags = usage_flags_arg;
5379 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005380 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005381 unsigned char *first_export = NULL;
5382 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005383 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005384 size_t first_exported_length;
5385 size_t second_exported_length;
5386
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005387 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5388 {
5389 ASSERT_ALLOC( first_export, export_size );
5390 ASSERT_ALLOC( second_export, export_size );
5391 }
Darryl Greend49a4992018-06-18 17:27:26 +01005392
Gilles Peskine8817f612018-12-18 00:18:46 +01005393 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005394
Gilles Peskinec87af662019-05-15 16:12:22 +02005395 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005396 psa_set_key_usage_flags( &attributes, usage_flags );
5397 psa_set_key_algorithm( &attributes, alg );
5398 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005399 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005400
Darryl Green0c6575a2018-11-07 16:05:30 +00005401 switch( generation_method )
5402 {
5403 case IMPORT_KEY:
5404 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005405 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005406 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005407 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005408
Darryl Green0c6575a2018-11-07 16:05:30 +00005409 case GENERATE_KEY:
5410 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005411 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005412 break;
5413
5414 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005415#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005416 {
5417 /* Create base key */
5418 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5419 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5420 psa_set_key_usage_flags( &base_attributes,
5421 PSA_KEY_USAGE_DERIVE );
5422 psa_set_key_algorithm( &base_attributes, derive_alg );
5423 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005424 PSA_ASSERT( psa_import_key( &base_attributes,
5425 data->x, data->len,
5426 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005427 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005429 PSA_ASSERT( psa_key_derivation_input_key(
5430 &operation,
5431 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005432 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005434 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005435 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5436 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005437 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005438 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005439 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005440 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005441 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005442#else
5443 TEST_ASSUME( ! "KDF not supported in this configuration" );
5444#endif
5445 break;
5446
5447 default:
5448 TEST_ASSERT( ! "generation_method not implemented in test" );
5449 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005450 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005451 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005452
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005453 /* Export the key if permitted by the key policy. */
5454 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5455 {
Ronald Cron5425a212020-08-04 14:58:35 +02005456 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005457 first_export, export_size,
5458 &first_exported_length ) );
5459 if( generation_method == IMPORT_KEY )
5460 ASSERT_COMPARE( data->x, data->len,
5461 first_export, first_exported_length );
5462 }
Darryl Greend49a4992018-06-18 17:27:26 +01005463
5464 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005465 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005466 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005467 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005468
Darryl Greend49a4992018-06-18 17:27:26 +01005469 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005470 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005471 TEST_ASSERT( mbedtls_svc_key_id_equal(
5472 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005473 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5474 PSA_KEY_LIFETIME_PERSISTENT );
5475 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5476 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005477 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005478 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005479 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005480
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005481 /* Export the key again if permitted by the key policy. */
5482 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005483 {
Ronald Cron5425a212020-08-04 14:58:35 +02005484 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005485 second_export, export_size,
5486 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005487 ASSERT_COMPARE( first_export, first_exported_length,
5488 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005489 }
5490
5491 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005492 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005493 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005494
5495exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005496 /*
5497 * Key attributes may have been returned by psa_get_key_attributes()
5498 * thus reset them as required.
5499 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005500 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005501
Darryl Greend49a4992018-06-18 17:27:26 +01005502 mbedtls_free( first_export );
5503 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005505 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005506 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005507 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005508}
5509/* END_CASE */