]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha512.c
CryptoPkg Updates to support RFC3161 timestamp signature verification.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptSha512.c
CommitLineData
2ac68e8b
QL
1/** @file\r
2 SHA-384 and SHA-512 Digest Wrapper Implementations over OpenSSL.\r
3\r
4Copyright (c) 2014, 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/sha.h>\r
17\r
18/**\r
19 Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.\r
20\r
21 @return The size, in bytes, of the context buffer required for SHA-384 hash operations.\r
22\r
23**/\r
24UINTN\r
25EFIAPI\r
26Sha384GetContextSize (\r
27 VOID\r
28 )\r
29{\r
30 //\r
31 // Retrieves OpenSSL SHA-384 Context Size\r
32 //\r
33 return (UINTN) (sizeof (SHA512_CTX));\r
34}\r
35\r
36/**\r
37 Initializes user-supplied memory pointed by Sha384Context as SHA-384 hash context for\r
38 subsequent use.\r
39\r
40 If Sha384Context is NULL, then return FALSE.\r
41\r
42 @param[out] Sha384Context Pointer to SHA-384 context being initialized.\r
43\r
44 @retval TRUE SHA-384 context initialization succeeded.\r
45 @retval FALSE SHA-384 context initialization failed.\r
46\r
47**/\r
48BOOLEAN\r
49EFIAPI\r
50Sha384Init (\r
51 OUT VOID *Sha384Context\r
52 )\r
53{\r
54 //\r
55 // Check input parameters.\r
56 //\r
57 if (Sha384Context == NULL) {\r
58 return FALSE;\r
59 }\r
60\r
61 //\r
62 // OpenSSL SHA-384 Context Initialization\r
63 //\r
64 return (BOOLEAN) (SHA384_Init ((SHA512_CTX *) Sha384Context));\r
65}\r
66\r
67/**\r
68 Makes a copy of an existing SHA-384 context.\r
69\r
70 If Sha384Context is NULL, then return FALSE.\r
71 If NewSha384Context is NULL, then return FALSE.\r
72 If this interface is not supported, then return FALSE.\r
73\r
74 @param[in] Sha384Context Pointer to SHA-384 context being copied.\r
75 @param[out] NewSha384Context Pointer to new SHA-384 context.\r
76\r
77 @retval TRUE SHA-384 context copy succeeded.\r
78 @retval FALSE SHA-384 context copy failed.\r
79 @retval FALSE This interface is not supported.\r
80\r
81**/\r
82BOOLEAN\r
83EFIAPI\r
84Sha384Duplicate (\r
85 IN CONST VOID *Sha384Context,\r
86 OUT VOID *NewSha384Context\r
87 )\r
88{\r
89 //\r
90 // Check input parameters.\r
91 //\r
92 if (Sha384Context == NULL || NewSha384Context == NULL) {\r
93 return FALSE;\r
94 }\r
95\r
96 CopyMem (NewSha384Context, Sha384Context, sizeof (SHA512_CTX));\r
97\r
98 return TRUE;\r
99}\r
100\r
101/**\r
102 Digests the input data and updates SHA-384 context.\r
103\r
104 This function performs SHA-384 digest on a data buffer of the specified size.\r
105 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
106 SHA-384 context should be already correctly intialized by Sha384Init(), and should not be finalized\r
107 by Sha384Final(). Behavior with invalid context is undefined.\r
108\r
109 If Sha384Context is NULL, then return FALSE.\r
110\r
111 @param[in, out] Sha384Context Pointer to the SHA-384 context.\r
112 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
113 @param[in] DataSize Size of Data buffer in bytes.\r
114\r
115 @retval TRUE SHA-384 data digest succeeded.\r
116 @retval FALSE SHA-384 data digest failed.\r
117\r
118**/\r
119BOOLEAN\r
120EFIAPI\r
121Sha384Update (\r
122 IN OUT VOID *Sha384Context,\r
123 IN CONST VOID *Data,\r
124 IN UINTN DataSize\r
125 )\r
126{\r
127 //\r
128 // Check input parameters.\r
129 //\r
130 if (Sha384Context == NULL) {\r
131 return FALSE;\r
132 }\r
133\r
134 //\r
135 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
136 //\r
137 if (Data == NULL && DataSize != 0) {\r
138 return FALSE;\r
139 }\r
140\r
141 //\r
142 // OpenSSL SHA-384 Hash Update\r
143 //\r
144 return (BOOLEAN) (SHA384_Update ((SHA512_CTX *) Sha384Context, Data, DataSize));\r
145}\r
146\r
147/**\r
148 Completes computation of the SHA-384 digest value.\r
149\r
150 This function completes SHA-384 hash computation and retrieves the digest value into\r
151 the specified memory. After this function has been called, the SHA-384 context cannot\r
152 be used again.\r
153 SHA-384 context should be already correctly intialized by Sha384Init(), and should not be\r
154 finalized by Sha384Final(). Behavior with invalid SHA-384 context is undefined.\r
155\r
156 If Sha384Context is NULL, then return FALSE.\r
157 If HashValue is NULL, then return FALSE.\r
158\r
159 @param[in, out] Sha384Context Pointer to the SHA-384 context.\r
160 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest\r
161 value (48 bytes).\r
162\r
163 @retval TRUE SHA-384 digest computation succeeded.\r
164 @retval FALSE SHA-384 digest computation failed.\r
165\r
166**/\r
167BOOLEAN\r
168EFIAPI\r
169Sha384Final (\r
170 IN OUT VOID *Sha384Context,\r
171 OUT UINT8 *HashValue\r
172 )\r
173{\r
174 //\r
175 // Check input parameters.\r
176 //\r
177 if (Sha384Context == NULL || HashValue == NULL) {\r
178 return FALSE;\r
179 }\r
180\r
181 //\r
182 // OpenSSL SHA-384 Hash Finalization\r
183 //\r
184 return (BOOLEAN) (SHA384_Final (HashValue, (SHA512_CTX *) Sha384Context));\r
185}\r
186\r
187/**\r
188 Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.\r
189\r
190 @return The size, in bytes, of the context buffer required for SHA-512 hash operations.\r
191\r
192**/\r
193UINTN\r
194EFIAPI\r
195Sha512GetContextSize (\r
196 VOID\r
197 )\r
198{\r
199 //\r
200 // Retrieves OpenSSL SHA-512 Context Size\r
201 //\r
202 return (UINTN) (sizeof (SHA512_CTX));\r
203}\r
204\r
205/**\r
206 Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for\r
207 subsequent use.\r
208\r
209 If Sha512Context is NULL, then return FALSE.\r
210\r
211 @param[out] Sha512Context Pointer to SHA-512 context being initialized.\r
212\r
213 @retval TRUE SHA-512 context initialization succeeded.\r
214 @retval FALSE SHA-512 context initialization failed.\r
215\r
216**/\r
217BOOLEAN\r
218EFIAPI\r
219Sha512Init (\r
220 OUT VOID *Sha512Context\r
221 )\r
222{\r
223 //\r
224 // Check input parameters.\r
225 //\r
226 if (Sha512Context == NULL) {\r
227 return FALSE;\r
228 }\r
229\r
230 //\r
231 // OpenSSL SHA-512 Context Initialization\r
232 //\r
233 return (BOOLEAN) (SHA512_Init ((SHA512_CTX *) Sha512Context));\r
234}\r
235\r
236/**\r
237 Makes a copy of an existing SHA-512 context.\r
238\r
239 If Sha512Context is NULL, then return FALSE.\r
240 If NewSha512Context is NULL, then return FALSE.\r
241 If this interface is not supported, then return FALSE.\r
242\r
243 @param[in] Sha512Context Pointer to SHA-512 context being copied.\r
244 @param[out] NewSha512Context Pointer to new SHA-512 context.\r
245\r
246 @retval TRUE SHA-512 context copy succeeded.\r
247 @retval FALSE SHA-512 context copy failed.\r
248 @retval FALSE This interface is not supported.\r
249\r
250**/\r
251BOOLEAN\r
252EFIAPI\r
253Sha512Duplicate (\r
254 IN CONST VOID *Sha512Context,\r
255 OUT VOID *NewSha512Context\r
256 )\r
257{\r
258 //\r
259 // Check input parameters.\r
260 //\r
261 if (Sha512Context == NULL || NewSha512Context == NULL) {\r
262 return FALSE;\r
263 }\r
264\r
265 CopyMem (NewSha512Context, Sha512Context, sizeof (SHA512_CTX));\r
266\r
267 return TRUE;\r
268}\r
269\r
270/**\r
271 Digests the input data and updates SHA-512 context.\r
272\r
273 This function performs SHA-512 digest on a data buffer of the specified size.\r
274 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
275 SHA-512 context should be already correctly intialized by Sha512Init(), and should not be finalized\r
276 by Sha512Final(). Behavior with invalid context is undefined.\r
277\r
278 If Sha512Context is NULL, then return FALSE.\r
279\r
280 @param[in, out] Sha512Context Pointer to the SHA-512 context.\r
281 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
282 @param[in] DataSize Size of Data buffer in bytes.\r
283\r
284 @retval TRUE SHA-512 data digest succeeded.\r
285 @retval FALSE SHA-512 data digest failed.\r
286\r
287**/\r
288BOOLEAN\r
289EFIAPI\r
290Sha512Update (\r
291 IN OUT VOID *Sha512Context,\r
292 IN CONST VOID *Data,\r
293 IN UINTN DataSize\r
294 )\r
295{\r
296 //\r
297 // Check input parameters.\r
298 //\r
299 if (Sha512Context == NULL) {\r
300 return FALSE;\r
301 }\r
302\r
303 //\r
304 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
305 //\r
306 if (Data == NULL && DataSize != 0) {\r
307 return FALSE;\r
308 }\r
309\r
310 //\r
311 // OpenSSL SHA-512 Hash Update\r
312 //\r
313 return (BOOLEAN) (SHA512_Update ((SHA512_CTX *) Sha512Context, Data, DataSize));\r
314}\r
315\r
316/**\r
317 Completes computation of the SHA-512 digest value.\r
318\r
319 This function completes SHA-512 hash computation and retrieves the digest value into\r
320 the specified memory. After this function has been called, the SHA-512 context cannot\r
321 be used again.\r
322 SHA-512 context should be already correctly intialized by Sha512Init(), and should not be\r
323 finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.\r
324\r
325 If Sha512Context is NULL, then return FALSE.\r
326 If HashValue is NULL, then return FALSE.\r
327\r
328 @param[in, out] Sha512Context Pointer to the SHA-512 context.\r
329 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest\r
330 value (64 bytes).\r
331\r
332 @retval TRUE SHA-512 digest computation succeeded.\r
333 @retval FALSE SHA-512 digest computation failed.\r
334\r
335**/\r
336BOOLEAN\r
337EFIAPI\r
338Sha512Final (\r
339 IN OUT VOID *Sha512Context,\r
340 OUT UINT8 *HashValue\r
341 )\r
342{\r
343 //\r
344 // Check input parameters.\r
345 //\r
346 if (Sha512Context == NULL || HashValue == NULL) {\r
347 return FALSE;\r
348 }\r
349\r
350 //\r
351 // OpenSSL SHA-512 Hash Finalization\r
352 //\r
353 return (BOOLEAN) (SHA384_Final (HashValue, (SHA512_CTX *) Sha512Context));\r
354}\r