]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha1.c
8126fb525f3586f911f8812ff95d09891313a72f
2 HMAC-SHA1 Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalCryptLib.h"
10 #include <openssl/hmac.h>
13 // NOTE: OpenSSL redefines the size of HMAC_CTX at crypto/hmac/hmac_lcl.h
14 // #define HMAC_MAX_MD_CBLOCK_SIZE 144
17 #define HMAC_SHA1_CTX_SIZE (sizeof(void *) * 4 + sizeof(unsigned int) + \
18 sizeof(unsigned char) * 144)
21 Retrieves the size, in bytes, of the context buffer required for HMAC-SHA1 operations.
22 (NOTE: This API is deprecated.
23 Use HmacSha1New() / HmacSha1Free() for HMAC-SHA1 Context operations.)
25 @return The size, in bytes, of the context buffer required for HMAC-SHA1 operations.
30 HmacSha1GetContextSize (
35 // Retrieves the OpenSSL HMAC-SHA1 Context Size
36 // NOTE: HMAC_CTX object was made opaque in openssl-1.1.x, here we just use the
37 // fixed size as a workaround to make this API work for compatibility.
38 // We should retire HmacSha15GetContextSize() in future, and use HmacSha1New()
39 // and HmacSha1Free() for context allocation and release.
41 return (UINTN
) HMAC_SHA1_CTX_SIZE
;
45 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA1 use.
47 @return Pointer to the HMAC_CTX context that has been initialized.
48 If the allocations fails, HmacSha1New() returns NULL.
58 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
60 return (VOID
*) HMAC_CTX_new ();
64 Release the specified HMAC_CTX context.
66 @param[in] HmacSha1Ctx Pointer to the HMAC_CTX context to be released.
76 // Free OpenSSL HMAC_CTX Context
78 HMAC_CTX_free ((HMAC_CTX
*)HmacSha1Ctx
);
82 Set user-supplied key for subsequent use. It must be done before any
83 calling to HmacSha1Update().
85 If HmacSha1Context is NULL, then return FALSE.
87 @param[out] HmacSha1Context Pointer to HMAC-SHA1 context.
88 @param[in] Key Pointer to the user-supplied key.
89 @param[in] KeySize Key size in bytes.
91 @retval TRUE The Key is set successfully.
92 @retval FALSE The Key is set unsuccessfully.
98 OUT VOID
*HmacSha1Context
,
104 // Check input parameters.
106 if (HmacSha1Context
== NULL
|| KeySize
> INT_MAX
) {
110 if (HMAC_Init_ex ((HMAC_CTX
*)HmacSha1Context
, Key
, (UINT32
) KeySize
, EVP_sha1(), NULL
) != 1) {
118 Makes a copy of an existing HMAC-SHA1 context.
120 If HmacSha1Context is NULL, then return FALSE.
121 If NewHmacSha1Context is NULL, then return FALSE.
123 @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.
124 @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.
126 @retval TRUE HMAC-SHA1 context copy succeeded.
127 @retval FALSE HMAC-SHA1 context copy failed.
133 IN CONST VOID
*HmacSha1Context
,
134 OUT VOID
*NewHmacSha1Context
138 // Check input parameters.
140 if (HmacSha1Context
== NULL
|| NewHmacSha1Context
== NULL
) {
144 if (HMAC_CTX_copy ((HMAC_CTX
*)NewHmacSha1Context
, (HMAC_CTX
*)HmacSha1Context
) != 1) {
152 Digests the input data and updates HMAC-SHA1 context.
154 This function performs HMAC-SHA1 digest on a data buffer of the specified size.
155 It can be called multiple times to compute the digest of long or discontinuous data streams.
156 HMAC-SHA1 context should be initialized by HmacSha1New(), and should not be finalized by
157 HmacSha1Final(). Behavior with invalid context is undefined.
159 If HmacSha1Context is NULL, then return FALSE.
161 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
162 @param[in] Data Pointer to the buffer containing the data to be digested.
163 @param[in] DataSize Size of Data buffer in bytes.
165 @retval TRUE HMAC-SHA1 data digest succeeded.
166 @retval FALSE HMAC-SHA1 data digest failed.
172 IN OUT VOID
*HmacSha1Context
,
178 // Check input parameters.
180 if (HmacSha1Context
== NULL
) {
185 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
187 if (Data
== NULL
&& DataSize
!= 0) {
192 // OpenSSL HMAC-SHA1 digest update
194 if (HMAC_Update ((HMAC_CTX
*)HmacSha1Context
, Data
, DataSize
) != 1) {
202 Completes computation of the HMAC-SHA1 digest value.
204 This function completes HMAC-SHA1 digest computation and retrieves the digest value into
205 the specified memory. After this function has been called, the HMAC-SHA1 context cannot
207 HMAC-SHA1 context should be initialized by HmacSha1New(), and should not be finalized by
208 HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.
210 If HmacSha1Context is NULL, then return FALSE.
211 If HmacValue is NULL, then return FALSE.
213 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.
214 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest
217 @retval TRUE HMAC-SHA1 digest computation succeeded.
218 @retval FALSE HMAC-SHA1 digest computation failed.
224 IN OUT VOID
*HmacSha1Context
,
231 // Check input parameters.
233 if (HmacSha1Context
== NULL
|| HmacValue
== NULL
) {
238 // OpenSSL HMAC-SHA1 digest finalization
240 if (HMAC_Final ((HMAC_CTX
*)HmacSha1Context
, HmacValue
, &Length
) != 1) {
243 if (HMAC_CTX_reset ((HMAC_CTX
*)HmacSha1Context
) != 1) {