]>
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 |
a8c44645 | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "InternalCryptLib.h"\r | |
16 | #include <openssl/hmac.h>\r | |
17 | \r | |
4c270243 QL |
18 | #define HMAC_SHA1_CTX_SIZE sizeof(void *) * 4 + sizeof(unsigned int) + \\r |
19 | sizeof(unsigned char) * HMAC_MAX_MD_CBLOCK\r | |
20 | \r | |
a8c44645 | 21 | /**\r |
22 | Retrieves the size, in bytes, of the context buffer required for HMAC-SHA1 operations.\r | |
4c270243 QL |
23 | (NOTE: This API is deprecated.\r |
24 | Use HmacSha1New() / HmacSha1Free() for HMAC-SHA1 Context operations.)\r | |
a8c44645 | 25 | \r |
26 | @return The size, in bytes, of the context buffer required for HMAC-SHA1 operations.\r | |
27 | \r | |
28 | **/\r | |
29 | UINTN\r | |
30 | EFIAPI\r | |
31 | HmacSha1GetContextSize (\r | |
32 | VOID\r | |
33 | )\r | |
34 | {\r | |
35 | //\r | |
36 | // Retrieves the OpenSSL HMAC-SHA1 Context Size\r | |
4c270243 QL |
37 | // NOTE: HMAC_CTX object was made opaque in openssl-1.1.x, here we just use the\r |
38 | // fixed size as a workaround to make this API work for compatibility.\r | |
39 | // We should retire HmacSha15GetContextSize() in future, and use HmacSha1New()\r | |
40 | // and HmacSha1Free() for context allocation and release.\r | |
41 | //\r | |
42 | return (UINTN) HMAC_SHA1_CTX_SIZE;\r | |
43 | }\r | |
44 | \r | |
45 | /**\r | |
46 | Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA1 use.\r | |
47 | \r | |
48 | @return Pointer to the HMAC_CTX context that has been initialized.\r | |
49 | If the allocations fails, HmacSha1New() returns NULL.\r | |
50 | \r | |
51 | **/\r | |
52 | VOID *\r | |
53 | EFIAPI\r | |
54 | HmacSha1New (\r | |
55 | VOID\r | |
56 | )\r | |
57 | {\r | |
58 | //\r | |
59 | // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()\r | |
60 | //\r | |
61 | return (VOID *) HMAC_CTX_new ();\r | |
62 | }\r | |
63 | \r | |
64 | /**\r | |
65 | Release the specified HMAC_CTX context.\r | |
66 | \r | |
67 | @param[in] HmacSha1Ctx Pointer to the HMAC_CTX context to be released.\r | |
68 | \r | |
69 | **/\r | |
70 | VOID\r | |
71 | EFIAPI\r | |
72 | HmacSha1Free (\r | |
73 | IN VOID *HmacSha1Ctx\r | |
74 | )\r | |
75 | {\r | |
a8c44645 | 76 | //\r |
4c270243 QL |
77 | // Free OpenSSL HMAC_CTX Context\r |
78 | //\r | |
79 | HMAC_CTX_free ((HMAC_CTX *)HmacSha1Ctx);\r | |
a8c44645 | 80 | }\r |
81 | \r | |
82 | /**\r | |
83 | Initializes user-supplied memory pointed by HmacSha1Context as HMAC-SHA1 context for\r | |
84 | subsequent use.\r | |
85 | \r | |
16d2c32c | 86 | If HmacSha1Context is NULL, then return FALSE.\r |
a8c44645 | 87 | \r |
88 | @param[out] HmacSha1Context Pointer to HMAC-SHA1 context being initialized.\r | |
89 | @param[in] Key Pointer to the user-supplied key.\r | |
90 | @param[in] KeySize Key size in bytes.\r | |
91 | \r | |
92 | @retval TRUE HMAC-SHA1 context initialization succeeded.\r | |
93 | @retval FALSE HMAC-SHA1 context initialization failed.\r | |
94 | \r | |
95 | **/\r | |
96 | BOOLEAN\r | |
97 | EFIAPI\r | |
98 | HmacSha1Init (\r | |
99 | OUT VOID *HmacSha1Context,\r | |
100 | IN CONST UINT8 *Key,\r | |
101 | IN UINTN KeySize\r | |
102 | )\r | |
103 | {\r | |
104 | //\r | |
16d2c32c | 105 | // Check input parameters.\r |
a8c44645 | 106 | //\r |
dda39f3a | 107 | if (HmacSha1Context == NULL || KeySize > INT_MAX) {\r |
16d2c32c | 108 | return FALSE;\r |
109 | }\r | |
a8c44645 | 110 | \r |
111 | //\r | |
112 | // OpenSSL HMAC-SHA1 Context Initialization\r | |
113 | //\r | |
4c270243 QL |
114 | memset(HmacSha1Context, 0, HMAC_SHA1_CTX_SIZE);\r |
115 | if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha1Context) != 1) {\r | |
116 | return FALSE;\r | |
117 | }\r | |
118 | if (HMAC_Init_ex ((HMAC_CTX *)HmacSha1Context, Key, (UINT32) KeySize, EVP_sha1(), NULL) != 1) {\r | |
119 | return FALSE;\r | |
120 | }\r | |
a8c44645 | 121 | \r |
122 | return TRUE;\r | |
123 | }\r | |
124 | \r | |
125 | /**\r | |
126 | Makes a copy of an existing HMAC-SHA1 context.\r | |
127 | \r | |
16d2c32c | 128 | If HmacSha1Context is NULL, then return FALSE.\r |
129 | If NewHmacSha1Context is NULL, then return FALSE.\r | |
a8c44645 | 130 | \r |
131 | @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.\r | |
132 | @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.\r | |
133 | \r | |
134 | @retval TRUE HMAC-SHA1 context copy succeeded.\r | |
135 | @retval FALSE HMAC-SHA1 context copy failed.\r | |
136 | \r | |
137 | **/\r | |
138 | BOOLEAN\r | |
139 | EFIAPI\r | |
140 | HmacSha1Duplicate (\r | |
141 | IN CONST VOID *HmacSha1Context,\r | |
142 | OUT VOID *NewHmacSha1Context\r | |
143 | )\r | |
144 | {\r | |
4a567c96 | 145 | //\r |
16d2c32c | 146 | // Check input parameters.\r |
4a567c96 | 147 | //\r |
16d2c32c | 148 | if (HmacSha1Context == NULL || NewHmacSha1Context == NULL) {\r |
149 | return FALSE;\r | |
150 | }\r | |
4a567c96 | 151 | \r |
4c270243 QL |
152 | if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha1Context, (HMAC_CTX *)HmacSha1Context) != 1) {\r |
153 | return FALSE;\r | |
154 | }\r | |
a8c44645 | 155 | \r |
156 | return TRUE;\r | |
157 | }\r | |
158 | \r | |
159 | /**\r | |
160 | Digests the input data and updates HMAC-SHA1 context.\r | |
161 | \r | |
162 | This function performs HMAC-SHA1 digest on a data buffer of the specified size.\r | |
163 | It can be called multiple times to compute the digest of long or discontinuous data streams.\r | |
2998af86 | 164 | HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should not\r |
a8c44645 | 165 | be finalized by HmacSha1Final(). Behavior with invalid context is undefined.\r |
166 | \r | |
16d2c32c | 167 | If HmacSha1Context is NULL, then return FALSE.\r |
a8c44645 | 168 | \r |
169 | @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r | |
170 | @param[in] Data Pointer to the buffer containing the data to be digested.\r | |
171 | @param[in] DataSize Size of Data buffer in bytes.\r | |
172 | \r | |
173 | @retval TRUE HMAC-SHA1 data digest succeeded.\r | |
174 | @retval FALSE HMAC-SHA1 data digest failed.\r | |
175 | \r | |
176 | **/\r | |
177 | BOOLEAN\r | |
178 | EFIAPI\r | |
179 | HmacSha1Update (\r | |
180 | IN OUT VOID *HmacSha1Context,\r | |
181 | IN CONST VOID *Data,\r | |
182 | IN UINTN DataSize\r | |
183 | )\r | |
184 | {\r | |
185 | //\r | |
16d2c32c | 186 | // Check input parameters.\r |
a8c44645 | 187 | //\r |
16d2c32c | 188 | if (HmacSha1Context == NULL) {\r |
189 | return FALSE;\r | |
190 | }\r | |
a8c44645 | 191 | \r |
192 | //\r | |
16d2c32c | 193 | // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r |
a8c44645 | 194 | //\r |
16d2c32c | 195 | if (Data == NULL && DataSize != 0) {\r |
196 | return FALSE;\r | |
a8c44645 | 197 | }\r |
198 | \r | |
199 | //\r | |
200 | // OpenSSL HMAC-SHA1 digest update\r | |
201 | //\r | |
4c270243 QL |
202 | if (HMAC_Update ((HMAC_CTX *)HmacSha1Context, Data, DataSize) != 1) {\r |
203 | return FALSE;\r | |
204 | }\r | |
a8c44645 | 205 | \r |
206 | return TRUE;\r | |
207 | }\r | |
208 | \r | |
209 | /**\r | |
210 | Completes computation of the HMAC-SHA1 digest value.\r | |
211 | \r | |
212 | This function completes HMAC-SHA1 digest computation and retrieves the digest value into\r | |
213 | the specified memory. After this function has been called, the HMAC-SHA1 context cannot\r | |
214 | be used again.\r | |
2998af86 | 215 | HMAC-SHA1 context should be already correctly initialized by HmacSha1Init(), and should\r |
a8c44645 | 216 | not be finalized by HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.\r |
217 | \r | |
16d2c32c | 218 | If HmacSha1Context is NULL, then return FALSE.\r |
219 | If HmacValue is NULL, then return FALSE.\r | |
a8c44645 | 220 | \r |
221 | @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r | |
222 | @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest\r | |
223 | value (20 bytes).\r | |
224 | \r | |
225 | @retval TRUE HMAC-SHA1 digest computation succeeded.\r | |
226 | @retval FALSE HMAC-SHA1 digest computation failed.\r | |
227 | \r | |
228 | **/\r | |
229 | BOOLEAN\r | |
230 | EFIAPI\r | |
231 | HmacSha1Final (\r | |
232 | IN OUT VOID *HmacSha1Context,\r | |
233 | OUT UINT8 *HmacValue\r | |
234 | )\r | |
235 | {\r | |
236 | UINT32 Length;\r | |
237 | \r | |
238 | //\r | |
16d2c32c | 239 | // Check input parameters.\r |
a8c44645 | 240 | //\r |
16d2c32c | 241 | if (HmacSha1Context == NULL || HmacValue == NULL) {\r |
242 | return FALSE;\r | |
243 | }\r | |
a8c44645 | 244 | \r |
245 | //\r | |
246 | // OpenSSL HMAC-SHA1 digest finalization\r | |
247 | //\r | |
4c270243 QL |
248 | if (HMAC_Final ((HMAC_CTX *)HmacSha1Context, HmacValue, &Length) != 1) {\r |
249 | return FALSE;\r | |
250 | }\r | |
251 | if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha1Context) != 1) {\r | |
252 | return FALSE;\r | |
253 | }\r | |
a8c44645 | 254 | \r |
255 | return TRUE;\r | |
256 | }\r |