blob: 3fcf77d379db9bfc77a6e46c673eda4ffd236bfb [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
Dave Rodgman16799db2023-11-02 19:47:20 +00007 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine66e7b902021-02-12 23:40:58 +01008 */
9
10#include <test/helpers.h>
11#include <test/macros.h>
Gilles Peskinee78b0022021-02-13 00:41:11 +010012#include <test/psa_exercise_key.h>
Gilles Peskine66e7b902021-02-12 23:40:58 +010013
14#if defined(MBEDTLS_PSA_CRYPTO_C)
15
Gilles Peskinee78b0022021-02-13 00:41:11 +010016#include <mbedtls/asn1.h>
Gilles Peskine66e7b902021-02-12 23:40:58 +010017#include <psa/crypto.h>
18
Gilles Peskinee78b0022021-02-13 00:41:11 +010019#include <test/asn1_helpers.h>
Przemyslaw Stekiel53de2622021-11-03 09:35:35 +010020#include <psa_crypto_slot_management.h>
Gilles Peskinee78b0022021-02-13 00:41:11 +010021#include <test/psa_crypto_helpers.h>
22
23#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010024static int lifetime_is_dynamic_secure_element(psa_key_lifetime_t lifetime)
Gilles Peskinee78b0022021-02-13 00:41:11 +010025{
Gilles Peskine449bd832023-01-11 14:50:10 +010026 return PSA_KEY_LIFETIME_GET_LOCATION(lifetime) !=
27 PSA_KEY_LOCATION_LOCAL_STORAGE;
Gilles Peskinee78b0022021-02-13 00:41:11 +010028}
29#endif
30
Gilles Peskine449bd832023-01-11 14:50:10 +010031static int check_key_attributes_sanity(mbedtls_svc_key_id_t key)
Gilles Peskinee78b0022021-02-13 00:41:11 +010032{
33 int ok = 0;
34 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
35 psa_key_lifetime_t lifetime;
36 mbedtls_svc_key_id_t id;
37 psa_key_type_t type;
Gilles Peskine6b362e62021-02-15 12:03:16 +010038 size_t bits;
Gilles Peskinee78b0022021-02-13 00:41:11 +010039
Gilles Peskine449bd832023-01-11 14:50:10 +010040 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
41 lifetime = psa_get_key_lifetime(&attributes);
42 id = psa_get_key_id(&attributes);
43 type = psa_get_key_type(&attributes);
44 bits = psa_get_key_bits(&attributes);
Gilles Peskinee78b0022021-02-13 00:41:11 +010045
46 /* Persistence */
Gilles Peskine449bd832023-01-11 14:50:10 +010047 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +010048 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +010049 (PSA_KEY_ID_VOLATILE_MIN <=
50 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id)) &&
51 (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) <=
52 PSA_KEY_ID_VOLATILE_MAX));
53 } else {
Gilles Peskinee78b0022021-02-13 00:41:11 +010054 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +010055 (PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id)) &&
56 (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(id) <= PSA_KEY_ID_USER_MAX));
Gilles Peskinee78b0022021-02-13 00:41:11 +010057 }
58#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
59 /* randomly-generated 64-bit constant, should never appear in test data */
60 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
Gilles Peskine449bd832023-01-11 14:50:10 +010061 psa_status_t status = psa_get_key_slot_number(&attributes, &slot_number);
62 if (lifetime_is_dynamic_secure_element(lifetime)) {
Fredrik Hessecc207bc2021-09-28 21:06:08 +020063 /* Mbed TLS currently always exposes the slot number to
Gilles Peskinee78b0022021-02-13 00:41:11 +010064 * applications. This is not mandated by the PSA specification
65 * and may change in future versions. */
Gilles Peskine449bd832023-01-11 14:50:10 +010066 TEST_EQUAL(status, 0);
67 TEST_ASSERT(slot_number != 0xec94d4a5058a1a21);
68 } else {
69 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Gilles Peskinee78b0022021-02-13 00:41:11 +010070 }
71#endif
72
73 /* Type and size */
Gilles Peskine449bd832023-01-11 14:50:10 +010074 TEST_ASSERT(type != 0);
75 TEST_ASSERT(bits != 0);
76 TEST_ASSERT(bits <= PSA_MAX_KEY_BITS);
77 if (PSA_KEY_TYPE_IS_UNSTRUCTURED(type)) {
78 TEST_ASSERT(bits % 8 == 0);
79 }
Gilles Peskinee78b0022021-02-13 00:41:11 +010080
81 /* MAX macros concerning specific key types */
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if (PSA_KEY_TYPE_IS_ECC(type)) {
83 TEST_ASSERT(bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS);
84 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
85 TEST_ASSERT(bits <= PSA_VENDOR_RSA_MAX_KEY_BITS);
86 }
87 TEST_ASSERT(PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE);
Gilles Peskinee78b0022021-02-13 00:41:11 +010088
89 ok = 1;
90
91exit:
92 /*
93 * Key attributes may have been returned by psa_get_key_attributes()
94 * thus reset them as required.
95 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096 psa_reset_key_attributes(&attributes);
Gilles Peskinee78b0022021-02-13 00:41:11 +010097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return ok;
Gilles Peskinee78b0022021-02-13 00:41:11 +010099}
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101static int exercise_mac_key(mbedtls_svc_key_id_t key,
102 psa_key_usage_t usage,
103 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100104{
105 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
106 const unsigned char input[] = "foo";
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 unsigned char mac[PSA_MAC_MAX_SIZE] = { 0 };
108 size_t mac_length = sizeof(mac);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100109
Steven Cooremanfb9cb922021-02-23 14:37:38 +0100110 /* Convert wildcard algorithm to exercisable algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) {
112 alg = PSA_ALG_TRUNCATED_MAC(alg, PSA_MAC_TRUNCATED_LENGTH(alg));
Steven Cooremanfb9cb922021-02-23 14:37:38 +0100113 }
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if (usage & PSA_KEY_USAGE_SIGN_HASH) {
116 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
117 PSA_ASSERT(psa_mac_update(&operation,
118 input, sizeof(input)));
119 PSA_ASSERT(psa_mac_sign_finish(&operation,
120 mac, sizeof(mac),
121 &mac_length));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (usage & PSA_KEY_USAGE_VERIFY_HASH) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100125 psa_status_t verify_status =
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 (usage & PSA_KEY_USAGE_SIGN_HASH ?
127 PSA_SUCCESS :
128 PSA_ERROR_INVALID_SIGNATURE);
129 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
130 PSA_ASSERT(psa_mac_update(&operation,
131 input, sizeof(input)));
132 TEST_EQUAL(psa_mac_verify_finish(&operation, mac, mac_length),
133 verify_status);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100134 }
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return 1;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100137
138exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 psa_mac_abort(&operation);
140 return 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100141}
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143static int exercise_cipher_key(mbedtls_svc_key_id_t key,
144 psa_key_usage_t usage,
145 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100146{
147 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Gilles Peskine5eef11a2022-04-21 11:14:30 +0200149 size_t iv_length;
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
151 psa_key_type_t key_type;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100152 const unsigned char plaintext[16] = "Hello, world...";
153 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 size_t ciphertext_length = sizeof(ciphertext);
155 unsigned char decrypted[sizeof(ciphertext)];
Gilles Peskinee78b0022021-02-13 00:41:11 +0100156 size_t part_length;
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
159 key_type = psa_get_key_type(&attributes);
160 iv_length = PSA_CIPHER_IV_LENGTH(key_type, alg);
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (usage & PSA_KEY_USAGE_ENCRYPT) {
163 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
164 if (iv_length != 0) {
165 PSA_ASSERT(psa_cipher_generate_iv(&operation,
166 iv, sizeof(iv),
167 &iv_length));
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100168 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 PSA_ASSERT(psa_cipher_update(&operation,
170 plaintext, sizeof(plaintext),
171 ciphertext, sizeof(ciphertext),
172 &ciphertext_length));
173 PSA_ASSERT(psa_cipher_finish(&operation,
174 ciphertext + ciphertext_length,
175 sizeof(ciphertext) - ciphertext_length,
176 &part_length));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100177 ciphertext_length += part_length;
178 }
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (usage & PSA_KEY_USAGE_DECRYPT) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100181 psa_status_t status;
182 int maybe_invalid_padding = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (!(usage & PSA_KEY_USAGE_ENCRYPT)) {
184 maybe_invalid_padding = !PSA_ALG_IS_STREAM_CIPHER(alg);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100185 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
187 if (iv_length != 0) {
188 PSA_ASSERT(psa_cipher_set_iv(&operation,
189 iv, iv_length));
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100190 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 PSA_ASSERT(psa_cipher_update(&operation,
192 ciphertext, ciphertext_length,
193 decrypted, sizeof(decrypted),
194 &part_length));
195 status = psa_cipher_finish(&operation,
196 decrypted + part_length,
197 sizeof(decrypted) - part_length,
198 &part_length);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100199 /* For a stream cipher, all inputs are valid. For a block cipher,
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800200 * if the input is some arbitrary data rather than an actual
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 ciphertext, a padding error is likely. */
202 if (maybe_invalid_padding) {
203 TEST_ASSERT(status == PSA_SUCCESS ||
204 status == PSA_ERROR_INVALID_PADDING);
205 } else {
206 PSA_ASSERT(status);
207 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100208 }
209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return 1;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100211
212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 psa_cipher_abort(&operation);
214 psa_reset_key_attributes(&attributes);
215 return 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100216}
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218static int exercise_aead_key(mbedtls_svc_key_id_t key,
219 psa_key_usage_t usage,
220 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100221{
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 unsigned char nonce[PSA_AEAD_NONCE_MAX_SIZE] = { 0 };
Gilles Peskine7acb1982022-03-19 11:03:32 +0100223 size_t nonce_length;
224 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
225 psa_key_type_t key_type;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100226 unsigned char plaintext[16] = "Hello, world...";
227 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 size_t ciphertext_length = sizeof(ciphertext);
229 size_t plaintext_length = sizeof(ciphertext);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100230
Steven Cooremanfb9cb922021-02-23 14:37:38 +0100231 /* Convert wildcard algorithm to exercisable algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) {
233 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, PSA_ALG_AEAD_GET_TAG_LENGTH(alg));
Steven Cooremanfb9cb922021-02-23 14:37:38 +0100234 }
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
237 key_type = psa_get_key_type(&attributes);
238 nonce_length = PSA_AEAD_NONCE_LENGTH(key_type, alg);
Steven Cooremanaaec3412021-02-18 13:30:34 +0100239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if (usage & PSA_KEY_USAGE_ENCRYPT) {
241 PSA_ASSERT(psa_aead_encrypt(key, alg,
242 nonce, nonce_length,
243 NULL, 0,
244 plaintext, sizeof(plaintext),
245 ciphertext, sizeof(ciphertext),
246 &ciphertext_length));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100247 }
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (usage & PSA_KEY_USAGE_DECRYPT) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100250 psa_status_t verify_status =
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 (usage & PSA_KEY_USAGE_ENCRYPT ?
252 PSA_SUCCESS :
253 PSA_ERROR_INVALID_SIGNATURE);
254 TEST_EQUAL(psa_aead_decrypt(key, alg,
255 nonce, nonce_length,
256 NULL, 0,
257 ciphertext, ciphertext_length,
258 plaintext, sizeof(plaintext),
259 &plaintext_length),
260 verify_status);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100261 }
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return 1;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100264
265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 psa_reset_key_attributes(&attributes);
267 return 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100268}
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270static int can_sign_or_verify_message(psa_key_usage_t usage,
271 psa_algorithm_t alg)
Gilles Peskined586b822022-03-19 11:15:41 +0100272{
273 /* Sign-the-unspecified-hash algorithms can only be used with
274 * {sign,verify}_hash, not with {sign,verify}_message. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (alg == PSA_ALG_ECDSA_ANY || alg == PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
276 return 0;
277 }
278 return usage & (PSA_KEY_USAGE_SIGN_MESSAGE |
279 PSA_KEY_USAGE_VERIFY_MESSAGE);
Gilles Peskined586b822022-03-19 11:15:41 +0100280}
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282static int exercise_signature_key(mbedtls_svc_key_id_t key,
283 psa_key_usage_t usage,
284 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100285{
Gilles Peskine4781bd92024-02-09 17:32:45 +0100286 /* If the policy allows signing with any hash, just pick one. */
287 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
288 if (PSA_ALG_IS_SIGN_HASH(alg) && hash_alg == PSA_ALG_ANY_HASH &&
289 usage & (PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH |
290 PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE)) {
291#if defined(KNOWN_SUPPORTED_HASH_ALG)
292 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
293 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
294#else
295 TEST_FAIL("No hash algorithm for hash-and-sign testing");
296#endif
297 }
298
oberon-skf7a824b2023-02-15 19:43:30 +0100299 if (usage & (PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH) &&
300 PSA_ALG_IS_SIGN_HASH(alg)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200302 size_t payload_length = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
304 size_t signature_length = sizeof(signature);
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200305
Janos Follath4c0b60e2021-06-14 12:34:30 +0100306 /* Some algorithms require the payload to have the size of
307 * the hash encoded in the algorithm. Use this input size
308 * even for algorithms that allow other input sizes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (hash_alg != 0) {
310 payload_length = PSA_HASH_LENGTH(hash_alg);
311 }
Janos Follath4c0b60e2021-06-14 12:34:30 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (usage & PSA_KEY_USAGE_SIGN_HASH) {
314 PSA_ASSERT(psa_sign_hash(key, alg,
315 payload, payload_length,
316 signature, sizeof(signature),
317 &signature_length));
318 }
319
320 if (usage & PSA_KEY_USAGE_VERIFY_HASH) {
321 psa_status_t verify_status =
322 (usage & PSA_KEY_USAGE_SIGN_HASH ?
323 PSA_SUCCESS :
324 PSA_ERROR_INVALID_SIGNATURE);
325 TEST_EQUAL(psa_verify_hash(key, alg,
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200326 payload, payload_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 signature, signature_length),
328 verify_status);
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200329 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100330 }
331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (can_sign_or_verify_message(usage, alg)) {
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200333 unsigned char message[256] = "Hello, world...";
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200335 size_t message_length = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 size_t signature_length = sizeof(signature);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (usage & PSA_KEY_USAGE_SIGN_MESSAGE) {
339 PSA_ASSERT(psa_sign_message(key, alg,
340 message, message_length,
341 signature, sizeof(signature),
342 &signature_length));
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200343 }
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (usage & PSA_KEY_USAGE_VERIFY_MESSAGE) {
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200346 psa_status_t verify_status =
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 (usage & PSA_KEY_USAGE_SIGN_MESSAGE ?
348 PSA_SUCCESS :
349 PSA_ERROR_INVALID_SIGNATURE);
350 TEST_EQUAL(psa_verify_message(key, alg,
351 message, message_length,
352 signature, signature_length),
353 verify_status);
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200354 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100355 }
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return 1;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100358
359exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100361}
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363static int exercise_asymmetric_encryption_key(mbedtls_svc_key_id_t key,
364 psa_key_usage_t usage,
365 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100366{
Gilles Peskinefdb809e2024-02-09 19:22:30 +0100367 unsigned char plaintext[PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE] =
368 "Hello, world...";
369 unsigned char ciphertext[PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE] =
370 "(wabblewebblewibblewobblewubble)";
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 size_t ciphertext_length = sizeof(ciphertext);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100372 size_t plaintext_length = 16;
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (usage & PSA_KEY_USAGE_ENCRYPT) {
375 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
376 plaintext, plaintext_length,
377 NULL, 0,
378 ciphertext, sizeof(ciphertext),
379 &ciphertext_length));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100380 }
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (usage & PSA_KEY_USAGE_DECRYPT) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100383 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 psa_asymmetric_decrypt(key, alg,
385 ciphertext, ciphertext_length,
386 NULL, 0,
387 plaintext, sizeof(plaintext),
388 &plaintext_length);
389 TEST_ASSERT(status == PSA_SUCCESS ||
390 ((usage & PSA_KEY_USAGE_ENCRYPT) == 0 &&
391 (status == PSA_ERROR_INVALID_ARGUMENT ||
392 status == PSA_ERROR_INVALID_PADDING)));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return 1;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100396
397exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100399}
400
401int mbedtls_test_psa_setup_key_derivation_wrap(
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 psa_key_derivation_operation_t *operation,
Gilles Peskinee78b0022021-02-13 00:41:11 +0100403 mbedtls_svc_key_id_t key,
404 psa_algorithm_t alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 const unsigned char *input1, size_t input1_length,
406 const unsigned char *input2, size_t input2_length,
407 size_t capacity)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100408{
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 PSA_ASSERT(psa_key_derivation_setup(operation, alg));
410 if (PSA_ALG_IS_HKDF(alg)) {
411 PSA_ASSERT(psa_key_derivation_input_bytes(operation,
412 PSA_KEY_DERIVATION_INPUT_SALT,
413 input1, input1_length));
414 PSA_ASSERT(psa_key_derivation_input_key(operation,
415 PSA_KEY_DERIVATION_INPUT_SECRET,
416 key));
417 PSA_ASSERT(psa_key_derivation_input_bytes(operation,
418 PSA_KEY_DERIVATION_INPUT_INFO,
419 input2,
420 input2_length));
Kusumit Ghoderao2c4264b2023-12-01 16:41:26 +0530421 } else if (PSA_ALG_IS_HKDF_EXTRACT(alg)) {
422 PSA_ASSERT(psa_key_derivation_input_bytes(operation,
423 PSA_KEY_DERIVATION_INPUT_SALT,
424 input1, input1_length));
425 PSA_ASSERT(psa_key_derivation_input_key(operation,
426 PSA_KEY_DERIVATION_INPUT_SECRET,
427 key));
428 } else if (PSA_ALG_IS_HKDF_EXPAND(alg)) {
429 PSA_ASSERT(psa_key_derivation_input_key(operation,
430 PSA_KEY_DERIVATION_INPUT_SECRET,
431 key));
432 PSA_ASSERT(psa_key_derivation_input_bytes(operation,
433 PSA_KEY_DERIVATION_INPUT_INFO,
434 input2,
435 input2_length));
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 } else if (PSA_ALG_IS_TLS12_PRF(alg) ||
437 PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
438 PSA_ASSERT(psa_key_derivation_input_bytes(operation,
439 PSA_KEY_DERIVATION_INPUT_SEED,
440 input1, input1_length));
441 PSA_ASSERT(psa_key_derivation_input_key(operation,
442 PSA_KEY_DERIVATION_INPUT_SECRET,
443 key));
444 PSA_ASSERT(psa_key_derivation_input_bytes(operation,
445 PSA_KEY_DERIVATION_INPUT_LABEL,
446 input2, input2_length));
Kusumit Ghoderaoac7a04a2023-08-18 13:47:47 +0530447 } else if (PSA_ALG_IS_PBKDF2(alg)) {
Kusumit Ghoderaoac7a04a2023-08-18 13:47:47 +0530448 PSA_ASSERT(psa_key_derivation_input_integer(operation,
449 PSA_KEY_DERIVATION_INPUT_COST,
Kusumit Ghoderao94d31902023-09-05 19:30:22 +0530450 1U));
Kusumit Ghoderaoac7a04a2023-08-18 13:47:47 +0530451 PSA_ASSERT(psa_key_derivation_input_bytes(operation,
452 PSA_KEY_DERIVATION_INPUT_SALT,
453 input2,
454 input2_length));
455 PSA_ASSERT(psa_key_derivation_input_key(operation,
456 PSA_KEY_DERIVATION_INPUT_PASSWORD,
457 key));
Kusumit Ghoderao2c4264b2023-12-01 16:41:26 +0530458 } else if (alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
459 PSA_ASSERT(psa_key_derivation_input_bytes(operation,
460 PSA_KEY_DERIVATION_INPUT_SECRET,
461 input1, input1_length));
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 } else {
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +0100463 TEST_FAIL("Key derivation algorithm not supported");
Gilles Peskinee78b0022021-02-13 00:41:11 +0100464 }
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (capacity != SIZE_MAX) {
467 PSA_ASSERT(psa_key_derivation_set_capacity(operation, capacity));
468 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return 1;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100471
472exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100474}
475
476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477static int exercise_key_derivation_key(mbedtls_svc_key_id_t key,
478 psa_key_usage_t usage,
479 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100480{
481 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
482 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 size_t input1_length = sizeof(input1);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100484 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 size_t input2_length = sizeof(input2);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100486 unsigned char output[1];
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 size_t capacity = sizeof(output);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (usage & PSA_KEY_USAGE_DERIVE) {
490 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
491 input1, input1_length,
492 input2, input2_length,
493 capacity)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100494 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
498 output,
499 capacity));
500 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100501 }
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return 1;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100504
505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100507}
508
509/* We need two keys to exercise key agreement. Exercise the
510 * private key against its own public key. */
511psa_status_t mbedtls_test_psa_key_agreement_with_self(
512 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 mbedtls_svc_key_id_t key)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100514{
515 psa_key_type_t private_key_type;
516 psa_key_type_t public_key_type;
517 size_t key_bits;
518 uint8_t *public_key = NULL;
519 size_t public_key_length;
520 /* Return GENERIC_ERROR if something other than the final call to
521 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
522 * but it's good enough: callers will report it as a failed test anyway. */
523 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
527 private_key_type = psa_get_key_type(&attributes);
528 key_bits = psa_get_key_bits(&attributes);
529 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type);
530 public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits);
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100531 TEST_CALLOC(public_key, public_key_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 PSA_ASSERT(psa_export_public_key(key, public_key, public_key_length,
533 &public_key_length));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100534
535 status = psa_key_derivation_key_agreement(
536 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 public_key, public_key_length);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100538exit:
539 /*
540 * Key attributes may have been returned by psa_get_key_attributes()
541 * thus reset them as required.
542 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 psa_reset_key_attributes(&attributes);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 mbedtls_free(public_key);
546 return status;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100547}
548
549/* We need two keys to exercise key agreement. Exercise the
550 * private key against its own public key. */
551psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
552 psa_algorithm_t alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 mbedtls_svc_key_id_t key)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100554{
555 psa_key_type_t private_key_type;
556 psa_key_type_t public_key_type;
557 size_t key_bits;
558 uint8_t *public_key = NULL;
559 size_t public_key_length;
560 uint8_t output[1024];
561 size_t output_length;
562 /* Return GENERIC_ERROR if something other than the final call to
563 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
564 * but it's good enough: callers will report it as a failed test anyway. */
565 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
569 private_key_type = psa_get_key_type(&attributes);
570 key_bits = psa_get_key_bits(&attributes);
571 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(private_key_type);
572 public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_key_type, key_bits);
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100573 TEST_CALLOC(public_key, public_key_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 PSA_ASSERT(psa_export_public_key(key,
575 public_key, public_key_length,
576 &public_key_length));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 status = psa_raw_key_agreement(alg, key,
579 public_key, public_key_length,
580 output, sizeof(output), &output_length);
581 if (status == PSA_SUCCESS) {
582 TEST_ASSERT(output_length <=
583 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(private_key_type,
584 key_bits));
585 TEST_ASSERT(output_length <=
586 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
gabor-mezei-armceface22021-01-21 12:26:17 +0100587 }
588
Gilles Peskinee78b0022021-02-13 00:41:11 +0100589exit:
590 /*
591 * Key attributes may have been returned by psa_get_key_attributes()
592 * thus reset them as required.
593 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 psa_reset_key_attributes(&attributes);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 mbedtls_free(public_key);
597 return status;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100598}
599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600static int exercise_raw_key_agreement_key(mbedtls_svc_key_id_t key,
601 psa_key_usage_t usage,
602 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100603{
604 int ok = 0;
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (usage & PSA_KEY_USAGE_DERIVE) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100607 /* We need two keys to exercise key agreement. Exercise the
608 * private key against its own public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 PSA_ASSERT(mbedtls_test_psa_raw_key_agreement_with_self(alg, key));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100610 }
611 ok = 1;
612
613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return ok;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100615}
616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617static int exercise_key_agreement_key(mbedtls_svc_key_id_t key,
618 psa_key_usage_t usage,
619 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100620{
621 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezeidc3f3bb2022-07-01 15:06:34 +0200622 unsigned char input[1] = { 0 };
Gilles Peskinee78b0022021-02-13 00:41:11 +0100623 unsigned char output[1];
624 int ok = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
Przemek Stekiel6c9fd612022-06-14 14:41:42 +0200626 psa_status_t expected_key_agreement_status = PSA_SUCCESS;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (usage & PSA_KEY_USAGE_DERIVE) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100629 /* We need two keys to exercise key agreement. Exercise the
630 * private key against its own public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
632 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
633 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
634 PSA_ASSERT(psa_key_derivation_input_bytes(
635 &operation, PSA_KEY_DERIVATION_INPUT_SEED,
636 input, sizeof(input)));
Gilles Peskineaa3449d2022-03-19 16:04:30 +0100637 }
638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
640 PSA_ASSERT(psa_key_derivation_input_bytes(
641 &operation, PSA_KEY_DERIVATION_INPUT_SALT,
642 input, sizeof(input)));
Przemek Stekield8987452022-06-14 11:41:52 +0200643 }
644
Przemek Stekiel6c9fd612022-06-14 14:41:42 +0200645 /* For HKDF_EXPAND input secret may fail as secret size may not match
646 to expected PRK size. In practice it means that key bits must match
647 hash length. Otherwise test should fail with INVALID_ARGUMENT. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel6c9fd612022-06-14 14:41:42 +0200649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
651 size_t key_bits = psa_get_key_bits(&attributes);
652 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Przemek Stekiel6c9fd612022-06-14 14:41:42 +0200653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (PSA_BITS_TO_BYTES(key_bits) != PSA_HASH_LENGTH(hash_alg)) {
Przemek Stekiel6c9fd612022-06-14 14:41:42 +0200655 expected_key_agreement_status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 }
Przemek Stekiel6c9fd612022-06-14 14:41:42 +0200657 }
658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(&operation, key),
660 expected_key_agreement_status);
Przemek Stekiel6c9fd612022-06-14 14:41:42 +0200661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (expected_key_agreement_status != PSA_SUCCESS) {
663 return 1;
664 }
Gilles Peskineaa3449d2022-03-19 16:04:30 +0100665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
667 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
668 PSA_ASSERT(psa_key_derivation_input_bytes(
669 &operation, PSA_KEY_DERIVATION_INPUT_LABEL,
670 input, sizeof(input)));
671 } else if (PSA_ALG_IS_HKDF(kdf_alg) || PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
672 PSA_ASSERT(psa_key_derivation_input_bytes(
673 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
674 input, sizeof(input)));
Gilles Peskineaa3449d2022-03-19 16:04:30 +0100675 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
677 output,
678 sizeof(output)));
679 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100680 }
681 ok = 1;
682
683exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return ok;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100685}
686
687int mbedtls_test_psa_exported_key_sanity_check(
688 psa_key_type_t type, size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 const uint8_t *exported, size_t exported_length)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100690{
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (PSA_KEY_TYPE_IS_UNSTRUCTURED(type)) {
694 TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits));
695 } else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100696
Ronald Cron64df7382021-07-06 09:23:06 +0200697#if defined(MBEDTLS_ASN1_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
699 uint8_t *p = (uint8_t *) exported;
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100700 const uint8_t *end = exported + exported_length;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100701 size_t len;
702 /* RSAPrivateKey ::= SEQUENCE {
703 * version INTEGER, -- must be 0
704 * modulus INTEGER, -- n
705 * publicExponent INTEGER, -- e
706 * privateExponent INTEGER, -- d
707 * prime1 INTEGER, -- p
708 * prime2 INTEGER, -- q
709 * exponent1 INTEGER, -- d mod (p-1)
710 * exponent2 INTEGER, -- d mod (q-1)
711 * coefficient INTEGER, -- (inverse of q) mod p
712 * }
713 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len,
715 MBEDTLS_ASN1_SEQUENCE |
716 MBEDTLS_ASN1_CONSTRUCTED), 0);
717 TEST_EQUAL(len, end - p);
718 if (!mbedtls_test_asn1_skip_integer(&p, end, 0, 0, 0)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100719 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 }
721 if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100722 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 }
724 if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100725 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100727 /* Require d to be at least half the size of n. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits, 1)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100731 /* Require p and q to be at most half the size of n, rounded up. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits / 2 + 1, 1)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100733 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 }
735 if (!mbedtls_test_asn1_skip_integer(&p, end, bits / 2, bits / 2 + 1, 1)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 }
738 if (!mbedtls_test_asn1_skip_integer(&p, end, 1, bits / 2 + 1, 0)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100739 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 }
741 if (!mbedtls_test_asn1_skip_integer(&p, end, 1, bits / 2 + 1, 0)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100742 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 }
744 if (!mbedtls_test_asn1_skip_integer(&p, end, 1, bits / 2 + 1, 0)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100745 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 }
747 TEST_EQUAL(p - end, 0);
gabor-mezei-armceface22021-01-21 12:26:17 +0100748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE);
750 } else
Ronald Cron64df7382021-07-06 09:23:06 +0200751#endif /* MBEDTLS_ASN1_PARSE_C */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100754 /* Just the secret value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 TEST_EQUAL(exported_length, PSA_BITS_TO_BYTES(bits));
gabor-mezei-armceface22021-01-21 12:26:17 +0100756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 TEST_ASSERT(exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE);
758 } else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100759
Ronald Cron64df7382021-07-06 09:23:06 +0200760#if defined(MBEDTLS_ASN1_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (type == PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
762 uint8_t *p = (uint8_t *) exported;
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100763 const uint8_t *end = exported + exported_length;
Gilles Peskinead557e52021-02-14 01:19:21 +0100764 size_t len;
765 /* RSAPublicKey ::= SEQUENCE {
766 * modulus INTEGER, -- n
767 * publicExponent INTEGER } -- e
768 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 TEST_EQUAL(mbedtls_asn1_get_tag(&p, end, &len,
770 MBEDTLS_ASN1_SEQUENCE |
771 MBEDTLS_ASN1_CONSTRUCTED),
772 0);
773 TEST_EQUAL(len, end - p);
774 if (!mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)) {
Gilles Peskinead557e52021-02-14 01:19:21 +0100775 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 }
777 if (!mbedtls_test_asn1_skip_integer(&p, end, 2, bits, 1)) {
Gilles Peskinead557e52021-02-14 01:19:21 +0100778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 }
780 TEST_EQUAL(p - end, 0);
gabor-mezei-armceface22021-01-21 12:26:17 +0100781
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 TEST_ASSERT(exported_length <=
784 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits));
785 TEST_ASSERT(exported_length <=
786 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
787 } else
Ronald Cron64df7382021-07-06 09:23:06 +0200788#endif /* MBEDTLS_ASN1_PARSE_C */
Gilles Peskinead557e52021-02-14 01:19:21 +0100789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type)) {
gabor-mezei-armceface22021-01-21 12:26:17 +0100791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 TEST_ASSERT(exported_length <=
793 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits));
794 TEST_ASSERT(exported_length <=
795 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
gabor-mezei-armceface22021-01-21 12:26:17 +0100796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_MONTGOMERY) {
Gilles Peskinead557e52021-02-14 01:19:21 +0100798 /* The representation of an ECC Montgomery public key is
799 * the raw compressed point */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 TEST_EQUAL(PSA_BITS_TO_BYTES(bits), exported_length);
Stephan Koch6eb73112023-03-03 17:48:40 +0100801 } else if (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_TWISTED_EDWARDS) {
oberon-sk6d501732023-02-13 12:13:20 +0100802 /* The representation of an ECC Edwards public key is
803 * the raw compressed point */
Stephan Koch6eb73112023-03-03 17:48:40 +0100804 TEST_EQUAL(PSA_BITS_TO_BYTES(bits + 1), exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 } else {
Gilles Peskinead557e52021-02-14 01:19:21 +0100806 /* The representation of an ECC Weierstrass public key is:
807 * - The byte 0x04;
808 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
809 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
810 * - where m is the bit size associated with the curve.
811 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 TEST_EQUAL(1 + 2 * PSA_BITS_TO_BYTES(bits), exported_length);
813 TEST_EQUAL(exported[0], 4);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100814 }
Przemek Stekiel7cf26df2022-12-01 15:09:40 +0100815 } else
816 if (PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type) || PSA_KEY_TYPE_IS_DH_KEY_PAIR(type)) {
Przemek Stekiel4c0da512023-04-27 13:04:20 +0200817 TEST_ASSERT(exported_length ==
Przemek Stekiel654bef02022-12-15 13:28:02 +0100818 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(type, bits));
819 TEST_ASSERT(exported_length <=
820 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Valerio Setti2dbc3062023-04-13 12:19:57 +0200821 } else {
Andrzej Kurek57d2f132022-01-17 15:26:24 +0100822 (void) exported;
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +0100823 TEST_FAIL("Sanity check not implemented for this key type");
Gilles Peskinead557e52021-02-14 01:19:21 +0100824 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100825
Gilles Peskinecc9db302021-02-14 01:29:52 +0100826#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (type == PSA_KEY_TYPE_DES) {
Gilles Peskinecc9db302021-02-14 01:29:52 +0100828 /* Check the parity bits. */
829 unsigned i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 for (i = 0; i < bits / 8; i++) {
Gilles Peskinecc9db302021-02-14 01:29:52 +0100831 unsigned bit_count = 0;
832 unsigned m;
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 for (m = 1; m <= 0x100; m <<= 1) {
834 if (exported[i] & m) {
Gilles Peskinecc9db302021-02-14 01:29:52 +0100835 ++bit_count;
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 }
Gilles Peskinecc9db302021-02-14 01:29:52 +0100837 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 TEST_ASSERT(bit_count % 2 != 0);
Gilles Peskinecc9db302021-02-14 01:29:52 +0100839 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100840 }
Gilles Peskinecc9db302021-02-14 01:29:52 +0100841#endif
Gilles Peskinee78b0022021-02-13 00:41:11 +0100842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 return 1;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100844
845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 return 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100847}
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849static int exercise_export_key(mbedtls_svc_key_id_t key,
850 psa_key_usage_t usage)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100851{
852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
853 uint8_t *exported = NULL;
854 size_t exported_size = 0;
855 size_t exported_length = 0;
856 int ok = 0;
857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100859
860 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 psa_get_key_type(&attributes),
862 psa_get_key_bits(&attributes));
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100863 TEST_CALLOC(exported, exported_size);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if ((usage & PSA_KEY_USAGE_EXPORT) == 0 &&
866 !PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(&attributes))) {
867 TEST_EQUAL(psa_export_key(key, exported,
868 exported_size, &exported_length),
869 PSA_ERROR_NOT_PERMITTED);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100870 ok = 1;
871 goto exit;
872 }
873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 PSA_ASSERT(psa_export_key(key,
875 exported, exported_size,
876 &exported_length));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100877 ok = mbedtls_test_psa_exported_key_sanity_check(
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 psa_get_key_type(&attributes), psa_get_key_bits(&attributes),
879 exported, exported_length);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100880
881exit:
882 /*
883 * Key attributes may have been returned by psa_get_key_attributes()
884 * thus reset them as required.
885 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 psa_reset_key_attributes(&attributes);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 mbedtls_free(exported);
889 return ok;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100890}
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892static int exercise_export_public_key(mbedtls_svc_key_id_t key)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100893{
894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
895 psa_key_type_t public_type;
896 uint8_t *exported = NULL;
897 size_t exported_size = 0;
898 size_t exported_length = 0;
899 int ok = 0;
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
902 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(psa_get_key_type(&attributes))) {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100903 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 psa_get_key_type(&attributes),
905 psa_get_key_bits(&attributes));
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100906 TEST_CALLOC(exported, exported_size);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 TEST_EQUAL(psa_export_public_key(key, exported,
909 exported_size, &exported_length),
910 PSA_ERROR_INVALID_ARGUMENT);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100911 ok = 1;
912 goto exit;
913 }
914
915 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 psa_get_key_type(&attributes));
917 exported_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type,
918 psa_get_key_bits(&attributes));
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100919 TEST_CALLOC(exported, exported_size);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 PSA_ASSERT(psa_export_public_key(key,
922 exported, exported_size,
923 &exported_length));
Gilles Peskinee78b0022021-02-13 00:41:11 +0100924 ok = mbedtls_test_psa_exported_key_sanity_check(
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 public_type, psa_get_key_bits(&attributes),
926 exported, exported_length);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100927
928exit:
929 /*
930 * Key attributes may have been returned by psa_get_key_attributes()
931 * thus reset them as required.
932 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 psa_reset_key_attributes(&attributes);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 mbedtls_free(exported);
936 return ok;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100937}
938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939int mbedtls_test_psa_exercise_key(mbedtls_svc_key_id_t key,
940 psa_key_usage_t usage,
941 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100942{
Gilles Peskine2385f712021-02-14 01:34:21 +0100943 int ok = 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (!check_key_attributes_sanity(key)) {
946 return 0;
947 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (alg == 0) {
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800950 ok = 1; /* If no algorithm, do nothing (used for raw data "keys"). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 } else if (PSA_ALG_IS_MAC(alg)) {
952 ok = exercise_mac_key(key, usage, alg);
953 } else if (PSA_ALG_IS_CIPHER(alg)) {
954 ok = exercise_cipher_key(key, usage, alg);
955 } else if (PSA_ALG_IS_AEAD(alg)) {
956 ok = exercise_aead_key(key, usage, alg);
957 } else if (PSA_ALG_IS_SIGN(alg)) {
958 ok = exercise_signature_key(key, usage, alg);
959 } else if (PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) {
960 ok = exercise_asymmetric_encryption_key(key, usage, alg);
961 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
962 ok = exercise_key_derivation_key(key, usage, alg);
963 } else if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
964 ok = exercise_raw_key_agreement_key(key, usage, alg);
965 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
966 ok = exercise_key_agreement_key(key, usage, alg);
967 } else {
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +0100968 TEST_FAIL("No code to exercise this category of algorithm");
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 ok = ok && exercise_export_key(key, usage);
972 ok = ok && exercise_export_public_key(key);
Gilles Peskinee78b0022021-02-13 00:41:11 +0100973
Gilles Peskine2385f712021-02-14 01:34:21 +0100974exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return ok;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100976}
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978psa_key_usage_t mbedtls_test_psa_usage_to_exercise(psa_key_type_t type,
979 psa_algorithm_t alg)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100980{
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (PSA_ALG_IS_MAC(alg) || PSA_ALG_IS_SIGN(alg)) {
982 if (PSA_ALG_IS_SIGN_HASH(alg)) {
983 if (PSA_ALG_SIGN_GET_HASH(alg)) {
984 return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ?
985 PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_MESSAGE :
986 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH |
987 PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE;
988 }
989 } else if (PSA_ALG_IS_SIGN_MESSAGE(alg)) {
990 return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ?
991 PSA_KEY_USAGE_VERIFY_MESSAGE :
992 PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE;
gabor-mezei-arm041887b2021-05-11 13:29:24 +0200993 }
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ?
996 PSA_KEY_USAGE_VERIFY_HASH :
997 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH;
998 } else if (PSA_ALG_IS_CIPHER(alg) || PSA_ALG_IS_AEAD(alg) ||
999 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg)) {
1000 return PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ?
1001 PSA_KEY_USAGE_ENCRYPT :
1002 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
1003 } else if (PSA_ALG_IS_KEY_DERIVATION(alg) ||
1004 PSA_ALG_IS_KEY_AGREEMENT(alg)) {
1005 return PSA_KEY_USAGE_DERIVE;
1006 } else {
1007 return 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +01001008 }
1009
1010}
Gilles Peskine66e7b902021-02-12 23:40:58 +01001011
Gilles Peskine34955672024-02-12 14:19:24 +01001012int mbedtls_test_can_exercise_psa_algorithm(psa_algorithm_t alg)
1013{
1014 /* Reject algorithms that we know are not supported. Default to
1015 * attempting exercise, so that if an algorithm is missing from this
1016 * function, the result will be a test failure and not silently
1017 * omitting exercise. */
1018#if !defined(PSA_WANT_ALG_RSA_PKCS1V15_CRYPT)
1019 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
1020 return 0;
1021 }
1022#endif
1023#if !defined(PSA_WANT_ALG_RSA_PKCS1V15_SIGN)
1024 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
1025 return 0;
1026 }
1027#endif
1028#if !defined(PSA_WANT_ALG_RSA_PSS)
1029 if (PSA_ALG_IS_RSA_PSS_STANDARD_SALT(alg)) {
1030 return 0;
1031 }
1032#endif
1033#if !defined(PSA_WANT_ALG_RSA_PSS_ANY_SALT)
1034 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
1035 return 0;
1036 }
1037#endif
1038#if !defined(PSA_WANT_ALG_ECDSA)
1039 if (PSA_ALG_IS_ECDSA(alg)) {
1040 return 0;
1041 }
1042#endif
1043#if !defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA)
1044 if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) {
1045 return 0;
1046 }
1047#endif
1048#if !defined(PSA_WANT_ALG_ECDH)
1049 if (PSA_ALG_IS_ECDH(alg)) {
1050 return 0;
1051 }
1052#endif
1053 (void) alg;
1054 return 1;
1055}
1056
Gilles Peskine66e7b902021-02-12 23:40:58 +01001057#endif /* MBEDTLS_PSA_CRYPTO_C */