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 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Protocol/Hash2.h>
18 #include <Library/BaseLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/BaseCryptLib.h>
28 Retrieves the size, in bytes, of the context buffer required for hash operations.
30 If this interface is not supported, then return zero.
32 @return The size, in bytes, of the context buffer required for hash operations.
33 @retval 0 This interface is not supported.
38 (EFIAPI
*EFI_HASH_GET_CONTEXT_SIZE
) (
43 Initializes user-supplied memory pointed by Sha1Context as hash context for
46 If HashContext is NULL, then return FALSE.
47 If this interface is not supported, then return FALSE.
49 @param[out] HashContext Pointer to Hashcontext being initialized.
51 @retval TRUE Hash context initialization succeeded.
52 @retval FALSE Hash context initialization failed.
53 @retval FALSE This interface is not supported.
58 (EFIAPI
*EFI_HASH_INIT
) (
63 Digests the input data and updates Hash context.
65 This function performs Hash digest on a data buffer of the specified size.
66 It can be called multiple times to compute the digest of long or discontinuous data streams.
67 Hash context should be already correctly initialized by HashInit(), and should not be finalized
68 by HashFinal(). Behavior with invalid context is undefined.
70 If HashContext is NULL, then return FALSE.
71 If this interface is not supported, then return FALSE.
73 @param[in, out] HashContext Pointer to the Hash context.
74 @param[in] Data Pointer to the buffer containing the data to be hashed.
75 @param[in] DataSize Size of Data buffer in bytes.
77 @retval TRUE SHA-1 data digest succeeded.
78 @retval FALSE SHA-1 data digest failed.
79 @retval FALSE This interface is not supported.
84 (EFIAPI
*EFI_HASH_UPDATE
) (
85 IN OUT VOID
*HashContext
,
91 Completes computation of the Hash digest value.
93 This function completes hash computation and retrieves the digest value into
94 the specified memory. After this function has been called, the Hash context cannot
96 Hash context should be already correctly intialized by HashInit(), and should not be
97 finalized by HashFinal(). Behavior with invalid Hash context is undefined.
99 If HashContext is NULL, then return FALSE.
100 If HashValue is NULL, then return FALSE.
101 If this interface is not supported, then return FALSE.
103 @param[in, out] HashContext Pointer to the Hash context.
104 @param[out] HashValue Pointer to a buffer that receives the Hash digest
107 @retval TRUE Hash digest computation succeeded.
108 @retval FALSE Hash digest computation failed.
109 @retval FALSE This interface is not supported.
114 (EFIAPI
*EFI_HASH_FINAL
) (
115 IN OUT VOID
*HashContext
,
122 EFI_HASH_GET_CONTEXT_SIZE GetContextSize
;
124 EFI_HASH_UPDATE Update
;
125 EFI_HASH_FINAL Final
;
128 EFI_HASH_INFO mHashInfo
[] = {
129 {&gEfiHashAlgorithmMD5Guid
, sizeof(EFI_MD5_HASH2
), Md5GetContextSize
, Md5Init
, Md5Update
, Md5Final
},
130 {&gEfiHashAlgorithmSha1Guid
, sizeof(EFI_SHA1_HASH2
), Sha1GetContextSize
, Sha1Init
, Sha1Update
, Sha1Final
},
131 {&gEfiHashAlgorithmSha256Guid
, sizeof(EFI_SHA256_HASH2
), Sha256GetContextSize
, Sha256Init
, Sha256Update
, Sha256Final
},
132 {&gEfiHashAlgorithmSha384Guid
, sizeof(EFI_SHA384_HASH2
), Sha384GetContextSize
, Sha384Init
, Sha384Update
, Sha384Final
},
133 {&gEfiHashAlgorithmSha512Guid
, sizeof(EFI_SHA512_HASH2
), Sha512GetContextSize
, Sha512Init
, Sha512Update
, Sha512Final
},
137 Returns the size of the hash which results from a specific algorithm.
139 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
140 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
141 @param[out] HashSize Holds the returned size of the algorithm's hash.
143 @retval EFI_SUCCESS Hash size returned successfully.
144 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.
145 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
146 or HashAlgorithm is null.
151 BaseCrypto2GetHashSize (
152 IN CONST EFI_HASH2_PROTOCOL
*This
,
153 IN CONST EFI_GUID
*HashAlgorithm
,
158 Creates a hash for the specified message text. The hash is not extendable.
159 The output is final with any algorithm-required padding added by the function.
161 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
162 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
163 @param[in] Message Points to the start of the message.
164 @param[in] MessageSize The size of Message, in bytes.
165 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
166 returned by GetHashSize() for the specified HashAlgorithm.
167 On output, the buffer holds the resulting hash computed from the message.
169 @retval EFI_SUCCESS Hash returned successfully.
170 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
171 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
172 or HashAlgorithm is Null.
173 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
174 or MessageSize is greater than platform maximum.
180 IN CONST EFI_HASH2_PROTOCOL
*This
,
181 IN CONST EFI_GUID
*HashAlgorithm
,
182 IN CONST UINT8
*Message
,
183 IN UINTN MessageSize
,
184 IN OUT EFI_HASH2_OUTPUT
*Hash
188 This function must be called to initialize a digest calculation to be subsequently performed using the
189 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().
191 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
192 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
194 @retval EFI_SUCCESS Initialized successfully.
195 @retval EFI_INVALID_PARAMETER This is NULL.
196 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
197 or HashAlgorithm is Null.
198 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.
199 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),
200 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.
205 BaseCrypto2HashInit (
206 IN CONST EFI_HASH2_PROTOCOL
*This
,
207 IN CONST EFI_GUID
*HashAlgorithm
211 Updates the hash of a computation in progress by adding a message text.
213 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
214 @param[in] Message Points to the start of the message.
215 @param[in] MessageSize The size of Message, in bytes.
217 @retval EFI_SUCCESS Digest in progress updated successfully.
218 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
219 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
220 or MessageSize is greater than platform maximum.
221 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),
222 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.
227 BaseCrypto2HashUpdate (
228 IN CONST EFI_HASH2_PROTOCOL
*This
,
229 IN CONST UINT8
*Message
,
234 Finalizes a hash operation in progress and returns calculation result.
235 The output is final with any necessary padding added by the function.
236 The hash may not be further updated or extended after HashFinal().
238 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
239 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
240 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().
241 On output, the buffer holds the resulting hash computed from the message.
243 @retval EFI_SUCCESS Hash returned successfully.
244 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
245 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),
246 or the operation in progress was canceled by a call to Hash() on the same instance.
251 BaseCrypto2HashFinal (
252 IN CONST EFI_HASH2_PROTOCOL
*This
,
253 IN OUT EFI_HASH2_OUTPUT
*Hash
256 EFI_HASH2_PROTOCOL mHash2Protocol
= {
257 BaseCrypto2GetHashSize
,
260 BaseCrypto2HashUpdate
,
261 BaseCrypto2HashFinal
,
265 Returns hash information.
267 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
269 @return Hash information.
273 IN CONST EFI_GUID
*HashAlgorithm
278 for (Index
= 0; Index
< sizeof(mHashInfo
)/sizeof(mHashInfo
[0]); Index
++) {
279 if (CompareGuid (HashAlgorithm
, mHashInfo
[Index
].Guid
)) {
280 return &mHashInfo
[Index
];
287 Returns the size of the hash which results from a specific algorithm.
289 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
290 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
291 @param[out] HashSize Holds the returned size of the algorithm's hash.
293 @retval EFI_SUCCESS Hash size returned successfully.
294 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.
295 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
296 or HashAlgorithm is null.
301 BaseCrypto2GetHashSize (
302 IN CONST EFI_HASH2_PROTOCOL
*This
,
303 IN CONST EFI_GUID
*HashAlgorithm
,
307 EFI_HASH_INFO
*HashInfo
;
309 if ((This
== NULL
) || (HashSize
== NULL
)) {
310 return EFI_INVALID_PARAMETER
;
313 if (HashAlgorithm
== NULL
) {
314 return EFI_UNSUPPORTED
;
317 HashInfo
= GetHashInfo (HashAlgorithm
);
318 if (HashInfo
== NULL
) {
319 return EFI_UNSUPPORTED
;
322 *HashSize
= HashInfo
->HashSize
;
327 Creates a hash for the specified message text. The hash is not extendable.
328 The output is final with any algorithm-required padding added by the function.
330 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
331 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
332 @param[in] Message Points to the start of the message.
333 @param[in] MessageSize The size of Message, in bytes.
334 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
335 returned by GetHashSize() for the specified HashAlgorithm.
336 On output, the buffer holds the resulting hash computed from the message.
338 @retval EFI_SUCCESS Hash returned successfully.
339 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
340 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
341 or HashAlgorithm is Null.
342 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
343 or MessageSize is greater than platform maximum.
349 IN CONST EFI_HASH2_PROTOCOL
*This
,
350 IN CONST EFI_GUID
*HashAlgorithm
,
351 IN CONST UINT8
*Message
,
352 IN UINTN MessageSize
,
353 IN OUT EFI_HASH2_OUTPUT
*Hash
356 EFI_HASH_INFO
*HashInfo
;
361 HASH2_INSTANCE_DATA
*Instance
;
363 Status
= EFI_SUCCESS
;
365 if ((This
== NULL
) || (Hash
== NULL
)) {
366 return EFI_INVALID_PARAMETER
;
369 if (HashAlgorithm
== NULL
) {
370 return EFI_UNSUPPORTED
;
373 HashInfo
= GetHashInfo (HashAlgorithm
);
374 if (HashInfo
== NULL
) {
375 return EFI_UNSUPPORTED
;
378 Instance
= HASH2_INSTANCE_DATA_FROM_THIS(This
);
379 if (Instance
->HashContext
!= NULL
) {
380 FreePool (Instance
->HashContext
);
382 Instance
->HashInfoContext
= NULL
;
383 Instance
->HashContext
= NULL
;
386 // Start hash sequence
388 CtxSize
= HashInfo
->GetContextSize ();
390 return EFI_UNSUPPORTED
;
392 HashCtx
= AllocatePool (CtxSize
);
393 if (HashCtx
== NULL
) {
394 return EFI_OUT_OF_RESOURCES
;
397 Ret
= HashInfo
->Init (HashCtx
);
399 Status
= EFI_OUT_OF_RESOURCES
;
406 Instance
->HashContext
= HashCtx
;
407 Instance
->HashInfoContext
= HashInfo
;
409 Ret
= HashInfo
->Update (HashCtx
, Message
, MessageSize
);
411 Status
= EFI_OUT_OF_RESOURCES
;
415 Ret
= HashInfo
->Final (HashCtx
, (UINT8
*)Hash
->Sha1Hash
);
417 Status
= EFI_OUT_OF_RESOURCES
;
422 // Cleanup the context
425 Instance
->HashInfoContext
= NULL
;
426 Instance
->HashContext
= NULL
;
431 This function must be called to initialize a digest calculation to be subsequently performed using the
432 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().
434 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
435 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
437 @retval EFI_SUCCESS Initialized successfully.
438 @retval EFI_INVALID_PARAMETER This is NULL.
439 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
440 or HashAlgorithm is Null.
441 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.
442 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),
443 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.
448 BaseCrypto2HashInit (
449 IN CONST EFI_HASH2_PROTOCOL
*This
,
450 IN CONST EFI_GUID
*HashAlgorithm
453 EFI_HASH_INFO
*HashInfo
;
457 HASH2_INSTANCE_DATA
*Instance
;
460 return EFI_INVALID_PARAMETER
;
463 if (HashAlgorithm
== NULL
) {
464 return EFI_UNSUPPORTED
;
467 HashInfo
= GetHashInfo (HashAlgorithm
);
468 if (HashInfo
== NULL
) {
469 return EFI_UNSUPPORTED
;
475 Instance
= HASH2_INSTANCE_DATA_FROM_THIS(This
);
476 if ((Instance
->HashContext
!= NULL
) || (Instance
->HashInfoContext
!= NULL
)) {
477 return EFI_ALREADY_STARTED
;
481 // Start hash sequence
483 CtxSize
= HashInfo
->GetContextSize ();
485 return EFI_UNSUPPORTED
;
487 HashCtx
= AllocatePool (CtxSize
);
488 if (HashCtx
== NULL
) {
489 return EFI_OUT_OF_RESOURCES
;
492 Ret
= HashInfo
->Init (HashCtx
);
495 return EFI_OUT_OF_RESOURCES
;
501 Instance
->HashContext
= HashCtx
;
502 Instance
->HashInfoContext
= HashInfo
;
503 Instance
->Updated
= FALSE
;
509 Updates the hash of a computation in progress by adding a message text.
511 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
512 @param[in] Message Points to the start of the message.
513 @param[in] MessageSize The size of Message, in bytes.
515 @retval EFI_SUCCESS Digest in progress updated successfully.
516 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
517 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
518 or MessageSize is greater than platform maximum.
519 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),
520 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.
525 BaseCrypto2HashUpdate (
526 IN CONST EFI_HASH2_PROTOCOL
*This
,
527 IN CONST UINT8
*Message
,
531 EFI_HASH_INFO
*HashInfo
;
534 HASH2_INSTANCE_DATA
*Instance
;
537 return EFI_INVALID_PARAMETER
;
543 Instance
= HASH2_INSTANCE_DATA_FROM_THIS(This
);
544 if ((Instance
->HashContext
== NULL
) || (Instance
->HashInfoContext
== NULL
)) {
545 return EFI_NOT_READY
;
547 HashInfo
= Instance
->HashInfoContext
;
548 HashCtx
= Instance
->HashContext
;
550 Ret
= HashInfo
->Update (HashCtx
, Message
, MessageSize
);
552 return EFI_OUT_OF_RESOURCES
;
555 Instance
->Updated
= TRUE
;
561 Finalizes a hash operation in progress and returns calculation result.
562 The output is final with any necessary padding added by the function.
563 The hash may not be further updated or extended after HashFinal().
565 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
566 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
567 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().
568 On output, the buffer holds the resulting hash computed from the message.
570 @retval EFI_SUCCESS Hash returned successfully.
571 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
572 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),
573 or the operation in progress was canceled by a call to Hash() on the same instance.
578 BaseCrypto2HashFinal (
579 IN CONST EFI_HASH2_PROTOCOL
*This
,
580 IN OUT EFI_HASH2_OUTPUT
*Hash
583 EFI_HASH_INFO
*HashInfo
;
586 HASH2_INSTANCE_DATA
*Instance
;
588 if ((This
== NULL
) || (Hash
== NULL
)) {
589 return EFI_INVALID_PARAMETER
;
595 Instance
= HASH2_INSTANCE_DATA_FROM_THIS(This
);
596 if ((Instance
->HashContext
== NULL
) || (Instance
->HashInfoContext
== NULL
) ||
597 (!Instance
->Updated
)) {
598 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
;