blob: a08ab32fea9cebe9e8ad40152239d80e6583b44a [file] [log] [blame]
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001From b8060d9e15b1b910cf9b466a3f43088c71d7a38f Mon Sep 17 00:00:00 2001
2From: Satish Kumar <satish.kumar01@arm.com>
3Date: Sun, 13 Feb 2022 09:49:51 +0000
4Subject: [PATCH] Integrate remaining psa-ipc client APIs.
5
6Signed-off-by: Satish Kumar <satish.kumar01@arm.com>
7
8Upstream-Status: Pending [Not submitted to upstream yet]
9Signed-off-by: Emekcan Aras <Emekcan.Aras@arm.com>
10
11
12---
13 .../caller/psa_ipc/crypto_caller_aead.h | 297 +++++++++++++++++-
14 .../caller/psa_ipc/crypto_caller_sign_hash.h | 35 +++
15 .../psa_ipc/crypto_caller_verify_hash.h | 33 +-
16 3 files changed, 352 insertions(+), 13 deletions(-)
17
18diff --git a/components/service/crypto/client/caller/psa_ipc/crypto_caller_aead.h b/components/service/crypto/client/caller/psa_ipc/crypto_caller_aead.h
19index 78517fe3..f6aadd8b 100644
20--- a/components/service/crypto/client/caller/psa_ipc/crypto_caller_aead.h
21+++ b/components/service/crypto/client/caller/psa_ipc/crypto_caller_aead.h
22@@ -152,7 +152,27 @@ static inline psa_status_t crypto_caller_aead_encrypt_setup(
23 psa_key_id_t key,
24 psa_algorithm_t alg)
25 {
26- return PSA_ERROR_NOT_SUPPORTED;
27+ struct service_client *ipc = context;
28+ struct rpc_caller *caller = ipc->caller;
29+ psa_status_t status;
30+ struct psa_ipc_crypto_pack_iovec iov = {
31+ .sfn_id = TFM_CRYPTO_AEAD_ENCRYPT_SETUP_SID,
32+ .key_id = key,
33+ .alg = alg,
34+ .op_handle = (*op_handle),
35+ };
36+
37+ struct psa_invec in_vec[] = {
38+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)}
39+ };
40+ struct psa_outvec out_vec[] = {
41+ {.base = psa_ptr_to_u32(op_handle), .len = sizeof(uint32_t)}
42+ };
43+
44+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
45+ IOVEC_LEN(in_vec), out_vec, IOVEC_LEN(out_vec));
46+
47+ return status;
48 }
49
50 static inline psa_status_t crypto_caller_aead_decrypt_setup(
51@@ -161,7 +181,26 @@ static inline psa_status_t crypto_caller_aead_decrypt_setup(
52 psa_key_id_t key,
53 psa_algorithm_t alg)
54 {
55- return PSA_ERROR_NOT_SUPPORTED;
56+ struct service_client *ipc = context;
57+ struct rpc_caller *caller = ipc->caller;
58+ psa_status_t status;
59+ struct psa_ipc_crypto_pack_iovec iov = {
60+ .sfn_id = TFM_CRYPTO_AEAD_DECRYPT_SETUP_SID,
61+ .key_id = key,
62+ .alg = alg,
63+ .op_handle = (*op_handle),
64+ };
65+
66+ struct psa_invec in_vec[] = {
67+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)}
68+ };
69+ struct psa_outvec out_vec[] = {
70+ {.base = psa_ptr_to_u32(op_handle), .len = sizeof(uint32_t)}
71+ };
72+
73+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
74+ IOVEC_LEN(in_vec), out_vec, IOVEC_LEN(out_vec));
75+ return status;
76 }
77
78 static inline psa_status_t crypto_caller_aead_generate_nonce(
79@@ -171,7 +210,27 @@ static inline psa_status_t crypto_caller_aead_generate_nonce(
80 size_t nonce_size,
81 size_t *nonce_length)
82 {
83- return PSA_ERROR_NOT_SUPPORTED;
84+ struct service_client *ipc = context;
85+ struct rpc_caller *caller = ipc->caller;
86+ psa_status_t status;
87+ struct psa_ipc_crypto_pack_iovec iov = {
88+ .sfn_id = TFM_CRYPTO_AEAD_GENERATE_NONCE_SID,
89+ .op_handle = op_handle,
90+ };
91+
92+ struct psa_invec in_vec[] = {
93+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)},
94+ };
95+ struct psa_outvec out_vec[] = {
96+ {.base = psa_ptr_to_u32(&op_handle), .len = sizeof(uint32_t)},
97+ {.base = psa_ptr_to_u32(nonce), .len = nonce_size}
98+ };
99+
100+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
101+ IOVEC_LEN(in_vec), out_vec, IOVEC_LEN(out_vec));
102+
103+ *nonce_length = out_vec[1].len;
104+ return status;
105 }
106
107 static inline psa_status_t crypto_caller_aead_set_nonce(
108@@ -180,7 +239,25 @@ static inline psa_status_t crypto_caller_aead_set_nonce(
109 const uint8_t *nonce,
110 size_t nonce_length)
111 {
112- return PSA_ERROR_NOT_SUPPORTED;
113+ struct service_client *ipc = context;
114+ struct rpc_caller *caller = ipc->caller;
115+ psa_status_t status;
116+ struct psa_ipc_crypto_pack_iovec iov = {
117+ .sfn_id = TFM_CRYPTO_AEAD_SET_NONCE_SID,
118+ .op_handle = op_handle,
119+ };
120+
121+ struct psa_invec in_vec[] = {
122+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)},
123+ {.base = psa_ptr_to_u32(nonce), .len = nonce_length}
124+ };
125+ struct psa_outvec out_vec[] = {
126+ {.base = psa_ptr_to_u32(&op_handle), .len = sizeof(uint32_t)}
127+ };
128+
129+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
130+ IOVEC_LEN(in_vec), out_vec, IOVEC_LEN(out_vec));
131+ return status;
132 }
133
134 static inline psa_status_t crypto_caller_aead_set_lengths(
135@@ -189,7 +266,27 @@ static inline psa_status_t crypto_caller_aead_set_lengths(
136 size_t ad_length,
137 size_t plaintext_length)
138 {
139- return PSA_ERROR_NOT_SUPPORTED;
140+ struct service_client *ipc = context;
141+ struct rpc_caller *caller = ipc->caller;
142+ psa_status_t status;
143+ struct psa_ipc_crypto_pack_iovec iov = {
144+ .sfn_id = TFM_CRYPTO_AEAD_SET_LENGTHS_SID,
145+ .ad_length = ad_length,
146+ .plaintext_length = plaintext_length,
147+ .op_handle = op_handle,
148+ };
149+
150+ struct psa_invec in_vec[] = {
151+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)},
152+ };
153+ struct psa_outvec out_vec[] = {
154+ {.base = psa_ptr_to_u32(&op_handle), .len = sizeof(uint32_t)}
155+ };
156+
157+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
158+ IOVEC_LEN(in_vec), out_vec, IOVEC_LEN(out_vec));
159+
160+ return status;
161 }
162
163 static inline psa_status_t crypto_caller_aead_update_ad(
164@@ -198,7 +295,35 @@ static inline psa_status_t crypto_caller_aead_update_ad(
165 const uint8_t *input,
166 size_t input_length)
167 {
168- return PSA_ERROR_NOT_SUPPORTED;
169+ struct service_client *ipc = context;
170+ struct rpc_caller *caller = ipc->caller;
171+ psa_status_t status;
172+ struct psa_ipc_crypto_pack_iovec iov = {
173+ .sfn_id = TFM_CRYPTO_AEAD_UPDATE_AD_SID,
174+ .op_handle = op_handle,
175+ };
176+
177+ /* Sanitize the optional input */
178+ if ((input == NULL) && (input_length != 0)) {
179+ return PSA_ERROR_INVALID_ARGUMENT;
180+ }
181+
182+ struct psa_invec in_vec[] = {
183+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)},
184+ {.base = psa_ptr_const_to_u32(input), .len = input_length}
185+ };
186+ struct psa_outvec out_vec[] = {
187+ {.base = psa_ptr_to_u32(&op_handle), .len = sizeof(uint32_t)}
188+ };
189+
190+ size_t in_len = IOVEC_LEN(in_vec);
191+
192+ if (input == NULL) {
193+ in_len--;
194+ }
195+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
196+ in_len, out_vec, IOVEC_LEN(out_vec));
197+ return status;
198 }
199
200 static inline psa_status_t crypto_caller_aead_update(
201@@ -210,7 +335,38 @@ static inline psa_status_t crypto_caller_aead_update(
202 size_t output_size,
203 size_t *output_length)
204 {
205- return PSA_ERROR_NOT_SUPPORTED;
206+ struct service_client *ipc = context;
207+ struct rpc_caller *caller = ipc->caller;
208+ psa_status_t status;
209+ struct psa_ipc_crypto_pack_iovec iov = {
210+ .sfn_id = TFM_CRYPTO_AEAD_UPDATE_SID,
211+ .op_handle = op_handle,
212+ };
213+
214+ /* Sanitize the optional input */
215+ if ((input == NULL) && (input_length != 0)) {
216+ return PSA_ERROR_INVALID_ARGUMENT;
217+ }
218+
219+ struct psa_invec in_vec[] = {
220+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)},
221+ {.base = psa_ptr_const_to_u32(input), .len = input_length}
222+ };
223+ struct psa_outvec out_vec[] = {
224+ {.base = psa_ptr_to_u32(&op_handle), .len = sizeof(uint32_t)},
225+ {.base = psa_ptr_const_to_u32(output), .len = output_size},
226+ };
227+
228+ size_t in_len = IOVEC_LEN(in_vec);
229+
230+ if (input == NULL) {
231+ in_len--;
232+ }
233+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
234+ in_len, out_vec, IOVEC_LEN(out_vec));
235+
236+ *output_length = out_vec[1].len;
237+ return status;
238 }
239
240 static inline psa_status_t crypto_caller_aead_finish(
241@@ -223,7 +379,48 @@ static inline psa_status_t crypto_caller_aead_finish(
242 size_t tag_size,
243 size_t *tag_length)
244 {
245- return PSA_ERROR_NOT_SUPPORTED;
246+ struct service_client *ipc = context;
247+ struct rpc_caller *caller = ipc->caller;
248+ psa_status_t status;
249+ struct psa_ipc_crypto_pack_iovec iov = {
250+ .sfn_id = TFM_CRYPTO_AEAD_FINISH_SID,
251+ .op_handle = op_handle,
252+ };
253+
254+ /* Sanitize the optional output */
255+ if ((aeadtext == NULL) && (aeadtext_size != 0)) {
256+ return PSA_ERROR_INVALID_ARGUMENT;
257+ }
258+
259+ struct psa_invec in_vec[] = {
260+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)},
261+ };
262+ struct psa_outvec out_vec[] = {
263+ {.base = psa_ptr_to_u32(&op_handle), .len = sizeof(uint32_t)},
264+ {.base = psa_ptr_const_to_u32(tag), .len = tag_size},
265+ {.base = psa_ptr_const_to_u32(aeadtext), .len = aeadtext_size}
266+ };
267+
268+ size_t out_len = IOVEC_LEN(out_vec);
269+
270+ if (aeadtext == NULL || aeadtext_size == 0) {
271+ out_len--;
272+ }
273+ if ((out_len == 3) && (aeadtext_length == NULL)) {
274+ return PSA_ERROR_INVALID_ARGUMENT;
275+ }
276+
277+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
278+ IOVEC_LEN(in_vec), out_vec, out_len);
279+
280+ *tag_length = out_vec[1].len;
281+
282+ if (out_len == 3) {
283+ *aeadtext_length = out_vec[2].len;
284+ } else {
285+ *aeadtext_length = 0;
286+ }
287+ return status;
288 }
289
290 static inline psa_status_t crypto_caller_aead_verify(
291@@ -235,14 +432,94 @@ static inline psa_status_t crypto_caller_aead_verify(
292 const uint8_t *tag,
293 size_t tag_length)
294 {
295- return PSA_ERROR_NOT_SUPPORTED;
296+ struct service_client *ipc = context;
297+ struct rpc_caller *caller = ipc->caller;
298+ psa_status_t status;
299+ struct psa_ipc_crypto_pack_iovec iov = {
300+ .sfn_id = TFM_CRYPTO_AEAD_VERIFY_SID,
301+ .op_handle = op_handle,
302+ };
303+
304+ /* Sanitize the optional output */
305+ if ((plaintext == NULL) && (plaintext_size != 0)) {
306+ return PSA_ERROR_INVALID_ARGUMENT;
307+ }
308+
309+ struct psa_invec in_vec[] = {
310+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)},
311+ {.base = psa_ptr_const_to_u32(tag), .len = tag_length}
312+ };
313+ struct psa_outvec out_vec[] = {
314+ {.base = psa_ptr_to_u32(&op_handle), .len = sizeof(uint32_t)},
315+ {.base = psa_ptr_const_to_u32(plaintext), .len = plaintext_size},
316+ };
317+
318+ size_t out_len = IOVEC_LEN(out_vec);
319+
320+ if (plaintext == NULL || plaintext_size == 0) {
321+ out_len--;
322+ }
323+ if ((out_len == 2) && (plaintext_length == NULL)) {
324+ return PSA_ERROR_INVALID_ARGUMENT;
325+ }
326+
327+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
328+ IOVEC_LEN(in_vec), out_vec, out_len);
329+
330+ if (out_len == 2) {
331+ *plaintext_length = out_vec[1].len;
332+ } else {
333+ *plaintext_length = 0;
334+ }
335+ return status;
336 }
337
338 static inline psa_status_t crypto_caller_aead_abort(
339 struct service_client *context,
340 uint32_t op_handle)
341 {
342- return PSA_ERROR_NOT_SUPPORTED;
343+ struct service_client *ipc = context;
344+ struct rpc_caller *caller = ipc->caller;
345+ psa_status_t status;
346+ struct psa_ipc_crypto_pack_iovec iov = {
347+ .sfn_id = TFM_CRYPTO_AEAD_ABORT_SID,
348+ .op_handle = op_handle,
349+ };
350+
351+ struct psa_invec in_vec[] = {
352+ {.base = psa_ptr_to_u32(&iov), .len = sizeof(struct psa_ipc_crypto_pack_iovec)},
353+ };
354+ struct psa_outvec out_vec[] = {
355+ {.base = psa_ptr_to_u32(&op_handle), .len = sizeof(uint32_t)},
356+ };
357+
358+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
359+ IOVEC_LEN(in_vec), out_vec, IOVEC_LEN(out_vec));
360+ return status;
361+}
362+
363+static inline size_t crypto_caller_aead_max_update_size(const struct service_client *context)
364+{
365+ /* Returns the maximum number of bytes that may be
366+ * carried as a parameter of the mac_update operation
367+ * using the packed-c encoding.
368+ */
369+ size_t payload_space = context->service_info.max_payload;
370+ size_t overhead = iov_size;
371+
372+ return (payload_space > overhead) ? payload_space - overhead : 0;
373+}
374+
375+static inline size_t crypto_caller_aead_max_update_ad_size(const struct service_client *context)
376+{
377+ /* Returns the maximum number of bytes that may be
378+ * carried as a parameter of the mac_update operation
379+ * using the packed-c encoding.
380+ */
381+ size_t payload_space = context->service_info.max_payload;
382+ size_t overhead = iov_size;
383+
384+ return (payload_space > overhead) ? payload_space - overhead : 0;
385 }
386
387 #ifdef __cplusplus
388diff --git a/components/service/crypto/client/caller/psa_ipc/crypto_caller_sign_hash.h b/components/service/crypto/client/caller/psa_ipc/crypto_caller_sign_hash.h
389index 71d88ced..e4a2b167 100644
390--- a/components/service/crypto/client/caller/psa_ipc/crypto_caller_sign_hash.h
391+++ b/components/service/crypto/client/caller/psa_ipc/crypto_caller_sign_hash.h
392@@ -57,6 +57,41 @@ static inline psa_status_t crypto_caller_sign_hash(struct service_client *contex
393 return status;
394 }
395
396+static inline psa_status_t crypto_caller_sign_message(struct service_client *context,
397+ psa_key_id_t id,
398+ psa_algorithm_t alg,
399+ const uint8_t *hash,
400+ size_t hash_length,
401+ uint8_t *signature,
402+ size_t signature_size,
403+ size_t *signature_length)
404+{
405+ struct service_client *ipc = context;
406+ struct rpc_caller *caller = ipc->caller;
407+ psa_status_t status;
408+ struct psa_ipc_crypto_pack_iovec iov = {
409+ .sfn_id = TFM_CRYPTO_SIGN_MESSAGE_SID,
410+ .key_id = id,
411+ .alg = alg,
412+ };
413+ struct psa_invec in_vec[] = {
414+ { .base = psa_ptr_to_u32(&iov), .len = iov_size },
415+ { .base = psa_ptr_const_to_u32(hash), .len = hash_length },
416+ };
417+ struct psa_outvec out_vec[] = {
418+ { .base = psa_ptr_to_u32(signature), .len = signature_size },
419+ };
420+
421+ status = psa_call(caller, TFM_CRYPTO_HANDLE, PSA_IPC_CALL, in_vec,
422+ IOVEC_LEN(in_vec), out_vec, IOVEC_LEN(out_vec));
423+
424+ *signature_length = out_vec[0].len;
425+
426+ return status;
427+}
428+
429+
430+
431 #ifdef __cplusplus
432 }
433 #endif
434diff --git a/components/service/crypto/client/caller/psa_ipc/crypto_caller_verify_hash.h b/components/service/crypto/client/caller/psa_ipc/crypto_caller_verify_hash.h
435index e16f6e54..cc9279ee 100644
436--- a/components/service/crypto/client/caller/psa_ipc/crypto_caller_verify_hash.h
437+++ b/components/service/crypto/client/caller/psa_ipc/crypto_caller_verify_hash.h
438@@ -24,19 +24,20 @@
439 extern "C" {
440 #endif
441
442-static inline psa_status_t crypto_caller_verify_hash(struct service_client *context,
443+static inline psa_status_t crypto_caller_common(struct service_client *context,
444 psa_key_id_t id,
445 psa_algorithm_t alg,
446 const uint8_t *hash,
447 size_t hash_length,
448 const uint8_t *signature,
449- size_t signature_length)
450+ size_t signature_length,
451+ uint32_t sfn_id)
452 {
453 struct service_client *ipc = context;
454 struct rpc_caller *caller = ipc->caller;
455 psa_status_t status;
456 struct psa_ipc_crypto_pack_iovec iov = {
457- .sfn_id = TFM_CRYPTO_VERIFY_HASH_SID,
458+ .sfn_id = sfn_id,
459 .key_id = id,
460 .alg = alg,
461 };
462@@ -52,6 +53,32 @@ static inline psa_status_t crypto_caller_verify_hash(struct service_client *cont
463 return status;
464 }
465
466+static inline psa_status_t crypto_caller_verify_hash(struct service_client *context,
467+ psa_key_id_t id,
468+ psa_algorithm_t alg,
469+ const uint8_t *hash,
470+ size_t hash_length,
471+ const uint8_t *signature,
472+ size_t signature_length)
473+{
474+
475+ return crypto_caller_common(context,id,alg,hash,hash_length,
476+ signature,signature_length, TFM_CRYPTO_VERIFY_HASH_SID);
477+}
478+
479+static inline psa_status_t crypto_caller_verify_message(struct service_client *context,
480+ psa_key_id_t id,
481+ psa_algorithm_t alg,
482+ const uint8_t *hash,
483+ size_t hash_length,
484+ const uint8_t *signature,
485+ size_t signature_length)
486+{
487+
488+ return crypto_caller_common(context,id,alg,hash,hash_length,
489+ signature,signature_length, TFM_CRYPTO_VERIFY_MESSAGE_SID);
490+}
491+
492 #ifdef __cplusplus
493 }
494 #endif