]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacMd5.c
CryptoPkg/BaseCryptLib: remove HmacXxxGetContextSize interface
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacMd5.c
CommitLineData
a8c44645 1/** @file\r
2 HMAC-MD5 Wrapper Implementation over OpenSSL.\r
3\r
9a1f14ad 4Copyright (c) 2010 - 2020, 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
4c270243
QL
12/**\r
13 Allocates and initializes one HMAC_CTX context for subsequent HMAC-MD5 use.\r
14\r
15 @return Pointer to the HMAC_CTX context that has been initialized.\r
16 If the allocations fails, HmacMd5New() returns NULL.\r
17\r
18**/\r
19VOID *\r
20EFIAPI\r
21HmacMd5New (\r
22 VOID\r
23 )\r
24{\r
25 //\r
26 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()\r
27 //\r
28 return (VOID *) HMAC_CTX_new ();\r
29}\r
30\r
31/**\r
32 Release the specified HMAC_CTX context.\r
33\r
34 @param[in] HmacMd5Ctx Pointer to the HMAC_CTX context to be released.\r
35\r
36**/\r
37VOID\r
38EFIAPI\r
39HmacMd5Free (\r
40 IN VOID *HmacMd5Ctx\r
41 )\r
42{\r
43 //\r
44 // Free OpenSSL HMAC_CTX Context\r
45 //\r
46 HMAC_CTX_free ((HMAC_CTX *)HmacMd5Ctx);\r
a8c44645 47}\r
48\r
49/**\r
a23fdff6
JW
50 Set user-supplied key for subsequent use. It must be done before any\r
51 calling to HmacMd5Update().\r
a8c44645 52\r
16d2c32c 53 If HmacMd5Context is NULL, then return FALSE.\r
a8c44645 54\r
a23fdff6 55 @param[out] HmacMd5Context Pointer to HMAC-MD5 context.\r
a8c44645 56 @param[in] Key Pointer to the user-supplied key.\r
57 @param[in] KeySize Key size in bytes.\r
58\r
a23fdff6
JW
59 @retval TRUE Key is set successfully.\r
60 @retval FALSE Key is set unsuccessfully.\r
a8c44645 61\r
62**/\r
63BOOLEAN\r
64EFIAPI\r
a23fdff6 65HmacMd5SetKey (\r
a8c44645 66 OUT VOID *HmacMd5Context,\r
67 IN CONST UINT8 *Key,\r
68 IN UINTN KeySize\r
69 )\r
70{\r
71 //\r
16d2c32c 72 // Check input parameters.\r
a8c44645 73 //\r
dda39f3a 74 if (HmacMd5Context == NULL || KeySize > INT_MAX) {\r
16d2c32c 75 return FALSE;\r
76 }\r
a8c44645 77\r
4c270243
QL
78 if (HMAC_Init_ex ((HMAC_CTX *)HmacMd5Context, Key, (UINT32) KeySize, EVP_md5(), NULL) != 1) {\r
79 return FALSE;\r
80 }\r
a8c44645 81\r
82 return TRUE;\r
83}\r
84\r
85/**\r
86 Makes a copy of an existing HMAC-MD5 context.\r
87\r
16d2c32c 88 If HmacMd5Context is NULL, then return FALSE.\r
89 If NewHmacMd5Context is NULL, then return FALSE.\r
a8c44645 90\r
91 @param[in] HmacMd5Context Pointer to HMAC-MD5 context being copied.\r
92 @param[out] NewHmacMd5Context Pointer to new HMAC-MD5 context.\r
93\r
94 @retval TRUE HMAC-MD5 context copy succeeded.\r
95 @retval FALSE HMAC-MD5 context copy failed.\r
96\r
97**/\r
98BOOLEAN\r
99EFIAPI\r
100HmacMd5Duplicate (\r
101 IN CONST VOID *HmacMd5Context,\r
102 OUT VOID *NewHmacMd5Context\r
103 )\r
104{\r
4a567c96 105 //\r
16d2c32c 106 // Check input parameters.\r
4a567c96 107 //\r
16d2c32c 108 if (HmacMd5Context == NULL || NewHmacMd5Context == NULL) {\r
109 return FALSE;\r
110 }\r
4c270243
QL
111\r
112 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacMd5Context, (HMAC_CTX *)HmacMd5Context) != 1) {\r
113 return FALSE;\r
114 }\r
a8c44645 115\r
116 return TRUE;\r
117}\r
118\r
119/**\r
120 Digests the input data and updates HMAC-MD5 context.\r
121\r
122 This function performs HMAC-MD5 digest on a data buffer of the specified size.\r
123 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
a23fdff6
JW
124 HMAC-MD5 context should be initialized by HmacMd5New(), and should not be finalized by\r
125 HmacMd5Final(). Behavior with invalid context is undefined.\r
a8c44645 126\r
16d2c32c 127 If HmacMd5Context is NULL, then return FALSE.\r
a8c44645 128\r
129 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.\r
130 @param[in] Data Pointer to the buffer containing the data to be digested.\r
131 @param[in] DataSize Size of Data buffer in bytes.\r
132\r
133 @retval TRUE HMAC-MD5 data digest succeeded.\r
134 @retval FALSE HMAC-MD5 data digest failed.\r
135\r
136**/\r
137BOOLEAN\r
138EFIAPI\r
139HmacMd5Update (\r
140 IN OUT VOID *HmacMd5Context,\r
141 IN CONST VOID *Data,\r
142 IN UINTN DataSize\r
143 )\r
144{\r
145 //\r
16d2c32c 146 // Check input parameters.\r
a8c44645 147 //\r
16d2c32c 148 if (HmacMd5Context == NULL) {\r
149 return FALSE;\r
150 }\r
a8c44645 151\r
152 //\r
16d2c32c 153 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
a8c44645 154 //\r
16d2c32c 155 if (Data == NULL && DataSize != 0) {\r
156 return FALSE;\r
a8c44645 157 }\r
158\r
159 //\r
160 // OpenSSL HMAC-MD5 digest update\r
161 //\r
4c270243
QL
162 if (HMAC_Update ((HMAC_CTX *)HmacMd5Context, Data, DataSize) != 1) {\r
163 return FALSE;\r
164 }\r
a8c44645 165\r
166 return TRUE;\r
167}\r
168\r
169/**\r
170 Completes computation of the HMAC-MD5 digest value.\r
171\r
172 This function completes HMAC-MD5 digest computation and retrieves the digest value into\r
173 the specified memory. After this function has been called, the HMAC-MD5 context cannot\r
174 be used again.\r
a23fdff6
JW
175 HMAC-MD5 context should be initialized by HmacMd5New(), and should not be finalized by\r
176 HmacMd5Final(). Behavior with invalid HMAC-MD5 context is undefined.\r
a8c44645 177\r
16d2c32c 178 If HmacMd5Context is NULL, then return FALSE.\r
179 If HmacValue is NULL, then return FALSE.\r
a8c44645 180\r
181 @param[in, out] HmacMd5Context Pointer to the HMAC-MD5 context.\r
182 @param[out] HmacValue Pointer to a buffer that receives the HMAC-MD5 digest\r
183 value (16 bytes).\r
184\r
185 @retval TRUE HMAC-MD5 digest computation succeeded.\r
186 @retval FALSE HMAC-MD5 digest computation failed.\r
187\r
188**/\r
189BOOLEAN\r
190EFIAPI\r
191HmacMd5Final (\r
192 IN OUT VOID *HmacMd5Context,\r
193 OUT UINT8 *HmacValue\r
194 )\r
195{\r
196 UINT32 Length;\r
197\r
198 //\r
16d2c32c 199 // Check input parameters.\r
a8c44645 200 //\r
16d2c32c 201 if (HmacMd5Context == NULL || HmacValue == NULL) {\r
202 return FALSE;\r
203 }\r
a8c44645 204\r
205 //\r
206 // OpenSSL HMAC-MD5 digest finalization\r
207 //\r
4c270243
QL
208 if (HMAC_Final ((HMAC_CTX *)HmacMd5Context, HmacValue, &Length) != 1) {\r
209 return FALSE;\r
210 }\r
211 if (HMAC_CTX_reset ((HMAC_CTX *)HmacMd5Context) != 1) {\r
212 return FALSE;\r
213 }\r
a8c44645 214\r
215 return TRUE;\r
216}\r