1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2015 - 2018, Intel Corporation
4  *
5  * Copyright 2015, Andreas Fuchs @ Fraunhofer SIT
6  *
7  * All rights reserved.
8  *
9  * Copyright (c) 2019, Wind River Systems.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  * this list of conditions and the following disclaimer in the documentation
20  * and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 #ifndef TSS2_TCTI_H
35 #define TSS2_TCTI_H
36 
37 #include <stdint.h>
38 #include <stddef.h>
39 #include "tss2_common.h"
40 #include "tss2_tpm2_types.h"
41 
42 #ifndef TSS2_API_VERSION_1_2_1_108
43 #error Version mismatch among TSS2 header files.
44 #endif  /* TSS2_API_VERSION_1_2_1_108 */
45 
46 #if defined(__linux__) || defined(__unix__) || defined(__APPLE__) || defined (__QNXNTO__) || defined (__VXWORKS__)
47 #if defined (__VXWORKS__)
48 #include <sys/poll.h>
49 #else
50 #include <poll.h>
51 #endif
52 typedef struct pollfd TSS2_TCTI_POLL_HANDLE;
53 #elif defined(_WIN32)
54 #include <windows.h>
55 typedef HANDLE TSS2_TCTI_POLL_HANDLE;
56 #elif defined(__ZEPHYR__)
57 typedef void* TSS2_TCTI_POLL_HANDLE;
58 #else
59 typedef void TSS2_TCTI_POLL_HANDLE;
60 #ifndef TSS2_TCTI_SUPPRESS_POLL_WARNINGS
61 #pragma message "Info: Platform not supported for TCTI_POLL_HANDLES"
62 #endif
63 #endif
64 
65 /* The following are used to configure timeout characteristics. */
66 #define  TSS2_TCTI_TIMEOUT_BLOCK    -1
67 #define  TSS2_TCTI_TIMEOUT_NONE     0
68 
69 /* Macros to simplify access to values in common TCTI structure */
70 #define TSS2_TCTI_MAGIC(tctiContext) \
71     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->magic
72 #define TSS2_TCTI_VERSION(tctiContext) \
73     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->version
74 #define TSS2_TCTI_TRANSMIT(tctiContext) \
75     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->transmit
76 #define TSS2_TCTI_RECEIVE(tctiContext) \
77     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->receive
78 #define TSS2_TCTI_FINALIZE(tctiContext) \
79     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->finalize
80 #define TSS2_TCTI_CANCEL(tctiContext) \
81     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->cancel
82 #define TSS2_TCTI_GET_POLL_HANDLES(tctiContext) \
83     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->getPollHandles
84 #define TSS2_TCTI_SET_LOCALITY(tctiContext) \
85     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->setLocality
86 #define TSS2_TCTI_MAKE_STICKY(tctiContext) \
87     ((TSS2_TCTI_CONTEXT_COMMON_V2*)tctiContext)->makeSticky
88 
89 /* Macros to simplify invocation of functions from the common TCTI structure */
90 #define Tss2_Tcti_Transmit(tctiContext, size, command) \
91     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_REFERENCE: \
92     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
93         TSS2_TCTI_RC_ABI_MISMATCH: \
94     (TSS2_TCTI_TRANSMIT(tctiContext) == NULL) ? \
95         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
96     TSS2_TCTI_TRANSMIT(tctiContext)(tctiContext, size, command))
97 #define Tss2_Tcti_Receive(tctiContext, size, response, timeout) \
98     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_REFERENCE: \
99     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
100         TSS2_TCTI_RC_ABI_MISMATCH: \
101     (TSS2_TCTI_RECEIVE(tctiContext) == NULL) ? \
102         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
103     TSS2_TCTI_RECEIVE(tctiContext)(tctiContext, size, response, timeout))
104 #define Tss2_Tcti_Finalize(tctiContext) \
105     do { \
106         if ((tctiContext != NULL) && \
107             (TSS2_TCTI_VERSION(tctiContext) >= 1) && \
108             (TSS2_TCTI_FINALIZE(tctiContext) != NULL)) \
109         { \
110             TSS2_TCTI_FINALIZE(tctiContext)(tctiContext); \
111         } \
112     } while (0)
113 #define Tss2_Tcti_Cancel(tctiContext) \
114     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_REFERENCE: \
115     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
116         TSS2_TCTI_RC_ABI_MISMATCH: \
117     (TSS2_TCTI_CANCEL(tctiContext) == NULL) ? \
118         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
119     TSS2_TCTI_CANCEL(tctiContext)(tctiContext))
120 #define Tss2_Tcti_GetPollHandles(tctiContext, handles, num_handles) \
121     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_REFERENCE: \
122     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
123         TSS2_TCTI_RC_ABI_MISMATCH: \
124     (TSS2_TCTI_GET_POLL_HANDLES(tctiContext) == NULL) ? \
125         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
126     TSS2_TCTI_GET_POLL_HANDLES(tctiContext)(tctiContext, handles, num_handles))
127 #define Tss2_Tcti_SetLocality(tctiContext, locality) \
128     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_REFERENCE: \
129     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
130         TSS2_TCTI_RC_ABI_MISMATCH: \
131     (TSS2_TCTI_SET_LOCALITY(tctiContext) == NULL) ? \
132         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
133     TSS2_TCTI_SET_LOCALITY(tctiContext)(tctiContext, locality))
134 #define Tss2_Tcti_MakeSticky(tctiContext, handle, sticky) \
135     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_REFERENCE: \
136     (TSS2_TCTI_VERSION(tctiContext) < 2) ? \
137         TSS2_TCTI_RC_ABI_MISMATCH: \
138     (TSS2_TCTI_MAKE_STICKY(tctiContext) == NULL) ? \
139         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
140     TSS2_TCTI_MAKE_STICKY(tctiContext)(tctiContext, handle, sticky))
141 
142 typedef struct TSS2_TCTI_OPAQUE_CONTEXT_BLOB TSS2_TCTI_CONTEXT;
143 
144 /*
145  * Types for TCTI functions. These are used for pointers to functions in the
146  * TCTI context structure.
147  */
148 typedef TSS2_RC (*TSS2_TCTI_TRANSMIT_FCN) (
149     TSS2_TCTI_CONTEXT *tctiContext,
150     size_t size,
151     uint8_t const *command);
152 typedef TSS2_RC (*TSS2_TCTI_RECEIVE_FCN) (
153     TSS2_TCTI_CONTEXT *tctiContext,
154     size_t *size,
155     uint8_t *response,
156     int32_t timeout);
157 typedef void (*TSS2_TCTI_FINALIZE_FCN) (
158     TSS2_TCTI_CONTEXT *tctiContext);
159 typedef TSS2_RC (*TSS2_TCTI_CANCEL_FCN) (
160     TSS2_TCTI_CONTEXT *tctiContext);
161 typedef TSS2_RC (*TSS2_TCTI_GET_POLL_HANDLES_FCN) (
162     TSS2_TCTI_CONTEXT *tctiContext,
163     TSS2_TCTI_POLL_HANDLE *handles,
164     size_t *num_handles);
165 typedef TSS2_RC (*TSS2_TCTI_SET_LOCALITY_FCN) (
166     TSS2_TCTI_CONTEXT *tctiContext,
167     uint8_t locality);
168 typedef TSS2_RC (*TSS2_TCTI_MAKE_STICKY_FCN) (
169     TSS2_TCTI_CONTEXT *tctiContext,
170     TPM2_HANDLE *handle,
171     uint8_t sticky);
172 typedef TSS2_RC (*TSS2_TCTI_INIT_FUNC) (
173     TSS2_TCTI_CONTEXT *tctiContext,
174     size_t *size,
175     const char *config);
176 
177 /* current version #1 known to this implementation */
178 typedef struct TSS2_TCTI_CONTEXT_COMMON_V1 TSS2_TCTI_CONTEXT_COMMON_V1;
179 struct TSS2_TCTI_CONTEXT_COMMON_V1 {
180     uint64_t magic;
181     uint32_t version;
182     TSS2_TCTI_TRANSMIT_FCN transmit;
183     TSS2_TCTI_RECEIVE_FCN receive;
184     TSS2_TCTI_FINALIZE_FCN finalize;
185     TSS2_TCTI_CANCEL_FCN cancel;
186     TSS2_TCTI_GET_POLL_HANDLES_FCN getPollHandles;
187     TSS2_TCTI_SET_LOCALITY_FCN setLocality;
188 };
189 
190 typedef struct TSS2_TCTI_CONTEXT_COMMON_V2 TSS2_TCTI_CONTEXT_COMMON_V2;
191 struct TSS2_TCTI_CONTEXT_COMMON_V2 {
192     TSS2_TCTI_CONTEXT_COMMON_V1 v1;
193     TSS2_TCTI_MAKE_STICKY_FCN makeSticky;
194 };
195 
196 typedef TSS2_TCTI_CONTEXT_COMMON_V2 TSS2_TCTI_CONTEXT_COMMON_CURRENT;
197 
198 #define TSS2_TCTI_INFO_SYMBOL "Tss2_Tcti_Info"
199 
200 typedef struct TSS2_TCTI_INFO TSS2_TCTI_INFO;
201 struct TSS2_TCTI_INFO {
202     uint32_t version;
203     const char *name;
204     const char *description;
205     const char *config_help;
206     TSS2_TCTI_INIT_FUNC init;
207 };
208 
209 typedef const TSS2_TCTI_INFO* (*TSS2_TCTI_INFO_FUNC) (void);
210 
211 #endif
212