2 This module implements Hash2 Protocol.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Protocol/Hash2.h>
12 #include <Library/BaseLib.h>
13 #include <Library/UefiBootServicesTableLib.h>
14 #include <Library/MemoryAllocationLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseCryptLib.h>
22 Retrieves the size, in bytes, of the context buffer required for hash operations.
24 If this interface is not supported, then return zero.
26 @return The size, in bytes, of the context buffer required for hash operations.
27 @retval 0 This interface is not supported.
32 (EFIAPI
*EFI_HASH_GET_CONTEXT_SIZE
)(
37 Initializes user-supplied memory pointed by Sha1Context as hash context for
40 If HashContext is NULL, then return FALSE.
41 If this interface is not supported, then return FALSE.
43 @param[out] HashContext Pointer to Hashcontext being initialized.
45 @retval TRUE Hash context initialization succeeded.
46 @retval FALSE Hash context initialization failed.
47 @retval FALSE This interface is not supported.
52 (EFIAPI
*EFI_HASH_INIT
)(
57 Digests the input data and updates Hash context.
59 This function performs Hash digest on a data buffer of the specified size.
60 It can be called multiple times to compute the digest of long or discontinuous data streams.
61 Hash context should be already correctly initialized by HashInit(), and should not be finalized
62 by HashFinal(). Behavior with invalid context is undefined.
64 If HashContext is NULL, then return FALSE.
65 If this interface is not supported, then return FALSE.
67 @param[in, out] HashContext Pointer to the Hash context.
68 @param[in] Data Pointer to the buffer containing the data to be hashed.
69 @param[in] DataSize Size of Data buffer in bytes.
71 @retval TRUE SHA-1 data digest succeeded.
72 @retval FALSE SHA-1 data digest failed.
73 @retval FALSE This interface is not supported.
78 (EFIAPI
*EFI_HASH_UPDATE
)(
79 IN OUT VOID
*HashContext
,
85 Completes computation of the Hash digest value.
87 This function completes hash computation and retrieves the digest value into
88 the specified memory. After this function has been called, the Hash context cannot
90 Hash context should be already correctly initialized by HashInit(), and should not be
91 finalized by HashFinal(). Behavior with invalid Hash context is undefined.
93 If HashContext is NULL, then return FALSE.
94 If HashValue is NULL, then return FALSE.
95 If this interface is not supported, then return FALSE.
97 @param[in, out] HashContext Pointer to the Hash context.
98 @param[out] HashValue Pointer to a buffer that receives the Hash digest
101 @retval TRUE Hash digest computation succeeded.
102 @retval FALSE Hash digest computation failed.
103 @retval FALSE This interface is not supported.
108 (EFIAPI
*EFI_HASH_FINAL
)(
109 IN OUT VOID
*HashContext
,
116 EFI_HASH_GET_CONTEXT_SIZE GetContextSize
;
118 EFI_HASH_UPDATE Update
;
119 EFI_HASH_FINAL Final
;
122 EFI_HASH_INFO mHashInfo
[] = {
123 { &gEfiHashAlgorithmSha256Guid
, sizeof (EFI_SHA256_HASH2
), Sha256GetContextSize
, Sha256Init
, Sha256Update
, Sha256Final
},
124 { &gEfiHashAlgorithmSha384Guid
, sizeof (EFI_SHA384_HASH2
), Sha384GetContextSize
, Sha384Init
, Sha384Update
, Sha384Final
},
125 { &gEfiHashAlgorithmSha512Guid
, sizeof (EFI_SHA512_HASH2
), Sha512GetContextSize
, Sha512Init
, Sha512Update
, Sha512Final
},
129 Returns the size of the hash which results from a specific algorithm.
131 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
132 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
133 @param[out] HashSize Holds the returned size of the algorithm's hash.
135 @retval EFI_SUCCESS Hash size returned successfully.
136 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.
137 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
138 or HashAlgorithm is null.
143 BaseCrypto2GetHashSize (
144 IN CONST EFI_HASH2_PROTOCOL
*This
,
145 IN CONST EFI_GUID
*HashAlgorithm
,
150 Creates a hash for the specified message text. The hash is not extendable.
151 The output is final with any algorithm-required padding added by the function.
153 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
154 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
155 @param[in] Message Points to the start of the message.
156 @param[in] MessageSize The size of Message, in bytes.
157 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
158 returned by GetHashSize() for the specified HashAlgorithm.
159 On output, the buffer holds the resulting hash computed from the message.
161 @retval EFI_SUCCESS Hash returned successfully.
162 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
163 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
164 or HashAlgorithm is Null.
165 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
166 or MessageSize is greater than platform maximum.
172 IN CONST EFI_HASH2_PROTOCOL
*This
,
173 IN CONST EFI_GUID
*HashAlgorithm
,
174 IN CONST UINT8
*Message
,
175 IN UINTN MessageSize
,
176 IN OUT EFI_HASH2_OUTPUT
*Hash
180 This function must be called to initialize a digest calculation to be subsequently performed using the
181 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().
183 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
184 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
186 @retval EFI_SUCCESS Initialized successfully.
187 @retval EFI_INVALID_PARAMETER This is NULL.
188 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
189 or HashAlgorithm is Null.
190 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.
191 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),
192 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.
197 BaseCrypto2HashInit (
198 IN CONST EFI_HASH2_PROTOCOL
*This
,
199 IN CONST EFI_GUID
*HashAlgorithm
203 Updates the hash of a computation in progress by adding a message text.
205 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
206 @param[in] Message Points to the start of the message.
207 @param[in] MessageSize The size of Message, in bytes.
209 @retval EFI_SUCCESS Digest in progress updated successfully.
210 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
211 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
212 or MessageSize is greater than platform maximum.
213 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),
214 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.
219 BaseCrypto2HashUpdate (
220 IN CONST EFI_HASH2_PROTOCOL
*This
,
221 IN CONST UINT8
*Message
,
226 Finalizes a hash operation in progress and returns calculation result.
227 The output is final with any necessary padding added by the function.
228 The hash may not be further updated or extended after HashFinal().
230 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
231 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
232 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().
233 On output, the buffer holds the resulting hash computed from the message.
235 @retval EFI_SUCCESS Hash returned successfully.
236 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
237 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),
238 or the operation in progress was canceled by a call to Hash() on the same instance.
243 BaseCrypto2HashFinal (
244 IN CONST EFI_HASH2_PROTOCOL
*This
,
245 IN OUT EFI_HASH2_OUTPUT
*Hash
248 EFI_HASH2_PROTOCOL mHash2Protocol
= {
249 BaseCrypto2GetHashSize
,
252 BaseCrypto2HashUpdate
,
253 BaseCrypto2HashFinal
,
257 Returns hash information.
259 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
261 @return Hash information.
265 IN CONST EFI_GUID
*HashAlgorithm
270 for (Index
= 0; Index
< sizeof (mHashInfo
)/sizeof (mHashInfo
[0]); Index
++) {
271 if (CompareGuid (HashAlgorithm
, mHashInfo
[Index
].Guid
)) {
272 return &mHashInfo
[Index
];
280 Returns the size of the hash which results from a specific algorithm.
282 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
283 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
284 @param[out] HashSize Holds the returned size of the algorithm's hash.
286 @retval EFI_SUCCESS Hash size returned successfully.
287 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.
288 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
289 or HashAlgorithm is null.
294 BaseCrypto2GetHashSize (
295 IN CONST EFI_HASH2_PROTOCOL
*This
,
296 IN CONST EFI_GUID
*HashAlgorithm
,
300 EFI_HASH_INFO
*HashInfo
;
302 if ((This
== NULL
) || (HashSize
== NULL
)) {
303 return EFI_INVALID_PARAMETER
;
306 if (HashAlgorithm
== NULL
) {
307 return EFI_UNSUPPORTED
;
310 HashInfo
= GetHashInfo (HashAlgorithm
);
311 if (HashInfo
== NULL
) {
312 return EFI_UNSUPPORTED
;
315 *HashSize
= HashInfo
->HashSize
;
320 Creates a hash for the specified message text. The hash is not extendable.
321 The output is final with any algorithm-required padding added by the function.
323 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
324 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
325 @param[in] Message Points to the start of the message.
326 @param[in] MessageSize The size of Message, in bytes.
327 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
328 returned by GetHashSize() for the specified HashAlgorithm.
329 On output, the buffer holds the resulting hash computed from the message.
331 @retval EFI_SUCCESS Hash returned successfully.
332 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
333 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
334 or HashAlgorithm is Null.
335 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
336 or MessageSize is greater than platform maximum.
342 IN CONST EFI_HASH2_PROTOCOL
*This
,
343 IN CONST EFI_GUID
*HashAlgorithm
,
344 IN CONST UINT8
*Message
,
345 IN UINTN MessageSize
,
346 IN OUT EFI_HASH2_OUTPUT
*Hash
349 EFI_HASH_INFO
*HashInfo
;
354 HASH2_INSTANCE_DATA
*Instance
;
356 Status
= EFI_SUCCESS
;
358 if ((This
== NULL
) || (Hash
== NULL
)) {
359 return EFI_INVALID_PARAMETER
;
362 if (HashAlgorithm
== NULL
) {
363 return EFI_UNSUPPORTED
;
366 HashInfo
= GetHashInfo (HashAlgorithm
);
367 if (HashInfo
== NULL
) {
368 return EFI_UNSUPPORTED
;
371 Instance
= HASH2_INSTANCE_DATA_FROM_THIS (This
);
372 if (Instance
->HashContext
!= NULL
) {
373 FreePool (Instance
->HashContext
);
376 Instance
->HashInfoContext
= NULL
;
377 Instance
->HashContext
= NULL
;
380 // Start hash sequence
382 CtxSize
= HashInfo
->GetContextSize ();
384 return EFI_UNSUPPORTED
;
387 HashCtx
= AllocatePool (CtxSize
);
388 if (HashCtx
== NULL
) {
389 return EFI_OUT_OF_RESOURCES
;
392 Ret
= HashInfo
->Init (HashCtx
);
394 Status
= EFI_OUT_OF_RESOURCES
;
401 Instance
->HashContext
= HashCtx
;
402 Instance
->HashInfoContext
= HashInfo
;
404 Ret
= HashInfo
->Update (HashCtx
, Message
, MessageSize
);
406 Status
= EFI_OUT_OF_RESOURCES
;
410 Ret
= HashInfo
->Final (HashCtx
, (UINT8
*)Hash
->Sha1Hash
);
412 Status
= EFI_OUT_OF_RESOURCES
;
418 // Cleanup the context
421 Instance
->HashInfoContext
= NULL
;
422 Instance
->HashContext
= NULL
;
427 This function must be called to initialize a digest calculation to be subsequently performed using the
428 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().
430 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
431 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
433 @retval EFI_SUCCESS Initialized successfully.
434 @retval EFI_INVALID_PARAMETER This is NULL.
435 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
436 or HashAlgorithm is Null.
437 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.
438 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),
439 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.
444 BaseCrypto2HashInit (
445 IN CONST EFI_HASH2_PROTOCOL
*This
,
446 IN CONST EFI_GUID
*HashAlgorithm
449 EFI_HASH_INFO
*HashInfo
;
453 HASH2_INSTANCE_DATA
*Instance
;
456 return EFI_INVALID_PARAMETER
;
459 if (HashAlgorithm
== NULL
) {
460 return EFI_UNSUPPORTED
;
463 HashInfo
= GetHashInfo (HashAlgorithm
);
464 if (HashInfo
== NULL
) {
465 return EFI_UNSUPPORTED
;
471 Instance
= HASH2_INSTANCE_DATA_FROM_THIS (This
);
472 if ((Instance
->HashContext
!= NULL
) || (Instance
->HashInfoContext
!= NULL
)) {
473 return EFI_ALREADY_STARTED
;
477 // Start hash sequence
479 CtxSize
= HashInfo
->GetContextSize ();
481 return EFI_UNSUPPORTED
;
484 HashCtx
= AllocatePool (CtxSize
);
485 if (HashCtx
== NULL
) {
486 return EFI_OUT_OF_RESOURCES
;
489 Ret
= HashInfo
->Init (HashCtx
);
492 return EFI_OUT_OF_RESOURCES
;
498 Instance
->HashContext
= HashCtx
;
499 Instance
->HashInfoContext
= HashInfo
;
500 Instance
->Updated
= FALSE
;
506 Updates the hash of a computation in progress by adding a message text.
508 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
509 @param[in] Message Points to the start of the message.
510 @param[in] MessageSize The size of Message, in bytes.
512 @retval EFI_SUCCESS Digest in progress updated successfully.
513 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
514 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
515 or MessageSize is greater than platform maximum.
516 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),
517 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.
522 BaseCrypto2HashUpdate (
523 IN CONST EFI_HASH2_PROTOCOL
*This
,
524 IN CONST UINT8
*Message
,
528 EFI_HASH_INFO
*HashInfo
;
531 HASH2_INSTANCE_DATA
*Instance
;
534 return EFI_INVALID_PARAMETER
;
540 Instance
= HASH2_INSTANCE_DATA_FROM_THIS (This
);
541 if ((Instance
->HashContext
== NULL
) || (Instance
->HashInfoContext
== NULL
)) {
542 return EFI_NOT_READY
;
545 HashInfo
= Instance
->HashInfoContext
;
546 HashCtx
= Instance
->HashContext
;
548 Ret
= HashInfo
->Update (HashCtx
, Message
, MessageSize
);
550 return EFI_OUT_OF_RESOURCES
;
553 Instance
->Updated
= TRUE
;
559 Finalizes a hash operation in progress and returns calculation result.
560 The output is final with any necessary padding added by the function.
561 The hash may not be further updated or extended after HashFinal().
563 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
564 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
565 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().
566 On output, the buffer holds the resulting hash computed from the message.
568 @retval EFI_SUCCESS Hash returned successfully.
569 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
570 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),
571 or the operation in progress was canceled by a call to Hash() on the same instance.
576 BaseCrypto2HashFinal (
577 IN CONST EFI_HASH2_PROTOCOL
*This
,
578 IN OUT EFI_HASH2_OUTPUT
*Hash
581 EFI_HASH_INFO
*HashInfo
;
584 HASH2_INSTANCE_DATA
*Instance
;
586 if ((This
== NULL
) || (Hash
== NULL
)) {
587 return EFI_INVALID_PARAMETER
;
593 Instance
= HASH2_INSTANCE_DATA_FROM_THIS (This
);
594 if ((Instance
->HashContext
== NULL
) || (Instance
->HashInfoContext
== NULL
) ||
595 (!Instance
->Updated
))
597 return EFI_NOT_READY
;
600 HashInfo
= Instance
->HashInfoContext
;
601 HashCtx
= Instance
->HashContext
;
603 Ret
= HashInfo
->Final (HashCtx
, (UINT8
*)Hash
->Sha1Hash
);
606 // Cleanup the context
609 Instance
->HashInfoContext
= NULL
;
610 Instance
->HashContext
= NULL
;
611 Instance
->Updated
= FALSE
;
614 return EFI_OUT_OF_RESOURCES
;