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 #else
57 typedef void TSS2_TCTI_POLL_HANDLE;
58 #ifndef TSS2_TCTI_SUPPRESS_POLL_WARNINGS
59 #pragma message "Info: Platform not supported for TCTI_POLL_HANDLES"
60 #endif
61 #endif
62 
63 /* The following are used to configure timeout characteristics. */
64 #define  TSS2_TCTI_TIMEOUT_BLOCK    -1
65 #define  TSS2_TCTI_TIMEOUT_NONE     0
66 
67 /* Macros to simplify access to values in common TCTI structure */
68 #define TSS2_TCTI_MAGIC(tctiContext) \
69     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->magic
70 #define TSS2_TCTI_VERSION(tctiContext) \
71     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->version
72 #define TSS2_TCTI_TRANSMIT(tctiContext) \
73     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->transmit
74 #define TSS2_TCTI_RECEIVE(tctiContext) \
75     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->receive
76 #define TSS2_TCTI_FINALIZE(tctiContext) \
77     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->finalize
78 #define TSS2_TCTI_CANCEL(tctiContext) \
79     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->cancel
80 #define TSS2_TCTI_GET_POLL_HANDLES(tctiContext) \
81     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->getPollHandles
82 #define TSS2_TCTI_SET_LOCALITY(tctiContext) \
83     ((TSS2_TCTI_CONTEXT_COMMON_V1*)tctiContext)->setLocality
84 #define TSS2_TCTI_MAKE_STICKY(tctiContext) \
85     ((TSS2_TCTI_CONTEXT_COMMON_V2*)tctiContext)->makeSticky
86 
87 /* Macros to simplify invocation of functions from the common TCTI structure */
88 #define Tss2_Tcti_Transmit(tctiContext, size, command) \
89     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_CONTEXT: \
90     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
91         TSS2_TCTI_RC_ABI_MISMATCH: \
92     (TSS2_TCTI_TRANSMIT(tctiContext) == NULL) ? \
93         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
94     TSS2_TCTI_TRANSMIT(tctiContext)(tctiContext, size, command))
95 #define Tss2_Tcti_Receive(tctiContext, size, response, timeout) \
96     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_CONTEXT: \
97     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
98         TSS2_TCTI_RC_ABI_MISMATCH: \
99     (TSS2_TCTI_RECEIVE(tctiContext) == NULL) ? \
100         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
101     TSS2_TCTI_RECEIVE(tctiContext)(tctiContext, size, response, timeout))
102 #define Tss2_Tcti_Finalize(tctiContext) \
103     do { \
104         if ((tctiContext != NULL) && \
105             (TSS2_TCTI_VERSION(tctiContext) >= 1) && \
106             (TSS2_TCTI_FINALIZE(tctiContext) != NULL)) \
107         { \
108             TSS2_TCTI_FINALIZE(tctiContext)(tctiContext); \
109         } \
110     } while (0)
111 #define Tss2_Tcti_Cancel(tctiContext) \
112     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_CONTEXT: \
113     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
114         TSS2_TCTI_RC_ABI_MISMATCH: \
115     (TSS2_TCTI_CANCEL(tctiContext) == NULL) ? \
116         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
117     TSS2_TCTI_CANCEL(tctiContext)(tctiContext))
118 #define Tss2_Tcti_GetPollHandles(tctiContext, handles, num_handles) \
119     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_CONTEXT: \
120     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
121         TSS2_TCTI_RC_ABI_MISMATCH: \
122     (TSS2_TCTI_GET_POLL_HANDLES(tctiContext) == NULL) ? \
123         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
124     TSS2_TCTI_GET_POLL_HANDLES(tctiContext)(tctiContext, handles, num_handles))
125 #define Tss2_Tcti_SetLocality(tctiContext, locality) \
126     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_CONTEXT: \
127     (TSS2_TCTI_VERSION(tctiContext) < 1) ? \
128         TSS2_TCTI_RC_ABI_MISMATCH: \
129     (TSS2_TCTI_SET_LOCALITY(tctiContext) == NULL) ? \
130         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
131     TSS2_TCTI_SET_LOCALITY(tctiContext)(tctiContext, locality))
132 #define Tss2_Tcti_MakeSticky(tctiContext, handle, sticky) \
133     ((tctiContext == NULL) ? TSS2_TCTI_RC_BAD_CONTEXT: \
134     (TSS2_TCTI_VERSION(tctiContext) < 2) ? \
135         TSS2_TCTI_RC_ABI_MISMATCH: \
136     (TSS2_TCTI_MAKE_STICKY(tctiContext) == NULL) ? \
137         TSS2_TCTI_RC_NOT_IMPLEMENTED: \
138     TSS2_TCTI_MAKE_STICKY(tctiContext)(tctiContext, handle, sticky))
139 
140 typedef struct TSS2_TCTI_OPAQUE_CONTEXT_BLOB TSS2_TCTI_CONTEXT;
141 
142 /*
143  * Types for TCTI functions. These are used for pointers to functions in the
144  * TCTI context structure.
145  */
146 typedef TSS2_RC (*TSS2_TCTI_TRANSMIT_FCN) (
147     TSS2_TCTI_CONTEXT *tctiContext,
148     size_t size,
149     uint8_t const *command);
150 typedef TSS2_RC (*TSS2_TCTI_RECEIVE_FCN) (
151     TSS2_TCTI_CONTEXT *tctiContext,
152     size_t *size,
153     uint8_t *response,
154     int32_t timeout);
155 typedef void (*TSS2_TCTI_FINALIZE_FCN) (
156     TSS2_TCTI_CONTEXT *tctiContext);
157 typedef TSS2_RC (*TSS2_TCTI_CANCEL_FCN) (
158     TSS2_TCTI_CONTEXT *tctiContext);
159 typedef TSS2_RC (*TSS2_TCTI_GET_POLL_HANDLES_FCN) (
160     TSS2_TCTI_CONTEXT *tctiContext,
161     TSS2_TCTI_POLL_HANDLE *handles,
162     size_t *num_handles);
163 typedef TSS2_RC (*TSS2_TCTI_SET_LOCALITY_FCN) (
164     TSS2_TCTI_CONTEXT *tctiContext,
165     uint8_t locality);
166 typedef TSS2_RC (*TSS2_TCTI_MAKE_STICKY_FCN) (
167     TSS2_TCTI_CONTEXT *tctiContext,
168     TPM2_HANDLE *handle,
169     uint8_t sticky);
170 typedef TSS2_RC (*TSS2_TCTI_INIT_FUNC) (
171     TSS2_TCTI_CONTEXT *tctiContext,
172     size_t *size,
173     const char *config);
174 
175 /* current version #1 known to this implementation */
176 typedef struct {
177     uint64_t magic;
178     uint32_t version;
179     TSS2_TCTI_TRANSMIT_FCN transmit;
180     TSS2_TCTI_RECEIVE_FCN receive;
181     TSS2_TCTI_FINALIZE_FCN finalize;
182     TSS2_TCTI_CANCEL_FCN cancel;
183     TSS2_TCTI_GET_POLL_HANDLES_FCN getPollHandles;
184     TSS2_TCTI_SET_LOCALITY_FCN setLocality;
185 } TSS2_TCTI_CONTEXT_COMMON_V1;
186 
187 typedef struct {
188     TSS2_TCTI_CONTEXT_COMMON_V1 v1;
189     TSS2_TCTI_MAKE_STICKY_FCN makeSticky;
190 } TSS2_TCTI_CONTEXT_COMMON_V2;
191 
192 typedef TSS2_TCTI_CONTEXT_COMMON_V2 TSS2_TCTI_CONTEXT_COMMON_CURRENT;
193 
194 #define TSS2_TCTI_INFO_SYMBOL "Tss2_Tcti_Info"
195 
196 typedef struct {
197     uint32_t version;
198     const char *name;
199     const char *description;
200     const char *config_help;
201     TSS2_TCTI_INIT_FUNC init;
202 } TSS2_TCTI_INFO;
203 
204 typedef const TSS2_TCTI_INFO* (*TSS2_TCTI_INFO_FUNC) (void);
205 
206 #endif
207