]>
Commit | Line | Data |
---|---|---|
724dcbb2 JY |
1 | /** @file\r |
2 | This module implements Hash2 Protocol.\r | |
3 | \r | |
cb9a7eba | 4 | (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r |
b3548d32 | 5 | Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r |
289b714b | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
724dcbb2 JY |
7 | \r |
8 | **/\r | |
9 | \r | |
10 | #include <Uefi.h>\r | |
11 | #include <Protocol/Hash2.h>\r | |
12 | #include <Library/BaseLib.h>\r | |
13 | #include <Library/UefiBootServicesTableLib.h>\r | |
14 | #include <Library/MemoryAllocationLib.h>\r | |
15 | #include <Library/BaseMemoryLib.h>\r | |
16 | #include <Library/DebugLib.h>\r | |
17 | #include <Library/BaseCryptLib.h>\r | |
18 | \r | |
19 | #include "Driver.h"\r | |
20 | \r | |
21 | /**\r | |
22 | Retrieves the size, in bytes, of the context buffer required for hash operations.\r | |
23 | \r | |
24 | If this interface is not supported, then return zero.\r | |
25 | \r | |
26 | @return The size, in bytes, of the context buffer required for hash operations.\r | |
27 | @retval 0 This interface is not supported.\r | |
28 | \r | |
29 | **/\r | |
30 | typedef\r | |
31 | UINTN\r | |
32 | (EFIAPI *EFI_HASH_GET_CONTEXT_SIZE) (\r | |
33 | VOID\r | |
34 | );\r | |
35 | \r | |
36 | /**\r | |
37 | Initializes user-supplied memory pointed by Sha1Context as hash context for\r | |
38 | subsequent use.\r | |
39 | \r | |
40 | If HashContext is NULL, then return FALSE.\r | |
41 | If this interface is not supported, then return FALSE.\r | |
42 | \r | |
43 | @param[out] HashContext Pointer to Hashcontext being initialized.\r | |
44 | \r | |
45 | @retval TRUE Hash context initialization succeeded.\r | |
46 | @retval FALSE Hash context initialization failed.\r | |
47 | @retval FALSE This interface is not supported.\r | |
48 | \r | |
49 | **/\r | |
50 | typedef\r | |
51 | BOOLEAN\r | |
52 | (EFIAPI *EFI_HASH_INIT) (\r | |
53 | OUT VOID *HashContext\r | |
54 | );\r | |
55 | \r | |
56 | /**\r | |
57 | Digests the input data and updates Hash context.\r | |
58 | \r | |
59 | This function performs Hash digest on a data buffer of the specified size.\r | |
60 | It can be called multiple times to compute the digest of long or discontinuous data streams.\r | |
0ab475c9 | 61 | Hash context should be already correctly initialized by HashInit(), and should not be finalized\r |
724dcbb2 JY |
62 | by HashFinal(). Behavior with invalid context is undefined.\r |
63 | \r | |
64 | If HashContext is NULL, then return FALSE.\r | |
65 | If this interface is not supported, then return FALSE.\r | |
66 | \r | |
67 | @param[in, out] HashContext Pointer to the Hash context.\r | |
68 | @param[in] Data Pointer to the buffer containing the data to be hashed.\r | |
69 | @param[in] DataSize Size of Data buffer in bytes.\r | |
70 | \r | |
71 | @retval TRUE SHA-1 data digest succeeded.\r | |
72 | @retval FALSE SHA-1 data digest failed.\r | |
73 | @retval FALSE This interface is not supported.\r | |
74 | \r | |
75 | **/\r | |
76 | typedef\r | |
77 | BOOLEAN\r | |
78 | (EFIAPI *EFI_HASH_UPDATE) (\r | |
79 | IN OUT VOID *HashContext,\r | |
80 | IN CONST VOID *Data,\r | |
81 | IN UINTN DataSize\r | |
82 | );\r | |
83 | \r | |
84 | /**\r | |
85 | Completes computation of the Hash digest value.\r | |
86 | \r | |
87 | This function completes hash computation and retrieves the digest value into\r | |
88 | the specified memory. After this function has been called, the Hash context cannot\r | |
89 | be used again.\r | |
ce4cca7c | 90 | Hash context should be already correctly initialized by HashInit(), and should not be\r |
724dcbb2 JY |
91 | finalized by HashFinal(). Behavior with invalid Hash context is undefined.\r |
92 | \r | |
93 | If HashContext is NULL, then return FALSE.\r | |
94 | If HashValue is NULL, then return FALSE.\r | |
95 | If this interface is not supported, then return FALSE.\r | |
96 | \r | |
97 | @param[in, out] HashContext Pointer to the Hash context.\r | |
98 | @param[out] HashValue Pointer to a buffer that receives the Hash digest\r | |
99 | value.\r | |
100 | \r | |
101 | @retval TRUE Hash digest computation succeeded.\r | |
102 | @retval FALSE Hash digest computation failed.\r | |
103 | @retval FALSE This interface is not supported.\r | |
104 | \r | |
105 | **/\r | |
106 | typedef\r | |
107 | BOOLEAN\r | |
108 | (EFIAPI *EFI_HASH_FINAL) (\r | |
109 | IN OUT VOID *HashContext,\r | |
110 | OUT UINT8 *HashValue\r | |
111 | );\r | |
112 | \r | |
113 | typedef struct {\r | |
114 | EFI_GUID *Guid;\r | |
115 | UINT32 HashSize;\r | |
116 | EFI_HASH_GET_CONTEXT_SIZE GetContextSize;\r | |
117 | EFI_HASH_INIT Init;\r | |
118 | EFI_HASH_UPDATE Update;\r | |
119 | EFI_HASH_FINAL Final;\r | |
120 | } EFI_HASH_INFO;\r | |
121 | \r | |
122 | EFI_HASH_INFO mHashInfo[] = {\r | |
724dcbb2 JY |
123 | {&gEfiHashAlgorithmSha256Guid, sizeof(EFI_SHA256_HASH2), Sha256GetContextSize, Sha256Init, Sha256Update, Sha256Final },\r |
124 | {&gEfiHashAlgorithmSha384Guid, sizeof(EFI_SHA384_HASH2), Sha384GetContextSize, Sha384Init, Sha384Update, Sha384Final },\r | |
125 | {&gEfiHashAlgorithmSha512Guid, sizeof(EFI_SHA512_HASH2), Sha512GetContextSize, Sha512Init, Sha512Update, Sha512Final },\r | |
126 | };\r | |
127 | \r | |
128 | /**\r | |
129 | Returns the size of the hash which results from a specific algorithm.\r | |
130 | \r | |
131 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
132 | @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r | |
133 | @param[out] HashSize Holds the returned size of the algorithm's hash.\r | |
134 | \r | |
135 | @retval EFI_SUCCESS Hash size returned successfully.\r | |
136 | @retval EFI_INVALID_PARAMETER This or HashSize is NULL.\r | |
137 | @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver\r | |
138 | or HashAlgorithm is null.\r | |
139 | \r | |
140 | **/\r | |
141 | EFI_STATUS\r | |
142 | EFIAPI\r | |
143 | BaseCrypto2GetHashSize (\r | |
144 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
145 | IN CONST EFI_GUID *HashAlgorithm,\r | |
146 | OUT UINTN *HashSize\r | |
147 | );\r | |
148 | \r | |
149 | /**\r | |
150 | Creates a hash for the specified message text. The hash is not extendable.\r | |
151 | The output is final with any algorithm-required padding added by the function.\r | |
152 | \r | |
153 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
154 | @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r | |
155 | @param[in] Message Points to the start of the message.\r | |
156 | @param[in] MessageSize The size of Message, in bytes.\r | |
157 | @param[in,out] Hash On input, points to a caller-allocated buffer of the size\r | |
158 | returned by GetHashSize() for the specified HashAlgorithm.\r | |
159 | On output, the buffer holds the resulting hash computed from the message.\r | |
160 | \r | |
161 | @retval EFI_SUCCESS Hash returned successfully.\r | |
162 | @retval EFI_INVALID_PARAMETER This or Hash is NULL.\r | |
163 | @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver\r | |
164 | or HashAlgorithm is Null.\r | |
165 | @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available\r | |
166 | or MessageSize is greater than platform maximum.\r | |
167 | \r | |
168 | **/\r | |
169 | EFI_STATUS\r | |
170 | EFIAPI\r | |
171 | BaseCrypto2Hash (\r | |
172 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
173 | IN CONST EFI_GUID *HashAlgorithm,\r | |
174 | IN CONST UINT8 *Message,\r | |
175 | IN UINTN MessageSize,\r | |
176 | IN OUT EFI_HASH2_OUTPUT *Hash\r | |
177 | );\r | |
178 | \r | |
179 | /**\r | |
180 | This function must be called to initialize a digest calculation to be subsequently performed using the\r | |
181 | EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().\r | |
182 | \r | |
183 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
184 | @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r | |
185 | \r | |
186 | @retval EFI_SUCCESS Initialized successfully.\r | |
187 | @retval EFI_INVALID_PARAMETER This is NULL.\r | |
188 | @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver\r | |
189 | or HashAlgorithm is Null.\r | |
190 | @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.\r | |
191 | @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),\r | |
192 | or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.\r | |
193 | \r | |
194 | **/\r | |
195 | EFI_STATUS\r | |
196 | EFIAPI\r | |
197 | BaseCrypto2HashInit (\r | |
198 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
199 | IN CONST EFI_GUID *HashAlgorithm\r | |
200 | );\r | |
201 | \r | |
202 | /**\r | |
203 | Updates the hash of a computation in progress by adding a message text.\r | |
204 | \r | |
205 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
206 | @param[in] Message Points to the start of the message.\r | |
207 | @param[in] MessageSize The size of Message, in bytes.\r | |
208 | \r | |
209 | @retval EFI_SUCCESS Digest in progress updated successfully.\r | |
210 | @retval EFI_INVALID_PARAMETER This or Hash is NULL.\r | |
211 | @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available\r | |
212 | or MessageSize is greater than platform maximum.\r | |
213 | @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),\r | |
214 | or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.\r | |
215 | \r | |
216 | **/\r | |
217 | EFI_STATUS\r | |
218 | EFIAPI\r | |
219 | BaseCrypto2HashUpdate (\r | |
220 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
221 | IN CONST UINT8 *Message,\r | |
222 | IN UINTN MessageSize\r | |
223 | );\r | |
224 | \r | |
225 | /**\r | |
226 | Finalizes a hash operation in progress and returns calculation result.\r | |
227 | The output is final with any necessary padding added by the function.\r | |
228 | The hash may not be further updated or extended after HashFinal().\r | |
229 | \r | |
230 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
231 | @param[in,out] Hash On input, points to a caller-allocated buffer of the size\r | |
232 | returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().\r | |
233 | On output, the buffer holds the resulting hash computed from the message.\r | |
234 | \r | |
235 | @retval EFI_SUCCESS Hash returned successfully.\r | |
236 | @retval EFI_INVALID_PARAMETER This or Hash is NULL.\r | |
237 | @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),\r | |
238 | or the operation in progress was canceled by a call to Hash() on the same instance.\r | |
239 | \r | |
240 | **/\r | |
241 | EFI_STATUS\r | |
242 | EFIAPI\r | |
243 | BaseCrypto2HashFinal (\r | |
244 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
245 | IN OUT EFI_HASH2_OUTPUT *Hash\r | |
246 | );\r | |
247 | \r | |
248 | EFI_HASH2_PROTOCOL mHash2Protocol = {\r | |
249 | BaseCrypto2GetHashSize,\r | |
250 | BaseCrypto2Hash,\r | |
251 | BaseCrypto2HashInit,\r | |
252 | BaseCrypto2HashUpdate,\r | |
253 | BaseCrypto2HashFinal,\r | |
254 | };\r | |
255 | \r | |
256 | /**\r | |
257 | Returns hash information.\r | |
258 | \r | |
259 | @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r | |
260 | \r | |
261 | @return Hash information.\r | |
262 | **/\r | |
263 | EFI_HASH_INFO *\r | |
264 | GetHashInfo (\r | |
265 | IN CONST EFI_GUID *HashAlgorithm\r | |
266 | )\r | |
267 | {\r | |
268 | UINTN Index;\r | |
269 | \r | |
270 | for (Index = 0; Index < sizeof(mHashInfo)/sizeof(mHashInfo[0]); Index++) {\r | |
271 | if (CompareGuid (HashAlgorithm, mHashInfo[Index].Guid)) {\r | |
272 | return &mHashInfo[Index];\r | |
273 | }\r | |
274 | }\r | |
275 | return NULL;\r | |
276 | }\r | |
277 | \r | |
278 | /**\r | |
279 | Returns the size of the hash which results from a specific algorithm.\r | |
280 | \r | |
281 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
282 | @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r | |
283 | @param[out] HashSize Holds the returned size of the algorithm's hash.\r | |
284 | \r | |
285 | @retval EFI_SUCCESS Hash size returned successfully.\r | |
286 | @retval EFI_INVALID_PARAMETER This or HashSize is NULL.\r | |
287 | @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver\r | |
288 | or HashAlgorithm is null.\r | |
289 | \r | |
290 | **/\r | |
291 | EFI_STATUS\r | |
292 | EFIAPI\r | |
293 | BaseCrypto2GetHashSize (\r | |
294 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
295 | IN CONST EFI_GUID *HashAlgorithm,\r | |
296 | OUT UINTN *HashSize\r | |
297 | )\r | |
298 | {\r | |
299 | EFI_HASH_INFO *HashInfo;\r | |
300 | \r | |
301 | if ((This == NULL) || (HashSize == NULL)) {\r | |
302 | return EFI_INVALID_PARAMETER;\r | |
303 | }\r | |
304 | \r | |
305 | if (HashAlgorithm == NULL) {\r | |
306 | return EFI_UNSUPPORTED;\r | |
307 | }\r | |
308 | \r | |
309 | HashInfo = GetHashInfo (HashAlgorithm);\r | |
310 | if (HashInfo == NULL) {\r | |
311 | return EFI_UNSUPPORTED;\r | |
312 | }\r | |
313 | \r | |
314 | *HashSize = HashInfo->HashSize;\r | |
315 | return EFI_SUCCESS;\r | |
316 | }\r | |
317 | \r | |
318 | /**\r | |
319 | Creates a hash for the specified message text. The hash is not extendable.\r | |
320 | The output is final with any algorithm-required padding added by the function.\r | |
321 | \r | |
322 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
323 | @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r | |
324 | @param[in] Message Points to the start of the message.\r | |
325 | @param[in] MessageSize The size of Message, in bytes.\r | |
326 | @param[in,out] Hash On input, points to a caller-allocated buffer of the size\r | |
327 | returned by GetHashSize() for the specified HashAlgorithm.\r | |
328 | On output, the buffer holds the resulting hash computed from the message.\r | |
329 | \r | |
330 | @retval EFI_SUCCESS Hash returned successfully.\r | |
331 | @retval EFI_INVALID_PARAMETER This or Hash is NULL.\r | |
332 | @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver\r | |
333 | or HashAlgorithm is Null.\r | |
334 | @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available\r | |
335 | or MessageSize is greater than platform maximum.\r | |
336 | \r | |
337 | **/\r | |
338 | EFI_STATUS\r | |
339 | EFIAPI\r | |
340 | BaseCrypto2Hash (\r | |
341 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
342 | IN CONST EFI_GUID *HashAlgorithm,\r | |
343 | IN CONST UINT8 *Message,\r | |
344 | IN UINTN MessageSize,\r | |
345 | IN OUT EFI_HASH2_OUTPUT *Hash\r | |
346 | )\r | |
347 | {\r | |
348 | EFI_HASH_INFO *HashInfo;\r | |
349 | VOID *HashCtx;\r | |
350 | UINTN CtxSize;\r | |
351 | BOOLEAN Ret;\r | |
352 | EFI_STATUS Status;\r | |
099bff5d | 353 | HASH2_INSTANCE_DATA *Instance;\r |
724dcbb2 JY |
354 | \r |
355 | Status = EFI_SUCCESS;\r | |
356 | \r | |
357 | if ((This == NULL) || (Hash == NULL)) {\r | |
358 | return EFI_INVALID_PARAMETER;\r | |
359 | }\r | |
360 | \r | |
361 | if (HashAlgorithm == NULL) {\r | |
362 | return EFI_UNSUPPORTED;\r | |
363 | }\r | |
364 | \r | |
365 | HashInfo = GetHashInfo (HashAlgorithm);\r | |
366 | if (HashInfo == NULL) {\r | |
367 | return EFI_UNSUPPORTED;\r | |
368 | }\r | |
b3548d32 | 369 | \r |
099bff5d JY |
370 | Instance = HASH2_INSTANCE_DATA_FROM_THIS(This);\r |
371 | if (Instance->HashContext != NULL) {\r | |
372 | FreePool (Instance->HashContext);\r | |
373 | }\r | |
374 | Instance->HashInfoContext = NULL;\r | |
375 | Instance->HashContext = NULL;\r | |
724dcbb2 JY |
376 | \r |
377 | //\r | |
378 | // Start hash sequence\r | |
379 | //\r | |
380 | CtxSize = HashInfo->GetContextSize ();\r | |
381 | if (CtxSize == 0) {\r | |
382 | return EFI_UNSUPPORTED;\r | |
383 | }\r | |
384 | HashCtx = AllocatePool (CtxSize);\r | |
385 | if (HashCtx == NULL) {\r | |
386 | return EFI_OUT_OF_RESOURCES;\r | |
387 | }\r | |
388 | \r | |
389 | Ret = HashInfo->Init (HashCtx);\r | |
390 | if (!Ret) {\r | |
391 | Status = EFI_OUT_OF_RESOURCES;\r | |
392 | goto Done;\r | |
393 | }\r | |
394 | \r | |
099bff5d JY |
395 | //\r |
396 | // Setup the context\r | |
397 | //\r | |
398 | Instance->HashContext = HashCtx;\r | |
399 | Instance->HashInfoContext = HashInfo;\r | |
400 | \r | |
724dcbb2 JY |
401 | Ret = HashInfo->Update (HashCtx, Message, MessageSize);\r |
402 | if (!Ret) {\r | |
403 | Status = EFI_OUT_OF_RESOURCES;\r | |
404 | goto Done;\r | |
405 | }\r | |
406 | \r | |
407 | Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);\r | |
408 | if (!Ret) {\r | |
409 | Status = EFI_OUT_OF_RESOURCES;\r | |
410 | goto Done;\r | |
411 | }\r | |
412 | Done:\r | |
099bff5d JY |
413 | //\r |
414 | // Cleanup the context\r | |
415 | //\r | |
724dcbb2 | 416 | FreePool (HashCtx);\r |
099bff5d JY |
417 | Instance->HashInfoContext = NULL;\r |
418 | Instance->HashContext = NULL;\r | |
724dcbb2 JY |
419 | return Status;\r |
420 | }\r | |
421 | \r | |
422 | /**\r | |
423 | This function must be called to initialize a digest calculation to be subsequently performed using the\r | |
424 | EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().\r | |
425 | \r | |
426 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
427 | @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r | |
428 | \r | |
429 | @retval EFI_SUCCESS Initialized successfully.\r | |
430 | @retval EFI_INVALID_PARAMETER This is NULL.\r | |
431 | @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver\r | |
432 | or HashAlgorithm is Null.\r | |
433 | @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.\r | |
434 | @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),\r | |
435 | or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.\r | |
436 | \r | |
437 | **/\r | |
438 | EFI_STATUS\r | |
439 | EFIAPI\r | |
440 | BaseCrypto2HashInit (\r | |
441 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
442 | IN CONST EFI_GUID *HashAlgorithm\r | |
443 | )\r | |
444 | {\r | |
445 | EFI_HASH_INFO *HashInfo;\r | |
446 | VOID *HashCtx;\r | |
447 | UINTN CtxSize;\r | |
448 | BOOLEAN Ret;\r | |
449 | HASH2_INSTANCE_DATA *Instance;\r | |
450 | \r | |
451 | if (This == NULL) {\r | |
452 | return EFI_INVALID_PARAMETER;\r | |
453 | }\r | |
454 | \r | |
455 | if (HashAlgorithm == NULL) {\r | |
456 | return EFI_UNSUPPORTED;\r | |
457 | }\r | |
458 | \r | |
459 | HashInfo = GetHashInfo (HashAlgorithm);\r | |
460 | if (HashInfo == NULL) {\r | |
461 | return EFI_UNSUPPORTED;\r | |
462 | }\r | |
463 | \r | |
464 | //\r | |
465 | // Consistency Check\r | |
466 | //\r | |
467 | Instance = HASH2_INSTANCE_DATA_FROM_THIS(This);\r | |
c533ed3e | 468 | if ((Instance->HashContext != NULL) || (Instance->HashInfoContext != NULL)) {\r |
724dcbb2 JY |
469 | return EFI_ALREADY_STARTED;\r |
470 | }\r | |
471 | \r | |
472 | //\r | |
473 | // Start hash sequence\r | |
474 | //\r | |
475 | CtxSize = HashInfo->GetContextSize ();\r | |
476 | if (CtxSize == 0) {\r | |
477 | return EFI_UNSUPPORTED;\r | |
478 | }\r | |
479 | HashCtx = AllocatePool (CtxSize);\r | |
480 | if (HashCtx == NULL) {\r | |
481 | return EFI_OUT_OF_RESOURCES;\r | |
482 | }\r | |
483 | \r | |
484 | Ret = HashInfo->Init (HashCtx);\r | |
485 | if (!Ret) {\r | |
486 | FreePool (HashCtx);\r | |
487 | return EFI_OUT_OF_RESOURCES;\r | |
488 | }\r | |
489 | \r | |
490 | //\r | |
491 | // Setup the context\r | |
492 | //\r | |
493 | Instance->HashContext = HashCtx;\r | |
494 | Instance->HashInfoContext = HashInfo;\r | |
a3a09748 | 495 | Instance->Updated = FALSE;\r |
724dcbb2 JY |
496 | \r |
497 | return EFI_SUCCESS;\r | |
498 | }\r | |
499 | \r | |
500 | /**\r | |
501 | Updates the hash of a computation in progress by adding a message text.\r | |
502 | \r | |
503 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
504 | @param[in] Message Points to the start of the message.\r | |
505 | @param[in] MessageSize The size of Message, in bytes.\r | |
506 | \r | |
507 | @retval EFI_SUCCESS Digest in progress updated successfully.\r | |
508 | @retval EFI_INVALID_PARAMETER This or Hash is NULL.\r | |
509 | @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available\r | |
510 | or MessageSize is greater than platform maximum.\r | |
511 | @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),\r | |
512 | or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.\r | |
513 | \r | |
514 | **/\r | |
515 | EFI_STATUS\r | |
516 | EFIAPI\r | |
517 | BaseCrypto2HashUpdate (\r | |
518 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
519 | IN CONST UINT8 *Message,\r | |
520 | IN UINTN MessageSize\r | |
521 | )\r | |
522 | {\r | |
523 | EFI_HASH_INFO *HashInfo;\r | |
524 | VOID *HashCtx;\r | |
525 | BOOLEAN Ret;\r | |
526 | HASH2_INSTANCE_DATA *Instance;\r | |
527 | \r | |
528 | if (This == NULL) {\r | |
529 | return EFI_INVALID_PARAMETER;\r | |
530 | }\r | |
531 | \r | |
532 | //\r | |
533 | // Consistency Check\r | |
534 | //\r | |
535 | Instance = HASH2_INSTANCE_DATA_FROM_THIS(This);\r | |
536 | if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL)) {\r | |
537 | return EFI_NOT_READY;\r | |
538 | }\r | |
539 | HashInfo = Instance->HashInfoContext;\r | |
540 | HashCtx = Instance->HashContext;\r | |
541 | \r | |
542 | Ret = HashInfo->Update (HashCtx, Message, MessageSize);\r | |
543 | if (!Ret) {\r | |
544 | return EFI_OUT_OF_RESOURCES;\r | |
545 | }\r | |
546 | \r | |
a3a09748 JY |
547 | Instance->Updated = TRUE;\r |
548 | \r | |
724dcbb2 JY |
549 | return EFI_SUCCESS;\r |
550 | }\r | |
551 | \r | |
552 | /**\r | |
553 | Finalizes a hash operation in progress and returns calculation result.\r | |
554 | The output is final with any necessary padding added by the function.\r | |
555 | The hash may not be further updated or extended after HashFinal().\r | |
556 | \r | |
557 | @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r | |
558 | @param[in,out] Hash On input, points to a caller-allocated buffer of the size\r | |
559 | returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().\r | |
560 | On output, the buffer holds the resulting hash computed from the message.\r | |
561 | \r | |
562 | @retval EFI_SUCCESS Hash returned successfully.\r | |
563 | @retval EFI_INVALID_PARAMETER This or Hash is NULL.\r | |
564 | @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),\r | |
565 | or the operation in progress was canceled by a call to Hash() on the same instance.\r | |
566 | \r | |
567 | **/\r | |
568 | EFI_STATUS\r | |
569 | EFIAPI\r | |
570 | BaseCrypto2HashFinal (\r | |
571 | IN CONST EFI_HASH2_PROTOCOL *This,\r | |
572 | IN OUT EFI_HASH2_OUTPUT *Hash\r | |
573 | )\r | |
574 | {\r | |
575 | EFI_HASH_INFO *HashInfo;\r | |
576 | VOID *HashCtx;\r | |
577 | BOOLEAN Ret;\r | |
578 | HASH2_INSTANCE_DATA *Instance;\r | |
579 | \r | |
580 | if ((This == NULL) || (Hash == NULL)) {\r | |
581 | return EFI_INVALID_PARAMETER;\r | |
582 | }\r | |
583 | \r | |
584 | //\r | |
585 | // Consistency Check\r | |
586 | //\r | |
587 | Instance = HASH2_INSTANCE_DATA_FROM_THIS(This);\r | |
a3a09748 JY |
588 | if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL) ||\r |
589 | (!Instance->Updated)) {\r | |
724dcbb2 JY |
590 | return EFI_NOT_READY;\r |
591 | }\r | |
592 | HashInfo = Instance->HashInfoContext;\r | |
593 | HashCtx = Instance->HashContext;\r | |
594 | \r | |
595 | Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);\r | |
596 | \r | |
597 | //\r | |
598 | // Cleanup the context\r | |
599 | //\r | |
600 | FreePool (HashCtx);\r | |
601 | Instance->HashInfoContext = NULL;\r | |
602 | Instance->HashContext = NULL;\r | |
a3a09748 | 603 | Instance->Updated = FALSE;\r |
724dcbb2 JY |
604 | \r |
605 | if (!Ret) {\r | |
606 | return EFI_OUT_OF_RESOURCES;\r | |
607 | }\r | |
608 | \r | |
609 | return EFI_SUCCESS;\r | |
610 | }\r |