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