blob: 8aa37fbb1c0d0387a877f11e9b09873a9299f524 [file] [log] [blame]
Summer Qin614002c2023-01-19 15:22:39 +08001From 2982172aba752f612b4e473626f787155087ddb5 Mon Sep 17 00:00:00 2001
Raef Colesb97e0982022-02-28 11:29:56 +00002From: Raef Coles <raef.coles@arm.com>
3Date: Tue, 19 Jul 2022 11:12:30 +0100
Summer Qin614002c2023-01-19 15:22:39 +08004Subject: [PATCH 4/6] Add TF-M builtin key driver
Raef Colesb97e0982022-02-28 11:29:56 +00005
6Signed-off-by: Raef Coles <raef.coles@arm.com>
7---
8 library/psa_crypto.c | 11 +++-
Summer Qin614002c2023-01-19 15:22:39 +08009 library/psa_crypto_driver_wrappers.c | 91 +++++++++++++++++++++++++++-
10 2 files changed, 100 insertions(+), 2 deletions(-)
Raef Colesb97e0982022-02-28 11:29:56 +000011
12diff --git a/library/psa_crypto.c b/library/psa_crypto.c
Summer Qin614002c2023-01-19 15:22:39 +080013index cb5791fc..43c8b9e6 100644
Raef Colesb97e0982022-02-28 11:29:56 +000014--- a/library/psa_crypto.c
15+++ b/library/psa_crypto.c
Summer Qin614002c2023-01-19 15:22:39 +080016@@ -82,6 +82,11 @@
Raef Colesb97e0982022-02-28 11:29:56 +000017 #include "mbedtls/sha256.h"
18 #include "mbedtls/sha512.h"
19
20+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
21+#include "tfm_crypto_defs.h"
22+#include "tfm_builtin_key_loader.h"
23+#endif /* PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER */
24+
25 #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
26
27 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
Summer Qin614002c2023-01-19 15:22:39 +080028@@ -947,7 +952,11 @@ static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
Raef Colesb97e0982022-02-28 11:29:56 +000029 if( status != PSA_SUCCESS )
30 return( status );
31
32- if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) )
33+ if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime )
34+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
35+ && PSA_KEY_LIFETIME_GET_LOCATION((*p_slot)->attr.lifetime) != TFM_BUILTIN_KEY_LOADER_KEY_LOCATION
36+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
37+ )
38 {
39 psa_unlock_key_slot( *p_slot );
40 *p_slot = NULL;
41diff --git a/library/psa_crypto_driver_wrappers.c b/library/psa_crypto_driver_wrappers.c
Summer Qin614002c2023-01-19 15:22:39 +080042index e822eef0..08427604 100644
Raef Colesb97e0982022-02-28 11:29:56 +000043--- a/library/psa_crypto_driver_wrappers.c
44+++ b/library/psa_crypto_driver_wrappers.c
Summer Qin614002c2023-01-19 15:22:39 +080045@@ -58,6 +58,18 @@
Raef Colesb97e0982022-02-28 11:29:56 +000046 #include "cc3xx.h"
47 #endif /* PSA_CRYPTO_DRIVER_CC3XX */
48
49+/* Include TF-M builtin key driver */
50+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
51+#ifndef PSA_CRYPTO_DRIVER_PRESENT
52+#define PSA_CRYPTO_DRIVER_PRESENT
53+#endif
54+#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
55+#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
56+#endif
57+#include "tfm_crypto_defs.h"
58+#include "tfm_builtin_key_loader.h"
59+#endif /* PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER */
60+
Raef Colesb97e0982022-02-28 11:29:56 +000061 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Summer Qin614002c2023-01-19 15:22:39 +080062 /* END-driver headers */
Raef Colesb97e0982022-02-28 11:29:56 +000063
Summer Qin614002c2023-01-19 15:22:39 +080064@@ -71,6 +83,9 @@
65 #if defined(PSA_CRYPTO_DRIVER_CC3XX)
Raef Colesb97e0982022-02-28 11:29:56 +000066 #define PSA_CRYPTO_CC3XX_DRIVER_ID (4)
67 #endif /* PSA_CRYPTO_DRIVER_CC3XX */
Raef Colesb97e0982022-02-28 11:29:56 +000068+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
69+#define PSA_CRYPTO_TFM_BUILTIN_KEY_LOADER_DRIVER_ID (5)
70+#endif /* PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER */
Summer Qin614002c2023-01-19 15:22:39 +080071
72 /* END-driver id */
73
74@@ -155,6 +170,9 @@ psa_status_t psa_driver_wrapper_sign_message(
Raef Colesb97e0982022-02-28 11:29:56 +000075 switch( location )
76 {
77 case PSA_KEY_LOCATION_LOCAL_STORAGE:
78+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
79+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
80+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
81 /* Key is stored in the slot in export representation, so
82 * cycle through all known transparent accelerators */
83 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +080084@@ -243,6 +261,9 @@ psa_status_t psa_driver_wrapper_verify_message(
Raef Colesb97e0982022-02-28 11:29:56 +000085 switch( location )
86 {
87 case PSA_KEY_LOCATION_LOCAL_STORAGE:
88+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
89+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
90+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
91 /* Key is stored in the slot in export representation, so
92 * cycle through all known transparent accelerators */
93 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +080094@@ -342,6 +363,9 @@ psa_status_t psa_driver_wrapper_sign_hash(
Raef Colesb97e0982022-02-28 11:29:56 +000095 switch( location )
96 {
97 case PSA_KEY_LOCATION_LOCAL_STORAGE:
98+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
99+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
100+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
101 /* Key is stored in the slot in export representation, so
102 * cycle through all known transparent accelerators */
103 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800104@@ -438,6 +462,9 @@ psa_status_t psa_driver_wrapper_verify_hash(
Raef Colesb97e0982022-02-28 11:29:56 +0000105 switch( location )
106 {
107 case PSA_KEY_LOCATION_LOCAL_STORAGE:
108+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
109+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
110+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
111 /* Key is stored in the slot in export representation, so
112 * cycle through all known transparent accelerators */
113 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800114@@ -582,7 +609,11 @@ psa_status_t psa_driver_wrapper_get_key_buffer_size(
Raef Colesb97e0982022-02-28 11:29:56 +0000115 return( ( *key_buffer_size != 0 ) ?
116 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
117 #endif /* PSA_CRYPTO_DRIVER_TEST */
118-
119+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
120+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
121+ return tfm_builtin_key_loader_get_key_buffer_size(psa_get_key_id(attributes),
122+ key_buffer_size);
123+#endif /* PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER */
124 default:
125 (void)key_type;
126 (void)key_bits;
Summer Qin614002c2023-01-19 15:22:39 +0800127@@ -622,6 +653,9 @@ psa_status_t psa_driver_wrapper_generate_key(
Raef Colesb97e0982022-02-28 11:29:56 +0000128 switch( location )
129 {
130 case PSA_KEY_LOCATION_LOCAL_STORAGE:
131+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
132+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
133+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
134 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
135 /* Transparent drivers are limited to generating asymmetric keys */
136 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Summer Qin614002c2023-01-19 15:22:39 +0800137@@ -714,6 +748,9 @@ psa_status_t psa_driver_wrapper_import_key(
Raef Colesb97e0982022-02-28 11:29:56 +0000138 switch( location )
139 {
140 case PSA_KEY_LOCATION_LOCAL_STORAGE:
141+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
142+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
143+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
144 /* Key is stored in the slot in export representation, so
145 * cycle through all known transparent accelerators */
146 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800147@@ -800,6 +837,9 @@ psa_status_t psa_driver_wrapper_export_key(
Raef Colesb97e0982022-02-28 11:29:56 +0000148 switch( location )
149 {
150 case PSA_KEY_LOCATION_LOCAL_STORAGE:
151+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
152+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
153+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
154 return( psa_export_key_internal( attributes,
155 key_buffer,
156 key_buffer_size,
Summer Qin614002c2023-01-19 15:22:39 +0800157@@ -865,6 +905,9 @@ psa_status_t psa_driver_wrapper_export_public_key(
Raef Colesb97e0982022-02-28 11:29:56 +0000158 switch( location )
159 {
160 case PSA_KEY_LOCATION_LOCAL_STORAGE:
161+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
162+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
163+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
164 /* Key is stored in the slot in export representation, so
165 * cycle through all known transparent accelerators */
166 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800167@@ -950,6 +993,13 @@ psa_status_t psa_driver_wrapper_get_builtin_key(
168
169
Raef Colesb97e0982022-02-28 11:29:56 +0000170 #endif /* PSA_CRYPTO_DRIVER_TEST */
171+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
172+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
173+ return( tfm_builtin_key_loader_get_key_buffer(
174+ slot_number,
175+ attributes,
176+ key_buffer, key_buffer_size, key_buffer_length ) );
177+#endif /* PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER */
178 default:
179 (void) slot_number;
180 (void) key_buffer;
Summer Qin614002c2023-01-19 15:22:39 +0800181@@ -1035,6 +1085,9 @@ psa_status_t psa_driver_wrapper_cipher_encrypt(
Raef Colesb97e0982022-02-28 11:29:56 +0000182 switch( location )
183 {
184 case PSA_KEY_LOCATION_LOCAL_STORAGE:
185+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
186+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
187+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
188 /* Key is stored in the slot in export representation, so
189 * cycle through all known transparent accelerators */
190 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800191@@ -1139,6 +1192,9 @@ psa_status_t psa_driver_wrapper_cipher_decrypt(
Raef Colesb97e0982022-02-28 11:29:56 +0000192 switch( location )
193 {
194 case PSA_KEY_LOCATION_LOCAL_STORAGE:
195+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
196+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
197+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
198 /* Key is stored in the slot in export representation, so
199 * cycle through all known transparent accelerators */
200 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800201@@ -1228,6 +1284,9 @@ psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Raef Colesb97e0982022-02-28 11:29:56 +0000202 switch( location )
203 {
204 case PSA_KEY_LOCATION_LOCAL_STORAGE:
205+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
206+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
207+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
208 /* Key is stored in the slot in export representation, so
209 * cycle through all known transparent accelerators */
210 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800211@@ -1311,6 +1370,9 @@ psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Raef Colesb97e0982022-02-28 11:29:56 +0000212 switch( location )
213 {
214 case PSA_KEY_LOCATION_LOCAL_STORAGE:
215+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
216+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
217+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
218 /* Key is stored in the slot in export representation, so
219 * cycle through all known transparent accelerators */
220 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800221@@ -1794,6 +1856,9 @@ psa_status_t psa_driver_wrapper_aead_encrypt(
Raef Colesb97e0982022-02-28 11:29:56 +0000222 switch( location )
223 {
224 case PSA_KEY_LOCATION_LOCAL_STORAGE:
225+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
226+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
227+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
228 /* Key is stored in the slot in export representation, so
229 * cycle through all known transparent accelerators */
230
Summer Qin614002c2023-01-19 15:22:39 +0800231@@ -1857,6 +1922,9 @@ psa_status_t psa_driver_wrapper_aead_decrypt(
Raef Colesb97e0982022-02-28 11:29:56 +0000232 switch( location )
233 {
234 case PSA_KEY_LOCATION_LOCAL_STORAGE:
235+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
236+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
237+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
238 /* Key is stored in the slot in export representation, so
239 * cycle through all known transparent accelerators */
240
Summer Qin614002c2023-01-19 15:22:39 +0800241@@ -1917,6 +1985,9 @@ psa_status_t psa_driver_wrapper_aead_encrypt_setup(
Raef Colesb97e0982022-02-28 11:29:56 +0000242 switch( location )
243 {
244 case PSA_KEY_LOCATION_LOCAL_STORAGE:
245+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
246+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
247+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
248 /* Key is stored in the slot in export representation, so
249 * cycle through all known transparent accelerators */
250
Summer Qin614002c2023-01-19 15:22:39 +0800251@@ -1974,6 +2045,9 @@ psa_status_t psa_driver_wrapper_aead_decrypt_setup(
Raef Colesb97e0982022-02-28 11:29:56 +0000252 switch( location )
253 {
254 case PSA_KEY_LOCATION_LOCAL_STORAGE:
255+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
256+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
257+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
258 /* Key is stored in the slot in export representation, so
259 * cycle through all known transparent accelerators */
260
Summer Qin614002c2023-01-19 15:22:39 +0800261@@ -2371,6 +2445,9 @@ psa_status_t psa_driver_wrapper_mac_compute(
Raef Colesb97e0982022-02-28 11:29:56 +0000262 switch( location )
263 {
264 case PSA_KEY_LOCATION_LOCAL_STORAGE:
265+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
266+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
267+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
268 /* Key is stored in the slot in export representation, so
269 * cycle through all known transparent accelerators */
270 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800271@@ -2441,6 +2518,9 @@ psa_status_t psa_driver_wrapper_mac_sign_setup(
Raef Colesb97e0982022-02-28 11:29:56 +0000272 switch( location )
273 {
274 case PSA_KEY_LOCATION_LOCAL_STORAGE:
275+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
276+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
277+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
278 /* Key is stored in the slot in export representation, so
279 * cycle through all known transparent accelerators */
280 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800281@@ -2522,6 +2602,9 @@ psa_status_t psa_driver_wrapper_mac_verify_setup(
Raef Colesb97e0982022-02-28 11:29:56 +0000282 switch( location )
283 {
284 case PSA_KEY_LOCATION_LOCAL_STORAGE:
285+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
286+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
287+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
288 /* Key is stored in the slot in export representation, so
289 * cycle through all known transparent accelerators */
290 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800291@@ -2822,6 +2905,9 @@ psa_status_t psa_driver_wrapper_asymmetric_decrypt(
Raef Colesb97e0982022-02-28 11:29:56 +0000292 switch( location )
293 {
294 case PSA_KEY_LOCATION_LOCAL_STORAGE:
295+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
296+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
297+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
298 /* Key is stored in the slot in export representation, so
299 * cycle through all known transparent accelerators */
300 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Summer Qin614002c2023-01-19 15:22:39 +0800301@@ -2900,6 +2986,9 @@ psa_status_t psa_driver_wrapper_key_agreement(
Raef Colesb97e0982022-02-28 11:29:56 +0000302 switch( location )
303 {
304 case PSA_KEY_LOCATION_LOCAL_STORAGE:
305+#if defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER)
306+ case TFM_BUILTIN_KEY_LOADER_KEY_LOCATION:
307+#endif /* defined(PSA_CRYPTO_DRIVER_TFM_BUILTIN_KEY_LOADER) */
308 /* Key is stored in the slot in export representation, so
309 * cycle through all known transparent accelerators */
310 #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
311--
Antonio de Angelis2718b582022-08-23 14:58:40 +01003122.25.1
Raef Colesb97e0982022-02-28 11:29:56 +0000313