2 Unified Hash API Implementation
4 This file implements the Unified Hash API.
6 This API, when called, will calculate the Hash using the
7 hashing algorithm specified by PcdHashApiLibPolicy.
9 Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
10 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include <IndustryStandard/Tpm20.h>
16 #include <Library/BaseLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/BaseCryptLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/HashApiLib.h>
25 Retrieves the size, in bytes, of the context buffer required for hash operations.
27 @return The size, in bytes, of the context buffer required for hash operations.
31 HashApiGetContextSize (
35 switch (PcdGet32 (PcdHashApiLibPolicy
)) {
37 return Sha1GetContextSize ();
41 return Sha256GetContextSize ();
45 return Sha384GetContextSize ();
49 return Sha512GetContextSize ();
52 case HASH_ALG_SM3_256
:
53 return Sm3GetContextSize ();
66 @param[out] HashContext Hash context.
68 @retval TRUE Hash start and HashHandle returned.
69 @retval FALSE Hash Init unsuccessful.
74 OUT HASH_API_CONTEXT HashContext
77 switch (PcdGet32 (PcdHashApiLibPolicy
)) {
79 return Sha1Init (HashContext
);
83 return Sha256Init (HashContext
);
87 return Sha384Init (HashContext
);
91 return Sha512Init (HashContext
);
94 case HASH_ALG_SM3_256
:
95 return Sm3Init (HashContext
);
106 Makes a copy of an existing hash context.
108 @param[in] HashContext Hash context.
109 @param[out] NewHashContext New copy of hash context.
111 @retval TRUE Hash context copy succeeded.
112 @retval FALSE Hash context copy failed.
117 IN HASH_API_CONTEXT HashContext
,
118 OUT HASH_API_CONTEXT NewHashContext
121 switch (PcdGet32 (PcdHashApiLibPolicy
)) {
123 return Sha1Duplicate (HashContext
, NewHashContext
);
126 case HASH_ALG_SHA256
:
127 return Sha256Duplicate (HashContext
, NewHashContext
);
130 case HASH_ALG_SHA384
:
131 return Sha384Duplicate (HashContext
, NewHashContext
);
134 case HASH_ALG_SHA512
:
135 return Sha512Duplicate (HashContext
, NewHashContext
);
138 case HASH_ALG_SM3_256
:
139 return Sm3Duplicate (HashContext
, NewHashContext
);
152 @param[in] HashContext Hash context.
153 @param[in] DataToHash Data to be hashed.
154 @param[in] DataToHashLen Data size.
156 @retval TRUE Hash updated.
157 @retval FALSE Hash updated unsuccessful.
162 IN HASH_API_CONTEXT HashContext
,
164 IN UINTN DataToHashLen
167 switch (PcdGet32 (PcdHashApiLibPolicy
)) {
169 return Sha1Update (HashContext
, DataToHash
, DataToHashLen
);
172 case HASH_ALG_SHA256
:
173 return Sha256Update (HashContext
, DataToHash
, DataToHashLen
);
176 case HASH_ALG_SHA384
:
177 return Sha384Update (HashContext
, DataToHash
, DataToHashLen
);
180 case HASH_ALG_SHA512
:
181 return Sha512Update (HashContext
, DataToHash
, DataToHashLen
);
184 case HASH_ALG_SM3_256
:
185 return Sm3Update (HashContext
, DataToHash
, DataToHashLen
);
198 @param[in] HashContext Hash context.
199 @param[out] Digest Hash Digest.
201 @retval TRUE Hash complete and Digest is returned.
202 @retval FALSE Hash complete unsuccessful.
207 IN HASH_API_CONTEXT HashContext
,
211 switch (PcdGet32 (PcdHashApiLibPolicy
)) {
213 return Sha1Final (HashContext
, Digest
);
216 case HASH_ALG_SHA256
:
217 return Sha256Final (HashContext
, Digest
);
220 case HASH_ALG_SHA384
:
221 return Sha384Final (HashContext
, Digest
);
224 case HASH_ALG_SHA512
:
225 return Sha512Final (HashContext
, Digest
);
228 case HASH_ALG_SM3_256
:
229 return Sm3Final (HashContext
, Digest
);
240 Computes hash message digest of a input data buffer.
242 @param[in] DataToHash Data to be hashed.
243 @param[in] DataToHashLen Data size.
244 @param[out] Digest Hash Digest.
246 @retval TRUE Hash digest computation succeeded.
247 @retval FALSE Hash digest computation failed.
252 IN CONST VOID
*DataToHash
,
253 IN UINTN DataToHashLen
,
257 switch (PcdGet32 (PcdHashApiLibPolicy
)) {
259 return Sha1HashAll (DataToHash
, DataToHashLen
, Digest
);
262 case HASH_ALG_SHA256
:
263 return Sha256HashAll (DataToHash
, DataToHashLen
, Digest
);
266 case HASH_ALG_SHA384
:
267 return Sha384HashAll (DataToHash
, DataToHashLen
, Digest
);
270 case HASH_ALG_SHA512
:
271 return Sha512HashAll (DataToHash
, DataToHashLen
, Digest
);
274 case HASH_ALG_SM3_256
:
275 return Sm3HashAll (DataToHash
, DataToHashLen
, Digest
);