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