1 /*
2  *  PSA crypto client code
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 #include "psa/crypto.h"
11 
12 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
13 
14 #include <string.h>
15 #include "mbedtls/platform.h"
16 
psa_reset_key_attributes(psa_key_attributes_t * attributes)17 void psa_reset_key_attributes(psa_key_attributes_t *attributes)
18 {
19     mbedtls_free(attributes->domain_parameters);
20     memset(attributes, 0, sizeof(*attributes));
21 }
22 
psa_set_key_domain_parameters(psa_key_attributes_t * attributes,psa_key_type_t type,const uint8_t * data,size_t data_length)23 psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
24                                            psa_key_type_t type,
25                                            const uint8_t *data,
26                                            size_t data_length)
27 {
28     uint8_t *copy = NULL;
29 
30     if (data_length != 0) {
31         copy = mbedtls_calloc(1, data_length);
32         if (copy == NULL) {
33             return PSA_ERROR_INSUFFICIENT_MEMORY;
34         }
35         memcpy(copy, data, data_length);
36     }
37     /* After this point, this function is guaranteed to succeed, so it
38      * can start modifying `*attributes`. */
39 
40     if (attributes->domain_parameters != NULL) {
41         mbedtls_free(attributes->domain_parameters);
42         attributes->domain_parameters = NULL;
43         attributes->domain_parameters_size = 0;
44     }
45 
46     attributes->domain_parameters = copy;
47     attributes->domain_parameters_size = data_length;
48     attributes->core.type = type;
49     return PSA_SUCCESS;
50 }
51 
psa_get_key_domain_parameters(const psa_key_attributes_t * attributes,uint8_t * data,size_t data_size,size_t * data_length)52 psa_status_t psa_get_key_domain_parameters(
53     const psa_key_attributes_t *attributes,
54     uint8_t *data, size_t data_size, size_t *data_length)
55 {
56     if (attributes->domain_parameters_size > data_size) {
57         return PSA_ERROR_BUFFER_TOO_SMALL;
58     }
59     *data_length = attributes->domain_parameters_size;
60     if (attributes->domain_parameters_size != 0) {
61         memcpy(data, attributes->domain_parameters,
62                attributes->domain_parameters_size);
63     }
64     return PSA_SUCCESS;
65 }
66 
67 #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
68