blob: 9e71206fc84368c58a32d83d239e30dc16b6bc7a [file] [log] [blame]
Will Arthur54e04e42015-07-15 11:29:25 -04001//**********************************************************************;
2// Copyright (c) 2015, Intel Corporation
3// All rights reserved.
Philip Tricca1ea84a52015-11-19 18:07:06 -08004//
5// Redistribution and use in source and binary forms, with or without
Will Arthur54e04e42015-07-15 11:29:25 -04006// modification, are permitted provided that the following conditions are met:
Philip Tricca1ea84a52015-11-19 18:07:06 -08007//
8// 1. Redistributions of source code must retain the above copyright notice,
Will Arthur54e04e42015-07-15 11:29:25 -04009// this list of conditions and the following disclaimer.
Philip Tricca1ea84a52015-11-19 18:07:06 -080010//
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
Will Arthur54e04e42015-07-15 11:29:25 -040013// and/or other materials provided with the distribution.
Philip Tricca1ea84a52015-11-19 18:07:06 -080014//
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
Will Arthur54e04e42015-07-15 11:29:25 -040025// THE POSSIBILITY OF SUCH DAMAGE.
26//**********************************************************************;
27
28#ifndef TSS2_SYSAPI_UTIL_H
29#define TSS2_SYSAPI_UTIL_H
30
Will Arthur54e04e42015-07-15 11:29:25 -040031#ifdef __cplusplus
32extern "C" {
33#endif
34
Philip Triccac3dedc22016-01-15 13:47:22 -080035#include "tcti_util.h"
Will Arthur54e04e42015-07-15 11:29:25 -040036
37// TBD: delete this after porting completed.
38#define CMD_STAGE_1 1
39
40enum cmdStates { CMD_STAGE_INITIALIZE, CMD_STAGE_PREPARE, CMD_STAGE_SEND_COMMAND, CMD_STAGE_RECEIVE_RESPONSE, CMD_STAGE_ALL = 0xff };
41
Will Arthur54e04e42015-07-15 11:29:25 -040042typedef struct {
Philip Tricca6da5eb22017-03-20 10:52:44 -070043 TPM_ST tag;
44 UINT32 size;
45 TPM_RC rsp_code;
46} TPM20_Rsp_Header;
47
48typedef struct {
Will Arthur54e04e42015-07-15 11:29:25 -040049 //
50 // These are inputs to system API functions.
51 //
52 TSS2_TCTI_CONTEXT *tctiContext;
53
54 // In and out buffers can be the same for a minimalized memory footprint implementation.
55 UINT8 *tpmInBuffPtr; // Input: Pointer to command buffer area
56 UINT32 maxCommandSize; // Input: max size of command buffer area
57 UINT8 *tpmOutBuffPtr; // Input: Pointer to response buffer
58 UINT32 maxResponseSize; // Input: max size of response buffer area
59
Philip Tricca6da5eb22017-03-20 10:52:44 -070060 TPM20_Rsp_Header rsp_header;
61
Will Arthur54e04e42015-07-15 11:29:25 -040062 //
63 // These are set by system API and used by helper functions to calculate cpHash,
64 // rpHash, and for auditing.
65 //
66 TPM_CC commandCodeSwapped;
Philip Triccadfa41a52016-07-20 17:43:57 -070067 UINT32 cpBufferUsedSize;
Will Arthur54e04e42015-07-15 11:29:25 -040068 UINT8 *cpBuffer;
wcarthur7b810772015-11-30 15:23:08 -050069 UINT32 *rspParamsSize; // Points to response paramsSize.
Philip Triccadfa41a52016-07-20 17:43:57 -070070 UINT32 rpBufferUsedSize;
71 UINT8 *rpBuffer;
Will Arthur54e04e42015-07-15 11:29:25 -040072 UINT8 previousStage; // Used to check for sequencing errors.
Will Arthur54e04e42015-07-15 11:29:25 -040073 UINT8 authsCount;
74 UINT8 numResponseHandles;
75 struct
76 {
77 UINT16 tpmVersionInfoValid:1; // Identifies whether the TPM version info fields are valid; if not valid
Philip Triccadfa41a52016-07-20 17:43:57 -070078 // this info can't be used for TPM version-specific workarounds.
Will Arthur54e04e42015-07-15 11:29:25 -040079 UINT16 decryptAllowed:1; // Identifies whether this command supports an encrypted command parameter.
80 UINT16 encryptAllowed:1; // Identifies whether this command supports an encrypted response parameter.
81
82 UINT16 decryptNull:1; // Indicates that the decrypt param was NULL at _Prepare call.
83 UINT16 authAllowed:1;
84
85 // Following are used to support decrypt/encrypt sessions with one-call.
86 UINT16 decryptSession:1; // If true, complex TPM2B's are not marshalled but instead treated as simple TPM2B's.
87 UINT16 encryptSession:1; // If true, complex TPM2B's are not unmarshalled but instead treated as simple TPM2B's.
88 UINT16 prepareCalledFromOneCall:1; // Indicates that the _Prepare call was called from the one-call.
89 UINT16 completeCalledFromOneCall:1; // Indicates that the _Prepare call was called from the one-call.
90 };
91
92 // Used to maintain state of SAPI functions.
93
94 // Placeholder for current rval. This is a convenience and code size optimization for SAPI functions.
95 // Marshalling functions check this and SAPI functions return it.
96 TSS2_RC rval;
97
98 // Location for next data in command/response buffer.
99 UINT8 *nextData;
Philip Triccadfa41a52016-07-20 17:43:57 -0700100
Will Arthur54e04e42015-07-15 11:29:25 -0400101} _TSS2_SYS_CONTEXT_BLOB;
102
103
104#define SYS_CONTEXT ( (_TSS2_SYS_CONTEXT_BLOB *)sysContext )
105
106#pragma pack(push, 1)
107//
108// Generic header
Philip Tricca1ea84a52015-11-19 18:07:06 -0800109//
Will Arthur54e04e42015-07-15 11:29:25 -0400110typedef struct _TPM20_Header_In {
111 TPM_ST tag;
112 UINT32 commandSize;
113 UINT32 commandCode;
114} TPM20_Header_In;
Philip Triccadfa41a52016-07-20 17:43:57 -0700115
Will Arthur54e04e42015-07-15 11:29:25 -0400116typedef struct _TPM20_Header_Out {
117 TPM_ST tag;
118 UINT32 responseSize;
119 UINT32 responseCode;
120 UINT8 otherData;
121} TPM20_Header_Out;
122
123typedef struct _TPM20_ErrorResponse {
124 TPM_ST tag;
125 UINT32 responseSize;
126 UINT32 responseCode;
127} TPM20_ErrorResponse;
128
Will Arthur54e04e42015-07-15 11:29:25 -0400129#pragma pack(pop)
130
131typedef struct {
132 TPM_CC commandCode;
133 int numCommandHandles; // Num of handles that require authorization in
134 // command: used for virtualization and for
135 // parsing sessions following handles section.
136 int numResponseHandles; // Num of handles that require authorization in
137 // in response: used for virtualization and for
138 // parsing sessions following handles section.
139} COMMAND_HANDLES;
140
141
142// Utility functions.
143void CopyCommandHeader( _TSS2_SYS_CONTEXT_BLOB *sysContext, TPM_CC commandCode );
Philip Triccadfa41a52016-07-20 17:43:57 -0700144TPM_RC FinishCommand( _TSS2_SYS_CONTEXT_BLOB *sysContext,
Will Arthur54e04e42015-07-15 11:29:25 -0400145 const TSS2_SYS_CMD_AUTHS *cmdAuthsArray, UINT32 *responseSize );
146
147UINT16 GetDigestSize( TPM_ALG_ID authHash );
wcarthur12eb0502015-11-06 12:01:24 -0500148UINT32 GetCommandSize( TSS2_SYS_CONTEXT *sysContext );
Will Arthur54e04e42015-07-15 11:29:25 -0400149TSS2_RC CopySessionsDataIn( void **otherData, const TSS2_SYS_CMD_AUTHS *pSessionDataIn );
150TSS2_RC CopySessionDataIn( void **otherData, TPMS_AUTH_COMMAND const *sessionData, UINT32 *sessionSizePtr );
151TSS2_RC CopySessionDataOut( TPMS_AUTH_RESPONSE *sessionData, void **otherData, UINT8* outBuffPtr, UINT32 outBuffSize );
152TSS2_RC CopySessionsDataOut( TSS2_SYS_RSP_AUTHS *rspAuthsArray, void *otherData, TPM_ST tag, UINT8* outBuffPtr, UINT32 outBuffSize );
153
154TPM_RC ConcatSizedByteBuffer( TPM2B_MAX_BUFFER *result, TPM2B *addBuffer );
155
156void InitSysContextFields( TSS2_SYS_CONTEXT *sysContext );
Philip Triccad87165e2016-08-31 10:42:47 -0700157void InitSysContextPtrs ( TSS2_SYS_CONTEXT *sysContext, size_t contextSize );
Will Arthur54e04e42015-07-15 11:29:25 -0400158
159TSS2_RC CompleteChecks( TSS2_SYS_CONTEXT *sysContext );
160
161TSS2_RC CommonComplete( TSS2_SYS_CONTEXT *sysContext );
162
163TSS2_RC CommonOneCallForNoResponseCmds(
164 TSS2_SYS_CONTEXT *sysContext,
165 TSS2_SYS_CMD_AUTHS const *cmdAuthsArray,
166 TSS2_SYS_RSP_AUTHS *rspAuthsArray
167 );
168
169TSS2_RC CommonOneCall(
170 TSS2_SYS_CONTEXT *sysContext,
171 TSS2_SYS_CMD_AUTHS const *cmdAuthsArray,
172 TSS2_SYS_RSP_AUTHS *rspAuthsArray
173 );
174
175TSS2_RC CommonPreparePrologue(
176 TSS2_SYS_CONTEXT *sysContext,
177 TPM_CC commandCode
178 );
179
180TSS2_RC CommonPrepareEpilogue(
181 TSS2_SYS_CONTEXT *sysContext
182 );
183
184TSS2_RC CopyMem( UINT8 *dest, const UINT8 *src, const size_t len, const UINT8 *limit );
185
186TSS2_RC CopyMemReverse( UINT8 *dest, const UINT8 *src, const size_t len, const UINT8 *limit );
187
188int GetNumCommandHandles( TPM_CC commandCode );
189
190int GetNumResponseHandles( TPM_CC commandCode );
191
Will Arthur54e04e42015-07-15 11:29:25 -0400192TSS2_SYS_CONTEXT *InitSysContext(
193 UINT16 maxCommandSize,
Philip Triccadfa41a52016-07-20 17:43:57 -0700194 TSS2_TCTI_CONTEXT *tctiContext,
Will Arthur54e04e42015-07-15 11:29:25 -0400195 TSS2_ABI_VERSION *abiVersion
196 );
197
198void TeardownSysContext( TSS2_SYS_CONTEXT **sysContext );
199
Philip Triccac3dedc22016-01-15 13:47:22 -0800200#include "sys_api_marshalUnmarshal.h"
Will Arthur54e04e42015-07-15 11:29:25 -0400201
202#ifdef __cplusplus
203}
204#endif
205
206#endif // TSS2_SYSAPI_UTIL_H