blob: b731f00f05d53f593dc826b41f98c5e29271e140 [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Antonio de Angelis8908f472018-08-31 15:44:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Jamie Foxefd82732018-11-26 10:34:32 +00008#include <stddef.h>
Jamie Fox0e54ebc2019-04-09 14:21:04 +01009#include <stdint.h>
Antonio de Angelis8908f472018-08-31 15:44:25 +010010
Jamie Fox0e54ebc2019-04-09 14:21:04 +010011#include "tfm_mbedcrypto_include.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010012
Jamie Fox0e54ebc2019-04-09 14:21:04 +010013#include "tfm_crypto_api.h"
14#include "tfm_crypto_defs.h"
Soby Mathewd8abdfd2020-10-14 10:28:01 +010015#include "tfm_crypto_private.h"
Jamie Fox82b87ca2018-12-11 16:41:11 +000016
David Hu42e77b52021-07-24 21:14:30 +080017#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 Angelis60a6fe62019-06-18 15:27:34 +010019#endif
David Hu105b4872021-05-19 16:43:19 +080020
Antonio de Angelis8908f472018-08-31 15:44:25 +010021/*!
22 * \defgroup public Public functions
23 *
24 */
Antonio de Angelis8908f472018-08-31 15:44:25 +010025/*!@{*/
Jamie Fox98ab4412020-01-17 17:12:30 +000026psa_status_t tfm_crypto_key_attributes_from_client(
Maulik Patel28659c42021-01-06 14:09:22 +000027 const struct psa_client_key_attributes_s *client_key_attr,
28 int32_t client_id,
29 psa_key_attributes_t *key_attributes)
Jamie Fox98ab4412020-01-17 17:12:30 +000030{
Summer Qin359167d2021-07-05 18:11:50 +080031 psa_core_key_attributes_t *core;
32
Jamie Fox98ab4412020-01-17 17:12:30 +000033 if (client_key_attr == NULL || key_attributes == NULL) {
34 return PSA_ERROR_PROGRAMMER_ERROR;
35 }
36
Soby Mathewd7b79f22020-05-21 15:06:54 +010037 *key_attributes = psa_key_attributes_init();
Summer Qin359167d2021-07-05 18:11:50 +080038 core = &(key_attributes->MBEDTLS_PRIVATE(core));
Jamie Fox98ab4412020-01-17 17:12:30 +000039
40 /* Copy core key attributes from the client core key attributes */
Summer Qin359167d2021-07-05 18:11:50 +080041 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 Fox98ab4412020-01-17 17:12:30 +000048
49 /* Use the client key id as the key_id and its partition id as the owner */
Summer Qin359167d2021-07-05 18:11:50 +080050 core->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = client_key_attr->id;
51 core->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = client_id;
Jamie Fox98ab4412020-01-17 17:12:30 +000052
53 return PSA_SUCCESS;
54}
55
56psa_status_t tfm_crypto_key_attributes_to_client(
Maulik Patel28659c42021-01-06 14:09:22 +000057 const psa_key_attributes_t *key_attributes,
58 struct psa_client_key_attributes_s *client_key_attr)
Jamie Fox98ab4412020-01-17 17:12:30 +000059{
60 if (client_key_attr == NULL || key_attributes == NULL) {
61 return PSA_ERROR_PROGRAMMER_ERROR;
62 }
63
Soby Mathewd7b79f22020-05-21 15:06:54 +010064 struct psa_client_key_attributes_s v = PSA_CLIENT_KEY_ATTRIBUTES_INIT;
65 *client_key_attr = v;
Summer Qin359167d2021-07-05 18:11:50 +080066 psa_core_key_attributes_t core = key_attributes->MBEDTLS_PRIVATE(core);
Jamie Fox98ab4412020-01-17 17:12:30 +000067
Soby Mathewd7b79f22020-05-21 15:06:54 +010068 /* Copy core key attributes from the client core key attributes */
Summer Qin359167d2021-07-05 18:11:50 +080069 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 Fox98ab4412020-01-17 17:12:30 +000074
75 /* Return the key_id as the client key id, do not return the owner */
Summer Qin359167d2021-07-05 18:11:50 +080076 client_key_attr->id = core.MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id);
Jamie Fox98ab4412020-01-17 17:12:30 +000077
78 return PSA_SUCCESS;
79}
80
Maulik Patel28659c42021-01-06 14:09:22 +000081psa_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 Angelis8908f472018-08-31 15:44:25 +0100101/*!@}*/