]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha1.c
CryptoPkg/BaseCryptLib: Make HMAC_CTX size backward compatible
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacSha1.c
CommitLineData
a8c44645 1/** @file\r
2 HMAC-SHA1 Wrapper Implementation over OpenSSL.\r
3\r
4c270243 4Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
2009f6b4 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
a8c44645 6\r
7**/\r
8\r
9#include "InternalCryptLib.h"\r
10#include <openssl/hmac.h>\r
11\r
acfb9091
XL
12//\r
13// NOTE: OpenSSL redefines the size of HMAC_CTX at crypto/hmac/hmac_lcl.h\r
14// #define HMAC_MAX_MD_CBLOCK_SIZE 144\r
15//\r
16//\r
17#define HMAC_SHA1_CTX_SIZE (sizeof(void *) * 4 + sizeof(unsigned int) + \\r
18 sizeof(unsigned char) * 144)\r
4c270243 19\r
a8c44645 20/**\r
21 Retrieves the size, in bytes, of the context buffer required for HMAC-SHA1 operations.\r
4c270243
QL
22 (NOTE: This API is deprecated.\r
23 Use HmacSha1New() / HmacSha1Free() for HMAC-SHA1 Context operations.)\r
a8c44645 24\r
25 @return The size, in bytes, of the context buffer required for HMAC-SHA1 operations.\r
26\r
27**/\r
28UINTN\r
29EFIAPI\r
30HmacSha1GetContextSize (\r
31 VOID\r
32 )\r
33{\r
34 //\r
35 // Retrieves the OpenSSL HMAC-SHA1 Context Size\r
4c270243
QL
36 // NOTE: HMAC_CTX object was made opaque in openssl-1.1.x, here we just use the\r
37 // fixed size as a workaround to make this API work for compatibility.\r
38 // We should retire HmacSha15GetContextSize() in future, and use HmacSha1New()\r
39 // and HmacSha1Free() for context allocation and release.\r
40 //\r
41 return (UINTN) HMAC_SHA1_CTX_SIZE;\r
42}\r
43\r
44/**\r
45 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA1 use.\r
46\r
47 @return Pointer to the HMAC_CTX context that has been initialized.\r
48 If the allocations fails, HmacSha1New() returns NULL.\r
49\r
50**/\r
51VOID *\r
52EFIAPI\r
53HmacSha1New (\r
54 VOID\r
55 )\r
56{\r
57 //\r
58 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()\r
59 //\r
60 return (VOID *) HMAC_CTX_new ();\r
61}\r
62\r
63/**\r
64 Release the specified HMAC_CTX context.\r
65\r
66 @param[in] HmacSha1Ctx Pointer to the HMAC_CTX context to be released.\r
67\r
68**/\r
69VOID\r
70EFIAPI\r
71HmacSha1Free (\r
72 IN VOID *HmacSha1Ctx\r
73 )\r
74{\r
a8c44645 75 //\r
4c270243
QL
76 // Free OpenSSL HMAC_CTX Context\r
77 //\r
78 HMAC_CTX_free ((HMAC_CTX *)HmacSha1Ctx);\r
a8c44645 79}\r
80\r
81/**\r
82 Initializes user-supplied memory pointed by HmacSha1Context as HMAC-SHA1 context for\r
83 subsequent use.\r
84\r
16d2c32c 85 If HmacSha1Context is NULL, then return FALSE.\r
a8c44645 86\r
87 @param[out] HmacSha1Context Pointer to HMAC-SHA1 context being initialized.\r
88 @param[in] Key Pointer to the user-supplied key.\r
89 @param[in] KeySize Key size in bytes.\r
90\r
91 @retval TRUE HMAC-SHA1 context initialization succeeded.\r
92 @retval FALSE HMAC-SHA1 context initialization failed.\r
93\r
94**/\r
95BOOLEAN\r
96EFIAPI\r
97HmacSha1Init (\r
98 OUT VOID *HmacSha1Context,\r
99 IN CONST UINT8 *Key,\r
100 IN UINTN KeySize\r
101 )\r
102{\r
103 //\r
16d2c32c 104 // Check input parameters.\r
a8c44645 105 //\r
dda39f3a 106 if (HmacSha1Context == NULL || KeySize > INT_MAX) {\r
16d2c32c 107 return FALSE;\r
108 }\r
a8c44645 109\r
110 //\r
111 // OpenSSL HMAC-SHA1 Context Initialization\r
112 //\r
4c270243
QL
113 memset(HmacSha1Context, 0, HMAC_SHA1_CTX_SIZE);\r
114 if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha1Context) != 1) {\r
115 return FALSE;\r
116 }\r
117 if (HMAC_Init_ex ((HMAC_CTX *)HmacSha1Context, Key, (UINT32) KeySize, EVP_sha1(), NULL) != 1) {\r
118 return FALSE;\r
119 }\r
a8c44645 120\r
121 return TRUE;\r
122}\r
123\r
124/**\r
125 Makes a copy of an existing HMAC-SHA1 context.\r
126\r
16d2c32c 127 If HmacSha1Context is NULL, then return FALSE.\r
128 If NewHmacSha1Context is NULL, then return FALSE.\r
a8c44645 129\r
130 @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.\r
131 @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.\r
132\r
133 @retval TRUE HMAC-SHA1 context copy succeeded.\r
134 @retval FALSE HMAC-SHA1 context copy failed.\r
135\r
136**/\r
137BOOLEAN\r
138EFIAPI\r
139HmacSha1Duplicate (\r
140 IN CONST VOID *HmacSha1Context,\r
141 OUT VOID *NewHmacSha1Context\r
142 )\r
143{\r
4a567c96 144 //\r
16d2c32c 145 // Check input parameters.\r
4a567c96 146 //\r
16d2c32c 147 if (HmacSha1Context == NULL || NewHmacSha1Context == NULL) {\r
148 return FALSE;\r
149 }\r
4a567c96 150\r
4c270243
QL
151 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha1Context, (HMAC_CTX *)HmacSha1Context) != 1) {\r
152 return FALSE;\r
153 }\r
a8c44645 154\r
155 return TRUE;\r
156}\r
157\r
158/**\r
159 Digests the input data and updates HMAC-SHA1 context.\r
160\r
161 This function performs HMAC-SHA1 digest on a data buffer of the specified size.\r
162 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
2998af86 163 HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should not\r
a8c44645 164 be finalized by HmacSha1Final(). Behavior with invalid context is undefined.\r
165\r
16d2c32c 166 If HmacSha1Context is NULL, then return FALSE.\r
a8c44645 167\r
168 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r
169 @param[in] Data Pointer to the buffer containing the data to be digested.\r
170 @param[in] DataSize Size of Data buffer in bytes.\r
171\r
172 @retval TRUE HMAC-SHA1 data digest succeeded.\r
173 @retval FALSE HMAC-SHA1 data digest failed.\r
174\r
175**/\r
176BOOLEAN\r
177EFIAPI\r
178HmacSha1Update (\r
179 IN OUT VOID *HmacSha1Context,\r
180 IN CONST VOID *Data,\r
181 IN UINTN DataSize\r
182 )\r
183{\r
184 //\r
16d2c32c 185 // Check input parameters.\r
a8c44645 186 //\r
16d2c32c 187 if (HmacSha1Context == NULL) {\r
188 return FALSE;\r
189 }\r
a8c44645 190\r
191 //\r
16d2c32c 192 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
a8c44645 193 //\r
16d2c32c 194 if (Data == NULL && DataSize != 0) {\r
195 return FALSE;\r
a8c44645 196 }\r
197\r
198 //\r
199 // OpenSSL HMAC-SHA1 digest update\r
200 //\r
4c270243
QL
201 if (HMAC_Update ((HMAC_CTX *)HmacSha1Context, Data, DataSize) != 1) {\r
202 return FALSE;\r
203 }\r
a8c44645 204\r
205 return TRUE;\r
206}\r
207\r
208/**\r
209 Completes computation of the HMAC-SHA1 digest value.\r
210\r
211 This function completes HMAC-SHA1 digest computation and retrieves the digest value into\r
212 the specified memory. After this function has been called, the HMAC-SHA1 context cannot\r
213 be used again.\r
2998af86 214 HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should\r
a8c44645 215 not be finalized by HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.\r
216\r
16d2c32c 217 If HmacSha1Context is NULL, then return FALSE.\r
218 If HmacValue is NULL, then return FALSE.\r
a8c44645 219\r
220 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r
221 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest\r
222 value (20 bytes).\r
223\r
224 @retval TRUE HMAC-SHA1 digest computation succeeded.\r
225 @retval FALSE HMAC-SHA1 digest computation failed.\r
226\r
227**/\r
228BOOLEAN\r
229EFIAPI\r
230HmacSha1Final (\r
231 IN OUT VOID *HmacSha1Context,\r
232 OUT UINT8 *HmacValue\r
233 )\r
234{\r
235 UINT32 Length;\r
236\r
237 //\r
16d2c32c 238 // Check input parameters.\r
a8c44645 239 //\r
16d2c32c 240 if (HmacSha1Context == NULL || HmacValue == NULL) {\r
241 return FALSE;\r
242 }\r
a8c44645 243\r
244 //\r
245 // OpenSSL HMAC-SHA1 digest finalization\r
246 //\r
4c270243
QL
247 if (HMAC_Final ((HMAC_CTX *)HmacSha1Context, HmacValue, &Length) != 1) {\r
248 return FALSE;\r
249 }\r
250 if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha1Context) != 1) {\r
251 return FALSE;\r
252 }\r
a8c44645 253\r
254 return TRUE;\r
255}\r