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