]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/HashInstanceLibSha1/HashInstanceLibSha1.c
SecurityPkg: Fix typo 'Ihis' with 'This' in codes
[mirror_edk2.git] / SecurityPkg / Library / HashInstanceLibSha1 / HashInstanceLibSha1.c
CommitLineData
c1d93242 1/** @file\r
07309c3d 2 This library is BaseCrypto SHA1 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 SHA1 to digest list.\r
27\r
28 @param DigestList digest list\r
29 @param Sha1Digest SHA1 digest\r
30**/\r
31VOID\r
32Tpm2SetSha1ToDigestList (\r
33 IN TPML_DIGEST_VALUES *DigestList,\r
34 IN UINT8 *Sha1Digest\r
35 )\r
36{\r
37 DigestList->count = 1;\r
38 DigestList->digests[0].hashAlg = TPM_ALG_SHA1;\r
39 CopyMem (\r
40 DigestList->digests[0].digest.sha1,\r
41 Sha1Digest,\r
42 SHA1_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
56Sha1HashInit (\r
57 OUT HASH_HANDLE *HashHandle\r
58 )\r
59{\r
60 VOID *Sha1Ctx;\r
61 UINTN CtxSize;\r
62\r
63 CtxSize = Sha1GetContextSize ();\r
64 Sha1Ctx = AllocatePool (CtxSize);\r
65 ASSERT (Sha1Ctx != NULL);\r
66\r
67 Sha1Init (Sha1Ctx);\r
68\r
69 *HashHandle = (HASH_HANDLE)Sha1Ctx;\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
85Sha1HashUpdate (\r
86 IN HASH_HANDLE HashHandle,\r
87 IN VOID *DataToHash,\r
88 IN UINTN DataToHashLen\r
89 )\r
90{\r
91 VOID *Sha1Ctx;\r
92\r
93 Sha1Ctx = (VOID *)HashHandle;\r
94 Sha1Update (Sha1Ctx, 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
109Sha1HashFinal (\r
110 IN HASH_HANDLE HashHandle,\r
111 OUT TPML_DIGEST_VALUES *DigestList\r
112 )\r
113{\r
114 UINT8 Digest[SHA1_DIGEST_SIZE];\r
115 VOID *Sha1Ctx;\r
116\r
117 Sha1Ctx = (VOID *)HashHandle;\r
118 Sha1Final (Sha1Ctx, Digest);\r
119\r
120 FreePool (Sha1Ctx);\r
121 \r
122 Tpm2SetSha1ToDigestList (DigestList, Digest);\r
123\r
124 return EFI_SUCCESS;\r
125}\r
126\r
127HASH_INTERFACE mSha1InternalHashInstance = {\r
128 HASH_ALGORITHM_SHA1_GUID,\r
129 Sha1HashInit,\r
130 Sha1HashUpdate,\r
131 Sha1HashFinal,\r
132};\r
133\r
134/**\r
135 The function register SHA1 instance.\r
136 \r
137 @retval EFI_SUCCESS SHA1 instance is registered, or system dose not surpport registr SHA1 instance\r
138**/\r
139EFI_STATUS\r
140EFIAPI\r
141HashInstanceLibSha1Constructor (\r
142 VOID\r
143 )\r
144{\r
145 EFI_STATUS Status;\r
146\r
147 Status = RegisterHashInterfaceLib (&mSha1InternalHashInstance);\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}