]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Hash2DxeCrypto/Hash2DxeCrypto.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / SecurityPkg / Hash2DxeCrypto / Hash2DxeCrypto.c
CommitLineData
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 5Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
289b714b 6SPDX-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
30typedef\r
31UINTN\r
c411b485 32(EFIAPI *EFI_HASH_GET_CONTEXT_SIZE)(\r
724dcbb2
JY
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
50typedef\r
51BOOLEAN\r
c411b485 52(EFIAPI *EFI_HASH_INIT)(\r
724dcbb2
JY
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
76typedef\r
77BOOLEAN\r
c411b485 78(EFIAPI *EFI_HASH_UPDATE)(\r
724dcbb2
JY
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
106typedef\r
107BOOLEAN\r
c411b485 108(EFIAPI *EFI_HASH_FINAL)(\r
724dcbb2
JY
109 IN OUT VOID *HashContext,\r
110 OUT UINT8 *HashValue\r
111 );\r
112\r
113typedef struct {\r
c411b485
MK
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
724dcbb2
JY
120} EFI_HASH_INFO;\r
121\r
122EFI_HASH_INFO mHashInfo[] = {\r
c411b485
MK
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
724dcbb2
JY
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
141EFI_STATUS\r
142EFIAPI\r
143BaseCrypto2GetHashSize (\r
c411b485
MK
144 IN CONST EFI_HASH2_PROTOCOL *This,\r
145 IN CONST EFI_GUID *HashAlgorithm,\r
146 OUT UINTN *HashSize\r
724dcbb2
JY
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
169EFI_STATUS\r
170EFIAPI\r
171BaseCrypto2Hash (\r
c411b485
MK
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
724dcbb2
JY
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
195EFI_STATUS\r
196EFIAPI\r
197BaseCrypto2HashInit (\r
c411b485
MK
198 IN CONST EFI_HASH2_PROTOCOL *This,\r
199 IN CONST EFI_GUID *HashAlgorithm\r
724dcbb2
JY
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
217EFI_STATUS\r
218EFIAPI\r
219BaseCrypto2HashUpdate (\r
c411b485
MK
220 IN CONST EFI_HASH2_PROTOCOL *This,\r
221 IN CONST UINT8 *Message,\r
222 IN UINTN MessageSize\r
724dcbb2
JY
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
241EFI_STATUS\r
242EFIAPI\r
243BaseCrypto2HashFinal (\r
c411b485
MK
244 IN CONST EFI_HASH2_PROTOCOL *This,\r
245 IN OUT EFI_HASH2_OUTPUT *Hash\r
724dcbb2
JY
246 );\r
247\r
c411b485 248EFI_HASH2_PROTOCOL mHash2Protocol = {\r
724dcbb2
JY
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
263EFI_HASH_INFO *\r
264GetHashInfo (\r
c411b485 265 IN CONST EFI_GUID *HashAlgorithm\r
724dcbb2
JY
266 )\r
267{\r
c411b485 268 UINTN Index;\r
724dcbb2 269\r
c411b485 270 for (Index = 0; Index < sizeof (mHashInfo)/sizeof (mHashInfo[0]); Index++) {\r
724dcbb2
JY
271 if (CompareGuid (HashAlgorithm, mHashInfo[Index].Guid)) {\r
272 return &mHashInfo[Index];\r
273 }\r
274 }\r
c411b485 275\r
724dcbb2
JY
276 return NULL;\r
277}\r
278\r
279/**\r
280 Returns the size of the hash which results from a specific algorithm.\r
281\r
282 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r
283 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r
284 @param[out] HashSize Holds the returned size of the algorithm's hash.\r
285\r
286 @retval EFI_SUCCESS Hash size returned successfully.\r
287 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.\r
288 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver\r
289 or HashAlgorithm is null.\r
290\r
291**/\r
292EFI_STATUS\r
293EFIAPI\r
294BaseCrypto2GetHashSize (\r
c411b485
MK
295 IN CONST EFI_HASH2_PROTOCOL *This,\r
296 IN CONST EFI_GUID *HashAlgorithm,\r
297 OUT UINTN *HashSize\r
724dcbb2
JY
298 )\r
299{\r
c411b485 300 EFI_HASH_INFO *HashInfo;\r
724dcbb2
JY
301\r
302 if ((This == NULL) || (HashSize == NULL)) {\r
303 return EFI_INVALID_PARAMETER;\r
304 }\r
305\r
306 if (HashAlgorithm == NULL) {\r
307 return EFI_UNSUPPORTED;\r
308 }\r
309\r
310 HashInfo = GetHashInfo (HashAlgorithm);\r
311 if (HashInfo == NULL) {\r
312 return EFI_UNSUPPORTED;\r
313 }\r
314\r
315 *HashSize = HashInfo->HashSize;\r
316 return EFI_SUCCESS;\r
317}\r
318\r
319/**\r
320 Creates a hash for the specified message text. The hash is not extendable.\r
321 The output is final with any algorithm-required padding added by the function.\r
322\r
323 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r
324 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r
325 @param[in] Message Points to the start of the message.\r
326 @param[in] MessageSize The size of Message, in bytes.\r
327 @param[in,out] Hash On input, points to a caller-allocated buffer of the size\r
328 returned by GetHashSize() for the specified HashAlgorithm.\r
329 On output, the buffer holds the resulting hash computed from the message.\r
330\r
331 @retval EFI_SUCCESS Hash returned successfully.\r
332 @retval EFI_INVALID_PARAMETER This or Hash is NULL.\r
333 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver\r
334 or HashAlgorithm is Null.\r
335 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available\r
336 or MessageSize is greater than platform maximum.\r
337\r
338**/\r
339EFI_STATUS\r
340EFIAPI\r
341BaseCrypto2Hash (\r
c411b485
MK
342 IN CONST EFI_HASH2_PROTOCOL *This,\r
343 IN CONST EFI_GUID *HashAlgorithm,\r
344 IN CONST UINT8 *Message,\r
345 IN UINTN MessageSize,\r
346 IN OUT EFI_HASH2_OUTPUT *Hash\r
724dcbb2
JY
347 )\r
348{\r
c411b485
MK
349 EFI_HASH_INFO *HashInfo;\r
350 VOID *HashCtx;\r
351 UINTN CtxSize;\r
352 BOOLEAN Ret;\r
353 EFI_STATUS Status;\r
354 HASH2_INSTANCE_DATA *Instance;\r
724dcbb2
JY
355\r
356 Status = EFI_SUCCESS;\r
357\r
358 if ((This == NULL) || (Hash == NULL)) {\r
359 return EFI_INVALID_PARAMETER;\r
360 }\r
361\r
362 if (HashAlgorithm == NULL) {\r
363 return EFI_UNSUPPORTED;\r
364 }\r
365\r
366 HashInfo = GetHashInfo (HashAlgorithm);\r
367 if (HashInfo == NULL) {\r
368 return EFI_UNSUPPORTED;\r
369 }\r
b3548d32 370\r
c411b485 371 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);\r
099bff5d
JY
372 if (Instance->HashContext != NULL) {\r
373 FreePool (Instance->HashContext);\r
374 }\r
c411b485 375\r
099bff5d 376 Instance->HashInfoContext = NULL;\r
c411b485 377 Instance->HashContext = NULL;\r
724dcbb2
JY
378\r
379 //\r
380 // Start hash sequence\r
381 //\r
382 CtxSize = HashInfo->GetContextSize ();\r
383 if (CtxSize == 0) {\r
384 return EFI_UNSUPPORTED;\r
385 }\r
c411b485 386\r
724dcbb2
JY
387 HashCtx = AllocatePool (CtxSize);\r
388 if (HashCtx == NULL) {\r
389 return EFI_OUT_OF_RESOURCES;\r
390 }\r
391\r
392 Ret = HashInfo->Init (HashCtx);\r
393 if (!Ret) {\r
394 Status = EFI_OUT_OF_RESOURCES;\r
395 goto Done;\r
396 }\r
397\r
099bff5d
JY
398 //\r
399 // Setup the context\r
400 //\r
c411b485 401 Instance->HashContext = HashCtx;\r
099bff5d
JY
402 Instance->HashInfoContext = HashInfo;\r
403\r
724dcbb2
JY
404 Ret = HashInfo->Update (HashCtx, Message, MessageSize);\r
405 if (!Ret) {\r
406 Status = EFI_OUT_OF_RESOURCES;\r
407 goto Done;\r
408 }\r
409\r
410 Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);\r
411 if (!Ret) {\r
412 Status = EFI_OUT_OF_RESOURCES;\r
413 goto Done;\r
414 }\r
c411b485 415\r
724dcbb2 416Done:\r
099bff5d
JY
417 //\r
418 // Cleanup the context\r
419 //\r
724dcbb2 420 FreePool (HashCtx);\r
099bff5d 421 Instance->HashInfoContext = NULL;\r
c411b485 422 Instance->HashContext = NULL;\r
724dcbb2
JY
423 return Status;\r
424}\r
425\r
426/**\r
427 This function must be called to initialize a digest calculation to be subsequently performed using the\r
428 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().\r
429\r
430 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r
431 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.\r
432\r
433 @retval EFI_SUCCESS Initialized successfully.\r
434 @retval EFI_INVALID_PARAMETER This is NULL.\r
435 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver\r
436 or HashAlgorithm is Null.\r
437 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.\r
438 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),\r
439 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.\r
440\r
441**/\r
442EFI_STATUS\r
443EFIAPI\r
444BaseCrypto2HashInit (\r
c411b485
MK
445 IN CONST EFI_HASH2_PROTOCOL *This,\r
446 IN CONST EFI_GUID *HashAlgorithm\r
724dcbb2
JY
447 )\r
448{\r
c411b485
MK
449 EFI_HASH_INFO *HashInfo;\r
450 VOID *HashCtx;\r
451 UINTN CtxSize;\r
452 BOOLEAN Ret;\r
453 HASH2_INSTANCE_DATA *Instance;\r
724dcbb2
JY
454\r
455 if (This == NULL) {\r
456 return EFI_INVALID_PARAMETER;\r
457 }\r
458\r
459 if (HashAlgorithm == NULL) {\r
460 return EFI_UNSUPPORTED;\r
461 }\r
462\r
463 HashInfo = GetHashInfo (HashAlgorithm);\r
464 if (HashInfo == NULL) {\r
465 return EFI_UNSUPPORTED;\r
466 }\r
467\r
468 //\r
469 // Consistency Check\r
470 //\r
c411b485 471 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);\r
c533ed3e 472 if ((Instance->HashContext != NULL) || (Instance->HashInfoContext != NULL)) {\r
724dcbb2
JY
473 return EFI_ALREADY_STARTED;\r
474 }\r
475\r
476 //\r
477 // Start hash sequence\r
478 //\r
479 CtxSize = HashInfo->GetContextSize ();\r
480 if (CtxSize == 0) {\r
481 return EFI_UNSUPPORTED;\r
482 }\r
c411b485 483\r
724dcbb2
JY
484 HashCtx = AllocatePool (CtxSize);\r
485 if (HashCtx == NULL) {\r
486 return EFI_OUT_OF_RESOURCES;\r
487 }\r
488\r
489 Ret = HashInfo->Init (HashCtx);\r
490 if (!Ret) {\r
491 FreePool (HashCtx);\r
492 return EFI_OUT_OF_RESOURCES;\r
493 }\r
494\r
495 //\r
496 // Setup the context\r
497 //\r
c411b485 498 Instance->HashContext = HashCtx;\r
724dcbb2 499 Instance->HashInfoContext = HashInfo;\r
c411b485 500 Instance->Updated = FALSE;\r
724dcbb2
JY
501\r
502 return EFI_SUCCESS;\r
503}\r
504\r
505/**\r
506 Updates the hash of a computation in progress by adding a message text.\r
507\r
508 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r
509 @param[in] Message Points to the start of the message.\r
510 @param[in] MessageSize The size of Message, in bytes.\r
511\r
512 @retval EFI_SUCCESS Digest in progress updated successfully.\r
513 @retval EFI_INVALID_PARAMETER This or Hash is NULL.\r
514 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available\r
515 or MessageSize is greater than platform maximum.\r
516 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),\r
517 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.\r
518\r
519**/\r
520EFI_STATUS\r
521EFIAPI\r
522BaseCrypto2HashUpdate (\r
c411b485
MK
523 IN CONST EFI_HASH2_PROTOCOL *This,\r
524 IN CONST UINT8 *Message,\r
525 IN UINTN MessageSize\r
724dcbb2
JY
526 )\r
527{\r
c411b485
MK
528 EFI_HASH_INFO *HashInfo;\r
529 VOID *HashCtx;\r
530 BOOLEAN Ret;\r
531 HASH2_INSTANCE_DATA *Instance;\r
724dcbb2
JY
532\r
533 if (This == NULL) {\r
534 return EFI_INVALID_PARAMETER;\r
535 }\r
536\r
537 //\r
538 // Consistency Check\r
539 //\r
c411b485 540 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);\r
724dcbb2
JY
541 if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL)) {\r
542 return EFI_NOT_READY;\r
543 }\r
c411b485 544\r
724dcbb2
JY
545 HashInfo = Instance->HashInfoContext;\r
546 HashCtx = Instance->HashContext;\r
547\r
548 Ret = HashInfo->Update (HashCtx, Message, MessageSize);\r
549 if (!Ret) {\r
550 return EFI_OUT_OF_RESOURCES;\r
551 }\r
552\r
a3a09748
JY
553 Instance->Updated = TRUE;\r
554\r
724dcbb2
JY
555 return EFI_SUCCESS;\r
556}\r
557\r
558/**\r
559 Finalizes a hash operation in progress and returns calculation result.\r
560 The output is final with any necessary padding added by the function.\r
561 The hash may not be further updated or extended after HashFinal().\r
562\r
563 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.\r
564 @param[in,out] Hash On input, points to a caller-allocated buffer of the size\r
565 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().\r
566 On output, the buffer holds the resulting hash computed from the message.\r
567\r
568 @retval EFI_SUCCESS Hash returned successfully.\r
569 @retval EFI_INVALID_PARAMETER This or Hash is NULL.\r
570 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),\r
571 or the operation in progress was canceled by a call to Hash() on the same instance.\r
572\r
573**/\r
574EFI_STATUS\r
575EFIAPI\r
576BaseCrypto2HashFinal (\r
c411b485
MK
577 IN CONST EFI_HASH2_PROTOCOL *This,\r
578 IN OUT EFI_HASH2_OUTPUT *Hash\r
724dcbb2
JY
579 )\r
580{\r
c411b485
MK
581 EFI_HASH_INFO *HashInfo;\r
582 VOID *HashCtx;\r
583 BOOLEAN Ret;\r
584 HASH2_INSTANCE_DATA *Instance;\r
724dcbb2
JY
585\r
586 if ((This == NULL) || (Hash == NULL)) {\r
587 return EFI_INVALID_PARAMETER;\r
588 }\r
589\r
590 //\r
591 // Consistency Check\r
592 //\r
c411b485 593 Instance = HASH2_INSTANCE_DATA_FROM_THIS (This);\r
a3a09748 594 if ((Instance->HashContext == NULL) || (Instance->HashInfoContext == NULL) ||\r
c411b485
MK
595 (!Instance->Updated))\r
596 {\r
724dcbb2
JY
597 return EFI_NOT_READY;\r
598 }\r
c411b485 599\r
724dcbb2
JY
600 HashInfo = Instance->HashInfoContext;\r
601 HashCtx = Instance->HashContext;\r
602\r
603 Ret = HashInfo->Final (HashCtx, (UINT8 *)Hash->Sha1Hash);\r
604\r
605 //\r
606 // Cleanup the context\r
607 //\r
608 FreePool (HashCtx);\r
609 Instance->HashInfoContext = NULL;\r
c411b485
MK
610 Instance->HashContext = NULL;\r
611 Instance->Updated = FALSE;\r
724dcbb2
JY
612\r
613 if (!Ret) {\r
614 return EFI_OUT_OF_RESOURCES;\r
615 }\r
616\r
617 return EFI_SUCCESS;\r
618}\r