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