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