]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c
CryptoPkg/BaseCryptLib: remove HmacXxxGetContextSize interface
[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
9a1f14ad 4Copyright (c) 2016 - 2020, 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/**\r
13 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA256 use.\r
14\r
15 @return Pointer to the HMAC_CTX context that has been initialized.\r
16 If the allocations fails, HmacSha256New() returns NULL.\r
17\r
18**/\r
19VOID *\r
20EFIAPI\r
21HmacSha256New (\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] HmacSha256Ctx Pointer to the HMAC_CTX context to be released.\r
35\r
36**/\r
37VOID\r
38EFIAPI\r
39HmacSha256Free (\r
40 IN VOID *HmacSha256Ctx\r
41 )\r
42{\r
72009c62 43 //\r
4c270243
QL
44 // Free OpenSSL HMAC_CTX Context\r
45 //\r
46 HMAC_CTX_free ((HMAC_CTX *)HmacSha256Ctx);\r
72009c62
QL
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 HmacSha256Update().\r
72009c62
QL
52\r
53 If HmacSha256Context is NULL, then return FALSE.\r
54\r
a23fdff6 55 @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.\r
72009c62
QL
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 The Key is set successfully.\r
60 @retval FALSE The Key is set unsuccessfully.\r
72009c62
QL
61\r
62**/\r
63BOOLEAN\r
64EFIAPI\r
a23fdff6 65HmacSha256SetKey (\r
72009c62
QL
66 OUT VOID *HmacSha256Context,\r
67 IN CONST UINT8 *Key,\r
68 IN UINTN KeySize\r
69 )\r
70{\r
71 //\r
72 // Check input parameters.\r
73 //\r
74 if (HmacSha256Context == NULL || KeySize > INT_MAX) {\r
75 return FALSE;\r
76 }\r
77\r
4c270243
QL
78 if (HMAC_Init_ex ((HMAC_CTX *)HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL) != 1) {\r
79 return FALSE;\r
80 }\r
72009c62
QL
81\r
82 return TRUE;\r
83}\r
84\r
85/**\r
86 Makes a copy of an existing HMAC-SHA256 context.\r
87\r
88 If HmacSha256Context is NULL, then return FALSE.\r
89 If NewHmacSha256Context is NULL, then return FALSE.\r
90\r
91 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.\r
92 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.\r
93\r
94 @retval TRUE HMAC-SHA256 context copy succeeded.\r
95 @retval FALSE HMAC-SHA256 context copy failed.\r
96\r
97**/\r
98BOOLEAN\r
99EFIAPI\r
100HmacSha256Duplicate (\r
101 IN CONST VOID *HmacSha256Context,\r
102 OUT VOID *NewHmacSha256Context\r
103 )\r
104{\r
105 //\r
106 // Check input parameters.\r
107 //\r
108 if (HmacSha256Context == NULL || NewHmacSha256Context == NULL) {\r
109 return FALSE;\r
110 }\r
111\r
4c270243
QL
112 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha256Context, (HMAC_CTX *)HmacSha256Context) != 1) {\r
113 return FALSE;\r
114 }\r
72009c62
QL
115\r
116 return TRUE;\r
117}\r
118\r
119/**\r
120 Digests the input data and updates HMAC-SHA256 context.\r
121\r
122 This function performs HMAC-SHA256 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-SHA256 context should be initialized by HmacSha256New(), and should not be finalized\r
125 by HmacSha256Final(). Behavior with invalid context is undefined.\r
72009c62
QL
126\r
127 If HmacSha256Context is NULL, then return FALSE.\r
128\r
129 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 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-SHA256 data digest succeeded.\r
134 @retval FALSE HMAC-SHA256 data digest failed.\r
135\r
136**/\r
137BOOLEAN\r
138EFIAPI\r
139HmacSha256Update (\r
140 IN OUT VOID *HmacSha256Context,\r
141 IN CONST VOID *Data,\r
142 IN UINTN DataSize\r
143 )\r
144{\r
145 //\r
146 // Check input parameters.\r
147 //\r
148 if (HmacSha256Context == NULL) {\r
149 return FALSE;\r
150 }\r
151\r
152 //\r
153 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
154 //\r
155 if (Data == NULL && DataSize != 0) {\r
156 return FALSE;\r
157 }\r
158\r
159 //\r
160 // OpenSSL HMAC-SHA256 digest update\r
161 //\r
4c270243
QL
162 if (HMAC_Update ((HMAC_CTX *)HmacSha256Context, Data, DataSize) != 1) {\r
163 return FALSE;\r
164 }\r
72009c62
QL
165\r
166 return TRUE;\r
167}\r
168\r
169/**\r
170 Completes computation of the HMAC-SHA256 digest value.\r
171\r
172 This function completes HMAC-SHA256 hash computation and retrieves the digest value into\r
173 the specified memory. After this function has been called, the HMAC-SHA256 context cannot\r
174 be used again.\r
a23fdff6
JW
175 HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized\r
176 by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.\r
72009c62
QL
177\r
178 If HmacSha256Context is NULL, then return FALSE.\r
68ae7cd6 179 If HmacValue is NULL, then return FALSE.\r
72009c62
QL
180\r
181 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.\r
68ae7cd6 182 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest\r
72009c62
QL
183 value (32 bytes).\r
184\r
185 @retval TRUE HMAC-SHA256 digest computation succeeded.\r
186 @retval FALSE HMAC-SHA256 digest computation failed.\r
187\r
188**/\r
189BOOLEAN\r
190EFIAPI\r
191HmacSha256Final (\r
192 IN OUT VOID *HmacSha256Context,\r
193 OUT UINT8 *HmacValue\r
194 )\r
195{\r
196 UINT32 Length;\r
197\r
198 //\r
199 // Check input parameters.\r
200 //\r
201 if (HmacSha256Context == NULL || HmacValue == NULL) {\r
202 return FALSE;\r
203 }\r
204\r
205 //\r
206 // OpenSSL HMAC-SHA256 digest finalization\r
207 //\r
4c270243
QL
208 if (HMAC_Final ((HMAC_CTX *)HmacSha256Context, HmacValue, &Length) != 1) {\r
209 return FALSE;\r
210 }\r
211 if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) {\r
212 return FALSE;\r
213 }\r
72009c62
QL
214\r
215 return TRUE;\r
216}\r