]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha1.c
1. Add new API supports for PEM & X509 key retrieving & verification;
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacSha1.c
CommitLineData
a8c44645 1/** @file\r
2 HMAC-SHA1 Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT 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
18/**\r
19 Retrieves the size, in bytes, of the context buffer required for HMAC-SHA1 operations.\r
20\r
21 @return The size, in bytes, of the context buffer required for HMAC-SHA1 operations.\r
22\r
23**/\r
24UINTN\r
25EFIAPI\r
26HmacSha1GetContextSize (\r
27 VOID\r
28 )\r
29{\r
30 //\r
31 // Retrieves the OpenSSL HMAC-SHA1 Context Size\r
32 //\r
33 return (UINTN)(sizeof (HMAC_CTX));\r
34}\r
35\r
36/**\r
37 Initializes user-supplied memory pointed by HmacSha1Context as HMAC-SHA1 context for\r
38 subsequent use.\r
39\r
40 If HmacSha1Context is NULL, then ASSERT().\r
41\r
42 @param[out] HmacSha1Context Pointer to HMAC-SHA1 context being initialized.\r
43 @param[in] Key Pointer to the user-supplied key.\r
44 @param[in] KeySize Key size in bytes.\r
45\r
46 @retval TRUE HMAC-SHA1 context initialization succeeded.\r
47 @retval FALSE HMAC-SHA1 context initialization failed.\r
48\r
49**/\r
50BOOLEAN\r
51EFIAPI\r
52HmacSha1Init (\r
53 OUT VOID *HmacSha1Context,\r
54 IN CONST UINT8 *Key,\r
55 IN UINTN KeySize\r
56 )\r
57{\r
58 //\r
59 // ASSERT if HmacSha1Context is NULL.\r
60 //\r
61 ASSERT (HmacSha1Context != NULL);\r
62\r
63 //\r
64 // OpenSSL HMAC-SHA1 Context Initialization\r
65 //\r
66 HMAC_CTX_init (HmacSha1Context);\r
67 HMAC_Init_ex (HmacSha1Context, Key, (UINT32) KeySize, EVP_sha1(), NULL);\r
68\r
69 return TRUE;\r
70}\r
71\r
72/**\r
73 Makes a copy of an existing HMAC-SHA1 context.\r
74\r
75 If HmacSha1Context is NULL, then ASSERT().\r
76 If NewHmacSha1Context is NULL, then ASSERT().\r
77\r
78 @param[in] HmacSha1Context Pointer to HMAC-SHA1 context being copied.\r
79 @param[out] NewHmacSha1Context Pointer to new HMAC-SHA1 context.\r
80\r
81 @retval TRUE HMAC-SHA1 context copy succeeded.\r
82 @retval FALSE HMAC-SHA1 context copy failed.\r
83\r
84**/\r
85BOOLEAN\r
86EFIAPI\r
87HmacSha1Duplicate (\r
88 IN CONST VOID *HmacSha1Context,\r
89 OUT VOID *NewHmacSha1Context\r
90 )\r
91{\r
4a567c96 92 //\r
93 // ASSERT if HmacSha1Context or NewHmacSha1Context is NULL.\r
94 //\r
95 ASSERT (HmacSha1Context != NULL);\r
96 ASSERT (NewHmacSha1Context != NULL);\r
97\r
a8c44645 98 CopyMem (NewHmacSha1Context, HmacSha1Context, sizeof (HMAC_CTX));\r
99\r
100 return TRUE;\r
101}\r
102\r
103/**\r
104 Digests the input data and updates HMAC-SHA1 context.\r
105\r
106 This function performs HMAC-SHA1 digest on a data buffer of the specified size.\r
107 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
108 HMAC-SHA1 context should be already correctly intialized by HmacSha1Init(), and should not\r
109 be finalized by HmacSha1Final(). Behavior with invalid context is undefined.\r
110\r
111 If HmacSha1Context is NULL, then ASSERT().\r
112\r
113 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r
114 @param[in] Data Pointer to the buffer containing the data to be digested.\r
115 @param[in] DataSize Size of Data buffer in bytes.\r
116\r
117 @retval TRUE HMAC-SHA1 data digest succeeded.\r
118 @retval FALSE HMAC-SHA1 data digest failed.\r
119\r
120**/\r
121BOOLEAN\r
122EFIAPI\r
123HmacSha1Update (\r
124 IN OUT VOID *HmacSha1Context,\r
125 IN CONST VOID *Data,\r
126 IN UINTN DataSize\r
127 )\r
128{\r
129 //\r
130 // ASSERT if HmacSha1Context is NULL\r
131 //\r
132 ASSERT (HmacSha1Context != NULL);\r
133\r
134 //\r
135 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
136 //\r
137 if (Data == NULL) {\r
138 ASSERT (DataSize == 0);\r
139 }\r
140\r
141 //\r
142 // OpenSSL HMAC-SHA1 digest update\r
143 //\r
144 HMAC_Update (HmacSha1Context, Data, DataSize);\r
145\r
146 return TRUE;\r
147}\r
148\r
149/**\r
150 Completes computation of the HMAC-SHA1 digest value.\r
151\r
152 This function completes HMAC-SHA1 digest computation and retrieves the digest value into\r
153 the specified memory. After this function has been called, the HMAC-SHA1 context cannot\r
154 be used again.\r
155 HMAC-SHA1 context should be already correctly intialized by HmacSha1Init(), and should\r
156 not be finalized by HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.\r
157\r
158 If HmacSha1Context is NULL, then ASSERT().\r
159 If HmacValue is NULL, then ASSERT().\r
160\r
161 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r
162 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest\r
163 value (20 bytes).\r
164\r
165 @retval TRUE HMAC-SHA1 digest computation succeeded.\r
166 @retval FALSE HMAC-SHA1 digest computation failed.\r
167\r
168**/\r
169BOOLEAN\r
170EFIAPI\r
171HmacSha1Final (\r
172 IN OUT VOID *HmacSha1Context,\r
173 OUT UINT8 *HmacValue\r
174 )\r
175{\r
176 UINT32 Length;\r
177\r
178 //\r
179 // ASSERT if HmacSha1Context is NULL or HmacValue is NULL\r
180 //\r
181 ASSERT (HmacSha1Context != NULL);\r
182 ASSERT (HmacValue != NULL);\r
183\r
184 //\r
185 // OpenSSL HMAC-SHA1 digest finalization\r
186 //\r
187 HMAC_Final (HmacSha1Context, HmacValue, &Length);\r
188 HMAC_CTX_cleanup (HmacSha1Context);\r
189\r
190 return TRUE;\r
191}\r