SecurityPkg Tpm2DeviceLibDTpm: Update enum type name to match the one in lib
[mirror_edk2.git] / SecurityPkg / Library / HashInstanceLibSha256 / HashInstanceLibSha256.c
CommitLineData
c1d93242 1/** @file\r
07309c3d 2 This library is BaseCrypto SHA256 hash instance.\r
c1d93242
JY
3 It can be registered to BaseCrypto router, to serve as hash engine.\r
4\r
07309c3d 5Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>\r
c1d93242
JY
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <PiPei.h>\r
17#include <Library/BaseLib.h>\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/Tpm2CommandLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/BaseCryptLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
23#include <Library/HashLib.h>\r
24\r
25/**\r
26 The function set SHA256 to digest list.\r
27\r
28 @param DigestList digest list\r
29 @param Sha256Digest SHA256 digest\r
30**/\r
31VOID\r
32Tpm2SetSha256ToDigestList (\r
33 IN TPML_DIGEST_VALUES *DigestList,\r
34 IN UINT8 *Sha256Digest\r
35 )\r
36{\r
37 DigestList->count = 1;\r
38 DigestList->digests[0].hashAlg = TPM_ALG_SHA256;\r
39 CopyMem (\r
40 DigestList->digests[0].digest.sha256,\r
41 Sha256Digest,\r
42 SHA256_DIGEST_SIZE\r
43 );\r
44}\r
45\r
46/**\r
47 Start hash sequence.\r
48\r
49 @param HashHandle Hash handle.\r
50\r
51 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.\r
52 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.\r
53**/\r
54EFI_STATUS\r
55EFIAPI\r
56Sha256HashInit (\r
57 OUT HASH_HANDLE *HashHandle\r
58 )\r
59{\r
60 VOID *Sha256Ctx;\r
61 UINTN CtxSize;\r
62\r
63 CtxSize = Sha256GetContextSize ();\r
64 Sha256Ctx = AllocatePool (CtxSize);\r
65 ASSERT (Sha256Ctx != NULL);\r
66\r
67 Sha256Init (Sha256Ctx);\r
68\r
69 *HashHandle = (HASH_HANDLE)Sha256Ctx;\r
70\r
71 return EFI_SUCCESS;\r
72}\r
73\r
74/**\r
75 Update hash sequence data.\r
76\r
77 @param HashHandle Hash handle.\r
78 @param DataToHash Data to be hashed.\r
79 @param DataToHashLen Data size.\r
80\r
81 @retval EFI_SUCCESS Hash sequence updated.\r
82**/\r
83EFI_STATUS\r
84EFIAPI\r
85Sha256HashUpdate (\r
86 IN HASH_HANDLE HashHandle,\r
87 IN VOID *DataToHash,\r
88 IN UINTN DataToHashLen\r
89 )\r
90{\r
91 VOID *Sha256Ctx;\r
92\r
93 Sha256Ctx = (VOID *)HashHandle;\r
94 Sha256Update (Sha256Ctx, DataToHash, DataToHashLen);\r
95\r
96 return EFI_SUCCESS;\r
97}\r
98\r
99/**\r
100 Complete hash sequence complete.\r
101\r
102 @param HashHandle Hash handle.\r
103 @param DigestList Digest list.\r
104\r
105 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.\r
106**/\r
107EFI_STATUS\r
108EFIAPI\r
109Sha256HashFinal (\r
110 IN HASH_HANDLE HashHandle,\r
111 OUT TPML_DIGEST_VALUES *DigestList\r
112 )\r
113{\r
114 UINT8 Digest[SHA256_DIGEST_SIZE];\r
115 VOID *Sha256Ctx;\r
116\r
117 Sha256Ctx = (VOID *)HashHandle;\r
118 Sha256Final (Sha256Ctx, Digest);\r
119\r
120 FreePool (Sha256Ctx);\r
121 \r
122 Tpm2SetSha256ToDigestList (DigestList, Digest);\r
123\r
124 return EFI_SUCCESS;\r
125}\r
126\r
127HASH_INTERFACE mSha256InternalHashInstance = {\r
128 HASH_ALGORITHM_SHA256_GUID,\r
129 Sha256HashInit,\r
130 Sha256HashUpdate,\r
131 Sha256HashFinal,\r
132};\r
133\r
134/**\r
135 The function register SHA256 instance.\r
136 \r
137 @retval EFI_SUCCESS SHA256 instance is registered, or system dose not surpport registr SHA256 instance\r
138**/\r
139EFI_STATUS\r
140EFIAPI\r
141HashInstanceLibSha256Constructor (\r
142 VOID\r
143 )\r
144{\r
145 EFI_STATUS Status;\r
146\r
147 Status = RegisterHashInterfaceLib (&mSha256InternalHashInstance);\r
148 if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) {\r
149 //\r
150 // Unsupported means platform policy does not need this instance enabled.\r
151 //\r
152 return EFI_SUCCESS;\r
153 }\r
154 return Status;\r
155}