]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/HashInstanceLibSha512/HashInstanceLibSha512.c
QuarkSocPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / SecurityPkg / Library / HashInstanceLibSha512 / HashInstanceLibSha512.c
CommitLineData
3a347533
ZC
1/** @file\r
2 This library is BaseCrypto SHA512 hash instance.\r
3 It can be registered to BaseCrypto router, to serve as hash engine.\r
4\r
5Copyright (c) 2018, Intel Corporation. All rights reserved. <BR>\r
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/DebugLib.h>\r
20#include <Library/BaseCryptLib.h>\r
21#include <Library/MemoryAllocationLib.h>\r
22#include <Library/HashLib.h>\r
23\r
24/**\r
25 The function set SHA512 to digest list.\r
26\r
27 @param DigestList digest list\r
28 @param Sha512Digest SHA512 digest\r
29**/\r
30VOID\r
31Tpm2SetSha512ToDigestList (\r
32 IN TPML_DIGEST_VALUES *DigestList,\r
33 IN UINT8 *Sha512Digest\r
34 )\r
35{\r
36 DigestList->count = 1;\r
37 DigestList->digests[0].hashAlg = TPM_ALG_SHA512;\r
38 CopyMem (\r
39 DigestList->digests[0].digest.sha512,\r
40 Sha512Digest,\r
41 SHA512_DIGEST_SIZE\r
42 );\r
43}\r
44\r
45/**\r
46 Start hash sequence.\r
47\r
48 @param HashHandle Hash handle.\r
49\r
50 @retval EFI_SUCCESS Hash sequence start and HandleHandle returned.\r
51 @retval EFI_OUT_OF_RESOURCES No enough resource to start hash.\r
52**/\r
53EFI_STATUS\r
54EFIAPI\r
55Sha512HashInit (\r
56 OUT HASH_HANDLE *HashHandle\r
57 )\r
58{\r
59 VOID *Sha512Ctx;\r
60 UINTN CtxSize;\r
61\r
62 CtxSize = Sha512GetContextSize ();\r
63 Sha512Ctx = AllocatePool (CtxSize);\r
64 ASSERT (Sha512Ctx != NULL);\r
65\r
66 Sha512Init (Sha512Ctx);\r
67\r
68 *HashHandle = (HASH_HANDLE)Sha512Ctx;\r
69\r
70 return EFI_SUCCESS;\r
71}\r
72\r
73/**\r
74 Update hash sequence data.\r
75\r
76 @param HashHandle Hash handle.\r
77 @param DataToHash Data to be hashed.\r
78 @param DataToHashLen Data size.\r
79\r
80 @retval EFI_SUCCESS Hash sequence updated.\r
81**/\r
82EFI_STATUS\r
83EFIAPI\r
84Sha512HashUpdate (\r
85 IN HASH_HANDLE HashHandle,\r
86 IN VOID *DataToHash,\r
87 IN UINTN DataToHashLen\r
88 )\r
89{\r
90 VOID *Sha512Ctx;\r
91\r
92 Sha512Ctx = (VOID *)HashHandle;\r
93 Sha512Update (Sha512Ctx, DataToHash, DataToHashLen);\r
94\r
95 return EFI_SUCCESS;\r
96}\r
97\r
98/**\r
99 Complete hash sequence complete.\r
100\r
101 @param HashHandle Hash handle.\r
102 @param DigestList Digest list.\r
103\r
104 @retval EFI_SUCCESS Hash sequence complete and DigestList is returned.\r
105**/\r
106EFI_STATUS\r
107EFIAPI\r
108Sha512HashFinal (\r
109 IN HASH_HANDLE HashHandle,\r
110 OUT TPML_DIGEST_VALUES *DigestList\r
111 )\r
112{\r
113 UINT8 Digest[SHA512_DIGEST_SIZE];\r
114 VOID *Sha512Ctx;\r
115\r
116 Sha512Ctx = (VOID *)HashHandle;\r
117 Sha512Final (Sha512Ctx, Digest);\r
118\r
119 FreePool (Sha512Ctx);\r
120\r
121 Tpm2SetSha512ToDigestList (DigestList, Digest);\r
122\r
123 return EFI_SUCCESS;\r
124}\r
125\r
126HASH_INTERFACE mSha512InternalHashInstance = {\r
127 HASH_ALGORITHM_SHA512_GUID,\r
128 Sha512HashInit,\r
129 Sha512HashUpdate,\r
130 Sha512HashFinal,\r
131};\r
132\r
133/**\r
134 The function register SHA512 instance.\r
135\r
136 @retval EFI_SUCCESS SHA512 instance is registered, or system dose not surpport registr SHA512 instance\r
137**/\r
138EFI_STATUS\r
139EFIAPI\r
140HashInstanceLibSha512Constructor (\r
141 VOID\r
142 )\r
143{\r
144 EFI_STATUS Status;\r
145\r
146 Status = RegisterHashInterfaceLib (&mSha512InternalHashInstance);\r
147 if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) {\r
148 //\r
149 // Unsupported means platform policy does not need this instance enabled.\r
150 //\r
151 return EFI_SUCCESS;\r
152 }\r
153 return Status;\r
154}\r