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-2022, 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 | #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
|
---|
37 | case HASH_ALG_SHA1:
|
---|
38 | return Sha1GetContextSize ();
|
---|
39 | break;
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | case HASH_ALG_SHA256:
|
---|
43 | return Sha256GetContextSize ();
|
---|
44 | break;
|
---|
45 |
|
---|
46 | case HASH_ALG_SHA384:
|
---|
47 | return Sha384GetContextSize ();
|
---|
48 | break;
|
---|
49 |
|
---|
50 | case HASH_ALG_SHA512:
|
---|
51 | return Sha512GetContextSize ();
|
---|
52 | break;
|
---|
53 |
|
---|
54 | case HASH_ALG_SM3_256:
|
---|
55 | return Sm3GetContextSize ();
|
---|
56 | break;
|
---|
57 |
|
---|
58 | default:
|
---|
59 | ASSERT (FALSE);
|
---|
60 | return 0;
|
---|
61 | break;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | Init hash sequence.
|
---|
67 |
|
---|
68 | @param[out] HashContext Hash context.
|
---|
69 |
|
---|
70 | @retval TRUE Hash start and HashHandle returned.
|
---|
71 | @retval FALSE Hash Init unsuccessful.
|
---|
72 | **/
|
---|
73 | BOOLEAN
|
---|
74 | EFIAPI
|
---|
75 | HashApiInit (
|
---|
76 | OUT HASH_API_CONTEXT HashContext
|
---|
77 | )
|
---|
78 | {
|
---|
79 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
80 | #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
|
---|
81 | case HASH_ALG_SHA1:
|
---|
82 | return Sha1Init (HashContext);
|
---|
83 | break;
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | case HASH_ALG_SHA256:
|
---|
87 | return Sha256Init (HashContext);
|
---|
88 | break;
|
---|
89 |
|
---|
90 | case HASH_ALG_SHA384:
|
---|
91 | return Sha384Init (HashContext);
|
---|
92 | break;
|
---|
93 |
|
---|
94 | case HASH_ALG_SHA512:
|
---|
95 | return Sha512Init (HashContext);
|
---|
96 | break;
|
---|
97 |
|
---|
98 | case HASH_ALG_SM3_256:
|
---|
99 | return Sm3Init (HashContext);
|
---|
100 | break;
|
---|
101 |
|
---|
102 | default:
|
---|
103 | ASSERT (FALSE);
|
---|
104 | return FALSE;
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | Makes a copy of an existing hash context.
|
---|
111 |
|
---|
112 | @param[in] HashContext Hash context.
|
---|
113 | @param[out] NewHashContext New copy of hash context.
|
---|
114 |
|
---|
115 | @retval TRUE Hash context copy succeeded.
|
---|
116 | @retval FALSE Hash context copy failed.
|
---|
117 | **/
|
---|
118 | BOOLEAN
|
---|
119 | EFIAPI
|
---|
120 | HashApiDuplicate (
|
---|
121 | IN HASH_API_CONTEXT HashContext,
|
---|
122 | OUT HASH_API_CONTEXT NewHashContext
|
---|
123 | )
|
---|
124 | {
|
---|
125 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
126 | #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
|
---|
127 | case HASH_ALG_SHA1:
|
---|
128 | return Sha1Duplicate (HashContext, NewHashContext);
|
---|
129 | break;
|
---|
130 | #endif
|
---|
131 |
|
---|
132 | case HASH_ALG_SHA256:
|
---|
133 | return Sha256Duplicate (HashContext, NewHashContext);
|
---|
134 | break;
|
---|
135 |
|
---|
136 | case HASH_ALG_SHA384:
|
---|
137 | return Sha384Duplicate (HashContext, NewHashContext);
|
---|
138 | break;
|
---|
139 |
|
---|
140 | case HASH_ALG_SHA512:
|
---|
141 | return Sha512Duplicate (HashContext, NewHashContext);
|
---|
142 | break;
|
---|
143 |
|
---|
144 | case HASH_ALG_SM3_256:
|
---|
145 | return Sm3Duplicate (HashContext, NewHashContext);
|
---|
146 | break;
|
---|
147 |
|
---|
148 | default:
|
---|
149 | ASSERT (FALSE);
|
---|
150 | return FALSE;
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | Update hash data.
|
---|
157 |
|
---|
158 | @param[in] HashContext Hash context.
|
---|
159 | @param[in] DataToHash Data to be hashed.
|
---|
160 | @param[in] DataToHashLen Data size.
|
---|
161 |
|
---|
162 | @retval TRUE Hash updated.
|
---|
163 | @retval FALSE Hash updated unsuccessful.
|
---|
164 | **/
|
---|
165 | BOOLEAN
|
---|
166 | EFIAPI
|
---|
167 | HashApiUpdate (
|
---|
168 | IN HASH_API_CONTEXT HashContext,
|
---|
169 | IN VOID *DataToHash,
|
---|
170 | IN UINTN DataToHashLen
|
---|
171 | )
|
---|
172 | {
|
---|
173 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
174 | #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
|
---|
175 | case HASH_ALG_SHA1:
|
---|
176 | return Sha1Update (HashContext, DataToHash, DataToHashLen);
|
---|
177 | break;
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | case HASH_ALG_SHA256:
|
---|
181 | return Sha256Update (HashContext, DataToHash, DataToHashLen);
|
---|
182 | break;
|
---|
183 |
|
---|
184 | case HASH_ALG_SHA384:
|
---|
185 | return Sha384Update (HashContext, DataToHash, DataToHashLen);
|
---|
186 | break;
|
---|
187 |
|
---|
188 | case HASH_ALG_SHA512:
|
---|
189 | return Sha512Update (HashContext, DataToHash, DataToHashLen);
|
---|
190 | break;
|
---|
191 |
|
---|
192 | case HASH_ALG_SM3_256:
|
---|
193 | return Sm3Update (HashContext, DataToHash, DataToHashLen);
|
---|
194 | break;
|
---|
195 |
|
---|
196 | default:
|
---|
197 | ASSERT (FALSE);
|
---|
198 | return FALSE;
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | Hash complete.
|
---|
205 |
|
---|
206 | @param[in] HashContext Hash context.
|
---|
207 | @param[out] Digest Hash Digest.
|
---|
208 |
|
---|
209 | @retval TRUE Hash complete and Digest is returned.
|
---|
210 | @retval FALSE Hash complete unsuccessful.
|
---|
211 | **/
|
---|
212 | BOOLEAN
|
---|
213 | EFIAPI
|
---|
214 | HashApiFinal (
|
---|
215 | IN HASH_API_CONTEXT HashContext,
|
---|
216 | OUT UINT8 *Digest
|
---|
217 | )
|
---|
218 | {
|
---|
219 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
220 | #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
|
---|
221 | case HASH_ALG_SHA1:
|
---|
222 | return Sha1Final (HashContext, Digest);
|
---|
223 | break;
|
---|
224 | #endif
|
---|
225 |
|
---|
226 | case HASH_ALG_SHA256:
|
---|
227 | return Sha256Final (HashContext, Digest);
|
---|
228 | break;
|
---|
229 |
|
---|
230 | case HASH_ALG_SHA384:
|
---|
231 | return Sha384Final (HashContext, Digest);
|
---|
232 | break;
|
---|
233 |
|
---|
234 | case HASH_ALG_SHA512:
|
---|
235 | return Sha512Final (HashContext, Digest);
|
---|
236 | break;
|
---|
237 |
|
---|
238 | case HASH_ALG_SM3_256:
|
---|
239 | return Sm3Final (HashContext, Digest);
|
---|
240 | break;
|
---|
241 |
|
---|
242 | default:
|
---|
243 | ASSERT (FALSE);
|
---|
244 | return FALSE;
|
---|
245 | break;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | Computes hash message digest of a input data buffer.
|
---|
251 |
|
---|
252 | @param[in] DataToHash Data to be hashed.
|
---|
253 | @param[in] DataToHashLen Data size.
|
---|
254 | @param[out] Digest Hash Digest.
|
---|
255 |
|
---|
256 | @retval TRUE Hash digest computation succeeded.
|
---|
257 | @retval FALSE Hash digest computation failed.
|
---|
258 | **/
|
---|
259 | BOOLEAN
|
---|
260 | EFIAPI
|
---|
261 | HashApiHashAll (
|
---|
262 | IN CONST VOID *DataToHash,
|
---|
263 | IN UINTN DataToHashLen,
|
---|
264 | OUT UINT8 *Digest
|
---|
265 | )
|
---|
266 | {
|
---|
267 | switch (PcdGet32 (PcdHashApiLibPolicy)) {
|
---|
268 | #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
|
---|
269 | case HASH_ALG_SHA1:
|
---|
270 | return Sha1HashAll (DataToHash, DataToHashLen, Digest);
|
---|
271 | break;
|
---|
272 | #endif
|
---|
273 |
|
---|
274 | case HASH_ALG_SHA256:
|
---|
275 | return Sha256HashAll (DataToHash, DataToHashLen, Digest);
|
---|
276 | break;
|
---|
277 |
|
---|
278 | case HASH_ALG_SHA384:
|
---|
279 | return Sha384HashAll (DataToHash, DataToHashLen, Digest);
|
---|
280 | break;
|
---|
281 |
|
---|
282 | case HASH_ALG_SHA512:
|
---|
283 | return Sha512HashAll (DataToHash, DataToHashLen, Digest);
|
---|
284 | break;
|
---|
285 |
|
---|
286 | case HASH_ALG_SM3_256:
|
---|
287 | return Sm3HashAll (DataToHash, DataToHashLen, Digest);
|
---|
288 | break;
|
---|
289 |
|
---|
290 | default:
|
---|
291 | ASSERT (FALSE);
|
---|
292 | return FALSE;
|
---|
293 | break;
|
---|
294 | }
|
---|
295 | }
|
---|