]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c
CryptoPkg: Replace BSD License with BSD+Patent License
[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
2009f6b4 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
72009c62
QL
6\r
7**/\r
8\r
9#include "InternalCryptLib.h"\r
10#include <openssl/hmac.h>\r
11\r
4c270243
QL
12#define HMAC_SHA256_CTX_SIZE sizeof(void *) * 4 + sizeof(unsigned int) + \\r
13 sizeof(unsigned char) * HMAC_MAX_MD_CBLOCK\r
14\r
72009c62
QL
15/**\r
16 Retrieves the size, in bytes, of the context buffer required for HMAC-SHA256 operations.\r
4c270243
QL
17 (NOTE: This API is deprecated.\r
18 Use HmacSha256New() / HmacSha256Free() for HMAC-SHA256 Context operations.)\r
72009c62
QL
19\r
20 @return The size, in bytes, of the context buffer required for HMAC-SHA256 operations.\r
21\r
22**/\r
23UINTN\r
24EFIAPI\r
25HmacSha256GetContextSize (\r
26 VOID\r
27 )\r
28{\r
29 //\r
30 // Retrieves the OpenSSL HMAC-SHA256 Context Size\r
4c270243
QL
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 HmacSha256GetContextSize() in future, and use HmacSha256New()\r
34 // and HmacSha256Free() for context allocation and release.\r
35 //\r
36 return (UINTN)HMAC_SHA256_CTX_SIZE;\r
37}\r
38\r
39/**\r
40 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.\r
41\r
42 @return Pointer to the HMAC_CTX context that has been initialized.\r
43 If the allocations fails, HmacSha256New() returns NULL.\r
44\r
45**/\r
46VOID *\r
47EFIAPI\r
48HmacSha256New (\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] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.\r
62\r
63**/\r
64VOID\r
65EFIAPI\r
66HmacSha256Free (\r
67 IN VOID *HmacSha256Ctx\r
68 )\r
69{\r
72009c62 70 //\r
4c270243
QL
71 // Free OpenSSL HMAC_CTX Context\r
72 //\r
73 HMAC_CTX_free ((HMAC_CTX *)HmacSha256Ctx);\r
72009c62
QL
74}\r
75\r
76/**\r
77 Initializes user-supplied memory pointed by HmacSha256Context as HMAC-SHA256 context for\r
78 subsequent use.\r
79\r
80 If HmacSha256Context is NULL, then return FALSE.\r
81\r
82 @param[out] HmacSha256Context Pointer to HMAC-SHA256 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-SHA256 context initialization succeeded.\r
87 @retval FALSE HMAC-SHA256 context initialization failed.\r
88\r
89**/\r
90BOOLEAN\r
91EFIAPI\r
92HmacSha256Init (\r
93 OUT VOID *HmacSha256Context,\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 (HmacSha256Context == NULL || KeySize > INT_MAX) {\r
102 return FALSE;\r
103 }\r
104\r
105 //\r
106 // OpenSSL HMAC-SHA256 Context Initialization\r
107 //\r
4c270243
QL
108 memset(HmacSha256Context, 0, HMAC_SHA256_CTX_SIZE);\r
109 if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) {\r
110 return FALSE;\r
111 }\r
112 if (HMAC_Init_ex ((HMAC_CTX *)HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL) != 1) {\r
113 return FALSE;\r
114 }\r
72009c62
QL
115\r
116 return TRUE;\r
117}\r
118\r
119/**\r
120 Makes a copy of an existing HMAC-SHA256 context.\r
121\r
122 If HmacSha256Context is NULL, then return FALSE.\r
123 If NewHmacSha256Context is NULL, then return FALSE.\r
124\r
125 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.\r
126 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.\r
127\r
128 @retval TRUE HMAC-SHA256 context copy succeeded.\r
129 @retval FALSE HMAC-SHA256 context copy failed.\r
130\r
131**/\r
132BOOLEAN\r
133EFIAPI\r
134HmacSha256Duplicate (\r
135 IN CONST VOID *HmacSha256Context,\r
136 OUT VOID *NewHmacSha256Context\r
137 )\r
138{\r
139 //\r
140 // Check input parameters.\r
141 //\r
142 if (HmacSha256Context == NULL || NewHmacSha256Context == NULL) {\r
143 return FALSE;\r
144 }\r
145\r
4c270243
QL
146 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha256Context, (HMAC_CTX *)HmacSha256Context) != 1) {\r
147 return FALSE;\r
148 }\r
72009c62
QL
149\r
150 return TRUE;\r
151}\r
152\r
153/**\r
154 Digests the input data and updates HMAC-SHA256 context.\r
155\r
156 This function performs HMAC-SHA256 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-SHA256 context should be already correctly initialized by HmacSha256Init(), and should not\r
159 be finalized by HmacSha256Final(). Behavior with invalid context is undefined.\r
160\r
161 If HmacSha256Context is NULL, then return FALSE.\r
162\r
163 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 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-SHA256 data digest succeeded.\r
168 @retval FALSE HMAC-SHA256 data digest failed.\r
169\r
170**/\r
171BOOLEAN\r
172EFIAPI\r
173HmacSha256Update (\r
174 IN OUT VOID *HmacSha256Context,\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 (HmacSha256Context == 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-SHA256 digest update\r
195 //\r
4c270243
QL
196 if (HMAC_Update ((HMAC_CTX *)HmacSha256Context, Data, DataSize) != 1) {\r
197 return FALSE;\r
198 }\r
72009c62
QL
199\r
200 return TRUE;\r
201}\r
202\r
203/**\r
204 Completes computation of the HMAC-SHA256 digest value.\r
205\r
206 This function completes HMAC-SHA256 hash computation and retrieves the digest value into\r
207 the specified memory. After this function has been called, the HMAC-SHA256 context cannot\r
208 be used again.\r
209 HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should\r
210 not be finalized by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.\r
211\r
212 If HmacSha256Context is NULL, then return FALSE.\r
68ae7cd6 213 If HmacValue is NULL, then return FALSE.\r
72009c62
QL
214\r
215 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.\r
68ae7cd6 216 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest\r
72009c62
QL
217 value (32 bytes).\r
218\r
219 @retval TRUE HMAC-SHA256 digest computation succeeded.\r
220 @retval FALSE HMAC-SHA256 digest computation failed.\r
221\r
222**/\r
223BOOLEAN\r
224EFIAPI\r
225HmacSha256Final (\r
226 IN OUT VOID *HmacSha256Context,\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 (HmacSha256Context == NULL || HmacValue == NULL) {\r
236 return FALSE;\r
237 }\r
238\r
239 //\r
240 // OpenSSL HMAC-SHA256 digest finalization\r
241 //\r
4c270243
QL
242 if (HMAC_Final ((HMAC_CTX *)HmacSha256Context, HmacValue, &Length) != 1) {\r
243 return FALSE;\r
244 }\r
245 if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) {\r
246 return FALSE;\r
247 }\r
72009c62
QL
248\r
249 return TRUE;\r
250}\r