]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c
SecurityPkg Tpm2DeviceLibDTpm: Update enum type name to match the one in lib
[mirror_edk2.git] / SecurityPkg / Library / Tpm2CommandLib / Tpm2Help.c
CommitLineData
c1d93242
JY
1/** @file\r
2 Implement TPM2 help.\r
3\r
dd577319 4Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>\r
c1d93242
JY
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
b8ae1f4d 25 UINT32 HashMask;\r
c1d93242
JY
26} INTERNAL_HASH_INFO;\r
27\r
28STATIC INTERNAL_HASH_INFO mHashInfo[] = {\r
b8ae1f4d
SZ
29 {TPM_ALG_SHA1, SHA1_DIGEST_SIZE, HASH_ALG_SHA1},\r
30 {TPM_ALG_SHA256, SHA256_DIGEST_SIZE, HASH_ALG_SHA256},\r
31 {TPM_ALG_SM3_256, SM3_256_DIGEST_SIZE, HASH_ALG_SM3_256},\r
32 {TPM_ALG_SHA384, SHA384_DIGEST_SIZE, HASH_ALG_SHA384},\r
33 {TPM_ALG_SHA512, SHA512_DIGEST_SIZE, HASH_ALG_SHA512},\r
c1d93242
JY
34};\r
35\r
36/**\r
37 Return size of digest.\r
38\r
39 @param[in] HashAlgo Hash algorithm\r
40\r
41 @return size of digest\r
42**/\r
43UINT16\r
44EFIAPI\r
45GetHashSizeFromAlgo (\r
46 IN TPMI_ALG_HASH HashAlgo\r
47 )\r
48{\r
49 UINTN Index;\r
50\r
51 for (Index = 0; Index < sizeof(mHashInfo)/sizeof(mHashInfo[0]); Index++) {\r
52 if (mHashInfo[Index].HashAlgo == HashAlgo) {\r
53 return mHashInfo[Index].HashSize;\r
54 }\r
55 }\r
56 return 0;\r
57}\r
58\r
b8ae1f4d
SZ
59/**\r
60 Get hash mask from algorithm.\r
61\r
62 @param[in] HashAlgo Hash algorithm\r
63\r
64 @return Hash mask\r
65**/\r
66UINT32\r
67EFIAPI\r
68GetHashMaskFromAlgo (\r
69 IN TPMI_ALG_HASH HashAlgo\r
70 )\r
71{\r
72 UINTN Index;\r
73\r
74 for (Index = 0; Index < sizeof(mHashInfo)/sizeof(mHashInfo[0]); Index++) {\r
75 if (mHashInfo[Index].HashAlgo == HashAlgo) {\r
76 return mHashInfo[Index].HashMask;\r
77 }\r
78 }\r
79 return 0;\r
80}\r
81\r
c1d93242
JY
82/**\r
83 Copy AuthSessionIn to TPM2 command buffer.\r
84\r
85 @param [in] AuthSessionIn Input AuthSession data\r
86 @param [out] AuthSessionOut Output AuthSession data in TPM2 command buffer\r
87\r
88 @return AuthSession size\r
89**/\r
90UINT32\r
91EFIAPI\r
92CopyAuthSessionCommand (\r
93 IN TPMS_AUTH_COMMAND *AuthSessionIn, OPTIONAL\r
94 OUT UINT8 *AuthSessionOut\r
95 )\r
96{\r
97 UINT8 *Buffer;\r
98\r
99 Buffer = (UINT8 *)AuthSessionOut;\r
100 \r
101 //\r
102 // Add in Auth session\r
103 //\r
104 if (AuthSessionIn != NULL) {\r
105 // sessionHandle\r
106 WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32(AuthSessionIn->sessionHandle));\r
107 Buffer += sizeof(UINT32);\r
108\r
109 // nonce\r
110 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (AuthSessionIn->nonce.size));\r
111 Buffer += sizeof(UINT16);\r
112\r
113 CopyMem (Buffer, AuthSessionIn->nonce.buffer, AuthSessionIn->nonce.size);\r
114 Buffer += AuthSessionIn->nonce.size;\r
115\r
116 // sessionAttributes\r
117 *(UINT8 *)Buffer = *(UINT8 *)&AuthSessionIn->sessionAttributes;\r
58dbfc3c 118 Buffer++;\r
c1d93242
JY
119\r
120 // hmac\r
121 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16 (AuthSessionIn->hmac.size));\r
122 Buffer += sizeof(UINT16);\r
123\r
124 CopyMem (Buffer, AuthSessionIn->hmac.buffer, AuthSessionIn->hmac.size);\r
125 Buffer += AuthSessionIn->hmac.size;\r
126 } else {\r
127 // sessionHandle\r
128 WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32(TPM_RS_PW));\r
129 Buffer += sizeof(UINT32);\r
130\r
131 // nonce = nullNonce\r
132 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16(0));\r
133 Buffer += sizeof(UINT16);\r
134\r
135 // sessionAttributes = 0\r
136 *(UINT8 *)Buffer = 0x00;\r
58dbfc3c 137 Buffer++;\r
c1d93242
JY
138\r
139 // hmac = nullAuth\r
140 WriteUnaligned16 ((UINT16 *)Buffer, SwapBytes16(0));\r
141 Buffer += sizeof(UINT16);\r
142 }\r
143\r
4333b99d 144 return (UINT32)((UINTN)Buffer - (UINTN)AuthSessionOut);\r
c1d93242
JY
145}\r
146\r
147/**\r
148 Copy AuthSessionIn from TPM2 response buffer.\r
149\r
150 @param [in] AuthSessionIn Input AuthSession data in TPM2 response buffer\r
151 @param [out] AuthSessionOut Output AuthSession data\r
152\r
dd577319
ZC
153 @return 0 copy failed\r
154 else AuthSession size\r
c1d93242
JY
155**/\r
156UINT32\r
157EFIAPI\r
158CopyAuthSessionResponse (\r
159 IN UINT8 *AuthSessionIn,\r
160 OUT TPMS_AUTH_RESPONSE *AuthSessionOut OPTIONAL\r
161 )\r
162{\r
163 UINT8 *Buffer;\r
164 TPMS_AUTH_RESPONSE LocalAuthSessionOut;\r
165\r
166 if (AuthSessionOut == NULL) {\r
167 AuthSessionOut = &LocalAuthSessionOut;\r
168 }\r
169\r
170 Buffer = (UINT8 *)AuthSessionIn;\r
171\r
172 // nonce\r
173 AuthSessionOut->nonce.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
174 Buffer += sizeof(UINT16);\r
dd577319
ZC
175 if (AuthSessionOut->nonce.size > sizeof(TPMU_HA)) {\r
176 DEBUG ((DEBUG_ERROR, "CopyAuthSessionResponse - nonce.size error %x\n", AuthSessionOut->nonce.size));\r
177 return 0;\r
178 }\r
c1d93242
JY
179\r
180 CopyMem (AuthSessionOut->nonce.buffer, Buffer, AuthSessionOut->nonce.size);\r
181 Buffer += AuthSessionOut->nonce.size;\r
182\r
183 // sessionAttributes\r
184 *(UINT8 *)&AuthSessionOut->sessionAttributes = *(UINT8 *)Buffer;\r
58dbfc3c 185 Buffer++;\r
c1d93242
JY
186\r
187 // hmac\r
188 AuthSessionOut->hmac.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
189 Buffer += sizeof(UINT16);\r
dd577319
ZC
190 if (AuthSessionOut->hmac.size > sizeof(TPMU_HA)) {\r
191 DEBUG ((DEBUG_ERROR, "CopyAuthSessionResponse - hmac.size error %x\n", AuthSessionOut->hmac.size));\r
192 return 0;\r
193 }\r
c1d93242
JY
194\r
195 CopyMem (AuthSessionOut->hmac.buffer, Buffer, AuthSessionOut->hmac.size);\r
196 Buffer += AuthSessionOut->hmac.size;\r
197\r
4333b99d 198 return (UINT32)((UINTN)Buffer - (UINTN)AuthSessionIn);\r
c1d93242 199}\r
d4b9b2c3 200\r
f5e34e37
JY
201/**\r
202 Return if hash alg is supported in HashAlgorithmMask.\r
203\r
204 @param HashAlg Hash algorithm to be checked.\r
205 @param HashAlgorithmMask Bitfield of allowed hash algorithms.\r
206\r
207 @retval TRUE Hash algorithm is supported.\r
208 @retval FALSE Hash algorithm is not supported.\r
209**/\r
210BOOLEAN\r
697c30b1 211EFIAPI\r
f5e34e37
JY
212IsHashAlgSupportedInHashAlgorithmMask(\r
213 IN TPMI_ALG_HASH HashAlg,\r
214 IN UINT32 HashAlgorithmMask\r
215 )\r
216{\r
217 switch (HashAlg) {\r
218 case TPM_ALG_SHA1:\r
219 if ((HashAlgorithmMask & HASH_ALG_SHA1) != 0) {\r
220 return TRUE;\r
221 }\r
222 break;\r
223 case TPM_ALG_SHA256:\r
224 if ((HashAlgorithmMask & HASH_ALG_SHA256) != 0) {\r
225 return TRUE;\r
226 }\r
227 break;\r
228 case TPM_ALG_SHA384:\r
229 if ((HashAlgorithmMask & HASH_ALG_SHA384) != 0) {\r
230 return TRUE;\r
231 }\r
232 break;\r
233 case TPM_ALG_SHA512:\r
234 if ((HashAlgorithmMask & HASH_ALG_SHA512) != 0) {\r
235 return TRUE;\r
236 }\r
237 break;\r
238 case TPM_ALG_SM3_256:\r
239 if ((HashAlgorithmMask & HASH_ALG_SM3_256) != 0) {\r
240 return TRUE;\r
241 }\r
242 break;\r
243 }\r
244\r
245 return FALSE;\r
246}\r
247\r
248/**\r
249 Copy TPML_DIGEST_VALUES into a buffer\r
250\r
ae1a4284 251 @param[in,out] Buffer Buffer to hold copied TPML_DIGEST_VALUES compact binary.\r
f5e34e37
JY
252 @param[in] DigestList TPML_DIGEST_VALUES to be copied.\r
253 @param[in] HashAlgorithmMask HASH bits corresponding to the desired digests to copy.\r
254\r
255 @return The end of buffer to hold TPML_DIGEST_VALUES.\r
256**/\r
257VOID *\r
258EFIAPI\r
259CopyDigestListToBuffer (\r
260 IN OUT VOID *Buffer,\r
261 IN TPML_DIGEST_VALUES *DigestList,\r
262 IN UINT32 HashAlgorithmMask\r
263 )\r
264{\r
265 UINTN Index;\r
266 UINT16 DigestSize;\r
be93a17b
SZ
267 UINT32 DigestListCount;\r
268 UINT32 *DigestListCountPtr;\r
f5e34e37 269\r
be93a17b
SZ
270 DigestListCountPtr = (UINT32 *) Buffer;\r
271 DigestListCount = 0;\r
f5e34e37
JY
272 Buffer = (UINT8 *)Buffer + sizeof(DigestList->count);\r
273 for (Index = 0; Index < DigestList->count; Index++) {\r
274 if (!IsHashAlgSupportedInHashAlgorithmMask(DigestList->digests[Index].hashAlg, HashAlgorithmMask)) {\r
275 DEBUG ((EFI_D_ERROR, "WARNING: TPM2 Event log has HashAlg unsupported by PCR bank (0x%x)\n", DigestList->digests[Index].hashAlg));\r
276 continue;\r
277 }\r
278 CopyMem (Buffer, &DigestList->digests[Index].hashAlg, sizeof(DigestList->digests[Index].hashAlg));\r
279 Buffer = (UINT8 *)Buffer + sizeof(DigestList->digests[Index].hashAlg);\r
280 DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);\r
281 CopyMem (Buffer, &DigestList->digests[Index].digest, DigestSize);\r
282 Buffer = (UINT8 *)Buffer + DigestSize;\r
be93a17b 283 DigestListCount++;\r
f5e34e37 284 }\r
be93a17b 285 WriteUnaligned32 (DigestListCountPtr, DigestListCount);\r
f5e34e37
JY
286\r
287 return Buffer;\r
288}\r
289\r
77e55cf4
JY
290/**\r
291 Get TPML_DIGEST_VALUES data size.\r
292\r
293 @param[in] DigestList TPML_DIGEST_VALUES data.\r
294\r
295 @return TPML_DIGEST_VALUES data size.\r
296**/\r
297UINT32\r
298EFIAPI\r
299GetDigestListSize (\r
300 IN TPML_DIGEST_VALUES *DigestList\r
301 )\r
302{\r
303 UINTN Index;\r
304 UINT16 DigestSize;\r
305 UINT32 TotalSize;\r
306\r
307 TotalSize = sizeof(DigestList->count);\r
308 for (Index = 0; Index < DigestList->count; Index++) {\r
309 DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);\r
310 TotalSize += sizeof(DigestList->digests[Index].hashAlg) + DigestSize;\r
311 }\r
312\r
313 return TotalSize;\r
314}\r
315\r
d4b9b2c3
JY
316/**\r
317 This function get digest from digest list.\r
318\r
f28ab849
SZ
319 @param[in] HashAlg Digest algorithm\r
320 @param[in] DigestList Digest list\r
321 @param[out] Digest Digest\r
d4b9b2c3 322\r
f28ab849
SZ
323 @retval EFI_SUCCESS Digest is found and returned.\r
324 @retval EFI_NOT_FOUND Digest is not found.\r
d4b9b2c3
JY
325**/\r
326EFI_STATUS\r
327EFIAPI\r
328GetDigestFromDigestList (\r
329 IN TPMI_ALG_HASH HashAlg,\r
330 IN TPML_DIGEST_VALUES *DigestList,\r
f28ab849 331 OUT VOID *Digest\r
d4b9b2c3
JY
332 )\r
333{\r
334 UINTN Index;\r
335 UINT16 DigestSize;\r
336\r
337 DigestSize = GetHashSizeFromAlgo (HashAlg);\r
338 for (Index = 0; Index < DigestList->count; Index++) {\r
339 if (DigestList->digests[Index].hashAlg == HashAlg) {\r
340 CopyMem (\r
341 Digest,\r
342 &DigestList->digests[Index].digest,\r
343 DigestSize\r
344 );\r
345 return EFI_SUCCESS;\r
346 }\r
347 }\r
348\r
349 return EFI_NOT_FOUND;\r
f28ab849 350}\r