1c1d932429ef9700a2da64452546be14e92468b07jyao/** @file
2c1d932429ef9700a2da64452546be14e92468b07jyao  Ihis library uses TPM2 device to calculation hash.
3c1d932429ef9700a2da64452546be14e92468b07jyao
41abfa4ce4835639c66ae82cc0d72cffcf3f28b6bYao, JiewenCopyright (c) 2013 - 2015, Intel Corporation. All rights reserved. <BR>
56aaac3838e5b408b67178d350d3fcc41e4bf3162Samer El-Haj-Mahmoud(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6c1d932429ef9700a2da64452546be14e92468b07jyaoThis program and the accompanying materials
7c1d932429ef9700a2da64452546be14e92468b07jyaoare licensed and made available under the terms and conditions of the BSD License
8c1d932429ef9700a2da64452546be14e92468b07jyaowhich accompanies this distribution.  The full text of the license may be found at
9c1d932429ef9700a2da64452546be14e92468b07jyaohttp://opensource.org/licenses/bsd-license.php
10c1d932429ef9700a2da64452546be14e92468b07jyao
11c1d932429ef9700a2da64452546be14e92468b07jyaoTHE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12c1d932429ef9700a2da64452546be14e92468b07jyaoWITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13c1d932429ef9700a2da64452546be14e92468b07jyao
14c1d932429ef9700a2da64452546be14e92468b07jyao**/
15c1d932429ef9700a2da64452546be14e92468b07jyao
16c1d932429ef9700a2da64452546be14e92468b07jyao#include <PiPei.h>
17c1d932429ef9700a2da64452546be14e92468b07jyao#include <Library/BaseLib.h>
18c1d932429ef9700a2da64452546be14e92468b07jyao#include <Library/BaseMemoryLib.h>
19c1d932429ef9700a2da64452546be14e92468b07jyao#include <Library/Tpm2CommandLib.h>
20c1d932429ef9700a2da64452546be14e92468b07jyao#include <Library/DebugLib.h>
21c1d932429ef9700a2da64452546be14e92468b07jyao#include <Library/MemoryAllocationLib.h>
22c1d932429ef9700a2da64452546be14e92468b07jyao#include <Library/HashLib.h>
23c1d932429ef9700a2da64452546be14e92468b07jyao#include <Library/PcdLib.h>
24c1d932429ef9700a2da64452546be14e92468b07jyao
25c1d932429ef9700a2da64452546be14e92468b07jyaotypedef struct {
26c1d932429ef9700a2da64452546be14e92468b07jyao  TPM_ALG_ID AlgoId;
27c1d932429ef9700a2da64452546be14e92468b07jyao  UINT32     Mask;
28c1d932429ef9700a2da64452546be14e92468b07jyao} TPM2_HASH_MASK;
29c1d932429ef9700a2da64452546be14e92468b07jyao
30c1d932429ef9700a2da64452546be14e92468b07jyaoTPM2_HASH_MASK mTpm2HashMask[] = {
311abfa4ce4835639c66ae82cc0d72cffcf3f28b6bYao, Jiewen  {TPM_ALG_SHA1,         HASH_ALG_SHA1},
321abfa4ce4835639c66ae82cc0d72cffcf3f28b6bYao, Jiewen  {TPM_ALG_SHA256,       HASH_ALG_SHA256},
331abfa4ce4835639c66ae82cc0d72cffcf3f28b6bYao, Jiewen  {TPM_ALG_SHA384,       HASH_ALG_SHA384},
341abfa4ce4835639c66ae82cc0d72cffcf3f28b6bYao, Jiewen  {TPM_ALG_SHA512,       HASH_ALG_SHA512},
35c1d932429ef9700a2da64452546be14e92468b07jyao};
36c1d932429ef9700a2da64452546be14e92468b07jyao
37c1d932429ef9700a2da64452546be14e92468b07jyao/**
38c1d932429ef9700a2da64452546be14e92468b07jyao  The function get algorith from hash mask info.
39c1d932429ef9700a2da64452546be14e92468b07jyao
40c1d932429ef9700a2da64452546be14e92468b07jyao  @return Hash algorithm
41c1d932429ef9700a2da64452546be14e92468b07jyao**/
42c1d932429ef9700a2da64452546be14e92468b07jyaoTPM_ALG_ID
43c1d932429ef9700a2da64452546be14e92468b07jyaoTpm2GetAlgoFromHashMask (
44c1d932429ef9700a2da64452546be14e92468b07jyao  VOID
45c1d932429ef9700a2da64452546be14e92468b07jyao  )
46c1d932429ef9700a2da64452546be14e92468b07jyao{
47c1d932429ef9700a2da64452546be14e92468b07jyao  UINT32 HashMask;
48c1d932429ef9700a2da64452546be14e92468b07jyao  UINTN  Index;
49c1d932429ef9700a2da64452546be14e92468b07jyao
50c1d932429ef9700a2da64452546be14e92468b07jyao  HashMask = PcdGet32 (PcdTpm2HashMask);
51c1d932429ef9700a2da64452546be14e92468b07jyao  for (Index = 0; Index < sizeof(mTpm2HashMask)/sizeof(mTpm2HashMask[0]); Index++) {
52c1d932429ef9700a2da64452546be14e92468b07jyao    if (mTpm2HashMask[Index].Mask == HashMask) {
53c1d932429ef9700a2da64452546be14e92468b07jyao      return mTpm2HashMask[Index].AlgoId;
54c1d932429ef9700a2da64452546be14e92468b07jyao    }
55c1d932429ef9700a2da64452546be14e92468b07jyao  }
56c1d932429ef9700a2da64452546be14e92468b07jyao
57c1d932429ef9700a2da64452546be14e92468b07jyao  return TPM_ALG_NULL;
58c1d932429ef9700a2da64452546be14e92468b07jyao}
59c1d932429ef9700a2da64452546be14e92468b07jyao
60c1d932429ef9700a2da64452546be14e92468b07jyao/**
61c1d932429ef9700a2da64452546be14e92468b07jyao  Start hash sequence.
62c1d932429ef9700a2da64452546be14e92468b07jyao
63c1d932429ef9700a2da64452546be14e92468b07jyao  @param HashHandle Hash handle.
64c1d932429ef9700a2da64452546be14e92468b07jyao
65c1d932429ef9700a2da64452546be14e92468b07jyao  @retval EFI_SUCCESS          Hash sequence start and HandleHandle returned.
66c1d932429ef9700a2da64452546be14e92468b07jyao  @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.
67c1d932429ef9700a2da64452546be14e92468b07jyao**/
68c1d932429ef9700a2da64452546be14e92468b07jyaoEFI_STATUS
69c1d932429ef9700a2da64452546be14e92468b07jyaoEFIAPI
70c1d932429ef9700a2da64452546be14e92468b07jyaoHashStart (
71c1d932429ef9700a2da64452546be14e92468b07jyao  OUT HASH_HANDLE    *HashHandle
72c1d932429ef9700a2da64452546be14e92468b07jyao  )
73c1d932429ef9700a2da64452546be14e92468b07jyao{
74c1d932429ef9700a2da64452546be14e92468b07jyao  TPMI_DH_OBJECT        SequenceHandle;
75c1d932429ef9700a2da64452546be14e92468b07jyao  EFI_STATUS            Status;
76c1d932429ef9700a2da64452546be14e92468b07jyao  TPM_ALG_ID            AlgoId;
77c1d932429ef9700a2da64452546be14e92468b07jyao
78c1d932429ef9700a2da64452546be14e92468b07jyao  AlgoId = Tpm2GetAlgoFromHashMask ();
79c1d932429ef9700a2da64452546be14e92468b07jyao
80c1d932429ef9700a2da64452546be14e92468b07jyao  Status = Tpm2HashSequenceStart (AlgoId, &SequenceHandle);
81c1d932429ef9700a2da64452546be14e92468b07jyao  if (!EFI_ERROR (Status)) {
82c1d932429ef9700a2da64452546be14e92468b07jyao    *HashHandle = (HASH_HANDLE)SequenceHandle;
83c1d932429ef9700a2da64452546be14e92468b07jyao  }
84c1d932429ef9700a2da64452546be14e92468b07jyao  return Status;
85c1d932429ef9700a2da64452546be14e92468b07jyao}
86c1d932429ef9700a2da64452546be14e92468b07jyao
87c1d932429ef9700a2da64452546be14e92468b07jyao/**
88c1d932429ef9700a2da64452546be14e92468b07jyao  Update hash sequence data.
89c1d932429ef9700a2da64452546be14e92468b07jyao
90c1d932429ef9700a2da64452546be14e92468b07jyao  @param HashHandle    Hash handle.
91c1d932429ef9700a2da64452546be14e92468b07jyao  @param DataToHash    Data to be hashed.
92c1d932429ef9700a2da64452546be14e92468b07jyao  @param DataToHashLen Data size.
93c1d932429ef9700a2da64452546be14e92468b07jyao
94c1d932429ef9700a2da64452546be14e92468b07jyao  @retval EFI_SUCCESS     Hash sequence updated.
95c1d932429ef9700a2da64452546be14e92468b07jyao**/
96c1d932429ef9700a2da64452546be14e92468b07jyaoEFI_STATUS
97c1d932429ef9700a2da64452546be14e92468b07jyaoEFIAPI
98c1d932429ef9700a2da64452546be14e92468b07jyaoHashUpdate (
99c1d932429ef9700a2da64452546be14e92468b07jyao  IN HASH_HANDLE    HashHandle,
100c1d932429ef9700a2da64452546be14e92468b07jyao  IN VOID           *DataToHash,
101c1d932429ef9700a2da64452546be14e92468b07jyao  IN UINTN          DataToHashLen
102c1d932429ef9700a2da64452546be14e92468b07jyao  )
103c1d932429ef9700a2da64452546be14e92468b07jyao{
104c1d932429ef9700a2da64452546be14e92468b07jyao  UINT8            *Buffer;
105c1d932429ef9700a2da64452546be14e92468b07jyao  UINT64           HashLen;
106c1d932429ef9700a2da64452546be14e92468b07jyao  TPM2B_MAX_BUFFER HashBuffer;
107c1d932429ef9700a2da64452546be14e92468b07jyao  EFI_STATUS       Status;
108c1d932429ef9700a2da64452546be14e92468b07jyao
109c1d932429ef9700a2da64452546be14e92468b07jyao  Buffer = (UINT8 *)(UINTN)DataToHash;
110c1d932429ef9700a2da64452546be14e92468b07jyao  for (HashLen = DataToHashLen; HashLen > sizeof(HashBuffer.buffer); HashLen -= sizeof(HashBuffer.buffer)) {
111c1d932429ef9700a2da64452546be14e92468b07jyao
112c1d932429ef9700a2da64452546be14e92468b07jyao    HashBuffer.size = sizeof(HashBuffer.buffer);
113c1d932429ef9700a2da64452546be14e92468b07jyao    CopyMem(HashBuffer.buffer, Buffer, sizeof(HashBuffer.buffer));
114c1d932429ef9700a2da64452546be14e92468b07jyao    Buffer += sizeof(HashBuffer.buffer);
115c1d932429ef9700a2da64452546be14e92468b07jyao
116c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2SequenceUpdate((TPMI_DH_OBJECT)HashHandle, &HashBuffer);
117c1d932429ef9700a2da64452546be14e92468b07jyao    if (EFI_ERROR(Status)) {
118c1d932429ef9700a2da64452546be14e92468b07jyao      return EFI_DEVICE_ERROR;
119c1d932429ef9700a2da64452546be14e92468b07jyao    }
120c1d932429ef9700a2da64452546be14e92468b07jyao  }
121c1d932429ef9700a2da64452546be14e92468b07jyao
122c1d932429ef9700a2da64452546be14e92468b07jyao  //
123c1d932429ef9700a2da64452546be14e92468b07jyao  // Last one
124c1d932429ef9700a2da64452546be14e92468b07jyao  //
125c1d932429ef9700a2da64452546be14e92468b07jyao  HashBuffer.size = (UINT16)HashLen;
126c1d932429ef9700a2da64452546be14e92468b07jyao  CopyMem(HashBuffer.buffer, Buffer, (UINTN)HashLen);
127c1d932429ef9700a2da64452546be14e92468b07jyao  Status = Tpm2SequenceUpdate((TPMI_DH_OBJECT)HashHandle, &HashBuffer);
128c1d932429ef9700a2da64452546be14e92468b07jyao  if (EFI_ERROR(Status)) {
129c1d932429ef9700a2da64452546be14e92468b07jyao    return EFI_DEVICE_ERROR;
130c1d932429ef9700a2da64452546be14e92468b07jyao  }
131c1d932429ef9700a2da64452546be14e92468b07jyao
132c1d932429ef9700a2da64452546be14e92468b07jyao  return EFI_SUCCESS;
133c1d932429ef9700a2da64452546be14e92468b07jyao}
134c1d932429ef9700a2da64452546be14e92468b07jyao
135c1d932429ef9700a2da64452546be14e92468b07jyao/**
136c1d932429ef9700a2da64452546be14e92468b07jyao  Hash sequence complete and extend to PCR.
137c1d932429ef9700a2da64452546be14e92468b07jyao
138c1d932429ef9700a2da64452546be14e92468b07jyao  @param HashHandle    Hash handle.
139c1d932429ef9700a2da64452546be14e92468b07jyao  @param PcrIndex      PCR to be extended.
140c1d932429ef9700a2da64452546be14e92468b07jyao  @param DataToHash    Data to be hashed.
141c1d932429ef9700a2da64452546be14e92468b07jyao  @param DataToHashLen Data size.
142c1d932429ef9700a2da64452546be14e92468b07jyao  @param DigestList    Digest list.
143c1d932429ef9700a2da64452546be14e92468b07jyao
144c1d932429ef9700a2da64452546be14e92468b07jyao  @retval EFI_SUCCESS     Hash sequence complete and DigestList is returned.
145c1d932429ef9700a2da64452546be14e92468b07jyao**/
146c1d932429ef9700a2da64452546be14e92468b07jyaoEFI_STATUS
147c1d932429ef9700a2da64452546be14e92468b07jyaoEFIAPI
148c1d932429ef9700a2da64452546be14e92468b07jyaoHashCompleteAndExtend (
149c1d932429ef9700a2da64452546be14e92468b07jyao  IN HASH_HANDLE         HashHandle,
150c1d932429ef9700a2da64452546be14e92468b07jyao  IN TPMI_DH_PCR         PcrIndex,
151c1d932429ef9700a2da64452546be14e92468b07jyao  IN VOID                *DataToHash,
152c1d932429ef9700a2da64452546be14e92468b07jyao  IN UINTN               DataToHashLen,
153c1d932429ef9700a2da64452546be14e92468b07jyao  OUT TPML_DIGEST_VALUES *DigestList
154c1d932429ef9700a2da64452546be14e92468b07jyao  )
155c1d932429ef9700a2da64452546be14e92468b07jyao{
156c1d932429ef9700a2da64452546be14e92468b07jyao  UINT8            *Buffer;
157c1d932429ef9700a2da64452546be14e92468b07jyao  UINT64           HashLen;
158c1d932429ef9700a2da64452546be14e92468b07jyao  TPM2B_MAX_BUFFER HashBuffer;
159c1d932429ef9700a2da64452546be14e92468b07jyao  EFI_STATUS       Status;
160c1d932429ef9700a2da64452546be14e92468b07jyao  TPM_ALG_ID       AlgoId;
161c1d932429ef9700a2da64452546be14e92468b07jyao  TPM2B_DIGEST     Result;
162c1d932429ef9700a2da64452546be14e92468b07jyao
163c1d932429ef9700a2da64452546be14e92468b07jyao  AlgoId = Tpm2GetAlgoFromHashMask ();
164c1d932429ef9700a2da64452546be14e92468b07jyao
165c1d932429ef9700a2da64452546be14e92468b07jyao  Buffer = (UINT8 *)(UINTN)DataToHash;
166c1d932429ef9700a2da64452546be14e92468b07jyao  for (HashLen = DataToHashLen; HashLen > sizeof(HashBuffer.buffer); HashLen -= sizeof(HashBuffer.buffer)) {
167c1d932429ef9700a2da64452546be14e92468b07jyao
168c1d932429ef9700a2da64452546be14e92468b07jyao    HashBuffer.size = sizeof(HashBuffer.buffer);
169c1d932429ef9700a2da64452546be14e92468b07jyao    CopyMem(HashBuffer.buffer, Buffer, sizeof(HashBuffer.buffer));
170c1d932429ef9700a2da64452546be14e92468b07jyao    Buffer += sizeof(HashBuffer.buffer);
171c1d932429ef9700a2da64452546be14e92468b07jyao
172c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2SequenceUpdate((TPMI_DH_OBJECT)HashHandle, &HashBuffer);
173c1d932429ef9700a2da64452546be14e92468b07jyao    if (EFI_ERROR(Status)) {
174c1d932429ef9700a2da64452546be14e92468b07jyao      return EFI_DEVICE_ERROR;
175c1d932429ef9700a2da64452546be14e92468b07jyao    }
176c1d932429ef9700a2da64452546be14e92468b07jyao  }
177c1d932429ef9700a2da64452546be14e92468b07jyao
178c1d932429ef9700a2da64452546be14e92468b07jyao  //
179c1d932429ef9700a2da64452546be14e92468b07jyao  // Last one
180c1d932429ef9700a2da64452546be14e92468b07jyao  //
181c1d932429ef9700a2da64452546be14e92468b07jyao  HashBuffer.size = (UINT16)HashLen;
182c1d932429ef9700a2da64452546be14e92468b07jyao  CopyMem(HashBuffer.buffer, Buffer, (UINTN)HashLen);
183c1d932429ef9700a2da64452546be14e92468b07jyao
184c1d932429ef9700a2da64452546be14e92468b07jyao  ZeroMem(DigestList, sizeof(*DigestList));
185c1d932429ef9700a2da64452546be14e92468b07jyao  DigestList->count = HASH_COUNT;
186c1d932429ef9700a2da64452546be14e92468b07jyao
187c1d932429ef9700a2da64452546be14e92468b07jyao  if (AlgoId == TPM_ALG_NULL) {
188c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2EventSequenceComplete (
189c1d932429ef9700a2da64452546be14e92468b07jyao               PcrIndex,
190c1d932429ef9700a2da64452546be14e92468b07jyao               (TPMI_DH_OBJECT)HashHandle,
191c1d932429ef9700a2da64452546be14e92468b07jyao               &HashBuffer,
192c1d932429ef9700a2da64452546be14e92468b07jyao               DigestList
193c1d932429ef9700a2da64452546be14e92468b07jyao               );
194c1d932429ef9700a2da64452546be14e92468b07jyao  } else {
195c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2SequenceComplete (
196c1d932429ef9700a2da64452546be14e92468b07jyao               (TPMI_DH_OBJECT)HashHandle,
197c1d932429ef9700a2da64452546be14e92468b07jyao               &HashBuffer,
198c1d932429ef9700a2da64452546be14e92468b07jyao               &Result
199c1d932429ef9700a2da64452546be14e92468b07jyao               );
200c1d932429ef9700a2da64452546be14e92468b07jyao    if (EFI_ERROR(Status)) {
201c1d932429ef9700a2da64452546be14e92468b07jyao      return EFI_DEVICE_ERROR;
202c1d932429ef9700a2da64452546be14e92468b07jyao    }
203c1d932429ef9700a2da64452546be14e92468b07jyao
204c1d932429ef9700a2da64452546be14e92468b07jyao    DigestList->count = 1;
205c1d932429ef9700a2da64452546be14e92468b07jyao    DigestList->digests[0].hashAlg = AlgoId;
206c1d932429ef9700a2da64452546be14e92468b07jyao    CopyMem (&DigestList->digests[0].digest, Result.buffer, Result.size);
207c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2PcrExtend (
208c1d932429ef9700a2da64452546be14e92468b07jyao               PcrIndex,
209c1d932429ef9700a2da64452546be14e92468b07jyao               DigestList
210c1d932429ef9700a2da64452546be14e92468b07jyao               );
211c1d932429ef9700a2da64452546be14e92468b07jyao  }
212c1d932429ef9700a2da64452546be14e92468b07jyao  if (EFI_ERROR(Status)) {
213c1d932429ef9700a2da64452546be14e92468b07jyao    return EFI_DEVICE_ERROR;
214c1d932429ef9700a2da64452546be14e92468b07jyao  }
215c1d932429ef9700a2da64452546be14e92468b07jyao  return EFI_SUCCESS;
216c1d932429ef9700a2da64452546be14e92468b07jyao}
217c1d932429ef9700a2da64452546be14e92468b07jyao
218c1d932429ef9700a2da64452546be14e92468b07jyao/**
219c1d932429ef9700a2da64452546be14e92468b07jyao  Hash data and extend to PCR.
220c1d932429ef9700a2da64452546be14e92468b07jyao
221c1d932429ef9700a2da64452546be14e92468b07jyao  @param PcrIndex      PCR to be extended.
222c1d932429ef9700a2da64452546be14e92468b07jyao  @param DataToHash    Data to be hashed.
223c1d932429ef9700a2da64452546be14e92468b07jyao  @param DataToHashLen Data size.
224c1d932429ef9700a2da64452546be14e92468b07jyao  @param DigestList    Digest list.
225c1d932429ef9700a2da64452546be14e92468b07jyao
226c1d932429ef9700a2da64452546be14e92468b07jyao  @retval EFI_SUCCESS     Hash data and DigestList is returned.
227c1d932429ef9700a2da64452546be14e92468b07jyao**/
228c1d932429ef9700a2da64452546be14e92468b07jyaoEFI_STATUS
229c1d932429ef9700a2da64452546be14e92468b07jyaoEFIAPI
230c1d932429ef9700a2da64452546be14e92468b07jyaoHashAndExtend (
231c1d932429ef9700a2da64452546be14e92468b07jyao  IN TPMI_DH_PCR                    PcrIndex,
232c1d932429ef9700a2da64452546be14e92468b07jyao  IN VOID                           *DataToHash,
233c1d932429ef9700a2da64452546be14e92468b07jyao  IN UINTN                          DataToHashLen,
234c1d932429ef9700a2da64452546be14e92468b07jyao  OUT TPML_DIGEST_VALUES            *DigestList
235c1d932429ef9700a2da64452546be14e92468b07jyao  )
236c1d932429ef9700a2da64452546be14e92468b07jyao{
237c1d932429ef9700a2da64452546be14e92468b07jyao  EFI_STATUS         Status;
238c1d932429ef9700a2da64452546be14e92468b07jyao  UINT8              *Buffer;
239c1d932429ef9700a2da64452546be14e92468b07jyao  UINT64             HashLen;
240c1d932429ef9700a2da64452546be14e92468b07jyao  TPMI_DH_OBJECT     SequenceHandle;
241c1d932429ef9700a2da64452546be14e92468b07jyao  TPM2B_MAX_BUFFER   HashBuffer;
242c1d932429ef9700a2da64452546be14e92468b07jyao  TPM_ALG_ID         AlgoId;
243c1d932429ef9700a2da64452546be14e92468b07jyao  TPM2B_EVENT        EventData;
244c1d932429ef9700a2da64452546be14e92468b07jyao  TPM2B_DIGEST       Result;
245c1d932429ef9700a2da64452546be14e92468b07jyao
2466aaac3838e5b408b67178d350d3fcc41e4bf3162Samer El-Haj-Mahmoud  DEBUG((EFI_D_VERBOSE, "\n HashAndExtend Entry \n"));
247c1d932429ef9700a2da64452546be14e92468b07jyao
248c1d932429ef9700a2da64452546be14e92468b07jyao  SequenceHandle = 0xFFFFFFFF; // Know bad value
249c1d932429ef9700a2da64452546be14e92468b07jyao
250c1d932429ef9700a2da64452546be14e92468b07jyao  AlgoId = Tpm2GetAlgoFromHashMask ();
251c1d932429ef9700a2da64452546be14e92468b07jyao
252c1d932429ef9700a2da64452546be14e92468b07jyao  if ((AlgoId == TPM_ALG_NULL) && (DataToHashLen <= sizeof(EventData.buffer))) {
253c1d932429ef9700a2da64452546be14e92468b07jyao    EventData.size = (UINT16)DataToHashLen;
254c1d932429ef9700a2da64452546be14e92468b07jyao    CopyMem (EventData.buffer, DataToHash, DataToHashLen);
255c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2PcrEvent (PcrIndex, &EventData, DigestList);
256c1d932429ef9700a2da64452546be14e92468b07jyao    if (EFI_ERROR(Status)) {
257c1d932429ef9700a2da64452546be14e92468b07jyao      return EFI_DEVICE_ERROR;
258c1d932429ef9700a2da64452546be14e92468b07jyao    }
259c1d932429ef9700a2da64452546be14e92468b07jyao    return EFI_SUCCESS;
260c1d932429ef9700a2da64452546be14e92468b07jyao  }
261c1d932429ef9700a2da64452546be14e92468b07jyao
262c1d932429ef9700a2da64452546be14e92468b07jyao  Status = Tpm2HashSequenceStart(AlgoId, &SequenceHandle);
263c1d932429ef9700a2da64452546be14e92468b07jyao  if (EFI_ERROR(Status)) {
264c1d932429ef9700a2da64452546be14e92468b07jyao    return EFI_DEVICE_ERROR;
265c1d932429ef9700a2da64452546be14e92468b07jyao  }
2666aaac3838e5b408b67178d350d3fcc41e4bf3162Samer El-Haj-Mahmoud  DEBUG((EFI_D_VERBOSE, "\n Tpm2HashSequenceStart Success \n"));
267c1d932429ef9700a2da64452546be14e92468b07jyao
268c1d932429ef9700a2da64452546be14e92468b07jyao  Buffer = (UINT8 *)(UINTN)DataToHash;
269c1d932429ef9700a2da64452546be14e92468b07jyao  for (HashLen = DataToHashLen; HashLen > sizeof(HashBuffer.buffer); HashLen -= sizeof(HashBuffer.buffer)) {
270c1d932429ef9700a2da64452546be14e92468b07jyao
271c1d932429ef9700a2da64452546be14e92468b07jyao    HashBuffer.size = sizeof(HashBuffer.buffer);
272c1d932429ef9700a2da64452546be14e92468b07jyao    CopyMem(HashBuffer.buffer, Buffer, sizeof(HashBuffer.buffer));
273c1d932429ef9700a2da64452546be14e92468b07jyao    Buffer += sizeof(HashBuffer.buffer);
274c1d932429ef9700a2da64452546be14e92468b07jyao
275c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2SequenceUpdate(SequenceHandle, &HashBuffer);
276c1d932429ef9700a2da64452546be14e92468b07jyao    if (EFI_ERROR(Status)) {
277c1d932429ef9700a2da64452546be14e92468b07jyao      return EFI_DEVICE_ERROR;
278c1d932429ef9700a2da64452546be14e92468b07jyao    }
279c1d932429ef9700a2da64452546be14e92468b07jyao  }
2806aaac3838e5b408b67178d350d3fcc41e4bf3162Samer El-Haj-Mahmoud  DEBUG((EFI_D_VERBOSE, "\n Tpm2SequenceUpdate Success \n"));
281c1d932429ef9700a2da64452546be14e92468b07jyao
282c1d932429ef9700a2da64452546be14e92468b07jyao  HashBuffer.size = (UINT16)HashLen;
283c1d932429ef9700a2da64452546be14e92468b07jyao  CopyMem(HashBuffer.buffer, Buffer, (UINTN)HashLen);
284c1d932429ef9700a2da64452546be14e92468b07jyao
285c1d932429ef9700a2da64452546be14e92468b07jyao  ZeroMem(DigestList, sizeof(*DigestList));
286c1d932429ef9700a2da64452546be14e92468b07jyao  DigestList->count = HASH_COUNT;
287c1d932429ef9700a2da64452546be14e92468b07jyao
288c1d932429ef9700a2da64452546be14e92468b07jyao  if (AlgoId == TPM_ALG_NULL) {
289c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2EventSequenceComplete (
290c1d932429ef9700a2da64452546be14e92468b07jyao               PcrIndex,
291c1d932429ef9700a2da64452546be14e92468b07jyao               SequenceHandle,
292c1d932429ef9700a2da64452546be14e92468b07jyao               &HashBuffer,
293c1d932429ef9700a2da64452546be14e92468b07jyao               DigestList
294c1d932429ef9700a2da64452546be14e92468b07jyao               );
295c1d932429ef9700a2da64452546be14e92468b07jyao    if (EFI_ERROR(Status)) {
296c1d932429ef9700a2da64452546be14e92468b07jyao      return EFI_DEVICE_ERROR;
297c1d932429ef9700a2da64452546be14e92468b07jyao    }
2986aaac3838e5b408b67178d350d3fcc41e4bf3162Samer El-Haj-Mahmoud    DEBUG((EFI_D_VERBOSE, "\n Tpm2EventSequenceComplete Success \n"));
299c1d932429ef9700a2da64452546be14e92468b07jyao  } else {
300c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2SequenceComplete (
301c1d932429ef9700a2da64452546be14e92468b07jyao               SequenceHandle,
302c1d932429ef9700a2da64452546be14e92468b07jyao               &HashBuffer,
303c1d932429ef9700a2da64452546be14e92468b07jyao               &Result
304c1d932429ef9700a2da64452546be14e92468b07jyao               );
305c1d932429ef9700a2da64452546be14e92468b07jyao    if (EFI_ERROR(Status)) {
306c1d932429ef9700a2da64452546be14e92468b07jyao      return EFI_DEVICE_ERROR;
307c1d932429ef9700a2da64452546be14e92468b07jyao    }
3086aaac3838e5b408b67178d350d3fcc41e4bf3162Samer El-Haj-Mahmoud    DEBUG((EFI_D_VERBOSE, "\n Tpm2SequenceComplete Success \n"));
309c1d932429ef9700a2da64452546be14e92468b07jyao
310c1d932429ef9700a2da64452546be14e92468b07jyao    DigestList->count = 1;
311c1d932429ef9700a2da64452546be14e92468b07jyao    DigestList->digests[0].hashAlg = AlgoId;
312c1d932429ef9700a2da64452546be14e92468b07jyao    CopyMem (&DigestList->digests[0].digest, Result.buffer, Result.size);
313c1d932429ef9700a2da64452546be14e92468b07jyao    Status = Tpm2PcrExtend (
314c1d932429ef9700a2da64452546be14e92468b07jyao               PcrIndex,
315c1d932429ef9700a2da64452546be14e92468b07jyao               DigestList
316c1d932429ef9700a2da64452546be14e92468b07jyao               );
317c1d932429ef9700a2da64452546be14e92468b07jyao    if (EFI_ERROR(Status)) {
318c1d932429ef9700a2da64452546be14e92468b07jyao      return EFI_DEVICE_ERROR;
319c1d932429ef9700a2da64452546be14e92468b07jyao    }
3206aaac3838e5b408b67178d350d3fcc41e4bf3162Samer El-Haj-Mahmoud    DEBUG((EFI_D_VERBOSE, "\n Tpm2PcrExtend Success \n"));
321c1d932429ef9700a2da64452546be14e92468b07jyao  }
322c1d932429ef9700a2da64452546be14e92468b07jyao
323c1d932429ef9700a2da64452546be14e92468b07jyao  return EFI_SUCCESS;
324c1d932429ef9700a2da64452546be14e92468b07jyao}
325c1d932429ef9700a2da64452546be14e92468b07jyao
326c1d932429ef9700a2da64452546be14e92468b07jyao/**
327c1d932429ef9700a2da64452546be14e92468b07jyao  This service register Hash.
328c1d932429ef9700a2da64452546be14e92468b07jyao
329c1d932429ef9700a2da64452546be14e92468b07jyao  @param HashInterface  Hash interface
330c1d932429ef9700a2da64452546be14e92468b07jyao
331c1d932429ef9700a2da64452546be14e92468b07jyao  @retval EFI_SUCCESS          This hash interface is registered successfully.
332c1d932429ef9700a2da64452546be14e92468b07jyao  @retval EFI_UNSUPPORTED      System does not support register this interface.
333c1d932429ef9700a2da64452546be14e92468b07jyao  @retval EFI_ALREADY_STARTED  System already register this interface.
334c1d932429ef9700a2da64452546be14e92468b07jyao**/
335c1d932429ef9700a2da64452546be14e92468b07jyaoEFI_STATUS
336c1d932429ef9700a2da64452546be14e92468b07jyaoEFIAPI
337c1d932429ef9700a2da64452546be14e92468b07jyaoRegisterHashInterfaceLib (
338c1d932429ef9700a2da64452546be14e92468b07jyao  IN HASH_INTERFACE   *HashInterface
339c1d932429ef9700a2da64452546be14e92468b07jyao  )
340c1d932429ef9700a2da64452546be14e92468b07jyao{
341c1d932429ef9700a2da64452546be14e92468b07jyao  return EFI_UNSUPPORTED;
342c1d932429ef9700a2da64452546be14e92468b07jyao}