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