]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c
CryptoPkg: Update HMAC Wrapper with opaque HMAC_CTX object.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacSha256.c
CommitLineData
72009c62
QL
1/** @file\r
2 HMAC-SHA256 Wrapper Implementation over OpenSSL.\r
3\r
4c270243 4Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
72009c62
QL
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
4c270243
QL
18#define HMAC_SHA256_CTX_SIZE sizeof(void *) * 4 + sizeof(unsigned int) + \\r
19 sizeof(unsigned char) * HMAC_MAX_MD_CBLOCK\r
20\r
72009c62
QL
21/**\r
22 Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations.\r
4c270243
QL
23 (NOTE: This API is deprecated.\r
24 Use HmacSha256New() / HmacSha256Free() for HMAC-SHA256 Context operations.)\r
72009c62
QL
25\r
26 @return The size, in bytes, of the context buffer required for HMAC-SHA256 operations.\r
27\r
28**/\r
29UINTN\r
30EFIAPI\r
31HmacSha256GetContextSize (\r
32 VOID\r
33 )\r
34{\r
35 //\r
36 // Retrieves the OpenSSL HMAC-SHA256 Context Size\r
4c270243
QL
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 HmacSha256GetContextSize() in future, and use HmacSha256New()\r
40 // and HmacSha256Free() for context allocation and release.\r
41 //\r
42 return (UINTN)HMAC_SHA256_CTX_SIZE;\r
43}\r
44\r
45/**\r
46 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.\r
47\r
48 @return Pointer to the HMAC_CTX context that has been initialized.\r
49 If the allocations fails, HmacSha256New() returns NULL.\r
50\r
51**/\r
52VOID *\r
53EFIAPI\r
54HmacSha256New (\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] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.\r
68\r
69**/\r
70VOID\r
71EFIAPI\r
72HmacSha256Free (\r
73 IN VOID *HmacSha256Ctx\r
74 )\r
75{\r
72009c62 76 //\r
4c270243
QL
77 // Free OpenSSL HMAC_CTX Context\r
78 //\r
79 HMAC_CTX_free ((HMAC_CTX *)HmacSha256Ctx);\r
72009c62
QL
80}\r
81\r
82/**\r
83 Initializes user-supplied memory pointed by HmacSha256Context as HMAC-SHA256 context for\r
84 subsequent use.\r
85\r
86 If HmacSha256Context is NULL, then return FALSE.\r
87\r
88 @param[out] HmacSha256Context Pointer to HMAC-SHA256 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-SHA256 context initialization succeeded.\r
93 @retval FALSE HMAC-SHA256 context initialization failed.\r
94\r
95**/\r
96BOOLEAN\r
97EFIAPI\r
98HmacSha256Init (\r
99 OUT VOID *HmacSha256Context,\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 (HmacSha256Context == NULL || KeySize > INT_MAX) {\r
108 return FALSE;\r
109 }\r
110\r
111 //\r
112 // OpenSSL HMAC-SHA256 Context Initialization\r
113 //\r
4c270243
QL
114 memset(HmacSha256Context, 0, HMAC_SHA256_CTX_SIZE);\r
115 if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) {\r
116 return FALSE;\r
117 }\r
118 if (HMAC_Init_ex ((HMAC_CTX *)HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL) != 1) {\r
119 return FALSE;\r
120 }\r
72009c62
QL
121\r
122 return TRUE;\r
123}\r
124\r
125/**\r
126 Makes a copy of an existing HMAC-SHA256 context.\r
127\r
128 If HmacSha256Context is NULL, then return FALSE.\r
129 If NewHmacSha256Context is NULL, then return FALSE.\r
130\r
131 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.\r
132 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.\r
133\r
134 @retval TRUE HMAC-SHA256 context copy succeeded.\r
135 @retval FALSE HMAC-SHA256 context copy failed.\r
136\r
137**/\r
138BOOLEAN\r
139EFIAPI\r
140HmacSha256Duplicate (\r
141 IN CONST VOID *HmacSha256Context,\r
142 OUT VOID *NewHmacSha256Context\r
143 )\r
144{\r
145 //\r
146 // Check input parameters.\r
147 //\r
148 if (HmacSha256Context == NULL || NewHmacSha256Context == NULL) {\r
149 return FALSE;\r
150 }\r
151\r
4c270243
QL
152 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha256Context, (HMAC_CTX *)HmacSha256Context) != 1) {\r
153 return FALSE;\r
154 }\r
72009c62
QL
155\r
156 return TRUE;\r
157}\r
158\r
159/**\r
160 Digests the input data and updates HMAC-SHA256 context.\r
161\r
162 This function performs HMAC-SHA256 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-SHA256 context should be already correctly initialized by HmacSha256Init(), and should not\r
165 be finalized by HmacSha256Final(). Behavior with invalid context is undefined.\r
166\r
167 If HmacSha256Context is NULL, then return FALSE.\r
168\r
169 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 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-SHA256 data digest succeeded.\r
174 @retval FALSE HMAC-SHA256 data digest failed.\r
175\r
176**/\r
177BOOLEAN\r
178EFIAPI\r
179HmacSha256Update (\r
180 IN OUT VOID *HmacSha256Context,\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 (HmacSha256Context == 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-SHA256 digest update\r
201 //\r
4c270243
QL
202 if (HMAC_Update ((HMAC_CTX *)HmacSha256Context, Data, DataSize) != 1) {\r
203 return FALSE;\r
204 }\r
72009c62
QL
205\r
206 return TRUE;\r
207}\r
208\r
209/**\r
210 Completes computation of the HMAC-SHA256 digest value.\r
211\r
212 This function completes HMAC-SHA256 hash computation and retrieves the digest value into\r
213 the specified memory. After this function has been called, the HMAC-SHA256 context cannot\r
214 be used again.\r
215 HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should\r
216 not be finalized by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.\r
217\r
218 If HmacSha256Context is NULL, then return FALSE.\r
68ae7cd6 219 If HmacValue is NULL, then return FALSE.\r
72009c62
QL
220\r
221 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.\r
68ae7cd6 222 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest\r
72009c62
QL
223 value (32 bytes).\r
224\r
225 @retval TRUE HMAC-SHA256 digest computation succeeded.\r
226 @retval FALSE HMAC-SHA256 digest computation failed.\r
227\r
228**/\r
229BOOLEAN\r
230EFIAPI\r
231HmacSha256Final (\r
232 IN OUT VOID *HmacSha256Context,\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 (HmacSha256Context == NULL || HmacValue == NULL) {\r
242 return FALSE;\r
243 }\r
244\r
245 //\r
246 // OpenSSL HMAC-SHA256 digest finalization\r
247 //\r
4c270243
QL
248 if (HMAC_Final ((HMAC_CTX *)HmacSha256Context, HmacValue, &Length) != 1) {\r
249 return FALSE;\r
250 }\r
251 if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) {\r
252 return FALSE;\r
253 }\r
72009c62
QL
254\r
255 return TRUE;\r
256}\r