blob: e84cf3015bd2081fa1dcd675c0d96735982f78c9 [file] [log] [blame]
Ronald Crond7906322021-01-28 16:07:56 +01001/*
2 * PSA crypto client code
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"
Ronald Crond7906322021-01-28 16:07:56 +010022#include "psa_crypto_service_integration.h"
23#include "psa/crypto.h"
24
Ronald Cron395889f2021-02-09 12:36:49 +010025#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
26
Ronald Cron21b56162021-01-28 16:36:00 +010027#include <string.h>
28#include "mbedtls/platform.h"
29#if !defined(MBEDTLS_PLATFORM_C)
30#define mbedtls_calloc calloc
31#define mbedtls_free free
32#endif
33
34void psa_reset_key_attributes( psa_key_attributes_t *attributes )
35{
36 mbedtls_free( attributes->domain_parameters );
37 memset( attributes, 0, sizeof( *attributes ) );
38}
39
40psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
41 psa_key_type_t type,
42 const uint8_t *data,
43 size_t data_length )
44{
45 uint8_t *copy = NULL;
46
47 if( data_length != 0 )
48 {
49 copy = mbedtls_calloc( 1, data_length );
50 if( copy == NULL )
51 return( PSA_ERROR_INSUFFICIENT_MEMORY );
52 memcpy( copy, data, data_length );
53 }
54 /* After this point, this function is guaranteed to succeed, so it
55 * can start modifying `*attributes`. */
56
57 if( attributes->domain_parameters != NULL )
58 {
59 mbedtls_free( attributes->domain_parameters );
60 attributes->domain_parameters = NULL;
61 attributes->domain_parameters_size = 0;
62 }
63
64 attributes->domain_parameters = copy;
65 attributes->domain_parameters_size = data_length;
66 attributes->core.type = type;
67 return( PSA_SUCCESS );
68}
69
70psa_status_t psa_get_key_domain_parameters(
71 const psa_key_attributes_t *attributes,
72 uint8_t *data, size_t data_size, size_t *data_length )
73{
74 if( attributes->domain_parameters_size > data_size )
75 return( PSA_ERROR_BUFFER_TOO_SMALL );
76 *data_length = attributes->domain_parameters_size;
77 if( attributes->domain_parameters_size != 0 )
78 memcpy( data, attributes->domain_parameters,
79 attributes->domain_parameters_size );
80 return( PSA_SUCCESS );
81}
82
Ronald Cron395889f2021-02-09 12:36:49 +010083#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */