blob: 5fa45d8e73a99bcf589c4e2b1084d5183b2a3290 [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
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200886 /* Calculate the MAC, one-shot case. */
887 uint8_t input[128] = {0};
888 size_t mac_len;
889 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
890 input, 128,
891 mac, PSA_MAC_MAX_SIZE, &mac_len ),
892 expected_status_sign );
893
894 /* Verify correct MAC, one-shot case. */
895 status = psa_mac_verify( key, exercise_alg, input, 128,
896 mac, mac_len );
897
898 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
899 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
900 else
901 TEST_EQUAL( status, expected_status_verify );
902
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200904
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200906 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200907 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908
909exit:
910 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200911 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200912 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200913}
914/* END_CASE */
915
916/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200917void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 int policy_alg,
919 int key_type,
920 data_t *key_data,
921 int exercise_alg )
922{
Ronald Cron5425a212020-08-04 14:58:35 +0200923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000925 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200926 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200927 psa_status_t status;
928
Gilles Peskine8817f612018-12-18 00:18:46 +0100929 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200931 psa_set_key_usage_flags( &attributes, policy_usage );
932 psa_set_key_algorithm( &attributes, policy_alg );
933 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200934
Gilles Peskine049c7532019-05-15 20:22:09 +0200935 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200936 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200938 /* Check if no key usage flag implication is done */
939 TEST_EQUAL( policy_usage,
940 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200941
Ronald Cron5425a212020-08-04 14:58:35 +0200942 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943 if( policy_alg == exercise_alg &&
944 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100945 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100947 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948 psa_cipher_abort( &operation );
949
Ronald Cron5425a212020-08-04 14:58:35 +0200950 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951 if( policy_alg == exercise_alg &&
952 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100953 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200954 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100955 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956
957exit:
958 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200959 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200960 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961}
962/* END_CASE */
963
964/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200965void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 int policy_alg,
967 int key_type,
968 data_t *key_data,
969 int nonce_length_arg,
970 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100971 int exercise_alg,
972 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973{
Ronald Cron5425a212020-08-04 14:58:35 +0200974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200976 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200977 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100978 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979 unsigned char nonce[16] = {0};
980 size_t nonce_length = nonce_length_arg;
981 unsigned char tag[16];
982 size_t tag_length = tag_length_arg;
983 size_t output_length;
984
985 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
986 TEST_ASSERT( tag_length <= sizeof( tag ) );
987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200990 psa_set_key_usage_flags( &attributes, policy_usage );
991 psa_set_key_algorithm( &attributes, policy_alg );
992 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200993
Gilles Peskine049c7532019-05-15 20:22:09 +0200994 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200995 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200996
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200997 /* Check if no key usage implication is done */
998 TEST_EQUAL( policy_usage,
999 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001000
Ronald Cron5425a212020-08-04 14:58:35 +02001001 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001002 nonce, nonce_length,
1003 NULL, 0,
1004 NULL, 0,
1005 tag, tag_length,
1006 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001007 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1008 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001009 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001010 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011
1012 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001013 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014 nonce, nonce_length,
1015 NULL, 0,
1016 tag, tag_length,
1017 NULL, 0,
1018 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001019 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1020 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1021 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001022 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001023 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001024 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025
1026exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001027 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001028 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001029}
1030/* END_CASE */
1031
1032/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001033void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034 int policy_alg,
1035 int key_type,
1036 data_t *key_data,
1037 int exercise_alg )
1038{
Ronald Cron5425a212020-08-04 14:58:35 +02001039 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001041 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001042 psa_status_t status;
1043 size_t key_bits;
1044 size_t buffer_length;
1045 unsigned char *buffer = NULL;
1046 size_t output_length;
1047
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001049
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001050 psa_set_key_usage_flags( &attributes, policy_usage );
1051 psa_set_key_algorithm( &attributes, policy_alg );
1052 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001053
Gilles Peskine049c7532019-05-15 20:22:09 +02001054 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001055 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001056
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001057 /* Check if no key usage implication is done */
1058 TEST_EQUAL( policy_usage,
1059 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001060
Ronald Cron5425a212020-08-04 14:58:35 +02001061 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001062 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001063 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1064 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001065 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001066
Ronald Cron5425a212020-08-04 14:58:35 +02001067 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068 NULL, 0,
1069 NULL, 0,
1070 buffer, buffer_length,
1071 &output_length );
1072 if( policy_alg == exercise_alg &&
1073 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001074 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001075 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001076 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001078 if( buffer_length != 0 )
1079 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001080 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001081 buffer, buffer_length,
1082 NULL, 0,
1083 buffer, buffer_length,
1084 &output_length );
1085 if( policy_alg == exercise_alg &&
1086 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001087 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001089 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001090
1091exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001092 /*
1093 * Key attributes may have been returned by psa_get_key_attributes()
1094 * thus reset them as required.
1095 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001096 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001097
1098 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001099 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100 mbedtls_free( buffer );
1101}
1102/* END_CASE */
1103
1104/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001105void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106 int policy_alg,
1107 int key_type,
1108 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001109 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001110 int payload_length_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001111 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001112{
Ronald Cron5425a212020-08-04 14:58:35 +02001113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001115 psa_key_usage_t policy_usage = policy_usage_arg;
1116 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001117 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001118 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1119 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1120 * compatible with the policy and `payload_length_arg` is supposed to be
1121 * a valid input length to sign. If `payload_length_arg <= 0`,
1122 * `exercise_alg` is supposed to be forbidden by the policy. */
1123 int compatible_alg = payload_length_arg > 0;
1124 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001125 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126 size_t signature_length;
1127
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001128 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001129 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001130 TEST_EQUAL( expected_usage,
1131 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001132
Gilles Peskine8817f612018-12-18 00:18:46 +01001133 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001135 psa_set_key_usage_flags( &attributes, policy_usage );
1136 psa_set_key_algorithm( &attributes, policy_alg );
1137 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001138
Gilles Peskine049c7532019-05-15 20:22:09 +02001139 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001140 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001142 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1143
Ronald Cron5425a212020-08-04 14:58:35 +02001144 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001145 payload, payload_length,
1146 signature, sizeof( signature ),
1147 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001148 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001149 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001150 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001151 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152
1153 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001154 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001155 payload, payload_length,
1156 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001157 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001158 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001160 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001161
gabor-mezei-arm79df41d2021-06-28 14:53:49 +02001162 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1163 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001164 {
1165 status = psa_sign_message( key, exercise_alg,
1166 payload, payload_length,
1167 signature, sizeof( signature ),
1168 &signature_length );
1169 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1170 PSA_ASSERT( status );
1171 else
1172 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1173
1174 memset( signature, 0, sizeof( signature ) );
1175 status = psa_verify_message( key, exercise_alg,
1176 payload, payload_length,
1177 signature, sizeof( signature ) );
1178 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1179 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1180 else
1181 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1182 }
1183
Gilles Peskined5b33222018-06-18 22:20:03 +02001184exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001186 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001187}
1188/* END_CASE */
1189
Janos Follathba3fab92019-06-11 14:50:16 +01001190/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001191void derive_key_policy( int policy_usage,
1192 int policy_alg,
1193 int key_type,
1194 data_t *key_data,
1195 int exercise_alg )
1196{
Ronald Cron5425a212020-08-04 14:58:35 +02001197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001198 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001199 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001200 psa_status_t status;
1201
Gilles Peskine8817f612018-12-18 00:18:46 +01001202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001203
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001204 psa_set_key_usage_flags( &attributes, policy_usage );
1205 psa_set_key_algorithm( &attributes, policy_alg );
1206 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001207
Gilles Peskine049c7532019-05-15 20:22:09 +02001208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001209 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001210
Janos Follathba3fab92019-06-11 14:50:16 +01001211 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1212
1213 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1214 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001215 {
Janos Follathba3fab92019-06-11 14:50:16 +01001216 PSA_ASSERT( psa_key_derivation_input_bytes(
1217 &operation,
1218 PSA_KEY_DERIVATION_INPUT_SEED,
1219 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001220 }
Janos Follathba3fab92019-06-11 14:50:16 +01001221
1222 status = psa_key_derivation_input_key( &operation,
1223 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001224 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001225
Gilles Peskineea0fb492018-07-12 17:17:20 +02001226 if( policy_alg == exercise_alg &&
1227 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001228 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001229 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001230 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001231
1232exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001233 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001234 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001235 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001236}
1237/* END_CASE */
1238
1239/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001240void agreement_key_policy( int policy_usage,
1241 int policy_alg,
1242 int key_type_arg,
1243 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001244 int exercise_alg,
1245 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001246{
Ronald Cron5425a212020-08-04 14:58:35 +02001247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001249 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001250 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001251 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001252 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001253
Gilles Peskine8817f612018-12-18 00:18:46 +01001254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001255
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001256 psa_set_key_usage_flags( &attributes, policy_usage );
1257 psa_set_key_algorithm( &attributes, policy_alg );
1258 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001259
Gilles Peskine049c7532019-05-15 20:22:09 +02001260 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001261 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001262
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001263 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001264 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001265
Steven Cooremance48e852020-10-05 16:02:45 +02001266 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001267
1268exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001269 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001270 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001271 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001272}
1273/* END_CASE */
1274
1275/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001276void key_policy_alg2( int key_type_arg, data_t *key_data,
1277 int usage_arg, int alg_arg, int alg2_arg )
1278{
Ronald Cron5425a212020-08-04 14:58:35 +02001279 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001280 psa_key_type_t key_type = key_type_arg;
1281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1282 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1283 psa_key_usage_t usage = usage_arg;
1284 psa_algorithm_t alg = alg_arg;
1285 psa_algorithm_t alg2 = alg2_arg;
1286
1287 PSA_ASSERT( psa_crypto_init( ) );
1288
1289 psa_set_key_usage_flags( &attributes, usage );
1290 psa_set_key_algorithm( &attributes, alg );
1291 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1292 psa_set_key_type( &attributes, key_type );
1293 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001294 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001295
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001296 /* Update the usage flags to obtain implicit usage flags */
1297 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001298 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001299 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1300 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1301 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1302
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001303 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001304 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001305 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001306 goto exit;
1307
1308exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001309 /*
1310 * Key attributes may have been returned by psa_get_key_attributes()
1311 * thus reset them as required.
1312 */
1313 psa_reset_key_attributes( &got_attributes );
1314
Ronald Cron5425a212020-08-04 14:58:35 +02001315 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001316 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001317}
1318/* END_CASE */
1319
1320/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001321void raw_agreement_key_policy( int policy_usage,
1322 int policy_alg,
1323 int key_type_arg,
1324 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001325 int exercise_alg,
1326 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001327{
Ronald Cron5425a212020-08-04 14:58:35 +02001328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001330 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001331 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001332 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001333 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001334
1335 PSA_ASSERT( psa_crypto_init( ) );
1336
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001337 psa_set_key_usage_flags( &attributes, policy_usage );
1338 psa_set_key_algorithm( &attributes, policy_alg );
1339 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001340
Gilles Peskine049c7532019-05-15 20:22:09 +02001341 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001342 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001343
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001344 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001345
Steven Cooremance48e852020-10-05 16:02:45 +02001346 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001347
1348exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001349 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001350 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001351 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001352}
1353/* END_CASE */
1354
1355/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001356void copy_success( int source_usage_arg,
1357 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001358 int type_arg, data_t *material,
1359 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001360 int target_usage_arg,
1361 int target_alg_arg, int target_alg2_arg,
1362 int expected_usage_arg,
1363 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001364{
Gilles Peskineca25db92019-04-19 11:43:08 +02001365 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1366 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001367 psa_key_usage_t expected_usage = expected_usage_arg;
1368 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001369 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001370 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1371 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001372 uint8_t *export_buffer = NULL;
1373
Gilles Peskine57ab7212019-01-28 13:03:09 +01001374 PSA_ASSERT( psa_crypto_init( ) );
1375
Gilles Peskineca25db92019-04-19 11:43:08 +02001376 /* Prepare the source key. */
1377 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1378 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001379 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001380 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001381 PSA_ASSERT( psa_import_key( &source_attributes,
1382 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001383 &source_key ) );
1384 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001385
Gilles Peskineca25db92019-04-19 11:43:08 +02001386 /* Prepare the target attributes. */
1387 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001388 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001389 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001390 /* Set volatile lifetime to reset the key identifier to 0. */
1391 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1392 }
1393
Gilles Peskineca25db92019-04-19 11:43:08 +02001394 if( target_usage_arg != -1 )
1395 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1396 if( target_alg_arg != -1 )
1397 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001398 if( target_alg2_arg != -1 )
1399 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001400
1401 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001402 PSA_ASSERT( psa_copy_key( source_key,
1403 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001404
1405 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001406 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001407
1408 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001409 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001410 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1411 psa_get_key_type( &target_attributes ) );
1412 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1413 psa_get_key_bits( &target_attributes ) );
1414 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1415 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001416 TEST_EQUAL( expected_alg2,
1417 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001418 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1419 {
1420 size_t length;
1421 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001422 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001423 material->len, &length ) );
1424 ASSERT_COMPARE( material->x, material->len,
1425 export_buffer, length );
1426 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001427
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001428 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001429 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001430 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001431 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001432
Ronald Cron5425a212020-08-04 14:58:35 +02001433 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001434
1435exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001436 /*
1437 * Source and target key attributes may have been returned by
1438 * psa_get_key_attributes() thus reset them as required.
1439 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001440 psa_reset_key_attributes( &source_attributes );
1441 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001442
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001443 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001444 mbedtls_free( export_buffer );
1445}
1446/* END_CASE */
1447
1448/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001449void copy_fail( int source_usage_arg,
1450 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001451 int type_arg, data_t *material,
1452 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001453 int target_usage_arg,
1454 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001455 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001456 int expected_status_arg )
1457{
1458 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1459 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001460 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1461 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001462 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001463
1464 PSA_ASSERT( psa_crypto_init( ) );
1465
1466 /* Prepare the source key. */
1467 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1468 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001469 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001470 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001471 PSA_ASSERT( psa_import_key( &source_attributes,
1472 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001473 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001474
1475 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001476 psa_set_key_id( &target_attributes, key_id );
1477 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001478 psa_set_key_type( &target_attributes, target_type_arg );
1479 psa_set_key_bits( &target_attributes, target_bits_arg );
1480 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1481 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001482 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001483
1484 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001485 TEST_EQUAL( psa_copy_key( source_key,
1486 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001487 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001488
Ronald Cron5425a212020-08-04 14:58:35 +02001489 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001490
Gilles Peskine4a644642019-05-03 17:14:08 +02001491exit:
1492 psa_reset_key_attributes( &source_attributes );
1493 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001494 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001495}
1496/* END_CASE */
1497
1498/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001499void hash_operation_init( )
1500{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001501 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001502 /* Test each valid way of initializing the object, except for `= {0}`, as
1503 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1504 * though it's OK by the C standard. We could test for this, but we'd need
1505 * to supress the Clang warning for the test. */
1506 psa_hash_operation_t func = psa_hash_operation_init( );
1507 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1508 psa_hash_operation_t zero;
1509
1510 memset( &zero, 0, sizeof( zero ) );
1511
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001512 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001513 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1514 PSA_ERROR_BAD_STATE );
1515 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1516 PSA_ERROR_BAD_STATE );
1517 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1518 PSA_ERROR_BAD_STATE );
1519
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001520 /* A default hash operation should be abortable without error. */
1521 PSA_ASSERT( psa_hash_abort( &func ) );
1522 PSA_ASSERT( psa_hash_abort( &init ) );
1523 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001528void hash_setup( int alg_arg,
1529 int expected_status_arg )
1530{
1531 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001532 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001533 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001534 psa_status_t status;
1535
Gilles Peskine8817f612018-12-18 00:18:46 +01001536 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001537
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001538 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001539 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001540
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001541 /* Whether setup succeeded or failed, abort must succeed. */
1542 PSA_ASSERT( psa_hash_abort( &operation ) );
1543
1544 /* If setup failed, reproduce the failure, so as to
1545 * test the resulting state of the operation object. */
1546 if( status != PSA_SUCCESS )
1547 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1548
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001549 /* Now the operation object should be reusable. */
1550#if defined(KNOWN_SUPPORTED_HASH_ALG)
1551 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1552 PSA_ASSERT( psa_hash_abort( &operation ) );
1553#endif
1554
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001555exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001556 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001557}
1558/* END_CASE */
1559
1560/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001561void hash_compute_fail( int alg_arg, data_t *input,
1562 int output_size_arg, int expected_status_arg )
1563{
1564 psa_algorithm_t alg = alg_arg;
1565 uint8_t *output = NULL;
1566 size_t output_size = output_size_arg;
1567 size_t output_length = INVALID_EXPORT_LENGTH;
1568 psa_status_t expected_status = expected_status_arg;
1569 psa_status_t status;
1570
1571 ASSERT_ALLOC( output, output_size );
1572
1573 PSA_ASSERT( psa_crypto_init( ) );
1574
1575 status = psa_hash_compute( alg, input->x, input->len,
1576 output, output_size, &output_length );
1577 TEST_EQUAL( status, expected_status );
1578 TEST_ASSERT( output_length <= output_size );
1579
1580exit:
1581 mbedtls_free( output );
1582 PSA_DONE( );
1583}
1584/* END_CASE */
1585
1586/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001587void hash_compare_fail( int alg_arg, data_t *input,
1588 data_t *reference_hash,
1589 int expected_status_arg )
1590{
1591 psa_algorithm_t alg = alg_arg;
1592 psa_status_t expected_status = expected_status_arg;
1593 psa_status_t status;
1594
1595 PSA_ASSERT( psa_crypto_init( ) );
1596
1597 status = psa_hash_compare( alg, input->x, input->len,
1598 reference_hash->x, reference_hash->len );
1599 TEST_EQUAL( status, expected_status );
1600
1601exit:
1602 PSA_DONE( );
1603}
1604/* END_CASE */
1605
1606/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001607void hash_compute_compare( int alg_arg, data_t *input,
1608 data_t *expected_output )
1609{
1610 psa_algorithm_t alg = alg_arg;
1611 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1612 size_t output_length = INVALID_EXPORT_LENGTH;
1613 size_t i;
1614
1615 PSA_ASSERT( psa_crypto_init( ) );
1616
1617 /* Compute with tight buffer */
1618 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001619 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001620 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001621 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001622 ASSERT_COMPARE( output, output_length,
1623 expected_output->x, expected_output->len );
1624
1625 /* Compute with larger buffer */
1626 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1627 output, sizeof( output ),
1628 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001629 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001630 ASSERT_COMPARE( output, output_length,
1631 expected_output->x, expected_output->len );
1632
1633 /* Compare with correct hash */
1634 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1635 output, output_length ) );
1636
1637 /* Compare with trailing garbage */
1638 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1639 output, output_length + 1 ),
1640 PSA_ERROR_INVALID_SIGNATURE );
1641
1642 /* Compare with truncated hash */
1643 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1644 output, output_length - 1 ),
1645 PSA_ERROR_INVALID_SIGNATURE );
1646
1647 /* Compare with corrupted value */
1648 for( i = 0; i < output_length; i++ )
1649 {
Chris Jones9634bb12021-01-20 15:56:42 +00001650 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001651 output[i] ^= 1;
1652 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1653 output, output_length ),
1654 PSA_ERROR_INVALID_SIGNATURE );
1655 output[i] ^= 1;
1656 }
1657
1658exit:
1659 PSA_DONE( );
1660}
1661/* END_CASE */
1662
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001663/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001664void hash_bad_order( )
1665{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001666 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001667 unsigned char input[] = "";
1668 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001669 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001670 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1671 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1672 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001673 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001674 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001675 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001676
Gilles Peskine8817f612018-12-18 00:18:46 +01001677 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001678
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001679 /* Call setup twice in a row. */
1680 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001681 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001682 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1683 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001684 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001685 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001686 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001687
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001688 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001689 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001690 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001691 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001692
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001693 /* Check that update calls abort on error. */
1694 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman54f73512021-06-24 18:14:52 +01001695 operation.id = UINT_MAX;
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001696 ASSERT_OPERATION_IS_ACTIVE( operation );
1697 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1698 PSA_ERROR_BAD_STATE );
1699 ASSERT_OPERATION_IS_INACTIVE( operation );
1700 PSA_ASSERT( psa_hash_abort( &operation ) );
1701 ASSERT_OPERATION_IS_INACTIVE( operation );
1702
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001703 /* Call update after finish. */
1704 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1705 PSA_ASSERT( psa_hash_finish( &operation,
1706 hash, sizeof( hash ), &hash_len ) );
1707 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001708 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001709 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001710
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001711 /* Call verify without calling setup beforehand. */
1712 TEST_EQUAL( psa_hash_verify( &operation,
1713 valid_hash, sizeof( valid_hash ) ),
1714 PSA_ERROR_BAD_STATE );
1715 PSA_ASSERT( psa_hash_abort( &operation ) );
1716
1717 /* Call verify after finish. */
1718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1719 PSA_ASSERT( psa_hash_finish( &operation,
1720 hash, sizeof( hash ), &hash_len ) );
1721 TEST_EQUAL( psa_hash_verify( &operation,
1722 valid_hash, sizeof( valid_hash ) ),
1723 PSA_ERROR_BAD_STATE );
1724 PSA_ASSERT( psa_hash_abort( &operation ) );
1725
1726 /* Call verify twice in a row. */
1727 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001728 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001729 PSA_ASSERT( psa_hash_verify( &operation,
1730 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001731 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001732 TEST_EQUAL( psa_hash_verify( &operation,
1733 valid_hash, sizeof( valid_hash ) ),
1734 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001735 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001736 PSA_ASSERT( psa_hash_abort( &operation ) );
1737
1738 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001739 TEST_EQUAL( psa_hash_finish( &operation,
1740 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001741 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001742 PSA_ASSERT( psa_hash_abort( &operation ) );
1743
1744 /* Call finish twice in a row. */
1745 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1746 PSA_ASSERT( psa_hash_finish( &operation,
1747 hash, sizeof( hash ), &hash_len ) );
1748 TEST_EQUAL( psa_hash_finish( &operation,
1749 hash, sizeof( hash ), &hash_len ),
1750 PSA_ERROR_BAD_STATE );
1751 PSA_ASSERT( psa_hash_abort( &operation ) );
1752
1753 /* Call finish after calling verify. */
1754 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1755 PSA_ASSERT( psa_hash_verify( &operation,
1756 valid_hash, sizeof( valid_hash ) ) );
1757 TEST_EQUAL( psa_hash_finish( &operation,
1758 hash, sizeof( hash ), &hash_len ),
1759 PSA_ERROR_BAD_STATE );
1760 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001761
1762exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001763 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001764}
1765/* END_CASE */
1766
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001767/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001768void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001769{
1770 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001771 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1772 * appended to it */
1773 unsigned char hash[] = {
1774 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1775 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1776 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001777 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001778 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001779
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001781
itayzafrir27e69452018-11-01 14:26:34 +02001782 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001784 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001785 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001786 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001787 ASSERT_OPERATION_IS_INACTIVE( operation );
1788 PSA_ASSERT( psa_hash_abort( &operation ) );
1789 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001790
itayzafrir27e69452018-11-01 14:26:34 +02001791 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001792 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001793 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001794 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001795
itayzafrir27e69452018-11-01 14:26:34 +02001796 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001797 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001798 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001799 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001800
itayzafrirec93d302018-10-18 18:01:10 +03001801exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001802 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001803}
1804/* END_CASE */
1805
Ronald Cronee414c72021-03-18 18:50:08 +01001806/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001807void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001808{
1809 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001810 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001811 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001812 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001813 size_t hash_len;
1814
Gilles Peskine8817f612018-12-18 00:18:46 +01001815 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001816
itayzafrir58028322018-10-25 10:22:01 +03001817 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001818 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001819 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001820 hash, expected_size - 1, &hash_len ),
1821 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001822
1823exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001824 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001825}
1826/* END_CASE */
1827
Ronald Cronee414c72021-03-18 18:50:08 +01001828/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001829void hash_clone_source_state( )
1830{
1831 psa_algorithm_t alg = PSA_ALG_SHA_256;
1832 unsigned char hash[PSA_HASH_MAX_SIZE];
1833 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1834 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1835 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1836 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1837 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1838 size_t hash_len;
1839
1840 PSA_ASSERT( psa_crypto_init( ) );
1841 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1842
1843 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1844 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1845 PSA_ASSERT( psa_hash_finish( &op_finished,
1846 hash, sizeof( hash ), &hash_len ) );
1847 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1848 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1849
1850 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1851 PSA_ERROR_BAD_STATE );
1852
1853 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1854 PSA_ASSERT( psa_hash_finish( &op_init,
1855 hash, sizeof( hash ), &hash_len ) );
1856 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1857 PSA_ASSERT( psa_hash_finish( &op_finished,
1858 hash, sizeof( hash ), &hash_len ) );
1859 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1860 PSA_ASSERT( psa_hash_finish( &op_aborted,
1861 hash, sizeof( hash ), &hash_len ) );
1862
1863exit:
1864 psa_hash_abort( &op_source );
1865 psa_hash_abort( &op_init );
1866 psa_hash_abort( &op_setup );
1867 psa_hash_abort( &op_finished );
1868 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001869 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001870}
1871/* END_CASE */
1872
Ronald Cronee414c72021-03-18 18:50:08 +01001873/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001874void hash_clone_target_state( )
1875{
1876 psa_algorithm_t alg = PSA_ALG_SHA_256;
1877 unsigned char hash[PSA_HASH_MAX_SIZE];
1878 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1879 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1880 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1881 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1882 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1883 size_t hash_len;
1884
1885 PSA_ASSERT( psa_crypto_init( ) );
1886
1887 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1888 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1889 PSA_ASSERT( psa_hash_finish( &op_finished,
1890 hash, sizeof( hash ), &hash_len ) );
1891 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1892 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1893
1894 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1895 PSA_ASSERT( psa_hash_finish( &op_target,
1896 hash, sizeof( hash ), &hash_len ) );
1897
1898 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1899 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1900 PSA_ERROR_BAD_STATE );
1901 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1902 PSA_ERROR_BAD_STATE );
1903
1904exit:
1905 psa_hash_abort( &op_target );
1906 psa_hash_abort( &op_init );
1907 psa_hash_abort( &op_setup );
1908 psa_hash_abort( &op_finished );
1909 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001910 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001911}
1912/* END_CASE */
1913
itayzafrir58028322018-10-25 10:22:01 +03001914/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001915void mac_operation_init( )
1916{
Jaeden Amero252ef282019-02-15 14:05:35 +00001917 const uint8_t input[1] = { 0 };
1918
Jaeden Amero769ce272019-01-04 11:48:03 +00001919 /* Test each valid way of initializing the object, except for `= {0}`, as
1920 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1921 * though it's OK by the C standard. We could test for this, but we'd need
1922 * to supress the Clang warning for the test. */
1923 psa_mac_operation_t func = psa_mac_operation_init( );
1924 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1925 psa_mac_operation_t zero;
1926
1927 memset( &zero, 0, sizeof( zero ) );
1928
Jaeden Amero252ef282019-02-15 14:05:35 +00001929 /* A freshly-initialized MAC operation should not be usable. */
1930 TEST_EQUAL( psa_mac_update( &func,
1931 input, sizeof( input ) ),
1932 PSA_ERROR_BAD_STATE );
1933 TEST_EQUAL( psa_mac_update( &init,
1934 input, sizeof( input ) ),
1935 PSA_ERROR_BAD_STATE );
1936 TEST_EQUAL( psa_mac_update( &zero,
1937 input, sizeof( input ) ),
1938 PSA_ERROR_BAD_STATE );
1939
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001940 /* A default MAC operation should be abortable without error. */
1941 PSA_ASSERT( psa_mac_abort( &func ) );
1942 PSA_ASSERT( psa_mac_abort( &init ) );
1943 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001944}
1945/* END_CASE */
1946
1947/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948void mac_setup( int key_type_arg,
1949 data_t *key,
1950 int alg_arg,
1951 int expected_status_arg )
1952{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001953 psa_key_type_t key_type = key_type_arg;
1954 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001955 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001956 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001957 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1958#if defined(KNOWN_SUPPORTED_MAC_ALG)
1959 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1960#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001961
Gilles Peskine8817f612018-12-18 00:18:46 +01001962 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001963
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001964 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1965 &operation, &status ) )
1966 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001967 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001968
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001969 /* The operation object should be reusable. */
1970#if defined(KNOWN_SUPPORTED_MAC_ALG)
1971 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1972 smoke_test_key_data,
1973 sizeof( smoke_test_key_data ),
1974 KNOWN_SUPPORTED_MAC_ALG,
1975 &operation, &status ) )
1976 goto exit;
1977 TEST_EQUAL( status, PSA_SUCCESS );
1978#endif
1979
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001980exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001981 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001982}
1983/* END_CASE */
1984
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001985/* 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 +00001986void mac_bad_order( )
1987{
Ronald Cron5425a212020-08-04 14:58:35 +02001988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001989 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1990 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001991 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001992 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1993 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1994 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001996 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1997 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1998 size_t sign_mac_length = 0;
1999 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2000 const uint8_t verify_mac[] = {
2001 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2002 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2003 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2004
2005 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002007 psa_set_key_algorithm( &attributes, alg );
2008 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002009
Ronald Cron5425a212020-08-04 14:58:35 +02002010 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2011 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002012
Jaeden Amero252ef282019-02-15 14:05:35 +00002013 /* Call update without calling setup beforehand. */
2014 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2015 PSA_ERROR_BAD_STATE );
2016 PSA_ASSERT( psa_mac_abort( &operation ) );
2017
2018 /* Call sign finish without calling setup beforehand. */
2019 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2020 &sign_mac_length),
2021 PSA_ERROR_BAD_STATE );
2022 PSA_ASSERT( psa_mac_abort( &operation ) );
2023
2024 /* Call verify finish without calling setup beforehand. */
2025 TEST_EQUAL( psa_mac_verify_finish( &operation,
2026 verify_mac, sizeof( verify_mac ) ),
2027 PSA_ERROR_BAD_STATE );
2028 PSA_ASSERT( psa_mac_abort( &operation ) );
2029
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002030 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002031 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002032 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002033 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002034 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002035 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002036 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002037 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002038
Jaeden Amero252ef282019-02-15 14:05:35 +00002039 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002040 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002041 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2042 PSA_ASSERT( psa_mac_sign_finish( &operation,
2043 sign_mac, sizeof( sign_mac ),
2044 &sign_mac_length ) );
2045 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2046 PSA_ERROR_BAD_STATE );
2047 PSA_ASSERT( psa_mac_abort( &operation ) );
2048
2049 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002050 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002051 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2052 PSA_ASSERT( psa_mac_verify_finish( &operation,
2053 verify_mac, sizeof( verify_mac ) ) );
2054 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2055 PSA_ERROR_BAD_STATE );
2056 PSA_ASSERT( psa_mac_abort( &operation ) );
2057
2058 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002059 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002060 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2061 PSA_ASSERT( psa_mac_sign_finish( &operation,
2062 sign_mac, sizeof( sign_mac ),
2063 &sign_mac_length ) );
2064 TEST_EQUAL( psa_mac_sign_finish( &operation,
2065 sign_mac, sizeof( sign_mac ),
2066 &sign_mac_length ),
2067 PSA_ERROR_BAD_STATE );
2068 PSA_ASSERT( psa_mac_abort( &operation ) );
2069
2070 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002071 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002072 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2073 PSA_ASSERT( psa_mac_verify_finish( &operation,
2074 verify_mac, sizeof( verify_mac ) ) );
2075 TEST_EQUAL( psa_mac_verify_finish( &operation,
2076 verify_mac, sizeof( verify_mac ) ),
2077 PSA_ERROR_BAD_STATE );
2078 PSA_ASSERT( psa_mac_abort( &operation ) );
2079
2080 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002081 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002082 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002083 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002084 TEST_EQUAL( psa_mac_verify_finish( &operation,
2085 verify_mac, sizeof( verify_mac ) ),
2086 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002087 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002088 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002089 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002090
2091 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002092 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002093 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002094 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002095 TEST_EQUAL( psa_mac_sign_finish( &operation,
2096 sign_mac, sizeof( sign_mac ),
2097 &sign_mac_length ),
2098 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002099 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002100 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002101 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002102
Ronald Cron5425a212020-08-04 14:58:35 +02002103 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002104
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002106 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002107}
2108/* END_CASE */
2109
2110/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002111void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002112 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002113 int alg_arg,
2114 data_t *input,
2115 data_t *expected_mac )
2116{
Ronald Cron5425a212020-08-04 14:58:35 +02002117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002118 psa_key_type_t key_type = key_type_arg;
2119 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002120 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002122 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002123 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002124 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002125 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002126 const size_t output_sizes_to_test[] = {
2127 0,
2128 1,
2129 expected_mac->len - 1,
2130 expected_mac->len,
2131 expected_mac->len + 1,
2132 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002133
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002134 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002135 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002136 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002137
Gilles Peskine8817f612018-12-18 00:18:46 +01002138 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002139
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002141 psa_set_key_algorithm( &attributes, alg );
2142 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002143
Ronald Cron5425a212020-08-04 14:58:35 +02002144 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2145 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002146
Gilles Peskine8b356b52020-08-25 23:44:59 +02002147 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2148 {
2149 const size_t output_size = output_sizes_to_test[i];
2150 psa_status_t expected_status =
2151 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2152 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002153
Chris Jones9634bb12021-01-20 15:56:42 +00002154 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002155 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002156
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002157 /* Calculate the MAC, one-shot case. */
2158 TEST_EQUAL( psa_mac_compute( key, alg,
2159 input->x, input->len,
2160 actual_mac, output_size, &mac_length ),
2161 expected_status );
2162 if( expected_status == PSA_SUCCESS )
2163 {
2164 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2165 actual_mac, mac_length );
2166 }
2167
2168 if( output_size > 0 )
2169 memset( actual_mac, 0, output_size );
2170
2171 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002172 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002173 PSA_ASSERT( psa_mac_update( &operation,
2174 input->x, input->len ) );
2175 TEST_EQUAL( psa_mac_sign_finish( &operation,
2176 actual_mac, output_size,
2177 &mac_length ),
2178 expected_status );
2179 PSA_ASSERT( psa_mac_abort( &operation ) );
2180
2181 if( expected_status == PSA_SUCCESS )
2182 {
2183 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2184 actual_mac, mac_length );
2185 }
2186 mbedtls_free( actual_mac );
2187 actual_mac = NULL;
2188 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002189
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002190exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002191 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002192 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002193 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002194 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002195}
2196/* END_CASE */
2197
2198/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002199void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002200 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002201 int alg_arg,
2202 data_t *input,
2203 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002204{
Ronald Cron5425a212020-08-04 14:58:35 +02002205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002206 psa_key_type_t key_type = key_type_arg;
2207 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002208 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002210 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002211
Gilles Peskine69c12672018-06-28 00:07:19 +02002212 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2213
Gilles Peskine8817f612018-12-18 00:18:46 +01002214 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002217 psa_set_key_algorithm( &attributes, alg );
2218 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002219
Ronald Cron5425a212020-08-04 14:58:35 +02002220 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2221 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002222
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002223 /* Verify correct MAC, one-shot case. */
2224 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2225 expected_mac->x, expected_mac->len ) );
2226
2227 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002228 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002229 PSA_ASSERT( psa_mac_update( &operation,
2230 input->x, input->len ) );
2231 PSA_ASSERT( psa_mac_verify_finish( &operation,
2232 expected_mac->x,
2233 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002234
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002235 /* Test a MAC that's too short, one-shot case. */
2236 TEST_EQUAL( psa_mac_verify( key, alg,
2237 input->x, input->len,
2238 expected_mac->x,
2239 expected_mac->len - 1 ),
2240 PSA_ERROR_INVALID_SIGNATURE );
2241
2242 /* Test a MAC that's too short, 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 expected_mac->x,
2248 expected_mac->len - 1 ),
2249 PSA_ERROR_INVALID_SIGNATURE );
2250
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002251 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002252 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2253 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002254 TEST_EQUAL( psa_mac_verify( key, alg,
2255 input->x, input->len,
2256 perturbed_mac, expected_mac->len + 1 ),
2257 PSA_ERROR_INVALID_SIGNATURE );
2258
2259 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002260 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002261 PSA_ASSERT( psa_mac_update( &operation,
2262 input->x, input->len ) );
2263 TEST_EQUAL( psa_mac_verify_finish( &operation,
2264 perturbed_mac,
2265 expected_mac->len + 1 ),
2266 PSA_ERROR_INVALID_SIGNATURE );
2267
2268 /* Test changing one byte. */
2269 for( size_t i = 0; i < expected_mac->len; i++ )
2270 {
Chris Jones9634bb12021-01-20 15:56:42 +00002271 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002272 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002273
2274 TEST_EQUAL( psa_mac_verify( key, alg,
2275 input->x, input->len,
2276 perturbed_mac, expected_mac->len ),
2277 PSA_ERROR_INVALID_SIGNATURE );
2278
Ronald Cron5425a212020-08-04 14:58:35 +02002279 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002280 PSA_ASSERT( psa_mac_update( &operation,
2281 input->x, input->len ) );
2282 TEST_EQUAL( psa_mac_verify_finish( &operation,
2283 perturbed_mac,
2284 expected_mac->len ),
2285 PSA_ERROR_INVALID_SIGNATURE );
2286 perturbed_mac[i] ^= 1;
2287 }
2288
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002290 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002291 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002292 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002293 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002294}
2295/* END_CASE */
2296
2297/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002298void cipher_operation_init( )
2299{
Jaeden Ameroab439972019-02-15 14:12:05 +00002300 const uint8_t input[1] = { 0 };
2301 unsigned char output[1] = { 0 };
2302 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002303 /* Test each valid way of initializing the object, except for `= {0}`, as
2304 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2305 * though it's OK by the C standard. We could test for this, but we'd need
2306 * to supress the Clang warning for the test. */
2307 psa_cipher_operation_t func = psa_cipher_operation_init( );
2308 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2309 psa_cipher_operation_t zero;
2310
2311 memset( &zero, 0, sizeof( zero ) );
2312
Jaeden Ameroab439972019-02-15 14:12:05 +00002313 /* A freshly-initialized cipher operation should not be usable. */
2314 TEST_EQUAL( psa_cipher_update( &func,
2315 input, sizeof( input ),
2316 output, sizeof( output ),
2317 &output_length ),
2318 PSA_ERROR_BAD_STATE );
2319 TEST_EQUAL( psa_cipher_update( &init,
2320 input, sizeof( input ),
2321 output, sizeof( output ),
2322 &output_length ),
2323 PSA_ERROR_BAD_STATE );
2324 TEST_EQUAL( psa_cipher_update( &zero,
2325 input, sizeof( input ),
2326 output, sizeof( output ),
2327 &output_length ),
2328 PSA_ERROR_BAD_STATE );
2329
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002330 /* A default cipher operation should be abortable without error. */
2331 PSA_ASSERT( psa_cipher_abort( &func ) );
2332 PSA_ASSERT( psa_cipher_abort( &init ) );
2333 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002334}
2335/* END_CASE */
2336
2337/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338void cipher_setup( int key_type_arg,
2339 data_t *key,
2340 int alg_arg,
2341 int expected_status_arg )
2342{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002343 psa_key_type_t key_type = key_type_arg;
2344 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002345 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002346 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002348#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002349 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2350#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002351
Gilles Peskine8817f612018-12-18 00:18:46 +01002352 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002353
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002354 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2355 &operation, &status ) )
2356 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002357 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002358
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002359 /* The operation object should be reusable. */
2360#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2361 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2362 smoke_test_key_data,
2363 sizeof( smoke_test_key_data ),
2364 KNOWN_SUPPORTED_CIPHER_ALG,
2365 &operation, &status ) )
2366 goto exit;
2367 TEST_EQUAL( status, PSA_SUCCESS );
2368#endif
2369
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002370exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002371 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002372 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002373}
2374/* END_CASE */
2375
Ronald Cronee414c72021-03-18 18:50:08 +01002376/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002377void cipher_bad_order( )
2378{
Ronald Cron5425a212020-08-04 14:58:35 +02002379 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002380 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2381 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002383 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002384 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002385 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002386 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2387 0xaa, 0xaa, 0xaa, 0xaa };
2388 const uint8_t text[] = {
2389 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2390 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002391 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002392 size_t length = 0;
2393
2394 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002395 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2396 psa_set_key_algorithm( &attributes, alg );
2397 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002398 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2399 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002400
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002401 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002402 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002403 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002404 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002405 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002406 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002407 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002408 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002409
2410 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002411 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002412 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002413 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002414 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002415 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002416 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002417 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002418
Jaeden Ameroab439972019-02-15 14:12:05 +00002419 /* Generate an IV without calling setup beforehand. */
2420 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2421 buffer, sizeof( buffer ),
2422 &length ),
2423 PSA_ERROR_BAD_STATE );
2424 PSA_ASSERT( psa_cipher_abort( &operation ) );
2425
2426 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002427 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002428 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2429 buffer, sizeof( buffer ),
2430 &length ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002431 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002432 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2433 buffer, sizeof( buffer ),
2434 &length ),
2435 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002436 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002437 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002438 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002439
2440 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002441 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002442 PSA_ASSERT( psa_cipher_set_iv( &operation,
2443 iv, sizeof( iv ) ) );
2444 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2445 buffer, sizeof( buffer ),
2446 &length ),
2447 PSA_ERROR_BAD_STATE );
2448 PSA_ASSERT( psa_cipher_abort( &operation ) );
2449
2450 /* Set an IV without calling setup beforehand. */
2451 TEST_EQUAL( psa_cipher_set_iv( &operation,
2452 iv, sizeof( iv ) ),
2453 PSA_ERROR_BAD_STATE );
2454 PSA_ASSERT( psa_cipher_abort( &operation ) );
2455
2456 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002457 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002458 PSA_ASSERT( psa_cipher_set_iv( &operation,
2459 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002460 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002461 TEST_EQUAL( psa_cipher_set_iv( &operation,
2462 iv, sizeof( iv ) ),
2463 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002464 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002465 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002466 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002467
2468 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002469 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002470 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2471 buffer, sizeof( buffer ),
2472 &length ) );
2473 TEST_EQUAL( psa_cipher_set_iv( &operation,
2474 iv, sizeof( iv ) ),
2475 PSA_ERROR_BAD_STATE );
2476 PSA_ASSERT( psa_cipher_abort( &operation ) );
2477
2478 /* Call update without calling setup beforehand. */
2479 TEST_EQUAL( psa_cipher_update( &operation,
2480 text, sizeof( text ),
2481 buffer, sizeof( buffer ),
2482 &length ),
2483 PSA_ERROR_BAD_STATE );
2484 PSA_ASSERT( psa_cipher_abort( &operation ) );
2485
2486 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002487 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002488 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002489 TEST_EQUAL( psa_cipher_update( &operation,
2490 text, sizeof( text ),
2491 buffer, sizeof( buffer ),
2492 &length ),
2493 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002494 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002495 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002496 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002497
2498 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002499 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002500 PSA_ASSERT( psa_cipher_set_iv( &operation,
2501 iv, sizeof( iv ) ) );
2502 PSA_ASSERT( psa_cipher_finish( &operation,
2503 buffer, sizeof( buffer ), &length ) );
2504 TEST_EQUAL( psa_cipher_update( &operation,
2505 text, sizeof( text ),
2506 buffer, sizeof( buffer ),
2507 &length ),
2508 PSA_ERROR_BAD_STATE );
2509 PSA_ASSERT( psa_cipher_abort( &operation ) );
2510
2511 /* Call finish without calling setup beforehand. */
2512 TEST_EQUAL( psa_cipher_finish( &operation,
2513 buffer, sizeof( buffer ), &length ),
2514 PSA_ERROR_BAD_STATE );
2515 PSA_ASSERT( psa_cipher_abort( &operation ) );
2516
2517 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002518 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002519 /* Not calling update means we are encrypting an empty buffer, which is OK
2520 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002521 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002522 TEST_EQUAL( psa_cipher_finish( &operation,
2523 buffer, sizeof( buffer ), &length ),
2524 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002525 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002526 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002527 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002528
2529 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002530 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002531 PSA_ASSERT( psa_cipher_set_iv( &operation,
2532 iv, sizeof( iv ) ) );
2533 PSA_ASSERT( psa_cipher_finish( &operation,
2534 buffer, sizeof( buffer ), &length ) );
2535 TEST_EQUAL( psa_cipher_finish( &operation,
2536 buffer, sizeof( buffer ), &length ),
2537 PSA_ERROR_BAD_STATE );
2538 PSA_ASSERT( psa_cipher_abort( &operation ) );
2539
Ronald Cron5425a212020-08-04 14:58:35 +02002540 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002541
Jaeden Ameroab439972019-02-15 14:12:05 +00002542exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002543 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002544 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002545}
2546/* END_CASE */
2547
2548/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002549void cipher_encrypt_fail( int alg_arg,
2550 int key_type_arg,
2551 data_t *key_data,
2552 data_t *input,
2553 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002554{
Ronald Cron5425a212020-08-04 14:58:35 +02002555 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002556 psa_status_t status;
2557 psa_key_type_t key_type = key_type_arg;
2558 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002559 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002560 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002561 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002562 size_t output_length = 0;
2563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2564
2565 if ( PSA_ERROR_BAD_STATE != expected_status )
2566 {
2567 PSA_ASSERT( psa_crypto_init( ) );
2568
2569 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2570 psa_set_key_algorithm( &attributes, alg );
2571 psa_set_key_type( &attributes, key_type );
2572
2573 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2574 input->len );
2575 ASSERT_ALLOC( output, output_buffer_size );
2576
2577 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2578 &key ) );
2579 }
2580
2581 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2582 output_buffer_size, &output_length );
2583
2584 TEST_EQUAL( status, expected_status );
2585
2586exit:
2587 mbedtls_free( output );
2588 psa_destroy_key( key );
2589 PSA_DONE( );
2590}
2591/* END_CASE */
2592
2593/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002594void cipher_encrypt_alg_without_iv( int alg_arg,
2595 int key_type_arg,
2596 data_t *key_data,
2597 data_t *input,
2598 data_t *expected_output )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002599{
2600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2601 psa_key_type_t key_type = key_type_arg;
2602 psa_algorithm_t alg = alg_arg;
2603 unsigned char *output = NULL;
2604 size_t output_buffer_size = 0;
2605 size_t output_length = 0;
2606 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2607
2608 PSA_ASSERT( psa_crypto_init( ) );
2609
2610 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2611 psa_set_key_algorithm( &attributes, alg );
2612 psa_set_key_type( &attributes, key_type );
2613
2614 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2615 ASSERT_ALLOC( output, output_buffer_size );
2616
2617 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2618 &key ) );
2619
2620 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2621 output_buffer_size, &output_length ) );
2622 TEST_ASSERT( output_length <=
2623 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2624 TEST_ASSERT( output_length <=
2625 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2626
2627 ASSERT_COMPARE( expected_output->x, expected_output->len,
2628 output, output_length );
2629exit:
2630 mbedtls_free( output );
2631 psa_destroy_key( key );
2632 PSA_DONE( );
2633}
2634/* END_CASE */
2635
2636/* BEGIN_CASE */
Paul Elliotted33ef12021-07-14 12:31:21 +01002637void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2638{
2639 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2640 psa_algorithm_t alg = alg_arg;
2641 psa_key_type_t key_type = key_type_arg;
2642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2643 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2644 psa_status_t status;
2645
2646 PSA_ASSERT( psa_crypto_init( ) );
2647
2648 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2649 psa_set_key_algorithm( &attributes, alg );
2650 psa_set_key_type( &attributes, key_type );
2651
2652 /* Usage of either of these two size macros would cause divide by zero
2653 * with incorrect key types previously. Input length should be irrelevant
2654 * here. */
2655 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2656 0 );
2657 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2658
2659
2660 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2661 &key ) );
2662
2663 /* Should fail due to invalid alg type (to support invalid key type).
2664 * Encrypt or decrypt will end up in the same place. */
2665 status = psa_cipher_encrypt_setup( &operation, key, alg );
2666
2667 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2668
2669exit:
2670 psa_cipher_abort( &operation );
2671 psa_destroy_key( key );
2672 PSA_DONE( );
2673}
2674/* END_CASE */
2675
2676/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002677void cipher_encrypt_validation( int alg_arg,
2678 int key_type_arg,
2679 data_t *key_data,
2680 data_t *input )
2681{
2682 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2683 psa_key_type_t key_type = key_type_arg;
2684 psa_algorithm_t alg = alg_arg;
2685 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2686 unsigned char *output1 = NULL;
2687 size_t output1_buffer_size = 0;
2688 size_t output1_length = 0;
2689 unsigned char *output2 = NULL;
2690 size_t output2_buffer_size = 0;
2691 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002692 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002693 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002694 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002695
Gilles Peskine8817f612018-12-18 00:18:46 +01002696 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002697
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002698 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2699 psa_set_key_algorithm( &attributes, alg );
2700 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002701
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002702 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2703 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2704 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2705 ASSERT_ALLOC( output1, output1_buffer_size );
2706 ASSERT_ALLOC( output2, output2_buffer_size );
2707
Ronald Cron5425a212020-08-04 14:58:35 +02002708 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2709 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002711 /* The one-shot cipher encryption uses generated iv so validating
2712 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002713 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2714 output1_buffer_size, &output1_length ) );
2715 TEST_ASSERT( output1_length <=
2716 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2717 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002718 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002719
2720 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2721 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002722
Gilles Peskine8817f612018-12-18 00:18:46 +01002723 PSA_ASSERT( psa_cipher_update( &operation,
2724 input->x, input->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002725 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002726 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002727 TEST_ASSERT( function_output_length <=
2728 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2729 TEST_ASSERT( function_output_length <=
2730 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002731 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002732
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002733 PSA_ASSERT( psa_cipher_finish( &operation,
2734 output2 + output2_length,
2735 output2_buffer_size - output2_length,
2736 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002737 TEST_ASSERT( function_output_length <=
2738 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2739 TEST_ASSERT( function_output_length <=
2740 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002741 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002742
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002743 PSA_ASSERT( psa_cipher_abort( &operation ) );
2744 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2745 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002746
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002748 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002749 mbedtls_free( output1 );
2750 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002751 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002752 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002753}
2754/* END_CASE */
2755
2756/* BEGIN_CASE */
2757void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002758 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002759 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002760 int first_part_size_arg,
2761 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002762 data_t *expected_output,
2763 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002764{
Ronald Cron5425a212020-08-04 14:58:35 +02002765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002766 psa_key_type_t key_type = key_type_arg;
2767 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002768 psa_status_t status;
2769 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002770 size_t first_part_size = first_part_size_arg;
2771 size_t output1_length = output1_length_arg;
2772 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002773 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002774 size_t output_buffer_size = 0;
2775 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002776 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002777 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002778 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002779
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002781
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2783 psa_set_key_algorithm( &attributes, alg );
2784 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002785
Ronald Cron5425a212020-08-04 14:58:35 +02002786 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2787 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788
Ronald Cron5425a212020-08-04 14:58:35 +02002789 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002790
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002791 if( iv->len > 0 )
2792 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002793 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002794 }
2795
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002796 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2797 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002798 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002799
Gilles Peskinee0866522019-02-19 19:44:00 +01002800 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002801 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2802 output, output_buffer_size,
2803 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002804 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002805 TEST_ASSERT( function_output_length <=
2806 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2807 TEST_ASSERT( function_output_length <=
2808 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002809 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002810
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002811 if( first_part_size < input->len )
2812 {
2813 PSA_ASSERT( psa_cipher_update( &operation,
2814 input->x + first_part_size,
2815 input->len - first_part_size,
2816 ( output_buffer_size == 0 ? NULL :
2817 output + total_output_length ),
2818 output_buffer_size - total_output_length,
2819 &function_output_length ) );
2820 TEST_ASSERT( function_output_length == output2_length );
2821 TEST_ASSERT( function_output_length <=
2822 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2823 alg,
2824 input->len - first_part_size ) );
2825 TEST_ASSERT( function_output_length <=
2826 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2827 total_output_length += function_output_length;
2828 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002829
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002830 status = psa_cipher_finish( &operation,
2831 ( output_buffer_size == 0 ? NULL :
2832 output + total_output_length ),
2833 output_buffer_size - total_output_length,
2834 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002835 TEST_ASSERT( function_output_length <=
2836 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2837 TEST_ASSERT( function_output_length <=
2838 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002839 total_output_length += function_output_length;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002840 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002842 if( expected_status == PSA_SUCCESS )
2843 {
2844 PSA_ASSERT( psa_cipher_abort( &operation ) );
2845
2846 ASSERT_COMPARE( expected_output->x, expected_output->len,
2847 output, total_output_length );
2848 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002849
2850exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002851 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002852 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002853 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002854 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002855}
2856/* END_CASE */
2857
2858/* BEGIN_CASE */
2859void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002860 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002861 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002862 int first_part_size_arg,
2863 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002864 data_t *expected_output,
2865 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002866{
Ronald Cron5425a212020-08-04 14:58:35 +02002867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002868 psa_key_type_t key_type = key_type_arg;
2869 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002870 psa_status_t status;
2871 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002872 size_t first_part_size = first_part_size_arg;
2873 size_t output1_length = output1_length_arg;
2874 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002875 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002876 size_t output_buffer_size = 0;
2877 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002878 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002879 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002884 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2885 psa_set_key_algorithm( &attributes, alg );
2886 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002887
Ronald Cron5425a212020-08-04 14:58:35 +02002888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2889 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002890
Ronald Cron5425a212020-08-04 14:58:35 +02002891 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
Steven Cooreman177deba2020-09-07 17:14:14 +02002893 if( iv->len > 0 )
2894 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002895 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002896 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002897
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002898 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2899 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002900 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901
Gilles Peskinee0866522019-02-19 19:44:00 +01002902 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002903 PSA_ASSERT( psa_cipher_update( &operation,
2904 input->x, first_part_size,
2905 output, output_buffer_size,
2906 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002907 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002908 TEST_ASSERT( function_output_length <=
2909 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2910 TEST_ASSERT( function_output_length <=
2911 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002912 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002913
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002914 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002915 {
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002916 PSA_ASSERT( psa_cipher_update( &operation,
2917 input->x + first_part_size,
2918 input->len - first_part_size,
2919 ( output_buffer_size == 0 ? NULL :
2920 output + total_output_length ),
2921 output_buffer_size - total_output_length,
2922 &function_output_length ) );
2923 TEST_ASSERT( function_output_length == output2_length );
2924 TEST_ASSERT( function_output_length <=
2925 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2926 alg,
2927 input->len - first_part_size ) );
2928 TEST_ASSERT( function_output_length <=
2929 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2930 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002931 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002932
Gilles Peskine50e586b2018-06-08 14:28:46 +02002933 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002934 ( output_buffer_size == 0 ? NULL :
2935 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002936 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002937 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002938 TEST_ASSERT( function_output_length <=
2939 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2940 TEST_ASSERT( function_output_length <=
2941 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002942 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002943 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944
2945 if( expected_status == PSA_SUCCESS )
2946 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002947 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002948
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002949 ASSERT_COMPARE( expected_output->x, expected_output->len,
2950 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002951 }
2952
Gilles Peskine50e586b2018-06-08 14:28:46 +02002953exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002954 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002955 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002956 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002957 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958}
2959/* END_CASE */
2960
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002962void cipher_decrypt_fail( int alg_arg,
2963 int key_type_arg,
2964 data_t *key_data,
2965 data_t *iv,
2966 data_t *input_arg,
2967 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002968{
2969 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2970 psa_status_t status;
2971 psa_key_type_t key_type = key_type_arg;
2972 psa_algorithm_t alg = alg_arg;
2973 psa_status_t expected_status = expected_status_arg;
2974 unsigned char *input = NULL;
2975 size_t input_buffer_size = 0;
2976 unsigned char *output = NULL;
2977 size_t output_buffer_size = 0;
2978 size_t output_length = 0;
2979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2980
2981 if ( PSA_ERROR_BAD_STATE != expected_status )
2982 {
2983 PSA_ASSERT( psa_crypto_init( ) );
2984
2985 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2986 psa_set_key_algorithm( &attributes, alg );
2987 psa_set_key_type( &attributes, key_type );
2988
2989 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2990 &key ) );
2991 }
2992
2993 /* Allocate input buffer and copy the iv and the plaintext */
2994 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2995 if ( input_buffer_size > 0 )
2996 {
2997 ASSERT_ALLOC( input, input_buffer_size );
2998 memcpy( input, iv->x, iv->len );
2999 memcpy( input + iv->len, input_arg->x, input_arg->len );
3000 }
3001
3002 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3003 ASSERT_ALLOC( output, output_buffer_size );
3004
3005 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3006 output_buffer_size, &output_length );
3007 TEST_EQUAL( status, expected_status );
3008
3009exit:
3010 mbedtls_free( input );
3011 mbedtls_free( output );
3012 psa_destroy_key( key );
3013 PSA_DONE( );
3014}
3015/* END_CASE */
3016
3017/* BEGIN_CASE */
3018void cipher_decrypt( int alg_arg,
3019 int key_type_arg,
3020 data_t *key_data,
3021 data_t *iv,
3022 data_t *input_arg,
3023 data_t *expected_output )
3024{
3025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3026 psa_key_type_t key_type = key_type_arg;
3027 psa_algorithm_t alg = alg_arg;
3028 unsigned char *input = NULL;
3029 size_t input_buffer_size = 0;
3030 unsigned char *output = NULL;
3031 size_t output_buffer_size = 0;
3032 size_t output_length = 0;
3033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3034
3035 PSA_ASSERT( psa_crypto_init( ) );
3036
3037 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3038 psa_set_key_algorithm( &attributes, alg );
3039 psa_set_key_type( &attributes, key_type );
3040
3041 /* Allocate input buffer and copy the iv and the plaintext */
3042 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3043 if ( input_buffer_size > 0 )
3044 {
3045 ASSERT_ALLOC( input, input_buffer_size );
3046 memcpy( input, iv->x, iv->len );
3047 memcpy( input + iv->len, input_arg->x, input_arg->len );
3048 }
3049
3050 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3051 ASSERT_ALLOC( output, output_buffer_size );
3052
3053 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3054 &key ) );
3055
3056 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3057 output_buffer_size, &output_length ) );
3058 TEST_ASSERT( output_length <=
3059 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3060 TEST_ASSERT( output_length <=
3061 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3062
3063 ASSERT_COMPARE( expected_output->x, expected_output->len,
3064 output, output_length );
3065exit:
3066 mbedtls_free( input );
3067 mbedtls_free( output );
3068 psa_destroy_key( key );
3069 PSA_DONE( );
3070}
3071/* END_CASE */
3072
3073/* BEGIN_CASE */
3074void cipher_verify_output( int alg_arg,
3075 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003076 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003077 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003078{
Ronald Cron5425a212020-08-04 14:58:35 +02003079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003080 psa_key_type_t key_type = key_type_arg;
3081 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003082 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003083 size_t output1_size = 0;
3084 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003085 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003086 size_t output2_size = 0;
3087 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003089
Gilles Peskine8817f612018-12-18 00:18:46 +01003090 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003092 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3093 psa_set_key_algorithm( &attributes, alg );
3094 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003095
Ronald Cron5425a212020-08-04 14:58:35 +02003096 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3097 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003098 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003099 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003100
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003101 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3102 output1, output1_size,
3103 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003104 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003105 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003106 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003107 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003108
3109 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003110 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003111
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003112 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3113 output2, output2_size,
3114 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003115 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003116 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003117 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003118 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003119
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003120 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003121
3122exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003123 mbedtls_free( output1 );
3124 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003125 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003126 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003127}
3128/* END_CASE */
3129
3130/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003131void cipher_verify_output_multipart( int alg_arg,
3132 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003133 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003134 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003135 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003136{
Ronald Cron5425a212020-08-04 14:58:35 +02003137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003138 psa_key_type_t key_type = key_type_arg;
3139 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003140 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003141 unsigned char iv[16] = {0};
3142 size_t iv_size = 16;
3143 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003144 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003145 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003146 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003147 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003148 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003149 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003150 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003151 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3152 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003156
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3158 psa_set_key_algorithm( &attributes, alg );
3159 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003160
Ronald Cron5425a212020-08-04 14:58:35 +02003161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3162 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003163
Ronald Cron5425a212020-08-04 14:58:35 +02003164 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3165 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003166
Steven Cooreman177deba2020-09-07 17:14:14 +02003167 if( alg != PSA_ALG_ECB_NO_PADDING )
3168 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003169 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3170 iv, iv_size,
3171 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003172 }
3173
gabor-mezei-armceface22021-01-21 12:26:17 +01003174 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3175 TEST_ASSERT( output1_buffer_size <=
3176 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003177 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003178
Gilles Peskinee0866522019-02-19 19:44:00 +01003179 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003180
Gilles Peskine8817f612018-12-18 00:18:46 +01003181 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3182 output1, output1_buffer_size,
3183 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003184 TEST_ASSERT( function_output_length <=
3185 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3186 TEST_ASSERT( function_output_length <=
3187 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003188 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003189
Gilles Peskine8817f612018-12-18 00:18:46 +01003190 PSA_ASSERT( psa_cipher_update( &operation1,
3191 input->x + first_part_size,
3192 input->len - first_part_size,
3193 output1, output1_buffer_size,
3194 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003195 TEST_ASSERT( function_output_length <=
3196 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3197 alg,
3198 input->len - first_part_size ) );
3199 TEST_ASSERT( function_output_length <=
3200 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003201 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003202
Gilles Peskine8817f612018-12-18 00:18:46 +01003203 PSA_ASSERT( psa_cipher_finish( &operation1,
3204 output1 + output1_length,
3205 output1_buffer_size - output1_length,
3206 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003207 TEST_ASSERT( function_output_length <=
3208 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3209 TEST_ASSERT( function_output_length <=
3210 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003211 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003212
Gilles Peskine8817f612018-12-18 00:18:46 +01003213 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003214
Gilles Peskine048b7f02018-06-08 14:20:49 +02003215 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003216 TEST_ASSERT( output2_buffer_size <=
3217 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3218 TEST_ASSERT( output2_buffer_size <=
3219 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003220 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003221
Steven Cooreman177deba2020-09-07 17:14:14 +02003222 if( iv_length > 0 )
3223 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003224 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3225 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003226 }
Moran Pekerded84402018-06-06 16:36:50 +03003227
Gilles Peskine8817f612018-12-18 00:18:46 +01003228 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3229 output2, output2_buffer_size,
3230 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003231 TEST_ASSERT( function_output_length <=
3232 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3233 TEST_ASSERT( function_output_length <=
3234 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003235 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003236
Gilles Peskine8817f612018-12-18 00:18:46 +01003237 PSA_ASSERT( psa_cipher_update( &operation2,
3238 output1 + first_part_size,
3239 output1_length - first_part_size,
3240 output2, output2_buffer_size,
3241 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003242 TEST_ASSERT( function_output_length <=
3243 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3244 alg,
3245 output1_length - first_part_size ) );
3246 TEST_ASSERT( function_output_length <=
3247 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003248 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_cipher_finish( &operation2,
3251 output2 + output2_length,
3252 output2_buffer_size - output2_length,
3253 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003254 TEST_ASSERT( function_output_length <=
3255 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3256 TEST_ASSERT( function_output_length <=
3257 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003258 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003259
Gilles Peskine8817f612018-12-18 00:18:46 +01003260 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003261
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003262 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003263
3264exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003265 psa_cipher_abort( &operation1 );
3266 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003267 mbedtls_free( output1 );
3268 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003269 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003270 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003271}
3272/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003273
Gilles Peskine20035e32018-02-03 22:44:14 +01003274/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003275void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003276 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003277 data_t *nonce,
3278 data_t *additional_data,
3279 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003280 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003281{
Ronald Cron5425a212020-08-04 14:58:35 +02003282 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003283 psa_key_type_t key_type = key_type_arg;
3284 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003285 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003286 unsigned char *output_data = NULL;
3287 size_t output_size = 0;
3288 size_t output_length = 0;
3289 unsigned char *output_data2 = NULL;
3290 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003291 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003292 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003294
Gilles Peskine8817f612018-12-18 00:18:46 +01003295 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003296
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003297 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3298 psa_set_key_algorithm( &attributes, alg );
3299 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003300
Gilles Peskine049c7532019-05-15 20:22:09 +02003301 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003302 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003303 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3304 key_bits = psa_get_key_bits( &attributes );
3305
3306 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3307 alg );
3308 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3309 * should be exact. */
3310 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3311 expected_result != PSA_ERROR_NOT_SUPPORTED )
3312 {
3313 TEST_EQUAL( output_size,
3314 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3315 TEST_ASSERT( output_size <=
3316 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3317 }
3318 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319
Steven Cooremanf49478b2021-02-15 15:19:25 +01003320 status = psa_aead_encrypt( key, alg,
3321 nonce->x, nonce->len,
3322 additional_data->x,
3323 additional_data->len,
3324 input_data->x, input_data->len,
3325 output_data, output_size,
3326 &output_length );
3327
3328 /* If the operation is not supported, just skip and not fail in case the
3329 * encryption involves a common limitation of cryptography hardwares and
3330 * an alternative implementation. */
3331 if( status == PSA_ERROR_NOT_SUPPORTED )
3332 {
3333 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3334 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3335 }
3336
3337 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003338
3339 if( PSA_SUCCESS == expected_result )
3340 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003341 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342
Gilles Peskine003a4a92019-05-14 16:09:40 +02003343 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3344 * should be exact. */
3345 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003346 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003347
gabor-mezei-armceface22021-01-21 12:26:17 +01003348 TEST_ASSERT( input_data->len <=
3349 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3350
Ronald Cron5425a212020-08-04 14:58:35 +02003351 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003352 nonce->x, nonce->len,
3353 additional_data->x,
3354 additional_data->len,
3355 output_data, output_length,
3356 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003357 &output_length2 ),
3358 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003359
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003360 ASSERT_COMPARE( input_data->x, input_data->len,
3361 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003363
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003365 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003366 mbedtls_free( output_data );
3367 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003368 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003369}
3370/* END_CASE */
3371
3372/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003373void aead_encrypt( int key_type_arg, data_t *key_data,
3374 int alg_arg,
3375 data_t *nonce,
3376 data_t *additional_data,
3377 data_t *input_data,
3378 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379{
Ronald Cron5425a212020-08-04 14:58:35 +02003380 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003381 psa_key_type_t key_type = key_type_arg;
3382 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003383 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003384 unsigned char *output_data = NULL;
3385 size_t output_size = 0;
3386 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003387 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003388 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003389
Gilles Peskine8817f612018-12-18 00:18:46 +01003390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003392 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3393 psa_set_key_algorithm( &attributes, alg );
3394 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003395
Gilles Peskine049c7532019-05-15 20:22:09 +02003396 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003397 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003398 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3399 key_bits = psa_get_key_bits( &attributes );
3400
3401 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3402 alg );
3403 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3404 * should be exact. */
3405 TEST_EQUAL( output_size,
3406 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3407 TEST_ASSERT( output_size <=
3408 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3409 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003410
Steven Cooremand588ea12021-01-11 19:36:04 +01003411 status = psa_aead_encrypt( key, alg,
3412 nonce->x, nonce->len,
3413 additional_data->x, additional_data->len,
3414 input_data->x, input_data->len,
3415 output_data, output_size,
3416 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417
Ronald Cron28a45ed2021-02-09 20:35:42 +01003418 /* If the operation is not supported, just skip and not fail in case the
3419 * encryption involves a common limitation of cryptography hardwares and
3420 * an alternative implementation. */
3421 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003422 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003423 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3424 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003425 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003426
3427 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003428 ASSERT_COMPARE( expected_result->x, expected_result->len,
3429 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003430
Gilles Peskinea1cac842018-06-11 19:33:02 +02003431exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003432 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003433 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003434 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003435}
3436/* END_CASE */
3437
3438/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003439void aead_decrypt( int key_type_arg, data_t *key_data,
3440 int alg_arg,
3441 data_t *nonce,
3442 data_t *additional_data,
3443 data_t *input_data,
3444 data_t *expected_data,
3445 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446{
Ronald Cron5425a212020-08-04 14:58:35 +02003447 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003448 psa_key_type_t key_type = key_type_arg;
3449 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003450 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003451 unsigned char *output_data = NULL;
3452 size_t output_size = 0;
3453 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003455 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003456 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003457
Gilles Peskine8817f612018-12-18 00:18:46 +01003458 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003460 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3461 psa_set_key_algorithm( &attributes, alg );
3462 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463
Gilles Peskine049c7532019-05-15 20:22:09 +02003464 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003465 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003466 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3467 key_bits = psa_get_key_bits( &attributes );
3468
3469 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3470 alg );
3471 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3472 expected_result != PSA_ERROR_NOT_SUPPORTED )
3473 {
3474 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3475 * should be exact. */
3476 TEST_EQUAL( output_size,
3477 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3478 TEST_ASSERT( output_size <=
3479 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3480 }
3481 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003482
Steven Cooremand588ea12021-01-11 19:36:04 +01003483 status = psa_aead_decrypt( key, alg,
3484 nonce->x, nonce->len,
3485 additional_data->x,
3486 additional_data->len,
3487 input_data->x, input_data->len,
3488 output_data, output_size,
3489 &output_length );
3490
Ronald Cron28a45ed2021-02-09 20:35:42 +01003491 /* If the operation is not supported, just skip and not fail in case the
3492 * decryption involves a common limitation of cryptography hardwares and
3493 * an alternative implementation. */
3494 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003495 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003496 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3497 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003498 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003499
3500 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003501
Gilles Peskine2d277862018-06-18 15:41:12 +02003502 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003503 ASSERT_COMPARE( expected_data->x, expected_data->len,
3504 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003505
Gilles Peskinea1cac842018-06-11 19:33:02 +02003506exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003507 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003508 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003509 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003510}
3511/* END_CASE */
3512
3513/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003514void signature_size( int type_arg,
3515 int bits,
3516 int alg_arg,
3517 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003518{
3519 psa_key_type_t type = type_arg;
3520 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003521 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003522
Gilles Peskinefe11b722018-12-18 00:24:04 +01003523 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003524#if defined(MBEDTLS_TEST_DEPRECATED)
3525 TEST_EQUAL( actual_size,
3526 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3527#endif /* MBEDTLS_TEST_DEPRECATED */
3528
Gilles Peskinee59236f2018-01-27 23:32:46 +01003529exit:
3530 ;
3531}
3532/* END_CASE */
3533
3534/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003535void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3536 int alg_arg, data_t *input_data,
3537 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003538{
Ronald Cron5425a212020-08-04 14:58:35 +02003539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003540 psa_key_type_t key_type = key_type_arg;
3541 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003542 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003543 unsigned char *signature = NULL;
3544 size_t signature_size;
3545 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003546 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003547
Gilles Peskine8817f612018-12-18 00:18:46 +01003548 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003549
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003550 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003551 psa_set_key_algorithm( &attributes, alg );
3552 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003553
Gilles Peskine049c7532019-05-15 20:22:09 +02003554 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003555 &key ) );
3556 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003557 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003558
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003559 /* Allocate a buffer which has the size advertized by the
3560 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003561 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003562 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003563 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003564 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003565 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003566
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003567 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003568 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003569 input_data->x, input_data->len,
3570 signature, signature_size,
3571 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003572 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003573 ASSERT_COMPARE( output_data->x, output_data->len,
3574 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003575
Gilles Peskine0627f982019-11-26 19:12:16 +01003576#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003577 memset( signature, 0, signature_size );
3578 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003579 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003580 input_data->x, input_data->len,
3581 signature, signature_size,
3582 &signature_length ) );
3583 ASSERT_COMPARE( output_data->x, output_data->len,
3584 signature, signature_length );
3585#endif /* MBEDTLS_TEST_DEPRECATED */
3586
Gilles Peskine20035e32018-02-03 22:44:14 +01003587exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003588 /*
3589 * Key attributes may have been returned by psa_get_key_attributes()
3590 * thus reset them as required.
3591 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003592 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003593
Ronald Cron5425a212020-08-04 14:58:35 +02003594 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003595 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003596 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003597}
3598/* END_CASE */
3599
3600/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003601void sign_hash_fail( int key_type_arg, data_t *key_data,
3602 int alg_arg, data_t *input_data,
3603 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003604{
Ronald Cron5425a212020-08-04 14:58:35 +02003605 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003606 psa_key_type_t key_type = key_type_arg;
3607 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003608 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003609 psa_status_t actual_status;
3610 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003611 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003612 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003614
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003615 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003616
Gilles Peskine8817f612018-12-18 00:18:46 +01003617 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003618
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003619 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003620 psa_set_key_algorithm( &attributes, alg );
3621 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003622
Gilles Peskine049c7532019-05-15 20:22:09 +02003623 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003624 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003625
Ronald Cron5425a212020-08-04 14:58:35 +02003626 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003627 input_data->x, input_data->len,
3628 signature, signature_size,
3629 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003630 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003631 /* The value of *signature_length is unspecified on error, but
3632 * whatever it is, it should be less than signature_size, so that
3633 * if the caller tries to read *signature_length bytes without
3634 * checking the error code then they don't overflow a buffer. */
3635 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003636
Gilles Peskine895242b2019-11-29 12:15:40 +01003637#if defined(MBEDTLS_TEST_DEPRECATED)
3638 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003639 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003640 input_data->x, input_data->len,
3641 signature, signature_size,
3642 &signature_length ),
3643 expected_status );
3644 TEST_ASSERT( signature_length <= signature_size );
3645#endif /* MBEDTLS_TEST_DEPRECATED */
3646
Gilles Peskine20035e32018-02-03 22:44:14 +01003647exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003648 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003649 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003650 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003651 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003652}
3653/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003654
3655/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003656void sign_verify_hash( int key_type_arg, data_t *key_data,
3657 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003658{
Ronald Cron5425a212020-08-04 14:58:35 +02003659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003660 psa_key_type_t key_type = key_type_arg;
3661 psa_algorithm_t alg = alg_arg;
3662 size_t key_bits;
3663 unsigned char *signature = NULL;
3664 size_t signature_size;
3665 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003667
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003669
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003671 psa_set_key_algorithm( &attributes, alg );
3672 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003673
Gilles Peskine049c7532019-05-15 20:22:09 +02003674 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003675 &key ) );
3676 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003677 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003678
3679 /* Allocate a buffer which has the size advertized by the
3680 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003681 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003682 key_bits, alg );
3683 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003684 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003685 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003686
3687 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003688 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003689 input_data->x, input_data->len,
3690 signature, signature_size,
3691 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003692 /* Check that the signature length looks sensible. */
3693 TEST_ASSERT( signature_length <= signature_size );
3694 TEST_ASSERT( signature_length > 0 );
3695
3696 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003697 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003698 input_data->x, input_data->len,
3699 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003700
3701 if( input_data->len != 0 )
3702 {
3703 /* Flip a bit in the input and verify that the signature is now
3704 * detected as invalid. Flip a bit at the beginning, not at the end,
3705 * because ECDSA may ignore the last few bits of the input. */
3706 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003707 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003708 input_data->x, input_data->len,
3709 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003710 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003711 }
3712
3713exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003714 /*
3715 * Key attributes may have been returned by psa_get_key_attributes()
3716 * thus reset them as required.
3717 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003718 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003719
Ronald Cron5425a212020-08-04 14:58:35 +02003720 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003721 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003722 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003723}
3724/* END_CASE */
3725
3726/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003727void verify_hash( int key_type_arg, data_t *key_data,
3728 int alg_arg, data_t *hash_data,
3729 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003730{
Ronald Cron5425a212020-08-04 14:58:35 +02003731 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003732 psa_key_type_t key_type = key_type_arg;
3733 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003734 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003735
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003736 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003737
Gilles Peskine8817f612018-12-18 00:18:46 +01003738 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003739
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003740 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003741 psa_set_key_algorithm( &attributes, alg );
3742 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003743
Gilles Peskine049c7532019-05-15 20:22:09 +02003744 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003745 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003746
Ronald Cron5425a212020-08-04 14:58:35 +02003747 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003748 hash_data->x, hash_data->len,
3749 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003750
3751#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003752 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003753 hash_data->x, hash_data->len,
3754 signature_data->x,
3755 signature_data->len ) );
3756
3757#endif /* MBEDTLS_TEST_DEPRECATED */
3758
itayzafrir5c753392018-05-08 11:18:38 +03003759exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003760 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003761 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003762 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003763}
3764/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003765
3766/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003767void verify_hash_fail( int key_type_arg, data_t *key_data,
3768 int alg_arg, data_t *hash_data,
3769 data_t *signature_data,
3770 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003771{
Ronald Cron5425a212020-08-04 14:58:35 +02003772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003773 psa_key_type_t key_type = key_type_arg;
3774 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003775 psa_status_t actual_status;
3776 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003778
Gilles Peskine8817f612018-12-18 00:18:46 +01003779 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003780
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003781 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003782 psa_set_key_algorithm( &attributes, alg );
3783 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003784
Gilles Peskine049c7532019-05-15 20:22:09 +02003785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003786 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003787
Ronald Cron5425a212020-08-04 14:58:35 +02003788 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003789 hash_data->x, hash_data->len,
3790 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003791 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003792
Gilles Peskine895242b2019-11-29 12:15:40 +01003793#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003794 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003795 hash_data->x, hash_data->len,
3796 signature_data->x, signature_data->len ),
3797 expected_status );
3798#endif /* MBEDTLS_TEST_DEPRECATED */
3799
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003800exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003801 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003802 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003803 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003804}
3805/* END_CASE */
3806
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003807/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003808void sign_message_deterministic( int key_type_arg,
3809 data_t *key_data,
3810 int alg_arg,
3811 data_t *input_data,
3812 data_t *output_data )
3813{
3814 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3815 psa_key_type_t key_type = key_type_arg;
3816 psa_algorithm_t alg = alg_arg;
3817 size_t key_bits;
3818 unsigned char *signature = NULL;
3819 size_t signature_size;
3820 size_t signature_length = 0xdeadbeef;
3821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3822
3823 PSA_ASSERT( psa_crypto_init( ) );
3824
3825 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3826 psa_set_key_algorithm( &attributes, alg );
3827 psa_set_key_type( &attributes, key_type );
3828
3829 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3830 &key ) );
3831 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3832 key_bits = psa_get_key_bits( &attributes );
3833
3834 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3835 TEST_ASSERT( signature_size != 0 );
3836 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3837 ASSERT_ALLOC( signature, signature_size );
3838
3839 PSA_ASSERT( psa_sign_message( key, alg,
3840 input_data->x, input_data->len,
3841 signature, signature_size,
3842 &signature_length ) );
3843
3844 ASSERT_COMPARE( output_data->x, output_data->len,
3845 signature, signature_length );
3846
3847exit:
3848 psa_reset_key_attributes( &attributes );
3849
3850 psa_destroy_key( key );
3851 mbedtls_free( signature );
3852 PSA_DONE( );
3853
3854}
3855/* END_CASE */
3856
3857/* BEGIN_CASE */
3858void sign_message_fail( int key_type_arg,
3859 data_t *key_data,
3860 int alg_arg,
3861 data_t *input_data,
3862 int signature_size_arg,
3863 int expected_status_arg )
3864{
3865 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3866 psa_key_type_t key_type = key_type_arg;
3867 psa_algorithm_t alg = alg_arg;
3868 size_t signature_size = signature_size_arg;
3869 psa_status_t actual_status;
3870 psa_status_t expected_status = expected_status_arg;
3871 unsigned char *signature = NULL;
3872 size_t signature_length = 0xdeadbeef;
3873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3874
3875 ASSERT_ALLOC( signature, signature_size );
3876
3877 PSA_ASSERT( psa_crypto_init( ) );
3878
3879 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3880 psa_set_key_algorithm( &attributes, alg );
3881 psa_set_key_type( &attributes, key_type );
3882
3883 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3884 &key ) );
3885
3886 actual_status = psa_sign_message( key, alg,
3887 input_data->x, input_data->len,
3888 signature, signature_size,
3889 &signature_length );
3890 TEST_EQUAL( actual_status, expected_status );
3891 /* The value of *signature_length is unspecified on error, but
3892 * whatever it is, it should be less than signature_size, so that
3893 * if the caller tries to read *signature_length bytes without
3894 * checking the error code then they don't overflow a buffer. */
3895 TEST_ASSERT( signature_length <= signature_size );
3896
3897exit:
3898 psa_reset_key_attributes( &attributes );
3899 psa_destroy_key( key );
3900 mbedtls_free( signature );
3901 PSA_DONE( );
3902}
3903/* END_CASE */
3904
3905/* BEGIN_CASE */
3906void sign_verify_message( int key_type_arg,
3907 data_t *key_data,
3908 int alg_arg,
3909 data_t *input_data )
3910{
3911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3912 psa_key_type_t key_type = key_type_arg;
3913 psa_algorithm_t alg = alg_arg;
3914 size_t key_bits;
3915 unsigned char *signature = NULL;
3916 size_t signature_size;
3917 size_t signature_length = 0xdeadbeef;
3918 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3919
3920 PSA_ASSERT( psa_crypto_init( ) );
3921
3922 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3923 PSA_KEY_USAGE_VERIFY_MESSAGE );
3924 psa_set_key_algorithm( &attributes, alg );
3925 psa_set_key_type( &attributes, key_type );
3926
3927 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3928 &key ) );
3929 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3930 key_bits = psa_get_key_bits( &attributes );
3931
3932 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3933 TEST_ASSERT( signature_size != 0 );
3934 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3935 ASSERT_ALLOC( signature, signature_size );
3936
3937 PSA_ASSERT( psa_sign_message( key, alg,
3938 input_data->x, input_data->len,
3939 signature, signature_size,
3940 &signature_length ) );
3941 TEST_ASSERT( signature_length <= signature_size );
3942 TEST_ASSERT( signature_length > 0 );
3943
3944 PSA_ASSERT( psa_verify_message( key, alg,
3945 input_data->x, input_data->len,
3946 signature, signature_length ) );
3947
3948 if( input_data->len != 0 )
3949 {
3950 /* Flip a bit in the input and verify that the signature is now
3951 * detected as invalid. Flip a bit at the beginning, not at the end,
3952 * because ECDSA may ignore the last few bits of the input. */
3953 input_data->x[0] ^= 1;
3954 TEST_EQUAL( psa_verify_message( key, alg,
3955 input_data->x, input_data->len,
3956 signature, signature_length ),
3957 PSA_ERROR_INVALID_SIGNATURE );
3958 }
3959
3960exit:
3961 psa_reset_key_attributes( &attributes );
3962
3963 psa_destroy_key( key );
3964 mbedtls_free( signature );
3965 PSA_DONE( );
3966}
3967/* END_CASE */
3968
3969/* BEGIN_CASE */
3970void verify_message( int key_type_arg,
3971 data_t *key_data,
3972 int alg_arg,
3973 data_t *input_data,
3974 data_t *signature_data )
3975{
3976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3977 psa_key_type_t key_type = key_type_arg;
3978 psa_algorithm_t alg = alg_arg;
3979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3980
3981 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3982
3983 PSA_ASSERT( psa_crypto_init( ) );
3984
3985 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3986 psa_set_key_algorithm( &attributes, alg );
3987 psa_set_key_type( &attributes, key_type );
3988
3989 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3990 &key ) );
3991
3992 PSA_ASSERT( psa_verify_message( key, alg,
3993 input_data->x, input_data->len,
3994 signature_data->x, signature_data->len ) );
3995
3996exit:
3997 psa_reset_key_attributes( &attributes );
3998 psa_destroy_key( key );
3999 PSA_DONE( );
4000}
4001/* END_CASE */
4002
4003/* BEGIN_CASE */
4004void verify_message_fail( int key_type_arg,
4005 data_t *key_data,
4006 int alg_arg,
4007 data_t *hash_data,
4008 data_t *signature_data,
4009 int expected_status_arg )
4010{
4011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4012 psa_key_type_t key_type = key_type_arg;
4013 psa_algorithm_t alg = alg_arg;
4014 psa_status_t actual_status;
4015 psa_status_t expected_status = expected_status_arg;
4016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4017
4018 PSA_ASSERT( psa_crypto_init( ) );
4019
4020 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4021 psa_set_key_algorithm( &attributes, alg );
4022 psa_set_key_type( &attributes, key_type );
4023
4024 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4025 &key ) );
4026
4027 actual_status = psa_verify_message( key, alg,
4028 hash_data->x, hash_data->len,
4029 signature_data->x,
4030 signature_data->len );
4031 TEST_EQUAL( actual_status, expected_status );
4032
4033exit:
4034 psa_reset_key_attributes( &attributes );
4035 psa_destroy_key( key );
4036 PSA_DONE( );
4037}
4038/* END_CASE */
4039
4040/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004041void asymmetric_encrypt( int key_type_arg,
4042 data_t *key_data,
4043 int alg_arg,
4044 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004045 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004046 int expected_output_length_arg,
4047 int expected_status_arg )
4048{
Ronald Cron5425a212020-08-04 14:58:35 +02004049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004050 psa_key_type_t key_type = key_type_arg;
4051 psa_algorithm_t alg = alg_arg;
4052 size_t expected_output_length = expected_output_length_arg;
4053 size_t key_bits;
4054 unsigned char *output = NULL;
4055 size_t output_size;
4056 size_t output_length = ~0;
4057 psa_status_t actual_status;
4058 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004060
Gilles Peskine8817f612018-12-18 00:18:46 +01004061 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004062
Gilles Peskine656896e2018-06-29 19:12:28 +02004063 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004064 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4065 psa_set_key_algorithm( &attributes, alg );
4066 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004067 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004068 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004069
4070 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004071 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004072 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004073
Gilles Peskine656896e2018-06-29 19:12:28 +02004074 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004075 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004076 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004077
4078 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004079 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004080 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004081 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004082 output, output_size,
4083 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004084 TEST_EQUAL( actual_status, expected_status );
4085 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004086
Gilles Peskine68428122018-06-30 18:42:41 +02004087 /* If the label is empty, the test framework puts a non-null pointer
4088 * in label->x. Test that a null pointer works as well. */
4089 if( label->len == 0 )
4090 {
4091 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004092 if( output_size != 0 )
4093 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004094 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004095 input_data->x, input_data->len,
4096 NULL, label->len,
4097 output, output_size,
4098 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004099 TEST_EQUAL( actual_status, expected_status );
4100 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004101 }
4102
Gilles Peskine656896e2018-06-29 19:12:28 +02004103exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004104 /*
4105 * Key attributes may have been returned by psa_get_key_attributes()
4106 * thus reset them as required.
4107 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004108 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004109
Ronald Cron5425a212020-08-04 14:58:35 +02004110 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004111 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004112 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004113}
4114/* END_CASE */
4115
4116/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004117void asymmetric_encrypt_decrypt( int key_type_arg,
4118 data_t *key_data,
4119 int alg_arg,
4120 data_t *input_data,
4121 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004122{
Ronald Cron5425a212020-08-04 14:58:35 +02004123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004124 psa_key_type_t key_type = key_type_arg;
4125 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004126 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004127 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004128 size_t output_size;
4129 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004130 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004131 size_t output2_size;
4132 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004134
Gilles Peskine8817f612018-12-18 00:18:46 +01004135 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4138 psa_set_key_algorithm( &attributes, alg );
4139 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004140
Gilles Peskine049c7532019-05-15 20:22:09 +02004141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004142 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004143
4144 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004145 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004146 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004147
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004148 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004149 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004150 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004151
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004152 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004153 TEST_ASSERT( output2_size <=
4154 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4155 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004156 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004157
Gilles Peskineeebd7382018-06-08 18:11:54 +02004158 /* We test encryption by checking that encrypt-then-decrypt gives back
4159 * the original plaintext because of the non-optional random
4160 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004161 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004162 input_data->x, input_data->len,
4163 label->x, label->len,
4164 output, output_size,
4165 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004166 /* We don't know what ciphertext length to expect, but check that
4167 * it looks sensible. */
4168 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004169
Ronald Cron5425a212020-08-04 14:58:35 +02004170 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004171 output, output_length,
4172 label->x, label->len,
4173 output2, output2_size,
4174 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004175 ASSERT_COMPARE( input_data->x, input_data->len,
4176 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004177
4178exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004179 /*
4180 * Key attributes may have been returned by psa_get_key_attributes()
4181 * thus reset them as required.
4182 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004183 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004184
Ronald Cron5425a212020-08-04 14:58:35 +02004185 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004186 mbedtls_free( output );
4187 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004188 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004189}
4190/* END_CASE */
4191
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004192/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004193void asymmetric_decrypt( int key_type_arg,
4194 data_t *key_data,
4195 int alg_arg,
4196 data_t *input_data,
4197 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004198 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199{
Ronald Cron5425a212020-08-04 14:58:35 +02004200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004201 psa_key_type_t key_type = key_type_arg;
4202 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004203 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004204 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004205 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004206 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004208
Gilles Peskine8817f612018-12-18 00:18:46 +01004209 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004210
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004211 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4212 psa_set_key_algorithm( &attributes, alg );
4213 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004214
Gilles Peskine049c7532019-05-15 20:22:09 +02004215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004216 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004217
gabor-mezei-armceface22021-01-21 12:26:17 +01004218 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4219 key_bits = psa_get_key_bits( &attributes );
4220
4221 /* Determine the maximum ciphertext length */
4222 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4223 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4224 ASSERT_ALLOC( output, output_size );
4225
Ronald Cron5425a212020-08-04 14:58:35 +02004226 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004227 input_data->x, input_data->len,
4228 label->x, label->len,
4229 output,
4230 output_size,
4231 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004232 ASSERT_COMPARE( expected_data->x, expected_data->len,
4233 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004234
Gilles Peskine68428122018-06-30 18:42:41 +02004235 /* If the label is empty, the test framework puts a non-null pointer
4236 * in label->x. Test that a null pointer works as well. */
4237 if( label->len == 0 )
4238 {
4239 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004240 if( output_size != 0 )
4241 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004242 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004243 input_data->x, input_data->len,
4244 NULL, label->len,
4245 output,
4246 output_size,
4247 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004248 ASSERT_COMPARE( expected_data->x, expected_data->len,
4249 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004250 }
4251
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004252exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004253 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004254 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004255 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004256 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257}
4258/* END_CASE */
4259
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004260/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004261void asymmetric_decrypt_fail( int key_type_arg,
4262 data_t *key_data,
4263 int alg_arg,
4264 data_t *input_data,
4265 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004266 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004267 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004268{
Ronald Cron5425a212020-08-04 14:58:35 +02004269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004270 psa_key_type_t key_type = key_type_arg;
4271 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004272 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004273 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004274 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004275 psa_status_t actual_status;
4276 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004278
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004279 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004280
Gilles Peskine8817f612018-12-18 00:18:46 +01004281 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004282
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4284 psa_set_key_algorithm( &attributes, alg );
4285 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004286
Gilles Peskine049c7532019-05-15 20:22:09 +02004287 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004288 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004289
Ronald Cron5425a212020-08-04 14:58:35 +02004290 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004291 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004292 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004293 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004294 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004295 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004296 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004297
Gilles Peskine68428122018-06-30 18:42:41 +02004298 /* If the label is empty, the test framework puts a non-null pointer
4299 * in label->x. Test that a null pointer works as well. */
4300 if( label->len == 0 )
4301 {
4302 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004303 if( output_size != 0 )
4304 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004305 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004306 input_data->x, input_data->len,
4307 NULL, label->len,
4308 output, output_size,
4309 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004310 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004311 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004312 }
4313
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004314exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004315 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004316 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004317 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004318 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004319}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004320/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004321
4322/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004323void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004324{
4325 /* Test each valid way of initializing the object, except for `= {0}`, as
4326 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4327 * though it's OK by the C standard. We could test for this, but we'd need
4328 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004329 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004330 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4331 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4332 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004333
4334 memset( &zero, 0, sizeof( zero ) );
4335
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004336 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004337 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004338 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004339 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004340 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004341 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004342 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004343
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004344 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004345 PSA_ASSERT( psa_key_derivation_abort(&func) );
4346 PSA_ASSERT( psa_key_derivation_abort(&init) );
4347 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004348}
4349/* END_CASE */
4350
Janos Follath16de4a42019-06-13 16:32:24 +01004351/* BEGIN_CASE */
4352void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004353{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004354 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004356 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004357
Gilles Peskine8817f612018-12-18 00:18:46 +01004358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004359
Janos Follath16de4a42019-06-13 16:32:24 +01004360 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004361 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004362
4363exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004364 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004365 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004366}
4367/* END_CASE */
4368
Janos Follathaf3c2a02019-06-12 12:34:34 +01004369/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004370void derive_set_capacity( int alg_arg, int capacity_arg,
4371 int expected_status_arg )
4372{
4373 psa_algorithm_t alg = alg_arg;
4374 size_t capacity = capacity_arg;
4375 psa_status_t expected_status = expected_status_arg;
4376 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4377
4378 PSA_ASSERT( psa_crypto_init( ) );
4379
4380 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4381
4382 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4383 expected_status );
4384
4385exit:
4386 psa_key_derivation_abort( &operation );
4387 PSA_DONE( );
4388}
4389/* END_CASE */
4390
4391/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004392void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004393 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004394 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004395 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004396 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004397 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004398 int expected_status_arg3,
4399 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004400{
4401 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004402 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4403 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004404 psa_status_t expected_statuses[] = {expected_status_arg1,
4405 expected_status_arg2,
4406 expected_status_arg3};
4407 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004408 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4409 MBEDTLS_SVC_KEY_ID_INIT,
4410 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004411 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4413 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004414 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004415 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004416 psa_status_t expected_output_status = expected_output_status_arg;
4417 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004418
4419 PSA_ASSERT( psa_crypto_init( ) );
4420
4421 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4422 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004423
4424 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4425
4426 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4427 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004428 mbedtls_test_set_step( i );
4429 if( steps[i] == 0 )
4430 {
4431 /* Skip this step */
4432 }
4433 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004434 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004435 psa_set_key_type( &attributes, key_types[i] );
4436 PSA_ASSERT( psa_import_key( &attributes,
4437 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004438 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004439 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4440 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4441 {
4442 // When taking a private key as secret input, use key agreement
4443 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004444 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4445 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004446 expected_statuses[i] );
4447 }
4448 else
4449 {
4450 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004451 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004452 expected_statuses[i] );
4453 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004454 }
4455 else
4456 {
4457 TEST_EQUAL( psa_key_derivation_input_bytes(
4458 &operation, steps[i],
4459 inputs[i]->x, inputs[i]->len ),
4460 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004461 }
4462 }
4463
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004464 if( output_key_type != PSA_KEY_TYPE_NONE )
4465 {
4466 psa_reset_key_attributes( &attributes );
4467 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4468 psa_set_key_bits( &attributes, 8 );
4469 actual_output_status =
4470 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004471 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004472 }
4473 else
4474 {
4475 uint8_t buffer[1];
4476 actual_output_status =
4477 psa_key_derivation_output_bytes( &operation,
4478 buffer, sizeof( buffer ) );
4479 }
4480 TEST_EQUAL( actual_output_status, expected_output_status );
4481
Janos Follathaf3c2a02019-06-12 12:34:34 +01004482exit:
4483 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004484 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4485 psa_destroy_key( keys[i] );
4486 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004487 PSA_DONE( );
4488}
4489/* END_CASE */
4490
Janos Follathd958bb72019-07-03 15:02:16 +01004491/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004492void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004493{
Janos Follathd958bb72019-07-03 15:02:16 +01004494 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004495 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004496 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004498 unsigned char input1[] = "Input 1";
4499 size_t input1_length = sizeof( input1 );
4500 unsigned char input2[] = "Input 2";
4501 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004502 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004503 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004504 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4505 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4506 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004508
Gilles Peskine8817f612018-12-18 00:18:46 +01004509 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004510
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004511 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4512 psa_set_key_algorithm( &attributes, alg );
4513 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004514
Gilles Peskine73676cb2019-05-15 20:15:10 +02004515 PSA_ASSERT( psa_import_key( &attributes,
4516 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004517 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004518
4519 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004520 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4521 input1, input1_length,
4522 input2, input2_length,
4523 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004524 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004525
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004526 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004527 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004528 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004529
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004530 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004533 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004534
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004535exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004536 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004537 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004538 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004539}
4540/* END_CASE */
4541
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004542/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004543void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004544{
4545 uint8_t output_buffer[16];
4546 size_t buffer_size = 16;
4547 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004549
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004550 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4551 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004552 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004553
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004554 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004555 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004556
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004558
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004559 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4560 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004561 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004562
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004564 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004565
4566exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004567 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004568}
4569/* END_CASE */
4570
4571/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004572void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004573 int step1_arg, data_t *input1,
4574 int step2_arg, data_t *input2,
4575 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004576 int requested_capacity_arg,
4577 data_t *expected_output1,
4578 data_t *expected_output2 )
4579{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004580 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004581 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4582 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004583 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4584 MBEDTLS_SVC_KEY_ID_INIT,
4585 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004586 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004588 uint8_t *expected_outputs[2] =
4589 {expected_output1->x, expected_output2->x};
4590 size_t output_sizes[2] =
4591 {expected_output1->len, expected_output2->len};
4592 size_t output_buffer_size = 0;
4593 uint8_t *output_buffer = NULL;
4594 size_t expected_capacity;
4595 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004597 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004598 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004599
4600 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4601 {
4602 if( output_sizes[i] > output_buffer_size )
4603 output_buffer_size = output_sizes[i];
4604 if( output_sizes[i] == 0 )
4605 expected_outputs[i] = NULL;
4606 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004607 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004608 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004609
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004610 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4611 psa_set_key_algorithm( &attributes, alg );
4612 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004613
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004614 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004615 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4616 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4617 requested_capacity ) );
4618 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004619 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004620 switch( steps[i] )
4621 {
4622 case 0:
4623 break;
4624 case PSA_KEY_DERIVATION_INPUT_SECRET:
4625 PSA_ASSERT( psa_import_key( &attributes,
4626 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004627 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004628
4629 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4630 {
4631 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4632 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4633 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4634 }
4635
Gilles Peskine1468da72019-05-29 17:35:49 +02004636 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004637 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004638 break;
4639 default:
4640 PSA_ASSERT( psa_key_derivation_input_bytes(
4641 &operation, steps[i],
4642 inputs[i]->x, inputs[i]->len ) );
4643 break;
4644 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004645 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004646
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004648 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004649 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004650 expected_capacity = requested_capacity;
4651
4652 /* Expansion phase. */
4653 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4654 {
4655 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004657 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004658 if( expected_capacity == 0 && output_sizes[i] == 0 )
4659 {
4660 /* Reading 0 bytes when 0 bytes are available can go either way. */
4661 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004662 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004663 continue;
4664 }
4665 else if( expected_capacity == 0 ||
4666 output_sizes[i] > expected_capacity )
4667 {
4668 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004669 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004670 expected_capacity = 0;
4671 continue;
4672 }
4673 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004674 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004675 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004676 ASSERT_COMPARE( output_buffer, output_sizes[i],
4677 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004678 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004679 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004681 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004682 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004683 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004685
4686exit:
4687 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004688 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004689 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4690 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004691 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004692}
4693/* END_CASE */
4694
4695/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004696void derive_full( int alg_arg,
4697 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004698 data_t *input1,
4699 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004700 int requested_capacity_arg )
4701{
Ronald Cron5425a212020-08-04 14:58:35 +02004702 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004703 psa_algorithm_t alg = alg_arg;
4704 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004705 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004706 unsigned char output_buffer[16];
4707 size_t expected_capacity = requested_capacity;
4708 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004710
Gilles Peskine8817f612018-12-18 00:18:46 +01004711 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004712
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4714 psa_set_key_algorithm( &attributes, alg );
4715 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004716
Gilles Peskine049c7532019-05-15 20:22:09 +02004717 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004718 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004719
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004720 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4721 input1->x, input1->len,
4722 input2->x, input2->len,
4723 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004724 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004725
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004727 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004728 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004729
4730 /* Expansion phase. */
4731 while( current_capacity > 0 )
4732 {
4733 size_t read_size = sizeof( output_buffer );
4734 if( read_size > current_capacity )
4735 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004737 output_buffer,
4738 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004739 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004741 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004742 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004743 }
4744
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 /* Check that the operation refuses to go over capacity. */
4746 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004747 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004748
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004750
4751exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004753 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004754 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004755}
4756/* END_CASE */
4757
Janos Follathe60c9052019-07-03 13:51:30 +01004758/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004759void derive_key_exercise( int alg_arg,
4760 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004761 data_t *input1,
4762 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004763 int derived_type_arg,
4764 int derived_bits_arg,
4765 int derived_usage_arg,
4766 int derived_alg_arg )
4767{
Ronald Cron5425a212020-08-04 14:58:35 +02004768 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4769 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004770 psa_algorithm_t alg = alg_arg;
4771 psa_key_type_t derived_type = derived_type_arg;
4772 size_t derived_bits = derived_bits_arg;
4773 psa_key_usage_t derived_usage = derived_usage_arg;
4774 psa_algorithm_t derived_alg = derived_alg_arg;
4775 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004776 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004778 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004779
Gilles Peskine8817f612018-12-18 00:18:46 +01004780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004781
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4783 psa_set_key_algorithm( &attributes, alg );
4784 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004786 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004787
4788 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004789 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4790 input1->x, input1->len,
4791 input2->x, input2->len,
4792 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004793 goto exit;
4794
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004795 psa_set_key_usage_flags( &attributes, derived_usage );
4796 psa_set_key_algorithm( &attributes, derived_alg );
4797 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004798 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004800 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004801
4802 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004803 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004804 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4805 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004806
4807 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004808 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004809 goto exit;
4810
4811exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004812 /*
4813 * Key attributes may have been returned by psa_get_key_attributes()
4814 * thus reset them as required.
4815 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004816 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004817
4818 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004819 psa_destroy_key( base_key );
4820 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004821 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004822}
4823/* END_CASE */
4824
Janos Follath42fd8882019-07-03 14:17:09 +01004825/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004826void derive_key_export( int alg_arg,
4827 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004828 data_t *input1,
4829 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004830 int bytes1_arg,
4831 int bytes2_arg )
4832{
Ronald Cron5425a212020-08-04 14:58:35 +02004833 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4834 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004835 psa_algorithm_t alg = alg_arg;
4836 size_t bytes1 = bytes1_arg;
4837 size_t bytes2 = bytes2_arg;
4838 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004840 uint8_t *output_buffer = NULL;
4841 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004842 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4843 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004844 size_t length;
4845
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004846 ASSERT_ALLOC( output_buffer, capacity );
4847 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004848 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004849
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004850 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4851 psa_set_key_algorithm( &base_attributes, alg );
4852 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004853 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004854 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004855
4856 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004857 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4858 input1->x, input1->len,
4859 input2->x, input2->len,
4860 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004861 goto exit;
4862
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004863 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004864 output_buffer,
4865 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004867
4868 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004869 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4870 input1->x, input1->len,
4871 input2->x, input2->len,
4872 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004873 goto exit;
4874
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004875 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4876 psa_set_key_algorithm( &derived_attributes, 0 );
4877 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004878 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004880 &derived_key ) );
4881 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004882 export_buffer, bytes1,
4883 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004884 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004885 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004886 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004887 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004888 &derived_key ) );
4889 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004890 export_buffer + bytes1, bytes2,
4891 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004892 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004893
4894 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004895 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4896 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004897
4898exit:
4899 mbedtls_free( output_buffer );
4900 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004902 psa_destroy_key( base_key );
4903 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004904 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004905}
4906/* END_CASE */
4907
4908/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004909void derive_key( int alg_arg,
4910 data_t *key_data, data_t *input1, data_t *input2,
4911 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004912 int expected_status_arg,
4913 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004914{
Ronald Cron5425a212020-08-04 14:58:35 +02004915 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4916 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004917 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004918 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004919 size_t bits = bits_arg;
4920 psa_status_t expected_status = expected_status_arg;
4921 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4922 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4923 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4924
4925 PSA_ASSERT( psa_crypto_init( ) );
4926
4927 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4928 psa_set_key_algorithm( &base_attributes, alg );
4929 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4930 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004931 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004932
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004933 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4934 input1->x, input1->len,
4935 input2->x, input2->len,
4936 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004937 goto exit;
4938
4939 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4940 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004941 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004942 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004943
4944 psa_status_t status =
4945 psa_key_derivation_output_key( &derived_attributes,
4946 &operation,
4947 &derived_key );
4948 if( is_large_output > 0 )
4949 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4950 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004951
4952exit:
4953 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004954 psa_destroy_key( base_key );
4955 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004956 PSA_DONE( );
4957}
4958/* END_CASE */
4959
4960/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004961void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004962 int our_key_type_arg, int our_key_alg_arg,
4963 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004964 int expected_status_arg )
4965{
Ronald Cron5425a212020-08-04 14:58:35 +02004966 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004967 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004968 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004969 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004971 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004972 psa_status_t expected_status = expected_status_arg;
4973 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004974
Gilles Peskine8817f612018-12-18 00:18:46 +01004975 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004976
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004977 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004978 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004979 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004980 PSA_ASSERT( psa_import_key( &attributes,
4981 our_key_data->x, our_key_data->len,
4982 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004983
Gilles Peskine77f40d82019-04-11 21:27:06 +02004984 /* The tests currently include inputs that should fail at either step.
4985 * Test cases that fail at the setup step should be changed to call
4986 * key_derivation_setup instead, and this function should be renamed
4987 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004989 if( status == PSA_SUCCESS )
4990 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004991 TEST_EQUAL( psa_key_derivation_key_agreement(
4992 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4993 our_key,
4994 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004995 expected_status );
4996 }
4997 else
4998 {
4999 TEST_ASSERT( status == expected_status );
5000 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005001
5002exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005004 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005005 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005006}
5007/* END_CASE */
5008
5009/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005010void raw_key_agreement( int alg_arg,
5011 int our_key_type_arg, data_t *our_key_data,
5012 data_t *peer_key_data,
5013 data_t *expected_output )
5014{
Ronald Cron5425a212020-08-04 14:58:35 +02005015 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005016 psa_algorithm_t alg = alg_arg;
5017 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005019 unsigned char *output = NULL;
5020 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005021 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005022
5023 ASSERT_ALLOC( output, expected_output->len );
5024 PSA_ASSERT( psa_crypto_init( ) );
5025
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005026 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5027 psa_set_key_algorithm( &attributes, alg );
5028 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005029 PSA_ASSERT( psa_import_key( &attributes,
5030 our_key_data->x, our_key_data->len,
5031 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005032
gabor-mezei-armceface22021-01-21 12:26:17 +01005033 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5034 key_bits = psa_get_key_bits( &attributes );
5035
Gilles Peskinebe697d82019-05-16 18:00:41 +02005036 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5037 peer_key_data->x, peer_key_data->len,
5038 output, expected_output->len,
5039 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005040 ASSERT_COMPARE( output, output_length,
5041 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005042 TEST_ASSERT( output_length <=
5043 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5044 TEST_ASSERT( output_length <=
5045 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005046
5047exit:
5048 mbedtls_free( output );
5049 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005050 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005051}
5052/* END_CASE */
5053
5054/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005055void key_agreement_capacity( int alg_arg,
5056 int our_key_type_arg, data_t *our_key_data,
5057 data_t *peer_key_data,
5058 int expected_capacity_arg )
5059{
Ronald Cron5425a212020-08-04 14:58:35 +02005060 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005061 psa_algorithm_t alg = alg_arg;
5062 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005065 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005066 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005067
Gilles Peskine8817f612018-12-18 00:18:46 +01005068 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005069
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005070 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5071 psa_set_key_algorithm( &attributes, alg );
5072 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005073 PSA_ASSERT( psa_import_key( &attributes,
5074 our_key_data->x, our_key_data->len,
5075 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005076
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005077 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005078 PSA_ASSERT( psa_key_derivation_key_agreement(
5079 &operation,
5080 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5081 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005082 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5083 {
5084 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005085 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005086 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005087 NULL, 0 ) );
5088 }
Gilles Peskine59685592018-09-18 12:11:34 +02005089
Gilles Peskinebf491972018-10-25 22:36:12 +02005090 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005091 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005092 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005093 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005094
Gilles Peskinebf491972018-10-25 22:36:12 +02005095 /* Test the actual capacity by reading the output. */
5096 while( actual_capacity > sizeof( output ) )
5097 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005098 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005099 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005100 actual_capacity -= sizeof( output );
5101 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005102 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005103 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005104 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005105 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005106
Gilles Peskine59685592018-09-18 12:11:34 +02005107exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005109 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005110 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005111}
5112/* END_CASE */
5113
5114/* BEGIN_CASE */
5115void key_agreement_output( int alg_arg,
5116 int our_key_type_arg, data_t *our_key_data,
5117 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005118 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005119{
Ronald Cron5425a212020-08-04 14:58:35 +02005120 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005121 psa_algorithm_t alg = alg_arg;
5122 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005125 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005126
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005127 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5128 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005129
Gilles Peskine8817f612018-12-18 00:18:46 +01005130 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005131
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005132 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5133 psa_set_key_algorithm( &attributes, alg );
5134 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005135 PSA_ASSERT( psa_import_key( &attributes,
5136 our_key_data->x, our_key_data->len,
5137 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005138
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005140 PSA_ASSERT( psa_key_derivation_key_agreement(
5141 &operation,
5142 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5143 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005144 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5145 {
5146 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005148 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005149 NULL, 0 ) );
5150 }
Gilles Peskine59685592018-09-18 12:11:34 +02005151
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005153 actual_output,
5154 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005155 ASSERT_COMPARE( actual_output, expected_output1->len,
5156 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005157 if( expected_output2->len != 0 )
5158 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005160 actual_output,
5161 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005162 ASSERT_COMPARE( actual_output, expected_output2->len,
5163 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005164 }
Gilles Peskine59685592018-09-18 12:11:34 +02005165
5166exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005167 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005168 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005169 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005170 mbedtls_free( actual_output );
5171}
5172/* END_CASE */
5173
5174/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005175void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005176{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005177 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005178 unsigned char *output = NULL;
5179 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005180 size_t i;
5181 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005182
Simon Butcher49f8e312020-03-03 15:51:50 +00005183 TEST_ASSERT( bytes_arg >= 0 );
5184
Gilles Peskine91892022021-02-08 19:50:26 +01005185 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005186 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005187
Gilles Peskine8817f612018-12-18 00:18:46 +01005188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005189
Gilles Peskinea50d7392018-06-21 10:22:13 +02005190 /* Run several times, to ensure that every output byte will be
5191 * nonzero at least once with overwhelming probability
5192 * (2^(-8*number_of_runs)). */
5193 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005194 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005195 if( bytes != 0 )
5196 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005197 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005198
Gilles Peskinea50d7392018-06-21 10:22:13 +02005199 for( i = 0; i < bytes; i++ )
5200 {
5201 if( output[i] != 0 )
5202 ++changed[i];
5203 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005204 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005205
5206 /* Check that every byte was changed to nonzero at least once. This
5207 * validates that psa_generate_random is overwriting every byte of
5208 * the output buffer. */
5209 for( i = 0; i < bytes; i++ )
5210 {
5211 TEST_ASSERT( changed[i] != 0 );
5212 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005213
5214exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005215 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005216 mbedtls_free( output );
5217 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005218}
5219/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005220
5221/* BEGIN_CASE */
5222void generate_key( int type_arg,
5223 int bits_arg,
5224 int usage_arg,
5225 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005226 int expected_status_arg,
5227 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005228{
Ronald Cron5425a212020-08-04 14:58:35 +02005229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005230 psa_key_type_t type = type_arg;
5231 psa_key_usage_t usage = usage_arg;
5232 size_t bits = bits_arg;
5233 psa_algorithm_t alg = alg_arg;
5234 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005236 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005237
Gilles Peskine8817f612018-12-18 00:18:46 +01005238 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005239
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005240 psa_set_key_usage_flags( &attributes, usage );
5241 psa_set_key_algorithm( &attributes, alg );
5242 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005243 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005244
5245 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005246 psa_status_t status = psa_generate_key( &attributes, &key );
5247
5248 if( is_large_key > 0 )
5249 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5250 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005251 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005252 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005253
5254 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005255 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005256 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5257 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005258
Gilles Peskine818ca122018-06-20 18:16:48 +02005259 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005260 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005261 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005262
5263exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005264 /*
5265 * Key attributes may have been returned by psa_get_key_attributes()
5266 * thus reset them as required.
5267 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005268 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005269
Ronald Cron5425a212020-08-04 14:58:35 +02005270 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005271 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005272}
5273/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005274
Ronald Cronee414c72021-03-18 18:50:08 +01005275/* 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 +02005276void generate_key_rsa( int bits_arg,
5277 data_t *e_arg,
5278 int expected_status_arg )
5279{
Ronald Cron5425a212020-08-04 14:58:35 +02005280 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005281 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005282 size_t bits = bits_arg;
5283 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5284 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5285 psa_status_t expected_status = expected_status_arg;
5286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5287 uint8_t *exported = NULL;
5288 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005289 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005290 size_t exported_length = SIZE_MAX;
5291 uint8_t *e_read_buffer = NULL;
5292 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005293 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005294 size_t e_read_length = SIZE_MAX;
5295
5296 if( e_arg->len == 0 ||
5297 ( e_arg->len == 3 &&
5298 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5299 {
5300 is_default_public_exponent = 1;
5301 e_read_size = 0;
5302 }
5303 ASSERT_ALLOC( e_read_buffer, e_read_size );
5304 ASSERT_ALLOC( exported, exported_size );
5305
5306 PSA_ASSERT( psa_crypto_init( ) );
5307
5308 psa_set_key_usage_flags( &attributes, usage );
5309 psa_set_key_algorithm( &attributes, alg );
5310 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5311 e_arg->x, e_arg->len ) );
5312 psa_set_key_bits( &attributes, bits );
5313
5314 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005315 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005316 if( expected_status != PSA_SUCCESS )
5317 goto exit;
5318
5319 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005320 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005321 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5322 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5323 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5324 e_read_buffer, e_read_size,
5325 &e_read_length ) );
5326 if( is_default_public_exponent )
5327 TEST_EQUAL( e_read_length, 0 );
5328 else
5329 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5330
5331 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005332 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005333 goto exit;
5334
5335 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005336 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005337 exported, exported_size,
5338 &exported_length ) );
5339 {
5340 uint8_t *p = exported;
5341 uint8_t *end = exported + exported_length;
5342 size_t len;
5343 /* RSAPublicKey ::= SEQUENCE {
5344 * modulus INTEGER, -- n
5345 * publicExponent INTEGER } -- e
5346 */
5347 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005348 MBEDTLS_ASN1_SEQUENCE |
5349 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005350 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005351 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5352 MBEDTLS_ASN1_INTEGER ) );
5353 if( len >= 1 && p[0] == 0 )
5354 {
5355 ++p;
5356 --len;
5357 }
5358 if( e_arg->len == 0 )
5359 {
5360 TEST_EQUAL( len, 3 );
5361 TEST_EQUAL( p[0], 1 );
5362 TEST_EQUAL( p[1], 0 );
5363 TEST_EQUAL( p[2], 1 );
5364 }
5365 else
5366 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5367 }
5368
5369exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005370 /*
5371 * Key attributes may have been returned by psa_get_key_attributes() or
5372 * set by psa_set_key_domain_parameters() thus reset them as required.
5373 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005374 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005375
Ronald Cron5425a212020-08-04 14:58:35 +02005376 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005377 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005378 mbedtls_free( e_read_buffer );
5379 mbedtls_free( exported );
5380}
5381/* END_CASE */
5382
Darryl Greend49a4992018-06-18 17:27:26 +01005383/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005384void persistent_key_load_key_from_storage( data_t *data,
5385 int type_arg, int bits_arg,
5386 int usage_flags_arg, int alg_arg,
5387 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005388{
Ronald Cron71016a92020-08-28 19:01:50 +02005389 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005391 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5392 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005393 psa_key_type_t type = type_arg;
5394 size_t bits = bits_arg;
5395 psa_key_usage_t usage_flags = usage_flags_arg;
5396 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005398 unsigned char *first_export = NULL;
5399 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005400 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005401 size_t first_exported_length;
5402 size_t second_exported_length;
5403
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005404 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5405 {
5406 ASSERT_ALLOC( first_export, export_size );
5407 ASSERT_ALLOC( second_export, export_size );
5408 }
Darryl Greend49a4992018-06-18 17:27:26 +01005409
Gilles Peskine8817f612018-12-18 00:18:46 +01005410 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005411
Gilles Peskinec87af662019-05-15 16:12:22 +02005412 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005413 psa_set_key_usage_flags( &attributes, usage_flags );
5414 psa_set_key_algorithm( &attributes, alg );
5415 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005416 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005417
Darryl Green0c6575a2018-11-07 16:05:30 +00005418 switch( generation_method )
5419 {
5420 case IMPORT_KEY:
5421 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005422 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005423 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005424 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005425
Darryl Green0c6575a2018-11-07 16:05:30 +00005426 case GENERATE_KEY:
5427 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005428 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005429 break;
5430
5431 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005432#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005433 {
5434 /* Create base key */
5435 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5436 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5437 psa_set_key_usage_flags( &base_attributes,
5438 PSA_KEY_USAGE_DERIVE );
5439 psa_set_key_algorithm( &base_attributes, derive_alg );
5440 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005441 PSA_ASSERT( psa_import_key( &base_attributes,
5442 data->x, data->len,
5443 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005444 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005446 PSA_ASSERT( psa_key_derivation_input_key(
5447 &operation,
5448 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005449 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005450 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005451 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005452 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5453 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005454 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005455 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005456 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005457 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005458 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005459#else
5460 TEST_ASSUME( ! "KDF not supported in this configuration" );
5461#endif
5462 break;
5463
5464 default:
5465 TEST_ASSERT( ! "generation_method not implemented in test" );
5466 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005467 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005468 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005469
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005470 /* Export the key if permitted by the key policy. */
5471 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5472 {
Ronald Cron5425a212020-08-04 14:58:35 +02005473 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005474 first_export, export_size,
5475 &first_exported_length ) );
5476 if( generation_method == IMPORT_KEY )
5477 ASSERT_COMPARE( data->x, data->len,
5478 first_export, first_exported_length );
5479 }
Darryl Greend49a4992018-06-18 17:27:26 +01005480
5481 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005482 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005483 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005484 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005485
Darryl Greend49a4992018-06-18 17:27:26 +01005486 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005487 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005488 TEST_ASSERT( mbedtls_svc_key_id_equal(
5489 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005490 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5491 PSA_KEY_LIFETIME_PERSISTENT );
5492 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5493 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005494 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005495 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005496 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005497
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005498 /* Export the key again if permitted by the key policy. */
5499 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005500 {
Ronald Cron5425a212020-08-04 14:58:35 +02005501 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005502 second_export, export_size,
5503 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005504 ASSERT_COMPARE( first_export, first_exported_length,
5505 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005506 }
5507
5508 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005509 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005510 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005511
5512exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005513 /*
5514 * Key attributes may have been returned by psa_get_key_attributes()
5515 * thus reset them as required.
5516 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005517 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005518
Darryl Greend49a4992018-06-18 17:27:26 +01005519 mbedtls_free( first_export );
5520 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005522 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005523 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005524 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005525}
5526/* END_CASE */