1 | /** @file
|
---|
2 | Unified Hash API Implementation
|
---|
3 |
|
---|
4 | This file implements the Unified Hash API.
|
---|
5 |
|
---|
6 | This API, when called, will calculate the Hash using the
|
---|
7 | hashing algorithm specified by PcdHashApiLibPolicy.
|
---|
8 |
|
---|
9 | Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
|
---|
10 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
11 |
|
---|
12 | **/
|
---|
13 |
|
---|
14 | #include <Base.h>
|
---|
15 | #include <IndustryStandard/Tpm20.h>
|
---|
16 | #include <Library/BaseLib.h>
|
---|
17 | #include <Library/BaseMemoryLib.h>
|
---|
18 | #include <Library/MemoryAllocationLib.h>
|
---|
19 | #include <Library/BaseCryptLib.h>
|
---|
20 | #include <Library/DebugLib.h>
|
---|
21 | #include <Library/PcdLib.h>
|
---|
22 | #include <Library/HashApiLib.h>
|
---|
23 |
|
---|
24 | /**
|
---|
25 | Retrieves the size, in bytes, of the context buffer required for hash operations.
|
---|
26 |
|
---|
27 | @return The size, in bytes, of the context buffer required for hash operations.
|
---|
28 | **/
|
---|
29 | UINTN
|
---|
30 | EFIAPI
|
---|
31 | HashApiGetContextSize (
|
---|
32 | VOID
|
---|
33 | )
|
---|
34 | {
|
---|
35 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
36 | case HASH_ALG_SHA1:
|
---|
37 | return Sha1GetContextSize ();
|
---|
38 | break;
|
---|
39 |
|
---|
40 | case HASH_ALG_SHA256:
|
---|
41 | return Sha256GetContextSize ();
|
---|
42 | break;
|
---|
43 |
|
---|
44 | case HASH_ALG_SHA384:
|
---|
45 | return Sha384GetContextSize ();
|
---|
46 | break;
|
---|
47 |
|
---|
48 | case HASH_ALG_SHA512:
|
---|
49 | return Sha512GetContextSize ();
|
---|
50 | break;
|
---|
51 |
|
---|
52 | case HASH_ALG_SM3_256:
|
---|
53 | return Sm3GetContextSize ();
|
---|
54 | break;
|
---|
55 |
|
---|
56 | default:
|
---|
57 | ASSERT (FALSE);
|
---|
58 | return 0;
|
---|
59 | break;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | Init hash sequence.
|
---|
65 |
|
---|
66 | @param[out] HashContext Hash context.
|
---|
67 |
|
---|
68 | @retval TRUE Hash start and HashHandle returned.
|
---|
69 | @retval FALSE Hash Init unsuccessful.
|
---|
70 | **/
|
---|
71 | BOOLEAN
|
---|
72 | EFIAPI
|
---|
73 | HashApiInit (
|
---|
74 | OUT HASH_API_CONTEXT HashContext
|
---|
75 | )
|
---|
76 | {
|
---|
77 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
78 | case HASH_ALG_SHA1:
|
---|
79 | return Sha1Init (HashContext);
|
---|
80 | break;
|
---|
81 |
|
---|
82 | case HASH_ALG_SHA256:
|
---|
83 | return Sha256Init (HashContext);
|
---|
84 | break;
|
---|
85 |
|
---|
86 | case HASH_ALG_SHA384:
|
---|
87 | return Sha384Init (HashContext);
|
---|
88 | break;
|
---|
89 |
|
---|
90 | case HASH_ALG_SHA512:
|
---|
91 | return Sha512Init (HashContext);
|
---|
92 | break;
|
---|
93 |
|
---|
94 | case HASH_ALG_SM3_256:
|
---|
95 | return Sm3Init (HashContext);
|
---|
96 | break;
|
---|
97 |
|
---|
98 | default:
|
---|
99 | ASSERT (FALSE);
|
---|
100 | return FALSE;
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | Makes a copy of an existing hash context.
|
---|
107 |
|
---|
108 | @param[in] HashContext Hash context.
|
---|
109 | @param[out] NewHashContext New copy of hash context.
|
---|
110 |
|
---|
111 | @retval TRUE Hash context copy succeeded.
|
---|
112 | @retval FALSE Hash context copy failed.
|
---|
113 | **/
|
---|
114 | BOOLEAN
|
---|
115 | EFIAPI
|
---|
116 | HashApiDuplicate (
|
---|
117 | IN HASH_API_CONTEXT HashContext,
|
---|
118 | OUT HASH_API_CONTEXT NewHashContext
|
---|
119 | )
|
---|
120 | {
|
---|
121 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
122 | case HASH_ALG_SHA1:
|
---|
123 | return Sha1Duplicate (HashContext, NewHashContext);
|
---|
124 | break;
|
---|
125 |
|
---|
126 | case HASH_ALG_SHA256:
|
---|
127 | return Sha256Duplicate (HashContext, NewHashContext);
|
---|
128 | break;
|
---|
129 |
|
---|
130 | case HASH_ALG_SHA384:
|
---|
131 | return Sha384Duplicate (HashContext, NewHashContext);
|
---|
132 | break;
|
---|
133 |
|
---|
134 | case HASH_ALG_SHA512:
|
---|
135 | return Sha512Duplicate (HashContext, NewHashContext);
|
---|
136 | break;
|
---|
137 |
|
---|
138 | case HASH_ALG_SM3_256:
|
---|
139 | return Sm3Duplicate (HashContext, NewHashContext);
|
---|
140 | break;
|
---|
141 |
|
---|
142 | default:
|
---|
143 | ASSERT (FALSE);
|
---|
144 | return FALSE;
|
---|
145 | break;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | Update hash data.
|
---|
151 |
|
---|
152 | @param[in] HashContext Hash context.
|
---|
153 | @param[in] DataToHash Data to be hashed.
|
---|
154 | @param[in] DataToHashLen Data size.
|
---|
155 |
|
---|
156 | @retval TRUE Hash updated.
|
---|
157 | @retval FALSE Hash updated unsuccessful.
|
---|
158 | **/
|
---|
159 | BOOLEAN
|
---|
160 | EFIAPI
|
---|
161 | HashApiUpdate (
|
---|
162 | IN HASH_API_CONTEXT HashContext,
|
---|
163 | IN VOID *DataToHash,
|
---|
164 | IN UINTN DataToHashLen
|
---|
165 | )
|
---|
166 | {
|
---|
167 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
168 | case HASH_ALG_SHA1:
|
---|
169 | return Sha1Update (HashContext, DataToHash, DataToHashLen);
|
---|
170 | break;
|
---|
171 |
|
---|
172 | case HASH_ALG_SHA256:
|
---|
173 | return Sha256Update (HashContext, DataToHash, DataToHashLen);
|
---|
174 | break;
|
---|
175 |
|
---|
176 | case HASH_ALG_SHA384:
|
---|
177 | return Sha384Update (HashContext, DataToHash, DataToHashLen);
|
---|
178 | break;
|
---|
179 |
|
---|
180 | case HASH_ALG_SHA512:
|
---|
181 | return Sha512Update (HashContext, DataToHash, DataToHashLen);
|
---|
182 | break;
|
---|
183 |
|
---|
184 | case HASH_ALG_SM3_256:
|
---|
185 | return Sm3Update (HashContext, DataToHash, DataToHashLen);
|
---|
186 | break;
|
---|
187 |
|
---|
188 | default:
|
---|
189 | ASSERT (FALSE);
|
---|
190 | return FALSE;
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | Hash complete.
|
---|
197 |
|
---|
198 | @param[in] HashContext Hash context.
|
---|
199 | @param[out] Digest Hash Digest.
|
---|
200 |
|
---|
201 | @retval TRUE Hash complete and Digest is returned.
|
---|
202 | @retval FALSE Hash complete unsuccessful.
|
---|
203 | **/
|
---|
204 | BOOLEAN
|
---|
205 | EFIAPI
|
---|
206 | HashApiFinal (
|
---|
207 | IN HASH_API_CONTEXT HashContext,
|
---|
208 | OUT UINT8 *Digest
|
---|
209 | )
|
---|
210 | {
|
---|
211 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
212 | case HASH_ALG_SHA1:
|
---|
213 | return Sha1Final (HashContext, Digest);
|
---|
214 | break;
|
---|
215 |
|
---|
216 | case HASH_ALG_SHA256:
|
---|
217 | return Sha256Final (HashContext, Digest);
|
---|
218 | break;
|
---|
219 |
|
---|
220 | case HASH_ALG_SHA384:
|
---|
221 | return Sha384Final (HashContext, Digest);
|
---|
222 | break;
|
---|
223 |
|
---|
224 | case HASH_ALG_SHA512:
|
---|
225 | return Sha512Final (HashContext, Digest);
|
---|
226 | break;
|
---|
227 |
|
---|
228 | case HASH_ALG_SM3_256:
|
---|
229 | return Sm3Final (HashContext, Digest);
|
---|
230 | break;
|
---|
231 |
|
---|
232 | default:
|
---|
233 | ASSERT (FALSE);
|
---|
234 | return FALSE;
|
---|
235 | break;
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | Computes hash message digest of a input data buffer.
|
---|
241 |
|
---|
242 | @param[in] DataToHash Data to be hashed.
|
---|
243 | @param[in] DataToHashLen Data size.
|
---|
244 | @param[out] Digest Hash Digest.
|
---|
245 |
|
---|
246 | @retval TRUE Hash digest computation succeeded.
|
---|
247 | @retval FALSE Hash digest computation failed.
|
---|
248 | **/
|
---|
249 | BOOLEAN
|
---|
250 | EFIAPI
|
---|
251 | HashApiHashAll (
|
---|
252 | IN CONST VOID *DataToHash,
|
---|
253 | IN UINTN DataToHashLen,
|
---|
254 | OUT UINT8 *Digest
|
---|
255 | )
|
---|
256 | {
|
---|
257 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
258 | case HASH_ALG_SHA1:
|
---|
259 | return Sha1HashAll (DataToHash, DataToHashLen, Digest);
|
---|
260 | break;
|
---|
261 |
|
---|
262 | case HASH_ALG_SHA256:
|
---|
263 | return Sha256HashAll (DataToHash, DataToHashLen, Digest);
|
---|
264 | break;
|
---|
265 |
|
---|
266 | case HASH_ALG_SHA384:
|
---|
267 | return Sha384HashAll (DataToHash, DataToHashLen, Digest);
|
---|
268 | break;
|
---|
269 |
|
---|
270 | case HASH_ALG_SHA512:
|
---|
271 | return Sha512HashAll (DataToHash, DataToHashLen, Digest);
|
---|
272 | break;
|
---|
273 |
|
---|
274 | case HASH_ALG_SM3_256:
|
---|
275 | return Sm3HashAll (DataToHash, DataToHashLen, Digest);
|
---|
276 | break;
|
---|
277 |
|
---|
278 | default:
|
---|
279 | ASSERT (FALSE);
|
---|
280 | return FALSE;
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | }
|
---|