ti_psa_crypto_helper.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022-2025, Texas Instruments Incorporated
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef TI_PSA_CRYPTO_HELPER_H
19 #define TI_PSA_CRYPTO_HELPER_H
20 
21 #include <stdbool.h>
22 #include <stdint.h>
23 
24 #include <ti/devices/DeviceFamily.h>
25 #if ((TFM_ENABLED == 1) && !defined(TFM_BUILD))
26  #include <third_party/tfm/interface/include/psa/crypto.h>
27 #else
28  #include <third_party/mbedtls/include/psa/crypto.h>
29 #endif
30 
32 
33 #if ((DeviceFamily_PARENT == DeviceFamily_PARENT_CC27XX) || (DeviceFamily_PARENT == DeviceFamily_PARENT_CC35XX))
34  #include <ti/drivers/ECDH.h>
35  #include <ti/drivers/ECDSA.h>
36 #endif
37 
38 /* Includes for mbedTLS content */
39 /* psa_crypto_local_input_t etc */
40 #if (TI_SW_CRYPTO_ENABLED == 1)
41  #include <third_party/mbedtls/library/psa_crypto_core.h>
42  #include <third_party/mbedtls/include/psa/build_info.h>
43 #endif
44 
45 /* Identifiers for S-only PSA operations to determine if a given operation
46  * is using the TI HW acceleration, or if it began with the mbedTLS SW
47  * implementation.
48  */
49 #define PSA_CRYPTO_TI_DRIVER_ID 1
50 #define PSA_CRYPTO_MBED_TLS_DRIVER_ID 2
51 
52 /* Includes for mbedTLS content */
53 /* psa_crypto_local_input_t etc */
54 #if (TI_SW_CRYPTO_ENABLED == 1)
55  #include <third_party/mbedtls/library/psa_crypto_core.h>
56  #include <third_party/mbedtls/include/psa/build_info.h>
57 #endif
58 
59 /* Identifiers for S-only PSA operations to determine if a given operation
60  * is using the TI HW acceleration, or if it began with the mbedTLS SW
61  * implementation.
62  */
63 #define PSA_CRYPTO_TI_DRIVER_ID 1
64 #define PSA_CRYPTO_MBED_TLS_DRIVER_ID 2
65 
66 /* Error mapping functions. These functions take as input SimpleLink driver
67  * error codes and maps them to nearest PSA error code.
68  */
69 psa_status_t map_AES_status(int_fast16_t status);
70 psa_status_t map_ECDH_status(int_fast16_t status);
71 psa_status_t map_ECDSA_status(int_fast16_t status, bool isVerifyOp);
72 psa_status_t map_EDDSA_status(int_fast16_t status, bool isVerifyOp);
73 psa_status_t map_RNG_status(int_fast16_t status);
74 psa_status_t map_SHA2_status(int_fast16_t status);
75 psa_status_t map_TRNG_status(int_fast16_t status);
76 
77 /* Convert to and from the keyID format expected by the SimpleLink KeyStore API */
78 psa_key_id_t toKeyID(mbedtls_svc_key_id_t keystoreKeyID);
79 mbedtls_svc_key_id_t toKeyStoreKeyID(psa_key_id_t keyID);
80 
81 #if ((DeviceFamily_PARENT == DeviceFamily_PARENT_CC27XX) || (DeviceFamily_PARENT == DeviceFamily_PARENT_CC35XX))
82 /* Maps PSA ECC key types to their corresponding ECDSA Curve Type for HSM.
83  * Assumes that the keyType is some form of ECC key type. Returns 0 if could not
84  * find a mapping.
85  */
86 ECDSA_CurveType map_keyTypeToECDSACurveTypeHSM(psa_key_type_t keyType, size_t keyBits);
87 
88 /* Maps PSA ECC key types to their corresponding ECDH Curve Type for HSM.
89  * Assumes that the keyType is some form of ECC key type. Returns 0 if could not
90  * find a mapping.
91  */
92 ECDH_CurveType map_keyTypeToECDHCurveTypeHSM(psa_key_type_t keyType, size_t keyBits);
93 #endif
94 
95 /* Maps PSA ECC key types to their corresponding ECCParams_CurveParams struct
96  * pointer. Assumes that the keyType is some form of ECC key type. Returns NULL
97  * if could not find a mapping.
98  */
99 const ECCParams_CurveParams *map_keyTypeToECCParams(psa_key_type_t keyType, size_t keyBits);
100 
101 bool isHardwareAccelerated(psa_algorithm_t alg);
102 /* For type-safety, the following APIs have not been combined into one. The
103  * operation ID would have to be stored at the same offset in each operation
104  * struct for use of a void pointer to work.
105  */
106 void setAeadOperationID(psa_aead_operation_t *operation, unsigned int id);
107 void setCipherOperationID(psa_cipher_operation_t *operation, unsigned int id);
108 void setHashOperationID(psa_hash_operation_t *operation, unsigned int id);
109 void setKeyDerivationOperationID(psa_key_derivation_operation_t *operation, unsigned int id);
110 void setMacOperationID(psa_mac_operation_t *operation, unsigned int id);
111 
112 bool isHardwareAeadOperation(const psa_aead_operation_t *operation);
113 bool isHardwareCipherOperation(const psa_cipher_operation_t *operation);
114 bool isHardwareHashOperation(const psa_hash_operation_t *operation);
115 bool isHardwareKeyDerivationOperation(const psa_key_derivation_operation_t *operation);
116 bool isHardwareMacOperation(const psa_mac_operation_t *operation);
117 
118 #if (TI_SW_CRYPTO_ENABLED == 1)
119  #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
120 
121  /* Declare a local copy of an input buffer and a variable that will be used
122  * to store a pointer to the start of the buffer.
123  *
124  * Note: This macro must be called before any operations which may jump to
125  * the exit label, so that the local input copy object is safe to be freed.
126  *
127  * Assumptions:
128  * - input is the name of a pointer to the buffer to be copied
129  * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
130  * - input_copy_name is a name that is unused in the current scope
131  */
132  #define LOCAL_INPUT_DECLARE(input, input_copy_name) \
133  psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \
134  const uint8_t *input_copy_name = NULL;
135 
136  /* Allocate a copy of the buffer input and set the pointer input_copy to
137  * point to the start of the copy.
138  *
139  * Assumptions:
140  * - psa_status_t status exists
141  * - An exit label is declared
142  * - input is the name of a pointer to the buffer to be copied
143  * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called
144  */
145  #define LOCAL_INPUT_ALLOC(input, length, input_copy) \
146  status = psa_crypto_local_input_alloc(input, length, &LOCAL_INPUT_COPY_OF_##input); \
147  if (status != PSA_SUCCESS) \
148  { \
149  goto exit; \
150  } \
151  input_copy = LOCAL_INPUT_COPY_OF_##input.buffer;
152 
153  /* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
154  *
155  * Assumptions:
156  * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC()
157  * - input is the name of the original buffer that was copied
158  */
159  #define LOCAL_INPUT_FREE(input, input_copy) \
160  input_copy = NULL; \
161  psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
162 
163  /* Declare a local copy of an output buffer and a variable that will be used
164  * to store a pointer to the start of the buffer.
165  *
166  * Note: This macro must be called before any operations which may jump to
167  * the exit label, so that the local output copy object is safe to be freed.
168  *
169  * Assumptions:
170  * - output is the name of a pointer to the buffer to be copied
171  * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
172  * - output_copy_name is a name that is unused in the current scope
173  */
174  #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \
175  psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
176  uint8_t *output_copy_name = NULL;
177 
178  /* Allocate a copy of the buffer output and set the pointer output_copy to
179  * point to the start of the copy.
180  *
181  * Assumptions:
182  * - psa_status_t status exists
183  * - An exit label is declared
184  * - output is the name of a pointer to the buffer to be copied
185  * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called
186  */
187  #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \
188  status = psa_crypto_local_output_alloc(output, length, &LOCAL_OUTPUT_COPY_OF_##output); \
189  if (status != PSA_SUCCESS) \
190  { \
191  goto exit; \
192  } \
193  output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer;
194 
195  /* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC()
196  * after first copying back its contents to the original buffer.
197  *
198  * Assumptions:
199  * - psa_status_t status exists
200  * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC()
201  * - output is the name of the original buffer that was copied
202  */
203  #define LOCAL_OUTPUT_FREE(output, output_copy) \
204  output_copy = NULL; \
205  do \
206  { \
207  psa_status_t local_output_status; \
208  local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \
209  if (local_output_status != PSA_SUCCESS) \
210  { \
211  /* Since this error case is an internal error, it's more serious than \
212  * any existing error code and so it's fine to overwrite the existing \
213  * status. */ \
214  status = local_output_status; \
215  } \
216  } while (0)
217  #else /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
218  #define LOCAL_INPUT_DECLARE(input, input_copy_name) const uint8_t *input_copy_name = NULL;
219  #define LOCAL_INPUT_ALLOC(input, length, input_copy) input_copy = input;
220  #define LOCAL_INPUT_FREE(input, input_copy) input_copy = NULL;
221  #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) uint8_t *output_copy_name = NULL;
222  #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) output_copy = output;
223  #define LOCAL_OUTPUT_FREE(output, output_copy) output_copy = NULL;
224  #endif /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */
225 #endif /* TI_SW_CRYPTO_ENABLED == 1 */
226 #endif /* TI_PSA_CRYPTO_HELPER_H */
TI Driver for Elliptic Curve Digital Signature Algorithm.
ECDSA_CurveType
Enum for the curve types supported by the driver.
Definition: ECDSA.h:644
ECDH_CurveType map_keyTypeToECDHCurveTypeHSM(psa_key_type_t keyType, size_t keyBits)
ECDH_CurveType
Enum for the curve types supported by the driver.
Definition: ECDH.h:602
psa_key_id_t toKeyID(mbedtls_svc_key_id_t keystoreKeyID)
void setAeadOperationID(psa_aead_operation_t *operation, unsigned int id)
psa_status_t map_EDDSA_status(int_fast16_t status, bool isVerifyOp)
psa_status_t map_ECDSA_status(int_fast16_t status, bool isVerifyOp)
void setCipherOperationID(psa_cipher_operation_t *operation, unsigned int id)
psa_status_t map_AES_status(int_fast16_t status)
bool isHardwareMacOperation(const psa_mac_operation_t *operation)
bool isHardwareHashOperation(const psa_hash_operation_t *operation)
bool isHardwareAccelerated(psa_algorithm_t alg)
TI Driver for Elliptic Curve Diffie-Hellman key agreement scheme.
bool isHardwareCipherOperation(const psa_cipher_operation_t *operation)
const ECCParams_CurveParams * map_keyTypeToECCParams(psa_key_type_t keyType, size_t keyBits)
bool isHardwareKeyDerivationOperation(const psa_key_derivation_operation_t *operation)
void setHashOperationID(psa_hash_operation_t *operation, unsigned int id)
void setMacOperationID(psa_mac_operation_t *operation, unsigned int id)
psa_status_t map_RNG_status(int_fast16_t status)
void setKeyDerivationOperationID(psa_key_derivation_operation_t *operation, unsigned int id)
A structure containing the parameters of an elliptic curve.
Definition: ECCParams.h:145
psa_status_t map_TRNG_status(int_fast16_t status)
ECDSA_CurveType map_keyTypeToECDSACurveTypeHSM(psa_key_type_t keyType, size_t keyBits)
mbedtls_svc_key_id_t toKeyStoreKeyID(psa_key_id_t keyID)
psa_algorithm_t alg
Definition: ti_psa_crypto_hash.h:73
psa_status_t map_SHA2_status(int_fast16_t status)
psa_status_t map_ECDH_status(int_fast16_t status)
bool isHardwareAeadOperation(const psa_aead_operation_t *operation)
© Copyright 1995-2026, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale