]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hmac/CryptHmacSha256.c
CryptoPkg: Add HMAC-SHA256 cipher support
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hmac / CryptHmacSha256.c
CommitLineData
72009c62
QL
1/** @file\r
2 HMAC-SHA256 Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2016, 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-SHA256 operations.\r
20\r
21 @return The size, in bytes, of the context buffer required for HMAC-SHA256 operations.\r
22\r
23**/\r
24UINTN\r
25EFIAPI\r
26HmacSha256GetContextSize (\r
27 VOID\r
28 )\r
29{\r
30 //\r
31 // Retrieves the OpenSSL HMAC-SHA256 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 HmacSha256Context as HMAC-SHA256 context for\r
38 subsequent use.\r
39\r
40 If HmacSha256Context is NULL, then return FALSE.\r
41\r
42 @param[out] HmacSha256Context Pointer to HMAC-SHA256 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-SHA256 context initialization succeeded.\r
47 @retval FALSE HMAC-SHA256 context initialization failed.\r
48\r
49**/\r
50BOOLEAN\r
51EFIAPI\r
52HmacSha256Init (\r
53 OUT VOID *HmacSha256Context,\r
54 IN CONST UINT8 *Key,\r
55 IN UINTN KeySize\r
56 )\r
57{\r
58 //\r
59 // Check input parameters.\r
60 //\r
61 if (HmacSha256Context == NULL || KeySize > INT_MAX) {\r
62 return FALSE;\r
63 }\r
64\r
65 //\r
66 // OpenSSL HMAC-SHA256 Context Initialization\r
67 //\r
68 HMAC_CTX_init (HmacSha256Context);\r
69 HMAC_Init_ex (HmacSha256Context, Key, (UINT32) KeySize, EVP_sha256(), NULL);\r
70\r
71 return TRUE;\r
72}\r
73\r
74/**\r
75 Makes a copy of an existing HMAC-SHA256 context.\r
76\r
77 If HmacSha256Context is NULL, then return FALSE.\r
78 If NewHmacSha256Context is NULL, then return FALSE.\r
79\r
80 @param[in] HmacSha256Context Pointer to HMAC-SHA256 context being copied.\r
81 @param[out] NewHmacSha256Context Pointer to new HMAC-SHA256 context.\r
82\r
83 @retval TRUE HMAC-SHA256 context copy succeeded.\r
84 @retval FALSE HMAC-SHA256 context copy failed.\r
85\r
86**/\r
87BOOLEAN\r
88EFIAPI\r
89HmacSha256Duplicate (\r
90 IN CONST VOID *HmacSha256Context,\r
91 OUT VOID *NewHmacSha256Context\r
92 )\r
93{\r
94 //\r
95 // Check input parameters.\r
96 //\r
97 if (HmacSha256Context == NULL || NewHmacSha256Context == NULL) {\r
98 return FALSE;\r
99 }\r
100\r
101 CopyMem (NewHmacSha256Context, HmacSha256Context, sizeof (HMAC_CTX));\r
102\r
103 return TRUE;\r
104}\r
105\r
106/**\r
107 Digests the input data and updates HMAC-SHA256 context.\r
108\r
109 This function performs HMAC-SHA256 digest on a data buffer of the specified size.\r
110 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
111 HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should not\r
112 be finalized by HmacSha256Final(). Behavior with invalid context is undefined.\r
113\r
114 If HmacSha256Context is NULL, then return FALSE.\r
115\r
116 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.\r
117 @param[in] Data Pointer to the buffer containing the data to be digested.\r
118 @param[in] DataSize Size of Data buffer in bytes.\r
119\r
120 @retval TRUE HMAC-SHA256 data digest succeeded.\r
121 @retval FALSE HMAC-SHA256 data digest failed.\r
122\r
123**/\r
124BOOLEAN\r
125EFIAPI\r
126HmacSha256Update (\r
127 IN OUT VOID *HmacSha256Context,\r
128 IN CONST VOID *Data,\r
129 IN UINTN DataSize\r
130 )\r
131{\r
132 //\r
133 // Check input parameters.\r
134 //\r
135 if (HmacSha256Context == NULL) {\r
136 return FALSE;\r
137 }\r
138\r
139 //\r
140 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
141 //\r
142 if (Data == NULL && DataSize != 0) {\r
143 return FALSE;\r
144 }\r
145\r
146 //\r
147 // OpenSSL HMAC-SHA256 digest update\r
148 //\r
149 HMAC_Update (HmacSha256Context, Data, DataSize);\r
150\r
151 return TRUE;\r
152}\r
153\r
154/**\r
155 Completes computation of the HMAC-SHA256 digest value.\r
156\r
157 This function completes HMAC-SHA256 hash computation and retrieves the digest value into\r
158 the specified memory. After this function has been called, the HMAC-SHA256 context cannot\r
159 be used again.\r
160 HMAC-SHA256 context should be already correctly initialized by HmacSha256Init(), and should\r
161 not be finalized by HmacSha256Final(). Behavior with invalid HMAC-SHA256 context is undefined.\r
162\r
163 If HmacSha256Context is NULL, then return FALSE.\r
164 If HashValue is NULL, then return FALSE.\r
165\r
166 @param[in, out] HmacSha256Context Pointer to the HMAC-SHA256 context.\r
167 @param[out] HashValue Pointer to a buffer that receives the HMAC-SHA256 digest\r
168 value (32 bytes).\r
169\r
170 @retval TRUE HMAC-SHA256 digest computation succeeded.\r
171 @retval FALSE HMAC-SHA256 digest computation failed.\r
172\r
173**/\r
174BOOLEAN\r
175EFIAPI\r
176HmacSha256Final (\r
177 IN OUT VOID *HmacSha256Context,\r
178 OUT UINT8 *HmacValue\r
179 )\r
180{\r
181 UINT32 Length;\r
182\r
183 //\r
184 // Check input parameters.\r
185 //\r
186 if (HmacSha256Context == NULL || HmacValue == NULL) {\r
187 return FALSE;\r
188 }\r
189\r
190 //\r
191 // OpenSSL HMAC-SHA256 digest finalization\r
192 //\r
193 HMAC_Final (HmacSha256Context, HmacValue, &Length);\r
194 HMAC_CTX_cleanup (HmacSha256Context);\r
195\r
196 return TRUE;\r
197}\r