]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha1.c
Update CryptoPkg for new ciphers (HMAC, Block Cipher, etc) supports.
[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
92 CopyMem (NewHmacSha1Context, HmacSha1Context, sizeof (HMAC_CTX));\r
93\r
94 return TRUE;\r
95}\r
96\r
97/**\r
98 Digests the input data and updates HMAC-SHA1 context.\r
99\r
100 This function performs HMAC-SHA1 digest on a data buffer of the specified size.\r
101 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
102 HMAC-SHA1 context should be already correctly intialized by HmacSha1Init(), and should not\r
103 be finalized by HmacSha1Final(). Behavior with invalid context is undefined.\r
104\r
105 If HmacSha1Context is NULL, then ASSERT().\r
106\r
107 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r
108 @param[in] Data Pointer to the buffer containing the data to be digested.\r
109 @param[in] DataSize Size of Data buffer in bytes.\r
110\r
111 @retval TRUE HMAC-SHA1 data digest succeeded.\r
112 @retval FALSE HMAC-SHA1 data digest failed.\r
113\r
114**/\r
115BOOLEAN\r
116EFIAPI\r
117HmacSha1Update (\r
118 IN OUT VOID *HmacSha1Context,\r
119 IN CONST VOID *Data,\r
120 IN UINTN DataSize\r
121 )\r
122{\r
123 //\r
124 // ASSERT if HmacSha1Context is NULL\r
125 //\r
126 ASSERT (HmacSha1Context != NULL);\r
127\r
128 //\r
129 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
130 //\r
131 if (Data == NULL) {\r
132 ASSERT (DataSize == 0);\r
133 }\r
134\r
135 //\r
136 // OpenSSL HMAC-SHA1 digest update\r
137 //\r
138 HMAC_Update (HmacSha1Context, Data, DataSize);\r
139\r
140 return TRUE;\r
141}\r
142\r
143/**\r
144 Completes computation of the HMAC-SHA1 digest value.\r
145\r
146 This function completes HMAC-SHA1 digest computation and retrieves the digest value into\r
147 the specified memory. After this function has been called, the HMAC-SHA1 context cannot\r
148 be used again.\r
149 HMAC-SHA1 context should be already correctly intialized by HmacSha1Init(), and should\r
150 not be finalized by HmacSha1Final(). Behavior with invalid HMAC-SHA1 context is undefined.\r
151\r
152 If HmacSha1Context is NULL, then ASSERT().\r
153 If HmacValue is NULL, then ASSERT().\r
154\r
155 @param[in, out] HmacSha1Context Pointer to the HMAC-SHA1 context.\r
156 @param[out] HmacValue Pointer to a buffer that receives the HMAC-SHA1 digest\r
157 value (20 bytes).\r
158\r
159 @retval TRUE HMAC-SHA1 digest computation succeeded.\r
160 @retval FALSE HMAC-SHA1 digest computation failed.\r
161\r
162**/\r
163BOOLEAN\r
164EFIAPI\r
165HmacSha1Final (\r
166 IN OUT VOID *HmacSha1Context,\r
167 OUT UINT8 *HmacValue\r
168 )\r
169{\r
170 UINT32 Length;\r
171\r
172 //\r
173 // ASSERT if HmacSha1Context is NULL or HmacValue is NULL\r
174 //\r
175 ASSERT (HmacSha1Context != NULL);\r
176 ASSERT (HmacValue != NULL);\r
177\r
178 //\r
179 // OpenSSL HMAC-SHA1 digest finalization\r
180 //\r
181 HMAC_Final (HmacSha1Context, HmacValue, &Length);\r
182 HMAC_CTX_cleanup (HmacSha1Context);\r
183\r
184 return TRUE;\r
185}\r