1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************;
3 * Copyright (c) 2015 - 2018, Intel Corporation
4 * All rights reserved.
5 ***********************************************************************/
6
7 #ifndef TSS2_SYSAPI_UTIL_H
8 #define TSS2_SYSAPI_UTIL_H
9 #include <stdbool.h>
10
11 #include "tss2_tpm2_types.h"
12 #include "tss2_tcti.h"
13 #include "tss2_sys.h"
14 #include "util/tpm2b.h"
15
16 enum cmdStates {CMD_STAGE_INITIALIZE,
17 CMD_STAGE_PREPARE,
18 CMD_STAGE_SEND_COMMAND,
19 CMD_STAGE_RECEIVE_RESPONSE,
20 CMD_STAGE_ALL = 0xff };
21
22 #pragma pack(push, 1)
23 typedef struct _TPM20_Header_In {
24 TPM2_ST tag;
25 UINT32 commandSize;
26 UINT32 commandCode;
27 } TPM20_Header_In;
28
29 typedef struct _TPM20_Header_Out {
30 TPM2_ST tag;
31 UINT32 responseSize;
32 UINT32 responseCode;
33 } TPM20_Header_Out;
34 #pragma pack(pop)
35
36 typedef struct {
37 TSS2_TCTI_CONTEXT *tctiContext;
38 UINT8 *cmdBuffer;
39 UINT32 maxCmdSize;
40 UINT8 cmd_header[sizeof(TPM20_Header_In)]; /* Copy of the cmd header to allow reissue */
41 TPM20_Header_Out rsp_header;
42
43 TPM2_CC commandCode; /* In host endian */
44 UINT32 cpBufferUsedSize;
45 UINT8 *cpBuffer;
46 UINT32 *rspParamsSize;
47 UINT8 previousStage;
48 UINT8 authsCount;
49 UINT8 numResponseHandles;
50
51 struct
52 {
53 UINT16 decryptAllowed:1;
54 UINT16 encryptAllowed:1;
55 UINT16 decryptNull:1;
56 UINT16 authAllowed:1;
57 };
58
59 /* Offset to next data in command/response buffer. */
60 size_t nextData;
61 } _TSS2_SYS_CONTEXT_BLOB;
62
63 static inline _TSS2_SYS_CONTEXT_BLOB *
syscontext_cast(TSS2_SYS_CONTEXT * ctx)64 syscontext_cast(TSS2_SYS_CONTEXT *ctx)
65 {
66 return (_TSS2_SYS_CONTEXT_BLOB*) ctx;
67 }
68
69 static inline TPM20_Header_Out *
resp_header_from_cxt(_TSS2_SYS_CONTEXT_BLOB * ctx)70 resp_header_from_cxt(_TSS2_SYS_CONTEXT_BLOB *ctx)
71 {
72 return (TPM20_Header_Out *)ctx->cmdBuffer;
73 }
74
75 static inline TPM20_Header_In *
req_header_from_cxt(_TSS2_SYS_CONTEXT_BLOB * ctx)76 req_header_from_cxt(_TSS2_SYS_CONTEXT_BLOB *ctx)
77 {
78 return (TPM20_Header_In *)ctx->cmdBuffer;
79 }
80
81 typedef struct {
82 TPM2_CC commandCode;
83 int numCommandHandles;
84 int numResponseHandles;
85 } COMMAND_HANDLES;
86
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90
91 TSS2_RC CopyCommandHeader(_TSS2_SYS_CONTEXT_BLOB *ctx, TPM2_CC commandCode);
92 UINT32 GetCommandSize(_TSS2_SYS_CONTEXT_BLOB *ctx);
93 void InitSysContextFields(_TSS2_SYS_CONTEXT_BLOB *ctx);
94 void InitSysContextPtrs(_TSS2_SYS_CONTEXT_BLOB *ctx, size_t contextSize);
95 TSS2_RC CompleteChecks(_TSS2_SYS_CONTEXT_BLOB *ctx);
96 TSS2_RC CommonComplete(_TSS2_SYS_CONTEXT_BLOB *ctx);
97
98 TSS2_RC CommonOneCall(
99 _TSS2_SYS_CONTEXT_BLOB *ctx,
100 TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
101 TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray);
102
103 TSS2_RC CommonPreparePrologue(
104 _TSS2_SYS_CONTEXT_BLOB *ctx,
105 TPM2_CC commandCode);
106
107 TSS2_RC CommonPrepareEpilogue(_TSS2_SYS_CONTEXT_BLOB *ctx);
108 bool IsAlgorithmWeak(TPM2_ALG_ID algorith, TPM2_KEY_SIZE key_size);
109 TSS2_RC ValidatePublicTemplate(const TPM2B_PUBLIC *pub);
110 TSS2_RC ValidateNV_Public(const TPM2B_NV_PUBLIC *nv_public_info);
111 TSS2_RC ValidateTPML_PCR_SELECTION(const TPML_PCR_SELECTION *pcr_selection);
112
113 #ifdef __cplusplus
114 }
115 #endif
116 #endif
117