]>
Commit | Line | Data |
---|---|---|
c1d93242 JY |
1 | /** @file\r |
2 | Implement TPM2 help.\r | |
3 | \r | |
4333b99d | 4 | Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved. <BR>\r |
c1d93242 JY |
5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT 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 | |
22 | typedef 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 | |
28 | STATIC 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 | |
43 | UINT16\r | |
44 | EFIAPI\r | |
45 | GetHashSizeFromAlgo (\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 | |
66 | UINT32\r | |
67 | EFIAPI\r | |
68 | GetHashMaskFromAlgo (\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 | |
90 | UINT32\r | |
91 | EFIAPI\r | |
92 | CopyAuthSessionCommand (\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 | |
153 | @return AuthSession size\r | |
154 | **/\r | |
155 | UINT32\r | |
156 | EFIAPI\r | |
157 | CopyAuthSessionResponse (\r | |
158 | IN UINT8 *AuthSessionIn,\r | |
159 | OUT TPMS_AUTH_RESPONSE *AuthSessionOut OPTIONAL\r | |
160 | )\r | |
161 | {\r | |
162 | UINT8 *Buffer;\r | |
163 | TPMS_AUTH_RESPONSE LocalAuthSessionOut;\r | |
164 | \r | |
165 | if (AuthSessionOut == NULL) {\r | |
166 | AuthSessionOut = &LocalAuthSessionOut;\r | |
167 | }\r | |
168 | \r | |
169 | Buffer = (UINT8 *)AuthSessionIn;\r | |
170 | \r | |
171 | // nonce\r | |
172 | AuthSessionOut->nonce.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r | |
173 | Buffer += sizeof(UINT16);\r | |
174 | \r | |
175 | CopyMem (AuthSessionOut->nonce.buffer, Buffer, AuthSessionOut->nonce.size);\r | |
176 | Buffer += AuthSessionOut->nonce.size;\r | |
177 | \r | |
178 | // sessionAttributes\r | |
179 | *(UINT8 *)&AuthSessionOut->sessionAttributes = *(UINT8 *)Buffer;\r | |
58dbfc3c | 180 | Buffer++;\r |
c1d93242 JY |
181 | \r |
182 | // hmac\r | |
183 | AuthSessionOut->hmac.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r | |
184 | Buffer += sizeof(UINT16);\r | |
185 | \r | |
186 | CopyMem (AuthSessionOut->hmac.buffer, Buffer, AuthSessionOut->hmac.size);\r | |
187 | Buffer += AuthSessionOut->hmac.size;\r | |
188 | \r | |
4333b99d | 189 | return (UINT32)((UINTN)Buffer - (UINTN)AuthSessionIn);\r |
c1d93242 | 190 | }\r |
d4b9b2c3 | 191 | \r |
f5e34e37 JY |
192 | /**\r |
193 | Return if hash alg is supported in HashAlgorithmMask.\r | |
194 | \r | |
195 | @param HashAlg Hash algorithm to be checked.\r | |
196 | @param HashAlgorithmMask Bitfield of allowed hash algorithms.\r | |
197 | \r | |
198 | @retval TRUE Hash algorithm is supported.\r | |
199 | @retval FALSE Hash algorithm is not supported.\r | |
200 | **/\r | |
201 | BOOLEAN\r | |
697c30b1 | 202 | EFIAPI\r |
f5e34e37 JY |
203 | IsHashAlgSupportedInHashAlgorithmMask(\r |
204 | IN TPMI_ALG_HASH HashAlg,\r | |
205 | IN UINT32 HashAlgorithmMask\r | |
206 | )\r | |
207 | {\r | |
208 | switch (HashAlg) {\r | |
209 | case TPM_ALG_SHA1:\r | |
210 | if ((HashAlgorithmMask & HASH_ALG_SHA1) != 0) {\r | |
211 | return TRUE;\r | |
212 | }\r | |
213 | break;\r | |
214 | case TPM_ALG_SHA256:\r | |
215 | if ((HashAlgorithmMask & HASH_ALG_SHA256) != 0) {\r | |
216 | return TRUE;\r | |
217 | }\r | |
218 | break;\r | |
219 | case TPM_ALG_SHA384:\r | |
220 | if ((HashAlgorithmMask & HASH_ALG_SHA384) != 0) {\r | |
221 | return TRUE;\r | |
222 | }\r | |
223 | break;\r | |
224 | case TPM_ALG_SHA512:\r | |
225 | if ((HashAlgorithmMask & HASH_ALG_SHA512) != 0) {\r | |
226 | return TRUE;\r | |
227 | }\r | |
228 | break;\r | |
229 | case TPM_ALG_SM3_256:\r | |
230 | if ((HashAlgorithmMask & HASH_ALG_SM3_256) != 0) {\r | |
231 | return TRUE;\r | |
232 | }\r | |
233 | break;\r | |
234 | }\r | |
235 | \r | |
236 | return FALSE;\r | |
237 | }\r | |
238 | \r | |
239 | /**\r | |
240 | Copy TPML_DIGEST_VALUES into a buffer\r | |
241 | \r | |
ae1a4284 | 242 | @param[in,out] Buffer Buffer to hold copied TPML_DIGEST_VALUES compact binary.\r |
f5e34e37 JY |
243 | @param[in] DigestList TPML_DIGEST_VALUES to be copied.\r |
244 | @param[in] HashAlgorithmMask HASH bits corresponding to the desired digests to copy.\r | |
245 | \r | |
246 | @return The end of buffer to hold TPML_DIGEST_VALUES.\r | |
247 | **/\r | |
248 | VOID *\r | |
249 | EFIAPI\r | |
250 | CopyDigestListToBuffer (\r | |
251 | IN OUT VOID *Buffer,\r | |
252 | IN TPML_DIGEST_VALUES *DigestList,\r | |
253 | IN UINT32 HashAlgorithmMask\r | |
254 | )\r | |
255 | {\r | |
256 | UINTN Index;\r | |
257 | UINT16 DigestSize;\r | |
be93a17b SZ |
258 | UINT32 DigestListCount;\r |
259 | UINT32 *DigestListCountPtr;\r | |
f5e34e37 | 260 | \r |
be93a17b SZ |
261 | DigestListCountPtr = (UINT32 *) Buffer;\r |
262 | DigestListCount = 0;\r | |
f5e34e37 JY |
263 | Buffer = (UINT8 *)Buffer + sizeof(DigestList->count);\r |
264 | for (Index = 0; Index < DigestList->count; Index++) {\r | |
265 | if (!IsHashAlgSupportedInHashAlgorithmMask(DigestList->digests[Index].hashAlg, HashAlgorithmMask)) {\r | |
266 | DEBUG ((EFI_D_ERROR, "WARNING: TPM2 Event log has HashAlg unsupported by PCR bank (0x%x)\n", DigestList->digests[Index].hashAlg));\r | |
267 | continue;\r | |
268 | }\r | |
269 | CopyMem (Buffer, &DigestList->digests[Index].hashAlg, sizeof(DigestList->digests[Index].hashAlg));\r | |
270 | Buffer = (UINT8 *)Buffer + sizeof(DigestList->digests[Index].hashAlg);\r | |
271 | DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);\r | |
272 | CopyMem (Buffer, &DigestList->digests[Index].digest, DigestSize);\r | |
273 | Buffer = (UINT8 *)Buffer + DigestSize;\r | |
be93a17b | 274 | DigestListCount++;\r |
f5e34e37 | 275 | }\r |
be93a17b | 276 | WriteUnaligned32 (DigestListCountPtr, DigestListCount);\r |
f5e34e37 JY |
277 | \r |
278 | return Buffer;\r | |
279 | }\r | |
280 | \r | |
77e55cf4 JY |
281 | /**\r |
282 | Get TPML_DIGEST_VALUES data size.\r | |
283 | \r | |
284 | @param[in] DigestList TPML_DIGEST_VALUES data.\r | |
285 | \r | |
286 | @return TPML_DIGEST_VALUES data size.\r | |
287 | **/\r | |
288 | UINT32\r | |
289 | EFIAPI\r | |
290 | GetDigestListSize (\r | |
291 | IN TPML_DIGEST_VALUES *DigestList\r | |
292 | )\r | |
293 | {\r | |
294 | UINTN Index;\r | |
295 | UINT16 DigestSize;\r | |
296 | UINT32 TotalSize;\r | |
297 | \r | |
298 | TotalSize = sizeof(DigestList->count);\r | |
299 | for (Index = 0; Index < DigestList->count; Index++) {\r | |
300 | DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);\r | |
301 | TotalSize += sizeof(DigestList->digests[Index].hashAlg) + DigestSize;\r | |
302 | }\r | |
303 | \r | |
304 | return TotalSize;\r | |
305 | }\r | |
306 | \r | |
d4b9b2c3 JY |
307 | /**\r |
308 | This function get digest from digest list.\r | |
309 | \r | |
f28ab849 SZ |
310 | @param[in] HashAlg Digest algorithm\r |
311 | @param[in] DigestList Digest list\r | |
312 | @param[out] Digest Digest\r | |
d4b9b2c3 | 313 | \r |
f28ab849 SZ |
314 | @retval EFI_SUCCESS Digest is found and returned.\r |
315 | @retval EFI_NOT_FOUND Digest is not found.\r | |
d4b9b2c3 JY |
316 | **/\r |
317 | EFI_STATUS\r | |
318 | EFIAPI\r | |
319 | GetDigestFromDigestList (\r | |
320 | IN TPMI_ALG_HASH HashAlg,\r | |
321 | IN TPML_DIGEST_VALUES *DigestList,\r | |
f28ab849 | 322 | OUT VOID *Digest\r |
d4b9b2c3 JY |
323 | )\r |
324 | {\r | |
325 | UINTN Index;\r | |
326 | UINT16 DigestSize;\r | |
327 | \r | |
328 | DigestSize = GetHashSizeFromAlgo (HashAlg);\r | |
329 | for (Index = 0; Index < DigestList->count; Index++) {\r | |
330 | if (DigestList->digests[Index].hashAlg == HashAlg) {\r | |
331 | CopyMem (\r | |
332 | Digest,\r | |
333 | &DigestList->digests[Index].digest,\r | |
334 | DigestSize\r | |
335 | );\r | |
336 | return EFI_SUCCESS;\r | |
337 | }\r | |
338 | }\r | |
339 | \r | |
340 | return EFI_NOT_FOUND;\r | |
f28ab849 | 341 | }\r |