]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c
Add TPM2 implementation.
[mirror_edk2.git] / SecurityPkg / Library / Tpm2CommandLib / Tpm2Help.c
CommitLineData
c1d93242
JY
1/** @file\r
2 Implement TPM2 help.\r
3\r
4Copyright (c) 2013, Intel Corporation. All rights reserved. <BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <IndustryStandard/UefiTcgPlatform.h>\r
16#include <Library/Tpm2CommandLib.h>\r
17#include <Library/Tpm2DeviceLib.h>\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/BaseLib.h>\r
20#include <Library/DebugLib.h>\r
21\r
22typedef struct {\r
23 TPMI_ALG_HASH HashAlgo;\r
24 UINT16 HashSize;\r
25} INTERNAL_HASH_INFO;\r
26\r
27STATIC INTERNAL_HASH_INFO mHashInfo[] = {\r
28 {TPM_ALG_SHA1, SHA1_DIGEST_SIZE},\r
29 {TPM_ALG_SHA256, SHA256_DIGEST_SIZE},\r
30 {TPM_ALG_SM3_256, SM3_256_DIGEST_SIZE},\r
31 {TPM_ALG_SHA384, SHA384_DIGEST_SIZE},\r
32 {TPM_ALG_SHA512, SHA512_DIGEST_SIZE},\r
33};\r
34\r
35/**\r
36 Return size of digest.\r
37\r
38 @param[in] HashAlgo Hash algorithm\r
39\r
40 @return size of digest\r
41**/\r
42UINT16\r
43EFIAPI\r
44GetHashSizeFromAlgo (\r
45 IN TPMI_ALG_HASH HashAlgo\r
46 )\r
47{\r
48 UINTN Index;\r
49\r
50 for (Index = 0; Index < sizeof(mHashInfo)/sizeof(mHashInfo[0]); Index++) {\r
51 if (mHashInfo[Index].HashAlgo == HashAlgo) {\r
52 return mHashInfo[Index].HashSize;\r
53 }\r
54 }\r
55 return 0;\r
56}\r
57\r
58/**\r
59 Copy AuthSessionIn to TPM2 command buffer.\r
60\r
61 @param [in] AuthSessionIn Input AuthSession data\r
62 @param [out] AuthSessionOut Output AuthSession data in TPM2 command buffer\r
63\r
64 @return AuthSession size\r
65**/\r
66UINT32\r
67EFIAPI\r
68CopyAuthSessionCommand (\r
69 IN TPMS_AUTH_COMMAND *AuthSessionIn, OPTIONAL\r
70 OUT UINT8 *AuthSessionOut\r
71 )\r
72{\r
73 UINT8 *Buffer;\r
74\r
75 Buffer = (UINT8 *)AuthSessionOut;\r
76 \r
77 //\r
78 // Add in Auth session\r
79 //\r
80 if (AuthSessionIn != NULL) {\r
81 // sessionHandle\r
82 WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32(AuthSessionIn->sessionHandle));\r
83 Buffer += sizeof(UINT32);\r
84\r
85 // nonce\r
86 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (AuthSessionIn->nonce.size));\r
87 Buffer += sizeof(UINT16);\r
88\r
89 CopyMem (Buffer, AuthSessionIn->nonce.buffer, AuthSessionIn->nonce.size);\r
90 Buffer += AuthSessionIn->nonce.size;\r
91\r
92 // sessionAttributes\r
93 *(UINT8 *)Buffer = *(UINT8 *)&AuthSessionIn->sessionAttributes;\r
94 Buffer += sizeof(UINT8);\r
95\r
96 // hmac\r
97 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (AuthSessionIn->hmac.size));\r
98 Buffer += sizeof(UINT16);\r
99\r
100 CopyMem (Buffer, AuthSessionIn->hmac.buffer, AuthSessionIn->hmac.size);\r
101 Buffer += AuthSessionIn->hmac.size;\r
102 } else {\r
103 // sessionHandle\r
104 WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32(TPM_RS_PW));\r
105 Buffer += sizeof(UINT32);\r
106\r
107 // nonce = nullNonce\r
108 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16(0));\r
109 Buffer += sizeof(UINT16);\r
110\r
111 // sessionAttributes = 0\r
112 *(UINT8 *)Buffer = 0x00;\r
113 Buffer += sizeof(UINT8);\r
114\r
115 // hmac = nullAuth\r
116 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16(0));\r
117 Buffer += sizeof(UINT16);\r
118 }\r
119\r
120 return (UINT32)(UINTN)(Buffer - (UINT8 *)AuthSessionOut);\r
121}\r
122\r
123/**\r
124 Copy AuthSessionIn from TPM2 response buffer.\r
125\r
126 @param [in] AuthSessionIn Input AuthSession data in TPM2 response buffer\r
127 @param [out] AuthSessionOut Output AuthSession data\r
128\r
129 @return AuthSession size\r
130**/\r
131UINT32\r
132EFIAPI\r
133CopyAuthSessionResponse (\r
134 IN UINT8 *AuthSessionIn,\r
135 OUT TPMS_AUTH_RESPONSE *AuthSessionOut OPTIONAL\r
136 )\r
137{\r
138 UINT8 *Buffer;\r
139 TPMS_AUTH_RESPONSE LocalAuthSessionOut;\r
140\r
141 if (AuthSessionOut == NULL) {\r
142 AuthSessionOut = &LocalAuthSessionOut;\r
143 }\r
144\r
145 Buffer = (UINT8 *)AuthSessionIn;\r
146\r
147 // nonce\r
148 AuthSessionOut->nonce.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
149 Buffer += sizeof(UINT16);\r
150\r
151 CopyMem (AuthSessionOut->nonce.buffer, Buffer, AuthSessionOut->nonce.size);\r
152 Buffer += AuthSessionOut->nonce.size;\r
153\r
154 // sessionAttributes\r
155 *(UINT8 *)&AuthSessionOut->sessionAttributes = *(UINT8 *)Buffer;\r
156 Buffer += sizeof(UINT8);\r
157\r
158 // hmac\r
159 AuthSessionOut->hmac.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
160 Buffer += sizeof(UINT16);\r
161\r
162 CopyMem (AuthSessionOut->hmac.buffer, Buffer, AuthSessionOut->hmac.size);\r
163 Buffer += AuthSessionOut->hmac.size;\r
164\r
165 return (UINT32)(UINTN)(Buffer - (UINT8 *)AuthSessionIn);\r
166}\r