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