]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha1.c
CryptoPkg/BaseCryptLib: remove HmacXxxGetContextSize interface
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacSha1.c
CommitLineData
a8c44645 1/** @file\r
2 HMAC-SHA1 Wrapper Implementation over OpenSSL.\r
3\r
9a1f14ad 4Copyright (c) 2010 - 2020, Intel Corporation. All rights reserved.<BR>\r
2009f6b4 5SPDX-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/**\r
13 Allocates and initializes one HMAC_CTX context for subsequent HMAC-SHA1 use.\r
14\r
15 @return Pointer to the HMAC_CTX context that has been initialized.\r
16 If the allocations fails, HmacSha1New() returns NULL.\r
17\r
18**/\r
19VOID *\r
20EFIAPI\r
21HmacSha1New (\r
22 VOID\r
23 )\r
24{\r
25 //\r
26 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()\r
27 //\r
28 return (VOID *) HMAC_CTX_new ();\r
29}\r
30\r
31/**\r
32 Release the specified HMAC_CTX context.\r
33\r
34 @param[in] HmacSha1Ctx Pointer to the HMAC_CTX context to be released.\r
35\r
36**/\r
37VOID\r
38EFIAPI\r
39HmacSha1Free (\r
40 IN VOID *HmacSha1Ctx\r
41 )\r
42{\r
a8c44645 43 //\r
4c270243
QL
44 // Free OpenSSL HMAC_CTX Context\r
45 //\r
46 HMAC_CTX_free ((HMAC_CTX *)HmacSha1Ctx);\r
a8c44645 47}\r
48\r
49/**\r
a23fdff6
JW
50 Set user-supplied key for subsequent use. It must be done before any\r
51 calling to HmacSha1Update().\r
a8c44645 52\r
16d2c32c 53 If HmacSha1Context is NULL, then return FALSE.\r
a8c44645 54\r
a23fdff6 55 @param[out] HmacSha1Context Pointer to HMAC-SHA1 context.\r
a8c44645 56 @param[in] Key Pointer to the user-supplied key.\r
57 @param[in] KeySize Key size in bytes.\r
58\r
a23fdff6
JW
59 @retval TRUE The Key is set successfully.\r
60 @retval FALSE The Key is set unsuccessfully.\r
a8c44645 61\r
62**/\r
63BOOLEAN\r
64EFIAPI\r
a23fdff6 65HmacSha1SetKey (\r
a8c44645 66 OUT VOID *HmacSha1Context,\r
67 IN CONST UINT8 *Key,\r
68 IN UINTN KeySize\r
69 )\r
70{\r
71 //\r
16d2c32c 72 // Check input parameters.\r
a8c44645 73 //\r
dda39f3a 74 if (HmacSha1Context == NULL || KeySize > INT_MAX) {\r
16d2c32c 75 return FALSE;\r
76 }\r
a8c44645 77\r
4c270243
QL
78 if (HMAC_Init_ex ((HMAC_CTX *)HmacSha1Context, Key, (UINT32) KeySize, EVP_sha1(), NULL) != 1) {\r
79 return FALSE;\r
80 }\r
a8c44645 81\r
82 return TRUE;\r
83}\r
84\r
85/**\r
86 Makes a copy of an existing HMAC-SHA1 context.\r
87\r
16d2c32c 88 If HmacSha1Context is NULL, then return FALSE.\r
89 If NewHmacSha1Context is NULL, then return FALSE.\r
a8c44645 90\r
91 @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.\r
92 @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.\r
93\r
94 @retval TRUE HMAC-SHA1 context copy succeeded.\r
95 @retval FALSE HMAC-SHA1 context copy failed.\r
96\r
97**/\r
98BOOLEAN\r
99EFIAPI\r
100HmacSha1Duplicate (\r
101 IN CONST VOID *HmacSha1Context,\r
102 OUT VOID *NewHmacSha1Context\r
103 )\r
104{\r
4a567c96 105 //\r
16d2c32c 106 // Check input parameters.\r
4a567c96 107 //\r
16d2c32c 108 if (HmacSha1Context == NULL || NewHmacSha1Context == NULL) {\r
109 return FALSE;\r
110 }\r
4a567c96 111\r
4c270243
QL
112 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacSha1Context, (HMAC_CTX *)HmacSha1Context) != 1) {\r
113 return FALSE;\r
114 }\r
a8c44645 115\r
116 return TRUE;\r
117}\r
118\r
119/**\r
120 Digests the input data and updates HMAC-SHA1 context.\r
121\r
122 This function performs HMAC-SHA1 digest on a data buffer of the specified size.\r
123 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
a23fdff6
JW
124 HMAC-SHA1 context should be initialized by HmacSha1New(), and should not be finalized by\r
125 HmacSha1Final(). Behavior with invalid context is undefined.\r
a8c44645 126\r
16d2c32c 127 If HmacSha1Context is NULL, then return FALSE.\r
a8c44645 128\r
129 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r
130 @param[in] Data Pointer to the buffer containing the data to be digested.\r
131 @param[in] DataSize Size of Data buffer in bytes.\r
132\r
133 @retval TRUE HMAC-SHA1 data digest succeeded.\r
134 @retval FALSE HMAC-SHA1 data digest failed.\r
135\r
136**/\r
137BOOLEAN\r
138EFIAPI\r
139HmacSha1Update (\r
140 IN OUT VOID *HmacSha1Context,\r
141 IN CONST VOID *Data,\r
142 IN UINTN DataSize\r
143 )\r
144{\r
145 //\r
16d2c32c 146 // Check input parameters.\r
a8c44645 147 //\r
16d2c32c 148 if (HmacSha1Context == NULL) {\r
149 return FALSE;\r
150 }\r
a8c44645 151\r
152 //\r
16d2c32c 153 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
a8c44645 154 //\r
16d2c32c 155 if (Data == NULL && DataSize != 0) {\r
156 return FALSE;\r
a8c44645 157 }\r
158\r
159 //\r
160 // OpenSSL HMAC-SHA1 digest update\r
161 //\r
4c270243
QL
162 if (HMAC_Update ((HMAC_CTX *)HmacSha1Context, Data, DataSize) != 1) {\r
163 return FALSE;\r
164 }\r
a8c44645 165\r
166 return TRUE;\r
167}\r
168\r
169/**\r
170 Completes computation of the HMAC-SHA1 digest value.\r
171\r
172 This function completes HMAC-SHA1 digest computation and retrieves the digest value into\r
173 the specified memory. After this function has been called, the HMAC-SHA1 context cannot\r
174 be used again.\r
a23fdff6
JW
175 HMAC-SHA1 context should be initialized by HmacSha1New(), and should not be finalized by\r
176 HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.\r
a8c44645 177\r
16d2c32c 178 If HmacSha1Context is NULL, then return FALSE.\r
179 If HmacValue is NULL, then return FALSE.\r
a8c44645 180\r
181 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r
182 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest\r
183 value (20 bytes).\r
184\r
185 @retval TRUE HMAC-SHA1 digest computation succeeded.\r
186 @retval FALSE HMAC-SHA1 digest computation failed.\r
187\r
188**/\r
189BOOLEAN\r
190EFIAPI\r
191HmacSha1Final (\r
192 IN OUT VOID *HmacSha1Context,\r
193 OUT UINT8 *HmacValue\r
194 )\r
195{\r
196 UINT32 Length;\r
197\r
198 //\r
16d2c32c 199 // Check input parameters.\r
a8c44645 200 //\r
16d2c32c 201 if (HmacSha1Context == NULL || HmacValue == NULL) {\r
202 return FALSE;\r
203 }\r
a8c44645 204\r
205 //\r
206 // OpenSSL HMAC-SHA1 digest finalization\r
207 //\r
4c270243
QL
208 if (HMAC_Final ((HMAC_CTX *)HmacSha1Context, HmacValue, &Length) != 1) {\r
209 return FALSE;\r
210 }\r
211 if (HMAC_CTX_reset ((HMAC_CTX *)HmacSha1Context) != 1) {\r
212 return FALSE;\r
213 }\r
a8c44645 214\r
215 return TRUE;\r
216}\r