blob: 20d3102e61b7d2f86670c95b628710f82e7c5c0f [file] [log] [blame]
Gilles Peskine66e7b902021-02-12 23:40:58 +01001/** Code to exercise a PSA key object, i.e. validate that it seems well-formed
2 * and can do what it is supposed to do.
3 */
4
5/*
6 * Copyright The Mbed TLS Contributors
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22#include <test/helpers.h>
23#include <test/macros.h>
Gilles Peskinee78b0022021-02-13 00:41:11 +010024#include <test/psa_exercise_key.h>
Gilles Peskine66e7b902021-02-12 23:40:58 +010025
26#if defined(MBEDTLS_PSA_CRYPTO_C)
27
Gilles Peskinee78b0022021-02-13 00:41:11 +010028#include <mbedtls/asn1.h>
Gilles Peskine66e7b902021-02-12 23:40:58 +010029#include <psa/crypto.h>
30
Gilles Peskinee78b0022021-02-13 00:41:11 +010031#include <test/asn1_helpers.h>
Przemyslaw Stekiel53de2622021-11-03 09:35:35 +010032#include <psa_crypto_slot_management.h>
Gilles Peskinee78b0022021-02-13 00:41:11 +010033#include <test/psa_crypto_helpers.h>
34
35#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
36static int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
37{
38 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
39 PSA_KEY_LOCATION_LOCAL_STORAGE );
40}
41#endif
42
43static int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
44{
45 int ok = 0;
46 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
47 psa_key_lifetime_t lifetime;
48 mbedtls_svc_key_id_t id;
49 psa_key_type_t type;
Gilles Peskine6b362e62021-02-15 12:03:16 +010050 size_t bits;
Gilles Peskinee78b0022021-02-13 00:41:11 +010051
52 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
53 lifetime = psa_get_key_lifetime( &attributes );
54 id = psa_get_key_id( &attributes );
55 type = psa_get_key_type( &attributes );
56 bits = psa_get_key_bits( &attributes );
57
58 /* Persistence */
59 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
60 {
61 TEST_ASSERT(
62 ( PSA_KEY_ID_VOLATILE_MIN <=
63 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
64 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
65 PSA_KEY_ID_VOLATILE_MAX ) );
66 }
67 else
68 {
69 TEST_ASSERT(
70 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
71 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
72 }
73#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
74 /* randomly-generated 64-bit constant, should never appear in test data */
75 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
76 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
77 if( lifetime_is_dynamic_secure_element( lifetime ) )
78 {
79 /* Mbed Crypto currently always exposes the slot number to
80 * applications. This is not mandated by the PSA specification
81 * and may change in future versions. */
82 TEST_EQUAL( status, 0 );
83 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
84 }
85 else
86 {
87 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
88 }
89#endif
90
91 /* Type and size */
92 TEST_ASSERT( type != 0 );
93 TEST_ASSERT( bits != 0 );
94 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
95 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
96 TEST_ASSERT( bits % 8 == 0 );
97
98 /* MAX macros concerning specific key types */
99 if( PSA_KEY_TYPE_IS_ECC( type ) )
100 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
101 else if( PSA_KEY_TYPE_IS_RSA( type ) )
102 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
103 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
104
105 ok = 1;
106
107exit:
108 /*
109 * Key attributes may have been returned by psa_get_key_attributes()
110 * thus reset them as required.
111 */
112 psa_reset_key_attributes( &attributes );
113
114 return( ok );
115}
116
117static int exercise_mac_key( mbedtls_svc_key_id_t key,
118 psa_key_usage_t usage,
119 psa_algorithm_t alg )
120{
121 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
122 const unsigned char input[] = "foo";
123 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
124 size_t mac_length = sizeof( mac );
125
Steven Cooremanfb9cb922021-02-23 14:37:38 +0100126 /* Convert wildcard algorithm to exercisable algorithm */
127 if( alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG )
128 {
129 alg = PSA_ALG_TRUNCATED_MAC( alg, PSA_MAC_TRUNCATED_LENGTH( alg ) );
130 }
131
Gilles Peskinee78b0022021-02-13 00:41:11 +0100132 if( usage & PSA_KEY_USAGE_SIGN_HASH )
133 {
134 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
135 PSA_ASSERT( psa_mac_update( &operation,
136 input, sizeof( input ) ) );
137 PSA_ASSERT( psa_mac_sign_finish( &operation,
138 mac, sizeof( mac ),
139 &mac_length ) );
140 }
141
142 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
143 {
144 psa_status_t verify_status =
145 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
146 PSA_SUCCESS :
147 PSA_ERROR_INVALID_SIGNATURE );
148 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
149 PSA_ASSERT( psa_mac_update( &operation,
150 input, sizeof( input ) ) );
151 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
152 verify_status );
153 }
154
155 return( 1 );
156
157exit:
158 psa_mac_abort( &operation );
159 return( 0 );
160}
161
162static int exercise_cipher_key( mbedtls_svc_key_id_t key,
163 psa_key_usage_t usage,
164 psa_algorithm_t alg )
165{
166 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskineb29d8142022-04-21 11:14:52 +0200167 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
Gilles Peskine5eef11a2022-04-21 11:14:30 +0200168 size_t iv_length;
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
170 psa_key_type_t key_type;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100177 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
178 key_type = psa_get_key_type( &attributes );
179 iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
180
Gilles Peskinee78b0022021-02-13 00:41:11 +0100181 if( usage & PSA_KEY_USAGE_ENCRYPT )
182 {
183 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskinef96e9772022-04-05 16:32:07 +0200184 if( iv_length != 0 )
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100185 {
186 PSA_ASSERT( psa_cipher_generate_iv( &operation,
187 iv, sizeof( iv ),
188 &iv_length ) );
189 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100190 PSA_ASSERT( psa_cipher_update( &operation,
191 plaintext, sizeof( plaintext ),
192 ciphertext, sizeof( ciphertext ),
193 &ciphertext_length ) );
194 PSA_ASSERT( psa_cipher_finish( &operation,
195 ciphertext + ciphertext_length,
196 sizeof( ciphertext ) - ciphertext_length,
197 &part_length ) );
198 ciphertext_length += part_length;
199 }
200
201 if( usage & PSA_KEY_USAGE_DECRYPT )
202 {
203 psa_status_t status;
204 int maybe_invalid_padding = 0;
205 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
206 {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100207 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100208 }
209 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100210 if( iv_length != 0 )
211 {
212 PSA_ASSERT( psa_cipher_set_iv( &operation,
213 iv, iv_length ) );
214 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100215 PSA_ASSERT( psa_cipher_update( &operation,
216 ciphertext, ciphertext_length,
217 decrypted, sizeof( decrypted ),
218 &part_length ) );
219 status = psa_cipher_finish( &operation,
220 decrypted + part_length,
221 sizeof( decrypted ) - part_length,
222 &part_length );
223 /* For a stream cipher, all inputs are valid. For a block cipher,
224 * if the input is some aribtrary data rather than an actual
225 ciphertext, a padding error is likely. */
226 if( maybe_invalid_padding )
227 TEST_ASSERT( status == PSA_SUCCESS ||
228 status == PSA_ERROR_INVALID_PADDING );
229 else
230 PSA_ASSERT( status );
231 }
232
233 return( 1 );
234
235exit:
236 psa_cipher_abort( &operation );
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100237 psa_reset_key_attributes( &attributes );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100238 return( 0 );
239}
240
241static int exercise_aead_key( mbedtls_svc_key_id_t key,
242 psa_key_usage_t usage,
243 psa_algorithm_t alg )
244{
Gilles Peskineb29d8142022-04-21 11:14:52 +0200245 unsigned char nonce[PSA_AEAD_NONCE_MAX_SIZE] = {0};
Gilles Peskine7acb1982022-03-19 11:03:32 +0100246 size_t nonce_length;
247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
248 psa_key_type_t key_type;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100249 unsigned char plaintext[16] = "Hello, world...";
250 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
251 size_t ciphertext_length = sizeof( ciphertext );
252 size_t plaintext_length = sizeof( ciphertext );
253
Steven Cooremanfb9cb922021-02-23 14:37:38 +0100254 /* Convert wildcard algorithm to exercisable algorithm */
255 if( alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG )
256 {
257 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, PSA_ALG_AEAD_GET_TAG_LENGTH( alg ) );
258 }
259
Gilles Peskine7acb1982022-03-19 11:03:32 +0100260 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
261 key_type = psa_get_key_type( &attributes );
262 nonce_length = PSA_AEAD_NONCE_LENGTH( key_type, alg );
Steven Cooremanaaec3412021-02-18 13:30:34 +0100263
Gilles Peskinee78b0022021-02-13 00:41:11 +0100264 if( usage & PSA_KEY_USAGE_ENCRYPT )
265 {
266 PSA_ASSERT( psa_aead_encrypt( key, alg,
267 nonce, nonce_length,
268 NULL, 0,
269 plaintext, sizeof( plaintext ),
270 ciphertext, sizeof( ciphertext ),
271 &ciphertext_length ) );
272 }
273
274 if( usage & PSA_KEY_USAGE_DECRYPT )
275 {
276 psa_status_t verify_status =
277 ( usage & PSA_KEY_USAGE_ENCRYPT ?
278 PSA_SUCCESS :
279 PSA_ERROR_INVALID_SIGNATURE );
280 TEST_EQUAL( psa_aead_decrypt( key, alg,
281 nonce, nonce_length,
282 NULL, 0,
283 ciphertext, ciphertext_length,
284 plaintext, sizeof( plaintext ),
285 &plaintext_length ),
286 verify_status );
287 }
288
289 return( 1 );
290
291exit:
Gilles Peskine7acb1982022-03-19 11:03:32 +0100292 psa_reset_key_attributes( &attributes );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100293 return( 0 );
294}
295
Gilles Peskined586b822022-03-19 11:15:41 +0100296static int can_sign_or_verify_message( psa_key_usage_t usage,
297 psa_algorithm_t alg )
298{
299 /* Sign-the-unspecified-hash algorithms can only be used with
300 * {sign,verify}_hash, not with {sign,verify}_message. */
301 if( alg == PSA_ALG_ECDSA_ANY || alg == PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
302 return( 0 );
303 return( usage & ( PSA_KEY_USAGE_SIGN_MESSAGE |
304 PSA_KEY_USAGE_VERIFY_MESSAGE ) );
305}
306
Gilles Peskinee78b0022021-02-13 00:41:11 +0100307static int exercise_signature_key( mbedtls_svc_key_id_t key,
308 psa_key_usage_t usage,
309 psa_algorithm_t alg )
310{
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200311 if( usage & ( PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ) )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100312 {
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200313 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
314 size_t payload_length = 16;
315 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
316 size_t signature_length = sizeof( signature );
317 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
318
319 /* If the policy allows signing with any hash, just pick one. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200320 if( PSA_ALG_IS_SIGN_HASH( alg ) && hash_alg == PSA_ALG_ANY_HASH )
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200321 {
Ronald Cron4c0ec762021-08-31 19:08:55 +0200322 #if defined(KNOWN_MBEDTLS_SUPPORTED_HASH_ALG)
323 hash_alg = KNOWN_MBEDTLS_SUPPORTED_HASH_ALG;
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200324 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
325 #else
326 TEST_ASSERT( ! "No hash algorithm for hash-and-sign testing" );
327 #endif
328 }
329
Janos Follath4c0b60e2021-06-14 12:34:30 +0100330 /* Some algorithms require the payload to have the size of
331 * the hash encoded in the algorithm. Use this input size
332 * even for algorithms that allow other input sizes. */
333 if( hash_alg != 0 )
334 payload_length = PSA_HASH_LENGTH( hash_alg );
335
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200336 if( usage & PSA_KEY_USAGE_SIGN_HASH )
337 {
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200338 PSA_ASSERT( psa_sign_hash( key, alg,
339 payload, payload_length,
340 signature, sizeof( signature ),
341 &signature_length ) );
342 }
343
344 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
345 {
346 psa_status_t verify_status =
347 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
348 PSA_SUCCESS :
349 PSA_ERROR_INVALID_SIGNATURE );
350 TEST_EQUAL( psa_verify_hash( key, alg,
351 payload, payload_length,
352 signature, signature_length ),
353 verify_status );
354 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100355 }
356
Gilles Peskined586b822022-03-19 11:15:41 +0100357 if( can_sign_or_verify_message( usage, alg ) )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100358 {
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200359 unsigned char message[256] = "Hello, world...";
360 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
361 size_t message_length = 16;
362 size_t signature_length = sizeof( signature );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100363
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200364 if( usage & PSA_KEY_USAGE_SIGN_MESSAGE )
365 {
366 PSA_ASSERT( psa_sign_message( key, alg,
367 message, message_length,
368 signature, sizeof( signature ),
369 &signature_length ) );
370 }
371
372 if( usage & PSA_KEY_USAGE_VERIFY_MESSAGE )
373 {
374 psa_status_t verify_status =
375 ( usage & PSA_KEY_USAGE_SIGN_MESSAGE ?
376 PSA_SUCCESS :
377 PSA_ERROR_INVALID_SIGNATURE );
378 TEST_EQUAL( psa_verify_message( key, alg,
379 message, message_length,
380 signature, signature_length ),
381 verify_status );
382 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100383 }
384
385 return( 1 );
386
387exit:
388 return( 0 );
389}
390
391static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
392 psa_key_usage_t usage,
393 psa_algorithm_t alg )
394{
395 unsigned char plaintext[256] = "Hello, world...";
396 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
397 size_t ciphertext_length = sizeof( ciphertext );
398 size_t plaintext_length = 16;
399
400 if( usage & PSA_KEY_USAGE_ENCRYPT )
401 {
402 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
403 plaintext, plaintext_length,
404 NULL, 0,
405 ciphertext, sizeof( ciphertext ),
406 &ciphertext_length ) );
407 }
408
409 if( usage & PSA_KEY_USAGE_DECRYPT )
410 {
411 psa_status_t status =
412 psa_asymmetric_decrypt( key, alg,
413 ciphertext, ciphertext_length,
414 NULL, 0,
415 plaintext, sizeof( plaintext ),
416 &plaintext_length );
417 TEST_ASSERT( status == PSA_SUCCESS ||
418 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
419 ( status == PSA_ERROR_INVALID_ARGUMENT ||
420 status == PSA_ERROR_INVALID_PADDING ) ) );
421 }
422
423 return( 1 );
424
425exit:
426 return( 0 );
427}
428
429int mbedtls_test_psa_setup_key_derivation_wrap(
430 psa_key_derivation_operation_t* operation,
431 mbedtls_svc_key_id_t key,
432 psa_algorithm_t alg,
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100433 const unsigned char* input1, size_t input1_length,
434 const unsigned char* input2, size_t input2_length,
Gilles Peskinee78b0022021-02-13 00:41:11 +0100435 size_t capacity )
436{
437 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
438 if( PSA_ALG_IS_HKDF( alg ) )
439 {
440 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
441 PSA_KEY_DERIVATION_INPUT_SALT,
442 input1, input1_length ) );
443 PSA_ASSERT( psa_key_derivation_input_key( operation,
444 PSA_KEY_DERIVATION_INPUT_SECRET,
445 key ) );
446 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
447 PSA_KEY_DERIVATION_INPUT_INFO,
448 input2,
449 input2_length ) );
450 }
451 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
452 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
453 {
454 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
455 PSA_KEY_DERIVATION_INPUT_SEED,
456 input1, input1_length ) );
457 PSA_ASSERT( psa_key_derivation_input_key( operation,
458 PSA_KEY_DERIVATION_INPUT_SECRET,
459 key ) );
460 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
461 PSA_KEY_DERIVATION_INPUT_LABEL,
462 input2, input2_length ) );
463 }
464 else
465 {
466 TEST_ASSERT( ! "Key derivation algorithm not supported" );
467 }
468
469 if( capacity != SIZE_MAX )
470 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
471
472 return( 1 );
473
474exit:
475 return( 0 );
476}
477
478
479static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
480 psa_key_usage_t usage,
481 psa_algorithm_t alg )
482{
483 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
484 unsigned char input1[] = "Input 1";
485 size_t input1_length = sizeof( input1 );
486 unsigned char input2[] = "Input 2";
487 size_t input2_length = sizeof( input2 );
488 unsigned char output[1];
489 size_t capacity = sizeof( output );
490
491 if( usage & PSA_KEY_USAGE_DERIVE )
492 {
493 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
494 input1, input1_length,
495 input2, input2_length,
496 capacity ) )
497 goto exit;
498
499 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
500 output,
501 capacity ) );
502 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
503 }
504
505 return( 1 );
506
507exit:
508 return( 0 );
509}
510
511/* We need two keys to exercise key agreement. Exercise the
512 * private key against its own public key. */
513psa_status_t mbedtls_test_psa_key_agreement_with_self(
514 psa_key_derivation_operation_t *operation,
515 mbedtls_svc_key_id_t key )
516{
517 psa_key_type_t private_key_type;
518 psa_key_type_t public_key_type;
519 size_t key_bits;
520 uint8_t *public_key = NULL;
521 size_t public_key_length;
522 /* Return GENERIC_ERROR if something other than the final call to
523 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
524 * but it's good enough: callers will report it as a failed test anyway. */
525 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
526 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
527
528 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
529 private_key_type = psa_get_key_type( &attributes );
530 key_bits = psa_get_key_bits( &attributes );
531 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armceface22021-01-21 12:26:17 +0100532 public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100533 ASSERT_ALLOC( public_key, public_key_length );
534 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
535 &public_key_length ) );
536
537 status = psa_key_derivation_key_agreement(
538 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
539 public_key, public_key_length );
540exit:
541 /*
542 * Key attributes may have been returned by psa_get_key_attributes()
543 * thus reset them as required.
544 */
545 psa_reset_key_attributes( &attributes );
546
547 mbedtls_free( public_key );
548 return( status );
549}
550
551/* We need two keys to exercise key agreement. Exercise the
552 * private key against its own public key. */
553psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
554 psa_algorithm_t alg,
555 mbedtls_svc_key_id_t key )
556{
557 psa_key_type_t private_key_type;
558 psa_key_type_t public_key_type;
559 size_t key_bits;
560 uint8_t *public_key = NULL;
561 size_t public_key_length;
562 uint8_t output[1024];
563 size_t output_length;
564 /* Return GENERIC_ERROR if something other than the final call to
565 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
566 * but it's good enough: callers will report it as a failed test anyway. */
567 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
568 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
569
570 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
571 private_key_type = psa_get_key_type( &attributes );
572 key_bits = psa_get_key_bits( &attributes );
573 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armceface22021-01-21 12:26:17 +0100574 public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100575 ASSERT_ALLOC( public_key, public_key_length );
576 PSA_ASSERT( psa_export_public_key( key,
577 public_key, public_key_length,
578 &public_key_length ) );
579
580 status = psa_raw_key_agreement( alg, key,
581 public_key, public_key_length,
582 output, sizeof( output ), &output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +0100583 if ( status == PSA_SUCCESS )
584 {
585 TEST_ASSERT( output_length <=
586 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( private_key_type,
587 key_bits ) );
588 TEST_ASSERT( output_length <=
589 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
590 }
591
Gilles Peskinee78b0022021-02-13 00:41:11 +0100592exit:
593 /*
594 * Key attributes may have been returned by psa_get_key_attributes()
595 * thus reset them as required.
596 */
597 psa_reset_key_attributes( &attributes );
598
599 mbedtls_free( public_key );
600 return( status );
601}
602
603static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
604 psa_key_usage_t usage,
605 psa_algorithm_t alg )
606{
607 int ok = 0;
608
609 if( usage & PSA_KEY_USAGE_DERIVE )
610 {
611 /* We need two keys to exercise key agreement. Exercise the
612 * private key against its own public key. */
613 PSA_ASSERT( mbedtls_test_psa_raw_key_agreement_with_self( alg, key ) );
614 }
615 ok = 1;
616
617exit:
618 return( ok );
619}
620
621static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
622 psa_key_usage_t usage,
623 psa_algorithm_t alg )
624{
625 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineaa3449d2022-03-19 16:04:30 +0100626 unsigned char input[1];
Gilles Peskinee78b0022021-02-13 00:41:11 +0100627 unsigned char output[1];
628 int ok = 0;
Gilles Peskineaa3449d2022-03-19 16:04:30 +0100629 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100630
631 if( usage & PSA_KEY_USAGE_DERIVE )
632 {
633 /* We need two keys to exercise key agreement. Exercise the
634 * private key against its own public key. */
635 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskineaa3449d2022-03-19 16:04:30 +0100636 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
637 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
638 {
639 PSA_ASSERT( psa_key_derivation_input_bytes(
640 &operation, PSA_KEY_DERIVATION_INPUT_SEED,
641 input, sizeof( input ) ) );
642 }
643
Przemek Stekield8987452022-06-14 11:41:52 +0200644 if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
645 {
646 PSA_ASSERT( psa_key_derivation_input_bytes(
647 &operation, PSA_KEY_DERIVATION_INPUT_SALT,
648 input, sizeof( input ) ) );
649 }
650
Gilles Peskinee78b0022021-02-13 00:41:11 +0100651 PSA_ASSERT( mbedtls_test_psa_key_agreement_with_self( &operation, key ) );
Gilles Peskineaa3449d2022-03-19 16:04:30 +0100652
653 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
654 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
655 {
656 PSA_ASSERT( psa_key_derivation_input_bytes(
657 &operation, PSA_KEY_DERIVATION_INPUT_LABEL,
658 input, sizeof( input ) ) );
659 }
660 else if( PSA_ALG_IS_HKDF( kdf_alg ) )
661 {
662 PSA_ASSERT( psa_key_derivation_input_bytes(
663 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
664 input, sizeof( input ) ) );
665 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100666 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
667 output,
668 sizeof( output ) ) );
669 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
670 }
671 ok = 1;
672
673exit:
674 return( ok );
675}
676
677int mbedtls_test_psa_exported_key_sanity_check(
678 psa_key_type_t type, size_t bits,
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100679 const uint8_t *exported, size_t exported_length )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100680{
Gilles Peskinecc9db302021-02-14 01:29:52 +0100681 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100682
Gilles Peskinecc9db302021-02-14 01:29:52 +0100683 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
684 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100685 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100686
Ronald Cron64df7382021-07-06 09:23:06 +0200687#if defined(MBEDTLS_ASN1_PARSE_C)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100688 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
689 {
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100690 uint8_t *p = (uint8_t*) exported;
691 const uint8_t *end = exported + exported_length;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100692 size_t len;
693 /* RSAPrivateKey ::= SEQUENCE {
694 * version INTEGER, -- must be 0
695 * modulus INTEGER, -- n
696 * publicExponent INTEGER, -- e
697 * privateExponent INTEGER, -- d
698 * prime1 INTEGER, -- p
699 * prime2 INTEGER, -- q
700 * exponent1 INTEGER, -- d mod (p-1)
701 * exponent2 INTEGER, -- d mod (q-1)
702 * coefficient INTEGER, -- (inverse of q) mod p
703 * }
704 */
705 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
706 MBEDTLS_ASN1_SEQUENCE |
707 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
Gilles Peskine89615ee2021-04-29 20:28:54 +0200708 TEST_EQUAL( len, end - p );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100709 if( ! mbedtls_test_asn1_skip_integer( &p, end, 0, 0, 0 ) )
710 goto exit;
711 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
712 goto exit;
713 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
714 goto exit;
715 /* Require d to be at least half the size of n. */
716 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
717 goto exit;
718 /* Require p and q to be at most half the size of n, rounded up. */
719 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
720 goto exit;
721 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
722 goto exit;
723 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
724 goto exit;
725 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
726 goto exit;
727 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
728 goto exit;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200729 TEST_EQUAL( p - end, 0 );
gabor-mezei-armceface22021-01-21 12:26:17 +0100730
731 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100732 }
733 else
Ronald Cron64df7382021-07-06 09:23:06 +0200734#endif /* MBEDTLS_ASN1_PARSE_C */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100735
736#if defined(MBEDTLS_ECP_C)
737 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
738 {
739 /* Just the secret value */
740 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100741
742 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100743 }
744 else
745#endif /* MBEDTLS_ECP_C */
746
Ronald Cron64df7382021-07-06 09:23:06 +0200747#if defined(MBEDTLS_ASN1_PARSE_C)
Gilles Peskinead557e52021-02-14 01:19:21 +0100748 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100749 {
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100750 uint8_t *p = (uint8_t*) exported;
751 const uint8_t *end = exported + exported_length;
Gilles Peskinead557e52021-02-14 01:19:21 +0100752 size_t len;
753 /* RSAPublicKey ::= SEQUENCE {
754 * modulus INTEGER, -- n
755 * publicExponent INTEGER } -- e
756 */
757 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
758 MBEDTLS_ASN1_SEQUENCE |
759 MBEDTLS_ASN1_CONSTRUCTED ),
760 0 );
Gilles Peskine89615ee2021-04-29 20:28:54 +0200761 TEST_EQUAL( len, end - p );
Gilles Peskinead557e52021-02-14 01:19:21 +0100762 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
763 goto exit;
764 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
765 goto exit;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200766 TEST_EQUAL( p - end, 0 );
gabor-mezei-armceface22021-01-21 12:26:17 +0100767
768
769 TEST_ASSERT( exported_length <=
770 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( type, bits ) );
771 TEST_ASSERT( exported_length <=
772 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskinead557e52021-02-14 01:19:21 +0100773 }
774 else
Ronald Cron64df7382021-07-06 09:23:06 +0200775#endif /* MBEDTLS_ASN1_PARSE_C */
Gilles Peskinead557e52021-02-14 01:19:21 +0100776
Gilles Peskinee78b0022021-02-13 00:41:11 +0100777#if defined(MBEDTLS_ECP_C)
Gilles Peskinead557e52021-02-14 01:19:21 +0100778 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
779 {
gabor-mezei-armceface22021-01-21 12:26:17 +0100780
781 TEST_ASSERT( exported_length <=
782 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( type, bits ) );
783 TEST_ASSERT( exported_length <=
784 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
785
Gilles Peskinead557e52021-02-14 01:19:21 +0100786 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100787 {
Gilles Peskinead557e52021-02-14 01:19:21 +0100788 /* The representation of an ECC Montgomery public key is
789 * the raw compressed point */
790 TEST_EQUAL( PSA_BITS_TO_BYTES( bits ), exported_length );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100791 }
792 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100793 {
Gilles Peskinead557e52021-02-14 01:19:21 +0100794 /* The representation of an ECC Weierstrass public key is:
795 * - The byte 0x04;
796 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
797 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
798 * - where m is the bit size associated with the curve.
799 */
800 TEST_EQUAL( 1 + 2 * PSA_BITS_TO_BYTES( bits ), exported_length );
801 TEST_EQUAL( exported[0], 4 );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100802 }
803 }
804 else
Gilles Peskinead557e52021-02-14 01:19:21 +0100805#endif /* MBEDTLS_ECP_C */
806
Gilles Peskinead557e52021-02-14 01:19:21 +0100807 {
Andrzej Kurek57d2f132022-01-17 15:26:24 +0100808 (void) exported;
Gilles Peskinecc9db302021-02-14 01:29:52 +0100809 TEST_ASSERT( ! "Sanity check not implemented for this key type" );
Gilles Peskinead557e52021-02-14 01:19:21 +0100810 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100811
Gilles Peskinecc9db302021-02-14 01:29:52 +0100812#if defined(MBEDTLS_DES_C)
813 if( type == PSA_KEY_TYPE_DES )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100814 {
Gilles Peskinecc9db302021-02-14 01:29:52 +0100815 /* Check the parity bits. */
816 unsigned i;
817 for( i = 0; i < bits / 8; i++ )
818 {
819 unsigned bit_count = 0;
820 unsigned m;
821 for( m = 1; m <= 0x100; m <<= 1 )
822 {
823 if( exported[i] & m )
824 ++bit_count;
825 }
826 TEST_ASSERT( bit_count % 2 != 0 );
827 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100828 }
Gilles Peskinecc9db302021-02-14 01:29:52 +0100829#endif
Gilles Peskinee78b0022021-02-13 00:41:11 +0100830
831 return( 1 );
832
833exit:
834 return( 0 );
835}
836
837static int exercise_export_key( mbedtls_svc_key_id_t key,
838 psa_key_usage_t usage )
839{
840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
841 uint8_t *exported = NULL;
842 size_t exported_size = 0;
843 size_t exported_length = 0;
844 int ok = 0;
845
846 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
847
848 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
849 psa_get_key_type( &attributes ),
850 psa_get_key_bits( &attributes ) );
851 ASSERT_ALLOC( exported, exported_size );
852
853 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
854 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
855 {
856 TEST_EQUAL( psa_export_key( key, exported,
857 exported_size, &exported_length ),
858 PSA_ERROR_NOT_PERMITTED );
859 ok = 1;
860 goto exit;
861 }
862
863 PSA_ASSERT( psa_export_key( key,
864 exported, exported_size,
865 &exported_length ) );
866 ok = mbedtls_test_psa_exported_key_sanity_check(
867 psa_get_key_type( &attributes ), psa_get_key_bits( &attributes ),
868 exported, exported_length );
869
870exit:
871 /*
872 * Key attributes may have been returned by psa_get_key_attributes()
873 * thus reset them as required.
874 */
875 psa_reset_key_attributes( &attributes );
876
877 mbedtls_free( exported );
878 return( ok );
879}
880
881static int exercise_export_public_key( mbedtls_svc_key_id_t key )
882{
883 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
884 psa_key_type_t public_type;
885 uint8_t *exported = NULL;
886 size_t exported_size = 0;
887 size_t exported_length = 0;
888 int ok = 0;
889
890 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
891 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
892 {
893 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
894 psa_get_key_type( &attributes ),
895 psa_get_key_bits( &attributes ) );
896 ASSERT_ALLOC( exported, exported_size );
897
898 TEST_EQUAL( psa_export_public_key( key, exported,
899 exported_size, &exported_length ),
900 PSA_ERROR_INVALID_ARGUMENT );
901 ok = 1;
902 goto exit;
903 }
904
905 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
906 psa_get_key_type( &attributes ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100907 exported_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type,
908 psa_get_key_bits( &attributes ) );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100909 ASSERT_ALLOC( exported, exported_size );
910
911 PSA_ASSERT( psa_export_public_key( key,
912 exported, exported_size,
913 &exported_length ) );
914 ok = mbedtls_test_psa_exported_key_sanity_check(
915 public_type, psa_get_key_bits( &attributes ),
916 exported, exported_length );
917
918exit:
919 /*
920 * Key attributes may have been returned by psa_get_key_attributes()
921 * thus reset them as required.
922 */
923 psa_reset_key_attributes( &attributes );
924
925 mbedtls_free( exported );
926 return( ok );
927}
928
929int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
930 psa_key_usage_t usage,
931 psa_algorithm_t alg )
932{
Gilles Peskine2385f712021-02-14 01:34:21 +0100933 int ok = 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100934
935 if( ! check_key_attributes_sanity( key ) )
936 return( 0 );
937
938 if( alg == 0 )
939 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
940 else if( PSA_ALG_IS_MAC( alg ) )
941 ok = exercise_mac_key( key, usage, alg );
942 else if( PSA_ALG_IS_CIPHER( alg ) )
943 ok = exercise_cipher_key( key, usage, alg );
944 else if( PSA_ALG_IS_AEAD( alg ) )
945 ok = exercise_aead_key( key, usage, alg );
946 else if( PSA_ALG_IS_SIGN( alg ) )
947 ok = exercise_signature_key( key, usage, alg );
948 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
949 ok = exercise_asymmetric_encryption_key( key, usage, alg );
950 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
951 ok = exercise_key_derivation_key( key, usage, alg );
952 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
953 ok = exercise_raw_key_agreement_key( key, usage, alg );
954 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
955 ok = exercise_key_agreement_key( key, usage, alg );
956 else
Gilles Peskine2385f712021-02-14 01:34:21 +0100957 TEST_ASSERT( ! "No code to exercise this category of algorithm" );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100958
959 ok = ok && exercise_export_key( key, usage );
960 ok = ok && exercise_export_public_key( key );
961
Gilles Peskine2385f712021-02-14 01:34:21 +0100962exit:
Gilles Peskinee78b0022021-02-13 00:41:11 +0100963 return( ok );
964}
965
966psa_key_usage_t mbedtls_test_psa_usage_to_exercise( psa_key_type_t type,
967 psa_algorithm_t alg )
968{
969 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
970 {
Gilles Peskinef7b41372021-09-22 16:15:05 +0200971 if( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm041887b2021-05-11 13:29:24 +0200972 {
973 if( PSA_ALG_SIGN_GET_HASH( alg ) )
974 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
975 PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_MESSAGE:
976 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH |
977 PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE );
978 }
979 else if( PSA_ALG_IS_SIGN_MESSAGE( alg) )
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200980 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
gabor-mezei-arm041887b2021-05-11 13:29:24 +0200981 PSA_KEY_USAGE_VERIFY_MESSAGE :
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200982 PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE );
983
gabor-mezei-arm041887b2021-05-11 13:29:24 +0200984 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
985 PSA_KEY_USAGE_VERIFY_HASH :
986 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100987 }
988 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
989 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
990 {
991 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
992 PSA_KEY_USAGE_ENCRYPT :
993 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
994 }
995 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
996 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
997 {
998 return( PSA_KEY_USAGE_DERIVE );
999 }
1000 else
1001 {
1002 return( 0 );
1003 }
1004
1005}
Gilles Peskine66e7b902021-02-12 23:40:58 +01001006
1007#endif /* MBEDTLS_PSA_CRYPTO_C */