]>
Commit | Line | Data |
---|---|---|
a8c44645 | 1 | /** @file\r |
2 | HMAC-SHA1 Wrapper Implementation over OpenSSL.\r | |
3 | \r | |
4c270243 | 4 | Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r |
2009f6b4 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
a8c44645 | 6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include "InternalCryptLib.h"\r | |
10 | #include <openssl/hmac.h>\r | |
11 | \r | |
4c270243 QL |
12 | #define HMAC_SHA1_CTX_SIZE sizeof(void *) * 4 + sizeof(unsigned int) + \\r |
13 | sizeof(unsigned char) * HMAC_MAX_MD_CBLOCK\r | |
14 | \r | |
a8c44645 | 15 | /**\r |
16 | Retrieves the size, in bytes, of the context buffer required for HMAC-SHA1 operations.\r | |
4c270243 QL |
17 | (NOTE: This API is deprecated.\r |
18 | Use HmacSha1New() / HmacSha1Free() for HMAC-SHA1 Context operations.)\r | |
a8c44645 | 19 | \r |
20 | @return The size, in bytes, of the context buffer required for HMAC-SHA1 operations.\r | |
21 | \r | |
22 | **/\r | |
23 | UINTN\r | |
24 | EFIAPI\r | |
25 | HmacSha1GetContextSize (\r | |
26 | VOID\r | |
27 | )\r | |
28 | {\r | |
29 | //\r | |
30 | // Retrieves the OpenSSL HMAC-SHA1 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 HmacSha15GetContextSize() in future, and use HmacSha1New()\r | |
34 | // and HmacSha1Free() for context allocation and release.\r | |
35 | //\r | |
36 | return (UINTN) HMAC_SHA1_CTX_SIZE;\r | |
37 | }\r | |
38 | \r | |
39 | /**\r | |
40 | Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA1 use.\r | |
41 | \r | |
42 | @return Pointer to the HMAC_CTX context that has been initialized.\r | |
43 | If the allocations fails, HmacSha1New() returns NULL.\r | |
44 | \r | |
45 | **/\r | |
46 | VOID *\r | |
47 | EFIAPI\r | |
48 | HmacSha1New (\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] HmacSha1Ctx Pointer to the HMAC_CTX context to be released.\r | |
62 | \r | |
63 | **/\r | |
64 | VOID\r | |
65 | EFIAPI\r | |
66 | HmacSha1Free (\r | |
67 | IN VOID *HmacSha1Ctx\r | |
68 | )\r | |
69 | {\r | |
a8c44645 | 70 | //\r |
4c270243 QL |
71 | // Free OpenSSL HMAC_CTX Context\r |
72 | //\r | |
73 | HMAC_CTX_free ((HMAC_CTX *)HmacSha1Ctx);\r | |
a8c44645 | 74 | }\r |
75 | \r | |
76 | /**\r | |
77 | Initializes user-supplied memory pointed by HmacSha1Context as HMAC-SHA1 context for\r | |
78 | subsequent use.\r | |
79 | \r | |
16d2c32c | 80 | If HmacSha1Context is NULL, then return FALSE.\r |
a8c44645 | 81 | \r |
82 | @param[out] HmacSha1Context Pointer to HMAC-SHA1 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-SHA1 context initialization succeeded.\r | |
87 | @retval FALSE HMAC-SHA1 context initialization failed.\r | |
88 | \r | |
89 | **/\r | |
90 | BOOLEAN\r | |
91 | EFIAPI\r | |
92 | HmacSha1Init (\r | |
93 | OUT VOID *HmacSha1Context,\r | |
94 | IN CONST UINT8 *Key,\r | |
95 | IN UINTN KeySize\r | |
96 | )\r | |
97 | {\r | |
98 | //\r | |
16d2c32c | 99 | // Check input parameters.\r |
a8c44645 | 100 | //\r |
dda39f3a | 101 | if (HmacSha1Context == NULL || KeySize > INT_MAX) {\r |
16d2c32c | 102 | return FALSE;\r |
103 | }\r | |
a8c44645 | 104 | \r |
105 | //\r | |
106 | // OpenSSL HMAC-SHA1 Context Initialization\r | |
107 | //\r | |
4c270243 QL |
108 | memset(HmacSha1Context, 0, HMAC_SHA1_CTX_SIZE);\r |
109 | if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha1Context) != 1) {\r | |
110 | return FALSE;\r | |
111 | }\r | |
112 | if (HMAC_Init_ex ((HMAC_CTX *)HmacSha1Context, Key, (UINT32) KeySize, EVP_sha1(), NULL) != 1) {\r | |
113 | return FALSE;\r | |
114 | }\r | |
a8c44645 | 115 | \r |
116 | return TRUE;\r | |
117 | }\r | |
118 | \r | |
119 | /**\r | |
120 | Makes a copy of an existing HMAC-SHA1 context.\r | |
121 | \r | |
16d2c32c | 122 | If HmacSha1Context is NULL, then return FALSE.\r |
123 | If NewHmacSha1Context is NULL, then return FALSE.\r | |
a8c44645 | 124 | \r |
125 | @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.\r | |
126 | @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.\r | |
127 | \r | |
128 | @retval TRUE HMAC-SHA1 context copy succeeded.\r | |
129 | @retval FALSE HMAC-SHA1 context copy failed.\r | |
130 | \r | |
131 | **/\r | |
132 | BOOLEAN\r | |
133 | EFIAPI\r | |
134 | HmacSha1Duplicate (\r | |
135 | IN CONST VOID *HmacSha1Context,\r | |
136 | OUT VOID *NewHmacSha1Context\r | |
137 | )\r | |
138 | {\r | |
4a567c96 | 139 | //\r |
16d2c32c | 140 | // Check input parameters.\r |
4a567c96 | 141 | //\r |
16d2c32c | 142 | if (HmacSha1Context == NULL || NewHmacSha1Context == NULL) {\r |
143 | return FALSE;\r | |
144 | }\r | |
4a567c96 | 145 | \r |
4c270243 QL |
146 | if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha1Context, (HMAC_CTX *)HmacSha1Context) != 1) {\r |
147 | return FALSE;\r | |
148 | }\r | |
a8c44645 | 149 | \r |
150 | return TRUE;\r | |
151 | }\r | |
152 | \r | |
153 | /**\r | |
154 | Digests the input data and updates HMAC-SHA1 context.\r | |
155 | \r | |
156 | This function performs HMAC-SHA1 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 | |
2998af86 | 158 | HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should not\r |
a8c44645 | 159 | be finalized by HmacSha1Final(). Behavior with invalid context is undefined.\r |
160 | \r | |
16d2c32c | 161 | If HmacSha1Context is NULL, then return FALSE.\r |
a8c44645 | 162 | \r |
163 | @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 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-SHA1 data digest succeeded.\r | |
168 | @retval FALSE HMAC-SHA1 data digest failed.\r | |
169 | \r | |
170 | **/\r | |
171 | BOOLEAN\r | |
172 | EFIAPI\r | |
173 | HmacSha1Update (\r | |
174 | IN OUT VOID *HmacSha1Context,\r | |
175 | IN CONST VOID *Data,\r | |
176 | IN UINTN DataSize\r | |
177 | )\r | |
178 | {\r | |
179 | //\r | |
16d2c32c | 180 | // Check input parameters.\r |
a8c44645 | 181 | //\r |
16d2c32c | 182 | if (HmacSha1Context == NULL) {\r |
183 | return FALSE;\r | |
184 | }\r | |
a8c44645 | 185 | \r |
186 | //\r | |
16d2c32c | 187 | // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r |
a8c44645 | 188 | //\r |
16d2c32c | 189 | if (Data == NULL && DataSize != 0) {\r |
190 | return FALSE;\r | |
a8c44645 | 191 | }\r |
192 | \r | |
193 | //\r | |
194 | // OpenSSL HMAC-SHA1 digest update\r | |
195 | //\r | |
4c270243 QL |
196 | if (HMAC_Update ((HMAC_CTX *)HmacSha1Context, Data, DataSize) != 1) {\r |
197 | return FALSE;\r | |
198 | }\r | |
a8c44645 | 199 | \r |
200 | return TRUE;\r | |
201 | }\r | |
202 | \r | |
203 | /**\r | |
204 | Completes computation of the HMAC-SHA1 digest value.\r | |
205 | \r | |
206 | This function completes HMAC-SHA1 digest computation and retrieves the digest value into\r | |
207 | the specified memory. After this function has been called, the HMAC-SHA1 context cannot\r | |
208 | be used again.\r | |
2998af86 | 209 | HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should\r |
a8c44645 | 210 | not be finalized by HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.\r |
211 | \r | |
16d2c32c | 212 | If HmacSha1Context is NULL, then return FALSE.\r |
213 | If HmacValue is NULL, then return FALSE.\r | |
a8c44645 | 214 | \r |
215 | @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r | |
216 | @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest\r | |
217 | value (20 bytes).\r | |
218 | \r | |
219 | @retval TRUE HMAC-SHA1 digest computation succeeded.\r | |
220 | @retval FALSE HMAC-SHA1 digest computation failed.\r | |
221 | \r | |
222 | **/\r | |
223 | BOOLEAN\r | |
224 | EFIAPI\r | |
225 | HmacSha1Final (\r | |
226 | IN OUT VOID *HmacSha1Context,\r | |
227 | OUT UINT8 *HmacValue\r | |
228 | )\r | |
229 | {\r | |
230 | UINT32 Length;\r | |
231 | \r | |
232 | //\r | |
16d2c32c | 233 | // Check input parameters.\r |
a8c44645 | 234 | //\r |
16d2c32c | 235 | if (HmacSha1Context == NULL || HmacValue == NULL) {\r |
236 | return FALSE;\r | |
237 | }\r | |
a8c44645 | 238 | \r |
239 | //\r | |
240 | // OpenSSL HMAC-SHA1 digest finalization\r | |
241 | //\r | |
4c270243 QL |
242 | if (HMAC_Final ((HMAC_CTX *)HmacSha1Context, HmacValue, &Length) != 1) {\r |
243 | return FALSE;\r | |
244 | }\r | |
245 | if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha1Context) != 1) {\r | |
246 | return FALSE;\r | |
247 | }\r | |
a8c44645 | 248 | \r |
249 | return TRUE;\r | |
250 | }\r |