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