blob: a07fc3066c9792e6bc561ff95879bb8a7f040859 [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}
Andrzej Kureke0015962022-01-17 15:29:38 +010051#if defined(MBEDTLS_ASN1_WRITE_C)
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}
Andrzej Kureke0015962022-01-17 15:29:38 +0100135#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200136
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137int exercise_mac_setup( psa_key_type_t key_type,
138 const unsigned char *key_bytes,
139 size_t key_length,
140 psa_algorithm_t alg,
141 psa_mac_operation_t *operation,
142 psa_status_t *status )
143{
Ronald Cron5425a212020-08-04 14:58:35 +0200144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100146
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100147 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200148 psa_set_key_algorithm( &attributes, alg );
149 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200150 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151
Ronald Cron5425a212020-08-04 14:58:35 +0200152 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100153 /* Whether setup succeeded or failed, abort must succeed. */
154 PSA_ASSERT( psa_mac_abort( operation ) );
155 /* If setup failed, reproduce the failure, so that the caller can
156 * test the resulting state of the operation object. */
157 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 {
Ronald Cron5425a212020-08-04 14:58:35 +0200159 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100160 }
161
Ronald Cron5425a212020-08-04 14:58:35 +0200162 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100163 return( 1 );
164
165exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200166 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100167 return( 0 );
168}
169
170int exercise_cipher_setup( psa_key_type_t key_type,
171 const unsigned char *key_bytes,
172 size_t key_length,
173 psa_algorithm_t alg,
174 psa_cipher_operation_t *operation,
175 psa_status_t *status )
176{
Ronald Cron5425a212020-08-04 14:58:35 +0200177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100179
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
181 psa_set_key_algorithm( &attributes, alg );
182 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200183 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100184
Ronald Cron5425a212020-08-04 14:58:35 +0200185 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100186 /* Whether setup succeeded or failed, abort must succeed. */
187 PSA_ASSERT( psa_cipher_abort( operation ) );
188 /* If setup failed, reproduce the failure, so that the caller can
189 * test the resulting state of the operation object. */
190 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191 {
Ronald Cron5425a212020-08-04 14:58:35 +0200192 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100193 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100194 }
195
Ronald Cron5425a212020-08-04 14:58:35 +0200196 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 return( 1 );
198
199exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200200 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201 return( 0 );
202}
203
Ronald Cron5425a212020-08-04 14:58:35 +0200204static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200205{
206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200207 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200208 uint8_t buffer[1];
209 size_t length;
210 int ok = 0;
211
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200213 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
214 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
215 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200216 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000217 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 TEST_EQUAL(
219 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
220 TEST_EQUAL(
221 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200222 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200223 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
226 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
227
Ronald Cron5425a212020-08-04 14:58:35 +0200228 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000229 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200230 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200231 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000232 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 ok = 1;
235
236exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100237 /*
238 * Key attributes may have been returned by psa_get_key_attributes()
239 * thus reset them as required.
240 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100242
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200243 return( ok );
244}
245
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200246/* Assert that a key isn't reported as having a slot number. */
247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
248#define ASSERT_NO_SLOT_NUMBER( attributes ) \
249 do \
250 { \
251 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
252 TEST_EQUAL( psa_get_key_slot_number( \
253 attributes, \
254 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
255 PSA_ERROR_INVALID_ARGUMENT ); \
256 } \
257 while( 0 )
258#else /* MBEDTLS_PSA_CRYPTO_SE_C */
259#define ASSERT_NO_SLOT_NUMBER( attributes ) \
260 ( (void) 0 )
261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
262
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100263/* An overapproximation of the amount of storage needed for a key of the
264 * given type and with the given content. The API doesn't make it easy
265 * to find a good value for the size. The current implementation doesn't
266 * care about the value anyway. */
267#define KEY_BITS_FROM_DATA( type, data ) \
268 ( data )->len
269
Darryl Green0c6575a2018-11-07 16:05:30 +0000270typedef enum {
271 IMPORT_KEY = 0,
272 GENERATE_KEY = 1,
273 DERIVE_KEY = 2
274} generate_method;
275
Gilles Peskinee59236f2018-01-27 23:32:46 +0100276/* END_HEADER */
277
278/* BEGIN_DEPENDENCIES
279 * depends_on:MBEDTLS_PSA_CRYPTO_C
280 * END_DEPENDENCIES
281 */
282
283/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200284void static_checks( )
285{
286 size_t max_truncated_mac_size =
287 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
288
289 /* Check that the length for a truncated MAC always fits in the algorithm
290 * encoding. The shifted mask is the maximum truncated value. The
291 * untruncated algorithm may be one byte larger. */
292 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100293
294#if defined(MBEDTLS_TEST_DEPRECATED)
295 /* Check deprecated constants. */
296 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
297 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
298 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
299 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
300 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
301 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
302 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
303 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100304
Paul Elliott8ff510a2020-06-02 17:19:28 +0100305 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
324 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
328 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
329 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
330 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
331 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
333 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
334 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
335
336 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
337 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
338 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
339 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
340 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
341 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
342 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
343 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100344
Paul Elliott75e27032020-06-03 15:17:39 +0100345 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
347 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
348 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
349 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
350
351 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
352 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100353#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200354}
355/* END_CASE */
356
357/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200358void import_with_policy( int type_arg,
359 int usage_arg, int alg_arg,
360 int expected_status_arg )
361{
362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
363 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200364 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200365 psa_key_type_t type = type_arg;
366 psa_key_usage_t usage = usage_arg;
367 psa_algorithm_t alg = alg_arg;
368 psa_status_t expected_status = expected_status_arg;
369 const uint8_t key_material[16] = {0};
370 psa_status_t status;
371
372 PSA_ASSERT( psa_crypto_init( ) );
373
374 psa_set_key_type( &attributes, type );
375 psa_set_key_usage_flags( &attributes, usage );
376 psa_set_key_algorithm( &attributes, alg );
377
378 status = psa_import_key( &attributes,
379 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200380 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200381 TEST_EQUAL( status, expected_status );
382 if( status != PSA_SUCCESS )
383 goto exit;
384
Ronald Cron5425a212020-08-04 14:58:35 +0200385 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200386 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200387 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200388 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200389 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200390 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200391
Ronald Cron5425a212020-08-04 14:58:35 +0200392 PSA_ASSERT( psa_destroy_key( key ) );
393 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200394
395exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100396 /*
397 * Key attributes may have been returned by psa_get_key_attributes()
398 * thus reset them as required.
399 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200400 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100401
402 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200403 PSA_DONE( );
404}
405/* END_CASE */
406
407/* BEGIN_CASE */
408void import_with_data( data_t *data, int type_arg,
409 int attr_bits_arg,
410 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200411{
412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
413 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200414 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200415 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200416 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200417 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419
Gilles Peskine8817f612018-12-18 00:18:46 +0100420 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100421
Gilles Peskine4747d192019-04-17 15:05:45 +0200422 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200423 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200424
Ronald Cron5425a212020-08-04 14:58:35 +0200425 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100426 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200427 if( status != PSA_SUCCESS )
428 goto exit;
429
Ronald Cron5425a212020-08-04 14:58:35 +0200430 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200431 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200432 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200433 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200434 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200435
Ronald Cron5425a212020-08-04 14:58:35 +0200436 PSA_ASSERT( psa_destroy_key( key ) );
437 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100438
439exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100440 /*
441 * Key attributes may have been returned by psa_get_key_attributes()
442 * thus reset them as required.
443 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200444 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100445
446 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200447 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100448}
449/* END_CASE */
450
451/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200452void import_large_key( int type_arg, int byte_size_arg,
453 int expected_status_arg )
454{
455 psa_key_type_t type = type_arg;
456 size_t byte_size = byte_size_arg;
457 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
458 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200459 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200460 psa_status_t status;
461 uint8_t *buffer = NULL;
462 size_t buffer_size = byte_size + 1;
463 size_t n;
464
Steven Cooreman69967ce2021-01-18 18:01:08 +0100465 /* Skip the test case if the target running the test cannot
466 * accomodate large keys due to heap size constraints */
467 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200468 memset( buffer, 'K', byte_size );
469
470 PSA_ASSERT( psa_crypto_init( ) );
471
472 /* Try importing the key */
473 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
474 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200475 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100476 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200477 TEST_EQUAL( status, expected_status );
478
479 if( status == PSA_SUCCESS )
480 {
Ronald Cron5425a212020-08-04 14:58:35 +0200481 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200482 TEST_EQUAL( psa_get_key_type( &attributes ), type );
483 TEST_EQUAL( psa_get_key_bits( &attributes ),
484 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200485 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200486 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200487 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200488 for( n = 0; n < byte_size; n++ )
489 TEST_EQUAL( buffer[n], 'K' );
490 for( n = byte_size; n < buffer_size; n++ )
491 TEST_EQUAL( buffer[n], 0 );
492 }
493
494exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100495 /*
496 * Key attributes may have been returned by psa_get_key_attributes()
497 * thus reset them as required.
498 */
499 psa_reset_key_attributes( &attributes );
500
Ronald Cron5425a212020-08-04 14:58:35 +0200501 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200502 PSA_DONE( );
503 mbedtls_free( buffer );
504}
505/* END_CASE */
506
Andrzej Kureke0015962022-01-17 15:29:38 +0100507/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200508void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
509{
Ronald Cron5425a212020-08-04 14:58:35 +0200510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200511 size_t bits = bits_arg;
512 psa_status_t expected_status = expected_status_arg;
513 psa_status_t status;
514 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200515 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516 size_t buffer_size = /* Slight overapproximations */
517 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200518 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200519 unsigned char *p;
520 int ret;
521 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200522 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200523
Gilles Peskine8817f612018-12-18 00:18:46 +0100524 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200525 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200526
527 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
528 bits, keypair ) ) >= 0 );
529 length = ret;
530
531 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200532 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200533 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100534 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200535
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200536 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200537 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200538
539exit:
540 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200541 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200542}
543/* END_CASE */
544
545/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300546void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300547 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200548 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 int expected_bits,
550 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200551 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 int canonical_input )
553{
Ronald Cron5425a212020-08-04 14:58:35 +0200554 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200556 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200557 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 unsigned char *exported = NULL;
560 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100561 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100562 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200565 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566
Moran Pekercb088e72018-07-17 17:36:59 +0300567 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200568 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200570 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572
Gilles Peskine4747d192019-04-17 15:05:45 +0200573 psa_set_key_usage_flags( &attributes, usage_arg );
574 psa_set_key_algorithm( &attributes, alg );
575 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700576
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100577 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200578 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579
580 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200581 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200582 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
583 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200584 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100585
586 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200587 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100588 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100589
590 /* The exported length must be set by psa_export_key() to a value between 0
591 * and export_size. On errors, the exported length must be 0. */
592 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
593 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
594 TEST_ASSERT( exported_length <= export_size );
595
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200596 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200597 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100598 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200599 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100600 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200602 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603
Gilles Peskineea38a922021-02-13 00:05:16 +0100604 /* Run sanity checks on the exported key. For non-canonical inputs,
605 * this validates the canonical representations. For canonical inputs,
606 * this doesn't directly validate the implementation, but it still helps
607 * by cross-validating the test data with the sanity check code. */
608 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200609 goto exit;
610
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100611 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200612 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100613 else
614 {
Ronald Cron5425a212020-08-04 14:58:35 +0200615 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200616 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200617 &key2 ) );
618 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100619 reexported,
620 export_size,
621 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200622 ASSERT_COMPARE( exported, exported_length,
623 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200624 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100626 TEST_ASSERT( exported_length <=
627 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
628 psa_get_key_bits( &got_attributes ) ) );
629 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630
631destroy:
632 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200633 PSA_ASSERT( psa_destroy_key( key ) );
634 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635
636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100637 /*
638 * Key attributes may have been returned by psa_get_key_attributes()
639 * thus reset them as required.
640 */
641 psa_reset_key_attributes( &got_attributes );
642
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643 mbedtls_free( exported );
644 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200645 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646}
647/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100648
Moran Pekerf709f4a2018-06-06 17:26:04 +0300649/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300650void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200651 int type_arg,
652 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100653 int export_size_delta,
654 int expected_export_status_arg,
655 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300656{
Ronald Cron5425a212020-08-04 14:58:35 +0200657 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300658 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200659 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200660 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300661 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100663 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100664 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300666
Gilles Peskine8817f612018-12-18 00:18:46 +0100667 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300668
Gilles Peskine4747d192019-04-17 15:05:45 +0200669 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
670 psa_set_key_algorithm( &attributes, alg );
671 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300672
673 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200674 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300675
Gilles Peskine49c25912018-10-29 15:15:31 +0100676 /* Export the public key */
677 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200678 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200679 exported, export_size,
680 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100681 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100682 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100683 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200684 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100685 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200686 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200687 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100688 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100689 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100690 TEST_ASSERT( expected_public_key->len <=
691 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
692 TEST_ASSERT( expected_public_key->len <=
693 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100694 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
695 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100696 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300697
698exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100699 /*
700 * Key attributes may have been returned by psa_get_key_attributes()
701 * thus reset them as required.
702 */
703 psa_reset_key_attributes( &attributes );
704
itayzafrir3e02b3b2018-06-12 17:06:52 +0300705 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200706 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200707 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300708}
709/* END_CASE */
710
Gilles Peskine20035e32018-02-03 22:44:14 +0100711/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200712void import_and_exercise_key( data_t *data,
713 int type_arg,
714 int bits_arg,
715 int alg_arg )
716{
Ronald Cron5425a212020-08-04 14:58:35 +0200717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200718 psa_key_type_t type = type_arg;
719 size_t bits = bits_arg;
720 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100721 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200723 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200724
Gilles Peskine8817f612018-12-18 00:18:46 +0100725 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200726
Gilles Peskine4747d192019-04-17 15:05:45 +0200727 psa_set_key_usage_flags( &attributes, usage );
728 psa_set_key_algorithm( &attributes, alg );
729 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200730
731 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200732 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200733
734 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200735 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200736 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
737 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200738
739 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100740 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200741 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200742
Ronald Cron5425a212020-08-04 14:58:35 +0200743 PSA_ASSERT( psa_destroy_key( key ) );
744 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200745
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200746exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100747 /*
748 * Key attributes may have been returned by psa_get_key_attributes()
749 * thus reset them as required.
750 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200751 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100752
753 psa_reset_key_attributes( &attributes );
754 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200755 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200756}
757/* END_CASE */
758
759/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100760void effective_key_attributes( int type_arg, int expected_type_arg,
761 int bits_arg, int expected_bits_arg,
762 int usage_arg, int expected_usage_arg,
763 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200764{
Ronald Cron5425a212020-08-04 14:58:35 +0200765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100766 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100767 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100768 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100769 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200770 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100771 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200772 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100773 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200775
Gilles Peskine8817f612018-12-18 00:18:46 +0100776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200777
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200778 psa_set_key_usage_flags( &attributes, usage );
779 psa_set_key_algorithm( &attributes, alg );
780 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100781 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200782
Ronald Cron5425a212020-08-04 14:58:35 +0200783 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100784 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200785
Ronald Cron5425a212020-08-04 14:58:35 +0200786 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100787 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
788 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
789 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
790 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200791
792exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100793 /*
794 * Key attributes may have been returned by psa_get_key_attributes()
795 * thus reset them as required.
796 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200797 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100798
799 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200800 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200801}
802/* END_CASE */
803
804/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100805void check_key_policy( int type_arg, int bits_arg,
806 int usage_arg, int alg_arg )
807{
808 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200809 usage_arg,
810 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200811 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100812 goto exit;
813}
814/* END_CASE */
815
816/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200817void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000818{
819 /* Test each valid way of initializing the object, except for `= {0}`, as
820 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
821 * though it's OK by the C standard. We could test for this, but we'd need
822 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200823 psa_key_attributes_t func = psa_key_attributes_init( );
824 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
825 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000826
827 memset( &zero, 0, sizeof( zero ) );
828
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200829 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
830 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
831 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000832
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200833 TEST_EQUAL( psa_get_key_type( &func ), 0 );
834 TEST_EQUAL( psa_get_key_type( &init ), 0 );
835 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
836
837 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
838 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
839 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
840
841 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
842 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
843 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
844
845 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
846 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
847 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000848}
849/* END_CASE */
850
851/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200852void mac_key_policy( int policy_usage_arg,
853 int policy_alg_arg,
854 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200855 data_t *key_data,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200856 int exercise_alg_arg,
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200857 int expected_status_sign_arg,
858 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200859{
Ronald Cron5425a212020-08-04 14:58:35 +0200860 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200861 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000862 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200863 psa_key_type_t key_type = key_type_arg;
864 psa_algorithm_t policy_alg = policy_alg_arg;
865 psa_algorithm_t exercise_alg = exercise_alg_arg;
866 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200867 psa_status_t status;
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200868 psa_status_t expected_status_sign = expected_status_sign_arg;
869 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200870 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200871
Gilles Peskine8817f612018-12-18 00:18:46 +0100872 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200873
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200874 psa_set_key_usage_flags( &attributes, policy_usage );
875 psa_set_key_algorithm( &attributes, policy_alg );
876 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200877
Gilles Peskine049c7532019-05-15 20:22:09 +0200878 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200879 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200880
gabor-mezei-arm659af9e2021-06-29 11:06:16 +0200881 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
882 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200883
Ronald Cron5425a212020-08-04 14:58:35 +0200884 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200885 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100886
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200887 /* Calculate the MAC, one-shot case. */
888 uint8_t input[128] = {0};
889 size_t mac_len;
890 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
891 input, 128,
892 mac, PSA_MAC_MAX_SIZE, &mac_len ),
893 expected_status_sign );
894
895 /* Verify correct MAC, one-shot case. */
896 status = psa_mac_verify( key, exercise_alg, input, 128,
897 mac, mac_len );
898
899 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
900 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
901 else
902 TEST_EQUAL( status, expected_status_verify );
903
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200904 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200905
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200906 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200907 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200908 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200909
910exit:
911 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200912 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200913 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200914}
915/* END_CASE */
916
917/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200918void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200919 int policy_alg,
920 int key_type,
921 data_t *key_data,
922 int exercise_alg )
923{
Ronald Cron5425a212020-08-04 14:58:35 +0200924 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200925 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000926 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200927 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928 psa_status_t status;
929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200931
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200932 psa_set_key_usage_flags( &attributes, policy_usage );
933 psa_set_key_algorithm( &attributes, policy_alg );
934 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935
Gilles Peskine049c7532019-05-15 20:22:09 +0200936 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200937 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200938
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200939 /* Check if no key usage flag implication is done */
940 TEST_EQUAL( policy_usage,
941 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200942
Ronald Cron5425a212020-08-04 14:58:35 +0200943 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200944 if( policy_alg == exercise_alg &&
945 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100946 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200947 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100948 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949 psa_cipher_abort( &operation );
950
Ronald Cron5425a212020-08-04 14:58:35 +0200951 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200952 if( policy_alg == exercise_alg &&
953 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100954 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100956 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200957
958exit:
959 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200960 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200961 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962}
963/* END_CASE */
964
965/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200966void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200967 int policy_alg,
968 int key_type,
969 data_t *key_data,
970 int nonce_length_arg,
971 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100972 int exercise_alg,
973 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200974{
Ronald Cron5425a212020-08-04 14:58:35 +0200975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200976 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200977 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200978 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100979 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200980 unsigned char nonce[16] = {0};
981 size_t nonce_length = nonce_length_arg;
982 unsigned char tag[16];
983 size_t tag_length = tag_length_arg;
984 size_t output_length;
985
986 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
987 TEST_ASSERT( tag_length <= sizeof( tag ) );
988
Gilles Peskine8817f612018-12-18 00:18:46 +0100989 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200990
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200991 psa_set_key_usage_flags( &attributes, policy_usage );
992 psa_set_key_algorithm( &attributes, policy_alg );
993 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200994
Gilles Peskine049c7532019-05-15 20:22:09 +0200995 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200996 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200998 /* Check if no key usage implication is done */
999 TEST_EQUAL( policy_usage,
1000 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001001
Ronald Cron5425a212020-08-04 14:58:35 +02001002 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003 nonce, nonce_length,
1004 NULL, 0,
1005 NULL, 0,
1006 tag, tag_length,
1007 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001008 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1009 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001011 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012
1013 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001014 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001015 nonce, nonce_length,
1016 NULL, 0,
1017 tag, tag_length,
1018 NULL, 0,
1019 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001020 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1021 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1022 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001023 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001024 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001025 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001026
1027exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001028 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001029 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001030}
1031/* END_CASE */
1032
1033/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001034void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001035 int policy_alg,
1036 int key_type,
1037 data_t *key_data,
1038 int exercise_alg )
1039{
Ronald Cron5425a212020-08-04 14:58:35 +02001040 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001041 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001042 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043 psa_status_t status;
1044 size_t key_bits;
1045 size_t buffer_length;
1046 unsigned char *buffer = NULL;
1047 size_t output_length;
1048
Gilles Peskine8817f612018-12-18 00:18:46 +01001049 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001050
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001051 psa_set_key_usage_flags( &attributes, policy_usage );
1052 psa_set_key_algorithm( &attributes, policy_alg );
1053 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001054
Gilles Peskine049c7532019-05-15 20:22:09 +02001055 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001056 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001057
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001058 /* Check if no key usage implication is done */
1059 TEST_EQUAL( policy_usage,
1060 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001061
Ronald Cron5425a212020-08-04 14:58:35 +02001062 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001063 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1065 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001066 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001067
Ronald Cron5425a212020-08-04 14:58:35 +02001068 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001069 NULL, 0,
1070 NULL, 0,
1071 buffer, buffer_length,
1072 &output_length );
1073 if( policy_alg == exercise_alg &&
1074 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001075 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001076 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001077 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001078
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001079 if( buffer_length != 0 )
1080 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001081 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001082 buffer, buffer_length,
1083 NULL, 0,
1084 buffer, buffer_length,
1085 &output_length );
1086 if( policy_alg == exercise_alg &&
1087 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001088 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001089 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001090 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001091
1092exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001093 /*
1094 * Key attributes may have been returned by psa_get_key_attributes()
1095 * thus reset them as required.
1096 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001097 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001098
1099 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001100 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001101 mbedtls_free( buffer );
1102}
1103/* END_CASE */
1104
1105/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001106void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001107 int policy_alg,
1108 int key_type,
1109 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001110 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001111 int payload_length_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001112 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001113{
Ronald Cron5425a212020-08-04 14:58:35 +02001114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001116 psa_key_usage_t policy_usage = policy_usage_arg;
1117 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001119 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1120 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1121 * compatible with the policy and `payload_length_arg` is supposed to be
1122 * a valid input length to sign. If `payload_length_arg <= 0`,
1123 * `exercise_alg` is supposed to be forbidden by the policy. */
1124 int compatible_alg = payload_length_arg > 0;
1125 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001126 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001127 size_t signature_length;
1128
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001129 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001130 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001131 TEST_EQUAL( expected_usage,
1132 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001133
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001135
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001136 psa_set_key_usage_flags( &attributes, policy_usage );
1137 psa_set_key_algorithm( &attributes, policy_alg );
1138 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139
Gilles Peskine049c7532019-05-15 20:22:09 +02001140 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001141 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001142
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001143 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1144
Ronald Cron5425a212020-08-04 14:58:35 +02001145 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001146 payload, payload_length,
1147 signature, sizeof( signature ),
1148 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001149 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001150 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001151 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001152 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001153
1154 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001155 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001156 payload, payload_length,
1157 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001158 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001159 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001160 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001161 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001162
Gilles Peskine8cb22c82021-09-22 16:15:05 +02001163 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-arm79df41d2021-06-28 14:53:49 +02001164 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001165 {
1166 status = psa_sign_message( key, exercise_alg,
1167 payload, payload_length,
1168 signature, sizeof( signature ),
1169 &signature_length );
1170 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1171 PSA_ASSERT( status );
1172 else
1173 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1174
1175 memset( signature, 0, sizeof( signature ) );
1176 status = psa_verify_message( key, exercise_alg,
1177 payload, payload_length,
1178 signature, sizeof( signature ) );
1179 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1180 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1181 else
1182 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1183 }
1184
Gilles Peskined5b33222018-06-18 22:20:03 +02001185exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001186 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001187 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001188}
1189/* END_CASE */
1190
Janos Follathba3fab92019-06-11 14:50:16 +01001191/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001192void derive_key_policy( int policy_usage,
1193 int policy_alg,
1194 int key_type,
1195 data_t *key_data,
1196 int exercise_alg )
1197{
Ronald Cron5425a212020-08-04 14:58:35 +02001198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001199 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001200 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001201 psa_status_t status;
1202
Gilles Peskine8817f612018-12-18 00:18:46 +01001203 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001204
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001205 psa_set_key_usage_flags( &attributes, policy_usage );
1206 psa_set_key_algorithm( &attributes, policy_alg );
1207 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001208
Gilles Peskine049c7532019-05-15 20:22:09 +02001209 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001210 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001211
Janos Follathba3fab92019-06-11 14:50:16 +01001212 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1213
1214 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1215 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001216 {
Janos Follathba3fab92019-06-11 14:50:16 +01001217 PSA_ASSERT( psa_key_derivation_input_bytes(
1218 &operation,
1219 PSA_KEY_DERIVATION_INPUT_SEED,
1220 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001221 }
Janos Follathba3fab92019-06-11 14:50:16 +01001222
1223 status = psa_key_derivation_input_key( &operation,
1224 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001225 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001226
Gilles Peskineea0fb492018-07-12 17:17:20 +02001227 if( policy_alg == exercise_alg &&
1228 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001229 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001230 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001231 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001232
1233exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001234 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001235 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001236 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001237}
1238/* END_CASE */
1239
1240/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001241void agreement_key_policy( int policy_usage,
1242 int policy_alg,
1243 int key_type_arg,
1244 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001245 int exercise_alg,
1246 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001247{
Ronald Cron5425a212020-08-04 14:58:35 +02001248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001250 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001251 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001252 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001253 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001254
Gilles Peskine8817f612018-12-18 00:18:46 +01001255 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001257 psa_set_key_usage_flags( &attributes, policy_usage );
1258 psa_set_key_algorithm( &attributes, policy_alg );
1259 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001260
Gilles Peskine049c7532019-05-15 20:22:09 +02001261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001262 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001263
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001264 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001265 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001266
Steven Cooremance48e852020-10-05 16:02:45 +02001267 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001268
1269exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001270 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001271 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001272 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001273}
1274/* END_CASE */
1275
1276/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001277void key_policy_alg2( int key_type_arg, data_t *key_data,
1278 int usage_arg, int alg_arg, int alg2_arg )
1279{
Ronald Cron5425a212020-08-04 14:58:35 +02001280 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001281 psa_key_type_t key_type = key_type_arg;
1282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1283 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1284 psa_key_usage_t usage = usage_arg;
1285 psa_algorithm_t alg = alg_arg;
1286 psa_algorithm_t alg2 = alg2_arg;
1287
1288 PSA_ASSERT( psa_crypto_init( ) );
1289
1290 psa_set_key_usage_flags( &attributes, usage );
1291 psa_set_key_algorithm( &attributes, alg );
1292 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1293 psa_set_key_type( &attributes, key_type );
1294 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001295 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001296
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001297 /* Update the usage flags to obtain implicit usage flags */
1298 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001299 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001300 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1301 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1302 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1303
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001304 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001305 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001306 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001307 goto exit;
1308
1309exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001310 /*
1311 * Key attributes may have been returned by psa_get_key_attributes()
1312 * thus reset them as required.
1313 */
1314 psa_reset_key_attributes( &got_attributes );
1315
Ronald Cron5425a212020-08-04 14:58:35 +02001316 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001317 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001318}
1319/* END_CASE */
1320
1321/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001322void raw_agreement_key_policy( int policy_usage,
1323 int policy_alg,
1324 int key_type_arg,
1325 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001326 int exercise_alg,
1327 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001328{
Ronald Cron5425a212020-08-04 14:58:35 +02001329 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001330 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001331 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001332 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001333 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001334 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001335
1336 PSA_ASSERT( psa_crypto_init( ) );
1337
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001338 psa_set_key_usage_flags( &attributes, policy_usage );
1339 psa_set_key_algorithm( &attributes, policy_alg );
1340 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001341
Gilles Peskine049c7532019-05-15 20:22:09 +02001342 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001343 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001344
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001345 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001346
Steven Cooremance48e852020-10-05 16:02:45 +02001347 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001348
1349exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001350 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001351 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001352 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001353}
1354/* END_CASE */
1355
1356/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001357void copy_success( int source_usage_arg,
1358 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001359 int type_arg, data_t *material,
1360 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001361 int target_usage_arg,
1362 int target_alg_arg, int target_alg2_arg,
1363 int expected_usage_arg,
1364 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001365{
Gilles Peskineca25db92019-04-19 11:43:08 +02001366 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1367 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001368 psa_key_usage_t expected_usage = expected_usage_arg;
1369 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001370 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001371 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1372 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001373 uint8_t *export_buffer = NULL;
1374
Gilles Peskine57ab7212019-01-28 13:03:09 +01001375 PSA_ASSERT( psa_crypto_init( ) );
1376
Gilles Peskineca25db92019-04-19 11:43:08 +02001377 /* Prepare the source key. */
1378 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1379 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001380 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001381 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001382 PSA_ASSERT( psa_import_key( &source_attributes,
1383 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001384 &source_key ) );
1385 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001386
Gilles Peskineca25db92019-04-19 11:43:08 +02001387 /* Prepare the target attributes. */
1388 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001389 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001390 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001391 /* Set volatile lifetime to reset the key identifier to 0. */
1392 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1393 }
1394
Gilles Peskineca25db92019-04-19 11:43:08 +02001395 if( target_usage_arg != -1 )
1396 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1397 if( target_alg_arg != -1 )
1398 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001399 if( target_alg2_arg != -1 )
1400 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001401
1402 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001403 PSA_ASSERT( psa_copy_key( source_key,
1404 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001405
1406 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001407 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001408
1409 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001410 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001411 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1412 psa_get_key_type( &target_attributes ) );
1413 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1414 psa_get_key_bits( &target_attributes ) );
1415 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1416 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001417 TEST_EQUAL( expected_alg2,
1418 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001419 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1420 {
1421 size_t length;
1422 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001423 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001424 material->len, &length ) );
1425 ASSERT_COMPARE( material->x, material->len,
1426 export_buffer, length );
1427 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001428
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001429 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001430 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001431 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001432 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001433
Ronald Cron5425a212020-08-04 14:58:35 +02001434 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001435
1436exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001437 /*
1438 * Source and target key attributes may have been returned by
1439 * psa_get_key_attributes() thus reset them as required.
1440 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001441 psa_reset_key_attributes( &source_attributes );
1442 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001443
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001444 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001445 mbedtls_free( export_buffer );
1446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001450void copy_fail( int source_usage_arg,
1451 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001452 int type_arg, data_t *material,
1453 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001454 int target_usage_arg,
1455 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001456 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001457 int expected_status_arg )
1458{
1459 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1460 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001461 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1462 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001463 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001464
1465 PSA_ASSERT( psa_crypto_init( ) );
1466
1467 /* Prepare the source key. */
1468 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1469 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001470 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001471 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001472 PSA_ASSERT( psa_import_key( &source_attributes,
1473 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001474 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001475
1476 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001477 psa_set_key_id( &target_attributes, key_id );
1478 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001479 psa_set_key_type( &target_attributes, target_type_arg );
1480 psa_set_key_bits( &target_attributes, target_bits_arg );
1481 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1482 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001483 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001484
1485 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001486 TEST_EQUAL( psa_copy_key( source_key,
1487 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001488 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001489
Ronald Cron5425a212020-08-04 14:58:35 +02001490 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001491
Gilles Peskine4a644642019-05-03 17:14:08 +02001492exit:
1493 psa_reset_key_attributes( &source_attributes );
1494 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001495 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001496}
1497/* END_CASE */
1498
1499/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001500void hash_operation_init( )
1501{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001502 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001503 /* Test each valid way of initializing the object, except for `= {0}`, as
1504 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1505 * though it's OK by the C standard. We could test for this, but we'd need
1506 * to supress the Clang warning for the test. */
1507 psa_hash_operation_t func = psa_hash_operation_init( );
1508 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1509 psa_hash_operation_t zero;
1510
1511 memset( &zero, 0, sizeof( zero ) );
1512
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001513 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001514 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1515 PSA_ERROR_BAD_STATE );
1516 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1517 PSA_ERROR_BAD_STATE );
1518 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1519 PSA_ERROR_BAD_STATE );
1520
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001521 /* A default hash operation should be abortable without error. */
1522 PSA_ASSERT( psa_hash_abort( &func ) );
1523 PSA_ASSERT( psa_hash_abort( &init ) );
1524 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001525}
1526/* END_CASE */
1527
1528/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001529void hash_setup( int alg_arg,
1530 int expected_status_arg )
1531{
1532 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001533 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001534 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001535 psa_status_t status;
1536
Gilles Peskine8817f612018-12-18 00:18:46 +01001537 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001538
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001539 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001540 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001541
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001542 /* Whether setup succeeded or failed, abort must succeed. */
1543 PSA_ASSERT( psa_hash_abort( &operation ) );
1544
1545 /* If setup failed, reproduce the failure, so as to
1546 * test the resulting state of the operation object. */
1547 if( status != PSA_SUCCESS )
1548 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1549
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001550 /* Now the operation object should be reusable. */
1551#if defined(KNOWN_SUPPORTED_HASH_ALG)
1552 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1553 PSA_ASSERT( psa_hash_abort( &operation ) );
1554#endif
1555
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001556exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001557 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001558}
1559/* END_CASE */
1560
1561/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001562void hash_compute_fail( int alg_arg, data_t *input,
1563 int output_size_arg, int expected_status_arg )
1564{
1565 psa_algorithm_t alg = alg_arg;
1566 uint8_t *output = NULL;
1567 size_t output_size = output_size_arg;
1568 size_t output_length = INVALID_EXPORT_LENGTH;
1569 psa_status_t expected_status = expected_status_arg;
1570 psa_status_t status;
1571
1572 ASSERT_ALLOC( output, output_size );
1573
1574 PSA_ASSERT( psa_crypto_init( ) );
1575
1576 status = psa_hash_compute( alg, input->x, input->len,
1577 output, output_size, &output_length );
1578 TEST_EQUAL( status, expected_status );
1579 TEST_ASSERT( output_length <= output_size );
1580
1581exit:
1582 mbedtls_free( output );
1583 PSA_DONE( );
1584}
1585/* END_CASE */
1586
1587/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001588void hash_compare_fail( int alg_arg, data_t *input,
1589 data_t *reference_hash,
1590 int expected_status_arg )
1591{
1592 psa_algorithm_t alg = alg_arg;
1593 psa_status_t expected_status = expected_status_arg;
1594 psa_status_t status;
1595
1596 PSA_ASSERT( psa_crypto_init( ) );
1597
1598 status = psa_hash_compare( alg, input->x, input->len,
1599 reference_hash->x, reference_hash->len );
1600 TEST_EQUAL( status, expected_status );
1601
1602exit:
1603 PSA_DONE( );
1604}
1605/* END_CASE */
1606
1607/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001608void hash_compute_compare( int alg_arg, data_t *input,
1609 data_t *expected_output )
1610{
1611 psa_algorithm_t alg = alg_arg;
1612 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1613 size_t output_length = INVALID_EXPORT_LENGTH;
1614 size_t i;
1615
1616 PSA_ASSERT( psa_crypto_init( ) );
1617
1618 /* Compute with tight buffer */
1619 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001620 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001621 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001622 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001623 ASSERT_COMPARE( output, output_length,
1624 expected_output->x, expected_output->len );
1625
1626 /* Compute with larger buffer */
1627 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1628 output, sizeof( output ),
1629 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001630 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001631 ASSERT_COMPARE( output, output_length,
1632 expected_output->x, expected_output->len );
1633
1634 /* Compare with correct hash */
1635 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1636 output, output_length ) );
1637
1638 /* Compare with trailing garbage */
1639 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1640 output, output_length + 1 ),
1641 PSA_ERROR_INVALID_SIGNATURE );
1642
1643 /* Compare with truncated hash */
1644 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1645 output, output_length - 1 ),
1646 PSA_ERROR_INVALID_SIGNATURE );
1647
1648 /* Compare with corrupted value */
1649 for( i = 0; i < output_length; i++ )
1650 {
Chris Jones9634bb12021-01-20 15:56:42 +00001651 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001652 output[i] ^= 1;
1653 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1654 output, output_length ),
1655 PSA_ERROR_INVALID_SIGNATURE );
1656 output[i] ^= 1;
1657 }
1658
1659exit:
1660 PSA_DONE( );
1661}
1662/* END_CASE */
1663
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001664/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001665void hash_bad_order( )
1666{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001667 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001668 unsigned char input[] = "";
1669 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001670 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001671 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1672 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1673 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001674 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001675 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001676 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001677
Gilles Peskine8817f612018-12-18 00:18:46 +01001678 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001679
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001680 /* Call setup twice in a row. */
1681 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001682 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001683 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1684 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001685 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001686 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001687 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001688
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001689 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001690 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001691 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001692 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001693
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001694 /* Check that update calls abort on error. */
1695 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman54f73512021-06-24 18:14:52 +01001696 operation.id = UINT_MAX;
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001697 ASSERT_OPERATION_IS_ACTIVE( operation );
1698 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1699 PSA_ERROR_BAD_STATE );
1700 ASSERT_OPERATION_IS_INACTIVE( operation );
1701 PSA_ASSERT( psa_hash_abort( &operation ) );
1702 ASSERT_OPERATION_IS_INACTIVE( operation );
1703
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001704 /* Call update after finish. */
1705 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1706 PSA_ASSERT( psa_hash_finish( &operation,
1707 hash, sizeof( hash ), &hash_len ) );
1708 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001709 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001710 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001711
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001712 /* Call verify without calling setup beforehand. */
1713 TEST_EQUAL( psa_hash_verify( &operation,
1714 valid_hash, sizeof( valid_hash ) ),
1715 PSA_ERROR_BAD_STATE );
1716 PSA_ASSERT( psa_hash_abort( &operation ) );
1717
1718 /* Call verify after finish. */
1719 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1720 PSA_ASSERT( psa_hash_finish( &operation,
1721 hash, sizeof( hash ), &hash_len ) );
1722 TEST_EQUAL( psa_hash_verify( &operation,
1723 valid_hash, sizeof( valid_hash ) ),
1724 PSA_ERROR_BAD_STATE );
1725 PSA_ASSERT( psa_hash_abort( &operation ) );
1726
1727 /* Call verify twice in a row. */
1728 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001729 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001730 PSA_ASSERT( psa_hash_verify( &operation,
1731 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001732 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001733 TEST_EQUAL( psa_hash_verify( &operation,
1734 valid_hash, sizeof( valid_hash ) ),
1735 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001736 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001737 PSA_ASSERT( psa_hash_abort( &operation ) );
1738
1739 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001740 TEST_EQUAL( psa_hash_finish( &operation,
1741 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001742 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001743 PSA_ASSERT( psa_hash_abort( &operation ) );
1744
1745 /* Call finish twice in a row. */
1746 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1747 PSA_ASSERT( psa_hash_finish( &operation,
1748 hash, sizeof( hash ), &hash_len ) );
1749 TEST_EQUAL( psa_hash_finish( &operation,
1750 hash, sizeof( hash ), &hash_len ),
1751 PSA_ERROR_BAD_STATE );
1752 PSA_ASSERT( psa_hash_abort( &operation ) );
1753
1754 /* Call finish after calling verify. */
1755 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1756 PSA_ASSERT( psa_hash_verify( &operation,
1757 valid_hash, sizeof( valid_hash ) ) );
1758 TEST_EQUAL( psa_hash_finish( &operation,
1759 hash, sizeof( hash ), &hash_len ),
1760 PSA_ERROR_BAD_STATE );
1761 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001762
1763exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001764 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001765}
1766/* END_CASE */
1767
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001768/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001769void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001770{
1771 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001772 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1773 * appended to it */
1774 unsigned char hash[] = {
1775 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1776 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1777 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001778 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001779 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001780
Gilles Peskine8817f612018-12-18 00:18:46 +01001781 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001782
itayzafrir27e69452018-11-01 14:26:34 +02001783 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001784 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001785 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001786 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001787 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001788 ASSERT_OPERATION_IS_INACTIVE( operation );
1789 PSA_ASSERT( psa_hash_abort( &operation ) );
1790 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001791
itayzafrir27e69452018-11-01 14:26:34 +02001792 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001793 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001794 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001795 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001796
itayzafrir27e69452018-11-01 14:26:34 +02001797 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001798 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001799 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001800 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001801
itayzafrirec93d302018-10-18 18:01:10 +03001802exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001803 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001804}
1805/* END_CASE */
1806
Ronald Cronee414c72021-03-18 18:50:08 +01001807/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001808void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001809{
1810 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001811 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001812 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001813 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001814 size_t hash_len;
1815
Gilles Peskine8817f612018-12-18 00:18:46 +01001816 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001817
itayzafrir58028322018-10-25 10:22:01 +03001818 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001819 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001820 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001821 hash, expected_size - 1, &hash_len ),
1822 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001823
1824exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001825 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001826}
1827/* END_CASE */
1828
Ronald Cronee414c72021-03-18 18:50:08 +01001829/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001830void hash_clone_source_state( )
1831{
1832 psa_algorithm_t alg = PSA_ALG_SHA_256;
1833 unsigned char hash[PSA_HASH_MAX_SIZE];
1834 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1835 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1836 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1837 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1838 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1839 size_t hash_len;
1840
1841 PSA_ASSERT( psa_crypto_init( ) );
1842 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1843
1844 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1845 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1846 PSA_ASSERT( psa_hash_finish( &op_finished,
1847 hash, sizeof( hash ), &hash_len ) );
1848 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1849 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1850
1851 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1852 PSA_ERROR_BAD_STATE );
1853
1854 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1855 PSA_ASSERT( psa_hash_finish( &op_init,
1856 hash, sizeof( hash ), &hash_len ) );
1857 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1858 PSA_ASSERT( psa_hash_finish( &op_finished,
1859 hash, sizeof( hash ), &hash_len ) );
1860 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1861 PSA_ASSERT( psa_hash_finish( &op_aborted,
1862 hash, sizeof( hash ), &hash_len ) );
1863
1864exit:
1865 psa_hash_abort( &op_source );
1866 psa_hash_abort( &op_init );
1867 psa_hash_abort( &op_setup );
1868 psa_hash_abort( &op_finished );
1869 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001870 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001871}
1872/* END_CASE */
1873
Ronald Cronee414c72021-03-18 18:50:08 +01001874/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001875void hash_clone_target_state( )
1876{
1877 psa_algorithm_t alg = PSA_ALG_SHA_256;
1878 unsigned char hash[PSA_HASH_MAX_SIZE];
1879 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1880 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1881 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1882 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1883 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1884 size_t hash_len;
1885
1886 PSA_ASSERT( psa_crypto_init( ) );
1887
1888 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1889 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1890 PSA_ASSERT( psa_hash_finish( &op_finished,
1891 hash, sizeof( hash ), &hash_len ) );
1892 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1893 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1894
1895 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1896 PSA_ASSERT( psa_hash_finish( &op_target,
1897 hash, sizeof( hash ), &hash_len ) );
1898
1899 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1900 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1901 PSA_ERROR_BAD_STATE );
1902 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1903 PSA_ERROR_BAD_STATE );
1904
1905exit:
1906 psa_hash_abort( &op_target );
1907 psa_hash_abort( &op_init );
1908 psa_hash_abort( &op_setup );
1909 psa_hash_abort( &op_finished );
1910 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001911 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001912}
1913/* END_CASE */
1914
itayzafrir58028322018-10-25 10:22:01 +03001915/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001916void mac_operation_init( )
1917{
Jaeden Amero252ef282019-02-15 14:05:35 +00001918 const uint8_t input[1] = { 0 };
1919
Jaeden Amero769ce272019-01-04 11:48:03 +00001920 /* Test each valid way of initializing the object, except for `= {0}`, as
1921 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1922 * though it's OK by the C standard. We could test for this, but we'd need
1923 * to supress the Clang warning for the test. */
1924 psa_mac_operation_t func = psa_mac_operation_init( );
1925 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1926 psa_mac_operation_t zero;
1927
1928 memset( &zero, 0, sizeof( zero ) );
1929
Jaeden Amero252ef282019-02-15 14:05:35 +00001930 /* A freshly-initialized MAC operation should not be usable. */
1931 TEST_EQUAL( psa_mac_update( &func,
1932 input, sizeof( input ) ),
1933 PSA_ERROR_BAD_STATE );
1934 TEST_EQUAL( psa_mac_update( &init,
1935 input, sizeof( input ) ),
1936 PSA_ERROR_BAD_STATE );
1937 TEST_EQUAL( psa_mac_update( &zero,
1938 input, sizeof( input ) ),
1939 PSA_ERROR_BAD_STATE );
1940
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001941 /* A default MAC operation should be abortable without error. */
1942 PSA_ASSERT( psa_mac_abort( &func ) );
1943 PSA_ASSERT( psa_mac_abort( &init ) );
1944 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001945}
1946/* END_CASE */
1947
1948/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001949void mac_setup( int key_type_arg,
1950 data_t *key,
1951 int alg_arg,
1952 int expected_status_arg )
1953{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001954 psa_key_type_t key_type = key_type_arg;
1955 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001956 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001957 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001958 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1959#if defined(KNOWN_SUPPORTED_MAC_ALG)
1960 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1961#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001962
Gilles Peskine8817f612018-12-18 00:18:46 +01001963 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001964
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001965 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1966 &operation, &status ) )
1967 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001968 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001969
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001970 /* The operation object should be reusable. */
1971#if defined(KNOWN_SUPPORTED_MAC_ALG)
1972 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1973 smoke_test_key_data,
1974 sizeof( smoke_test_key_data ),
1975 KNOWN_SUPPORTED_MAC_ALG,
1976 &operation, &status ) )
1977 goto exit;
1978 TEST_EQUAL( status, PSA_SUCCESS );
1979#endif
1980
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001981exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001982 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001983}
1984/* END_CASE */
1985
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001986/* 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 +00001987void mac_bad_order( )
1988{
Ronald Cron5425a212020-08-04 14:58:35 +02001989 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001990 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1991 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001992 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001993 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1994 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1995 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001997 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1998 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1999 size_t sign_mac_length = 0;
2000 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2001 const uint8_t verify_mac[] = {
2002 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2003 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2004 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2005
2006 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002007 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002008 psa_set_key_algorithm( &attributes, alg );
2009 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002010
Ronald Cron5425a212020-08-04 14:58:35 +02002011 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2012 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002013
Jaeden Amero252ef282019-02-15 14:05:35 +00002014 /* Call update without calling setup beforehand. */
2015 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2016 PSA_ERROR_BAD_STATE );
2017 PSA_ASSERT( psa_mac_abort( &operation ) );
2018
2019 /* Call sign finish without calling setup beforehand. */
2020 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2021 &sign_mac_length),
2022 PSA_ERROR_BAD_STATE );
2023 PSA_ASSERT( psa_mac_abort( &operation ) );
2024
2025 /* Call verify finish without calling setup beforehand. */
2026 TEST_EQUAL( psa_mac_verify_finish( &operation,
2027 verify_mac, sizeof( verify_mac ) ),
2028 PSA_ERROR_BAD_STATE );
2029 PSA_ASSERT( psa_mac_abort( &operation ) );
2030
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002031 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002032 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002033 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002034 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002035 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002036 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002037 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002038 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002039
Jaeden Amero252ef282019-02-15 14:05:35 +00002040 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002041 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002042 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2043 PSA_ASSERT( psa_mac_sign_finish( &operation,
2044 sign_mac, sizeof( sign_mac ),
2045 &sign_mac_length ) );
2046 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2047 PSA_ERROR_BAD_STATE );
2048 PSA_ASSERT( psa_mac_abort( &operation ) );
2049
2050 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002051 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002052 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2053 PSA_ASSERT( psa_mac_verify_finish( &operation,
2054 verify_mac, sizeof( verify_mac ) ) );
2055 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2056 PSA_ERROR_BAD_STATE );
2057 PSA_ASSERT( psa_mac_abort( &operation ) );
2058
2059 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002060 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002061 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2062 PSA_ASSERT( psa_mac_sign_finish( &operation,
2063 sign_mac, sizeof( sign_mac ),
2064 &sign_mac_length ) );
2065 TEST_EQUAL( psa_mac_sign_finish( &operation,
2066 sign_mac, sizeof( sign_mac ),
2067 &sign_mac_length ),
2068 PSA_ERROR_BAD_STATE );
2069 PSA_ASSERT( psa_mac_abort( &operation ) );
2070
2071 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002072 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002073 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2074 PSA_ASSERT( psa_mac_verify_finish( &operation,
2075 verify_mac, sizeof( verify_mac ) ) );
2076 TEST_EQUAL( psa_mac_verify_finish( &operation,
2077 verify_mac, sizeof( verify_mac ) ),
2078 PSA_ERROR_BAD_STATE );
2079 PSA_ASSERT( psa_mac_abort( &operation ) );
2080
2081 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002082 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002083 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002084 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002085 TEST_EQUAL( psa_mac_verify_finish( &operation,
2086 verify_mac, sizeof( verify_mac ) ),
2087 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002088 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002089 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002090 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002091
2092 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002093 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002094 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002095 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002096 TEST_EQUAL( psa_mac_sign_finish( &operation,
2097 sign_mac, sizeof( sign_mac ),
2098 &sign_mac_length ),
2099 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002100 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002101 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002102 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002103
Ronald Cron5425a212020-08-04 14:58:35 +02002104 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002105
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002106exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002107 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002108}
2109/* END_CASE */
2110
2111/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002112void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002113 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002114 int alg_arg,
2115 data_t *input,
2116 data_t *expected_mac )
2117{
Ronald Cron5425a212020-08-04 14:58:35 +02002118 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002119 psa_key_type_t key_type = key_type_arg;
2120 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002121 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002123 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002124 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002125 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002126 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002127 const size_t output_sizes_to_test[] = {
2128 0,
2129 1,
2130 expected_mac->len - 1,
2131 expected_mac->len,
2132 expected_mac->len + 1,
2133 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002134
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002135 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002136 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002137 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002138
Gilles Peskine8817f612018-12-18 00:18:46 +01002139 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002142 psa_set_key_algorithm( &attributes, alg );
2143 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002144
Ronald Cron5425a212020-08-04 14:58:35 +02002145 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2146 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002147
Gilles Peskine8b356b52020-08-25 23:44:59 +02002148 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2149 {
2150 const size_t output_size = output_sizes_to_test[i];
2151 psa_status_t expected_status =
2152 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2153 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002154
Chris Jones9634bb12021-01-20 15:56:42 +00002155 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002156 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002157
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002158 /* Calculate the MAC, one-shot case. */
2159 TEST_EQUAL( psa_mac_compute( key, alg,
2160 input->x, input->len,
2161 actual_mac, output_size, &mac_length ),
2162 expected_status );
2163 if( expected_status == PSA_SUCCESS )
2164 {
2165 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2166 actual_mac, mac_length );
2167 }
2168
2169 if( output_size > 0 )
2170 memset( actual_mac, 0, output_size );
2171
2172 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002173 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002174 PSA_ASSERT( psa_mac_update( &operation,
2175 input->x, input->len ) );
2176 TEST_EQUAL( psa_mac_sign_finish( &operation,
2177 actual_mac, output_size,
2178 &mac_length ),
2179 expected_status );
2180 PSA_ASSERT( psa_mac_abort( &operation ) );
2181
2182 if( expected_status == PSA_SUCCESS )
2183 {
2184 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2185 actual_mac, mac_length );
2186 }
2187 mbedtls_free( actual_mac );
2188 actual_mac = NULL;
2189 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002190
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002191exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002192 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002193 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002194 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002195 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002196}
2197/* END_CASE */
2198
2199/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002200void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002201 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002202 int alg_arg,
2203 data_t *input,
2204 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002205{
Ronald Cron5425a212020-08-04 14:58:35 +02002206 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002207 psa_key_type_t key_type = key_type_arg;
2208 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002209 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002211 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002212
Gilles Peskine69c12672018-06-28 00:07:19 +02002213 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2214
Gilles Peskine8817f612018-12-18 00:18:46 +01002215 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002216
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002217 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002218 psa_set_key_algorithm( &attributes, alg );
2219 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002220
Ronald Cron5425a212020-08-04 14:58:35 +02002221 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2222 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002223
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002224 /* Verify correct MAC, one-shot case. */
2225 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2226 expected_mac->x, expected_mac->len ) );
2227
2228 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002229 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002230 PSA_ASSERT( psa_mac_update( &operation,
2231 input->x, input->len ) );
2232 PSA_ASSERT( psa_mac_verify_finish( &operation,
2233 expected_mac->x,
2234 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002235
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002236 /* Test a MAC that's too short, one-shot case. */
2237 TEST_EQUAL( psa_mac_verify( key, alg,
2238 input->x, input->len,
2239 expected_mac->x,
2240 expected_mac->len - 1 ),
2241 PSA_ERROR_INVALID_SIGNATURE );
2242
2243 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002244 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002245 PSA_ASSERT( psa_mac_update( &operation,
2246 input->x, input->len ) );
2247 TEST_EQUAL( psa_mac_verify_finish( &operation,
2248 expected_mac->x,
2249 expected_mac->len - 1 ),
2250 PSA_ERROR_INVALID_SIGNATURE );
2251
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002252 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002253 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2254 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002255 TEST_EQUAL( psa_mac_verify( key, alg,
2256 input->x, input->len,
2257 perturbed_mac, expected_mac->len + 1 ),
2258 PSA_ERROR_INVALID_SIGNATURE );
2259
2260 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002261 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002262 PSA_ASSERT( psa_mac_update( &operation,
2263 input->x, input->len ) );
2264 TEST_EQUAL( psa_mac_verify_finish( &operation,
2265 perturbed_mac,
2266 expected_mac->len + 1 ),
2267 PSA_ERROR_INVALID_SIGNATURE );
2268
2269 /* Test changing one byte. */
2270 for( size_t i = 0; i < expected_mac->len; i++ )
2271 {
Chris Jones9634bb12021-01-20 15:56:42 +00002272 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002273 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002274
2275 TEST_EQUAL( psa_mac_verify( key, alg,
2276 input->x, input->len,
2277 perturbed_mac, expected_mac->len ),
2278 PSA_ERROR_INVALID_SIGNATURE );
2279
Ronald Cron5425a212020-08-04 14:58:35 +02002280 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002281 PSA_ASSERT( psa_mac_update( &operation,
2282 input->x, input->len ) );
2283 TEST_EQUAL( psa_mac_verify_finish( &operation,
2284 perturbed_mac,
2285 expected_mac->len ),
2286 PSA_ERROR_INVALID_SIGNATURE );
2287 perturbed_mac[i] ^= 1;
2288 }
2289
Gilles Peskine8c9def32018-02-08 10:02:12 +01002290exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002291 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002292 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002293 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002294 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002295}
2296/* END_CASE */
2297
2298/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002299void cipher_operation_init( )
2300{
Jaeden Ameroab439972019-02-15 14:12:05 +00002301 const uint8_t input[1] = { 0 };
2302 unsigned char output[1] = { 0 };
2303 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002304 /* Test each valid way of initializing the object, except for `= {0}`, as
2305 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2306 * though it's OK by the C standard. We could test for this, but we'd need
2307 * to supress the Clang warning for the test. */
2308 psa_cipher_operation_t func = psa_cipher_operation_init( );
2309 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2310 psa_cipher_operation_t zero;
2311
2312 memset( &zero, 0, sizeof( zero ) );
2313
Jaeden Ameroab439972019-02-15 14:12:05 +00002314 /* A freshly-initialized cipher operation should not be usable. */
2315 TEST_EQUAL( psa_cipher_update( &func,
2316 input, sizeof( input ),
2317 output, sizeof( output ),
2318 &output_length ),
2319 PSA_ERROR_BAD_STATE );
2320 TEST_EQUAL( psa_cipher_update( &init,
2321 input, sizeof( input ),
2322 output, sizeof( output ),
2323 &output_length ),
2324 PSA_ERROR_BAD_STATE );
2325 TEST_EQUAL( psa_cipher_update( &zero,
2326 input, sizeof( input ),
2327 output, sizeof( output ),
2328 &output_length ),
2329 PSA_ERROR_BAD_STATE );
2330
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002331 /* A default cipher operation should be abortable without error. */
2332 PSA_ASSERT( psa_cipher_abort( &func ) );
2333 PSA_ASSERT( psa_cipher_abort( &init ) );
2334 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002335}
2336/* END_CASE */
2337
2338/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002339void cipher_setup( int key_type_arg,
2340 data_t *key,
2341 int alg_arg,
2342 int expected_status_arg )
2343{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002344 psa_key_type_t key_type = key_type_arg;
2345 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002346 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002347 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002348 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002349#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002350 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2351#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002352
Gilles Peskine8817f612018-12-18 00:18:46 +01002353 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002354
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002355 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2356 &operation, &status ) )
2357 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002358 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002359
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002360 /* The operation object should be reusable. */
2361#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2362 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2363 smoke_test_key_data,
2364 sizeof( smoke_test_key_data ),
2365 KNOWN_SUPPORTED_CIPHER_ALG,
2366 &operation, &status ) )
2367 goto exit;
2368 TEST_EQUAL( status, PSA_SUCCESS );
2369#endif
2370
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002371exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002372 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002373 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002374}
2375/* END_CASE */
2376
Ronald Cronee414c72021-03-18 18:50:08 +01002377/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002378void cipher_bad_order( )
2379{
Ronald Cron5425a212020-08-04 14:58:35 +02002380 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002381 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2382 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002384 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002385 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002386 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002387 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2388 0xaa, 0xaa, 0xaa, 0xaa };
2389 const uint8_t text[] = {
2390 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2391 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002392 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002393 size_t length = 0;
2394
2395 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002396 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2397 psa_set_key_algorithm( &attributes, alg );
2398 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002399 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2400 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002401
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002402 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002403 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002404 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002405 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002406 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002407 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002408 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002409 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002410
2411 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002412 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002413 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002414 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002415 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002416 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002417 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002418 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002419
Jaeden Ameroab439972019-02-15 14:12:05 +00002420 /* Generate an IV without calling setup beforehand. */
2421 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2422 buffer, sizeof( buffer ),
2423 &length ),
2424 PSA_ERROR_BAD_STATE );
2425 PSA_ASSERT( psa_cipher_abort( &operation ) );
2426
2427 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002428 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002429 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2430 buffer, sizeof( buffer ),
2431 &length ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002432 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002433 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2434 buffer, sizeof( buffer ),
2435 &length ),
2436 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002437 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002438 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002439 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002440
2441 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002442 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002443 PSA_ASSERT( psa_cipher_set_iv( &operation,
2444 iv, sizeof( iv ) ) );
2445 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2446 buffer, sizeof( buffer ),
2447 &length ),
2448 PSA_ERROR_BAD_STATE );
2449 PSA_ASSERT( psa_cipher_abort( &operation ) );
2450
2451 /* Set an IV without calling setup beforehand. */
2452 TEST_EQUAL( psa_cipher_set_iv( &operation,
2453 iv, sizeof( iv ) ),
2454 PSA_ERROR_BAD_STATE );
2455 PSA_ASSERT( psa_cipher_abort( &operation ) );
2456
2457 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002458 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002459 PSA_ASSERT( psa_cipher_set_iv( &operation,
2460 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002461 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002462 TEST_EQUAL( psa_cipher_set_iv( &operation,
2463 iv, sizeof( iv ) ),
2464 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002465 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002466 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002467 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002468
2469 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002470 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002471 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2472 buffer, sizeof( buffer ),
2473 &length ) );
2474 TEST_EQUAL( psa_cipher_set_iv( &operation,
2475 iv, sizeof( iv ) ),
2476 PSA_ERROR_BAD_STATE );
2477 PSA_ASSERT( psa_cipher_abort( &operation ) );
2478
2479 /* Call update without calling setup beforehand. */
2480 TEST_EQUAL( psa_cipher_update( &operation,
2481 text, sizeof( text ),
2482 buffer, sizeof( buffer ),
2483 &length ),
2484 PSA_ERROR_BAD_STATE );
2485 PSA_ASSERT( psa_cipher_abort( &operation ) );
2486
2487 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002488 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002489 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002490 TEST_EQUAL( psa_cipher_update( &operation,
2491 text, sizeof( text ),
2492 buffer, sizeof( buffer ),
2493 &length ),
2494 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002495 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002496 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002497 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002498
2499 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002500 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002501 PSA_ASSERT( psa_cipher_set_iv( &operation,
2502 iv, sizeof( iv ) ) );
2503 PSA_ASSERT( psa_cipher_finish( &operation,
2504 buffer, sizeof( buffer ), &length ) );
2505 TEST_EQUAL( psa_cipher_update( &operation,
2506 text, sizeof( text ),
2507 buffer, sizeof( buffer ),
2508 &length ),
2509 PSA_ERROR_BAD_STATE );
2510 PSA_ASSERT( psa_cipher_abort( &operation ) );
2511
2512 /* Call finish without calling setup beforehand. */
2513 TEST_EQUAL( psa_cipher_finish( &operation,
2514 buffer, sizeof( buffer ), &length ),
2515 PSA_ERROR_BAD_STATE );
2516 PSA_ASSERT( psa_cipher_abort( &operation ) );
2517
2518 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002519 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002520 /* Not calling update means we are encrypting an empty buffer, which is OK
2521 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002522 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002523 TEST_EQUAL( psa_cipher_finish( &operation,
2524 buffer, sizeof( buffer ), &length ),
2525 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002526 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002527 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002528 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002529
2530 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002531 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002532 PSA_ASSERT( psa_cipher_set_iv( &operation,
2533 iv, sizeof( iv ) ) );
2534 PSA_ASSERT( psa_cipher_finish( &operation,
2535 buffer, sizeof( buffer ), &length ) );
2536 TEST_EQUAL( psa_cipher_finish( &operation,
2537 buffer, sizeof( buffer ), &length ),
2538 PSA_ERROR_BAD_STATE );
2539 PSA_ASSERT( psa_cipher_abort( &operation ) );
2540
Ronald Cron5425a212020-08-04 14:58:35 +02002541 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002542
Jaeden Ameroab439972019-02-15 14:12:05 +00002543exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002544 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002545 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002546}
2547/* END_CASE */
2548
2549/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002550void cipher_encrypt_fail( int alg_arg,
2551 int key_type_arg,
2552 data_t *key_data,
2553 data_t *input,
2554 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002555{
Ronald Cron5425a212020-08-04 14:58:35 +02002556 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002557 psa_status_t status;
2558 psa_key_type_t key_type = key_type_arg;
2559 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002560 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002561 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002562 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002563 size_t output_length = 0;
2564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2565
2566 if ( PSA_ERROR_BAD_STATE != expected_status )
2567 {
2568 PSA_ASSERT( psa_crypto_init( ) );
2569
2570 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2571 psa_set_key_algorithm( &attributes, alg );
2572 psa_set_key_type( &attributes, key_type );
2573
2574 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2575 input->len );
2576 ASSERT_ALLOC( output, output_buffer_size );
2577
2578 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2579 &key ) );
2580 }
2581
2582 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2583 output_buffer_size, &output_length );
2584
2585 TEST_EQUAL( status, expected_status );
2586
2587exit:
2588 mbedtls_free( output );
2589 psa_destroy_key( key );
2590 PSA_DONE( );
2591}
2592/* END_CASE */
2593
2594/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002595void cipher_encrypt_alg_without_iv( int alg_arg,
2596 int key_type_arg,
2597 data_t *key_data,
2598 data_t *input,
2599 data_t *expected_output )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002600{
2601 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2602 psa_key_type_t key_type = key_type_arg;
2603 psa_algorithm_t alg = alg_arg;
Ronald Cron33c69682021-07-15 09:38:11 +02002604 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2605 uint8_t iv[1] = { 0x5a };
2606 size_t iv_length;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002607 unsigned char *output = NULL;
2608 size_t output_buffer_size = 0;
2609 size_t output_length = 0;
2610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2611
2612 PSA_ASSERT( psa_crypto_init( ) );
2613
Gilles Peskine5f504202022-04-20 16:55:03 +02002614 /* Validate size macros */
2615 TEST_ASSERT( expected_output->len <=
2616 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2617 TEST_ASSERT( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) <=
2618 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2619
2620 /* Set up key and output buffer */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002621 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2622 psa_set_key_algorithm( &attributes, alg );
2623 psa_set_key_type( &attributes, key_type );
Gilles Peskine5f504202022-04-20 16:55:03 +02002624 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2625 &key ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002626 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2627 ASSERT_ALLOC( output, output_buffer_size );
2628
Gilles Peskine5f504202022-04-20 16:55:03 +02002629 /* set_iv() is not allowed */
Ronald Cron33c69682021-07-15 09:38:11 +02002630 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2631 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2632 PSA_ERROR_BAD_STATE );
Gilles Peskine5f504202022-04-20 16:55:03 +02002633
2634 /* generate_iv() is not allowed */
Ronald Cron33c69682021-07-15 09:38:11 +02002635 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2636 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
2637 &iv_length ),
2638 PSA_ERROR_BAD_STATE );
2639
Gilles Peskine5f504202022-04-20 16:55:03 +02002640 /* One-shot encryption */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002641 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2642 output_buffer_size, &output_length ) );
2643 TEST_ASSERT( output_length <=
2644 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2645 TEST_ASSERT( output_length <=
2646 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2647
2648 ASSERT_COMPARE( expected_output->x, expected_output->len,
2649 output, output_length );
Gilles Peskine5f504202022-04-20 16:55:03 +02002650
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002651exit:
2652 mbedtls_free( output );
Gilles Peskine5f504202022-04-20 16:55:03 +02002653 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002654 psa_destroy_key( key );
2655 PSA_DONE( );
2656}
2657/* END_CASE */
2658
2659/* BEGIN_CASE */
Paul Elliotted33ef12021-07-14 12:31:21 +01002660void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2661{
2662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2663 psa_algorithm_t alg = alg_arg;
2664 psa_key_type_t key_type = key_type_arg;
2665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2666 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2667 psa_status_t status;
2668
2669 PSA_ASSERT( psa_crypto_init( ) );
2670
2671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2672 psa_set_key_algorithm( &attributes, alg );
2673 psa_set_key_type( &attributes, key_type );
2674
2675 /* Usage of either of these two size macros would cause divide by zero
2676 * with incorrect key types previously. Input length should be irrelevant
2677 * here. */
2678 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2679 0 );
2680 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2681
2682
2683 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2684 &key ) );
2685
2686 /* Should fail due to invalid alg type (to support invalid key type).
2687 * Encrypt or decrypt will end up in the same place. */
2688 status = psa_cipher_encrypt_setup( &operation, key, alg );
2689
2690 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2691
2692exit:
2693 psa_cipher_abort( &operation );
2694 psa_destroy_key( key );
2695 PSA_DONE( );
2696}
2697/* END_CASE */
2698
2699/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002700void cipher_encrypt_validation( int alg_arg,
2701 int key_type_arg,
2702 data_t *key_data,
2703 data_t *input )
2704{
2705 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2706 psa_key_type_t key_type = key_type_arg;
2707 psa_algorithm_t alg = alg_arg;
2708 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2709 unsigned char *output1 = NULL;
2710 size_t output1_buffer_size = 0;
2711 size_t output1_length = 0;
2712 unsigned char *output2 = NULL;
2713 size_t output2_buffer_size = 0;
2714 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002716 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718
Gilles Peskine8817f612018-12-18 00:18:46 +01002719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002721 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2722 psa_set_key_algorithm( &attributes, alg );
2723 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002724
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002725 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2726 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2727 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2728 ASSERT_ALLOC( output1, output1_buffer_size );
2729 ASSERT_ALLOC( output2, output2_buffer_size );
2730
Ronald Cron5425a212020-08-04 14:58:35 +02002731 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2732 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002733
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002734 /* The one-shot cipher encryption uses generated iv so validating
2735 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002736 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2737 output1_buffer_size, &output1_length ) );
2738 TEST_ASSERT( output1_length <=
2739 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2740 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002741 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002742
2743 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2744 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745
Gilles Peskine8817f612018-12-18 00:18:46 +01002746 PSA_ASSERT( psa_cipher_update( &operation,
2747 input->x, input->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002748 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002749 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002750 TEST_ASSERT( function_output_length <=
2751 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2752 TEST_ASSERT( function_output_length <=
2753 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002754 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002755
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002756 PSA_ASSERT( psa_cipher_finish( &operation,
2757 output2 + output2_length,
2758 output2_buffer_size - output2_length,
2759 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002760 TEST_ASSERT( function_output_length <=
2761 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2762 TEST_ASSERT( function_output_length <=
2763 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002764 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002765
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002766 PSA_ASSERT( psa_cipher_abort( &operation ) );
2767 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2768 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002769
Gilles Peskine50e586b2018-06-08 14:28:46 +02002770exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002771 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002772 mbedtls_free( output1 );
2773 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002774 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002775 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002776}
2777/* END_CASE */
2778
2779/* BEGIN_CASE */
2780void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002781 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002782 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002783 int first_part_size_arg,
2784 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002785 data_t *expected_output,
2786 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002787{
Ronald Cron5425a212020-08-04 14:58:35 +02002788 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002789 psa_key_type_t key_type = key_type_arg;
2790 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002791 psa_status_t status;
2792 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002793 size_t first_part_size = first_part_size_arg;
2794 size_t output1_length = output1_length_arg;
2795 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002796 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002797 size_t output_buffer_size = 0;
2798 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002799 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002800 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002801 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002802
Gilles Peskine8817f612018-12-18 00:18:46 +01002803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002804
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002805 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2806 psa_set_key_algorithm( &attributes, alg );
2807 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002808
Ronald Cron5425a212020-08-04 14:58:35 +02002809 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2810 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002811
Ronald Cron5425a212020-08-04 14:58:35 +02002812 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002813
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002814 if( iv->len > 0 )
2815 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002816 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002817 }
2818
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002819 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2820 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002821 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822
Gilles Peskinee0866522019-02-19 19:44:00 +01002823 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2825 output, output_buffer_size,
2826 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002827 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002828 TEST_ASSERT( function_output_length <=
2829 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2830 TEST_ASSERT( function_output_length <=
2831 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002832 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002833
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002834 if( first_part_size < input->len )
2835 {
2836 PSA_ASSERT( psa_cipher_update( &operation,
2837 input->x + first_part_size,
2838 input->len - first_part_size,
2839 ( output_buffer_size == 0 ? NULL :
2840 output + total_output_length ),
2841 output_buffer_size - total_output_length,
2842 &function_output_length ) );
2843 TEST_ASSERT( function_output_length == output2_length );
2844 TEST_ASSERT( function_output_length <=
2845 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2846 alg,
2847 input->len - first_part_size ) );
2848 TEST_ASSERT( function_output_length <=
2849 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2850 total_output_length += function_output_length;
2851 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002852
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002853 status = psa_cipher_finish( &operation,
2854 ( output_buffer_size == 0 ? NULL :
2855 output + total_output_length ),
2856 output_buffer_size - total_output_length,
2857 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002858 TEST_ASSERT( function_output_length <=
2859 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2860 TEST_ASSERT( function_output_length <=
2861 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002862 total_output_length += function_output_length;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002863 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002864
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002865 if( expected_status == PSA_SUCCESS )
2866 {
2867 PSA_ASSERT( psa_cipher_abort( &operation ) );
2868
2869 ASSERT_COMPARE( expected_output->x, expected_output->len,
2870 output, total_output_length );
2871 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002872
2873exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002874 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002875 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002876 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002877 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002878}
2879/* END_CASE */
2880
2881/* BEGIN_CASE */
2882void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002883 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002884 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002885 int first_part_size_arg,
2886 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002887 data_t *expected_output,
2888 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889{
Ronald Cron5425a212020-08-04 14:58:35 +02002890 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002891 psa_key_type_t key_type = key_type_arg;
2892 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002893 psa_status_t status;
2894 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002895 size_t first_part_size = first_part_size_arg;
2896 size_t output1_length = output1_length_arg;
2897 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002898 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002899 size_t output_buffer_size = 0;
2900 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002901 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002902 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002904
Gilles Peskine8817f612018-12-18 00:18:46 +01002905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002906
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2908 psa_set_key_algorithm( &attributes, alg );
2909 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002910
Ronald Cron5425a212020-08-04 14:58:35 +02002911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2912 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Ronald Cron5425a212020-08-04 14:58:35 +02002914 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002915
Steven Cooreman177deba2020-09-07 17:14:14 +02002916 if( iv->len > 0 )
2917 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002918 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002919 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002921 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2922 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002923 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924
Gilles Peskinee0866522019-02-19 19:44:00 +01002925 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002926 PSA_ASSERT( psa_cipher_update( &operation,
2927 input->x, first_part_size,
2928 output, output_buffer_size,
2929 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002930 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002931 TEST_ASSERT( function_output_length <=
2932 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2933 TEST_ASSERT( function_output_length <=
2934 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002935 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002936
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002937 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002938 {
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002939 PSA_ASSERT( psa_cipher_update( &operation,
2940 input->x + first_part_size,
2941 input->len - first_part_size,
2942 ( output_buffer_size == 0 ? NULL :
2943 output + total_output_length ),
2944 output_buffer_size - total_output_length,
2945 &function_output_length ) );
2946 TEST_ASSERT( function_output_length == output2_length );
2947 TEST_ASSERT( function_output_length <=
2948 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2949 alg,
2950 input->len - first_part_size ) );
2951 TEST_ASSERT( function_output_length <=
2952 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2953 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002954 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002955
Gilles Peskine50e586b2018-06-08 14:28:46 +02002956 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002957 ( output_buffer_size == 0 ? NULL :
2958 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002959 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002960 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002961 TEST_ASSERT( function_output_length <=
2962 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2963 TEST_ASSERT( function_output_length <=
2964 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002965 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002966 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967
2968 if( expected_status == PSA_SUCCESS )
2969 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002970 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002971
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002972 ASSERT_COMPARE( expected_output->x, expected_output->len,
2973 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974 }
2975
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002977 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002978 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002979 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002980 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002981}
2982/* END_CASE */
2983
Gilles Peskine50e586b2018-06-08 14:28:46 +02002984/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002985void cipher_decrypt_fail( int alg_arg,
2986 int key_type_arg,
2987 data_t *key_data,
2988 data_t *iv,
2989 data_t *input_arg,
2990 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002991{
2992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2993 psa_status_t status;
2994 psa_key_type_t key_type = key_type_arg;
2995 psa_algorithm_t alg = alg_arg;
2996 psa_status_t expected_status = expected_status_arg;
2997 unsigned char *input = NULL;
2998 size_t input_buffer_size = 0;
2999 unsigned char *output = NULL;
3000 size_t output_buffer_size = 0;
3001 size_t output_length = 0;
3002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3003
3004 if ( PSA_ERROR_BAD_STATE != expected_status )
3005 {
3006 PSA_ASSERT( psa_crypto_init( ) );
3007
3008 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3009 psa_set_key_algorithm( &attributes, alg );
3010 psa_set_key_type( &attributes, key_type );
3011
3012 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3013 &key ) );
3014 }
3015
3016 /* Allocate input buffer and copy the iv and the plaintext */
3017 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3018 if ( input_buffer_size > 0 )
3019 {
3020 ASSERT_ALLOC( input, input_buffer_size );
3021 memcpy( input, iv->x, iv->len );
3022 memcpy( input + iv->len, input_arg->x, input_arg->len );
3023 }
3024
3025 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3026 ASSERT_ALLOC( output, output_buffer_size );
3027
3028 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3029 output_buffer_size, &output_length );
3030 TEST_EQUAL( status, expected_status );
3031
3032exit:
3033 mbedtls_free( input );
3034 mbedtls_free( output );
3035 psa_destroy_key( key );
3036 PSA_DONE( );
3037}
3038/* END_CASE */
3039
3040/* BEGIN_CASE */
3041void cipher_decrypt( int alg_arg,
3042 int key_type_arg,
3043 data_t *key_data,
3044 data_t *iv,
3045 data_t *input_arg,
3046 data_t *expected_output )
3047{
3048 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3049 psa_key_type_t key_type = key_type_arg;
3050 psa_algorithm_t alg = alg_arg;
3051 unsigned char *input = NULL;
3052 size_t input_buffer_size = 0;
3053 unsigned char *output = NULL;
3054 size_t output_buffer_size = 0;
3055 size_t output_length = 0;
3056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3057
3058 PSA_ASSERT( psa_crypto_init( ) );
3059
3060 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3061 psa_set_key_algorithm( &attributes, alg );
3062 psa_set_key_type( &attributes, key_type );
3063
3064 /* Allocate input buffer and copy the iv and the plaintext */
3065 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3066 if ( input_buffer_size > 0 )
3067 {
3068 ASSERT_ALLOC( input, input_buffer_size );
3069 memcpy( input, iv->x, iv->len );
3070 memcpy( input + iv->len, input_arg->x, input_arg->len );
3071 }
3072
3073 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3074 ASSERT_ALLOC( output, output_buffer_size );
3075
3076 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3077 &key ) );
3078
3079 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3080 output_buffer_size, &output_length ) );
3081 TEST_ASSERT( output_length <=
3082 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3083 TEST_ASSERT( output_length <=
3084 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3085
3086 ASSERT_COMPARE( expected_output->x, expected_output->len,
3087 output, output_length );
3088exit:
3089 mbedtls_free( input );
3090 mbedtls_free( output );
3091 psa_destroy_key( key );
3092 PSA_DONE( );
3093}
3094/* END_CASE */
3095
3096/* BEGIN_CASE */
3097void cipher_verify_output( int alg_arg,
3098 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003099 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003100 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003101{
Ronald Cron5425a212020-08-04 14:58:35 +02003102 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003103 psa_key_type_t key_type = key_type_arg;
3104 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003105 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003106 size_t output1_size = 0;
3107 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003108 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003109 size_t output2_size = 0;
3110 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003112
Gilles Peskine8817f612018-12-18 00:18:46 +01003113 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003114
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003115 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3116 psa_set_key_algorithm( &attributes, alg );
3117 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003118
Ronald Cron5425a212020-08-04 14:58:35 +02003119 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3120 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003121 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003122 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003123
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003124 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3125 output1, output1_size,
3126 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003127 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003128 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003129 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003130 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003131
3132 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003133 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003134
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003135 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3136 output2, output2_size,
3137 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003138 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003139 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003140 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003141 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003142
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003143 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003144
3145exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003146 mbedtls_free( output1 );
3147 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003148 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003149 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003150}
3151/* END_CASE */
3152
3153/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003154void cipher_verify_output_multipart( int alg_arg,
3155 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003156 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003157 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003158 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003159{
Ronald Cron5425a212020-08-04 14:58:35 +02003160 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003161 psa_key_type_t key_type = key_type_arg;
3162 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003163 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003164 unsigned char iv[16] = {0};
3165 size_t iv_size = 16;
3166 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003167 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003168 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003169 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003170 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003171 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003172 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003173 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003174 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3175 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003177
Gilles Peskine8817f612018-12-18 00:18:46 +01003178 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003179
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3181 psa_set_key_algorithm( &attributes, alg );
3182 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003183
Ronald Cron5425a212020-08-04 14:58:35 +02003184 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3185 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003186
Ronald Cron5425a212020-08-04 14:58:35 +02003187 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3188 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003189
Steven Cooreman177deba2020-09-07 17:14:14 +02003190 if( alg != PSA_ALG_ECB_NO_PADDING )
3191 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003192 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3193 iv, iv_size,
3194 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003195 }
3196
gabor-mezei-armceface22021-01-21 12:26:17 +01003197 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3198 TEST_ASSERT( output1_buffer_size <=
3199 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003200 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003201
Gilles Peskinee0866522019-02-19 19:44:00 +01003202 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003203
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3205 output1, output1_buffer_size,
3206 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003207 TEST_ASSERT( function_output_length <=
3208 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3209 TEST_ASSERT( function_output_length <=
3210 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003211 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003212
Gilles Peskine8817f612018-12-18 00:18:46 +01003213 PSA_ASSERT( psa_cipher_update( &operation1,
3214 input->x + first_part_size,
3215 input->len - first_part_size,
3216 output1, output1_buffer_size,
3217 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003218 TEST_ASSERT( function_output_length <=
3219 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3220 alg,
3221 input->len - first_part_size ) );
3222 TEST_ASSERT( function_output_length <=
3223 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003224 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003225
Gilles Peskine8817f612018-12-18 00:18:46 +01003226 PSA_ASSERT( psa_cipher_finish( &operation1,
3227 output1 + output1_length,
3228 output1_buffer_size - output1_length,
3229 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003230 TEST_ASSERT( function_output_length <=
3231 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3232 TEST_ASSERT( function_output_length <=
3233 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003234 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003235
Gilles Peskine8817f612018-12-18 00:18:46 +01003236 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003237
Gilles Peskine048b7f02018-06-08 14:20:49 +02003238 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003239 TEST_ASSERT( output2_buffer_size <=
3240 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3241 TEST_ASSERT( output2_buffer_size <=
3242 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003243 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003244
Steven Cooreman177deba2020-09-07 17:14:14 +02003245 if( iv_length > 0 )
3246 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003247 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3248 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003249 }
Moran Pekerded84402018-06-06 16:36:50 +03003250
Gilles Peskine8817f612018-12-18 00:18:46 +01003251 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3252 output2, output2_buffer_size,
3253 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003254 TEST_ASSERT( function_output_length <=
3255 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3256 TEST_ASSERT( function_output_length <=
3257 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003258 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003259
Gilles Peskine8817f612018-12-18 00:18:46 +01003260 PSA_ASSERT( psa_cipher_update( &operation2,
3261 output1 + first_part_size,
3262 output1_length - first_part_size,
3263 output2, output2_buffer_size,
3264 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003265 TEST_ASSERT( function_output_length <=
3266 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3267 alg,
3268 output1_length - first_part_size ) );
3269 TEST_ASSERT( function_output_length <=
3270 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003271 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003272
Gilles Peskine8817f612018-12-18 00:18:46 +01003273 PSA_ASSERT( psa_cipher_finish( &operation2,
3274 output2 + output2_length,
3275 output2_buffer_size - output2_length,
3276 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003277 TEST_ASSERT( function_output_length <=
3278 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3279 TEST_ASSERT( function_output_length <=
3280 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003281 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003282
Gilles Peskine8817f612018-12-18 00:18:46 +01003283 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003284
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003285 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003286
3287exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003288 psa_cipher_abort( &operation1 );
3289 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003290 mbedtls_free( output1 );
3291 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003292 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003293 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003294}
3295/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003296
Gilles Peskine20035e32018-02-03 22:44:14 +01003297/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003298void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003299 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003300 data_t *nonce,
3301 data_t *additional_data,
3302 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003303 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003304{
Ronald Cron5425a212020-08-04 14:58:35 +02003305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003306 psa_key_type_t key_type = key_type_arg;
3307 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003308 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003309 unsigned char *output_data = NULL;
3310 size_t output_size = 0;
3311 size_t output_length = 0;
3312 unsigned char *output_data2 = NULL;
3313 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003314 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003315 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317
Gilles Peskine8817f612018-12-18 00:18:46 +01003318 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003319
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003320 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3321 psa_set_key_algorithm( &attributes, alg );
3322 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003323
Gilles Peskine049c7532019-05-15 20:22:09 +02003324 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003325 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003326 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3327 key_bits = psa_get_key_bits( &attributes );
3328
3329 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3330 alg );
3331 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3332 * should be exact. */
3333 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3334 expected_result != PSA_ERROR_NOT_SUPPORTED )
3335 {
3336 TEST_EQUAL( output_size,
3337 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3338 TEST_ASSERT( output_size <=
3339 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3340 }
3341 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342
Steven Cooremanf49478b2021-02-15 15:19:25 +01003343 status = psa_aead_encrypt( key, alg,
3344 nonce->x, nonce->len,
3345 additional_data->x,
3346 additional_data->len,
3347 input_data->x, input_data->len,
3348 output_data, output_size,
3349 &output_length );
3350
3351 /* If the operation is not supported, just skip and not fail in case the
3352 * encryption involves a common limitation of cryptography hardwares and
3353 * an alternative implementation. */
3354 if( status == PSA_ERROR_NOT_SUPPORTED )
3355 {
3356 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3357 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3358 }
3359
3360 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003361
3362 if( PSA_SUCCESS == expected_result )
3363 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003364 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365
Gilles Peskine003a4a92019-05-14 16:09:40 +02003366 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3367 * should be exact. */
3368 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003369 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003370
gabor-mezei-armceface22021-01-21 12:26:17 +01003371 TEST_ASSERT( input_data->len <=
3372 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3373
Ronald Cron5425a212020-08-04 14:58:35 +02003374 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003375 nonce->x, nonce->len,
3376 additional_data->x,
3377 additional_data->len,
3378 output_data, output_length,
3379 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003380 &output_length2 ),
3381 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003382
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003383 ASSERT_COMPARE( input_data->x, input_data->len,
3384 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003386
Gilles Peskinea1cac842018-06-11 19:33:02 +02003387exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003388 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003389 mbedtls_free( output_data );
3390 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003391 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003392}
3393/* END_CASE */
3394
3395/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003396void aead_encrypt( int key_type_arg, data_t *key_data,
3397 int alg_arg,
3398 data_t *nonce,
3399 data_t *additional_data,
3400 data_t *input_data,
3401 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402{
Ronald Cron5425a212020-08-04 14:58:35 +02003403 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404 psa_key_type_t key_type = key_type_arg;
3405 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003406 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003407 unsigned char *output_data = NULL;
3408 size_t output_size = 0;
3409 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003411 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003412
Gilles Peskine8817f612018-12-18 00:18:46 +01003413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3416 psa_set_key_algorithm( &attributes, alg );
3417 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
Gilles Peskine049c7532019-05-15 20:22:09 +02003419 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003420 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003421 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3422 key_bits = psa_get_key_bits( &attributes );
3423
3424 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3425 alg );
3426 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3427 * should be exact. */
3428 TEST_EQUAL( output_size,
3429 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3430 TEST_ASSERT( output_size <=
3431 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3432 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003433
Steven Cooremand588ea12021-01-11 19:36:04 +01003434 status = psa_aead_encrypt( key, alg,
3435 nonce->x, nonce->len,
3436 additional_data->x, additional_data->len,
3437 input_data->x, input_data->len,
3438 output_data, output_size,
3439 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003440
Ronald Cron28a45ed2021-02-09 20:35:42 +01003441 /* If the operation is not supported, just skip and not fail in case the
3442 * encryption involves a common limitation of cryptography hardwares and
3443 * an alternative implementation. */
3444 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003445 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003446 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3447 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003448 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003449
3450 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003451 ASSERT_COMPARE( expected_result->x, expected_result->len,
3452 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003453
Gilles Peskinea1cac842018-06-11 19:33:02 +02003454exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003455 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003457 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003458}
3459/* END_CASE */
3460
3461/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003462void aead_decrypt( int key_type_arg, data_t *key_data,
3463 int alg_arg,
3464 data_t *nonce,
3465 data_t *additional_data,
3466 data_t *input_data,
3467 data_t *expected_data,
3468 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003469{
Ronald Cron5425a212020-08-04 14:58:35 +02003470 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003471 psa_key_type_t key_type = key_type_arg;
3472 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003473 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474 unsigned char *output_data = NULL;
3475 size_t output_size = 0;
3476 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003478 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003479 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003480
Gilles Peskine8817f612018-12-18 00:18:46 +01003481 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003482
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003483 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3484 psa_set_key_algorithm( &attributes, alg );
3485 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003486
Gilles Peskine049c7532019-05-15 20:22:09 +02003487 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003488 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003489 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3490 key_bits = psa_get_key_bits( &attributes );
3491
3492 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3493 alg );
3494 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3495 expected_result != PSA_ERROR_NOT_SUPPORTED )
3496 {
3497 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3498 * should be exact. */
3499 TEST_EQUAL( output_size,
3500 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3501 TEST_ASSERT( output_size <=
3502 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3503 }
3504 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003505
Steven Cooremand588ea12021-01-11 19:36:04 +01003506 status = psa_aead_decrypt( key, alg,
3507 nonce->x, nonce->len,
3508 additional_data->x,
3509 additional_data->len,
3510 input_data->x, input_data->len,
3511 output_data, output_size,
3512 &output_length );
3513
Ronald Cron28a45ed2021-02-09 20:35:42 +01003514 /* If the operation is not supported, just skip and not fail in case the
3515 * decryption involves a common limitation of cryptography hardwares and
3516 * an alternative implementation. */
3517 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003518 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003519 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3520 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003521 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003522
3523 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003524
Gilles Peskine2d277862018-06-18 15:41:12 +02003525 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003526 ASSERT_COMPARE( expected_data->x, expected_data->len,
3527 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003528
Gilles Peskinea1cac842018-06-11 19:33:02 +02003529exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003530 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003531 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003532 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003533}
3534/* END_CASE */
3535
3536/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003537void signature_size( int type_arg,
3538 int bits,
3539 int alg_arg,
3540 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003541{
3542 psa_key_type_t type = type_arg;
3543 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003544 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003545
Gilles Peskinefe11b722018-12-18 00:24:04 +01003546 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003547#if defined(MBEDTLS_TEST_DEPRECATED)
3548 TEST_EQUAL( actual_size,
3549 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3550#endif /* MBEDTLS_TEST_DEPRECATED */
3551
Gilles Peskinee59236f2018-01-27 23:32:46 +01003552exit:
3553 ;
3554}
3555/* END_CASE */
3556
3557/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003558void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3559 int alg_arg, data_t *input_data,
3560 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003561{
Ronald Cron5425a212020-08-04 14:58:35 +02003562 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003563 psa_key_type_t key_type = key_type_arg;
3564 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003565 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003566 unsigned char *signature = NULL;
3567 size_t signature_size;
3568 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003570
Gilles Peskine8817f612018-12-18 00:18:46 +01003571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003572
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003574 psa_set_key_algorithm( &attributes, alg );
3575 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003576
Gilles Peskine049c7532019-05-15 20:22:09 +02003577 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003578 &key ) );
3579 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003580 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003581
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003582 /* Allocate a buffer which has the size advertized by the
3583 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003584 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003585 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003586 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003587 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003588 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003589
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003590 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003591 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003592 input_data->x, input_data->len,
3593 signature, signature_size,
3594 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003595 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003596 ASSERT_COMPARE( output_data->x, output_data->len,
3597 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003598
Gilles Peskine0627f982019-11-26 19:12:16 +01003599#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003600 memset( signature, 0, signature_size );
3601 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003602 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003603 input_data->x, input_data->len,
3604 signature, signature_size,
3605 &signature_length ) );
3606 ASSERT_COMPARE( output_data->x, output_data->len,
3607 signature, signature_length );
3608#endif /* MBEDTLS_TEST_DEPRECATED */
3609
Gilles Peskine20035e32018-02-03 22:44:14 +01003610exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003611 /*
3612 * Key attributes may have been returned by psa_get_key_attributes()
3613 * thus reset them as required.
3614 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003615 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003616
Ronald Cron5425a212020-08-04 14:58:35 +02003617 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003618 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003619 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003620}
3621/* END_CASE */
3622
3623/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003624void sign_hash_fail( int key_type_arg, data_t *key_data,
3625 int alg_arg, data_t *input_data,
3626 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003627{
Ronald Cron5425a212020-08-04 14:58:35 +02003628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003629 psa_key_type_t key_type = key_type_arg;
3630 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003631 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003632 psa_status_t actual_status;
3633 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003634 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003635 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003636 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003637
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003638 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003639
Gilles Peskine8817f612018-12-18 00:18:46 +01003640 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003641
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003642 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003643 psa_set_key_algorithm( &attributes, alg );
3644 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003645
Gilles Peskine049c7532019-05-15 20:22:09 +02003646 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003647 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003648
Ronald Cron5425a212020-08-04 14:58:35 +02003649 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003650 input_data->x, input_data->len,
3651 signature, signature_size,
3652 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003653 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003654 /* The value of *signature_length is unspecified on error, but
3655 * whatever it is, it should be less than signature_size, so that
3656 * if the caller tries to read *signature_length bytes without
3657 * checking the error code then they don't overflow a buffer. */
3658 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003659
Gilles Peskine895242b2019-11-29 12:15:40 +01003660#if defined(MBEDTLS_TEST_DEPRECATED)
3661 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003662 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003663 input_data->x, input_data->len,
3664 signature, signature_size,
3665 &signature_length ),
3666 expected_status );
3667 TEST_ASSERT( signature_length <= signature_size );
3668#endif /* MBEDTLS_TEST_DEPRECATED */
3669
Gilles Peskine20035e32018-02-03 22:44:14 +01003670exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003671 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003672 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003673 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003674 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003675}
3676/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003677
3678/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003679void sign_verify_hash( int key_type_arg, data_t *key_data,
3680 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003681{
Ronald Cron5425a212020-08-04 14:58:35 +02003682 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003683 psa_key_type_t key_type = key_type_arg;
3684 psa_algorithm_t alg = alg_arg;
3685 size_t key_bits;
3686 unsigned char *signature = NULL;
3687 size_t signature_size;
3688 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003689 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003690
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003692
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003693 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003694 psa_set_key_algorithm( &attributes, alg );
3695 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003696
Gilles Peskine049c7532019-05-15 20:22:09 +02003697 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003698 &key ) );
3699 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003700 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003701
3702 /* Allocate a buffer which has the size advertized by the
3703 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003704 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003705 key_bits, alg );
3706 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003707 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003708 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003709
3710 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003711 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003712 input_data->x, input_data->len,
3713 signature, signature_size,
3714 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003715 /* Check that the signature length looks sensible. */
3716 TEST_ASSERT( signature_length <= signature_size );
3717 TEST_ASSERT( signature_length > 0 );
3718
3719 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003720 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003721 input_data->x, input_data->len,
3722 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003723
3724 if( input_data->len != 0 )
3725 {
3726 /* Flip a bit in the input and verify that the signature is now
3727 * detected as invalid. Flip a bit at the beginning, not at the end,
3728 * because ECDSA may ignore the last few bits of the input. */
3729 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003730 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003731 input_data->x, input_data->len,
3732 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003733 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003734 }
3735
3736exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003737 /*
3738 * Key attributes may have been returned by psa_get_key_attributes()
3739 * thus reset them as required.
3740 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003741 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003742
Ronald Cron5425a212020-08-04 14:58:35 +02003743 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003744 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003745 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003746}
3747/* END_CASE */
3748
3749/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003750void verify_hash( int key_type_arg, data_t *key_data,
3751 int alg_arg, data_t *hash_data,
3752 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003753{
Ronald Cron5425a212020-08-04 14:58:35 +02003754 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003755 psa_key_type_t key_type = key_type_arg;
3756 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003758
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003759 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003760
Gilles Peskine8817f612018-12-18 00:18:46 +01003761 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003762
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003763 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003764 psa_set_key_algorithm( &attributes, alg );
3765 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003766
Gilles Peskine049c7532019-05-15 20:22:09 +02003767 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003768 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003769
Ronald Cron5425a212020-08-04 14:58:35 +02003770 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003771 hash_data->x, hash_data->len,
3772 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003773
3774#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003775 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003776 hash_data->x, hash_data->len,
3777 signature_data->x,
3778 signature_data->len ) );
3779
3780#endif /* MBEDTLS_TEST_DEPRECATED */
3781
itayzafrir5c753392018-05-08 11:18:38 +03003782exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003783 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003784 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003785 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003786}
3787/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003788
3789/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003790void verify_hash_fail( int key_type_arg, data_t *key_data,
3791 int alg_arg, data_t *hash_data,
3792 data_t *signature_data,
3793 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003794{
Ronald Cron5425a212020-08-04 14:58:35 +02003795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003796 psa_key_type_t key_type = key_type_arg;
3797 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003798 psa_status_t actual_status;
3799 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003801
Gilles Peskine8817f612018-12-18 00:18:46 +01003802 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003803
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003804 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003805 psa_set_key_algorithm( &attributes, alg );
3806 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003807
Gilles Peskine049c7532019-05-15 20:22:09 +02003808 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003809 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003810
Ronald Cron5425a212020-08-04 14:58:35 +02003811 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003812 hash_data->x, hash_data->len,
3813 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003814 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003815
Gilles Peskine895242b2019-11-29 12:15:40 +01003816#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003817 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003818 hash_data->x, hash_data->len,
3819 signature_data->x, signature_data->len ),
3820 expected_status );
3821#endif /* MBEDTLS_TEST_DEPRECATED */
3822
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003823exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003824 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003825 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003826 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003827}
3828/* END_CASE */
3829
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003830/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003831void sign_message_deterministic( int key_type_arg,
3832 data_t *key_data,
3833 int alg_arg,
3834 data_t *input_data,
3835 data_t *output_data )
3836{
3837 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3838 psa_key_type_t key_type = key_type_arg;
3839 psa_algorithm_t alg = alg_arg;
3840 size_t key_bits;
3841 unsigned char *signature = NULL;
3842 size_t signature_size;
3843 size_t signature_length = 0xdeadbeef;
3844 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3845
3846 PSA_ASSERT( psa_crypto_init( ) );
3847
3848 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3849 psa_set_key_algorithm( &attributes, alg );
3850 psa_set_key_type( &attributes, key_type );
3851
3852 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3853 &key ) );
3854 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3855 key_bits = psa_get_key_bits( &attributes );
3856
3857 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3858 TEST_ASSERT( signature_size != 0 );
3859 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3860 ASSERT_ALLOC( signature, signature_size );
3861
3862 PSA_ASSERT( psa_sign_message( key, alg,
3863 input_data->x, input_data->len,
3864 signature, signature_size,
3865 &signature_length ) );
3866
3867 ASSERT_COMPARE( output_data->x, output_data->len,
3868 signature, signature_length );
3869
3870exit:
3871 psa_reset_key_attributes( &attributes );
3872
3873 psa_destroy_key( key );
3874 mbedtls_free( signature );
3875 PSA_DONE( );
3876
3877}
3878/* END_CASE */
3879
3880/* BEGIN_CASE */
3881void sign_message_fail( int key_type_arg,
3882 data_t *key_data,
3883 int alg_arg,
3884 data_t *input_data,
3885 int signature_size_arg,
3886 int expected_status_arg )
3887{
3888 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3889 psa_key_type_t key_type = key_type_arg;
3890 psa_algorithm_t alg = alg_arg;
3891 size_t signature_size = signature_size_arg;
3892 psa_status_t actual_status;
3893 psa_status_t expected_status = expected_status_arg;
3894 unsigned char *signature = NULL;
3895 size_t signature_length = 0xdeadbeef;
3896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3897
3898 ASSERT_ALLOC( signature, signature_size );
3899
3900 PSA_ASSERT( psa_crypto_init( ) );
3901
3902 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3903 psa_set_key_algorithm( &attributes, alg );
3904 psa_set_key_type( &attributes, key_type );
3905
3906 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3907 &key ) );
3908
3909 actual_status = psa_sign_message( key, alg,
3910 input_data->x, input_data->len,
3911 signature, signature_size,
3912 &signature_length );
3913 TEST_EQUAL( actual_status, expected_status );
3914 /* The value of *signature_length is unspecified on error, but
3915 * whatever it is, it should be less than signature_size, so that
3916 * if the caller tries to read *signature_length bytes without
3917 * checking the error code then they don't overflow a buffer. */
3918 TEST_ASSERT( signature_length <= signature_size );
3919
3920exit:
3921 psa_reset_key_attributes( &attributes );
3922 psa_destroy_key( key );
3923 mbedtls_free( signature );
3924 PSA_DONE( );
3925}
3926/* END_CASE */
3927
3928/* BEGIN_CASE */
3929void sign_verify_message( int key_type_arg,
3930 data_t *key_data,
3931 int alg_arg,
3932 data_t *input_data )
3933{
3934 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3935 psa_key_type_t key_type = key_type_arg;
3936 psa_algorithm_t alg = alg_arg;
3937 size_t key_bits;
3938 unsigned char *signature = NULL;
3939 size_t signature_size;
3940 size_t signature_length = 0xdeadbeef;
3941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3942
3943 PSA_ASSERT( psa_crypto_init( ) );
3944
3945 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3946 PSA_KEY_USAGE_VERIFY_MESSAGE );
3947 psa_set_key_algorithm( &attributes, alg );
3948 psa_set_key_type( &attributes, key_type );
3949
3950 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3951 &key ) );
3952 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3953 key_bits = psa_get_key_bits( &attributes );
3954
3955 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3956 TEST_ASSERT( signature_size != 0 );
3957 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3958 ASSERT_ALLOC( signature, signature_size );
3959
3960 PSA_ASSERT( psa_sign_message( key, alg,
3961 input_data->x, input_data->len,
3962 signature, signature_size,
3963 &signature_length ) );
3964 TEST_ASSERT( signature_length <= signature_size );
3965 TEST_ASSERT( signature_length > 0 );
3966
3967 PSA_ASSERT( psa_verify_message( key, alg,
3968 input_data->x, input_data->len,
3969 signature, signature_length ) );
3970
3971 if( input_data->len != 0 )
3972 {
3973 /* Flip a bit in the input and verify that the signature is now
3974 * detected as invalid. Flip a bit at the beginning, not at the end,
3975 * because ECDSA may ignore the last few bits of the input. */
3976 input_data->x[0] ^= 1;
3977 TEST_EQUAL( psa_verify_message( key, alg,
3978 input_data->x, input_data->len,
3979 signature, signature_length ),
3980 PSA_ERROR_INVALID_SIGNATURE );
3981 }
3982
3983exit:
3984 psa_reset_key_attributes( &attributes );
3985
3986 psa_destroy_key( key );
3987 mbedtls_free( signature );
3988 PSA_DONE( );
3989}
3990/* END_CASE */
3991
3992/* BEGIN_CASE */
3993void verify_message( int key_type_arg,
3994 data_t *key_data,
3995 int alg_arg,
3996 data_t *input_data,
3997 data_t *signature_data )
3998{
3999 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4000 psa_key_type_t key_type = key_type_arg;
4001 psa_algorithm_t alg = alg_arg;
4002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4003
4004 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
4005
4006 PSA_ASSERT( psa_crypto_init( ) );
4007
4008 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4009 psa_set_key_algorithm( &attributes, alg );
4010 psa_set_key_type( &attributes, key_type );
4011
4012 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4013 &key ) );
4014
4015 PSA_ASSERT( psa_verify_message( key, alg,
4016 input_data->x, input_data->len,
4017 signature_data->x, signature_data->len ) );
4018
4019exit:
4020 psa_reset_key_attributes( &attributes );
4021 psa_destroy_key( key );
4022 PSA_DONE( );
4023}
4024/* END_CASE */
4025
4026/* BEGIN_CASE */
4027void verify_message_fail( int key_type_arg,
4028 data_t *key_data,
4029 int alg_arg,
4030 data_t *hash_data,
4031 data_t *signature_data,
4032 int expected_status_arg )
4033{
4034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4035 psa_key_type_t key_type = key_type_arg;
4036 psa_algorithm_t alg = alg_arg;
4037 psa_status_t actual_status;
4038 psa_status_t expected_status = expected_status_arg;
4039 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4040
4041 PSA_ASSERT( psa_crypto_init( ) );
4042
4043 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4044 psa_set_key_algorithm( &attributes, alg );
4045 psa_set_key_type( &attributes, key_type );
4046
4047 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4048 &key ) );
4049
4050 actual_status = psa_verify_message( key, alg,
4051 hash_data->x, hash_data->len,
4052 signature_data->x,
4053 signature_data->len );
4054 TEST_EQUAL( actual_status, expected_status );
4055
4056exit:
4057 psa_reset_key_attributes( &attributes );
4058 psa_destroy_key( key );
4059 PSA_DONE( );
4060}
4061/* END_CASE */
4062
4063/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004064void asymmetric_encrypt( int key_type_arg,
4065 data_t *key_data,
4066 int alg_arg,
4067 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004068 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004069 int expected_output_length_arg,
4070 int expected_status_arg )
4071{
Ronald Cron5425a212020-08-04 14:58:35 +02004072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004073 psa_key_type_t key_type = key_type_arg;
4074 psa_algorithm_t alg = alg_arg;
4075 size_t expected_output_length = expected_output_length_arg;
4076 size_t key_bits;
4077 unsigned char *output = NULL;
4078 size_t output_size;
4079 size_t output_length = ~0;
4080 psa_status_t actual_status;
4081 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004083
Gilles Peskine8817f612018-12-18 00:18:46 +01004084 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004085
Gilles Peskine656896e2018-06-29 19:12:28 +02004086 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004087 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4088 psa_set_key_algorithm( &attributes, alg );
4089 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004090 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004091 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004092
4093 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004094 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004095 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004096
Gilles Peskine656896e2018-06-29 19:12:28 +02004097 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004098 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004099 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004100
4101 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004102 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004103 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004104 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004105 output, output_size,
4106 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004107 TEST_EQUAL( actual_status, expected_status );
4108 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004109
Gilles Peskine68428122018-06-30 18:42:41 +02004110 /* If the label is empty, the test framework puts a non-null pointer
4111 * in label->x. Test that a null pointer works as well. */
4112 if( label->len == 0 )
4113 {
4114 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004115 if( output_size != 0 )
4116 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004117 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004118 input_data->x, input_data->len,
4119 NULL, label->len,
4120 output, output_size,
4121 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004122 TEST_EQUAL( actual_status, expected_status );
4123 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004124 }
4125
Gilles Peskine656896e2018-06-29 19:12:28 +02004126exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004127 /*
4128 * Key attributes may have been returned by psa_get_key_attributes()
4129 * thus reset them as required.
4130 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004131 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004132
Ronald Cron5425a212020-08-04 14:58:35 +02004133 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004134 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004135 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004136}
4137/* END_CASE */
4138
4139/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004140void asymmetric_encrypt_decrypt( int key_type_arg,
4141 data_t *key_data,
4142 int alg_arg,
4143 data_t *input_data,
4144 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004145{
Ronald Cron5425a212020-08-04 14:58:35 +02004146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004147 psa_key_type_t key_type = key_type_arg;
4148 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004149 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004150 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004151 size_t output_size;
4152 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004153 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004154 size_t output2_size;
4155 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157
Gilles Peskine8817f612018-12-18 00:18:46 +01004158 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004159
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004160 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4161 psa_set_key_algorithm( &attributes, alg );
4162 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004163
Gilles Peskine049c7532019-05-15 20:22:09 +02004164 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004165 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004166
4167 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004168 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004169 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004170
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004171 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004172 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004173 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004174
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004175 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004176 TEST_ASSERT( output2_size <=
4177 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4178 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004179 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004180
Gilles Peskineeebd7382018-06-08 18:11:54 +02004181 /* We test encryption by checking that encrypt-then-decrypt gives back
4182 * the original plaintext because of the non-optional random
4183 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004184 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004185 input_data->x, input_data->len,
4186 label->x, label->len,
4187 output, output_size,
4188 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004189 /* We don't know what ciphertext length to expect, but check that
4190 * it looks sensible. */
4191 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004192
Ronald Cron5425a212020-08-04 14:58:35 +02004193 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004194 output, output_length,
4195 label->x, label->len,
4196 output2, output2_size,
4197 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004198 ASSERT_COMPARE( input_data->x, input_data->len,
4199 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004200
4201exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004202 /*
4203 * Key attributes may have been returned by psa_get_key_attributes()
4204 * thus reset them as required.
4205 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004206 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004207
Ronald Cron5425a212020-08-04 14:58:35 +02004208 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004209 mbedtls_free( output );
4210 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004211 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004212}
4213/* END_CASE */
4214
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004216void asymmetric_decrypt( int key_type_arg,
4217 data_t *key_data,
4218 int alg_arg,
4219 data_t *input_data,
4220 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004221 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004222{
Ronald Cron5425a212020-08-04 14:58:35 +02004223 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004224 psa_key_type_t key_type = key_type_arg;
4225 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004226 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004227 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004228 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004229 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004231
Gilles Peskine8817f612018-12-18 00:18:46 +01004232 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004233
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4235 psa_set_key_algorithm( &attributes, alg );
4236 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004237
Gilles Peskine049c7532019-05-15 20:22:09 +02004238 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004239 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004240
gabor-mezei-armceface22021-01-21 12:26:17 +01004241 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4242 key_bits = psa_get_key_bits( &attributes );
4243
4244 /* Determine the maximum ciphertext length */
4245 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4246 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4247 ASSERT_ALLOC( output, output_size );
4248
Ronald Cron5425a212020-08-04 14:58:35 +02004249 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004250 input_data->x, input_data->len,
4251 label->x, label->len,
4252 output,
4253 output_size,
4254 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004255 ASSERT_COMPARE( expected_data->x, expected_data->len,
4256 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257
Gilles Peskine68428122018-06-30 18:42:41 +02004258 /* If the label is empty, the test framework puts a non-null pointer
4259 * in label->x. Test that a null pointer works as well. */
4260 if( label->len == 0 )
4261 {
4262 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004263 if( output_size != 0 )
4264 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004265 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004266 input_data->x, input_data->len,
4267 NULL, label->len,
4268 output,
4269 output_size,
4270 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004271 ASSERT_COMPARE( expected_data->x, expected_data->len,
4272 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004273 }
4274
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004275exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004276 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004277 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004278 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004279 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004280}
4281/* END_CASE */
4282
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004283/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004284void asymmetric_decrypt_fail( int key_type_arg,
4285 data_t *key_data,
4286 int alg_arg,
4287 data_t *input_data,
4288 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004289 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004290 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004291{
Ronald Cron5425a212020-08-04 14:58:35 +02004292 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004293 psa_key_type_t key_type = key_type_arg;
4294 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004295 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004296 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004297 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004298 psa_status_t actual_status;
4299 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004301
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004302 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004303
Gilles Peskine8817f612018-12-18 00:18:46 +01004304 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004305
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004306 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4307 psa_set_key_algorithm( &attributes, alg );
4308 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004309
Gilles Peskine049c7532019-05-15 20:22:09 +02004310 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004311 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004312
Ronald Cron5425a212020-08-04 14:58:35 +02004313 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004314 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004315 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004316 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004317 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004318 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004319 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320
Gilles Peskine68428122018-06-30 18:42:41 +02004321 /* If the label is empty, the test framework puts a non-null pointer
4322 * in label->x. Test that a null pointer works as well. */
4323 if( label->len == 0 )
4324 {
4325 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004326 if( output_size != 0 )
4327 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004328 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004329 input_data->x, input_data->len,
4330 NULL, label->len,
4331 output, output_size,
4332 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004333 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004334 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004335 }
4336
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004338 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004339 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004340 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004341 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004342}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004343/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004344
4345/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004346void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004347{
4348 /* Test each valid way of initializing the object, except for `= {0}`, as
4349 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4350 * though it's OK by the C standard. We could test for this, but we'd need
4351 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004352 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004353 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4354 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4355 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004356
4357 memset( &zero, 0, sizeof( zero ) );
4358
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004359 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004360 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004361 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004362 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004363 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004364 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004365 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004366
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004368 PSA_ASSERT( psa_key_derivation_abort(&func) );
4369 PSA_ASSERT( psa_key_derivation_abort(&init) );
4370 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004371}
4372/* END_CASE */
4373
Janos Follath16de4a42019-06-13 16:32:24 +01004374/* BEGIN_CASE */
4375void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004376{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004377 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004378 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004379 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004380
Gilles Peskine8817f612018-12-18 00:18:46 +01004381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004382
Janos Follath16de4a42019-06-13 16:32:24 +01004383 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004384 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004385
4386exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004387 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004388 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004389}
4390/* END_CASE */
4391
Janos Follathaf3c2a02019-06-12 12:34:34 +01004392/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004393void derive_set_capacity( int alg_arg, int capacity_arg,
4394 int expected_status_arg )
4395{
4396 psa_algorithm_t alg = alg_arg;
4397 size_t capacity = capacity_arg;
4398 psa_status_t expected_status = expected_status_arg;
4399 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4400
4401 PSA_ASSERT( psa_crypto_init( ) );
4402
4403 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4404
4405 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4406 expected_status );
4407
4408exit:
4409 psa_key_derivation_abort( &operation );
4410 PSA_DONE( );
4411}
4412/* END_CASE */
4413
4414/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004415void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004416 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004417 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004418 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004419 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004420 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004421 int expected_status_arg3,
4422 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004423{
4424 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004425 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4426 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004427 psa_status_t expected_statuses[] = {expected_status_arg1,
4428 expected_status_arg2,
4429 expected_status_arg3};
4430 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004431 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4432 MBEDTLS_SVC_KEY_ID_INIT,
4433 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004434 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4436 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004437 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004438 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004439 psa_status_t expected_output_status = expected_output_status_arg;
4440 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004441
4442 PSA_ASSERT( psa_crypto_init( ) );
4443
4444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4445 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004446
4447 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4448
4449 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4450 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004451 mbedtls_test_set_step( i );
4452 if( steps[i] == 0 )
4453 {
4454 /* Skip this step */
4455 }
4456 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004457 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004458 psa_set_key_type( &attributes, key_types[i] );
4459 PSA_ASSERT( psa_import_key( &attributes,
4460 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004461 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004462 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4463 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4464 {
4465 // When taking a private key as secret input, use key agreement
4466 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004467 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4468 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004469 expected_statuses[i] );
4470 }
4471 else
4472 {
4473 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004474 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004475 expected_statuses[i] );
4476 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004477 }
4478 else
4479 {
4480 TEST_EQUAL( psa_key_derivation_input_bytes(
4481 &operation, steps[i],
4482 inputs[i]->x, inputs[i]->len ),
4483 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004484 }
4485 }
4486
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004487 if( output_key_type != PSA_KEY_TYPE_NONE )
4488 {
4489 psa_reset_key_attributes( &attributes );
Dave Rodgmandc4e4b72021-11-16 12:12:49 +00004490 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004491 psa_set_key_bits( &attributes, 8 );
4492 actual_output_status =
4493 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004494 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004495 }
4496 else
4497 {
4498 uint8_t buffer[1];
4499 actual_output_status =
4500 psa_key_derivation_output_bytes( &operation,
4501 buffer, sizeof( buffer ) );
4502 }
4503 TEST_EQUAL( actual_output_status, expected_output_status );
4504
Janos Follathaf3c2a02019-06-12 12:34:34 +01004505exit:
4506 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004507 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4508 psa_destroy_key( keys[i] );
4509 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004510 PSA_DONE( );
4511}
4512/* END_CASE */
4513
Janos Follathd958bb72019-07-03 15:02:16 +01004514/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004515void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004516{
Janos Follathd958bb72019-07-03 15:02:16 +01004517 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004519 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004520 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004521 unsigned char input1[] = "Input 1";
4522 size_t input1_length = sizeof( input1 );
4523 unsigned char input2[] = "Input 2";
4524 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004525 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004526 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004527 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4528 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4529 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004531
Gilles Peskine8817f612018-12-18 00:18:46 +01004532 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004533
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004534 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4535 psa_set_key_algorithm( &attributes, alg );
4536 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004537
Gilles Peskine73676cb2019-05-15 20:15:10 +02004538 PSA_ASSERT( psa_import_key( &attributes,
4539 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004540 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004541
4542 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004543 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4544 input1, input1_length,
4545 input2, input2_length,
4546 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004547 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004548
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004549 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004550 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004551 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004552
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004554
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004555 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004556 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004557
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004558exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004560 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004561 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004562}
4563/* END_CASE */
4564
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004565/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004566void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004567{
4568 uint8_t output_buffer[16];
4569 size_t buffer_size = 16;
4570 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004571 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004572
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004573 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4574 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004575 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004576
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004578 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004579
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004580 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004581
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004582 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4583 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004584 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004585
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004586 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004587 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004588
4589exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004590 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004591}
4592/* END_CASE */
4593
4594/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004595void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004596 int step1_arg, data_t *input1,
4597 int step2_arg, data_t *input2,
4598 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004599 int requested_capacity_arg,
4600 data_t *expected_output1,
4601 data_t *expected_output2 )
4602{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004603 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004604 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4605 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004606 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4607 MBEDTLS_SVC_KEY_ID_INIT,
4608 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004609 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004611 uint8_t *expected_outputs[2] =
4612 {expected_output1->x, expected_output2->x};
4613 size_t output_sizes[2] =
4614 {expected_output1->len, expected_output2->len};
4615 size_t output_buffer_size = 0;
4616 uint8_t *output_buffer = NULL;
4617 size_t expected_capacity;
4618 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004620 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004621 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004622
4623 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4624 {
4625 if( output_sizes[i] > output_buffer_size )
4626 output_buffer_size = output_sizes[i];
4627 if( output_sizes[i] == 0 )
4628 expected_outputs[i] = NULL;
4629 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004630 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004631 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004632
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4634 psa_set_key_algorithm( &attributes, alg );
4635 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004636
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004637 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004638 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4639 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4640 requested_capacity ) );
4641 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004642 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004643 switch( steps[i] )
4644 {
4645 case 0:
4646 break;
4647 case PSA_KEY_DERIVATION_INPUT_SECRET:
4648 PSA_ASSERT( psa_import_key( &attributes,
4649 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004650 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004651
4652 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4653 {
4654 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4655 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4656 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4657 }
4658
Gilles Peskine1468da72019-05-29 17:35:49 +02004659 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004660 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004661 break;
4662 default:
4663 PSA_ASSERT( psa_key_derivation_input_bytes(
4664 &operation, steps[i],
4665 inputs[i]->x, inputs[i]->len ) );
4666 break;
4667 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004668 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004669
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004671 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004672 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004673 expected_capacity = requested_capacity;
4674
4675 /* Expansion phase. */
4676 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4677 {
4678 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004679 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004680 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004681 if( expected_capacity == 0 && output_sizes[i] == 0 )
4682 {
4683 /* Reading 0 bytes when 0 bytes are available can go either way. */
4684 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004685 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004686 continue;
4687 }
4688 else if( expected_capacity == 0 ||
4689 output_sizes[i] > expected_capacity )
4690 {
4691 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004692 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004693 expected_capacity = 0;
4694 continue;
4695 }
4696 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004697 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004698 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004699 ASSERT_COMPARE( output_buffer, output_sizes[i],
4700 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004701 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004702 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004704 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004705 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004706 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004707 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004708
4709exit:
4710 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004711 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004712 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4713 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004714 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004715}
4716/* END_CASE */
4717
4718/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004719void derive_full( int alg_arg,
4720 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004721 data_t *input1,
4722 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004723 int requested_capacity_arg )
4724{
Ronald Cron5425a212020-08-04 14:58:35 +02004725 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004726 psa_algorithm_t alg = alg_arg;
4727 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004729 unsigned char output_buffer[16];
4730 size_t expected_capacity = requested_capacity;
4731 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004732 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004733
Gilles Peskine8817f612018-12-18 00:18:46 +01004734 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004735
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004736 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4737 psa_set_key_algorithm( &attributes, alg );
4738 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004739
Gilles Peskine049c7532019-05-15 20:22:09 +02004740 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004741 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004742
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004743 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4744 input1->x, input1->len,
4745 input2->x, input2->len,
4746 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004747 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004748
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004750 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004751 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004752
4753 /* Expansion phase. */
4754 while( current_capacity > 0 )
4755 {
4756 size_t read_size = sizeof( output_buffer );
4757 if( read_size > current_capacity )
4758 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004760 output_buffer,
4761 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004762 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004764 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004765 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004766 }
4767
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 /* Check that the operation refuses to go over capacity. */
4769 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004770 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004771
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004773
4774exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004775 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004776 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004777 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004778}
4779/* END_CASE */
4780
Janos Follathe60c9052019-07-03 13:51:30 +01004781/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004782void derive_key_exercise( int alg_arg,
4783 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004784 data_t *input1,
4785 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004786 int derived_type_arg,
4787 int derived_bits_arg,
4788 int derived_usage_arg,
4789 int derived_alg_arg )
4790{
Ronald Cron5425a212020-08-04 14:58:35 +02004791 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4792 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004793 psa_algorithm_t alg = alg_arg;
4794 psa_key_type_t derived_type = derived_type_arg;
4795 size_t derived_bits = derived_bits_arg;
4796 psa_key_usage_t derived_usage = derived_usage_arg;
4797 psa_algorithm_t derived_alg = derived_alg_arg;
4798 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004801 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004802
Gilles Peskine8817f612018-12-18 00:18:46 +01004803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004804
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004805 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4806 psa_set_key_algorithm( &attributes, alg );
4807 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004808 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004809 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004810
4811 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004812 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4813 input1->x, input1->len,
4814 input2->x, input2->len,
4815 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004816 goto exit;
4817
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004818 psa_set_key_usage_flags( &attributes, derived_usage );
4819 psa_set_key_algorithm( &attributes, derived_alg );
4820 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004821 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004823 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004824
4825 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004826 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004827 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4828 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004829
4830 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004831 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004832 goto exit;
4833
4834exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004835 /*
4836 * Key attributes may have been returned by psa_get_key_attributes()
4837 * thus reset them as required.
4838 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004839 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004840
4841 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004842 psa_destroy_key( base_key );
4843 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004844 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004845}
4846/* END_CASE */
4847
Janos Follath42fd8882019-07-03 14:17:09 +01004848/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004849void derive_key_export( int alg_arg,
4850 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004851 data_t *input1,
4852 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004853 int bytes1_arg,
4854 int bytes2_arg )
4855{
Ronald Cron5425a212020-08-04 14:58:35 +02004856 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4857 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004858 psa_algorithm_t alg = alg_arg;
4859 size_t bytes1 = bytes1_arg;
4860 size_t bytes2 = bytes2_arg;
4861 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004863 uint8_t *output_buffer = NULL;
4864 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4866 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004867 size_t length;
4868
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004869 ASSERT_ALLOC( output_buffer, capacity );
4870 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004871 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004872
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004873 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4874 psa_set_key_algorithm( &base_attributes, alg );
4875 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004876 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004877 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004878
4879 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004880 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4881 input1->x, input1->len,
4882 input2->x, input2->len,
4883 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004884 goto exit;
4885
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004886 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004887 output_buffer,
4888 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004889 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004890
4891 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004892 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4893 input1->x, input1->len,
4894 input2->x, input2->len,
4895 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004896 goto exit;
4897
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004898 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4899 psa_set_key_algorithm( &derived_attributes, 0 );
4900 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004901 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004902 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004903 &derived_key ) );
4904 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004905 export_buffer, bytes1,
4906 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004907 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004908 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004909 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004911 &derived_key ) );
4912 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004913 export_buffer + bytes1, bytes2,
4914 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004915 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004916
4917 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004918 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4919 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004920
4921exit:
4922 mbedtls_free( output_buffer );
4923 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004924 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004925 psa_destroy_key( base_key );
4926 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004927 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004928}
4929/* END_CASE */
4930
4931/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004932void derive_key( int alg_arg,
4933 data_t *key_data, data_t *input1, data_t *input2,
4934 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004935 int expected_status_arg,
4936 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004937{
Ronald Cron5425a212020-08-04 14:58:35 +02004938 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4939 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004940 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004941 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004942 size_t bits = bits_arg;
4943 psa_status_t expected_status = expected_status_arg;
4944 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4945 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4946 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4947
4948 PSA_ASSERT( psa_crypto_init( ) );
4949
4950 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4951 psa_set_key_algorithm( &base_attributes, alg );
4952 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4953 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004954 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004955
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004956 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4957 input1->x, input1->len,
4958 input2->x, input2->len,
4959 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004960 goto exit;
4961
4962 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4963 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004964 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004965 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004966
4967 psa_status_t status =
4968 psa_key_derivation_output_key( &derived_attributes,
4969 &operation,
4970 &derived_key );
4971 if( is_large_output > 0 )
4972 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4973 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004974
4975exit:
4976 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004977 psa_destroy_key( base_key );
4978 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004979 PSA_DONE( );
4980}
4981/* END_CASE */
4982
4983/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004984void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004985 int our_key_type_arg, int our_key_alg_arg,
4986 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004987 int expected_status_arg )
4988{
Ronald Cron5425a212020-08-04 14:58:35 +02004989 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004990 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004991 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004992 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004995 psa_status_t expected_status = expected_status_arg;
4996 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004997
Gilles Peskine8817f612018-12-18 00:18:46 +01004998 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004999
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005000 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005001 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005002 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005003 PSA_ASSERT( psa_import_key( &attributes,
5004 our_key_data->x, our_key_data->len,
5005 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005006
Gilles Peskine77f40d82019-04-11 21:27:06 +02005007 /* The tests currently include inputs that should fail at either step.
5008 * Test cases that fail at the setup step should be changed to call
5009 * key_derivation_setup instead, and this function should be renamed
5010 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005012 if( status == PSA_SUCCESS )
5013 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005014 TEST_EQUAL( psa_key_derivation_key_agreement(
5015 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5016 our_key,
5017 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005018 expected_status );
5019 }
5020 else
5021 {
5022 TEST_ASSERT( status == expected_status );
5023 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005024
5025exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005027 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005028 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005029}
5030/* END_CASE */
5031
5032/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005033void raw_key_agreement( int alg_arg,
5034 int our_key_type_arg, data_t *our_key_data,
5035 data_t *peer_key_data,
5036 data_t *expected_output )
5037{
Ronald Cron5425a212020-08-04 14:58:35 +02005038 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005039 psa_algorithm_t alg = alg_arg;
5040 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005041 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005042 unsigned char *output = NULL;
5043 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005044 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005045
5046 ASSERT_ALLOC( output, expected_output->len );
5047 PSA_ASSERT( psa_crypto_init( ) );
5048
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005049 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5050 psa_set_key_algorithm( &attributes, alg );
5051 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005052 PSA_ASSERT( psa_import_key( &attributes,
5053 our_key_data->x, our_key_data->len,
5054 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005055
gabor-mezei-armceface22021-01-21 12:26:17 +01005056 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5057 key_bits = psa_get_key_bits( &attributes );
5058
Gilles Peskinebe697d82019-05-16 18:00:41 +02005059 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5060 peer_key_data->x, peer_key_data->len,
5061 output, expected_output->len,
5062 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005063 ASSERT_COMPARE( output, output_length,
5064 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005065 TEST_ASSERT( output_length <=
5066 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5067 TEST_ASSERT( output_length <=
5068 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005069
5070exit:
5071 mbedtls_free( output );
5072 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005073 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005074}
5075/* END_CASE */
5076
5077/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005078void key_agreement_capacity( int alg_arg,
5079 int our_key_type_arg, data_t *our_key_data,
5080 data_t *peer_key_data,
5081 int expected_capacity_arg )
5082{
Ronald Cron5425a212020-08-04 14:58:35 +02005083 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005084 psa_algorithm_t alg = alg_arg;
5085 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005088 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005089 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005090
Gilles Peskine8817f612018-12-18 00:18:46 +01005091 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005092
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5094 psa_set_key_algorithm( &attributes, alg );
5095 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005096 PSA_ASSERT( psa_import_key( &attributes,
5097 our_key_data->x, our_key_data->len,
5098 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005099
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005101 PSA_ASSERT( psa_key_derivation_key_agreement(
5102 &operation,
5103 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5104 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005105 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5106 {
5107 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005109 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005110 NULL, 0 ) );
5111 }
Gilles Peskine59685592018-09-18 12:11:34 +02005112
Gilles Peskinebf491972018-10-25 22:36:12 +02005113 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005114 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005116 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005117
Gilles Peskinebf491972018-10-25 22:36:12 +02005118 /* Test the actual capacity by reading the output. */
5119 while( actual_capacity > sizeof( output ) )
5120 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005122 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005123 actual_capacity -= sizeof( output );
5124 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005126 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005127 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005128 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005129
Gilles Peskine59685592018-09-18 12:11:34 +02005130exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005132 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005133 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005134}
5135/* END_CASE */
5136
5137/* BEGIN_CASE */
5138void key_agreement_output( int alg_arg,
5139 int our_key_type_arg, data_t *our_key_data,
5140 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005141 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005142{
Ronald Cron5425a212020-08-04 14:58:35 +02005143 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005144 psa_algorithm_t alg = alg_arg;
5145 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005146 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005148 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005149
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005150 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5151 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005152
Gilles Peskine8817f612018-12-18 00:18:46 +01005153 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005154
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005155 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5156 psa_set_key_algorithm( &attributes, alg );
5157 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005158 PSA_ASSERT( psa_import_key( &attributes,
5159 our_key_data->x, our_key_data->len,
5160 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005161
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005163 PSA_ASSERT( psa_key_derivation_key_agreement(
5164 &operation,
5165 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5166 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005167 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5168 {
5169 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005170 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005171 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005172 NULL, 0 ) );
5173 }
Gilles Peskine59685592018-09-18 12:11:34 +02005174
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005176 actual_output,
5177 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005178 ASSERT_COMPARE( actual_output, expected_output1->len,
5179 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005180 if( expected_output2->len != 0 )
5181 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005183 actual_output,
5184 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005185 ASSERT_COMPARE( actual_output, expected_output2->len,
5186 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005187 }
Gilles Peskine59685592018-09-18 12:11:34 +02005188
5189exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005191 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005192 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005193 mbedtls_free( actual_output );
5194}
5195/* END_CASE */
5196
5197/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005198void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005199{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005200 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005201 unsigned char *output = NULL;
5202 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005203 size_t i;
5204 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005205
Simon Butcher49f8e312020-03-03 15:51:50 +00005206 TEST_ASSERT( bytes_arg >= 0 );
5207
Gilles Peskine91892022021-02-08 19:50:26 +01005208 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005209 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005210
Gilles Peskine8817f612018-12-18 00:18:46 +01005211 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005212
Gilles Peskinea50d7392018-06-21 10:22:13 +02005213 /* Run several times, to ensure that every output byte will be
5214 * nonzero at least once with overwhelming probability
5215 * (2^(-8*number_of_runs)). */
5216 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005217 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005218 if( bytes != 0 )
5219 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005220 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005221
Gilles Peskinea50d7392018-06-21 10:22:13 +02005222 for( i = 0; i < bytes; i++ )
5223 {
5224 if( output[i] != 0 )
5225 ++changed[i];
5226 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005227 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005228
5229 /* Check that every byte was changed to nonzero at least once. This
5230 * validates that psa_generate_random is overwriting every byte of
5231 * the output buffer. */
5232 for( i = 0; i < bytes; i++ )
5233 {
5234 TEST_ASSERT( changed[i] != 0 );
5235 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005236
5237exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005238 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005239 mbedtls_free( output );
5240 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005241}
5242/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005243
5244/* BEGIN_CASE */
5245void generate_key( int type_arg,
5246 int bits_arg,
5247 int usage_arg,
5248 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005249 int expected_status_arg,
5250 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005251{
Ronald Cron5425a212020-08-04 14:58:35 +02005252 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005253 psa_key_type_t type = type_arg;
5254 psa_key_usage_t usage = usage_arg;
5255 size_t bits = bits_arg;
5256 psa_algorithm_t alg = alg_arg;
5257 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005259 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005260
Gilles Peskine8817f612018-12-18 00:18:46 +01005261 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005262
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005263 psa_set_key_usage_flags( &attributes, usage );
5264 psa_set_key_algorithm( &attributes, alg );
5265 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005266 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005267
5268 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005269 psa_status_t status = psa_generate_key( &attributes, &key );
5270
5271 if( is_large_key > 0 )
5272 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5273 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005274 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005275 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005276
5277 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005278 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005279 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5280 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005281
Gilles Peskine818ca122018-06-20 18:16:48 +02005282 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005283 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005284 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005285
5286exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005287 /*
5288 * Key attributes may have been returned by psa_get_key_attributes()
5289 * thus reset them as required.
5290 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005291 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005292
Ronald Cron5425a212020-08-04 14:58:35 +02005293 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005294 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005295}
5296/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005297
Ronald Cronee414c72021-03-18 18:50:08 +01005298/* 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 +02005299void generate_key_rsa( int bits_arg,
5300 data_t *e_arg,
5301 int expected_status_arg )
5302{
Ronald Cron5425a212020-08-04 14:58:35 +02005303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005304 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005305 size_t bits = bits_arg;
5306 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5307 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5308 psa_status_t expected_status = expected_status_arg;
5309 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5310 uint8_t *exported = NULL;
5311 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005312 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005313 size_t exported_length = SIZE_MAX;
5314 uint8_t *e_read_buffer = NULL;
5315 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005316 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005317 size_t e_read_length = SIZE_MAX;
5318
5319 if( e_arg->len == 0 ||
5320 ( e_arg->len == 3 &&
5321 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5322 {
5323 is_default_public_exponent = 1;
5324 e_read_size = 0;
5325 }
5326 ASSERT_ALLOC( e_read_buffer, e_read_size );
5327 ASSERT_ALLOC( exported, exported_size );
5328
5329 PSA_ASSERT( psa_crypto_init( ) );
5330
5331 psa_set_key_usage_flags( &attributes, usage );
5332 psa_set_key_algorithm( &attributes, alg );
5333 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5334 e_arg->x, e_arg->len ) );
5335 psa_set_key_bits( &attributes, bits );
5336
5337 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005338 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005339 if( expected_status != PSA_SUCCESS )
5340 goto exit;
5341
5342 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005343 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005344 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5345 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5346 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5347 e_read_buffer, e_read_size,
5348 &e_read_length ) );
5349 if( is_default_public_exponent )
5350 TEST_EQUAL( e_read_length, 0 );
5351 else
5352 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5353
5354 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005355 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005356 goto exit;
5357
5358 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005359 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005360 exported, exported_size,
5361 &exported_length ) );
5362 {
5363 uint8_t *p = exported;
5364 uint8_t *end = exported + exported_length;
5365 size_t len;
5366 /* RSAPublicKey ::= SEQUENCE {
5367 * modulus INTEGER, -- n
5368 * publicExponent INTEGER } -- e
5369 */
5370 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005371 MBEDTLS_ASN1_SEQUENCE |
5372 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005373 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005374 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5375 MBEDTLS_ASN1_INTEGER ) );
5376 if( len >= 1 && p[0] == 0 )
5377 {
5378 ++p;
5379 --len;
5380 }
5381 if( e_arg->len == 0 )
5382 {
5383 TEST_EQUAL( len, 3 );
5384 TEST_EQUAL( p[0], 1 );
5385 TEST_EQUAL( p[1], 0 );
5386 TEST_EQUAL( p[2], 1 );
5387 }
5388 else
5389 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5390 }
5391
5392exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005393 /*
5394 * Key attributes may have been returned by psa_get_key_attributes() or
5395 * set by psa_set_key_domain_parameters() thus reset them as required.
5396 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005397 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005398
Ronald Cron5425a212020-08-04 14:58:35 +02005399 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005400 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005401 mbedtls_free( e_read_buffer );
5402 mbedtls_free( exported );
5403}
5404/* END_CASE */
5405
Darryl Greend49a4992018-06-18 17:27:26 +01005406/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005407void persistent_key_load_key_from_storage( data_t *data,
5408 int type_arg, int bits_arg,
5409 int usage_flags_arg, int alg_arg,
5410 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005411{
Ronald Cron71016a92020-08-28 19:01:50 +02005412 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005414 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5415 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005416 psa_key_type_t type = type_arg;
5417 size_t bits = bits_arg;
5418 psa_key_usage_t usage_flags = usage_flags_arg;
5419 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005420 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005421 unsigned char *first_export = NULL;
5422 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005423 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005424 size_t first_exported_length;
5425 size_t second_exported_length;
5426
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005427 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5428 {
5429 ASSERT_ALLOC( first_export, export_size );
5430 ASSERT_ALLOC( second_export, export_size );
5431 }
Darryl Greend49a4992018-06-18 17:27:26 +01005432
Gilles Peskine8817f612018-12-18 00:18:46 +01005433 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005434
Gilles Peskinec87af662019-05-15 16:12:22 +02005435 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005436 psa_set_key_usage_flags( &attributes, usage_flags );
5437 psa_set_key_algorithm( &attributes, alg );
5438 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005439 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005440
Darryl Green0c6575a2018-11-07 16:05:30 +00005441 switch( generation_method )
5442 {
5443 case IMPORT_KEY:
5444 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005445 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005446 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005447 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005448
Darryl Green0c6575a2018-11-07 16:05:30 +00005449 case GENERATE_KEY:
5450 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005451 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005452 break;
5453
5454 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005455#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005456 {
5457 /* Create base key */
5458 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5459 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5460 psa_set_key_usage_flags( &base_attributes,
5461 PSA_KEY_USAGE_DERIVE );
5462 psa_set_key_algorithm( &base_attributes, derive_alg );
5463 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005464 PSA_ASSERT( psa_import_key( &base_attributes,
5465 data->x, data->len,
5466 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005467 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005469 PSA_ASSERT( psa_key_derivation_input_key(
5470 &operation,
5471 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005472 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005473 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005474 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005475 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5476 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005477 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005478 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005479 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005480 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005481 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005482#else
5483 TEST_ASSUME( ! "KDF not supported in this configuration" );
5484#endif
5485 break;
5486
5487 default:
5488 TEST_ASSERT( ! "generation_method not implemented in test" );
5489 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005490 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005491 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005492
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005493 /* Export the key if permitted by the key policy. */
5494 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5495 {
Ronald Cron5425a212020-08-04 14:58:35 +02005496 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005497 first_export, export_size,
5498 &first_exported_length ) );
5499 if( generation_method == IMPORT_KEY )
5500 ASSERT_COMPARE( data->x, data->len,
5501 first_export, first_exported_length );
5502 }
Darryl Greend49a4992018-06-18 17:27:26 +01005503
5504 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005505 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005506 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005507 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005508
Darryl Greend49a4992018-06-18 17:27:26 +01005509 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005510 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005511 TEST_ASSERT( mbedtls_svc_key_id_equal(
5512 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005513 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5514 PSA_KEY_LIFETIME_PERSISTENT );
5515 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5516 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005517 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005518 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005519 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005520
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005521 /* Export the key again if permitted by the key policy. */
5522 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005523 {
Ronald Cron5425a212020-08-04 14:58:35 +02005524 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005525 second_export, export_size,
5526 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005527 ASSERT_COMPARE( first_export, first_exported_length,
5528 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005529 }
5530
5531 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005532 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005533 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005534
5535exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005536 /*
5537 * Key attributes may have been returned by psa_get_key_attributes()
5538 * thus reset them as required.
5539 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005540 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005541
Darryl Greend49a4992018-06-18 17:27:26 +01005542 mbedtls_free( first_export );
5543 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005544 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005545 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005546 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005547 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005548}
5549/* END_CASE */