]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha1.c
2 HMAC-SHA1 Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalCryptLib.h"
10 #include <openssl/hmac.h>
13 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA1 use.
15 @return Pointer to the HMAC_CTX context that has been initialized.
16 If the allocations fails, HmacSha1New() returns NULL.
26 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
28 return (VOID
*) HMAC_CTX_new ();
32 Release the specified HMAC_CTX context.
34 @param[in] HmacSha1Ctx Pointer to the HMAC_CTX context to be released.
44 // Free OpenSSL HMAC_CTX Context
46 HMAC_CTX_free ((HMAC_CTX
*)HmacSha1Ctx
);
50 Set user-supplied key for subsequent use. It must be done before any
51 calling to HmacSha1Update().
53 If HmacSha1Context is NULL, then return FALSE.
55 @param[out] HmacSha1Context Pointer to HMAC-SHA1 context.
56 @param[in] Key Pointer to the user-supplied key.
57 @param[in] KeySize Key size in bytes.
59 @retval TRUE The Key is set successfully.
60 @retval FALSE The Key is set unsuccessfully.
66 OUT VOID
*HmacSha1Context
,
72 // Check input parameters.
74 if (HmacSha1Context
== NULL
|| KeySize
> INT_MAX
) {
78 if (HMAC_Init_ex ((HMAC_CTX
*)HmacSha1Context
, Key
, (UINT32
) KeySize
, EVP_sha1(), NULL
) != 1) {
86 Makes a copy of an existing HMAC-SHA1 context.
88 If HmacSha1Context is NULL, then return FALSE.
89 If NewHmacSha1Context is NULL, then return FALSE.
91 @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.
92 @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.
94 @retval TRUE HMAC-SHA1 context copy succeeded.
95 @retval FALSE HMAC-SHA1 context copy failed.
101 IN CONST VOID
*HmacSha1Context
,
102 OUT VOID
*NewHmacSha1Context
106 // Check input parameters.
108 if (HmacSha1Context
== NULL
|| NewHmacSha1Context
== NULL
) {
112 if (HMAC_CTX_copy ((HMAC_CTX
*)NewHmacSha1Context
, (HMAC_CTX
*)HmacSha1Context
) != 1) {
120 Digests the input data and updates HMAC-SHA1 context.
122 This function performs HMAC-SHA1 digest on a data buffer of the specified size.
123 It can be called multiple times to compute the digest of long or discontinuous data streams.
124 HMAC-SHA1 context should be initialized by HmacSha1New(), and should not be finalized by
125 HmacSha1Final(). Behavior with invalid context is undefined.
127 If HmacSha1Context is NULL, then return FALSE.
129 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
130 @param[in] Data Pointer to the buffer containing the data to be digested.
131 @param[in] DataSize Size of Data buffer in bytes.
133 @retval TRUE HMAC-SHA1 data digest succeeded.
134 @retval FALSE HMAC-SHA1 data digest failed.
140 IN OUT VOID
*HmacSha1Context
,
146 // Check input parameters.
148 if (HmacSha1Context
== NULL
) {
153 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
155 if (Data
== NULL
&& DataSize
!= 0) {
160 // OpenSSL HMAC-SHA1 digest update
162 if (HMAC_Update ((HMAC_CTX
*)HmacSha1Context
, Data
, DataSize
) != 1) {
170 Completes computation of the HMAC-SHA1 digest value.
172 This function completes HMAC-SHA1 digest computation and retrieves the digest value into
173 the specified memory. After this function has been called, the HMAC-SHA1 context cannot
175 HMAC-SHA1 context should be initialized by HmacSha1New(), and should not be finalized by
176 HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.
178 If HmacSha1Context is NULL, then return FALSE.
179 If HmacValue is NULL, then return FALSE.
181 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
182 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest
185 @retval TRUE HMAC-SHA1 digest computation succeeded.
186 @retval FALSE HMAC-SHA1 digest computation failed.
192 IN OUT VOID
*HmacSha1Context
,
199 // Check input parameters.
201 if (HmacSha1Context
== NULL
|| HmacValue
== NULL
) {
206 // OpenSSL HMAC-SHA1 digest finalization
208 if (HMAC_Final ((HMAC_CTX
*)HmacSha1Context
, HmacValue
, &Length
) != 1) {
211 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacSha1Context
) != 1) {