blob: eb9458fdeebba24c7aa59b660d5064a7533065aa [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskinef6279312021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Dave Rodgman34b147d2021-06-23 12:49:59 +010022/* Assert that an operation is (not) active.
23 * This serves as a proxy for checking if the operation is aborted. */
24#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
25#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
26
Jaeden Amerof24c7f82018-06-27 17:20:43 +010027/** An invalid export length that will never be set by psa_export_key(). */
28static const size_t INVALID_EXPORT_LENGTH = ~0U;
29
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030/** Test if a buffer contains a constant byte value.
31 *
32 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 *
34 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020035 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 * \param size Size of the buffer in bytes.
37 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 * \return 1 if the buffer is all-bits-zero.
39 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042{
43 size_t i;
44 for( i = 0; i < size; i++ )
45 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020046 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020047 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020049 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020050}
Gilles Peskine818ca122018-06-20 18:16:48 +020051
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
53static int asn1_write_10x( unsigned char **p,
54 unsigned char *start,
55 size_t bits,
56 unsigned char x )
57{
58 int ret;
59 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020060 if( bits == 0 )
61 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
62 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020063 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030064 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
66 *p -= len;
67 ( *p )[len-1] = x;
68 if( bits % 8 == 0 )
69 ( *p )[1] |= 1;
70 else
71 ( *p )[0] |= 1 << ( bits % 8 );
72 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
74 MBEDTLS_ASN1_INTEGER ) );
75 return( len );
76}
77
78static int construct_fake_rsa_key( unsigned char *buffer,
79 size_t buffer_size,
80 unsigned char **p,
81 size_t bits,
82 int keypair )
83{
84 size_t half_bits = ( bits + 1 ) / 2;
85 int ret;
86 int len = 0;
87 /* Construct something that looks like a DER encoding of
88 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
89 * RSAPrivateKey ::= SEQUENCE {
90 * version Version,
91 * modulus INTEGER, -- n
92 * publicExponent INTEGER, -- e
93 * privateExponent INTEGER, -- d
94 * prime1 INTEGER, -- p
95 * prime2 INTEGER, -- q
96 * exponent1 INTEGER, -- d mod (p-1)
97 * exponent2 INTEGER, -- d mod (q-1)
98 * coefficient INTEGER, -- (inverse of q) mod p
99 * otherPrimeInfos OtherPrimeInfos OPTIONAL
100 * }
101 * Or, for a public key, the same structure with only
102 * version, modulus and publicExponent.
103 */
104 *p = buffer + buffer_size;
105 if( keypair )
106 {
107 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
112 asn1_write_10x( p, buffer, half_bits, 1 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* q */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
116 asn1_write_10x( p, buffer, half_bits, 3 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* d */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 }
120 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
121 asn1_write_10x( p, buffer, 17, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* n */
123 asn1_write_10x( p, buffer, bits, 1 ) );
124 if( keypair )
125 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
126 mbedtls_asn1_write_int( p, buffer, 0 ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
128 {
129 const unsigned char tag =
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
132 }
133 return( len );
134}
135
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100136int exercise_mac_setup( psa_key_type_t key_type,
137 const unsigned char *key_bytes,
138 size_t key_length,
139 psa_algorithm_t alg,
140 psa_mac_operation_t *operation,
141 psa_status_t *status )
142{
Ronald Cron5425a212020-08-04 14:58:35 +0200143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200147 psa_set_key_algorithm( &attributes, alg );
148 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200149 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150
Ronald Cron5425a212020-08-04 14:58:35 +0200151 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100152 /* Whether setup succeeded or failed, abort must succeed. */
153 PSA_ASSERT( psa_mac_abort( operation ) );
154 /* If setup failed, reproduce the failure, so that the caller can
155 * test the resulting state of the operation object. */
156 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 {
Ronald Cron5425a212020-08-04 14:58:35 +0200158 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 }
160
Ronald Cron5425a212020-08-04 14:58:35 +0200161 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162 return( 1 );
163
164exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200165 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 return( 0 );
167}
168
169int exercise_cipher_setup( psa_key_type_t key_type,
170 const unsigned char *key_bytes,
171 size_t key_length,
172 psa_algorithm_t alg,
173 psa_cipher_operation_t *operation,
174 psa_status_t *status )
175{
Ronald Cron5425a212020-08-04 14:58:35 +0200176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
180 psa_set_key_algorithm( &attributes, alg );
181 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200182 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Ronald Cron5425a212020-08-04 14:58:35 +0200184 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100185 /* Whether setup succeeded or failed, abort must succeed. */
186 PSA_ASSERT( psa_cipher_abort( operation ) );
187 /* If setup failed, reproduce the failure, so that the caller can
188 * test the resulting state of the operation object. */
189 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 {
Ronald Cron5425a212020-08-04 14:58:35 +0200191 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 1 );
197
198exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200199 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 return( 0 );
201}
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204{
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 uint8_t buffer[1];
208 size_t length;
209 int ok = 0;
210
Ronald Cronecfb2372020-07-23 17:13:42 +0200211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000216 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 TEST_EQUAL(
218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
226
Ronald Cron5425a212020-08-04 14:58:35 +0200227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000228 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000231 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233 ok = 1;
234
235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236 /*
237 * Key attributes may have been returned by psa_get_key_attributes()
238 * thus reset them as required.
239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100241
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 return( ok );
243}
244
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200245/* Assert that a key isn't reported as having a slot number. */
246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
248 do \
249 { \
250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
251 TEST_EQUAL( psa_get_key_slot_number( \
252 attributes, \
253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
254 PSA_ERROR_INVALID_ARGUMENT ); \
255 } \
256 while( 0 )
257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
259 ( (void) 0 )
260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
261
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100262/* An overapproximation of the amount of storage needed for a key of the
263 * given type and with the given content. The API doesn't make it easy
264 * to find a good value for the size. The current implementation doesn't
265 * care about the value anyway. */
266#define KEY_BITS_FROM_DATA( type, data ) \
267 ( data )->len
268
Darryl Green0c6575a2018-11-07 16:05:30 +0000269typedef enum {
270 IMPORT_KEY = 0,
271 GENERATE_KEY = 1,
272 DERIVE_KEY = 2
273} generate_method;
274
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275/* END_HEADER */
276
277/* BEGIN_DEPENDENCIES
278 * depends_on:MBEDTLS_PSA_CRYPTO_C
279 * END_DEPENDENCIES
280 */
281
282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200283void static_checks( )
284{
285 size_t max_truncated_mac_size =
286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
287
288 /* Check that the length for a truncated MAC always fits in the algorithm
289 * encoding. The shifted mask is the maximum truncated value. The
290 * untruncated algorithm may be one byte larger. */
291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100292
293#if defined(MBEDTLS_TEST_DEPRECATED)
294 /* Check deprecated constants. */
295 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
296 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
297 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
298 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
299 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
300 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
301 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
302 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100303
Paul Elliott8ff510a2020-06-02 17:19:28 +0100304 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
324 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
328 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
329 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
330 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
331 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
333 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
334
335 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
336 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
337 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
338 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
339 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
340 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
341 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
342 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100343
Paul Elliott75e27032020-06-03 15:17:39 +0100344 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
345 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
347 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
348 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
349
350 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
351 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100352#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200353}
354/* END_CASE */
355
356/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200357void import_with_policy( int type_arg,
358 int usage_arg, int alg_arg,
359 int expected_status_arg )
360{
361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
362 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200363 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200364 psa_key_type_t type = type_arg;
365 psa_key_usage_t usage = usage_arg;
366 psa_algorithm_t alg = alg_arg;
367 psa_status_t expected_status = expected_status_arg;
368 const uint8_t key_material[16] = {0};
369 psa_status_t status;
370
371 PSA_ASSERT( psa_crypto_init( ) );
372
373 psa_set_key_type( &attributes, type );
374 psa_set_key_usage_flags( &attributes, usage );
375 psa_set_key_algorithm( &attributes, alg );
376
377 status = psa_import_key( &attributes,
378 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200379 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380 TEST_EQUAL( status, expected_status );
381 if( status != PSA_SUCCESS )
382 goto exit;
383
Ronald Cron5425a212020-08-04 14:58:35 +0200384 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200385 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200386 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200387 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200388 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200389 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200390
Ronald Cron5425a212020-08-04 14:58:35 +0200391 PSA_ASSERT( psa_destroy_key( key ) );
392 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393
394exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100395 /*
396 * Key attributes may have been returned by psa_get_key_attributes()
397 * thus reset them as required.
398 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200399 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100400
401 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200402 PSA_DONE( );
403}
404/* END_CASE */
405
406/* BEGIN_CASE */
407void import_with_data( data_t *data, int type_arg,
408 int attr_bits_arg,
409 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200410{
411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
412 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200414 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200415 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200416 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418
Gilles Peskine8817f612018-12-18 00:18:46 +0100419 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100420
Gilles Peskine4747d192019-04-17 15:05:45 +0200421 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200422 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200423
Ronald Cron5425a212020-08-04 14:58:35 +0200424 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100425 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200426 if( status != PSA_SUCCESS )
427 goto exit;
428
Ronald Cron5425a212020-08-04 14:58:35 +0200429 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200430 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200431 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200432 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200433 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200434
Ronald Cron5425a212020-08-04 14:58:35 +0200435 PSA_ASSERT( psa_destroy_key( key ) );
436 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100437
438exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100439 /*
440 * Key attributes may have been returned by psa_get_key_attributes()
441 * thus reset them as required.
442 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200443 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100444
445 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200446 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447}
448/* END_CASE */
449
450/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200451void import_large_key( int type_arg, int byte_size_arg,
452 int expected_status_arg )
453{
454 psa_key_type_t type = type_arg;
455 size_t byte_size = byte_size_arg;
456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
457 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200459 psa_status_t status;
460 uint8_t *buffer = NULL;
461 size_t buffer_size = byte_size + 1;
462 size_t n;
463
Steven Cooreman69967ce2021-01-18 18:01:08 +0100464 /* Skip the test case if the target running the test cannot
465 * accomodate large keys due to heap size constraints */
466 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200467 memset( buffer, 'K', byte_size );
468
469 PSA_ASSERT( psa_crypto_init( ) );
470
471 /* Try importing the key */
472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
473 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200474 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100475 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 TEST_EQUAL( status, expected_status );
477
478 if( status == PSA_SUCCESS )
479 {
Ronald Cron5425a212020-08-04 14:58:35 +0200480 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200481 TEST_EQUAL( psa_get_key_type( &attributes ), type );
482 TEST_EQUAL( psa_get_key_bits( &attributes ),
483 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200484 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200485 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200486 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200487 for( n = 0; n < byte_size; n++ )
488 TEST_EQUAL( buffer[n], 'K' );
489 for( n = byte_size; n < buffer_size; n++ )
490 TEST_EQUAL( buffer[n], 0 );
491 }
492
493exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100494 /*
495 * Key attributes may have been returned by psa_get_key_attributes()
496 * thus reset them as required.
497 */
498 psa_reset_key_attributes( &attributes );
499
Ronald Cron5425a212020-08-04 14:58:35 +0200500 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200501 PSA_DONE( );
502 mbedtls_free( buffer );
503}
504/* END_CASE */
505
506/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200507void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
508{
Ronald Cron5425a212020-08-04 14:58:35 +0200509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200510 size_t bits = bits_arg;
511 psa_status_t expected_status = expected_status_arg;
512 psa_status_t status;
513 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200514 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200515 size_t buffer_size = /* Slight overapproximations */
516 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200517 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200518 unsigned char *p;
519 int ret;
520 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200522
Gilles Peskine8817f612018-12-18 00:18:46 +0100523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200524 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200525
526 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
527 bits, keypair ) ) >= 0 );
528 length = ret;
529
530 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200531 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200532 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100533 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200534
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200535 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200536 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200537
538exit:
539 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200540 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200541}
542/* END_CASE */
543
544/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300545void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300546 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200547 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 int expected_bits,
549 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200550 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 int canonical_input )
552{
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200555 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200556 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558 unsigned char *exported = NULL;
559 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100561 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200564 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565
Moran Pekercb088e72018-07-17 17:36:59 +0300566 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200567 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200569 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100570 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571
Gilles Peskine4747d192019-04-17 15:05:45 +0200572 psa_set_key_usage_flags( &attributes, usage_arg );
573 psa_set_key_algorithm( &attributes, alg );
574 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700575
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100576 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200577 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578
579 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200580 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200581 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
582 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200583 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584
585 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200586 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100587 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100588
589 /* The exported length must be set by psa_export_key() to a value between 0
590 * and export_size. On errors, the exported length must be 0. */
591 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
592 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
593 TEST_ASSERT( exported_length <= export_size );
594
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200595 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200596 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200598 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100599 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200601 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602
Gilles Peskineea38a922021-02-13 00:05:16 +0100603 /* Run sanity checks on the exported key. For non-canonical inputs,
604 * this validates the canonical representations. For canonical inputs,
605 * this doesn't directly validate the implementation, but it still helps
606 * by cross-validating the test data with the sanity check code. */
607 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200608 goto exit;
609
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200611 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100612 else
613 {
Ronald Cron5425a212020-08-04 14:58:35 +0200614 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200615 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200616 &key2 ) );
617 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100618 reexported,
619 export_size,
620 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200621 ASSERT_COMPARE( exported, exported_length,
622 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200623 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100625 TEST_ASSERT( exported_length <=
626 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
627 psa_get_key_bits( &got_attributes ) ) );
628 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629
630destroy:
631 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200632 PSA_ASSERT( psa_destroy_key( key ) );
633 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634
635exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100636 /*
637 * Key attributes may have been returned by psa_get_key_attributes()
638 * thus reset them as required.
639 */
640 psa_reset_key_attributes( &got_attributes );
641
itayzafrir3e02b3b2018-06-12 17:06:52 +0300642 mbedtls_free( exported );
643 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200644 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645}
646/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100647
Moran Pekerf709f4a2018-06-06 17:26:04 +0300648/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300649void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200650 int type_arg,
651 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100652 int export_size_delta,
653 int expected_export_status_arg,
654 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655{
Ronald Cron5425a212020-08-04 14:58:35 +0200656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300657 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200658 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200659 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300661 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100662 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100663 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665
Gilles Peskine8817f612018-12-18 00:18:46 +0100666 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300667
Gilles Peskine4747d192019-04-17 15:05:45 +0200668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
669 psa_set_key_algorithm( &attributes, alg );
670 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300671
672 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300674
Gilles Peskine49c25912018-10-29 15:15:31 +0100675 /* Export the public key */
676 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200677 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200678 exported, export_size,
679 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100680 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100681 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100682 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200683 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100684 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100687 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100688 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100689 TEST_ASSERT( expected_public_key->len <=
690 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
691 TEST_ASSERT( expected_public_key->len <=
692 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100693 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
694 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100695 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300696
697exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100698 /*
699 * Key attributes may have been returned by psa_get_key_attributes()
700 * thus reset them as required.
701 */
702 psa_reset_key_attributes( &attributes );
703
itayzafrir3e02b3b2018-06-12 17:06:52 +0300704 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200705 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200706 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300707}
708/* END_CASE */
709
Gilles Peskine20035e32018-02-03 22:44:14 +0100710/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200711void import_and_exercise_key( data_t *data,
712 int type_arg,
713 int bits_arg,
714 int alg_arg )
715{
Ronald Cron5425a212020-08-04 14:58:35 +0200716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200717 psa_key_type_t type = type_arg;
718 size_t bits = bits_arg;
719 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100720 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200722 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200723
Gilles Peskine8817f612018-12-18 00:18:46 +0100724 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200725
Gilles Peskine4747d192019-04-17 15:05:45 +0200726 psa_set_key_usage_flags( &attributes, usage );
727 psa_set_key_algorithm( &attributes, alg );
728 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200729
730 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200731 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
733 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200734 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200735 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
736 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200737
738 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100739 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200740 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200741
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_destroy_key( key ) );
743 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200744
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200745exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100746 /*
747 * Key attributes may have been returned by psa_get_key_attributes()
748 * thus reset them as required.
749 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200750 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100751
752 psa_reset_key_attributes( &attributes );
753 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200754 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200755}
756/* END_CASE */
757
758/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100759void effective_key_attributes( int type_arg, int expected_type_arg,
760 int bits_arg, int expected_bits_arg,
761 int usage_arg, int expected_usage_arg,
762 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200763{
Ronald Cron5425a212020-08-04 14:58:35 +0200764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100765 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100766 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100767 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100768 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200769 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100770 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200771 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100772 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200774
Gilles Peskine8817f612018-12-18 00:18:46 +0100775 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200776
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200777 psa_set_key_usage_flags( &attributes, usage );
778 psa_set_key_algorithm( &attributes, alg );
779 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100780 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200781
Ronald Cron5425a212020-08-04 14:58:35 +0200782 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100783 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200784
Ronald Cron5425a212020-08-04 14:58:35 +0200785 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100786 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
787 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
788 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
789 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200790
791exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100792 /*
793 * Key attributes may have been returned by psa_get_key_attributes()
794 * thus reset them as required.
795 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200796 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100797
798 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200799 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200800}
801/* END_CASE */
802
803/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100804void check_key_policy( int type_arg, int bits_arg,
805 int usage_arg, int alg_arg )
806{
807 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200808 usage_arg,
809 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200810 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100811 goto exit;
812}
813/* END_CASE */
814
815/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200816void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000817{
818 /* Test each valid way of initializing the object, except for `= {0}`, as
819 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
820 * though it's OK by the C standard. We could test for this, but we'd need
821 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200822 psa_key_attributes_t func = psa_key_attributes_init( );
823 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
824 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000825
826 memset( &zero, 0, sizeof( zero ) );
827
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200828 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
829 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
830 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000831
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200832 TEST_EQUAL( psa_get_key_type( &func ), 0 );
833 TEST_EQUAL( psa_get_key_type( &init ), 0 );
834 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
835
836 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
837 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
838 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
839
840 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
841 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
842 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
843
844 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
845 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
846 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000847}
848/* END_CASE */
849
850/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200851void mac_key_policy( int policy_usage_arg,
852 int policy_alg_arg,
853 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200854 data_t *key_data,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200855 int exercise_alg_arg,
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200856 int expected_status_sign_arg,
857 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200858{
Ronald Cron5425a212020-08-04 14:58:35 +0200859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000861 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200862 psa_key_type_t key_type = key_type_arg;
863 psa_algorithm_t policy_alg = policy_alg_arg;
864 psa_algorithm_t exercise_alg = exercise_alg_arg;
865 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200866 psa_status_t status;
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200867 psa_status_t expected_status_sign = expected_status_sign_arg;
868 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200870
Gilles Peskine8817f612018-12-18 00:18:46 +0100871 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200872
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200873 psa_set_key_usage_flags( &attributes, policy_usage );
874 psa_set_key_algorithm( &attributes, policy_alg );
875 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200876
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200879
gabor-mezei-arm659af9e2021-06-29 11:06:16 +0200880 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
881 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200882
Ronald Cron5425a212020-08-04 14:58:35 +0200883 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200884 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100885
Mateusz Starzyke6e02b62021-08-30 17:09:03 +0200886 /* Calculate the MAC, one-shot case. */
887 uint8_t input[128] = {0};
888 size_t mac_len;
889 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
890 input, 128,
891 mac, PSA_MAC_MAX_SIZE, &mac_len ),
892 expected_status_sign );
893
894 /* Verify correct MAC, one-shot case. */
895 status = psa_mac_verify( key, exercise_alg, input, 128,
896 mac, mac_len );
897
898 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
899 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
900 else
901 TEST_EQUAL( status, expected_status_verify );
902
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200903 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200904
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200906 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzyk25e65db2021-08-24 11:01:23 +0200907 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908
909exit:
910 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200911 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200912 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200913}
914/* END_CASE */
915
916/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200917void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 int policy_alg,
919 int key_type,
920 data_t *key_data,
921 int exercise_alg )
922{
Ronald Cron5425a212020-08-04 14:58:35 +0200923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000925 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200926 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200927 psa_status_t status;
928
Gilles Peskine8817f612018-12-18 00:18:46 +0100929 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200931 psa_set_key_usage_flags( &attributes, policy_usage );
932 psa_set_key_algorithm( &attributes, policy_alg );
933 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200934
Gilles Peskine049c7532019-05-15 20:22:09 +0200935 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200936 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200938 /* Check if no key usage flag implication is done */
939 TEST_EQUAL( policy_usage,
940 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200941
Ronald Cron5425a212020-08-04 14:58:35 +0200942 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943 if( policy_alg == exercise_alg &&
944 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100945 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200946 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100947 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948 psa_cipher_abort( &operation );
949
Ronald Cron5425a212020-08-04 14:58:35 +0200950 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200951 if( policy_alg == exercise_alg &&
952 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100953 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200954 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100955 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956
957exit:
958 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200959 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200960 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961}
962/* END_CASE */
963
964/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200965void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 int policy_alg,
967 int key_type,
968 data_t *key_data,
969 int nonce_length_arg,
970 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100971 int exercise_alg,
972 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973{
Ronald Cron5425a212020-08-04 14:58:35 +0200974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200976 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200977 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100978 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979 unsigned char nonce[16] = {0};
980 size_t nonce_length = nonce_length_arg;
981 unsigned char tag[16];
982 size_t tag_length = tag_length_arg;
983 size_t output_length;
984
985 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
986 TEST_ASSERT( tag_length <= sizeof( tag ) );
987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200990 psa_set_key_usage_flags( &attributes, policy_usage );
991 psa_set_key_algorithm( &attributes, policy_alg );
992 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200993
Gilles Peskine049c7532019-05-15 20:22:09 +0200994 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200995 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200996
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200997 /* Check if no key usage implication is done */
998 TEST_EQUAL( policy_usage,
999 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001000
Ronald Cron5425a212020-08-04 14:58:35 +02001001 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001002 nonce, nonce_length,
1003 NULL, 0,
1004 NULL, 0,
1005 tag, tag_length,
1006 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001007 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1008 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001009 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001010 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011
1012 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001013 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014 nonce, nonce_length,
1015 NULL, 0,
1016 tag, tag_length,
1017 NULL, 0,
1018 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001019 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1020 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1021 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001022 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001023 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001024 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025
1026exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001027 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001028 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001029}
1030/* END_CASE */
1031
1032/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001033void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034 int policy_alg,
1035 int key_type,
1036 data_t *key_data,
1037 int exercise_alg )
1038{
Ronald Cron5425a212020-08-04 14:58:35 +02001039 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001040 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001041 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001042 psa_status_t status;
1043 size_t key_bits;
1044 size_t buffer_length;
1045 unsigned char *buffer = NULL;
1046 size_t output_length;
1047
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001049
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001050 psa_set_key_usage_flags( &attributes, policy_usage );
1051 psa_set_key_algorithm( &attributes, policy_alg );
1052 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001053
Gilles Peskine049c7532019-05-15 20:22:09 +02001054 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001055 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001056
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001057 /* Check if no key usage implication is done */
1058 TEST_EQUAL( policy_usage,
1059 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001060
Ronald Cron5425a212020-08-04 14:58:35 +02001061 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001062 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001063 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1064 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001065 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001066
Ronald Cron5425a212020-08-04 14:58:35 +02001067 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068 NULL, 0,
1069 NULL, 0,
1070 buffer, buffer_length,
1071 &output_length );
1072 if( policy_alg == exercise_alg &&
1073 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001074 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001075 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001076 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001078 if( buffer_length != 0 )
1079 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001080 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001081 buffer, buffer_length,
1082 NULL, 0,
1083 buffer, buffer_length,
1084 &output_length );
1085 if( policy_alg == exercise_alg &&
1086 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001087 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001088 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001089 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001090
1091exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001092 /*
1093 * Key attributes may have been returned by psa_get_key_attributes()
1094 * thus reset them as required.
1095 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001096 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001097
1098 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001099 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100 mbedtls_free( buffer );
1101}
1102/* END_CASE */
1103
1104/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001105void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106 int policy_alg,
1107 int key_type,
1108 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001109 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001110 int payload_length_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001111 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001112{
Ronald Cron5425a212020-08-04 14:58:35 +02001113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001115 psa_key_usage_t policy_usage = policy_usage_arg;
1116 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001117 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001118 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1119 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1120 * compatible with the policy and `payload_length_arg` is supposed to be
1121 * a valid input length to sign. If `payload_length_arg <= 0`,
1122 * `exercise_alg` is supposed to be forbidden by the policy. */
1123 int compatible_alg = payload_length_arg > 0;
1124 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001125 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001126 size_t signature_length;
1127
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001128 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001129 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001130 TEST_EQUAL( expected_usage,
1131 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001132
Gilles Peskine8817f612018-12-18 00:18:46 +01001133 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001134
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001135 psa_set_key_usage_flags( &attributes, policy_usage );
1136 psa_set_key_algorithm( &attributes, policy_alg );
1137 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001138
Gilles Peskine049c7532019-05-15 20:22:09 +02001139 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001140 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001141
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001142 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1143
Ronald Cron5425a212020-08-04 14:58:35 +02001144 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001145 payload, payload_length,
1146 signature, sizeof( signature ),
1147 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001148 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001149 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001150 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001151 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152
1153 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001154 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001155 payload, payload_length,
1156 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001157 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001158 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001159 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001160 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001161
Gilles Peskine8cb22c82021-09-22 16:15:05 +02001162 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-arm79df41d2021-06-28 14:53:49 +02001163 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001164 {
1165 status = psa_sign_message( key, exercise_alg,
1166 payload, payload_length,
1167 signature, sizeof( signature ),
1168 &signature_length );
1169 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1170 PSA_ASSERT( status );
1171 else
1172 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1173
1174 memset( signature, 0, sizeof( signature ) );
1175 status = psa_verify_message( key, exercise_alg,
1176 payload, payload_length,
1177 signature, sizeof( signature ) );
1178 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1179 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1180 else
1181 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1182 }
1183
Gilles Peskined5b33222018-06-18 22:20:03 +02001184exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001186 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001187}
1188/* END_CASE */
1189
Janos Follathba3fab92019-06-11 14:50:16 +01001190/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001191void derive_key_policy( int policy_usage,
1192 int policy_alg,
1193 int key_type,
1194 data_t *key_data,
1195 int exercise_alg )
1196{
Ronald Cron5425a212020-08-04 14:58:35 +02001197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001198 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001199 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001200 psa_status_t status;
1201
Gilles Peskine8817f612018-12-18 00:18:46 +01001202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001203
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001204 psa_set_key_usage_flags( &attributes, policy_usage );
1205 psa_set_key_algorithm( &attributes, policy_alg );
1206 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001207
Gilles Peskine049c7532019-05-15 20:22:09 +02001208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001209 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001210
Janos Follathba3fab92019-06-11 14:50:16 +01001211 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1212
1213 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1214 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001215 {
Janos Follathba3fab92019-06-11 14:50:16 +01001216 PSA_ASSERT( psa_key_derivation_input_bytes(
1217 &operation,
1218 PSA_KEY_DERIVATION_INPUT_SEED,
1219 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001220 }
Janos Follathba3fab92019-06-11 14:50:16 +01001221
1222 status = psa_key_derivation_input_key( &operation,
1223 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001224 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001225
Gilles Peskineea0fb492018-07-12 17:17:20 +02001226 if( policy_alg == exercise_alg &&
1227 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001228 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001229 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001230 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001231
1232exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001233 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001234 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001235 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001236}
1237/* END_CASE */
1238
1239/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001240void agreement_key_policy( int policy_usage,
1241 int policy_alg,
1242 int key_type_arg,
1243 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001244 int exercise_alg,
1245 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001246{
Ronald Cron5425a212020-08-04 14:58:35 +02001247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001249 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001250 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001251 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001252 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001253
Gilles Peskine8817f612018-12-18 00:18:46 +01001254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001255
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001256 psa_set_key_usage_flags( &attributes, policy_usage );
1257 psa_set_key_algorithm( &attributes, policy_alg );
1258 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001259
Gilles Peskine049c7532019-05-15 20:22:09 +02001260 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001261 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001262
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001263 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001264 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001265
Steven Cooremance48e852020-10-05 16:02:45 +02001266 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001267
1268exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001269 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001270 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001271 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001272}
1273/* END_CASE */
1274
1275/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001276void key_policy_alg2( int key_type_arg, data_t *key_data,
1277 int usage_arg, int alg_arg, int alg2_arg )
1278{
Ronald Cron5425a212020-08-04 14:58:35 +02001279 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001280 psa_key_type_t key_type = key_type_arg;
1281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1282 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1283 psa_key_usage_t usage = usage_arg;
1284 psa_algorithm_t alg = alg_arg;
1285 psa_algorithm_t alg2 = alg2_arg;
1286
1287 PSA_ASSERT( psa_crypto_init( ) );
1288
1289 psa_set_key_usage_flags( &attributes, usage );
1290 psa_set_key_algorithm( &attributes, alg );
1291 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1292 psa_set_key_type( &attributes, key_type );
1293 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001294 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001295
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001296 /* Update the usage flags to obtain implicit usage flags */
1297 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001298 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001299 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1300 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1301 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1302
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001303 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001304 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001305 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001306 goto exit;
1307
1308exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001309 /*
1310 * Key attributes may have been returned by psa_get_key_attributes()
1311 * thus reset them as required.
1312 */
1313 psa_reset_key_attributes( &got_attributes );
1314
Ronald Cron5425a212020-08-04 14:58:35 +02001315 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001316 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001317}
1318/* END_CASE */
1319
1320/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001321void raw_agreement_key_policy( int policy_usage,
1322 int policy_alg,
1323 int key_type_arg,
1324 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001325 int exercise_alg,
1326 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001327{
Ronald Cron5425a212020-08-04 14:58:35 +02001328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001330 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001331 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001332 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001333 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001334
1335 PSA_ASSERT( psa_crypto_init( ) );
1336
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001337 psa_set_key_usage_flags( &attributes, policy_usage );
1338 psa_set_key_algorithm( &attributes, policy_alg );
1339 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001340
Gilles Peskine049c7532019-05-15 20:22:09 +02001341 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001342 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001343
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001344 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001345
Steven Cooremance48e852020-10-05 16:02:45 +02001346 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001347
1348exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001349 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001350 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001351 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001352}
1353/* END_CASE */
1354
1355/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001356void copy_success( int source_usage_arg,
1357 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001358 int type_arg, data_t *material,
1359 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001360 int target_usage_arg,
1361 int target_alg_arg, int target_alg2_arg,
1362 int expected_usage_arg,
1363 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001364{
Gilles Peskineca25db92019-04-19 11:43:08 +02001365 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1366 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001367 psa_key_usage_t expected_usage = expected_usage_arg;
1368 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001369 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001370 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1371 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001372 uint8_t *export_buffer = NULL;
1373
Gilles Peskine57ab7212019-01-28 13:03:09 +01001374 PSA_ASSERT( psa_crypto_init( ) );
1375
Gilles Peskineca25db92019-04-19 11:43:08 +02001376 /* Prepare the source key. */
1377 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1378 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001379 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001380 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001381 PSA_ASSERT( psa_import_key( &source_attributes,
1382 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001383 &source_key ) );
1384 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001385
Gilles Peskineca25db92019-04-19 11:43:08 +02001386 /* Prepare the target attributes. */
1387 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001388 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001389 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001390 /* Set volatile lifetime to reset the key identifier to 0. */
1391 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1392 }
1393
Gilles Peskineca25db92019-04-19 11:43:08 +02001394 if( target_usage_arg != -1 )
1395 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1396 if( target_alg_arg != -1 )
1397 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001398 if( target_alg2_arg != -1 )
1399 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001400
1401 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001402 PSA_ASSERT( psa_copy_key( source_key,
1403 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001404
1405 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001406 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001407
1408 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001409 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001410 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1411 psa_get_key_type( &target_attributes ) );
1412 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1413 psa_get_key_bits( &target_attributes ) );
1414 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1415 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001416 TEST_EQUAL( expected_alg2,
1417 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001418 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1419 {
1420 size_t length;
1421 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001422 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001423 material->len, &length ) );
1424 ASSERT_COMPARE( material->x, material->len,
1425 export_buffer, length );
1426 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001427
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001428 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001429 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001430 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001431 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001432
Ronald Cron5425a212020-08-04 14:58:35 +02001433 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001434
1435exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001436 /*
1437 * Source and target key attributes may have been returned by
1438 * psa_get_key_attributes() thus reset them as required.
1439 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001440 psa_reset_key_attributes( &source_attributes );
1441 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001442
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001443 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001444 mbedtls_free( export_buffer );
1445}
1446/* END_CASE */
1447
1448/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001449void copy_fail( int source_usage_arg,
1450 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001451 int type_arg, data_t *material,
1452 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001453 int target_usage_arg,
1454 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001455 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001456 int expected_status_arg )
1457{
1458 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1459 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001460 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1461 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001462 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001463
1464 PSA_ASSERT( psa_crypto_init( ) );
1465
1466 /* Prepare the source key. */
1467 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1468 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001469 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001470 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001471 PSA_ASSERT( psa_import_key( &source_attributes,
1472 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001473 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001474
1475 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001476 psa_set_key_id( &target_attributes, key_id );
1477 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001478 psa_set_key_type( &target_attributes, target_type_arg );
1479 psa_set_key_bits( &target_attributes, target_bits_arg );
1480 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1481 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001482 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001483
1484 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001485 TEST_EQUAL( psa_copy_key( source_key,
1486 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001487 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001488
Ronald Cron5425a212020-08-04 14:58:35 +02001489 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001490
Gilles Peskine4a644642019-05-03 17:14:08 +02001491exit:
1492 psa_reset_key_attributes( &source_attributes );
1493 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001494 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001495}
1496/* END_CASE */
1497
1498/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001499void hash_operation_init( )
1500{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001501 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001502 /* Test each valid way of initializing the object, except for `= {0}`, as
1503 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1504 * though it's OK by the C standard. We could test for this, but we'd need
1505 * to supress the Clang warning for the test. */
1506 psa_hash_operation_t func = psa_hash_operation_init( );
1507 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1508 psa_hash_operation_t zero;
1509
1510 memset( &zero, 0, sizeof( zero ) );
1511
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001512 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001513 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1514 PSA_ERROR_BAD_STATE );
1515 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1516 PSA_ERROR_BAD_STATE );
1517 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1518 PSA_ERROR_BAD_STATE );
1519
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001520 /* A default hash operation should be abortable without error. */
1521 PSA_ASSERT( psa_hash_abort( &func ) );
1522 PSA_ASSERT( psa_hash_abort( &init ) );
1523 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001524}
1525/* END_CASE */
1526
1527/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001528void hash_setup( int alg_arg,
1529 int expected_status_arg )
1530{
1531 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001532 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001533 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001534 psa_status_t status;
1535
Gilles Peskine8817f612018-12-18 00:18:46 +01001536 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001537
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001538 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001539 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001540
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001541 /* Whether setup succeeded or failed, abort must succeed. */
1542 PSA_ASSERT( psa_hash_abort( &operation ) );
1543
1544 /* If setup failed, reproduce the failure, so as to
1545 * test the resulting state of the operation object. */
1546 if( status != PSA_SUCCESS )
1547 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1548
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001549 /* Now the operation object should be reusable. */
1550#if defined(KNOWN_SUPPORTED_HASH_ALG)
1551 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1552 PSA_ASSERT( psa_hash_abort( &operation ) );
1553#endif
1554
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001555exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001556 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001557}
1558/* END_CASE */
1559
1560/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001561void hash_compute_fail( int alg_arg, data_t *input,
1562 int output_size_arg, int expected_status_arg )
1563{
1564 psa_algorithm_t alg = alg_arg;
1565 uint8_t *output = NULL;
1566 size_t output_size = output_size_arg;
1567 size_t output_length = INVALID_EXPORT_LENGTH;
1568 psa_status_t expected_status = expected_status_arg;
1569 psa_status_t status;
1570
1571 ASSERT_ALLOC( output, output_size );
1572
1573 PSA_ASSERT( psa_crypto_init( ) );
1574
1575 status = psa_hash_compute( alg, input->x, input->len,
1576 output, output_size, &output_length );
1577 TEST_EQUAL( status, expected_status );
1578 TEST_ASSERT( output_length <= output_size );
1579
1580exit:
1581 mbedtls_free( output );
1582 PSA_DONE( );
1583}
1584/* END_CASE */
1585
1586/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001587void hash_compare_fail( int alg_arg, data_t *input,
1588 data_t *reference_hash,
1589 int expected_status_arg )
1590{
1591 psa_algorithm_t alg = alg_arg;
1592 psa_status_t expected_status = expected_status_arg;
1593 psa_status_t status;
1594
1595 PSA_ASSERT( psa_crypto_init( ) );
1596
1597 status = psa_hash_compare( alg, input->x, input->len,
1598 reference_hash->x, reference_hash->len );
1599 TEST_EQUAL( status, expected_status );
1600
1601exit:
1602 PSA_DONE( );
1603}
1604/* END_CASE */
1605
1606/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001607void hash_compute_compare( int alg_arg, data_t *input,
1608 data_t *expected_output )
1609{
1610 psa_algorithm_t alg = alg_arg;
1611 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1612 size_t output_length = INVALID_EXPORT_LENGTH;
1613 size_t i;
1614
1615 PSA_ASSERT( psa_crypto_init( ) );
1616
1617 /* Compute with tight buffer */
1618 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001619 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001620 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001621 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001622 ASSERT_COMPARE( output, output_length,
1623 expected_output->x, expected_output->len );
1624
1625 /* Compute with larger buffer */
1626 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1627 output, sizeof( output ),
1628 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001629 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001630 ASSERT_COMPARE( output, output_length,
1631 expected_output->x, expected_output->len );
1632
1633 /* Compare with correct hash */
1634 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1635 output, output_length ) );
1636
1637 /* Compare with trailing garbage */
1638 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1639 output, output_length + 1 ),
1640 PSA_ERROR_INVALID_SIGNATURE );
1641
1642 /* Compare with truncated hash */
1643 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1644 output, output_length - 1 ),
1645 PSA_ERROR_INVALID_SIGNATURE );
1646
1647 /* Compare with corrupted value */
1648 for( i = 0; i < output_length; i++ )
1649 {
Chris Jones9634bb12021-01-20 15:56:42 +00001650 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001651 output[i] ^= 1;
1652 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1653 output, output_length ),
1654 PSA_ERROR_INVALID_SIGNATURE );
1655 output[i] ^= 1;
1656 }
1657
1658exit:
1659 PSA_DONE( );
1660}
1661/* END_CASE */
1662
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001663/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001664void hash_bad_order( )
1665{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001666 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001667 unsigned char input[] = "";
1668 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001669 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001670 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1671 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1672 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001673 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001674 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001675 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001676
Gilles Peskine8817f612018-12-18 00:18:46 +01001677 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001678
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001679 /* Call setup twice in a row. */
1680 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001681 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001682 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1683 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001684 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001685 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001686 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001687
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001688 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001689 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001690 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001691 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001692
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001693 /* Check that update calls abort on error. */
1694 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman54f73512021-06-24 18:14:52 +01001695 operation.id = UINT_MAX;
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001696 ASSERT_OPERATION_IS_ACTIVE( operation );
1697 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1698 PSA_ERROR_BAD_STATE );
1699 ASSERT_OPERATION_IS_INACTIVE( operation );
1700 PSA_ASSERT( psa_hash_abort( &operation ) );
1701 ASSERT_OPERATION_IS_INACTIVE( operation );
1702
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001703 /* Call update after finish. */
1704 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1705 PSA_ASSERT( psa_hash_finish( &operation,
1706 hash, sizeof( hash ), &hash_len ) );
1707 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001708 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001709 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001710
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001711 /* Call verify without calling setup beforehand. */
1712 TEST_EQUAL( psa_hash_verify( &operation,
1713 valid_hash, sizeof( valid_hash ) ),
1714 PSA_ERROR_BAD_STATE );
1715 PSA_ASSERT( psa_hash_abort( &operation ) );
1716
1717 /* Call verify after finish. */
1718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1719 PSA_ASSERT( psa_hash_finish( &operation,
1720 hash, sizeof( hash ), &hash_len ) );
1721 TEST_EQUAL( psa_hash_verify( &operation,
1722 valid_hash, sizeof( valid_hash ) ),
1723 PSA_ERROR_BAD_STATE );
1724 PSA_ASSERT( psa_hash_abort( &operation ) );
1725
1726 /* Call verify twice in a row. */
1727 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001728 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001729 PSA_ASSERT( psa_hash_verify( &operation,
1730 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001731 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001732 TEST_EQUAL( psa_hash_verify( &operation,
1733 valid_hash, sizeof( valid_hash ) ),
1734 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001735 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001736 PSA_ASSERT( psa_hash_abort( &operation ) );
1737
1738 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001739 TEST_EQUAL( psa_hash_finish( &operation,
1740 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001741 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001742 PSA_ASSERT( psa_hash_abort( &operation ) );
1743
1744 /* Call finish twice in a row. */
1745 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1746 PSA_ASSERT( psa_hash_finish( &operation,
1747 hash, sizeof( hash ), &hash_len ) );
1748 TEST_EQUAL( psa_hash_finish( &operation,
1749 hash, sizeof( hash ), &hash_len ),
1750 PSA_ERROR_BAD_STATE );
1751 PSA_ASSERT( psa_hash_abort( &operation ) );
1752
1753 /* Call finish after calling verify. */
1754 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1755 PSA_ASSERT( psa_hash_verify( &operation,
1756 valid_hash, sizeof( valid_hash ) ) );
1757 TEST_EQUAL( psa_hash_finish( &operation,
1758 hash, sizeof( hash ), &hash_len ),
1759 PSA_ERROR_BAD_STATE );
1760 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001761
1762exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001763 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001764}
1765/* END_CASE */
1766
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001767/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001768void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001769{
1770 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001771 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1772 * appended to it */
1773 unsigned char hash[] = {
1774 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1775 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1776 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001777 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001778 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001779
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001781
itayzafrir27e69452018-11-01 14:26:34 +02001782 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001784 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001785 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001786 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001787 ASSERT_OPERATION_IS_INACTIVE( operation );
1788 PSA_ASSERT( psa_hash_abort( &operation ) );
1789 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001790
itayzafrir27e69452018-11-01 14:26:34 +02001791 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001792 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001793 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001794 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001795
itayzafrir27e69452018-11-01 14:26:34 +02001796 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001797 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001798 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001799 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001800
itayzafrirec93d302018-10-18 18:01:10 +03001801exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001802 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001803}
1804/* END_CASE */
1805
Ronald Cronee414c72021-03-18 18:50:08 +01001806/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001807void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001808{
1809 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001810 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001811 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001812 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001813 size_t hash_len;
1814
Gilles Peskine8817f612018-12-18 00:18:46 +01001815 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001816
itayzafrir58028322018-10-25 10:22:01 +03001817 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001818 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001819 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001820 hash, expected_size - 1, &hash_len ),
1821 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001822
1823exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001824 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001825}
1826/* END_CASE */
1827
Ronald Cronee414c72021-03-18 18:50:08 +01001828/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001829void hash_clone_source_state( )
1830{
1831 psa_algorithm_t alg = PSA_ALG_SHA_256;
1832 unsigned char hash[PSA_HASH_MAX_SIZE];
1833 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1834 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1835 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1836 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1837 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1838 size_t hash_len;
1839
1840 PSA_ASSERT( psa_crypto_init( ) );
1841 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1842
1843 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1844 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1845 PSA_ASSERT( psa_hash_finish( &op_finished,
1846 hash, sizeof( hash ), &hash_len ) );
1847 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1848 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1849
1850 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1851 PSA_ERROR_BAD_STATE );
1852
1853 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1854 PSA_ASSERT( psa_hash_finish( &op_init,
1855 hash, sizeof( hash ), &hash_len ) );
1856 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1857 PSA_ASSERT( psa_hash_finish( &op_finished,
1858 hash, sizeof( hash ), &hash_len ) );
1859 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1860 PSA_ASSERT( psa_hash_finish( &op_aborted,
1861 hash, sizeof( hash ), &hash_len ) );
1862
1863exit:
1864 psa_hash_abort( &op_source );
1865 psa_hash_abort( &op_init );
1866 psa_hash_abort( &op_setup );
1867 psa_hash_abort( &op_finished );
1868 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001869 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001870}
1871/* END_CASE */
1872
Ronald Cronee414c72021-03-18 18:50:08 +01001873/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001874void hash_clone_target_state( )
1875{
1876 psa_algorithm_t alg = PSA_ALG_SHA_256;
1877 unsigned char hash[PSA_HASH_MAX_SIZE];
1878 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1879 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1880 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1881 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1882 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1883 size_t hash_len;
1884
1885 PSA_ASSERT( psa_crypto_init( ) );
1886
1887 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1888 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1889 PSA_ASSERT( psa_hash_finish( &op_finished,
1890 hash, sizeof( hash ), &hash_len ) );
1891 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1892 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1893
1894 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1895 PSA_ASSERT( psa_hash_finish( &op_target,
1896 hash, sizeof( hash ), &hash_len ) );
1897
1898 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1899 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1900 PSA_ERROR_BAD_STATE );
1901 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1902 PSA_ERROR_BAD_STATE );
1903
1904exit:
1905 psa_hash_abort( &op_target );
1906 psa_hash_abort( &op_init );
1907 psa_hash_abort( &op_setup );
1908 psa_hash_abort( &op_finished );
1909 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001910 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001911}
1912/* END_CASE */
1913
itayzafrir58028322018-10-25 10:22:01 +03001914/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001915void mac_operation_init( )
1916{
Jaeden Amero252ef282019-02-15 14:05:35 +00001917 const uint8_t input[1] = { 0 };
1918
Jaeden Amero769ce272019-01-04 11:48:03 +00001919 /* Test each valid way of initializing the object, except for `= {0}`, as
1920 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1921 * though it's OK by the C standard. We could test for this, but we'd need
1922 * to supress the Clang warning for the test. */
1923 psa_mac_operation_t func = psa_mac_operation_init( );
1924 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1925 psa_mac_operation_t zero;
1926
1927 memset( &zero, 0, sizeof( zero ) );
1928
Jaeden Amero252ef282019-02-15 14:05:35 +00001929 /* A freshly-initialized MAC operation should not be usable. */
1930 TEST_EQUAL( psa_mac_update( &func,
1931 input, sizeof( input ) ),
1932 PSA_ERROR_BAD_STATE );
1933 TEST_EQUAL( psa_mac_update( &init,
1934 input, sizeof( input ) ),
1935 PSA_ERROR_BAD_STATE );
1936 TEST_EQUAL( psa_mac_update( &zero,
1937 input, sizeof( input ) ),
1938 PSA_ERROR_BAD_STATE );
1939
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001940 /* A default MAC operation should be abortable without error. */
1941 PSA_ASSERT( psa_mac_abort( &func ) );
1942 PSA_ASSERT( psa_mac_abort( &init ) );
1943 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001944}
1945/* END_CASE */
1946
1947/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948void mac_setup( int key_type_arg,
1949 data_t *key,
1950 int alg_arg,
1951 int expected_status_arg )
1952{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001953 psa_key_type_t key_type = key_type_arg;
1954 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001955 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001956 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001957 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1958#if defined(KNOWN_SUPPORTED_MAC_ALG)
1959 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1960#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001961
Gilles Peskine8817f612018-12-18 00:18:46 +01001962 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001963
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001964 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1965 &operation, &status ) )
1966 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001967 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001968
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001969 /* The operation object should be reusable. */
1970#if defined(KNOWN_SUPPORTED_MAC_ALG)
1971 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1972 smoke_test_key_data,
1973 sizeof( smoke_test_key_data ),
1974 KNOWN_SUPPORTED_MAC_ALG,
1975 &operation, &status ) )
1976 goto exit;
1977 TEST_EQUAL( status, PSA_SUCCESS );
1978#endif
1979
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001980exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001981 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001982}
1983/* END_CASE */
1984
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001985/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00001986void mac_bad_order( )
1987{
Ronald Cron5425a212020-08-04 14:58:35 +02001988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001989 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1990 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001991 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001992 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1993 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1994 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001996 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1997 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1998 size_t sign_mac_length = 0;
1999 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2000 const uint8_t verify_mac[] = {
2001 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2002 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2003 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2004
2005 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002006 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002007 psa_set_key_algorithm( &attributes, alg );
2008 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002009
Ronald Cron5425a212020-08-04 14:58:35 +02002010 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2011 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002012
Jaeden Amero252ef282019-02-15 14:05:35 +00002013 /* Call update without calling setup beforehand. */
2014 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2015 PSA_ERROR_BAD_STATE );
2016 PSA_ASSERT( psa_mac_abort( &operation ) );
2017
2018 /* Call sign finish without calling setup beforehand. */
2019 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2020 &sign_mac_length),
2021 PSA_ERROR_BAD_STATE );
2022 PSA_ASSERT( psa_mac_abort( &operation ) );
2023
2024 /* Call verify finish without calling setup beforehand. */
2025 TEST_EQUAL( psa_mac_verify_finish( &operation,
2026 verify_mac, sizeof( verify_mac ) ),
2027 PSA_ERROR_BAD_STATE );
2028 PSA_ASSERT( psa_mac_abort( &operation ) );
2029
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002030 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002031 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002032 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002033 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002034 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002035 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002036 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002037 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002038
Jaeden Amero252ef282019-02-15 14:05:35 +00002039 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002040 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002041 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2042 PSA_ASSERT( psa_mac_sign_finish( &operation,
2043 sign_mac, sizeof( sign_mac ),
2044 &sign_mac_length ) );
2045 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2046 PSA_ERROR_BAD_STATE );
2047 PSA_ASSERT( psa_mac_abort( &operation ) );
2048
2049 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002050 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002051 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2052 PSA_ASSERT( psa_mac_verify_finish( &operation,
2053 verify_mac, sizeof( verify_mac ) ) );
2054 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2055 PSA_ERROR_BAD_STATE );
2056 PSA_ASSERT( psa_mac_abort( &operation ) );
2057
2058 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002059 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002060 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2061 PSA_ASSERT( psa_mac_sign_finish( &operation,
2062 sign_mac, sizeof( sign_mac ),
2063 &sign_mac_length ) );
2064 TEST_EQUAL( psa_mac_sign_finish( &operation,
2065 sign_mac, sizeof( sign_mac ),
2066 &sign_mac_length ),
2067 PSA_ERROR_BAD_STATE );
2068 PSA_ASSERT( psa_mac_abort( &operation ) );
2069
2070 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002071 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002072 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2073 PSA_ASSERT( psa_mac_verify_finish( &operation,
2074 verify_mac, sizeof( verify_mac ) ) );
2075 TEST_EQUAL( psa_mac_verify_finish( &operation,
2076 verify_mac, sizeof( verify_mac ) ),
2077 PSA_ERROR_BAD_STATE );
2078 PSA_ASSERT( psa_mac_abort( &operation ) );
2079
2080 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002081 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002082 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002083 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002084 TEST_EQUAL( psa_mac_verify_finish( &operation,
2085 verify_mac, sizeof( verify_mac ) ),
2086 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002087 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002088 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002089 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002090
2091 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002092 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002093 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002094 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002095 TEST_EQUAL( psa_mac_sign_finish( &operation,
2096 sign_mac, sizeof( sign_mac ),
2097 &sign_mac_length ),
2098 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002099 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002100 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002101 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002102
Ronald Cron5425a212020-08-04 14:58:35 +02002103 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002104
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002106 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002107}
2108/* END_CASE */
2109
2110/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002111void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002112 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002113 int alg_arg,
2114 data_t *input,
2115 data_t *expected_mac )
2116{
Ronald Cron5425a212020-08-04 14:58:35 +02002117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002118 psa_key_type_t key_type = key_type_arg;
2119 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002120 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002122 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002123 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002124 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002125 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002126 const size_t output_sizes_to_test[] = {
2127 0,
2128 1,
2129 expected_mac->len - 1,
2130 expected_mac->len,
2131 expected_mac->len + 1,
2132 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002133
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002134 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002135 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002136 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002137
Gilles Peskine8817f612018-12-18 00:18:46 +01002138 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002139
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002141 psa_set_key_algorithm( &attributes, alg );
2142 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002143
Ronald Cron5425a212020-08-04 14:58:35 +02002144 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2145 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002146
Gilles Peskine8b356b52020-08-25 23:44:59 +02002147 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2148 {
2149 const size_t output_size = output_sizes_to_test[i];
2150 psa_status_t expected_status =
2151 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2152 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002153
Chris Jones9634bb12021-01-20 15:56:42 +00002154 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002155 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002156
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002157 /* Calculate the MAC, one-shot case. */
2158 TEST_EQUAL( psa_mac_compute( key, alg,
2159 input->x, input->len,
2160 actual_mac, output_size, &mac_length ),
2161 expected_status );
2162 if( expected_status == PSA_SUCCESS )
2163 {
2164 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2165 actual_mac, mac_length );
2166 }
2167
2168 if( output_size > 0 )
2169 memset( actual_mac, 0, output_size );
2170
2171 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002172 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002173 PSA_ASSERT( psa_mac_update( &operation,
2174 input->x, input->len ) );
2175 TEST_EQUAL( psa_mac_sign_finish( &operation,
2176 actual_mac, output_size,
2177 &mac_length ),
2178 expected_status );
2179 PSA_ASSERT( psa_mac_abort( &operation ) );
2180
2181 if( expected_status == PSA_SUCCESS )
2182 {
2183 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2184 actual_mac, mac_length );
2185 }
2186 mbedtls_free( actual_mac );
2187 actual_mac = NULL;
2188 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002189
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002190exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002191 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002192 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002193 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002194 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002195}
2196/* END_CASE */
2197
2198/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002199void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002200 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002201 int alg_arg,
2202 data_t *input,
2203 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002204{
Ronald Cron5425a212020-08-04 14:58:35 +02002205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002206 psa_key_type_t key_type = key_type_arg;
2207 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002208 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002210 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002211
Gilles Peskine69c12672018-06-28 00:07:19 +02002212 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2213
Gilles Peskine8817f612018-12-18 00:18:46 +01002214 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002217 psa_set_key_algorithm( &attributes, alg );
2218 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002219
Ronald Cron5425a212020-08-04 14:58:35 +02002220 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2221 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002222
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002223 /* Verify correct MAC, one-shot case. */
2224 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2225 expected_mac->x, expected_mac->len ) );
2226
2227 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002228 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002229 PSA_ASSERT( psa_mac_update( &operation,
2230 input->x, input->len ) );
2231 PSA_ASSERT( psa_mac_verify_finish( &operation,
2232 expected_mac->x,
2233 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002234
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002235 /* Test a MAC that's too short, one-shot case. */
2236 TEST_EQUAL( psa_mac_verify( key, alg,
2237 input->x, input->len,
2238 expected_mac->x,
2239 expected_mac->len - 1 ),
2240 PSA_ERROR_INVALID_SIGNATURE );
2241
2242 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002243 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002244 PSA_ASSERT( psa_mac_update( &operation,
2245 input->x, input->len ) );
2246 TEST_EQUAL( psa_mac_verify_finish( &operation,
2247 expected_mac->x,
2248 expected_mac->len - 1 ),
2249 PSA_ERROR_INVALID_SIGNATURE );
2250
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002251 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002252 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2253 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002254 TEST_EQUAL( psa_mac_verify( key, alg,
2255 input->x, input->len,
2256 perturbed_mac, expected_mac->len + 1 ),
2257 PSA_ERROR_INVALID_SIGNATURE );
2258
2259 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002260 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002261 PSA_ASSERT( psa_mac_update( &operation,
2262 input->x, input->len ) );
2263 TEST_EQUAL( psa_mac_verify_finish( &operation,
2264 perturbed_mac,
2265 expected_mac->len + 1 ),
2266 PSA_ERROR_INVALID_SIGNATURE );
2267
2268 /* Test changing one byte. */
2269 for( size_t i = 0; i < expected_mac->len; i++ )
2270 {
Chris Jones9634bb12021-01-20 15:56:42 +00002271 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002272 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002273
2274 TEST_EQUAL( psa_mac_verify( key, alg,
2275 input->x, input->len,
2276 perturbed_mac, expected_mac->len ),
2277 PSA_ERROR_INVALID_SIGNATURE );
2278
Ronald Cron5425a212020-08-04 14:58:35 +02002279 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002280 PSA_ASSERT( psa_mac_update( &operation,
2281 input->x, input->len ) );
2282 TEST_EQUAL( psa_mac_verify_finish( &operation,
2283 perturbed_mac,
2284 expected_mac->len ),
2285 PSA_ERROR_INVALID_SIGNATURE );
2286 perturbed_mac[i] ^= 1;
2287 }
2288
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002290 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002291 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002292 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002293 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002294}
2295/* END_CASE */
2296
2297/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002298void cipher_operation_init( )
2299{
Jaeden Ameroab439972019-02-15 14:12:05 +00002300 const uint8_t input[1] = { 0 };
2301 unsigned char output[1] = { 0 };
2302 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002303 /* Test each valid way of initializing the object, except for `= {0}`, as
2304 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2305 * though it's OK by the C standard. We could test for this, but we'd need
2306 * to supress the Clang warning for the test. */
2307 psa_cipher_operation_t func = psa_cipher_operation_init( );
2308 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2309 psa_cipher_operation_t zero;
2310
2311 memset( &zero, 0, sizeof( zero ) );
2312
Jaeden Ameroab439972019-02-15 14:12:05 +00002313 /* A freshly-initialized cipher operation should not be usable. */
2314 TEST_EQUAL( psa_cipher_update( &func,
2315 input, sizeof( input ),
2316 output, sizeof( output ),
2317 &output_length ),
2318 PSA_ERROR_BAD_STATE );
2319 TEST_EQUAL( psa_cipher_update( &init,
2320 input, sizeof( input ),
2321 output, sizeof( output ),
2322 &output_length ),
2323 PSA_ERROR_BAD_STATE );
2324 TEST_EQUAL( psa_cipher_update( &zero,
2325 input, sizeof( input ),
2326 output, sizeof( output ),
2327 &output_length ),
2328 PSA_ERROR_BAD_STATE );
2329
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002330 /* A default cipher operation should be abortable without error. */
2331 PSA_ASSERT( psa_cipher_abort( &func ) );
2332 PSA_ASSERT( psa_cipher_abort( &init ) );
2333 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002334}
2335/* END_CASE */
2336
2337/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338void cipher_setup( int key_type_arg,
2339 data_t *key,
2340 int alg_arg,
2341 int expected_status_arg )
2342{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002343 psa_key_type_t key_type = key_type_arg;
2344 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002345 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002346 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002348#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002349 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2350#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002351
Gilles Peskine8817f612018-12-18 00:18:46 +01002352 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002353
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002354 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2355 &operation, &status ) )
2356 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002357 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002358
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002359 /* The operation object should be reusable. */
2360#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2361 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2362 smoke_test_key_data,
2363 sizeof( smoke_test_key_data ),
2364 KNOWN_SUPPORTED_CIPHER_ALG,
2365 &operation, &status ) )
2366 goto exit;
2367 TEST_EQUAL( status, PSA_SUCCESS );
2368#endif
2369
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002370exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002371 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002372 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002373}
2374/* END_CASE */
2375
Ronald Cronee414c72021-03-18 18:50:08 +01002376/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002377void cipher_bad_order( )
2378{
Ronald Cron5425a212020-08-04 14:58:35 +02002379 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002380 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2381 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002383 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002384 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002385 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002386 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2387 0xaa, 0xaa, 0xaa, 0xaa };
2388 const uint8_t text[] = {
2389 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2390 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002391 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002392 size_t length = 0;
2393
2394 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002395 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2396 psa_set_key_algorithm( &attributes, alg );
2397 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002398 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2399 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002400
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002401 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002402 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002403 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002404 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002405 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002406 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002407 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002408 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002409
2410 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002411 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002412 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002413 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002414 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002415 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002416 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002417 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002418
Jaeden Ameroab439972019-02-15 14:12:05 +00002419 /* Generate an IV without calling setup beforehand. */
2420 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2421 buffer, sizeof( buffer ),
2422 &length ),
2423 PSA_ERROR_BAD_STATE );
2424 PSA_ASSERT( psa_cipher_abort( &operation ) );
2425
2426 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002427 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002428 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2429 buffer, sizeof( buffer ),
2430 &length ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002431 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002432 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2433 buffer, sizeof( buffer ),
2434 &length ),
2435 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002436 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002437 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002438 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002439
2440 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002441 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002442 PSA_ASSERT( psa_cipher_set_iv( &operation,
2443 iv, sizeof( iv ) ) );
2444 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2445 buffer, sizeof( buffer ),
2446 &length ),
2447 PSA_ERROR_BAD_STATE );
2448 PSA_ASSERT( psa_cipher_abort( &operation ) );
2449
2450 /* Set an IV without calling setup beforehand. */
2451 TEST_EQUAL( psa_cipher_set_iv( &operation,
2452 iv, sizeof( iv ) ),
2453 PSA_ERROR_BAD_STATE );
2454 PSA_ASSERT( psa_cipher_abort( &operation ) );
2455
2456 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002457 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002458 PSA_ASSERT( psa_cipher_set_iv( &operation,
2459 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002460 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002461 TEST_EQUAL( psa_cipher_set_iv( &operation,
2462 iv, sizeof( iv ) ),
2463 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002464 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002465 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002466 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002467
2468 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002469 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002470 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2471 buffer, sizeof( buffer ),
2472 &length ) );
2473 TEST_EQUAL( psa_cipher_set_iv( &operation,
2474 iv, sizeof( iv ) ),
2475 PSA_ERROR_BAD_STATE );
2476 PSA_ASSERT( psa_cipher_abort( &operation ) );
2477
2478 /* Call update without calling setup beforehand. */
2479 TEST_EQUAL( psa_cipher_update( &operation,
2480 text, sizeof( text ),
2481 buffer, sizeof( buffer ),
2482 &length ),
2483 PSA_ERROR_BAD_STATE );
2484 PSA_ASSERT( psa_cipher_abort( &operation ) );
2485
2486 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002487 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002488 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002489 TEST_EQUAL( psa_cipher_update( &operation,
2490 text, sizeof( text ),
2491 buffer, sizeof( buffer ),
2492 &length ),
2493 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002494 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002495 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002496 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002497
2498 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002499 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002500 PSA_ASSERT( psa_cipher_set_iv( &operation,
2501 iv, sizeof( iv ) ) );
2502 PSA_ASSERT( psa_cipher_finish( &operation,
2503 buffer, sizeof( buffer ), &length ) );
2504 TEST_EQUAL( psa_cipher_update( &operation,
2505 text, sizeof( text ),
2506 buffer, sizeof( buffer ),
2507 &length ),
2508 PSA_ERROR_BAD_STATE );
2509 PSA_ASSERT( psa_cipher_abort( &operation ) );
2510
2511 /* Call finish without calling setup beforehand. */
2512 TEST_EQUAL( psa_cipher_finish( &operation,
2513 buffer, sizeof( buffer ), &length ),
2514 PSA_ERROR_BAD_STATE );
2515 PSA_ASSERT( psa_cipher_abort( &operation ) );
2516
2517 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002518 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002519 /* Not calling update means we are encrypting an empty buffer, which is OK
2520 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002521 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002522 TEST_EQUAL( psa_cipher_finish( &operation,
2523 buffer, sizeof( buffer ), &length ),
2524 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002525 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002526 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002527 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002528
2529 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002530 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002531 PSA_ASSERT( psa_cipher_set_iv( &operation,
2532 iv, sizeof( iv ) ) );
2533 PSA_ASSERT( psa_cipher_finish( &operation,
2534 buffer, sizeof( buffer ), &length ) );
2535 TEST_EQUAL( psa_cipher_finish( &operation,
2536 buffer, sizeof( buffer ), &length ),
2537 PSA_ERROR_BAD_STATE );
2538 PSA_ASSERT( psa_cipher_abort( &operation ) );
2539
Ronald Cron5425a212020-08-04 14:58:35 +02002540 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002541
Jaeden Ameroab439972019-02-15 14:12:05 +00002542exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002543 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002544 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002545}
2546/* END_CASE */
2547
2548/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002549void cipher_encrypt_fail( int alg_arg,
2550 int key_type_arg,
2551 data_t *key_data,
2552 data_t *input,
2553 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002554{
Ronald Cron5425a212020-08-04 14:58:35 +02002555 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002556 psa_status_t status;
2557 psa_key_type_t key_type = key_type_arg;
2558 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002559 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002560 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002561 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002562 size_t output_length = 0;
2563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2564
2565 if ( PSA_ERROR_BAD_STATE != expected_status )
2566 {
2567 PSA_ASSERT( psa_crypto_init( ) );
2568
2569 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2570 psa_set_key_algorithm( &attributes, alg );
2571 psa_set_key_type( &attributes, key_type );
2572
2573 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2574 input->len );
2575 ASSERT_ALLOC( output, output_buffer_size );
2576
2577 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2578 &key ) );
2579 }
2580
2581 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2582 output_buffer_size, &output_length );
2583
2584 TEST_EQUAL( status, expected_status );
2585
2586exit:
2587 mbedtls_free( output );
2588 psa_destroy_key( key );
2589 PSA_DONE( );
2590}
2591/* END_CASE */
2592
2593/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002594void cipher_encrypt_alg_without_iv( int alg_arg,
2595 int key_type_arg,
2596 data_t *key_data,
2597 data_t *input,
2598 data_t *expected_output )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002599{
2600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2601 psa_key_type_t key_type = key_type_arg;
2602 psa_algorithm_t alg = alg_arg;
Ronald Cron33c69682021-07-15 09:38:11 +02002603 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2604 uint8_t iv[1] = { 0x5a };
2605 size_t iv_length;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002606 unsigned char *output = NULL;
2607 size_t output_buffer_size = 0;
2608 size_t output_length = 0;
2609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2610
2611 PSA_ASSERT( psa_crypto_init( ) );
2612
2613 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2614 psa_set_key_algorithm( &attributes, alg );
2615 psa_set_key_type( &attributes, key_type );
2616
2617 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2618 ASSERT_ALLOC( output, output_buffer_size );
2619
2620 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2621 &key ) );
2622
Ronald Cron33c69682021-07-15 09:38:11 +02002623 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2624 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2625 PSA_ERROR_BAD_STATE );
2626 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2627 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
2628 &iv_length ),
2629 PSA_ERROR_BAD_STATE );
2630
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002631 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2632 output_buffer_size, &output_length ) );
2633 TEST_ASSERT( output_length <=
2634 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2635 TEST_ASSERT( output_length <=
2636 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2637
2638 ASSERT_COMPARE( expected_output->x, expected_output->len,
2639 output, output_length );
2640exit:
2641 mbedtls_free( output );
2642 psa_destroy_key( key );
2643 PSA_DONE( );
2644}
2645/* END_CASE */
2646
2647/* BEGIN_CASE */
Paul Elliotted33ef12021-07-14 12:31:21 +01002648void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2649{
2650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2651 psa_algorithm_t alg = alg_arg;
2652 psa_key_type_t key_type = key_type_arg;
2653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2654 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2655 psa_status_t status;
2656
2657 PSA_ASSERT( psa_crypto_init( ) );
2658
2659 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2660 psa_set_key_algorithm( &attributes, alg );
2661 psa_set_key_type( &attributes, key_type );
2662
2663 /* Usage of either of these two size macros would cause divide by zero
2664 * with incorrect key types previously. Input length should be irrelevant
2665 * here. */
2666 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2667 0 );
2668 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2669
2670
2671 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2672 &key ) );
2673
2674 /* Should fail due to invalid alg type (to support invalid key type).
2675 * Encrypt or decrypt will end up in the same place. */
2676 status = psa_cipher_encrypt_setup( &operation, key, alg );
2677
2678 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2679
2680exit:
2681 psa_cipher_abort( &operation );
2682 psa_destroy_key( key );
2683 PSA_DONE( );
2684}
2685/* END_CASE */
2686
2687/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002688void cipher_encrypt_validation( int alg_arg,
2689 int key_type_arg,
2690 data_t *key_data,
2691 data_t *input )
2692{
2693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2694 psa_key_type_t key_type = key_type_arg;
2695 psa_algorithm_t alg = alg_arg;
2696 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2697 unsigned char *output1 = NULL;
2698 size_t output1_buffer_size = 0;
2699 size_t output1_length = 0;
2700 unsigned char *output2 = NULL;
2701 size_t output2_buffer_size = 0;
2702 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002704 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002709 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2710 psa_set_key_algorithm( &attributes, alg );
2711 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002712
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002713 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2714 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2715 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2716 ASSERT_ALLOC( output1, output1_buffer_size );
2717 ASSERT_ALLOC( output2, output2_buffer_size );
2718
Ronald Cron5425a212020-08-04 14:58:35 +02002719 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2720 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002721
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002722 /* The one-shot cipher encryption uses generated iv so validating
2723 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002724 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2725 output1_buffer_size, &output1_length ) );
2726 TEST_ASSERT( output1_length <=
2727 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2728 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002729 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002730
2731 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2732 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002733
Gilles Peskine8817f612018-12-18 00:18:46 +01002734 PSA_ASSERT( psa_cipher_update( &operation,
2735 input->x, input->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002736 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002737 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002738 TEST_ASSERT( function_output_length <=
2739 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2740 TEST_ASSERT( function_output_length <=
2741 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002742 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002743
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002744 PSA_ASSERT( psa_cipher_finish( &operation,
2745 output2 + output2_length,
2746 output2_buffer_size - output2_length,
2747 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002748 TEST_ASSERT( function_output_length <=
2749 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2750 TEST_ASSERT( function_output_length <=
2751 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002752 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002753
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002754 PSA_ASSERT( psa_cipher_abort( &operation ) );
2755 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2756 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002757
Gilles Peskine50e586b2018-06-08 14:28:46 +02002758exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002759 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002760 mbedtls_free( output1 );
2761 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002762 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002763 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002764}
2765/* END_CASE */
2766
2767/* BEGIN_CASE */
2768void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002769 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002770 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002771 int first_part_size_arg,
2772 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002773 data_t *expected_output,
2774 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002775{
Ronald Cron5425a212020-08-04 14:58:35 +02002776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002777 psa_key_type_t key_type = key_type_arg;
2778 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002779 psa_status_t status;
2780 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002781 size_t first_part_size = first_part_size_arg;
2782 size_t output1_length = output1_length_arg;
2783 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002784 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002785 size_t output_buffer_size = 0;
2786 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002787 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002788 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002790
Gilles Peskine8817f612018-12-18 00:18:46 +01002791 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002792
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002793 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2794 psa_set_key_algorithm( &attributes, alg );
2795 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002796
Ronald Cron5425a212020-08-04 14:58:35 +02002797 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2798 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002799
Ronald Cron5425a212020-08-04 14:58:35 +02002800 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002801
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002802 if( iv->len > 0 )
2803 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002804 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002805 }
2806
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002807 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2808 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002809 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002810
Gilles Peskinee0866522019-02-19 19:44:00 +01002811 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002812 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2813 output, output_buffer_size,
2814 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002815 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002816 TEST_ASSERT( function_output_length <=
2817 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2818 TEST_ASSERT( function_output_length <=
2819 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002820 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002821
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002822 if( first_part_size < input->len )
2823 {
2824 PSA_ASSERT( psa_cipher_update( &operation,
2825 input->x + first_part_size,
2826 input->len - first_part_size,
2827 ( output_buffer_size == 0 ? NULL :
2828 output + total_output_length ),
2829 output_buffer_size - total_output_length,
2830 &function_output_length ) );
2831 TEST_ASSERT( function_output_length == output2_length );
2832 TEST_ASSERT( function_output_length <=
2833 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2834 alg,
2835 input->len - first_part_size ) );
2836 TEST_ASSERT( function_output_length <=
2837 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2838 total_output_length += function_output_length;
2839 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002840
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002841 status = psa_cipher_finish( &operation,
2842 ( output_buffer_size == 0 ? NULL :
2843 output + total_output_length ),
2844 output_buffer_size - total_output_length,
2845 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002846 TEST_ASSERT( function_output_length <=
2847 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2848 TEST_ASSERT( function_output_length <=
2849 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002850 total_output_length += function_output_length;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002851 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002852
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002853 if( expected_status == PSA_SUCCESS )
2854 {
2855 PSA_ASSERT( psa_cipher_abort( &operation ) );
2856
2857 ASSERT_COMPARE( expected_output->x, expected_output->len,
2858 output, total_output_length );
2859 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002860
2861exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002862 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002863 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002864 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002865 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002866}
2867/* END_CASE */
2868
2869/* BEGIN_CASE */
2870void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002871 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002872 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002873 int first_part_size_arg,
2874 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002875 data_t *expected_output,
2876 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877{
Ronald Cron5425a212020-08-04 14:58:35 +02002878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002879 psa_key_type_t key_type = key_type_arg;
2880 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002881 psa_status_t status;
2882 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002883 size_t first_part_size = first_part_size_arg;
2884 size_t output1_length = output1_length_arg;
2885 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002886 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887 size_t output_buffer_size = 0;
2888 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002889 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002890 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
Gilles Peskine8817f612018-12-18 00:18:46 +01002893 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002894
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2896 psa_set_key_algorithm( &attributes, alg );
2897 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002898
Ronald Cron5425a212020-08-04 14:58:35 +02002899 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2900 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901
Ronald Cron5425a212020-08-04 14:58:35 +02002902 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903
Steven Cooreman177deba2020-09-07 17:14:14 +02002904 if( iv->len > 0 )
2905 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002906 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002907 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002909 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2910 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002911 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002912
Gilles Peskinee0866522019-02-19 19:44:00 +01002913 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_cipher_update( &operation,
2915 input->x, first_part_size,
2916 output, output_buffer_size,
2917 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002918 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002919 TEST_ASSERT( function_output_length <=
2920 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2921 TEST_ASSERT( function_output_length <=
2922 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002923 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002924
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002925 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002926 {
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002927 PSA_ASSERT( psa_cipher_update( &operation,
2928 input->x + first_part_size,
2929 input->len - first_part_size,
2930 ( output_buffer_size == 0 ? NULL :
2931 output + total_output_length ),
2932 output_buffer_size - total_output_length,
2933 &function_output_length ) );
2934 TEST_ASSERT( function_output_length == output2_length );
2935 TEST_ASSERT( function_output_length <=
2936 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2937 alg,
2938 input->len - first_part_size ) );
2939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2941 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002942 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002945 ( output_buffer_size == 0 ? NULL :
2946 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002947 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002949 TEST_ASSERT( function_output_length <=
2950 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2951 TEST_ASSERT( function_output_length <=
2952 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002953 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002954 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002955
2956 if( expected_status == PSA_SUCCESS )
2957 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002958 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002959
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002960 ASSERT_COMPARE( expected_output->x, expected_output->len,
2961 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962 }
2963
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002965 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002966 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002968 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969}
2970/* END_CASE */
2971
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002973void cipher_decrypt_fail( int alg_arg,
2974 int key_type_arg,
2975 data_t *key_data,
2976 data_t *iv,
2977 data_t *input_arg,
2978 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002979{
2980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2981 psa_status_t status;
2982 psa_key_type_t key_type = key_type_arg;
2983 psa_algorithm_t alg = alg_arg;
2984 psa_status_t expected_status = expected_status_arg;
2985 unsigned char *input = NULL;
2986 size_t input_buffer_size = 0;
2987 unsigned char *output = NULL;
2988 size_t output_buffer_size = 0;
2989 size_t output_length = 0;
2990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2991
2992 if ( PSA_ERROR_BAD_STATE != expected_status )
2993 {
2994 PSA_ASSERT( psa_crypto_init( ) );
2995
2996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2997 psa_set_key_algorithm( &attributes, alg );
2998 psa_set_key_type( &attributes, key_type );
2999
3000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3001 &key ) );
3002 }
3003
3004 /* Allocate input buffer and copy the iv and the plaintext */
3005 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3006 if ( input_buffer_size > 0 )
3007 {
3008 ASSERT_ALLOC( input, input_buffer_size );
3009 memcpy( input, iv->x, iv->len );
3010 memcpy( input + iv->len, input_arg->x, input_arg->len );
3011 }
3012
3013 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3014 ASSERT_ALLOC( output, output_buffer_size );
3015
3016 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3017 output_buffer_size, &output_length );
3018 TEST_EQUAL( status, expected_status );
3019
3020exit:
3021 mbedtls_free( input );
3022 mbedtls_free( output );
3023 psa_destroy_key( key );
3024 PSA_DONE( );
3025}
3026/* END_CASE */
3027
3028/* BEGIN_CASE */
3029void cipher_decrypt( int alg_arg,
3030 int key_type_arg,
3031 data_t *key_data,
3032 data_t *iv,
3033 data_t *input_arg,
3034 data_t *expected_output )
3035{
3036 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3037 psa_key_type_t key_type = key_type_arg;
3038 psa_algorithm_t alg = alg_arg;
3039 unsigned char *input = NULL;
3040 size_t input_buffer_size = 0;
3041 unsigned char *output = NULL;
3042 size_t output_buffer_size = 0;
3043 size_t output_length = 0;
3044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3045
3046 PSA_ASSERT( psa_crypto_init( ) );
3047
3048 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3049 psa_set_key_algorithm( &attributes, alg );
3050 psa_set_key_type( &attributes, key_type );
3051
3052 /* Allocate input buffer and copy the iv and the plaintext */
3053 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3054 if ( input_buffer_size > 0 )
3055 {
3056 ASSERT_ALLOC( input, input_buffer_size );
3057 memcpy( input, iv->x, iv->len );
3058 memcpy( input + iv->len, input_arg->x, input_arg->len );
3059 }
3060
3061 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3062 ASSERT_ALLOC( output, output_buffer_size );
3063
3064 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3065 &key ) );
3066
3067 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3068 output_buffer_size, &output_length ) );
3069 TEST_ASSERT( output_length <=
3070 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3071 TEST_ASSERT( output_length <=
3072 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3073
3074 ASSERT_COMPARE( expected_output->x, expected_output->len,
3075 output, output_length );
3076exit:
3077 mbedtls_free( input );
3078 mbedtls_free( output );
3079 psa_destroy_key( key );
3080 PSA_DONE( );
3081}
3082/* END_CASE */
3083
3084/* BEGIN_CASE */
3085void cipher_verify_output( int alg_arg,
3086 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003087 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003088 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003089{
Ronald Cron5425a212020-08-04 14:58:35 +02003090 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003091 psa_key_type_t key_type = key_type_arg;
3092 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003093 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003094 size_t output1_size = 0;
3095 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003096 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003097 size_t output2_size = 0;
3098 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003100
Gilles Peskine8817f612018-12-18 00:18:46 +01003101 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003102
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3104 psa_set_key_algorithm( &attributes, alg );
3105 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003106
Ronald Cron5425a212020-08-04 14:58:35 +02003107 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3108 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003109 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003110 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003111
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003112 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3113 output1, output1_size,
3114 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003115 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003116 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003117 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003118 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003119
3120 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003121 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003122
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003123 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3124 output2, output2_size,
3125 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003126 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003127 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003128 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003129 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003130
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003131 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003132
3133exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003134 mbedtls_free( output1 );
3135 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003136 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003137 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003138}
3139/* END_CASE */
3140
3141/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003142void cipher_verify_output_multipart( int alg_arg,
3143 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003144 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003145 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003146 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003147{
Ronald Cron5425a212020-08-04 14:58:35 +02003148 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003149 psa_key_type_t key_type = key_type_arg;
3150 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003151 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003152 unsigned char iv[16] = {0};
3153 size_t iv_size = 16;
3154 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003155 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003156 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003157 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003158 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003159 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003160 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003161 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003162 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3163 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003165
Gilles Peskine8817f612018-12-18 00:18:46 +01003166 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003167
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003168 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3169 psa_set_key_algorithm( &attributes, alg );
3170 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003171
Ronald Cron5425a212020-08-04 14:58:35 +02003172 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3173 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003174
Ronald Cron5425a212020-08-04 14:58:35 +02003175 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3176 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003177
Steven Cooreman177deba2020-09-07 17:14:14 +02003178 if( alg != PSA_ALG_ECB_NO_PADDING )
3179 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003180 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3181 iv, iv_size,
3182 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003183 }
3184
gabor-mezei-armceface22021-01-21 12:26:17 +01003185 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3186 TEST_ASSERT( output1_buffer_size <=
3187 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003188 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003189
Gilles Peskinee0866522019-02-19 19:44:00 +01003190 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3193 output1, output1_buffer_size,
3194 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003195 TEST_ASSERT( function_output_length <=
3196 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3197 TEST_ASSERT( function_output_length <=
3198 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003199 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_cipher_update( &operation1,
3202 input->x + first_part_size,
3203 input->len - first_part_size,
3204 output1, output1_buffer_size,
3205 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003206 TEST_ASSERT( function_output_length <=
3207 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3208 alg,
3209 input->len - first_part_size ) );
3210 TEST_ASSERT( function_output_length <=
3211 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003212 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003213
Gilles Peskine8817f612018-12-18 00:18:46 +01003214 PSA_ASSERT( psa_cipher_finish( &operation1,
3215 output1 + output1_length,
3216 output1_buffer_size - output1_length,
3217 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003218 TEST_ASSERT( function_output_length <=
3219 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3220 TEST_ASSERT( function_output_length <=
3221 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003222 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003223
Gilles Peskine8817f612018-12-18 00:18:46 +01003224 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003225
Gilles Peskine048b7f02018-06-08 14:20:49 +02003226 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003227 TEST_ASSERT( output2_buffer_size <=
3228 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3229 TEST_ASSERT( output2_buffer_size <=
3230 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003231 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003232
Steven Cooreman177deba2020-09-07 17:14:14 +02003233 if( iv_length > 0 )
3234 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003235 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3236 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003237 }
Moran Pekerded84402018-06-06 16:36:50 +03003238
Gilles Peskine8817f612018-12-18 00:18:46 +01003239 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3240 output2, output2_buffer_size,
3241 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003242 TEST_ASSERT( function_output_length <=
3243 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3244 TEST_ASSERT( function_output_length <=
3245 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003246 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003247
Gilles Peskine8817f612018-12-18 00:18:46 +01003248 PSA_ASSERT( psa_cipher_update( &operation2,
3249 output1 + first_part_size,
3250 output1_length - first_part_size,
3251 output2, output2_buffer_size,
3252 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003253 TEST_ASSERT( function_output_length <=
3254 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3255 alg,
3256 output1_length - first_part_size ) );
3257 TEST_ASSERT( function_output_length <=
3258 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003259 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003260
Gilles Peskine8817f612018-12-18 00:18:46 +01003261 PSA_ASSERT( psa_cipher_finish( &operation2,
3262 output2 + output2_length,
3263 output2_buffer_size - output2_length,
3264 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003265 TEST_ASSERT( function_output_length <=
3266 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3267 TEST_ASSERT( function_output_length <=
3268 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003269 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003270
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003272
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003273 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003274
3275exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003276 psa_cipher_abort( &operation1 );
3277 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003278 mbedtls_free( output1 );
3279 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003280 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003281 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003282}
3283/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003284
Gilles Peskine20035e32018-02-03 22:44:14 +01003285/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003286void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003287 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003288 data_t *nonce,
3289 data_t *additional_data,
3290 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003291 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292{
Ronald Cron5425a212020-08-04 14:58:35 +02003293 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003294 psa_key_type_t key_type = key_type_arg;
3295 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003296 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003297 unsigned char *output_data = NULL;
3298 size_t output_size = 0;
3299 size_t output_length = 0;
3300 unsigned char *output_data2 = NULL;
3301 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003302 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003303 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003305
Gilles Peskine8817f612018-12-18 00:18:46 +01003306 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003308 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3309 psa_set_key_algorithm( &attributes, alg );
3310 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
Gilles Peskine049c7532019-05-15 20:22:09 +02003312 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003313 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003314 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3315 key_bits = psa_get_key_bits( &attributes );
3316
3317 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3318 alg );
3319 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3320 * should be exact. */
3321 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3322 expected_result != PSA_ERROR_NOT_SUPPORTED )
3323 {
3324 TEST_EQUAL( output_size,
3325 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3326 TEST_ASSERT( output_size <=
3327 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3328 }
3329 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003330
Steven Cooremanf49478b2021-02-15 15:19:25 +01003331 status = psa_aead_encrypt( key, alg,
3332 nonce->x, nonce->len,
3333 additional_data->x,
3334 additional_data->len,
3335 input_data->x, input_data->len,
3336 output_data, output_size,
3337 &output_length );
3338
3339 /* If the operation is not supported, just skip and not fail in case the
3340 * encryption involves a common limitation of cryptography hardwares and
3341 * an alternative implementation. */
3342 if( status == PSA_ERROR_NOT_SUPPORTED )
3343 {
3344 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3345 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3346 }
3347
3348 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349
3350 if( PSA_SUCCESS == expected_result )
3351 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003352 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003353
Gilles Peskine003a4a92019-05-14 16:09:40 +02003354 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3355 * should be exact. */
3356 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003357 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003358
gabor-mezei-armceface22021-01-21 12:26:17 +01003359 TEST_ASSERT( input_data->len <=
3360 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3361
Ronald Cron5425a212020-08-04 14:58:35 +02003362 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003363 nonce->x, nonce->len,
3364 additional_data->x,
3365 additional_data->len,
3366 output_data, output_length,
3367 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003368 &output_length2 ),
3369 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003370
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003371 ASSERT_COMPARE( input_data->x, input_data->len,
3372 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003373 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003374
Gilles Peskinea1cac842018-06-11 19:33:02 +02003375exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003376 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377 mbedtls_free( output_data );
3378 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003379 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380}
3381/* END_CASE */
3382
3383/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003384void aead_encrypt( int key_type_arg, data_t *key_data,
3385 int alg_arg,
3386 data_t *nonce,
3387 data_t *additional_data,
3388 data_t *input_data,
3389 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390{
Ronald Cron5425a212020-08-04 14:58:35 +02003391 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003392 psa_key_type_t key_type = key_type_arg;
3393 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003394 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003395 unsigned char *output_data = NULL;
3396 size_t output_size = 0;
3397 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003399 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400
Gilles Peskine8817f612018-12-18 00:18:46 +01003401 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003403 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3404 psa_set_key_algorithm( &attributes, alg );
3405 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406
Gilles Peskine049c7532019-05-15 20:22:09 +02003407 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003408 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003409 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3410 key_bits = psa_get_key_bits( &attributes );
3411
3412 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3413 alg );
3414 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3415 * should be exact. */
3416 TEST_EQUAL( output_size,
3417 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3418 TEST_ASSERT( output_size <=
3419 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3420 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003421
Steven Cooremand588ea12021-01-11 19:36:04 +01003422 status = psa_aead_encrypt( key, alg,
3423 nonce->x, nonce->len,
3424 additional_data->x, additional_data->len,
3425 input_data->x, input_data->len,
3426 output_data, output_size,
3427 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003428
Ronald Cron28a45ed2021-02-09 20:35:42 +01003429 /* If the operation is not supported, just skip and not fail in case the
3430 * encryption involves a common limitation of cryptography hardwares and
3431 * an alternative implementation. */
3432 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003433 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003434 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3435 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003436 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003437
3438 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003439 ASSERT_COMPARE( expected_result->x, expected_result->len,
3440 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003441
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003443 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003445 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446}
3447/* END_CASE */
3448
3449/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003450void aead_decrypt( int key_type_arg, data_t *key_data,
3451 int alg_arg,
3452 data_t *nonce,
3453 data_t *additional_data,
3454 data_t *input_data,
3455 data_t *expected_data,
3456 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003457{
Ronald Cron5425a212020-08-04 14:58:35 +02003458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459 psa_key_type_t key_type = key_type_arg;
3460 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003461 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003462 unsigned char *output_data = NULL;
3463 size_t output_size = 0;
3464 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003466 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003467 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003468
Gilles Peskine8817f612018-12-18 00:18:46 +01003469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003471 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3472 psa_set_key_algorithm( &attributes, alg );
3473 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskine049c7532019-05-15 20:22:09 +02003475 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003476 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003477 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3478 key_bits = psa_get_key_bits( &attributes );
3479
3480 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3481 alg );
3482 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3483 expected_result != PSA_ERROR_NOT_SUPPORTED )
3484 {
3485 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3486 * should be exact. */
3487 TEST_EQUAL( output_size,
3488 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3489 TEST_ASSERT( output_size <=
3490 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3491 }
3492 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003493
Steven Cooremand588ea12021-01-11 19:36:04 +01003494 status = psa_aead_decrypt( key, alg,
3495 nonce->x, nonce->len,
3496 additional_data->x,
3497 additional_data->len,
3498 input_data->x, input_data->len,
3499 output_data, output_size,
3500 &output_length );
3501
Ronald Cron28a45ed2021-02-09 20:35:42 +01003502 /* If the operation is not supported, just skip and not fail in case the
3503 * decryption involves a common limitation of cryptography hardwares and
3504 * an alternative implementation. */
3505 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003506 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003507 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3508 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003509 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003510
3511 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003512
Gilles Peskine2d277862018-06-18 15:41:12 +02003513 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003514 ASSERT_COMPARE( expected_data->x, expected_data->len,
3515 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003516
Gilles Peskinea1cac842018-06-11 19:33:02 +02003517exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003518 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003519 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003520 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003521}
3522/* END_CASE */
3523
3524/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003525void signature_size( int type_arg,
3526 int bits,
3527 int alg_arg,
3528 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003529{
3530 psa_key_type_t type = type_arg;
3531 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003532 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003533
Gilles Peskinefe11b722018-12-18 00:24:04 +01003534 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003535#if defined(MBEDTLS_TEST_DEPRECATED)
3536 TEST_EQUAL( actual_size,
3537 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3538#endif /* MBEDTLS_TEST_DEPRECATED */
3539
Gilles Peskinee59236f2018-01-27 23:32:46 +01003540exit:
3541 ;
3542}
3543/* END_CASE */
3544
3545/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003546void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3547 int alg_arg, data_t *input_data,
3548 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003549{
Ronald Cron5425a212020-08-04 14:58:35 +02003550 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003551 psa_key_type_t key_type = key_type_arg;
3552 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003553 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003554 unsigned char *signature = NULL;
3555 size_t signature_size;
3556 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003558
Gilles Peskine8817f612018-12-18 00:18:46 +01003559 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003560
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003561 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003562 psa_set_key_algorithm( &attributes, alg );
3563 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003564
Gilles Peskine049c7532019-05-15 20:22:09 +02003565 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003566 &key ) );
3567 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003568 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003569
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003570 /* Allocate a buffer which has the size advertized by the
3571 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003572 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003573 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003574 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003575 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003576 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003577
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003578 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003579 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003580 input_data->x, input_data->len,
3581 signature, signature_size,
3582 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003583 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003584 ASSERT_COMPARE( output_data->x, output_data->len,
3585 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003586
Gilles Peskine0627f982019-11-26 19:12:16 +01003587#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003588 memset( signature, 0, signature_size );
3589 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003590 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003591 input_data->x, input_data->len,
3592 signature, signature_size,
3593 &signature_length ) );
3594 ASSERT_COMPARE( output_data->x, output_data->len,
3595 signature, signature_length );
3596#endif /* MBEDTLS_TEST_DEPRECATED */
3597
Gilles Peskine20035e32018-02-03 22:44:14 +01003598exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003599 /*
3600 * Key attributes may have been returned by psa_get_key_attributes()
3601 * thus reset them as required.
3602 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003603 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003604
Ronald Cron5425a212020-08-04 14:58:35 +02003605 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003606 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003607 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003608}
3609/* END_CASE */
3610
3611/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003612void sign_hash_fail( int key_type_arg, data_t *key_data,
3613 int alg_arg, data_t *input_data,
3614 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003615{
Ronald Cron5425a212020-08-04 14:58:35 +02003616 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003617 psa_key_type_t key_type = key_type_arg;
3618 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003619 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003620 psa_status_t actual_status;
3621 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003622 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003623 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003625
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003626 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003627
Gilles Peskine8817f612018-12-18 00:18:46 +01003628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003629
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003631 psa_set_key_algorithm( &attributes, alg );
3632 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003633
Gilles Peskine049c7532019-05-15 20:22:09 +02003634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003635 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003636
Ronald Cron5425a212020-08-04 14:58:35 +02003637 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003638 input_data->x, input_data->len,
3639 signature, signature_size,
3640 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003641 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003642 /* The value of *signature_length is unspecified on error, but
3643 * whatever it is, it should be less than signature_size, so that
3644 * if the caller tries to read *signature_length bytes without
3645 * checking the error code then they don't overflow a buffer. */
3646 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003647
Gilles Peskine895242b2019-11-29 12:15:40 +01003648#if defined(MBEDTLS_TEST_DEPRECATED)
3649 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003650 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003651 input_data->x, input_data->len,
3652 signature, signature_size,
3653 &signature_length ),
3654 expected_status );
3655 TEST_ASSERT( signature_length <= signature_size );
3656#endif /* MBEDTLS_TEST_DEPRECATED */
3657
Gilles Peskine20035e32018-02-03 22:44:14 +01003658exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003659 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003660 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003661 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003662 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003663}
3664/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003665
3666/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003667void sign_verify_hash( int key_type_arg, data_t *key_data,
3668 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003669{
Ronald Cron5425a212020-08-04 14:58:35 +02003670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003671 psa_key_type_t key_type = key_type_arg;
3672 psa_algorithm_t alg = alg_arg;
3673 size_t key_bits;
3674 unsigned char *signature = NULL;
3675 size_t signature_size;
3676 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003678
Gilles Peskine8817f612018-12-18 00:18:46 +01003679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003680
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003682 psa_set_key_algorithm( &attributes, alg );
3683 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003684
Gilles Peskine049c7532019-05-15 20:22:09 +02003685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003686 &key ) );
3687 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003688 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003689
3690 /* Allocate a buffer which has the size advertized by the
3691 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003692 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003693 key_bits, alg );
3694 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003695 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003696 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003697
3698 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003699 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003700 input_data->x, input_data->len,
3701 signature, signature_size,
3702 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003703 /* Check that the signature length looks sensible. */
3704 TEST_ASSERT( signature_length <= signature_size );
3705 TEST_ASSERT( signature_length > 0 );
3706
3707 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003708 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003709 input_data->x, input_data->len,
3710 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003711
3712 if( input_data->len != 0 )
3713 {
3714 /* Flip a bit in the input and verify that the signature is now
3715 * detected as invalid. Flip a bit at the beginning, not at the end,
3716 * because ECDSA may ignore the last few bits of the input. */
3717 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003718 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003719 input_data->x, input_data->len,
3720 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003721 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003722 }
3723
3724exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003725 /*
3726 * Key attributes may have been returned by psa_get_key_attributes()
3727 * thus reset them as required.
3728 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003729 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003730
Ronald Cron5425a212020-08-04 14:58:35 +02003731 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003732 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003733 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003734}
3735/* END_CASE */
3736
3737/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003738void verify_hash( int key_type_arg, data_t *key_data,
3739 int alg_arg, data_t *hash_data,
3740 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003741{
Ronald Cron5425a212020-08-04 14:58:35 +02003742 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003743 psa_key_type_t key_type = key_type_arg;
3744 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003746
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003747 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003748
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003750
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003752 psa_set_key_algorithm( &attributes, alg );
3753 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003754
Gilles Peskine049c7532019-05-15 20:22:09 +02003755 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003756 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003757
Ronald Cron5425a212020-08-04 14:58:35 +02003758 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003759 hash_data->x, hash_data->len,
3760 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003761
3762#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003763 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003764 hash_data->x, hash_data->len,
3765 signature_data->x,
3766 signature_data->len ) );
3767
3768#endif /* MBEDTLS_TEST_DEPRECATED */
3769
itayzafrir5c753392018-05-08 11:18:38 +03003770exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003771 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003772 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003773 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003774}
3775/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003776
3777/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003778void verify_hash_fail( int key_type_arg, data_t *key_data,
3779 int alg_arg, data_t *hash_data,
3780 data_t *signature_data,
3781 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003782{
Ronald Cron5425a212020-08-04 14:58:35 +02003783 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003784 psa_key_type_t key_type = key_type_arg;
3785 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003786 psa_status_t actual_status;
3787 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003788 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003789
Gilles Peskine8817f612018-12-18 00:18:46 +01003790 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003791
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003792 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003793 psa_set_key_algorithm( &attributes, alg );
3794 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003795
Gilles Peskine049c7532019-05-15 20:22:09 +02003796 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003797 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003798
Ronald Cron5425a212020-08-04 14:58:35 +02003799 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003800 hash_data->x, hash_data->len,
3801 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003802 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003803
Gilles Peskine895242b2019-11-29 12:15:40 +01003804#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003805 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003806 hash_data->x, hash_data->len,
3807 signature_data->x, signature_data->len ),
3808 expected_status );
3809#endif /* MBEDTLS_TEST_DEPRECATED */
3810
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003811exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003812 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003813 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003814 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003815}
3816/* END_CASE */
3817
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003818/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003819void sign_message_deterministic( int key_type_arg,
3820 data_t *key_data,
3821 int alg_arg,
3822 data_t *input_data,
3823 data_t *output_data )
3824{
3825 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3826 psa_key_type_t key_type = key_type_arg;
3827 psa_algorithm_t alg = alg_arg;
3828 size_t key_bits;
3829 unsigned char *signature = NULL;
3830 size_t signature_size;
3831 size_t signature_length = 0xdeadbeef;
3832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3833
3834 PSA_ASSERT( psa_crypto_init( ) );
3835
3836 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3837 psa_set_key_algorithm( &attributes, alg );
3838 psa_set_key_type( &attributes, key_type );
3839
3840 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3841 &key ) );
3842 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3843 key_bits = psa_get_key_bits( &attributes );
3844
3845 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3846 TEST_ASSERT( signature_size != 0 );
3847 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3848 ASSERT_ALLOC( signature, signature_size );
3849
3850 PSA_ASSERT( psa_sign_message( key, alg,
3851 input_data->x, input_data->len,
3852 signature, signature_size,
3853 &signature_length ) );
3854
3855 ASSERT_COMPARE( output_data->x, output_data->len,
3856 signature, signature_length );
3857
3858exit:
3859 psa_reset_key_attributes( &attributes );
3860
3861 psa_destroy_key( key );
3862 mbedtls_free( signature );
3863 PSA_DONE( );
3864
3865}
3866/* END_CASE */
3867
3868/* BEGIN_CASE */
3869void sign_message_fail( int key_type_arg,
3870 data_t *key_data,
3871 int alg_arg,
3872 data_t *input_data,
3873 int signature_size_arg,
3874 int expected_status_arg )
3875{
3876 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3877 psa_key_type_t key_type = key_type_arg;
3878 psa_algorithm_t alg = alg_arg;
3879 size_t signature_size = signature_size_arg;
3880 psa_status_t actual_status;
3881 psa_status_t expected_status = expected_status_arg;
3882 unsigned char *signature = NULL;
3883 size_t signature_length = 0xdeadbeef;
3884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3885
3886 ASSERT_ALLOC( signature, signature_size );
3887
3888 PSA_ASSERT( psa_crypto_init( ) );
3889
3890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3891 psa_set_key_algorithm( &attributes, alg );
3892 psa_set_key_type( &attributes, key_type );
3893
3894 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3895 &key ) );
3896
3897 actual_status = psa_sign_message( key, alg,
3898 input_data->x, input_data->len,
3899 signature, signature_size,
3900 &signature_length );
3901 TEST_EQUAL( actual_status, expected_status );
3902 /* The value of *signature_length is unspecified on error, but
3903 * whatever it is, it should be less than signature_size, so that
3904 * if the caller tries to read *signature_length bytes without
3905 * checking the error code then they don't overflow a buffer. */
3906 TEST_ASSERT( signature_length <= signature_size );
3907
3908exit:
3909 psa_reset_key_attributes( &attributes );
3910 psa_destroy_key( key );
3911 mbedtls_free( signature );
3912 PSA_DONE( );
3913}
3914/* END_CASE */
3915
3916/* BEGIN_CASE */
3917void sign_verify_message( int key_type_arg,
3918 data_t *key_data,
3919 int alg_arg,
3920 data_t *input_data )
3921{
3922 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3923 psa_key_type_t key_type = key_type_arg;
3924 psa_algorithm_t alg = alg_arg;
3925 size_t key_bits;
3926 unsigned char *signature = NULL;
3927 size_t signature_size;
3928 size_t signature_length = 0xdeadbeef;
3929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3930
3931 PSA_ASSERT( psa_crypto_init( ) );
3932
3933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3934 PSA_KEY_USAGE_VERIFY_MESSAGE );
3935 psa_set_key_algorithm( &attributes, alg );
3936 psa_set_key_type( &attributes, key_type );
3937
3938 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3939 &key ) );
3940 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3941 key_bits = psa_get_key_bits( &attributes );
3942
3943 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3944 TEST_ASSERT( signature_size != 0 );
3945 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3946 ASSERT_ALLOC( signature, signature_size );
3947
3948 PSA_ASSERT( psa_sign_message( key, alg,
3949 input_data->x, input_data->len,
3950 signature, signature_size,
3951 &signature_length ) );
3952 TEST_ASSERT( signature_length <= signature_size );
3953 TEST_ASSERT( signature_length > 0 );
3954
3955 PSA_ASSERT( psa_verify_message( key, alg,
3956 input_data->x, input_data->len,
3957 signature, signature_length ) );
3958
3959 if( input_data->len != 0 )
3960 {
3961 /* Flip a bit in the input and verify that the signature is now
3962 * detected as invalid. Flip a bit at the beginning, not at the end,
3963 * because ECDSA may ignore the last few bits of the input. */
3964 input_data->x[0] ^= 1;
3965 TEST_EQUAL( psa_verify_message( key, alg,
3966 input_data->x, input_data->len,
3967 signature, signature_length ),
3968 PSA_ERROR_INVALID_SIGNATURE );
3969 }
3970
3971exit:
3972 psa_reset_key_attributes( &attributes );
3973
3974 psa_destroy_key( key );
3975 mbedtls_free( signature );
3976 PSA_DONE( );
3977}
3978/* END_CASE */
3979
3980/* BEGIN_CASE */
3981void verify_message( int key_type_arg,
3982 data_t *key_data,
3983 int alg_arg,
3984 data_t *input_data,
3985 data_t *signature_data )
3986{
3987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3988 psa_key_type_t key_type = key_type_arg;
3989 psa_algorithm_t alg = alg_arg;
3990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3991
3992 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3993
3994 PSA_ASSERT( psa_crypto_init( ) );
3995
3996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3997 psa_set_key_algorithm( &attributes, alg );
3998 psa_set_key_type( &attributes, key_type );
3999
4000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4001 &key ) );
4002
4003 PSA_ASSERT( psa_verify_message( key, alg,
4004 input_data->x, input_data->len,
4005 signature_data->x, signature_data->len ) );
4006
4007exit:
4008 psa_reset_key_attributes( &attributes );
4009 psa_destroy_key( key );
4010 PSA_DONE( );
4011}
4012/* END_CASE */
4013
4014/* BEGIN_CASE */
4015void verify_message_fail( int key_type_arg,
4016 data_t *key_data,
4017 int alg_arg,
4018 data_t *hash_data,
4019 data_t *signature_data,
4020 int expected_status_arg )
4021{
4022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4023 psa_key_type_t key_type = key_type_arg;
4024 psa_algorithm_t alg = alg_arg;
4025 psa_status_t actual_status;
4026 psa_status_t expected_status = expected_status_arg;
4027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4028
4029 PSA_ASSERT( psa_crypto_init( ) );
4030
4031 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4032 psa_set_key_algorithm( &attributes, alg );
4033 psa_set_key_type( &attributes, key_type );
4034
4035 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4036 &key ) );
4037
4038 actual_status = psa_verify_message( key, alg,
4039 hash_data->x, hash_data->len,
4040 signature_data->x,
4041 signature_data->len );
4042 TEST_EQUAL( actual_status, expected_status );
4043
4044exit:
4045 psa_reset_key_attributes( &attributes );
4046 psa_destroy_key( key );
4047 PSA_DONE( );
4048}
4049/* END_CASE */
4050
4051/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004052void asymmetric_encrypt( int key_type_arg,
4053 data_t *key_data,
4054 int alg_arg,
4055 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004056 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004057 int expected_output_length_arg,
4058 int expected_status_arg )
4059{
Ronald Cron5425a212020-08-04 14:58:35 +02004060 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004061 psa_key_type_t key_type = key_type_arg;
4062 psa_algorithm_t alg = alg_arg;
4063 size_t expected_output_length = expected_output_length_arg;
4064 size_t key_bits;
4065 unsigned char *output = NULL;
4066 size_t output_size;
4067 size_t output_length = ~0;
4068 psa_status_t actual_status;
4069 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004071
Gilles Peskine8817f612018-12-18 00:18:46 +01004072 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004073
Gilles Peskine656896e2018-06-29 19:12:28 +02004074 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004075 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4076 psa_set_key_algorithm( &attributes, alg );
4077 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004078 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004079 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004080
4081 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004082 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004083 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004084
Gilles Peskine656896e2018-06-29 19:12:28 +02004085 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004086 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004087 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004088
4089 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004090 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004091 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004092 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004093 output, output_size,
4094 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004095 TEST_EQUAL( actual_status, expected_status );
4096 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004097
Gilles Peskine68428122018-06-30 18:42:41 +02004098 /* If the label is empty, the test framework puts a non-null pointer
4099 * in label->x. Test that a null pointer works as well. */
4100 if( label->len == 0 )
4101 {
4102 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004103 if( output_size != 0 )
4104 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004105 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004106 input_data->x, input_data->len,
4107 NULL, label->len,
4108 output, output_size,
4109 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004110 TEST_EQUAL( actual_status, expected_status );
4111 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004112 }
4113
Gilles Peskine656896e2018-06-29 19:12:28 +02004114exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004115 /*
4116 * Key attributes may have been returned by psa_get_key_attributes()
4117 * thus reset them as required.
4118 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004119 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004120
Ronald Cron5425a212020-08-04 14:58:35 +02004121 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004122 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004123 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004124}
4125/* END_CASE */
4126
4127/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004128void asymmetric_encrypt_decrypt( int key_type_arg,
4129 data_t *key_data,
4130 int alg_arg,
4131 data_t *input_data,
4132 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133{
Ronald Cron5425a212020-08-04 14:58:35 +02004134 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004135 psa_key_type_t key_type = key_type_arg;
4136 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004137 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004138 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004139 size_t output_size;
4140 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004141 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004142 size_t output2_size;
4143 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004145
Gilles Peskine8817f612018-12-18 00:18:46 +01004146 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004147
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004148 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4149 psa_set_key_algorithm( &attributes, alg );
4150 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004151
Gilles Peskine049c7532019-05-15 20:22:09 +02004152 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004153 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004154
4155 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004156 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004157 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004158
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004159 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004160 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004161 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004162
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004163 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004164 TEST_ASSERT( output2_size <=
4165 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4166 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004167 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004168
Gilles Peskineeebd7382018-06-08 18:11:54 +02004169 /* We test encryption by checking that encrypt-then-decrypt gives back
4170 * the original plaintext because of the non-optional random
4171 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004172 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004173 input_data->x, input_data->len,
4174 label->x, label->len,
4175 output, output_size,
4176 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004177 /* We don't know what ciphertext length to expect, but check that
4178 * it looks sensible. */
4179 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004180
Ronald Cron5425a212020-08-04 14:58:35 +02004181 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004182 output, output_length,
4183 label->x, label->len,
4184 output2, output2_size,
4185 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004186 ASSERT_COMPARE( input_data->x, input_data->len,
4187 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004188
4189exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004190 /*
4191 * Key attributes may have been returned by psa_get_key_attributes()
4192 * thus reset them as required.
4193 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004194 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004195
Ronald Cron5425a212020-08-04 14:58:35 +02004196 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004197 mbedtls_free( output );
4198 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004199 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004200}
4201/* END_CASE */
4202
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004203/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004204void asymmetric_decrypt( int key_type_arg,
4205 data_t *key_data,
4206 int alg_arg,
4207 data_t *input_data,
4208 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004209 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004210{
Ronald Cron5425a212020-08-04 14:58:35 +02004211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004212 psa_key_type_t key_type = key_type_arg;
4213 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004214 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004216 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004217 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004218 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004219
Gilles Peskine8817f612018-12-18 00:18:46 +01004220 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004221
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004222 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4223 psa_set_key_algorithm( &attributes, alg );
4224 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004225
Gilles Peskine049c7532019-05-15 20:22:09 +02004226 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004227 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004228
gabor-mezei-armceface22021-01-21 12:26:17 +01004229 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4230 key_bits = psa_get_key_bits( &attributes );
4231
4232 /* Determine the maximum ciphertext length */
4233 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4234 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4235 ASSERT_ALLOC( output, output_size );
4236
Ronald Cron5425a212020-08-04 14:58:35 +02004237 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004238 input_data->x, input_data->len,
4239 label->x, label->len,
4240 output,
4241 output_size,
4242 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004243 ASSERT_COMPARE( expected_data->x, expected_data->len,
4244 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004245
Gilles Peskine68428122018-06-30 18:42:41 +02004246 /* If the label is empty, the test framework puts a non-null pointer
4247 * in label->x. Test that a null pointer works as well. */
4248 if( label->len == 0 )
4249 {
4250 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004251 if( output_size != 0 )
4252 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004253 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004254 input_data->x, input_data->len,
4255 NULL, label->len,
4256 output,
4257 output_size,
4258 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004259 ASSERT_COMPARE( expected_data->x, expected_data->len,
4260 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004261 }
4262
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004263exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004264 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004265 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004266 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004267 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004268}
4269/* END_CASE */
4270
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004271/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004272void asymmetric_decrypt_fail( int key_type_arg,
4273 data_t *key_data,
4274 int alg_arg,
4275 data_t *input_data,
4276 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004277 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004278 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004279{
Ronald Cron5425a212020-08-04 14:58:35 +02004280 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004281 psa_key_type_t key_type = key_type_arg;
4282 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004283 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004284 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004285 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004286 psa_status_t actual_status;
4287 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004289
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004290 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004291
Gilles Peskine8817f612018-12-18 00:18:46 +01004292 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004293
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004294 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4295 psa_set_key_algorithm( &attributes, alg );
4296 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004297
Gilles Peskine049c7532019-05-15 20:22:09 +02004298 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004299 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004300
Ronald Cron5425a212020-08-04 14:58:35 +02004301 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004302 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004303 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004304 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004305 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004306 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004307 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004308
Gilles Peskine68428122018-06-30 18:42:41 +02004309 /* If the label is empty, the test framework puts a non-null pointer
4310 * in label->x. Test that a null pointer works as well. */
4311 if( label->len == 0 )
4312 {
4313 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004314 if( output_size != 0 )
4315 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004316 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004317 input_data->x, input_data->len,
4318 NULL, label->len,
4319 output, output_size,
4320 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004321 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004322 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004323 }
4324
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004325exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004326 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004327 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004328 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004329 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004330}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004331/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004332
4333/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004334void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004335{
4336 /* Test each valid way of initializing the object, except for `= {0}`, as
4337 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4338 * though it's OK by the C standard. We could test for this, but we'd need
4339 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004340 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004341 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4342 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4343 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004344
4345 memset( &zero, 0, sizeof( zero ) );
4346
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004347 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004348 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004349 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004350 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004351 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004352 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004353 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004354
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004355 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004356 PSA_ASSERT( psa_key_derivation_abort(&func) );
4357 PSA_ASSERT( psa_key_derivation_abort(&init) );
4358 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004359}
4360/* END_CASE */
4361
Janos Follath16de4a42019-06-13 16:32:24 +01004362/* BEGIN_CASE */
4363void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004364{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004365 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004366 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004368
Gilles Peskine8817f612018-12-18 00:18:46 +01004369 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004370
Janos Follath16de4a42019-06-13 16:32:24 +01004371 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004372 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004373
4374exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004375 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004376 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004377}
4378/* END_CASE */
4379
Janos Follathaf3c2a02019-06-12 12:34:34 +01004380/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004381void derive_set_capacity( int alg_arg, int capacity_arg,
4382 int expected_status_arg )
4383{
4384 psa_algorithm_t alg = alg_arg;
4385 size_t capacity = capacity_arg;
4386 psa_status_t expected_status = expected_status_arg;
4387 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4388
4389 PSA_ASSERT( psa_crypto_init( ) );
4390
4391 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4392
4393 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4394 expected_status );
4395
4396exit:
4397 psa_key_derivation_abort( &operation );
4398 PSA_DONE( );
4399}
4400/* END_CASE */
4401
4402/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004403void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004404 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004405 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004406 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004407 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004408 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004409 int expected_status_arg3,
4410 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004411{
4412 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004413 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4414 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004415 psa_status_t expected_statuses[] = {expected_status_arg1,
4416 expected_status_arg2,
4417 expected_status_arg3};
4418 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004419 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4420 MBEDTLS_SVC_KEY_ID_INIT,
4421 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004422 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4424 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004425 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004426 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004427 psa_status_t expected_output_status = expected_output_status_arg;
4428 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004429
4430 PSA_ASSERT( psa_crypto_init( ) );
4431
4432 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4433 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004434
4435 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4436
4437 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4438 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004439 mbedtls_test_set_step( i );
4440 if( steps[i] == 0 )
4441 {
4442 /* Skip this step */
4443 }
4444 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004445 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004446 psa_set_key_type( &attributes, key_types[i] );
4447 PSA_ASSERT( psa_import_key( &attributes,
4448 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004449 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004450 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4451 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4452 {
4453 // When taking a private key as secret input, use key agreement
4454 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004455 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4456 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004457 expected_statuses[i] );
4458 }
4459 else
4460 {
4461 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004462 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004463 expected_statuses[i] );
4464 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004465 }
4466 else
4467 {
4468 TEST_EQUAL( psa_key_derivation_input_bytes(
4469 &operation, steps[i],
4470 inputs[i]->x, inputs[i]->len ),
4471 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004472 }
4473 }
4474
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004475 if( output_key_type != PSA_KEY_TYPE_NONE )
4476 {
4477 psa_reset_key_attributes( &attributes );
Dave Rodgmandc4e4b72021-11-16 12:12:49 +00004478 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004479 psa_set_key_bits( &attributes, 8 );
4480 actual_output_status =
4481 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004482 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004483 }
4484 else
4485 {
4486 uint8_t buffer[1];
4487 actual_output_status =
4488 psa_key_derivation_output_bytes( &operation,
4489 buffer, sizeof( buffer ) );
4490 }
4491 TEST_EQUAL( actual_output_status, expected_output_status );
4492
Janos Follathaf3c2a02019-06-12 12:34:34 +01004493exit:
4494 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004495 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4496 psa_destroy_key( keys[i] );
4497 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004498 PSA_DONE( );
4499}
4500/* END_CASE */
4501
Janos Follathd958bb72019-07-03 15:02:16 +01004502/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004503void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004504{
Janos Follathd958bb72019-07-03 15:02:16 +01004505 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004506 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004507 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004508 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004509 unsigned char input1[] = "Input 1";
4510 size_t input1_length = sizeof( input1 );
4511 unsigned char input2[] = "Input 2";
4512 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004513 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004514 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004515 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4516 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4517 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004518 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004519
Gilles Peskine8817f612018-12-18 00:18:46 +01004520 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004521
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004522 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4523 psa_set_key_algorithm( &attributes, alg );
4524 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004525
Gilles Peskine73676cb2019-05-15 20:15:10 +02004526 PSA_ASSERT( psa_import_key( &attributes,
4527 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004528 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004529
4530 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004531 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4532 input1, input1_length,
4533 input2, input2_length,
4534 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004535 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004536
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004538 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004539 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004540
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004542
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004543 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004544 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004545
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004546exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004547 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004548 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004549 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004550}
4551/* END_CASE */
4552
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004553/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004554void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004555{
4556 uint8_t output_buffer[16];
4557 size_t buffer_size = 16;
4558 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004560
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004561 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4562 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004563 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004564
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004565 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004566 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004567
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004568 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004569
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004570 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4571 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004572 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004573
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004574 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004575 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004576
4577exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004578 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004579}
4580/* END_CASE */
4581
4582/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004583void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004584 int step1_arg, data_t *input1,
4585 int step2_arg, data_t *input2,
4586 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004587 int requested_capacity_arg,
4588 data_t *expected_output1,
4589 data_t *expected_output2 )
4590{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004591 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004592 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4593 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004594 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4595 MBEDTLS_SVC_KEY_ID_INIT,
4596 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004597 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004598 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004599 uint8_t *expected_outputs[2] =
4600 {expected_output1->x, expected_output2->x};
4601 size_t output_sizes[2] =
4602 {expected_output1->len, expected_output2->len};
4603 size_t output_buffer_size = 0;
4604 uint8_t *output_buffer = NULL;
4605 size_t expected_capacity;
4606 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004608 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004609 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004610
4611 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4612 {
4613 if( output_sizes[i] > output_buffer_size )
4614 output_buffer_size = output_sizes[i];
4615 if( output_sizes[i] == 0 )
4616 expected_outputs[i] = NULL;
4617 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004618 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004619 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004620
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004621 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4622 psa_set_key_algorithm( &attributes, alg );
4623 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004624
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004625 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004626 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4627 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4628 requested_capacity ) );
4629 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004630 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004631 switch( steps[i] )
4632 {
4633 case 0:
4634 break;
4635 case PSA_KEY_DERIVATION_INPUT_SECRET:
4636 PSA_ASSERT( psa_import_key( &attributes,
4637 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004638 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004639
4640 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4641 {
4642 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4643 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4644 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4645 }
4646
Gilles Peskine1468da72019-05-29 17:35:49 +02004647 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004648 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004649 break;
4650 default:
4651 PSA_ASSERT( psa_key_derivation_input_bytes(
4652 &operation, steps[i],
4653 inputs[i]->x, inputs[i]->len ) );
4654 break;
4655 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004656 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004657
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004659 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004660 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004661 expected_capacity = requested_capacity;
4662
4663 /* Expansion phase. */
4664 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4665 {
4666 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004668 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004669 if( expected_capacity == 0 && output_sizes[i] == 0 )
4670 {
4671 /* Reading 0 bytes when 0 bytes are available can go either way. */
4672 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004673 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004674 continue;
4675 }
4676 else if( expected_capacity == 0 ||
4677 output_sizes[i] > expected_capacity )
4678 {
4679 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004680 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004681 expected_capacity = 0;
4682 continue;
4683 }
4684 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004685 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004686 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004687 ASSERT_COMPARE( output_buffer, output_sizes[i],
4688 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004690 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004691 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004692 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004693 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004694 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004695 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004696
4697exit:
4698 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004700 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4701 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004702 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004703}
4704/* END_CASE */
4705
4706/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004707void derive_full( int alg_arg,
4708 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004709 data_t *input1,
4710 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004711 int requested_capacity_arg )
4712{
Ronald Cron5425a212020-08-04 14:58:35 +02004713 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004714 psa_algorithm_t alg = alg_arg;
4715 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004716 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004717 unsigned char output_buffer[16];
4718 size_t expected_capacity = requested_capacity;
4719 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004721
Gilles Peskine8817f612018-12-18 00:18:46 +01004722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004723
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4725 psa_set_key_algorithm( &attributes, alg );
4726 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004727
Gilles Peskine049c7532019-05-15 20:22:09 +02004728 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004729 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004730
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004731 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4732 input1->x, input1->len,
4733 input2->x, input2->len,
4734 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004735 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004736
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004738 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004739 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004740
4741 /* Expansion phase. */
4742 while( current_capacity > 0 )
4743 {
4744 size_t read_size = sizeof( output_buffer );
4745 if( read_size > current_capacity )
4746 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004748 output_buffer,
4749 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004750 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004752 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004753 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004754 }
4755
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756 /* Check that the operation refuses to go over capacity. */
4757 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004758 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004759
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004761
4762exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004764 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004765 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004766}
4767/* END_CASE */
4768
Janos Follathe60c9052019-07-03 13:51:30 +01004769/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004770void derive_key_exercise( int alg_arg,
4771 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004772 data_t *input1,
4773 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004774 int derived_type_arg,
4775 int derived_bits_arg,
4776 int derived_usage_arg,
4777 int derived_alg_arg )
4778{
Ronald Cron5425a212020-08-04 14:58:35 +02004779 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4780 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004781 psa_algorithm_t alg = alg_arg;
4782 psa_key_type_t derived_type = derived_type_arg;
4783 size_t derived_bits = derived_bits_arg;
4784 psa_key_usage_t derived_usage = derived_usage_arg;
4785 psa_algorithm_t derived_alg = derived_alg_arg;
4786 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004788 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004789 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004790
Gilles Peskine8817f612018-12-18 00:18:46 +01004791 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004792
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004793 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4794 psa_set_key_algorithm( &attributes, alg );
4795 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004796 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004797 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004798
4799 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004800 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4801 input1->x, input1->len,
4802 input2->x, input2->len,
4803 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004804 goto exit;
4805
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004806 psa_set_key_usage_flags( &attributes, derived_usage );
4807 psa_set_key_algorithm( &attributes, derived_alg );
4808 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004809 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004811 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004812
4813 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004814 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004815 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4816 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004817
4818 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004819 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004820 goto exit;
4821
4822exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004823 /*
4824 * Key attributes may have been returned by psa_get_key_attributes()
4825 * thus reset them as required.
4826 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004827 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004828
4829 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004830 psa_destroy_key( base_key );
4831 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004832 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004833}
4834/* END_CASE */
4835
Janos Follath42fd8882019-07-03 14:17:09 +01004836/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004837void derive_key_export( int alg_arg,
4838 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004839 data_t *input1,
4840 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004841 int bytes1_arg,
4842 int bytes2_arg )
4843{
Ronald Cron5425a212020-08-04 14:58:35 +02004844 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4845 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004846 psa_algorithm_t alg = alg_arg;
4847 size_t bytes1 = bytes1_arg;
4848 size_t bytes2 = bytes2_arg;
4849 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004851 uint8_t *output_buffer = NULL;
4852 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004853 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4854 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004855 size_t length;
4856
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004857 ASSERT_ALLOC( output_buffer, capacity );
4858 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004859 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004860
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004861 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4862 psa_set_key_algorithm( &base_attributes, alg );
4863 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004864 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004865 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004866
4867 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004868 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4869 input1->x, input1->len,
4870 input2->x, input2->len,
4871 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004872 goto exit;
4873
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004874 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004875 output_buffer,
4876 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004877 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004878
4879 /* Derive the same output again, but this time store it in key objects. */
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 Peskineff5f0e72019-04-18 12:53:30 +02004886 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4887 psa_set_key_algorithm( &derived_attributes, 0 );
4888 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004889 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004891 &derived_key ) );
4892 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004893 export_buffer, bytes1,
4894 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004895 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004896 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004897 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004899 &derived_key ) );
4900 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004901 export_buffer + bytes1, bytes2,
4902 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004903 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004904
4905 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004906 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4907 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004908
4909exit:
4910 mbedtls_free( output_buffer );
4911 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004913 psa_destroy_key( base_key );
4914 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004915 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004916}
4917/* END_CASE */
4918
4919/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004920void derive_key( int alg_arg,
4921 data_t *key_data, data_t *input1, data_t *input2,
4922 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004923 int expected_status_arg,
4924 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004925{
Ronald Cron5425a212020-08-04 14:58:35 +02004926 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4927 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004928 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004929 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004930 size_t bits = bits_arg;
4931 psa_status_t expected_status = expected_status_arg;
4932 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4933 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4934 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4935
4936 PSA_ASSERT( psa_crypto_init( ) );
4937
4938 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4939 psa_set_key_algorithm( &base_attributes, alg );
4940 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4941 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004942 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004943
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004944 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4945 input1->x, input1->len,
4946 input2->x, input2->len,
4947 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004948 goto exit;
4949
4950 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4951 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004952 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004953 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004954
4955 psa_status_t status =
4956 psa_key_derivation_output_key( &derived_attributes,
4957 &operation,
4958 &derived_key );
4959 if( is_large_output > 0 )
4960 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4961 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004962
4963exit:
4964 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004965 psa_destroy_key( base_key );
4966 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004967 PSA_DONE( );
4968}
4969/* END_CASE */
4970
4971/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004972void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004973 int our_key_type_arg, int our_key_alg_arg,
4974 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004975 int expected_status_arg )
4976{
Ronald Cron5425a212020-08-04 14:58:35 +02004977 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004978 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004979 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004980 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004983 psa_status_t expected_status = expected_status_arg;
4984 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004985
Gilles Peskine8817f612018-12-18 00:18:46 +01004986 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004987
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004988 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004989 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004990 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004991 PSA_ASSERT( psa_import_key( &attributes,
4992 our_key_data->x, our_key_data->len,
4993 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004994
Gilles Peskine77f40d82019-04-11 21:27:06 +02004995 /* The tests currently include inputs that should fail at either step.
4996 * Test cases that fail at the setup step should be changed to call
4997 * key_derivation_setup instead, and this function should be renamed
4998 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004999 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005000 if( status == PSA_SUCCESS )
5001 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005002 TEST_EQUAL( psa_key_derivation_key_agreement(
5003 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5004 our_key,
5005 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005006 expected_status );
5007 }
5008 else
5009 {
5010 TEST_ASSERT( status == expected_status );
5011 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005012
5013exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005014 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005015 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005016 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005017}
5018/* END_CASE */
5019
5020/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005021void raw_key_agreement( int alg_arg,
5022 int our_key_type_arg, data_t *our_key_data,
5023 data_t *peer_key_data,
5024 data_t *expected_output )
5025{
Ronald Cron5425a212020-08-04 14:58:35 +02005026 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005027 psa_algorithm_t alg = alg_arg;
5028 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005030 unsigned char *output = NULL;
5031 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005032 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005033
5034 ASSERT_ALLOC( output, expected_output->len );
5035 PSA_ASSERT( psa_crypto_init( ) );
5036
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005037 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5038 psa_set_key_algorithm( &attributes, alg );
5039 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005040 PSA_ASSERT( psa_import_key( &attributes,
5041 our_key_data->x, our_key_data->len,
5042 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005043
gabor-mezei-armceface22021-01-21 12:26:17 +01005044 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5045 key_bits = psa_get_key_bits( &attributes );
5046
Gilles Peskinebe697d82019-05-16 18:00:41 +02005047 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5048 peer_key_data->x, peer_key_data->len,
5049 output, expected_output->len,
5050 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005051 ASSERT_COMPARE( output, output_length,
5052 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005053 TEST_ASSERT( output_length <=
5054 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5055 TEST_ASSERT( output_length <=
5056 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005057
5058exit:
5059 mbedtls_free( output );
5060 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005061 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005062}
5063/* END_CASE */
5064
5065/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005066void key_agreement_capacity( int alg_arg,
5067 int our_key_type_arg, data_t *our_key_data,
5068 data_t *peer_key_data,
5069 int expected_capacity_arg )
5070{
Ronald Cron5425a212020-08-04 14:58:35 +02005071 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005072 psa_algorithm_t alg = alg_arg;
5073 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005074 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005076 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005077 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005078
Gilles Peskine8817f612018-12-18 00:18:46 +01005079 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005080
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005081 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5082 psa_set_key_algorithm( &attributes, alg );
5083 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005084 PSA_ASSERT( psa_import_key( &attributes,
5085 our_key_data->x, our_key_data->len,
5086 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005087
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005088 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005089 PSA_ASSERT( psa_key_derivation_key_agreement(
5090 &operation,
5091 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5092 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005093 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5094 {
5095 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005096 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005097 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005098 NULL, 0 ) );
5099 }
Gilles Peskine59685592018-09-18 12:11:34 +02005100
Gilles Peskinebf491972018-10-25 22:36:12 +02005101 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005102 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005104 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005105
Gilles Peskinebf491972018-10-25 22:36:12 +02005106 /* Test the actual capacity by reading the output. */
5107 while( actual_capacity > sizeof( output ) )
5108 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005109 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005110 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005111 actual_capacity -= sizeof( output );
5112 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005114 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005116 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005117
Gilles Peskine59685592018-09-18 12:11:34 +02005118exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005119 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005120 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005121 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005122}
5123/* END_CASE */
5124
5125/* BEGIN_CASE */
5126void key_agreement_output( int alg_arg,
5127 int our_key_type_arg, data_t *our_key_data,
5128 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005129 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005130{
Ronald Cron5425a212020-08-04 14:58:35 +02005131 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005132 psa_algorithm_t alg = alg_arg;
5133 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005136 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005137
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005138 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5139 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005140
Gilles Peskine8817f612018-12-18 00:18:46 +01005141 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005142
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005143 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5144 psa_set_key_algorithm( &attributes, alg );
5145 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005146 PSA_ASSERT( psa_import_key( &attributes,
5147 our_key_data->x, our_key_data->len,
5148 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005149
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005151 PSA_ASSERT( psa_key_derivation_key_agreement(
5152 &operation,
5153 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5154 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005155 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5156 {
5157 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005158 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005159 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005160 NULL, 0 ) );
5161 }
Gilles Peskine59685592018-09-18 12:11:34 +02005162
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005163 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005164 actual_output,
5165 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005166 ASSERT_COMPARE( actual_output, expected_output1->len,
5167 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005168 if( expected_output2->len != 0 )
5169 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005170 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005171 actual_output,
5172 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005173 ASSERT_COMPARE( actual_output, expected_output2->len,
5174 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005175 }
Gilles Peskine59685592018-09-18 12:11:34 +02005176
5177exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005179 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005180 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005181 mbedtls_free( actual_output );
5182}
5183/* END_CASE */
5184
5185/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005186void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005187{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005188 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005189 unsigned char *output = NULL;
5190 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005191 size_t i;
5192 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005193
Simon Butcher49f8e312020-03-03 15:51:50 +00005194 TEST_ASSERT( bytes_arg >= 0 );
5195
Gilles Peskine91892022021-02-08 19:50:26 +01005196 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005197 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005198
Gilles Peskine8817f612018-12-18 00:18:46 +01005199 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005200
Gilles Peskinea50d7392018-06-21 10:22:13 +02005201 /* Run several times, to ensure that every output byte will be
5202 * nonzero at least once with overwhelming probability
5203 * (2^(-8*number_of_runs)). */
5204 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005205 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005206 if( bytes != 0 )
5207 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005208 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005209
Gilles Peskinea50d7392018-06-21 10:22:13 +02005210 for( i = 0; i < bytes; i++ )
5211 {
5212 if( output[i] != 0 )
5213 ++changed[i];
5214 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005215 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005216
5217 /* Check that every byte was changed to nonzero at least once. This
5218 * validates that psa_generate_random is overwriting every byte of
5219 * the output buffer. */
5220 for( i = 0; i < bytes; i++ )
5221 {
5222 TEST_ASSERT( changed[i] != 0 );
5223 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005224
5225exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005226 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005227 mbedtls_free( output );
5228 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005229}
5230/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005231
5232/* BEGIN_CASE */
5233void generate_key( int type_arg,
5234 int bits_arg,
5235 int usage_arg,
5236 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005237 int expected_status_arg,
5238 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005239{
Ronald Cron5425a212020-08-04 14:58:35 +02005240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005241 psa_key_type_t type = type_arg;
5242 psa_key_usage_t usage = usage_arg;
5243 size_t bits = bits_arg;
5244 psa_algorithm_t alg = alg_arg;
5245 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005247 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005248
Gilles Peskine8817f612018-12-18 00:18:46 +01005249 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005250
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005251 psa_set_key_usage_flags( &attributes, usage );
5252 psa_set_key_algorithm( &attributes, alg );
5253 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005254 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005255
5256 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005257 psa_status_t status = psa_generate_key( &attributes, &key );
5258
5259 if( is_large_key > 0 )
5260 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5261 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005262 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005263 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005264
5265 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005266 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005267 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5268 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005269
Gilles Peskine818ca122018-06-20 18:16:48 +02005270 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005271 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005272 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005273
5274exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005275 /*
5276 * Key attributes may have been returned by psa_get_key_attributes()
5277 * thus reset them as required.
5278 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005279 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005280
Ronald Cron5425a212020-08-04 14:58:35 +02005281 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005282 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005283}
5284/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005285
Ronald Cronee414c72021-03-18 18:50:08 +01005286/* 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 +02005287void generate_key_rsa( int bits_arg,
5288 data_t *e_arg,
5289 int expected_status_arg )
5290{
Ronald Cron5425a212020-08-04 14:58:35 +02005291 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005292 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005293 size_t bits = bits_arg;
5294 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5295 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5296 psa_status_t expected_status = expected_status_arg;
5297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5298 uint8_t *exported = NULL;
5299 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005300 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005301 size_t exported_length = SIZE_MAX;
5302 uint8_t *e_read_buffer = NULL;
5303 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005304 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005305 size_t e_read_length = SIZE_MAX;
5306
5307 if( e_arg->len == 0 ||
5308 ( e_arg->len == 3 &&
5309 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5310 {
5311 is_default_public_exponent = 1;
5312 e_read_size = 0;
5313 }
5314 ASSERT_ALLOC( e_read_buffer, e_read_size );
5315 ASSERT_ALLOC( exported, exported_size );
5316
5317 PSA_ASSERT( psa_crypto_init( ) );
5318
5319 psa_set_key_usage_flags( &attributes, usage );
5320 psa_set_key_algorithm( &attributes, alg );
5321 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5322 e_arg->x, e_arg->len ) );
5323 psa_set_key_bits( &attributes, bits );
5324
5325 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005326 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005327 if( expected_status != PSA_SUCCESS )
5328 goto exit;
5329
5330 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005331 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005332 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5333 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5334 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5335 e_read_buffer, e_read_size,
5336 &e_read_length ) );
5337 if( is_default_public_exponent )
5338 TEST_EQUAL( e_read_length, 0 );
5339 else
5340 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5341
5342 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005343 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005344 goto exit;
5345
5346 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005347 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005348 exported, exported_size,
5349 &exported_length ) );
5350 {
5351 uint8_t *p = exported;
5352 uint8_t *end = exported + exported_length;
5353 size_t len;
5354 /* RSAPublicKey ::= SEQUENCE {
5355 * modulus INTEGER, -- n
5356 * publicExponent INTEGER } -- e
5357 */
5358 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005359 MBEDTLS_ASN1_SEQUENCE |
5360 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005361 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005362 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5363 MBEDTLS_ASN1_INTEGER ) );
5364 if( len >= 1 && p[0] == 0 )
5365 {
5366 ++p;
5367 --len;
5368 }
5369 if( e_arg->len == 0 )
5370 {
5371 TEST_EQUAL( len, 3 );
5372 TEST_EQUAL( p[0], 1 );
5373 TEST_EQUAL( p[1], 0 );
5374 TEST_EQUAL( p[2], 1 );
5375 }
5376 else
5377 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5378 }
5379
5380exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005381 /*
5382 * Key attributes may have been returned by psa_get_key_attributes() or
5383 * set by psa_set_key_domain_parameters() thus reset them as required.
5384 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005385 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005386
Ronald Cron5425a212020-08-04 14:58:35 +02005387 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005388 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005389 mbedtls_free( e_read_buffer );
5390 mbedtls_free( exported );
5391}
5392/* END_CASE */
5393
Darryl Greend49a4992018-06-18 17:27:26 +01005394/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005395void persistent_key_load_key_from_storage( data_t *data,
5396 int type_arg, int bits_arg,
5397 int usage_flags_arg, int alg_arg,
5398 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005399{
Ronald Cron71016a92020-08-28 19:01:50 +02005400 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5403 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005404 psa_key_type_t type = type_arg;
5405 size_t bits = bits_arg;
5406 psa_key_usage_t usage_flags = usage_flags_arg;
5407 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005408 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005409 unsigned char *first_export = NULL;
5410 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005411 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005412 size_t first_exported_length;
5413 size_t second_exported_length;
5414
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005415 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5416 {
5417 ASSERT_ALLOC( first_export, export_size );
5418 ASSERT_ALLOC( second_export, export_size );
5419 }
Darryl Greend49a4992018-06-18 17:27:26 +01005420
Gilles Peskine8817f612018-12-18 00:18:46 +01005421 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005422
Gilles Peskinec87af662019-05-15 16:12:22 +02005423 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005424 psa_set_key_usage_flags( &attributes, usage_flags );
5425 psa_set_key_algorithm( &attributes, alg );
5426 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005427 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005428
Darryl Green0c6575a2018-11-07 16:05:30 +00005429 switch( generation_method )
5430 {
5431 case IMPORT_KEY:
5432 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005433 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005434 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005435 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005436
Darryl Green0c6575a2018-11-07 16:05:30 +00005437 case GENERATE_KEY:
5438 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005439 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005440 break;
5441
5442 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005443#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005444 {
5445 /* Create base key */
5446 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5447 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5448 psa_set_key_usage_flags( &base_attributes,
5449 PSA_KEY_USAGE_DERIVE );
5450 psa_set_key_algorithm( &base_attributes, derive_alg );
5451 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005452 PSA_ASSERT( psa_import_key( &base_attributes,
5453 data->x, data->len,
5454 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005455 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005456 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005457 PSA_ASSERT( psa_key_derivation_input_key(
5458 &operation,
5459 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005460 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005461 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005462 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005463 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5464 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005465 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005467 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005468 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005469 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005470#else
5471 TEST_ASSUME( ! "KDF not supported in this configuration" );
5472#endif
5473 break;
5474
5475 default:
5476 TEST_ASSERT( ! "generation_method not implemented in test" );
5477 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005478 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005479 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005480
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005481 /* Export the key if permitted by the key policy. */
5482 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5483 {
Ronald Cron5425a212020-08-04 14:58:35 +02005484 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005485 first_export, export_size,
5486 &first_exported_length ) );
5487 if( generation_method == IMPORT_KEY )
5488 ASSERT_COMPARE( data->x, data->len,
5489 first_export, first_exported_length );
5490 }
Darryl Greend49a4992018-06-18 17:27:26 +01005491
5492 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005493 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005494 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005495 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005496
Darryl Greend49a4992018-06-18 17:27:26 +01005497 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005498 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005499 TEST_ASSERT( mbedtls_svc_key_id_equal(
5500 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005501 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5502 PSA_KEY_LIFETIME_PERSISTENT );
5503 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5504 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005505 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005506 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005507 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005508
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005509 /* Export the key again if permitted by the key policy. */
5510 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005511 {
Ronald Cron5425a212020-08-04 14:58:35 +02005512 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005513 second_export, export_size,
5514 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005515 ASSERT_COMPARE( first_export, first_exported_length,
5516 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005517 }
5518
5519 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005520 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005521 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005522
5523exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005524 /*
5525 * Key attributes may have been returned by psa_get_key_attributes()
5526 * thus reset them as required.
5527 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005528 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005529
Darryl Greend49a4992018-06-18 17:27:26 +01005530 mbedtls_free( first_export );
5531 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005532 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005533 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005534 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005535 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005536}
5537/* END_CASE */