1b3b289e10c47471e9564dd31a54c4677c4bb214
[aaf/sshsm.git] / TPM2-Plugin / lib / include / tcti_util.h
1 //**********************************************************************;
2 // Copyright (c) 2017, Intel Corporation
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 // THE POSSIBILITY OF SUCH DAMAGE.
26 //**********************************************************************;
27
28 //
29 // The context for TCTI implementations is on opaque
30 // structure. There shall never be a definition of its content.
31 // Implementation provide the size information to
32 // applications via the initialize call.
33 // This makes use of a compiler trick that allows type
34 // checking of the pointer even though the type isn't
35 // defined.
36 //
37 // The first field of a Context must be the common part
38 // (see below).
39 #ifndef TSS2_TCTI_UTIL_H
40 #define TSS2_TCTI_UTIL_H
41
42 #if defined linux || defined unix
43 #include <sys/socket.h>
44 #define SOCKET int
45 #endif
46
47 #include <tcti/common.h>
48
49 #define TCTI_MAGIC   0x7e18e9defa8bc9e2
50 #define TCTI_VERSION 0x1
51
52 #define TCTI_LOG_CALLBACK(ctx) ((TSS2_TCTI_CONTEXT_INTEL*)ctx)->logCallback
53 #define TCTI_LOG_DATA(ctx)     ((TSS2_TCTI_CONTEXT_INTEL*)ctx)->logData
54 #define TCTI_LOG_BUFFER_CALLBACK(ctx) ((TSS2_TCTI_CONTEXT_INTEL*)ctx)->logBufferCallback
55
56 typedef TSS2_RC (*TCTI_TRANSMIT_PTR)( TSS2_TCTI_CONTEXT *tctiContext, size_t size, uint8_t *command);
57 typedef TSS2_RC (*TCTI_RECEIVE_PTR) (TSS2_TCTI_CONTEXT *tctiContext, size_t *size, uint8_t *response, int32_t timeout);
58
59 enum tctiStates { TCTI_STAGE_INITIALIZE, TCTI_STAGE_SEND_COMMAND, TCTI_STAGE_RECEIVE_RESPONSE };
60
61 /* current Intel version */
62 typedef struct {
63     uint64_t magic;
64     uint32_t version;
65     TCTI_TRANSMIT_PTR transmit;
66     TCTI_RECEIVE_PTR receive;
67     TSS2_RC (*finalize) (TSS2_TCTI_CONTEXT *tctiContext);
68     TSS2_RC (*cancel) (TSS2_TCTI_CONTEXT *tctiContext);
69     TSS2_RC (*getPollHandles) (TSS2_TCTI_CONTEXT *tctiContext,
70               TSS2_TCTI_POLL_HANDLE *handles, size_t *num_handles);
71     TSS2_RC (*setLocality) (TSS2_TCTI_CONTEXT *tctiContext, uint8_t locality);
72     struct {
73         UINT32 debugMsgEnabled: 1;
74         UINT32 locality: 8;
75         UINT32 commandSent: 1;
76         UINT32 rmDebugPrefix: 1;  // Used to add a prefix to RM debug messages.  This is ONLY used
77                                   // for TPM commands and responses as a way to differentiate
78                                   // RM generated TPM commands from application generated ones.
79
80         // Following two fields used to save partial response status in case receive buffer's too small.
81         UINT32 tagReceived: 1;
82         UINT32 responseSizeReceived: 1;
83         UINT32 protocolResponseSizeReceived: 1;
84     } status;
85
86     // Following two fields used to save partial response in case receive buffer's too small.
87     TPM_ST tag;
88     TPM_RC responseSize;
89
90     TSS2_TCTI_CONTEXT *currentTctiContext;
91
92     // Sockets if socket interface is being used.
93     SOCKET otherSock;
94     SOCKET tpmSock;
95     SOCKET currentConnectSock;
96
97     // File descriptor for device file if real TPM is being used.
98     int devFile;
99     UINT8 previousStage;            // Used to check for sequencing errors.
100     unsigned char responseBuffer[4096];
101     TCTI_LOG_CALLBACK logCallback;
102     TCTI_LOG_BUFFER_CALLBACK logBufferCallback;
103     void *logData;
104 } TSS2_TCTI_CONTEXT_INTEL;
105
106 #define TCTI_CONTEXT ( (TSS2_TCTI_CONTEXT_COMMON_CURRENT *)(SYS_CONTEXT->tctiContext) )
107 #define TCTI_CONTEXT_INTEL ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )
108
109 #endif