]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha512.c
CryptoPkg: Add xxxxHashAll APIs to facilitate the digest computation
[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
b7d1ba0a 4Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved.<BR>\r
2ac68e8b
QL
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
b7d1ba0a
QL
187/**\r
188 Computes the SHA-384 message digest of a input data buffer.\r
189\r
190 This function performs the SHA-384 message digest of a given data buffer, and places\r
191 the digest value into the specified memory.\r
192\r
193 If this interface is not supported, then return FALSE.\r
194\r
195 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
196 @param[in] DataSize Size of Data buffer in bytes.\r
197 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest\r
198 value (48 bytes).\r
199\r
200 @retval TRUE SHA-384 digest computation succeeded.\r
201 @retval FALSE SHA-384 digest computation failed.\r
202 @retval FALSE This interface is not supported.\r
203\r
204**/\r
205BOOLEAN\r
206EFIAPI\r
207Sha384HashAll (\r
208 IN CONST VOID *Data,\r
209 IN UINTN DataSize,\r
210 OUT UINT8 *HashValue\r
211 )\r
212{\r
213 //\r
214 // Check input parameters.\r
215 //\r
216 if (HashValue == NULL) {\r
217 return FALSE;\r
218 }\r
219 if (Data == NULL && DataSize != 0) {\r
220 return FALSE;\r
221 }\r
222\r
223 //\r
224 // OpenSSL SHA-384 Hash Computation.\r
225 //\r
226 if (SHA384 (Data, DataSize, HashValue) == NULL) {\r
227 return FALSE;\r
228 } else {\r
229 return TRUE;\r
230 }\r
231}\r
232\r
2ac68e8b
QL
233/**\r
234 Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.\r
235\r
236 @return The size, in bytes, of the context buffer required for SHA-512 hash operations.\r
237\r
238**/\r
239UINTN\r
240EFIAPI\r
241Sha512GetContextSize (\r
242 VOID\r
243 )\r
244{\r
245 //\r
246 // Retrieves OpenSSL SHA-512 Context Size\r
247 //\r
248 return (UINTN) (sizeof (SHA512_CTX));\r
249}\r
250\r
251/**\r
252 Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for\r
253 subsequent use.\r
254\r
255 If Sha512Context is NULL, then return FALSE.\r
256\r
257 @param[out] Sha512Context Pointer to SHA-512 context being initialized.\r
258\r
259 @retval TRUE SHA-512 context initialization succeeded.\r
260 @retval FALSE SHA-512 context initialization failed.\r
261\r
262**/\r
263BOOLEAN\r
264EFIAPI\r
265Sha512Init (\r
266 OUT VOID *Sha512Context\r
267 )\r
268{\r
269 //\r
270 // Check input parameters.\r
271 //\r
272 if (Sha512Context == NULL) {\r
273 return FALSE;\r
274 }\r
275\r
276 //\r
277 // OpenSSL SHA-512 Context Initialization\r
278 //\r
279 return (BOOLEAN) (SHA512_Init ((SHA512_CTX *) Sha512Context));\r
280}\r
281\r
282/**\r
283 Makes a copy of an existing SHA-512 context.\r
284\r
285 If Sha512Context is NULL, then return FALSE.\r
286 If NewSha512Context is NULL, then return FALSE.\r
287 If this interface is not supported, then return FALSE.\r
288\r
289 @param[in] Sha512Context Pointer to SHA-512 context being copied.\r
290 @param[out] NewSha512Context Pointer to new SHA-512 context.\r
291\r
292 @retval TRUE SHA-512 context copy succeeded.\r
293 @retval FALSE SHA-512 context copy failed.\r
294 @retval FALSE This interface is not supported.\r
295\r
296**/\r
297BOOLEAN\r
298EFIAPI\r
299Sha512Duplicate (\r
300 IN CONST VOID *Sha512Context,\r
301 OUT VOID *NewSha512Context\r
302 )\r
303{\r
304 //\r
305 // Check input parameters.\r
306 //\r
307 if (Sha512Context == NULL || NewSha512Context == NULL) {\r
308 return FALSE;\r
309 }\r
310\r
311 CopyMem (NewSha512Context, Sha512Context, sizeof (SHA512_CTX));\r
312\r
313 return TRUE;\r
314}\r
315\r
316/**\r
317 Digests the input data and updates SHA-512 context.\r
318\r
319 This function performs SHA-512 digest on a data buffer of the specified size.\r
320 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
321 SHA-512 context should be already correctly intialized by Sha512Init(), and should not be finalized\r
322 by Sha512Final(). Behavior with invalid context is undefined.\r
323\r
324 If Sha512Context is NULL, then return FALSE.\r
325\r
326 @param[in, out] Sha512Context Pointer to the SHA-512 context.\r
327 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
328 @param[in] DataSize Size of Data buffer in bytes.\r
329\r
330 @retval TRUE SHA-512 data digest succeeded.\r
331 @retval FALSE SHA-512 data digest failed.\r
332\r
333**/\r
334BOOLEAN\r
335EFIAPI\r
336Sha512Update (\r
337 IN OUT VOID *Sha512Context,\r
338 IN CONST VOID *Data,\r
339 IN UINTN DataSize\r
340 )\r
341{\r
342 //\r
343 // Check input parameters.\r
344 //\r
345 if (Sha512Context == NULL) {\r
346 return FALSE;\r
347 }\r
348\r
349 //\r
350 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
351 //\r
352 if (Data == NULL && DataSize != 0) {\r
353 return FALSE;\r
354 }\r
355\r
356 //\r
357 // OpenSSL SHA-512 Hash Update\r
358 //\r
359 return (BOOLEAN) (SHA512_Update ((SHA512_CTX *) Sha512Context, Data, DataSize));\r
360}\r
361\r
362/**\r
363 Completes computation of the SHA-512 digest value.\r
364\r
365 This function completes SHA-512 hash computation and retrieves the digest value into\r
366 the specified memory. After this function has been called, the SHA-512 context cannot\r
367 be used again.\r
368 SHA-512 context should be already correctly intialized by Sha512Init(), and should not be\r
369 finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.\r
370\r
371 If Sha512Context is NULL, then return FALSE.\r
372 If HashValue is NULL, then return FALSE.\r
373\r
374 @param[in, out] Sha512Context Pointer to the SHA-512 context.\r
375 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest\r
376 value (64 bytes).\r
377\r
378 @retval TRUE SHA-512 digest computation succeeded.\r
379 @retval FALSE SHA-512 digest computation failed.\r
380\r
381**/\r
382BOOLEAN\r
383EFIAPI\r
384Sha512Final (\r
385 IN OUT VOID *Sha512Context,\r
386 OUT UINT8 *HashValue\r
387 )\r
388{\r
389 //\r
390 // Check input parameters.\r
391 //\r
392 if (Sha512Context == NULL || HashValue == NULL) {\r
393 return FALSE;\r
394 }\r
395\r
396 //\r
397 // OpenSSL SHA-512 Hash Finalization\r
398 //\r
399 return (BOOLEAN) (SHA384_Final (HashValue, (SHA512_CTX *) Sha512Context));\r
400}\r
b7d1ba0a
QL
401\r
402/**\r
403 Computes the SHA-512 message digest of a input data buffer.\r
404\r
405 This function performs the SHA-512 message digest of a given data buffer, and places\r
406 the digest value into the specified memory.\r
407\r
408 If this interface is not supported, then return FALSE.\r
409\r
410 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
411 @param[in] DataSize Size of Data buffer in bytes.\r
412 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest\r
413 value (64 bytes).\r
414\r
415 @retval TRUE SHA-512 digest computation succeeded.\r
416 @retval FALSE SHA-512 digest computation failed.\r
417 @retval FALSE This interface is not supported.\r
418\r
419**/\r
420BOOLEAN\r
421EFIAPI\r
422Sha512HashAll (\r
423 IN CONST VOID *Data,\r
424 IN UINTN DataSize,\r
425 OUT UINT8 *HashValue\r
426 )\r
427{\r
428 //\r
429 // Check input parameters.\r
430 //\r
431 if (HashValue == NULL) {\r
432 return FALSE;\r
433 }\r
434 if (Data == NULL && DataSize != 0) {\r
435 return FALSE;\r
436 }\r
437\r
438 //\r
439 // OpenSSL SHA-512 Hash Computation.\r
440 //\r
441 if (SHA512 (Data, DataSize, HashValue) == NULL) {\r
442 return FALSE;\r
443 } else {\r
444 return TRUE;\r
445 }\r
446}\r