Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, Arm Limited. All rights reserved. |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 8 | #include <stddef.h> |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 10 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 11 | #include "tfm_mbedcrypto_include.h" |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 12 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 13 | #include "tfm_crypto_api.h" |
| 14 | #include "tfm_crypto_defs.h" |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 15 | #include "tfm_crypto_private.h" |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 16 | |
David Hu | 42e77b5 | 2021-07-24 21:14:30 +0800 | [diff] [blame] | 17 | #ifndef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
| 18 | #error "MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER must be selected in Mbed TLS config file" |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 19 | #endif |
David Hu | 105b487 | 2021-05-19 16:43:19 +0800 | [diff] [blame] | 20 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 21 | /*! |
| 22 | * \defgroup public Public functions |
| 23 | * |
| 24 | */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 25 | /*!@{*/ |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 26 | psa_status_t tfm_crypto_key_attributes_from_client( |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 27 | const struct psa_client_key_attributes_s *client_key_attr, |
| 28 | int32_t client_id, |
| 29 | psa_key_attributes_t *key_attributes) |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 30 | { |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 31 | psa_core_key_attributes_t *core; |
| 32 | |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 33 | if (client_key_attr == NULL || key_attributes == NULL) { |
| 34 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 35 | } |
| 36 | |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 37 | *key_attributes = psa_key_attributes_init(); |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 38 | core = &(key_attributes->MBEDTLS_PRIVATE(core)); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 39 | |
| 40 | /* Copy core key attributes from the client core key attributes */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 41 | core->MBEDTLS_PRIVATE(type) = client_key_attr->type; |
| 42 | core->MBEDTLS_PRIVATE(lifetime) = client_key_attr->lifetime; |
| 43 | core->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = |
| 44 | client_key_attr->usage; |
| 45 | core->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = |
| 46 | client_key_attr->alg; |
| 47 | core->MBEDTLS_PRIVATE(bits) = client_key_attr->bits; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 48 | |
| 49 | /* Use the client key id as the key_id and its partition id as the owner */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 50 | core->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = client_key_attr->id; |
| 51 | core->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = client_id; |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 52 | |
| 53 | return PSA_SUCCESS; |
| 54 | } |
| 55 | |
| 56 | psa_status_t tfm_crypto_key_attributes_to_client( |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 57 | const psa_key_attributes_t *key_attributes, |
| 58 | struct psa_client_key_attributes_s *client_key_attr) |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 59 | { |
| 60 | if (client_key_attr == NULL || key_attributes == NULL) { |
| 61 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 62 | } |
| 63 | |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 64 | struct psa_client_key_attributes_s v = PSA_CLIENT_KEY_ATTRIBUTES_INIT; |
| 65 | *client_key_attr = v; |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 66 | psa_core_key_attributes_t core = key_attributes->MBEDTLS_PRIVATE(core); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 67 | |
Soby Mathew | d7b79f2 | 2020-05-21 15:06:54 +0100 | [diff] [blame] | 68 | /* Copy core key attributes from the client core key attributes */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 69 | client_key_attr->type = core.MBEDTLS_PRIVATE(type); |
| 70 | client_key_attr->lifetime = core.MBEDTLS_PRIVATE(lifetime); |
| 71 | client_key_attr->usage = core.MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage); |
| 72 | client_key_attr->alg = core.MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg); |
| 73 | client_key_attr->bits = core.MBEDTLS_PRIVATE(bits); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 74 | |
| 75 | /* Return the key_id as the client key id, do not return the owner */ |
Summer Qin | 359167d | 2021-07-05 18:11:50 +0800 | [diff] [blame] | 76 | client_key_attr->id = core.MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id); |
Jamie Fox | 98ab441 | 2020-01-17 17:12:30 +0000 | [diff] [blame] | 77 | |
| 78 | return PSA_SUCCESS; |
| 79 | } |
| 80 | |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 81 | psa_status_t tfm_crypto_encode_id_and_owner(psa_key_id_t key_id, |
| 82 | mbedtls_svc_key_id_t *enc_key_ptr) |
| 83 | { |
| 84 | int32_t partition_id = 0; |
| 85 | psa_status_t status = tfm_crypto_get_caller_id(&partition_id); |
| 86 | |
| 87 | if (status != PSA_SUCCESS) { |
| 88 | return status; |
| 89 | } |
| 90 | |
| 91 | /* If Null Pointer, return PSA_ERROR_PROGRAMMER_ERROR */ |
| 92 | if (enc_key_ptr == NULL) { |
| 93 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 94 | } |
| 95 | |
| 96 | /* Use the client key id as the key_id and its partition id as the owner */ |
| 97 | *enc_key_ptr = mbedtls_svc_key_id_make(partition_id, key_id); |
| 98 | |
| 99 | return PSA_SUCCESS; |
| 100 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 101 | /*!@}*/ |