1 /*
2 * PSA ECP layer on top of Mbed TLS crypto
3 */
4 /*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 */
8
9 #include "common.h"
10
11 #if defined(MBEDTLS_PSA_CRYPTO_C)
12
13 #include <psa/crypto.h>
14 #include "psa_crypto_core.h"
15 #include "psa_crypto_ecp.h"
16 #include "psa_crypto_random_impl.h"
17 #include "md_psa.h"
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include "mbedtls/platform.h"
22
23 #include <mbedtls/ecdsa.h>
24 #include <mbedtls/ecdh.h>
25 #include <mbedtls/ecp.h>
26 #include <mbedtls/error.h>
27
28 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) || \
29 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
30 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
31 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
32 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
33 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
34 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
mbedtls_psa_ecp_load_representation(psa_key_type_t type,size_t curve_bits,const uint8_t * data,size_t data_length,mbedtls_ecp_keypair ** p_ecp)35 psa_status_t mbedtls_psa_ecp_load_representation(
36 psa_key_type_t type, size_t curve_bits,
37 const uint8_t *data, size_t data_length,
38 mbedtls_ecp_keypair **p_ecp)
39 {
40 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
41 psa_status_t status;
42 mbedtls_ecp_keypair *ecp = NULL;
43 size_t curve_bytes = data_length;
44 int explicit_bits = (curve_bits != 0);
45
46 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
47 PSA_KEY_TYPE_ECC_GET_FAMILY(type) != PSA_ECC_FAMILY_MONTGOMERY) {
48 /* A Weierstrass public key is represented as:
49 * - The byte 0x04;
50 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
51 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
52 * So its data length is 2m+1 where m is the curve size in bits.
53 */
54 if ((data_length & 1) == 0) {
55 return PSA_ERROR_INVALID_ARGUMENT;
56 }
57 curve_bytes = data_length / 2;
58
59 /* Montgomery public keys are represented in compressed format, meaning
60 * their curve_bytes is equal to the amount of input. */
61
62 /* Private keys are represented in uncompressed private random integer
63 * format, meaning their curve_bytes is equal to the amount of input. */
64 }
65
66 if (explicit_bits) {
67 /* With an explicit bit-size, the data must have the matching length. */
68 if (curve_bytes != PSA_BITS_TO_BYTES(curve_bits)) {
69 return PSA_ERROR_INVALID_ARGUMENT;
70 }
71 } else {
72 /* We need to infer the bit-size from the data. Since the only
73 * information we have is the length in bytes, the value of curve_bits
74 * at this stage is rounded up to the nearest multiple of 8. */
75 curve_bits = PSA_BYTES_TO_BITS(curve_bytes);
76 }
77
78 /* Allocate and initialize a key representation. */
79 ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
80 if (ecp == NULL) {
81 return PSA_ERROR_INSUFFICIENT_MEMORY;
82 }
83 mbedtls_ecp_keypair_init(ecp);
84
85 /* Load the group. */
86 grp_id = mbedtls_ecc_group_of_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(type),
87 curve_bits, !explicit_bits);
88 if (grp_id == MBEDTLS_ECP_DP_NONE) {
89 /* We can't distinguish between a nonsensical family/size combination
90 * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a
91 * well-regarded curve that Mbed TLS just doesn't know about (which
92 * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how
93 * curves that Mbed TLS knows about but for which support is disabled
94 * at build time, return NOT_SUPPORTED. */
95 status = PSA_ERROR_NOT_SUPPORTED;
96 goto exit;
97 }
98
99 status = mbedtls_to_psa_error(
100 mbedtls_ecp_group_load(&ecp->grp, grp_id));
101 if (status != PSA_SUCCESS) {
102 goto exit;
103 }
104
105 /* Load the key material. */
106 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
107 /* Load the public value. */
108 status = mbedtls_to_psa_error(
109 mbedtls_ecp_point_read_binary(&ecp->grp, &ecp->Q,
110 data,
111 data_length));
112 if (status != PSA_SUCCESS) {
113 goto exit;
114 }
115
116 /* Check that the point is on the curve. */
117 status = mbedtls_to_psa_error(
118 mbedtls_ecp_check_pubkey(&ecp->grp, &ecp->Q));
119 if (status != PSA_SUCCESS) {
120 goto exit;
121 }
122 } else {
123 /* Load and validate the secret value. */
124 status = mbedtls_to_psa_error(
125 mbedtls_ecp_read_key(ecp->grp.id,
126 ecp,
127 data,
128 data_length));
129 if (status != PSA_SUCCESS) {
130 goto exit;
131 }
132 }
133
134 *p_ecp = ecp;
135 exit:
136 if (status != PSA_SUCCESS) {
137 mbedtls_ecp_keypair_free(ecp);
138 mbedtls_free(ecp);
139 }
140
141 return status;
142 }
143 #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) ||
144 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
145 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
146 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
147 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
148 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
149 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
150
151 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
152 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
153 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
154
mbedtls_psa_ecp_import_key(const psa_key_attributes_t * attributes,const uint8_t * data,size_t data_length,uint8_t * key_buffer,size_t key_buffer_size,size_t * key_buffer_length,size_t * bits)155 psa_status_t mbedtls_psa_ecp_import_key(
156 const psa_key_attributes_t *attributes,
157 const uint8_t *data, size_t data_length,
158 uint8_t *key_buffer, size_t key_buffer_size,
159 size_t *key_buffer_length, size_t *bits)
160 {
161 psa_status_t status;
162 mbedtls_ecp_keypair *ecp = NULL;
163
164 /* Parse input */
165 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
166 attributes->core.bits,
167 data,
168 data_length,
169 &ecp);
170 if (status != PSA_SUCCESS) {
171 goto exit;
172 }
173
174 if (PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type) ==
175 PSA_ECC_FAMILY_MONTGOMERY) {
176 *bits = ecp->grp.nbits + 1;
177 } else {
178 *bits = ecp->grp.nbits;
179 }
180
181 /* Re-export the data to PSA export format. There is currently no support
182 * for other input formats then the export format, so this is a 1-1
183 * copy operation. */
184 status = mbedtls_psa_ecp_export_key(attributes->core.type,
185 ecp,
186 key_buffer,
187 key_buffer_size,
188 key_buffer_length);
189 exit:
190 /* Always free the PK object (will also free contained ECP context) */
191 mbedtls_ecp_keypair_free(ecp);
192 mbedtls_free(ecp);
193
194 return status;
195 }
196
mbedtls_psa_ecp_export_key(psa_key_type_t type,mbedtls_ecp_keypair * ecp,uint8_t * data,size_t data_size,size_t * data_length)197 psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type,
198 mbedtls_ecp_keypair *ecp,
199 uint8_t *data,
200 size_t data_size,
201 size_t *data_length)
202 {
203 psa_status_t status;
204
205 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
206 /* Check whether the public part is loaded */
207 if (mbedtls_ecp_is_zero(&ecp->Q)) {
208 /* Calculate the public key */
209 status = mbedtls_to_psa_error(
210 mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
211 mbedtls_psa_get_random,
212 MBEDTLS_PSA_RANDOM_STATE));
213 if (status != PSA_SUCCESS) {
214 return status;
215 }
216 }
217
218 status = mbedtls_to_psa_error(
219 mbedtls_ecp_point_write_binary(&ecp->grp, &ecp->Q,
220 MBEDTLS_ECP_PF_UNCOMPRESSED,
221 data_length,
222 data,
223 data_size));
224 if (status != PSA_SUCCESS) {
225 memset(data, 0, data_size);
226 }
227
228 return status;
229 } else {
230 if (data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits)) {
231 return PSA_ERROR_BUFFER_TOO_SMALL;
232 }
233
234 status = mbedtls_to_psa_error(
235 mbedtls_ecp_write_key(ecp,
236 data,
237 PSA_BITS_TO_BYTES(ecp->grp.nbits)));
238 if (status == PSA_SUCCESS) {
239 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
240 } else {
241 memset(data, 0, data_size);
242 }
243
244 return status;
245 }
246 }
247
mbedtls_psa_ecp_export_public_key(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,uint8_t * data,size_t data_size,size_t * data_length)248 psa_status_t mbedtls_psa_ecp_export_public_key(
249 const psa_key_attributes_t *attributes,
250 const uint8_t *key_buffer, size_t key_buffer_size,
251 uint8_t *data, size_t data_size, size_t *data_length)
252 {
253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
254 mbedtls_ecp_keypair *ecp = NULL;
255
256 status = mbedtls_psa_ecp_load_representation(
257 attributes->core.type, attributes->core.bits,
258 key_buffer, key_buffer_size, &ecp);
259 if (status != PSA_SUCCESS) {
260 return status;
261 }
262
263 status = mbedtls_psa_ecp_export_key(
264 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
265 PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type)),
266 ecp, data, data_size, data_length);
267
268 mbedtls_ecp_keypair_free(ecp);
269 mbedtls_free(ecp);
270
271 return status;
272 }
273 #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
274 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
275 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
276
277 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
mbedtls_psa_ecp_generate_key(const psa_key_attributes_t * attributes,uint8_t * key_buffer,size_t key_buffer_size,size_t * key_buffer_length)278 psa_status_t mbedtls_psa_ecp_generate_key(
279 const psa_key_attributes_t *attributes,
280 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
281 {
282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
284
285 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
286 attributes->core.type);
287 mbedtls_ecp_group_id grp_id =
288 mbedtls_ecc_group_of_psa(curve, attributes->core.bits, 0);
289
290 const mbedtls_ecp_curve_info *curve_info =
291 mbedtls_ecp_curve_info_from_grp_id(grp_id);
292 mbedtls_ecp_keypair ecp;
293
294 if (attributes->domain_parameters_size != 0) {
295 return PSA_ERROR_NOT_SUPPORTED;
296 }
297
298 if (grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL) {
299 return PSA_ERROR_NOT_SUPPORTED;
300 }
301
302 mbedtls_ecp_keypair_init(&ecp);
303 ret = mbedtls_ecp_gen_key(grp_id, &ecp,
304 mbedtls_psa_get_random,
305 MBEDTLS_PSA_RANDOM_STATE);
306 if (ret != 0) {
307 mbedtls_ecp_keypair_free(&ecp);
308 return mbedtls_to_psa_error(ret);
309 }
310
311 status = mbedtls_to_psa_error(
312 mbedtls_ecp_write_key(&ecp, key_buffer, key_buffer_size));
313
314 mbedtls_ecp_keypair_free(&ecp);
315
316 if (status == PSA_SUCCESS) {
317 *key_buffer_length = key_buffer_size;
318 }
319
320 return status;
321 }
322 #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE */
323
324 /****************************************************************/
325 /* ECDSA sign/verify */
326 /****************************************************************/
327
328 #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
329 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
mbedtls_psa_ecdsa_sign_hash(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * hash,size_t hash_length,uint8_t * signature,size_t signature_size,size_t * signature_length)330 psa_status_t mbedtls_psa_ecdsa_sign_hash(
331 const psa_key_attributes_t *attributes,
332 const uint8_t *key_buffer, size_t key_buffer_size,
333 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
334 uint8_t *signature, size_t signature_size, size_t *signature_length)
335 {
336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
337 mbedtls_ecp_keypair *ecp = NULL;
338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
339 size_t curve_bytes;
340 mbedtls_mpi r, s;
341
342 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
343 attributes->core.bits,
344 key_buffer,
345 key_buffer_size,
346 &ecp);
347 if (status != PSA_SUCCESS) {
348 return status;
349 }
350
351 curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits);
352 mbedtls_mpi_init(&r);
353 mbedtls_mpi_init(&s);
354
355 if (signature_size < 2 * curve_bytes) {
356 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
357 goto cleanup;
358 }
359
360 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) {
361 #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
362 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
363 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
364 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_ext(
365 &ecp->grp, &r, &s,
366 &ecp->d, hash,
367 hash_length, md_alg,
368 mbedtls_psa_get_random,
369 MBEDTLS_PSA_RANDOM_STATE));
370 #else
371 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
372 goto cleanup;
373 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
374 } else {
375 (void) alg;
376 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ecp->grp, &r, &s, &ecp->d,
377 hash, hash_length,
378 mbedtls_psa_get_random,
379 MBEDTLS_PSA_RANDOM_STATE));
380 }
381
382 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&r,
383 signature,
384 curve_bytes));
385 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&s,
386 signature + curve_bytes,
387 curve_bytes));
388 cleanup:
389 mbedtls_mpi_free(&r);
390 mbedtls_mpi_free(&s);
391 if (ret == 0) {
392 *signature_length = 2 * curve_bytes;
393 }
394
395 mbedtls_ecp_keypair_free(ecp);
396 mbedtls_free(ecp);
397
398 return mbedtls_to_psa_error(ret);
399 }
400
mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair * ecp)401 psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp)
402 {
403 int ret = 0;
404
405 /* Check whether the public part is loaded. If not, load it. */
406 if (mbedtls_ecp_is_zero(&ecp->Q)) {
407 ret = mbedtls_ecp_mul(&ecp->grp, &ecp->Q,
408 &ecp->d, &ecp->grp.G,
409 mbedtls_psa_get_random,
410 MBEDTLS_PSA_RANDOM_STATE);
411 }
412
413 return mbedtls_to_psa_error(ret);
414 }
415
mbedtls_psa_ecdsa_verify_hash(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * hash,size_t hash_length,const uint8_t * signature,size_t signature_length)416 psa_status_t mbedtls_psa_ecdsa_verify_hash(
417 const psa_key_attributes_t *attributes,
418 const uint8_t *key_buffer, size_t key_buffer_size,
419 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
420 const uint8_t *signature, size_t signature_length)
421 {
422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
423 mbedtls_ecp_keypair *ecp = NULL;
424 size_t curve_bytes;
425 mbedtls_mpi r, s;
426
427 (void) alg;
428
429 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
430 attributes->core.bits,
431 key_buffer,
432 key_buffer_size,
433 &ecp);
434 if (status != PSA_SUCCESS) {
435 return status;
436 }
437
438 curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits);
439 mbedtls_mpi_init(&r);
440 mbedtls_mpi_init(&s);
441
442 if (signature_length != 2 * curve_bytes) {
443 status = PSA_ERROR_INVALID_SIGNATURE;
444 goto cleanup;
445 }
446
447 status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&r,
448 signature,
449 curve_bytes));
450 if (status != PSA_SUCCESS) {
451 goto cleanup;
452 }
453
454 status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&s,
455 signature + curve_bytes,
456 curve_bytes));
457 if (status != PSA_SUCCESS) {
458 goto cleanup;
459 }
460
461 status = mbedtls_psa_ecp_load_public_part(ecp);
462 if (status != PSA_SUCCESS) {
463 goto cleanup;
464 }
465
466 status = mbedtls_to_psa_error(mbedtls_ecdsa_verify(&ecp->grp, hash,
467 hash_length, &ecp->Q,
468 &r, &s));
469 cleanup:
470 mbedtls_mpi_free(&r);
471 mbedtls_mpi_free(&s);
472 mbedtls_ecp_keypair_free(ecp);
473 mbedtls_free(ecp);
474
475 return status;
476 }
477
478 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
479 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
480
481 /****************************************************************/
482 /* ECDH Key Agreement */
483 /****************************************************************/
484
485 #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
mbedtls_psa_key_agreement_ecdh(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * peer_key,size_t peer_key_length,uint8_t * shared_secret,size_t shared_secret_size,size_t * shared_secret_length)486 psa_status_t mbedtls_psa_key_agreement_ecdh(
487 const psa_key_attributes_t *attributes,
488 const uint8_t *key_buffer, size_t key_buffer_size,
489 psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length,
490 uint8_t *shared_secret, size_t shared_secret_size,
491 size_t *shared_secret_length)
492 {
493 psa_status_t status;
494 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->core.type) ||
495 !PSA_ALG_IS_ECDH(alg)) {
496 return PSA_ERROR_INVALID_ARGUMENT;
497 }
498 mbedtls_ecp_keypair *ecp = NULL;
499 status = mbedtls_psa_ecp_load_representation(
500 attributes->core.type,
501 attributes->core.bits,
502 key_buffer,
503 key_buffer_size,
504 &ecp);
505 if (status != PSA_SUCCESS) {
506 return status;
507 }
508 mbedtls_ecp_keypair *their_key = NULL;
509 mbedtls_ecdh_context ecdh;
510 size_t bits = 0;
511 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ecp->grp.id, &bits);
512 mbedtls_ecdh_init(&ecdh);
513
514 status = mbedtls_psa_ecp_load_representation(
515 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
516 bits,
517 peer_key,
518 peer_key_length,
519 &their_key);
520 if (status != PSA_SUCCESS) {
521 goto exit;
522 }
523
524 status = mbedtls_to_psa_error(
525 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
526 if (status != PSA_SUCCESS) {
527 goto exit;
528 }
529 status = mbedtls_to_psa_error(
530 mbedtls_ecdh_get_params(&ecdh, ecp, MBEDTLS_ECDH_OURS));
531 if (status != PSA_SUCCESS) {
532 goto exit;
533 }
534
535 status = mbedtls_to_psa_error(
536 mbedtls_ecdh_calc_secret(&ecdh,
537 shared_secret_length,
538 shared_secret, shared_secret_size,
539 mbedtls_psa_get_random,
540 MBEDTLS_PSA_RANDOM_STATE));
541 if (status != PSA_SUCCESS) {
542 goto exit;
543 }
544 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
545 status = PSA_ERROR_CORRUPTION_DETECTED;
546 }
547 exit:
548 if (status != PSA_SUCCESS) {
549 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
550 }
551 mbedtls_ecdh_free(&ecdh);
552 mbedtls_ecp_keypair_free(their_key);
553 mbedtls_free(their_key);
554 mbedtls_ecp_keypair_free(ecp);
555 mbedtls_free(ecp);
556 return status;
557 }
558 #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
559
560
561 #endif /* MBEDTLS_PSA_CRYPTO_C */
562