4 Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <IndustryStandard/UefiTcgPlatform.h>
16 #include <Library/Tpm2CommandLib.h>
17 #include <Library/Tpm2DeviceLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
23 TPMI_ALG_HASH HashAlgo
;
28 STATIC INTERNAL_HASH_INFO mHashInfo
[] = {
29 {TPM_ALG_SHA1
, SHA1_DIGEST_SIZE
, HASH_ALG_SHA1
},
30 {TPM_ALG_SHA256
, SHA256_DIGEST_SIZE
, HASH_ALG_SHA256
},
31 {TPM_ALG_SM3_256
, SM3_256_DIGEST_SIZE
, HASH_ALG_SM3_256
},
32 {TPM_ALG_SHA384
, SHA384_DIGEST_SIZE
, HASH_ALG_SHA384
},
33 {TPM_ALG_SHA512
, SHA512_DIGEST_SIZE
, HASH_ALG_SHA512
},
37 Return size of digest.
39 @param[in] HashAlgo Hash algorithm
41 @return size of digest
46 IN TPMI_ALG_HASH HashAlgo
51 for (Index
= 0; Index
< sizeof(mHashInfo
)/sizeof(mHashInfo
[0]); Index
++) {
52 if (mHashInfo
[Index
].HashAlgo
== HashAlgo
) {
53 return mHashInfo
[Index
].HashSize
;
60 Get hash mask from algorithm.
62 @param[in] HashAlgo Hash algorithm
69 IN TPMI_ALG_HASH HashAlgo
74 for (Index
= 0; Index
< sizeof(mHashInfo
)/sizeof(mHashInfo
[0]); Index
++) {
75 if (mHashInfo
[Index
].HashAlgo
== HashAlgo
) {
76 return mHashInfo
[Index
].HashMask
;
83 Copy AuthSessionIn to TPM2 command buffer.
85 @param [in] AuthSessionIn Input AuthSession data
86 @param [out] AuthSessionOut Output AuthSession data in TPM2 command buffer
88 @return AuthSession size
92 CopyAuthSessionCommand (
93 IN TPMS_AUTH_COMMAND
*AuthSessionIn
, OPTIONAL
94 OUT UINT8
*AuthSessionOut
99 Buffer
= (UINT8
*)AuthSessionOut
;
102 // Add in Auth session
104 if (AuthSessionIn
!= NULL
) {
106 WriteUnaligned32 ((UINT32
*)Buffer
, SwapBytes32(AuthSessionIn
->sessionHandle
));
107 Buffer
+= sizeof(UINT32
);
110 WriteUnaligned16 ((UINT16
*)Buffer
, SwapBytes16 (AuthSessionIn
->nonce
.size
));
111 Buffer
+= sizeof(UINT16
);
113 CopyMem (Buffer
, AuthSessionIn
->nonce
.buffer
, AuthSessionIn
->nonce
.size
);
114 Buffer
+= AuthSessionIn
->nonce
.size
;
117 *(UINT8
*)Buffer
= *(UINT8
*)&AuthSessionIn
->sessionAttributes
;
121 WriteUnaligned16 ((UINT16
*)Buffer
, SwapBytes16 (AuthSessionIn
->hmac
.size
));
122 Buffer
+= sizeof(UINT16
);
124 CopyMem (Buffer
, AuthSessionIn
->hmac
.buffer
, AuthSessionIn
->hmac
.size
);
125 Buffer
+= AuthSessionIn
->hmac
.size
;
128 WriteUnaligned32 ((UINT32
*)Buffer
, SwapBytes32(TPM_RS_PW
));
129 Buffer
+= sizeof(UINT32
);
132 WriteUnaligned16 ((UINT16
*)Buffer
, SwapBytes16(0));
133 Buffer
+= sizeof(UINT16
);
135 // sessionAttributes = 0
136 *(UINT8
*)Buffer
= 0x00;
140 WriteUnaligned16 ((UINT16
*)Buffer
, SwapBytes16(0));
141 Buffer
+= sizeof(UINT16
);
144 return (UINT32
)(UINTN
)(Buffer
- (UINT8
*)AuthSessionOut
);
148 Copy AuthSessionIn from TPM2 response buffer.
150 @param [in] AuthSessionIn Input AuthSession data in TPM2 response buffer
151 @param [out] AuthSessionOut Output AuthSession data
153 @return AuthSession size
157 CopyAuthSessionResponse (
158 IN UINT8
*AuthSessionIn
,
159 OUT TPMS_AUTH_RESPONSE
*AuthSessionOut OPTIONAL
163 TPMS_AUTH_RESPONSE LocalAuthSessionOut
;
165 if (AuthSessionOut
== NULL
) {
166 AuthSessionOut
= &LocalAuthSessionOut
;
169 Buffer
= (UINT8
*)AuthSessionIn
;
172 AuthSessionOut
->nonce
.size
= SwapBytes16 (ReadUnaligned16 ((UINT16
*)Buffer
));
173 Buffer
+= sizeof(UINT16
);
175 CopyMem (AuthSessionOut
->nonce
.buffer
, Buffer
, AuthSessionOut
->nonce
.size
);
176 Buffer
+= AuthSessionOut
->nonce
.size
;
179 *(UINT8
*)&AuthSessionOut
->sessionAttributes
= *(UINT8
*)Buffer
;
183 AuthSessionOut
->hmac
.size
= SwapBytes16 (ReadUnaligned16 ((UINT16
*)Buffer
));
184 Buffer
+= sizeof(UINT16
);
186 CopyMem (AuthSessionOut
->hmac
.buffer
, Buffer
, AuthSessionOut
->hmac
.size
);
187 Buffer
+= AuthSessionOut
->hmac
.size
;
189 return (UINT32
)(UINTN
)(Buffer
- (UINT8
*)AuthSessionIn
);
193 Return if hash alg is supported in HashAlgorithmMask.
195 @param HashAlg Hash algorithm to be checked.
196 @param HashAlgorithmMask Bitfield of allowed hash algorithms.
198 @retval TRUE Hash algorithm is supported.
199 @retval FALSE Hash algorithm is not supported.
203 IsHashAlgSupportedInHashAlgorithmMask(
204 IN TPMI_ALG_HASH HashAlg
,
205 IN UINT32 HashAlgorithmMask
210 if ((HashAlgorithmMask
& HASH_ALG_SHA1
) != 0) {
215 if ((HashAlgorithmMask
& HASH_ALG_SHA256
) != 0) {
220 if ((HashAlgorithmMask
& HASH_ALG_SHA384
) != 0) {
225 if ((HashAlgorithmMask
& HASH_ALG_SHA512
) != 0) {
229 case TPM_ALG_SM3_256
:
230 if ((HashAlgorithmMask
& HASH_ALG_SM3_256
) != 0) {
240 Copy TPML_DIGEST_VALUES into a buffer
242 @param[in,out] Buffer Buffer to hold copied TPML_DIGEST_VALUES compact binary.
243 @param[in] DigestList TPML_DIGEST_VALUES to be copied.
244 @param[in] HashAlgorithmMask HASH bits corresponding to the desired digests to copy.
246 @return The end of buffer to hold TPML_DIGEST_VALUES.
250 CopyDigestListToBuffer (
252 IN TPML_DIGEST_VALUES
*DigestList
,
253 IN UINT32 HashAlgorithmMask
258 UINT32 DigestListCount
;
259 UINT32
*DigestListCountPtr
;
261 DigestListCountPtr
= (UINT32
*) Buffer
;
263 Buffer
= (UINT8
*)Buffer
+ sizeof(DigestList
->count
);
264 for (Index
= 0; Index
< DigestList
->count
; Index
++) {
265 if (!IsHashAlgSupportedInHashAlgorithmMask(DigestList
->digests
[Index
].hashAlg
, HashAlgorithmMask
)) {
266 DEBUG ((EFI_D_ERROR
, "WARNING: TPM2 Event log has HashAlg unsupported by PCR bank (0x%x)\n", DigestList
->digests
[Index
].hashAlg
));
269 CopyMem (Buffer
, &DigestList
->digests
[Index
].hashAlg
, sizeof(DigestList
->digests
[Index
].hashAlg
));
270 Buffer
= (UINT8
*)Buffer
+ sizeof(DigestList
->digests
[Index
].hashAlg
);
271 DigestSize
= GetHashSizeFromAlgo (DigestList
->digests
[Index
].hashAlg
);
272 CopyMem (Buffer
, &DigestList
->digests
[Index
].digest
, DigestSize
);
273 Buffer
= (UINT8
*)Buffer
+ DigestSize
;
276 WriteUnaligned32 (DigestListCountPtr
, DigestListCount
);
282 Get TPML_DIGEST_VALUES data size.
284 @param[in] DigestList TPML_DIGEST_VALUES data.
286 @return TPML_DIGEST_VALUES data size.
291 IN TPML_DIGEST_VALUES
*DigestList
298 TotalSize
= sizeof(DigestList
->count
);
299 for (Index
= 0; Index
< DigestList
->count
; Index
++) {
300 DigestSize
= GetHashSizeFromAlgo (DigestList
->digests
[Index
].hashAlg
);
301 TotalSize
+= sizeof(DigestList
->digests
[Index
].hashAlg
) + DigestSize
;
308 This function get digest from digest list.
310 @param[in] HashAlg Digest algorithm
311 @param[in] DigestList Digest list
312 @param[out] Digest Digest
314 @retval EFI_SUCCESS Digest is found and returned.
315 @retval EFI_NOT_FOUND Digest is not found.
319 GetDigestFromDigestList (
320 IN TPMI_ALG_HASH HashAlg
,
321 IN TPML_DIGEST_VALUES
*DigestList
,
328 DigestSize
= GetHashSizeFromAlgo (HashAlg
);
329 for (Index
= 0; Index
< DigestList
->count
; Index
++) {
330 if (DigestList
->digests
[Index
].hashAlg
== HashAlg
) {
333 &DigestList
->digests
[Index
].digest
,
340 return EFI_NOT_FOUND
;