2 This module implements Hash2 Protocol.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Protocol/Hash2.h>
17 #include <Library/BaseLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/BaseCryptLib.h>
27 Retrieves the size, in bytes, of the context buffer required for hash operations.
29 If this interface is not supported, then return zero.
31 @return The size, in bytes, of the context buffer required for hash operations.
32 @retval 0 This interface is not supported.
37 (EFIAPI
*EFI_HASH_GET_CONTEXT_SIZE
) (
42 Initializes user-supplied memory pointed by Sha1Context as hash context for
45 If HashContext is NULL, then return FALSE.
46 If this interface is not supported, then return FALSE.
48 @param[out] HashContext Pointer to Hashcontext being initialized.
50 @retval TRUE Hash context initialization succeeded.
51 @retval FALSE Hash context initialization failed.
52 @retval FALSE This interface is not supported.
57 (EFIAPI
*EFI_HASH_INIT
) (
62 Digests the input data and updates Hash context.
64 This function performs Hash digest on a data buffer of the specified size.
65 It can be called multiple times to compute the digest of long or discontinuous data streams.
66 Hash context should be already correctly intialized by HashInit(), and should not be finalized
67 by HashFinal(). Behavior with invalid context is undefined.
69 If HashContext is NULL, then return FALSE.
70 If this interface is not supported, then return FALSE.
72 @param[in, out] HashContext Pointer to the Hash context.
73 @param[in] Data Pointer to the buffer containing the data to be hashed.
74 @param[in] DataSize Size of Data buffer in bytes.
76 @retval TRUE SHA-1 data digest succeeded.
77 @retval FALSE SHA-1 data digest failed.
78 @retval FALSE This interface is not supported.
83 (EFIAPI
*EFI_HASH_UPDATE
) (
84 IN OUT VOID
*HashContext
,
90 Completes computation of the Hash digest value.
92 This function completes hash computation and retrieves the digest value into
93 the specified memory. After this function has been called, the Hash context cannot
95 Hash context should be already correctly intialized by HashInit(), and should not be
96 finalized by HashFinal(). Behavior with invalid Hash context is undefined.
98 If HashContext is NULL, then return FALSE.
99 If HashValue is NULL, then return FALSE.
100 If this interface is not supported, then return FALSE.
102 @param[in, out] HashContext Pointer to the Hash context.
103 @param[out] HashValue Pointer to a buffer that receives the Hash digest
106 @retval TRUE Hash digest computation succeeded.
107 @retval FALSE Hash digest computation failed.
108 @retval FALSE This interface is not supported.
113 (EFIAPI
*EFI_HASH_FINAL
) (
114 IN OUT VOID
*HashContext
,
121 EFI_HASH_GET_CONTEXT_SIZE GetContextSize
;
123 EFI_HASH_UPDATE Update
;
124 EFI_HASH_FINAL Final
;
127 EFI_HASH_INFO mHashInfo
[] = {
128 {&gEfiHashAlgorithmSha1Guid
, sizeof(EFI_SHA1_HASH2
), Sha1GetContextSize
, Sha1Init
, Sha1Update
, Sha1Final
},
129 {&gEfiHashAlgorithmSha256Guid
, sizeof(EFI_SHA256_HASH2
), Sha256GetContextSize
, Sha256Init
, Sha256Update
, Sha256Final
},
130 {&gEfiHashAlgorithmSha384Guid
, sizeof(EFI_SHA384_HASH2
), Sha384GetContextSize
, Sha384Init
, Sha384Update
, Sha384Final
},
131 {&gEfiHashAlgorithmSha512Guid
, sizeof(EFI_SHA512_HASH2
), Sha512GetContextSize
, Sha512Init
, Sha512Update
, Sha512Final
},
135 Returns the size of the hash which results from a specific algorithm.
137 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
138 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
139 @param[out] HashSize Holds the returned size of the algorithm's hash.
141 @retval EFI_SUCCESS Hash size returned successfully.
142 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.
143 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
144 or HashAlgorithm is null.
149 BaseCrypto2GetHashSize (
150 IN CONST EFI_HASH2_PROTOCOL
*This
,
151 IN CONST EFI_GUID
*HashAlgorithm
,
156 Creates a hash for the specified message text. The hash is not extendable.
157 The output is final with any algorithm-required padding added by the function.
159 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
160 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
161 @param[in] Message Points to the start of the message.
162 @param[in] MessageSize The size of Message, in bytes.
163 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
164 returned by GetHashSize() for the specified HashAlgorithm.
165 On output, the buffer holds the resulting hash computed from the message.
167 @retval EFI_SUCCESS Hash returned successfully.
168 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
169 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
170 or HashAlgorithm is Null.
171 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
172 or MessageSize is greater than platform maximum.
178 IN CONST EFI_HASH2_PROTOCOL
*This
,
179 IN CONST EFI_GUID
*HashAlgorithm
,
180 IN CONST UINT8
*Message
,
181 IN UINTN MessageSize
,
182 IN OUT EFI_HASH2_OUTPUT
*Hash
186 This function must be called to initialize a digest calculation to be subsequently performed using the
187 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().
189 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
190 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
192 @retval EFI_SUCCESS Initialized successfully.
193 @retval EFI_INVALID_PARAMETER This is NULL.
194 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
195 or HashAlgorithm is Null.
196 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.
197 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),
198 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.
203 BaseCrypto2HashInit (
204 IN CONST EFI_HASH2_PROTOCOL
*This
,
205 IN CONST EFI_GUID
*HashAlgorithm
209 Updates the hash of a computation in progress by adding a message text.
211 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
212 @param[in] Message Points to the start of the message.
213 @param[in] MessageSize The size of Message, in bytes.
215 @retval EFI_SUCCESS Digest in progress updated successfully.
216 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
217 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
218 or MessageSize is greater than platform maximum.
219 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),
220 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.
225 BaseCrypto2HashUpdate (
226 IN CONST EFI_HASH2_PROTOCOL
*This
,
227 IN CONST UINT8
*Message
,
232 Finalizes a hash operation in progress and returns calculation result.
233 The output is final with any necessary padding added by the function.
234 The hash may not be further updated or extended after HashFinal().
236 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
237 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
238 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().
239 On output, the buffer holds the resulting hash computed from the message.
241 @retval EFI_SUCCESS Hash returned successfully.
242 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
243 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),
244 or the operation in progress was canceled by a call to Hash() on the same instance.
249 BaseCrypto2HashFinal (
250 IN CONST EFI_HASH2_PROTOCOL
*This
,
251 IN OUT EFI_HASH2_OUTPUT
*Hash
254 EFI_HASH2_PROTOCOL mHash2Protocol
= {
255 BaseCrypto2GetHashSize
,
258 BaseCrypto2HashUpdate
,
259 BaseCrypto2HashFinal
,
263 Returns hash information.
265 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
267 @return Hash information.
271 IN CONST EFI_GUID
*HashAlgorithm
276 for (Index
= 0; Index
< sizeof(mHashInfo
)/sizeof(mHashInfo
[0]); Index
++) {
277 if (CompareGuid (HashAlgorithm
, mHashInfo
[Index
].Guid
)) {
278 return &mHashInfo
[Index
];
285 Returns the size of the hash which results from a specific algorithm.
287 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
288 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
289 @param[out] HashSize Holds the returned size of the algorithm's hash.
291 @retval EFI_SUCCESS Hash size returned successfully.
292 @retval EFI_INVALID_PARAMETER This or HashSize is NULL.
293 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
294 or HashAlgorithm is null.
299 BaseCrypto2GetHashSize (
300 IN CONST EFI_HASH2_PROTOCOL
*This
,
301 IN CONST EFI_GUID
*HashAlgorithm
,
305 EFI_HASH_INFO
*HashInfo
;
307 if ((This
== NULL
) || (HashSize
== NULL
)) {
308 return EFI_INVALID_PARAMETER
;
311 if (HashAlgorithm
== NULL
) {
312 return EFI_UNSUPPORTED
;
315 HashInfo
= GetHashInfo (HashAlgorithm
);
316 if (HashInfo
== NULL
) {
317 return EFI_UNSUPPORTED
;
320 *HashSize
= HashInfo
->HashSize
;
325 Creates a hash for the specified message text. The hash is not extendable.
326 The output is final with any algorithm-required padding added by the function.
328 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
329 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
330 @param[in] Message Points to the start of the message.
331 @param[in] MessageSize The size of Message, in bytes.
332 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
333 returned by GetHashSize() for the specified HashAlgorithm.
334 On output, the buffer holds the resulting hash computed from the message.
336 @retval EFI_SUCCESS Hash returned successfully.
337 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
338 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
339 or HashAlgorithm is Null.
340 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
341 or MessageSize is greater than platform maximum.
347 IN CONST EFI_HASH2_PROTOCOL
*This
,
348 IN CONST EFI_GUID
*HashAlgorithm
,
349 IN CONST UINT8
*Message
,
350 IN UINTN MessageSize
,
351 IN OUT EFI_HASH2_OUTPUT
*Hash
354 EFI_HASH_INFO
*HashInfo
;
360 Status
= EFI_SUCCESS
;
362 if ((This
== NULL
) || (Hash
== NULL
)) {
363 return EFI_INVALID_PARAMETER
;
366 if (HashAlgorithm
== NULL
) {
367 return EFI_UNSUPPORTED
;
370 HashInfo
= GetHashInfo (HashAlgorithm
);
371 if (HashInfo
== NULL
) {
372 return EFI_UNSUPPORTED
;
376 // Start hash sequence
378 CtxSize
= HashInfo
->GetContextSize ();
380 return EFI_UNSUPPORTED
;
382 HashCtx
= AllocatePool (CtxSize
);
383 if (HashCtx
== NULL
) {
384 return EFI_OUT_OF_RESOURCES
;
387 Ret
= HashInfo
->Init (HashCtx
);
389 Status
= EFI_OUT_OF_RESOURCES
;
393 Ret
= HashInfo
->Update (HashCtx
, Message
, MessageSize
);
395 Status
= EFI_OUT_OF_RESOURCES
;
399 Ret
= HashInfo
->Final (HashCtx
, (UINT8
*)Hash
->Sha1Hash
);
401 Status
= EFI_OUT_OF_RESOURCES
;
410 This function must be called to initialize a digest calculation to be subsequently performed using the
411 EFI_HASH2_PROTOCOL functions HashUpdate() and HashFinal().
413 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
414 @param[in] HashAlgorithm Points to the EFI_GUID which identifies the algorithm to use.
416 @retval EFI_SUCCESS Initialized successfully.
417 @retval EFI_INVALID_PARAMETER This is NULL.
418 @retval EFI_UNSUPPORTED The algorithm specified by HashAlgorithm is not supported by this driver
419 or HashAlgorithm is Null.
420 @retval EFI_OUT_OF_RESOURCES Process failed due to lack of required resource.
421 @retval EFI_ALREADY_STARTED This function is called when the operation in progress is still in processing Hash(),
422 or HashInit() is already called before and not terminated by HashFinal() yet on the same instance.
427 BaseCrypto2HashInit (
428 IN CONST EFI_HASH2_PROTOCOL
*This
,
429 IN CONST EFI_GUID
*HashAlgorithm
432 EFI_HASH_INFO
*HashInfo
;
436 HASH2_INSTANCE_DATA
*Instance
;
439 return EFI_INVALID_PARAMETER
;
442 if (HashAlgorithm
== NULL
) {
443 return EFI_UNSUPPORTED
;
446 HashInfo
= GetHashInfo (HashAlgorithm
);
447 if (HashInfo
== NULL
) {
448 return EFI_UNSUPPORTED
;
454 Instance
= HASH2_INSTANCE_DATA_FROM_THIS(This
);
455 if ((Instance
->HashContext
!= NULL
) || (Instance
->HashInfoContext
!= HashInfo
)) {
456 return EFI_ALREADY_STARTED
;
460 // Start hash sequence
462 CtxSize
= HashInfo
->GetContextSize ();
464 return EFI_UNSUPPORTED
;
466 HashCtx
= AllocatePool (CtxSize
);
467 if (HashCtx
== NULL
) {
468 return EFI_OUT_OF_RESOURCES
;
471 Ret
= HashInfo
->Init (HashCtx
);
474 return EFI_OUT_OF_RESOURCES
;
480 Instance
->HashContext
= HashCtx
;
481 Instance
->HashInfoContext
= HashInfo
;
487 Updates the hash of a computation in progress by adding a message text.
489 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
490 @param[in] Message Points to the start of the message.
491 @param[in] MessageSize The size of Message, in bytes.
493 @retval EFI_SUCCESS Digest in progress updated successfully.
494 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
495 @retval EFI_OUT_OF_RESOURCES Some resource required by the function is not available
496 or MessageSize is greater than platform maximum.
497 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit(),
498 or the operation in progress was terminated by a call to Hash() or HashFinal() on the same instance.
503 BaseCrypto2HashUpdate (
504 IN CONST EFI_HASH2_PROTOCOL
*This
,
505 IN CONST UINT8
*Message
,
509 EFI_HASH_INFO
*HashInfo
;
512 HASH2_INSTANCE_DATA
*Instance
;
515 return EFI_INVALID_PARAMETER
;
521 Instance
= HASH2_INSTANCE_DATA_FROM_THIS(This
);
522 if ((Instance
->HashContext
== NULL
) || (Instance
->HashInfoContext
== NULL
)) {
523 return EFI_NOT_READY
;
525 HashInfo
= Instance
->HashInfoContext
;
526 HashCtx
= Instance
->HashContext
;
528 Ret
= HashInfo
->Update (HashCtx
, Message
, MessageSize
);
530 return EFI_OUT_OF_RESOURCES
;
537 Finalizes a hash operation in progress and returns calculation result.
538 The output is final with any necessary padding added by the function.
539 The hash may not be further updated or extended after HashFinal().
541 @param[in] This Points to this instance of EFI_HASH2_PROTOCOL.
542 @param[in,out] Hash On input, points to a caller-allocated buffer of the size
543 returned by GetHashSize() for the specified HashAlgorithm specified in preceding HashInit().
544 On output, the buffer holds the resulting hash computed from the message.
546 @retval EFI_SUCCESS Hash returned successfully.
547 @retval EFI_INVALID_PARAMETER This or Hash is NULL.
548 @retval EFI_NOT_READY This call was not preceded by a valid call to HashInit() and at least one call to HashUpdate(),
549 or the operation in progress was canceled by a call to Hash() on the same instance.
554 BaseCrypto2HashFinal (
555 IN CONST EFI_HASH2_PROTOCOL
*This
,
556 IN OUT EFI_HASH2_OUTPUT
*Hash
559 EFI_HASH_INFO
*HashInfo
;
562 HASH2_INSTANCE_DATA
*Instance
;
564 if ((This
== NULL
) || (Hash
== NULL
)) {
565 return EFI_INVALID_PARAMETER
;
571 Instance
= HASH2_INSTANCE_DATA_FROM_THIS(This
);
572 if ((Instance
->HashContext
== NULL
) || (Instance
->HashInfoContext
== NULL
)) {
573 return EFI_NOT_READY
;
575 HashInfo
= Instance
->HashInfoContext
;
576 HashCtx
= Instance
->HashContext
;
578 Ret
= HashInfo
->Final (HashCtx
, (UINT8
*)Hash
->Sha1Hash
);
581 // Cleanup the context
584 Instance
->HashInfoContext
= NULL
;
585 Instance
->HashContext
= NULL
;
588 return EFI_OUT_OF_RESOURCES
;