]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c
SecurityPkg TPM2: Make IsHashAlgSupportedInHashAlgorithmMask external
[mirror_edk2.git] / SecurityPkg / Library / Tpm2CommandLib / Tpm2Help.c
CommitLineData
c1d93242
JY
1/** @file\r
2 Implement TPM2 help.\r
3\r
d4b9b2c3 4Copyright (c) 2013 - 2016, 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
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
58dbfc3c 94 Buffer++;\r
c1d93242
JY
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
58dbfc3c 113 Buffer++;\r
c1d93242
JY
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
58dbfc3c 156 Buffer++;\r
c1d93242
JY
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
d4b9b2c3 167\r
f5e34e37
JY
168/**\r
169 Return if hash alg is supported in HashAlgorithmMask.\r
170\r
171 @param HashAlg Hash algorithm to be checked.\r
172 @param HashAlgorithmMask Bitfield of allowed hash algorithms.\r
173\r
174 @retval TRUE Hash algorithm is supported.\r
175 @retval FALSE Hash algorithm is not supported.\r
176**/\r
177BOOLEAN\r
697c30b1 178EFIAPI\r
f5e34e37
JY
179IsHashAlgSupportedInHashAlgorithmMask(\r
180 IN TPMI_ALG_HASH HashAlg,\r
181 IN UINT32 HashAlgorithmMask\r
182 )\r
183{\r
184 switch (HashAlg) {\r
185 case TPM_ALG_SHA1:\r
186 if ((HashAlgorithmMask & HASH_ALG_SHA1) != 0) {\r
187 return TRUE;\r
188 }\r
189 break;\r
190 case TPM_ALG_SHA256:\r
191 if ((HashAlgorithmMask & HASH_ALG_SHA256) != 0) {\r
192 return TRUE;\r
193 }\r
194 break;\r
195 case TPM_ALG_SHA384:\r
196 if ((HashAlgorithmMask & HASH_ALG_SHA384) != 0) {\r
197 return TRUE;\r
198 }\r
199 break;\r
200 case TPM_ALG_SHA512:\r
201 if ((HashAlgorithmMask & HASH_ALG_SHA512) != 0) {\r
202 return TRUE;\r
203 }\r
204 break;\r
205 case TPM_ALG_SM3_256:\r
206 if ((HashAlgorithmMask & HASH_ALG_SM3_256) != 0) {\r
207 return TRUE;\r
208 }\r
209 break;\r
210 }\r
211\r
212 return FALSE;\r
213}\r
214\r
215/**\r
216 Copy TPML_DIGEST_VALUES into a buffer\r
217\r
218 @param[in,out] Buffer Buffer to hold TPML_DIGEST_VALUES.\r
219 @param[in] DigestList TPML_DIGEST_VALUES to be copied.\r
220 @param[in] HashAlgorithmMask HASH bits corresponding to the desired digests to copy.\r
221\r
222 @return The end of buffer to hold TPML_DIGEST_VALUES.\r
223**/\r
224VOID *\r
225EFIAPI\r
226CopyDigestListToBuffer (\r
227 IN OUT VOID *Buffer,\r
228 IN TPML_DIGEST_VALUES *DigestList,\r
229 IN UINT32 HashAlgorithmMask\r
230 )\r
231{\r
232 UINTN Index;\r
233 UINT16 DigestSize;\r
be93a17b
SZ
234 UINT32 DigestListCount;\r
235 UINT32 *DigestListCountPtr;\r
f5e34e37 236\r
be93a17b
SZ
237 DigestListCountPtr = (UINT32 *) Buffer;\r
238 DigestListCount = 0;\r
f5e34e37
JY
239 Buffer = (UINT8 *)Buffer + sizeof(DigestList->count);\r
240 for (Index = 0; Index < DigestList->count; Index++) {\r
241 if (!IsHashAlgSupportedInHashAlgorithmMask(DigestList->digests[Index].hashAlg, HashAlgorithmMask)) {\r
242 DEBUG ((EFI_D_ERROR, "WARNING: TPM2 Event log has HashAlg unsupported by PCR bank (0x%x)\n", DigestList->digests[Index].hashAlg));\r
243 continue;\r
244 }\r
245 CopyMem (Buffer, &DigestList->digests[Index].hashAlg, sizeof(DigestList->digests[Index].hashAlg));\r
246 Buffer = (UINT8 *)Buffer + sizeof(DigestList->digests[Index].hashAlg);\r
247 DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);\r
248 CopyMem (Buffer, &DigestList->digests[Index].digest, DigestSize);\r
249 Buffer = (UINT8 *)Buffer + DigestSize;\r
be93a17b 250 DigestListCount++;\r
f5e34e37 251 }\r
be93a17b 252 WriteUnaligned32 (DigestListCountPtr, DigestListCount);\r
f5e34e37
JY
253\r
254 return Buffer;\r
255}\r
256\r
77e55cf4
JY
257/**\r
258 Get TPML_DIGEST_VALUES data size.\r
259\r
260 @param[in] DigestList TPML_DIGEST_VALUES data.\r
261\r
262 @return TPML_DIGEST_VALUES data size.\r
263**/\r
264UINT32\r
265EFIAPI\r
266GetDigestListSize (\r
267 IN TPML_DIGEST_VALUES *DigestList\r
268 )\r
269{\r
270 UINTN Index;\r
271 UINT16 DigestSize;\r
272 UINT32 TotalSize;\r
273\r
274 TotalSize = sizeof(DigestList->count);\r
275 for (Index = 0; Index < DigestList->count; Index++) {\r
276 DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);\r
277 TotalSize += sizeof(DigestList->digests[Index].hashAlg) + DigestSize;\r
278 }\r
279\r
280 return TotalSize;\r
281}\r
282\r
d4b9b2c3
JY
283/**\r
284 This function get digest from digest list.\r
285\r
f28ab849
SZ
286 @param[in] HashAlg Digest algorithm\r
287 @param[in] DigestList Digest list\r
288 @param[out] Digest Digest\r
d4b9b2c3 289\r
f28ab849
SZ
290 @retval EFI_SUCCESS Digest is found and returned.\r
291 @retval EFI_NOT_FOUND Digest is not found.\r
d4b9b2c3
JY
292**/\r
293EFI_STATUS\r
294EFIAPI\r
295GetDigestFromDigestList (\r
296 IN TPMI_ALG_HASH HashAlg,\r
297 IN TPML_DIGEST_VALUES *DigestList,\r
f28ab849 298 OUT VOID *Digest\r
d4b9b2c3
JY
299 )\r
300{\r
301 UINTN Index;\r
302 UINT16 DigestSize;\r
303\r
304 DigestSize = GetHashSizeFromAlgo (HashAlg);\r
305 for (Index = 0; Index < DigestList->count; Index++) {\r
306 if (DigestList->digests[Index].hashAlg == HashAlg) {\r
307 CopyMem (\r
308 Digest,\r
309 &DigestList->digests[Index].digest,\r
310 DigestSize\r
311 );\r
312 return EFI_SUCCESS;\r
313 }\r
314 }\r
315\r
316 return EFI_NOT_FOUND;\r
f28ab849 317}\r