]>
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 | |
a23fdff6 JW |
81 | Set user-supplied key for subsequent use. It must be done before any\r |
82 | calling to HmacSha256Update().\r | |
72009c62 QL |
83 | \r |
84 | If HmacSha256Context is NULL, then return FALSE.\r | |
85 | \r | |
a23fdff6 | 86 | @param[out] HmacSha256Context Pointer to HMAC-SHA256 context.\r |
72009c62 QL |
87 | @param[in] Key Pointer to the user-supplied key.\r |
88 | @param[in] KeySize Key size in bytes.\r | |
89 | \r | |
a23fdff6 JW |
90 | @retval TRUE The Key is set successfully.\r |
91 | @retval FALSE The Key is set unsuccessfully.\r | |
72009c62 QL |
92 | \r |
93 | **/\r | |
94 | BOOLEAN\r | |
95 | EFIAPI\r | |
a23fdff6 | 96 | HmacSha256SetKey (\r |
72009c62 QL |
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 | |
4c270243 QL |
109 | if (HMAC_Init_ex ((HMAC_CTX *)HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL) != 1) {\r |
110 | return FALSE;\r | |
111 | }\r | |
72009c62 QL |
112 | \r |
113 | return TRUE;\r | |
114 | }\r | |
115 | \r | |
116 | /**\r | |
117 | Makes a copy of an existing HMAC-SHA256 context.\r | |
118 | \r | |
119 | If HmacSha256Context is NULL, then return FALSE.\r | |
120 | If NewHmacSha256Context is NULL, then return FALSE.\r | |
121 | \r | |
122 | @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.\r | |
123 | @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.\r | |
124 | \r | |
125 | @retval TRUE HMAC-SHA256 context copy succeeded.\r | |
126 | @retval FALSE HMAC-SHA256 context copy failed.\r | |
127 | \r | |
128 | **/\r | |
129 | BOOLEAN\r | |
130 | EFIAPI\r | |
131 | HmacSha256Duplicate (\r | |
132 | IN CONST VOID *HmacSha256Context,\r | |
133 | OUT VOID *NewHmacSha256Context\r | |
134 | )\r | |
135 | {\r | |
136 | //\r | |
137 | // Check input parameters.\r | |
138 | //\r | |
139 | if (HmacSha256Context == NULL || NewHmacSha256Context == NULL) {\r | |
140 | return FALSE;\r | |
141 | }\r | |
142 | \r | |
4c270243 QL |
143 | if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha256Context, (HMAC_CTX *)HmacSha256Context) != 1) {\r |
144 | return FALSE;\r | |
145 | }\r | |
72009c62 QL |
146 | \r |
147 | return TRUE;\r | |
148 | }\r | |
149 | \r | |
150 | /**\r | |
151 | Digests the input data and updates HMAC-SHA256 context.\r | |
152 | \r | |
153 | This function performs HMAC-SHA256 digest on a data buffer of the specified size.\r | |
154 | It can be called multiple times to compute the digest of long or discontinuous data streams.\r | |
a23fdff6 JW |
155 | HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized\r |
156 | by HmacSha256Final(). Behavior with invalid context is undefined.\r | |
72009c62 QL |
157 | \r |
158 | If HmacSha256Context is NULL, then return FALSE.\r | |
159 | \r | |
160 | @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.\r | |
161 | @param[in] Data Pointer to the buffer containing the data to be digested.\r | |
162 | @param[in] DataSize Size of Data buffer in bytes.\r | |
163 | \r | |
164 | @retval TRUE HMAC-SHA256 data digest succeeded.\r | |
165 | @retval FALSE HMAC-SHA256 data digest failed.\r | |
166 | \r | |
167 | **/\r | |
168 | BOOLEAN\r | |
169 | EFIAPI\r | |
170 | HmacSha256Update (\r | |
171 | IN OUT VOID *HmacSha256Context,\r | |
172 | IN CONST VOID *Data,\r | |
173 | IN UINTN DataSize\r | |
174 | )\r | |
175 | {\r | |
176 | //\r | |
177 | // Check input parameters.\r | |
178 | //\r | |
179 | if (HmacSha256Context == NULL) {\r | |
180 | return FALSE;\r | |
181 | }\r | |
182 | \r | |
183 | //\r | |
184 | // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r | |
185 | //\r | |
186 | if (Data == NULL && DataSize != 0) {\r | |
187 | return FALSE;\r | |
188 | }\r | |
189 | \r | |
190 | //\r | |
191 | // OpenSSL HMAC-SHA256 digest update\r | |
192 | //\r | |
4c270243 QL |
193 | if (HMAC_Update ((HMAC_CTX *)HmacSha256Context, Data, DataSize) != 1) {\r |
194 | return FALSE;\r | |
195 | }\r | |
72009c62 QL |
196 | \r |
197 | return TRUE;\r | |
198 | }\r | |
199 | \r | |
200 | /**\r | |
201 | Completes computation of the HMAC-SHA256 digest value.\r | |
202 | \r | |
203 | This function completes HMAC-SHA256 hash computation and retrieves the digest value into\r | |
204 | the specified memory. After this function has been called, the HMAC-SHA256 context cannot\r | |
205 | be used again.\r | |
a23fdff6 JW |
206 | HMAC-SHA256 context should be initialized by HmacSha256New(), and should not be finalized\r |
207 | by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.\r | |
72009c62 QL |
208 | \r |
209 | If HmacSha256Context is NULL, then return FALSE.\r | |
68ae7cd6 | 210 | If HmacValue is NULL, then return FALSE.\r |
72009c62 QL |
211 | \r |
212 | @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.\r | |
68ae7cd6 | 213 | @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA256 digest\r |
72009c62 QL |
214 | value (32 bytes).\r |
215 | \r | |
216 | @retval TRUE HMAC-SHA256 digest computation succeeded.\r | |
217 | @retval FALSE HMAC-SHA256 digest computation failed.\r | |
218 | \r | |
219 | **/\r | |
220 | BOOLEAN\r | |
221 | EFIAPI\r | |
222 | HmacSha256Final (\r | |
223 | IN OUT VOID *HmacSha256Context,\r | |
224 | OUT UINT8 *HmacValue\r | |
225 | )\r | |
226 | {\r | |
227 | UINT32 Length;\r | |
228 | \r | |
229 | //\r | |
230 | // Check input parameters.\r | |
231 | //\r | |
232 | if (HmacSha256Context == NULL || HmacValue == NULL) {\r | |
233 | return FALSE;\r | |
234 | }\r | |
235 | \r | |
236 | //\r | |
237 | // OpenSSL HMAC-SHA256 digest finalization\r | |
238 | //\r | |
4c270243 QL |
239 | if (HMAC_Final ((HMAC_CTX *)HmacSha256Context, HmacValue, &Length) != 1) {\r |
240 | return FALSE;\r | |
241 | }\r | |
242 | if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha256Context) != 1) {\r | |
243 | return FALSE;\r | |
244 | }\r | |
72009c62 QL |
245 | \r |
246 | return TRUE;\r | |
247 | }\r |