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