1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright (c) 20187-2020, Linaro Limited
4 */
5
6 #ifndef LIBCKTEEC_INVOKE_TA_H
7 #define LIBCKTEEC_INVOKE_TA_H
8
9 #include <pkcs11.h>
10 #include <tee_client_api.h>
11
12 enum ckteec_shm_dir {
13 CKTEEC_SHM_IN,
14 CKTEEC_SHM_OUT,
15 CKTEEC_SHM_INOUT,
16 };
17
18 /**
19 * ckteec_alloc_shm - Allocate memory in the TEE SHM (in, out or in/out)
20 *
21 * @size - Allocated size in byte
22 * @dir - Data direction used for the shared memory
23 *
24 * Return a shm reference or NULL on failure.
25 */
26 TEEC_SharedMemory *ckteec_alloc_shm(size_t size, enum ckteec_shm_dir dir);
27
28 /**
29 * ckteec_register_shm - Register memory as shared in the TEE SHM
30 *
31 * @buffer - Base address of buffer to register
32 * @size - Allocated size in byte
33 * @dir - Data direction used for the shared memory
34 *
35 * Return a shm reference or NULL on failure.
36 */
37 TEEC_SharedMemory *ckteec_register_shm(void *buffer, size_t size,
38 enum ckteec_shm_dir dir);
39
40 /**
41 * ckteec_free_shm - Release allocated or registered emory in the TEE SHM
42 *
43 * @shm - memory reference
44 */
45 void ckteec_free_shm(TEEC_SharedMemory *shm);
46
47 /**
48 * ckteec_invoke_ta - Invoke PKCS11 TA for a target request through the TEE
49 *
50 * @cmd - PKCS11 TA command ID
51 * @ctrl - shared memory with serialized request input arguments or NULL
52 * @io1 - In memory buffer argument #1 for the command or NULL
53 * @io2 - In and/or out memory buffer argument #2 for the command or NULL
54 * @out2_size - Reference to @io2 output buffer size or NULL if not applicable
55 * @io3 - In and/or out memory buffer argument #3 for the command or NULL
56 * @out3_size - Reference to @io3 output buffer size or NULL if not applicable
57 *
58 * Return a CR_RV compliant return value
59 */
60 CK_RV ckteec_invoke_ta(unsigned long cmd, TEEC_SharedMemory *ctrl,
61 TEEC_SharedMemory *io1,
62 TEEC_SharedMemory *io2, size_t *out2_size,
63 TEEC_SharedMemory *io3, size_t *out3_size);
64
ckteec_invoke_ctrl(unsigned long cmd,TEEC_SharedMemory * ctrl)65 static inline CK_RV ckteec_invoke_ctrl(unsigned long cmd,
66 TEEC_SharedMemory *ctrl)
67 {
68 return ckteec_invoke_ta(cmd, ctrl, NULL, NULL, NULL, NULL, NULL);
69 }
70
ckteec_invoke_ctrl_in(unsigned long cmd,TEEC_SharedMemory * ctrl,TEEC_SharedMemory * io1)71 static inline CK_RV ckteec_invoke_ctrl_in(unsigned long cmd,
72 TEEC_SharedMemory *ctrl,
73 TEEC_SharedMemory *io1)
74 {
75 return ckteec_invoke_ta(cmd, ctrl, io1, NULL, NULL, NULL, NULL);
76 }
77
ckteec_invoke_ctrl_out(unsigned long cmd,TEEC_SharedMemory * ctrl,TEEC_SharedMemory * io2,size_t * out_sz)78 static inline CK_RV ckteec_invoke_ctrl_out(unsigned long cmd,
79 TEEC_SharedMemory *ctrl,
80 TEEC_SharedMemory *io2,
81 size_t *out_sz)
82 {
83 return ckteec_invoke_ta(cmd, ctrl, NULL, io2, out_sz, NULL, NULL);
84 }
85
86 /*
87 * ckteec_invoke_init - Initialize TEE session with the PKCS11 TA
88 *
89 * Return a CR_RV compliant return value
90 */
91 CK_RV ckteec_invoke_init(void);
92
93 /*
94 * ckteec_invoke_terminate - Release all allocated invocation resources
95 *
96 * Return a CR_RV compliant return value
97 */
98 CK_RV ckteec_invoke_terminate(void);
99
100 /* Return true if and only if the PKCS11 TA invocation context is initiated */
101 bool ckteec_invoke_initiated(void);
102
103 #endif /*LIBCKTEEC_INVOKE_TA_H*/
104