Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 1 | //**********************************************************************; |
| 2 | // Copyright (c) 2015, Intel Corporation |
| 3 | // All rights reserved. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 6 | // modification, are permitted provided that the following conditions are met: |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 9 | // this list of conditions and the following disclaimer. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 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 |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 13 | // and/or other materials provided with the distribution. |
Philip Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 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 |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 25 | // THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | //**********************************************************************; |
| 27 | |
| 28 | #ifndef TSS2_SYSAPI_UTIL_H |
| 29 | #define TSS2_SYSAPI_UTIL_H |
| 30 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
Philip Tricca | c3dedc2 | 2016-01-15 13:47:22 -0800 | [diff] [blame] | 35 | #include "tcti_util.h" |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 36 | |
| 37 | // TBD: delete this after porting completed. |
| 38 | #define CMD_STAGE_1 1 |
| 39 | |
| 40 | enum cmdStates { CMD_STAGE_INITIALIZE, CMD_STAGE_PREPARE, CMD_STAGE_SEND_COMMAND, CMD_STAGE_RECEIVE_RESPONSE, CMD_STAGE_ALL = 0xff }; |
| 41 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 42 | typedef struct { |
Philip Tricca | 6da5eb2 | 2017-03-20 10:52:44 -0700 | [diff] [blame] | 43 | TPM_ST tag; |
| 44 | UINT32 size; |
| 45 | TPM_RC rsp_code; |
| 46 | } TPM20_Rsp_Header; |
| 47 | |
| 48 | typedef struct { |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 49 | // |
| 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 Tricca | 6da5eb2 | 2017-03-20 10:52:44 -0700 | [diff] [blame] | 60 | TPM20_Rsp_Header rsp_header; |
| 61 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 62 | // |
| 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 Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 67 | UINT32 cpBufferUsedSize; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 68 | UINT8 *cpBuffer; |
wcarthur | 7b81077 | 2015-11-30 15:23:08 -0500 | [diff] [blame] | 69 | UINT32 *rspParamsSize; // Points to response paramsSize. |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 70 | UINT32 rpBufferUsedSize; |
| 71 | UINT8 *rpBuffer; |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 72 | UINT8 previousStage; // Used to check for sequencing errors. |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 73 | 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 Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 78 | // this info can't be used for TPM version-specific workarounds. |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 79 | 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 Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 100 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 101 | } _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 Tricca | 1ea84a5 | 2015-11-19 18:07:06 -0800 | [diff] [blame] | 109 | // |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 110 | typedef struct _TPM20_Header_In { |
| 111 | TPM_ST tag; |
| 112 | UINT32 commandSize; |
| 113 | UINT32 commandCode; |
| 114 | } TPM20_Header_In; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 115 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 116 | typedef struct _TPM20_Header_Out { |
| 117 | TPM_ST tag; |
| 118 | UINT32 responseSize; |
| 119 | UINT32 responseCode; |
| 120 | UINT8 otherData; |
| 121 | } TPM20_Header_Out; |
| 122 | |
| 123 | typedef struct _TPM20_ErrorResponse { |
| 124 | TPM_ST tag; |
| 125 | UINT32 responseSize; |
| 126 | UINT32 responseCode; |
| 127 | } TPM20_ErrorResponse; |
| 128 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 129 | #pragma pack(pop) |
| 130 | |
| 131 | typedef 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. |
| 143 | void CopyCommandHeader( _TSS2_SYS_CONTEXT_BLOB *sysContext, TPM_CC commandCode ); |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 144 | TPM_RC FinishCommand( _TSS2_SYS_CONTEXT_BLOB *sysContext, |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 145 | const TSS2_SYS_CMD_AUTHS *cmdAuthsArray, UINT32 *responseSize ); |
| 146 | |
| 147 | UINT16 GetDigestSize( TPM_ALG_ID authHash ); |
wcarthur | 12eb050 | 2015-11-06 12:01:24 -0500 | [diff] [blame] | 148 | UINT32 GetCommandSize( TSS2_SYS_CONTEXT *sysContext ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 149 | TSS2_RC CopySessionsDataIn( void **otherData, const TSS2_SYS_CMD_AUTHS *pSessionDataIn ); |
| 150 | TSS2_RC CopySessionDataIn( void **otherData, TPMS_AUTH_COMMAND const *sessionData, UINT32 *sessionSizePtr ); |
| 151 | TSS2_RC CopySessionDataOut( TPMS_AUTH_RESPONSE *sessionData, void **otherData, UINT8* outBuffPtr, UINT32 outBuffSize ); |
| 152 | TSS2_RC CopySessionsDataOut( TSS2_SYS_RSP_AUTHS *rspAuthsArray, void *otherData, TPM_ST tag, UINT8* outBuffPtr, UINT32 outBuffSize ); |
| 153 | |
| 154 | TPM_RC ConcatSizedByteBuffer( TPM2B_MAX_BUFFER *result, TPM2B *addBuffer ); |
| 155 | |
| 156 | void InitSysContextFields( TSS2_SYS_CONTEXT *sysContext ); |
Philip Tricca | d87165e | 2016-08-31 10:42:47 -0700 | [diff] [blame] | 157 | void InitSysContextPtrs ( TSS2_SYS_CONTEXT *sysContext, size_t contextSize ); |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 158 | |
| 159 | TSS2_RC CompleteChecks( TSS2_SYS_CONTEXT *sysContext ); |
| 160 | |
| 161 | TSS2_RC CommonComplete( TSS2_SYS_CONTEXT *sysContext ); |
| 162 | |
| 163 | TSS2_RC CommonOneCallForNoResponseCmds( |
| 164 | TSS2_SYS_CONTEXT *sysContext, |
| 165 | TSS2_SYS_CMD_AUTHS const *cmdAuthsArray, |
| 166 | TSS2_SYS_RSP_AUTHS *rspAuthsArray |
| 167 | ); |
| 168 | |
| 169 | TSS2_RC CommonOneCall( |
| 170 | TSS2_SYS_CONTEXT *sysContext, |
| 171 | TSS2_SYS_CMD_AUTHS const *cmdAuthsArray, |
| 172 | TSS2_SYS_RSP_AUTHS *rspAuthsArray |
| 173 | ); |
| 174 | |
| 175 | TSS2_RC CommonPreparePrologue( |
| 176 | TSS2_SYS_CONTEXT *sysContext, |
| 177 | TPM_CC commandCode |
| 178 | ); |
| 179 | |
| 180 | TSS2_RC CommonPrepareEpilogue( |
| 181 | TSS2_SYS_CONTEXT *sysContext |
| 182 | ); |
| 183 | |
| 184 | TSS2_RC CopyMem( UINT8 *dest, const UINT8 *src, const size_t len, const UINT8 *limit ); |
| 185 | |
| 186 | TSS2_RC CopyMemReverse( UINT8 *dest, const UINT8 *src, const size_t len, const UINT8 *limit ); |
| 187 | |
| 188 | int GetNumCommandHandles( TPM_CC commandCode ); |
| 189 | |
| 190 | int GetNumResponseHandles( TPM_CC commandCode ); |
| 191 | |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 192 | TSS2_SYS_CONTEXT *InitSysContext( |
| 193 | UINT16 maxCommandSize, |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 194 | TSS2_TCTI_CONTEXT *tctiContext, |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 195 | TSS2_ABI_VERSION *abiVersion |
| 196 | ); |
| 197 | |
| 198 | void TeardownSysContext( TSS2_SYS_CONTEXT **sysContext ); |
| 199 | |
Philip Tricca | c3dedc2 | 2016-01-15 13:47:22 -0800 | [diff] [blame] | 200 | #include "sys_api_marshalUnmarshal.h" |
Will Arthur | 54e04e4 | 2015-07-15 11:29:25 -0400 | [diff] [blame] | 201 | |
| 202 | #ifdef __cplusplus |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | #endif // TSS2_SYSAPI_UTIL_H |