]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha512.c
CryptoPkg: Apply uncrustify changes
[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
2009f6b4 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
2ac68e8b
QL
6\r
7**/\r
8\r
9#include "InternalCryptLib.h"\r
10#include <openssl/sha.h>\r
11\r
12/**\r
13 Retrieves the size, in bytes, of the context buffer required for SHA-384 hash operations.\r
14\r
15 @return The size, in bytes, of the context buffer required for SHA-384 hash operations.\r
16\r
17**/\r
18UINTN\r
19EFIAPI\r
20Sha384GetContextSize (\r
21 VOID\r
22 )\r
23{\r
24 //\r
25 // Retrieves OpenSSL SHA-384 Context Size\r
26 //\r
7c342378 27 return (UINTN)(sizeof (SHA512_CTX));\r
2ac68e8b
QL
28}\r
29\r
30/**\r
31 Initializes user-supplied memory pointed by Sha384Context as SHA-384 hash context for\r
32 subsequent use.\r
33\r
34 If Sha384Context is NULL, then return FALSE.\r
35\r
36 @param[out] Sha384Context Pointer to SHA-384 context being initialized.\r
37\r
38 @retval TRUE SHA-384 context initialization succeeded.\r
39 @retval FALSE SHA-384 context initialization failed.\r
40\r
41**/\r
42BOOLEAN\r
43EFIAPI\r
44Sha384Init (\r
45 OUT VOID *Sha384Context\r
46 )\r
47{\r
48 //\r
49 // Check input parameters.\r
50 //\r
51 if (Sha384Context == NULL) {\r
52 return FALSE;\r
53 }\r
54\r
55 //\r
56 // OpenSSL SHA-384 Context Initialization\r
57 //\r
7c342378 58 return (BOOLEAN)(SHA384_Init ((SHA512_CTX *)Sha384Context));\r
2ac68e8b
QL
59}\r
60\r
61/**\r
62 Makes a copy of an existing SHA-384 context.\r
63\r
64 If Sha384Context is NULL, then return FALSE.\r
65 If NewSha384Context is NULL, then return FALSE.\r
66 If this interface is not supported, then return FALSE.\r
67\r
68 @param[in] Sha384Context Pointer to SHA-384 context being copied.\r
69 @param[out] NewSha384Context Pointer to new SHA-384 context.\r
70\r
71 @retval TRUE SHA-384 context copy succeeded.\r
72 @retval FALSE SHA-384 context copy failed.\r
73 @retval FALSE This interface is not supported.\r
74\r
75**/\r
76BOOLEAN\r
77EFIAPI\r
78Sha384Duplicate (\r
79 IN CONST VOID *Sha384Context,\r
80 OUT VOID *NewSha384Context\r
81 )\r
82{\r
83 //\r
84 // Check input parameters.\r
85 //\r
7c342378 86 if ((Sha384Context == NULL) || (NewSha384Context == NULL)) {\r
2ac68e8b
QL
87 return FALSE;\r
88 }\r
89\r
90 CopyMem (NewSha384Context, Sha384Context, sizeof (SHA512_CTX));\r
91\r
92 return TRUE;\r
93}\r
94\r
95/**\r
96 Digests the input data and updates SHA-384 context.\r
97\r
98 This function performs SHA-384 digest on a data buffer of the specified size.\r
99 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
2998af86 100 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be finalized\r
2ac68e8b
QL
101 by Sha384Final(). Behavior with invalid context is undefined.\r
102\r
103 If Sha384Context is NULL, then return FALSE.\r
104\r
105 @param[in, out] Sha384Context Pointer to the SHA-384 context.\r
106 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
107 @param[in] DataSize Size of Data buffer in bytes.\r
108\r
109 @retval TRUE SHA-384 data digest succeeded.\r
110 @retval FALSE SHA-384 data digest failed.\r
111\r
112**/\r
113BOOLEAN\r
114EFIAPI\r
115Sha384Update (\r
116 IN OUT VOID *Sha384Context,\r
117 IN CONST VOID *Data,\r
118 IN UINTN DataSize\r
119 )\r
120{\r
121 //\r
122 // Check input parameters.\r
123 //\r
124 if (Sha384Context == NULL) {\r
125 return FALSE;\r
126 }\r
127\r
128 //\r
129 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
130 //\r
7c342378 131 if ((Data == NULL) && (DataSize != 0)) {\r
2ac68e8b
QL
132 return FALSE;\r
133 }\r
134\r
135 //\r
136 // OpenSSL SHA-384 Hash Update\r
137 //\r
7c342378 138 return (BOOLEAN)(SHA384_Update ((SHA512_CTX *)Sha384Context, Data, DataSize));\r
2ac68e8b
QL
139}\r
140\r
141/**\r
142 Completes computation of the SHA-384 digest value.\r
143\r
144 This function completes SHA-384 hash computation and retrieves the digest value into\r
145 the specified memory. After this function has been called, the SHA-384 context cannot\r
146 be used again.\r
2998af86 147 SHA-384 context should be already correctly initialized by Sha384Init(), and should not be\r
2ac68e8b
QL
148 finalized by Sha384Final(). Behavior with invalid SHA-384 context is undefined.\r
149\r
150 If Sha384Context is NULL, then return FALSE.\r
151 If HashValue is NULL, then return FALSE.\r
152\r
153 @param[in, out] Sha384Context Pointer to the SHA-384 context.\r
154 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest\r
155 value (48 bytes).\r
156\r
157 @retval TRUE SHA-384 digest computation succeeded.\r
158 @retval FALSE SHA-384 digest computation failed.\r
159\r
160**/\r
161BOOLEAN\r
162EFIAPI\r
163Sha384Final (\r
164 IN OUT VOID *Sha384Context,\r
165 OUT UINT8 *HashValue\r
166 )\r
167{\r
168 //\r
169 // Check input parameters.\r
170 //\r
7c342378 171 if ((Sha384Context == NULL) || (HashValue == NULL)) {\r
2ac68e8b
QL
172 return FALSE;\r
173 }\r
174\r
175 //\r
176 // OpenSSL SHA-384 Hash Finalization\r
177 //\r
7c342378 178 return (BOOLEAN)(SHA384_Final (HashValue, (SHA512_CTX *)Sha384Context));\r
2ac68e8b
QL
179}\r
180\r
b7d1ba0a
QL
181/**\r
182 Computes the SHA-384 message digest of a input data buffer.\r
183\r
184 This function performs the SHA-384 message digest of a given data buffer, and places\r
185 the digest value into the specified memory.\r
186\r
187 If this interface is not supported, then return FALSE.\r
188\r
189 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
190 @param[in] DataSize Size of Data buffer in bytes.\r
191 @param[out] HashValue Pointer to a buffer that receives the SHA-384 digest\r
192 value (48 bytes).\r
193\r
194 @retval TRUE SHA-384 digest computation succeeded.\r
195 @retval FALSE SHA-384 digest computation failed.\r
196 @retval FALSE This interface is not supported.\r
197\r
198**/\r
199BOOLEAN\r
200EFIAPI\r
201Sha384HashAll (\r
202 IN CONST VOID *Data,\r
203 IN UINTN DataSize,\r
204 OUT UINT8 *HashValue\r
205 )\r
206{\r
207 //\r
208 // Check input parameters.\r
209 //\r
210 if (HashValue == NULL) {\r
211 return FALSE;\r
212 }\r
7c342378
MK
213\r
214 if ((Data == NULL) && (DataSize != 0)) {\r
b7d1ba0a
QL
215 return FALSE;\r
216 }\r
217\r
218 //\r
219 // OpenSSL SHA-384 Hash Computation.\r
220 //\r
221 if (SHA384 (Data, DataSize, HashValue) == NULL) {\r
222 return FALSE;\r
223 } else {\r
224 return TRUE;\r
225 }\r
226}\r
227\r
2ac68e8b
QL
228/**\r
229 Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.\r
230\r
231 @return The size, in bytes, of the context buffer required for SHA-512 hash operations.\r
232\r
233**/\r
234UINTN\r
235EFIAPI\r
236Sha512GetContextSize (\r
237 VOID\r
238 )\r
239{\r
240 //\r
241 // Retrieves OpenSSL SHA-512 Context Size\r
242 //\r
7c342378 243 return (UINTN)(sizeof (SHA512_CTX));\r
2ac68e8b
QL
244}\r
245\r
246/**\r
247 Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for\r
248 subsequent use.\r
249\r
250 If Sha512Context is NULL, then return FALSE.\r
251\r
252 @param[out] Sha512Context Pointer to SHA-512 context being initialized.\r
253\r
254 @retval TRUE SHA-512 context initialization succeeded.\r
255 @retval FALSE SHA-512 context initialization failed.\r
256\r
257**/\r
258BOOLEAN\r
259EFIAPI\r
260Sha512Init (\r
261 OUT VOID *Sha512Context\r
262 )\r
263{\r
264 //\r
265 // Check input parameters.\r
266 //\r
267 if (Sha512Context == NULL) {\r
268 return FALSE;\r
269 }\r
270\r
271 //\r
272 // OpenSSL SHA-512 Context Initialization\r
273 //\r
7c342378 274 return (BOOLEAN)(SHA512_Init ((SHA512_CTX *)Sha512Context));\r
2ac68e8b
QL
275}\r
276\r
277/**\r
278 Makes a copy of an existing SHA-512 context.\r
279\r
280 If Sha512Context is NULL, then return FALSE.\r
281 If NewSha512Context is NULL, then return FALSE.\r
282 If this interface is not supported, then return FALSE.\r
283\r
284 @param[in] Sha512Context Pointer to SHA-512 context being copied.\r
285 @param[out] NewSha512Context Pointer to new SHA-512 context.\r
286\r
287 @retval TRUE SHA-512 context copy succeeded.\r
288 @retval FALSE SHA-512 context copy failed.\r
289 @retval FALSE This interface is not supported.\r
290\r
291**/\r
292BOOLEAN\r
293EFIAPI\r
294Sha512Duplicate (\r
295 IN CONST VOID *Sha512Context,\r
296 OUT VOID *NewSha512Context\r
297 )\r
298{\r
299 //\r
300 // Check input parameters.\r
301 //\r
7c342378 302 if ((Sha512Context == NULL) || (NewSha512Context == NULL)) {\r
2ac68e8b
QL
303 return FALSE;\r
304 }\r
305\r
306 CopyMem (NewSha512Context, Sha512Context, sizeof (SHA512_CTX));\r
307\r
308 return TRUE;\r
309}\r
310\r
311/**\r
312 Digests the input data and updates SHA-512 context.\r
313\r
314 This function performs SHA-512 digest on a data buffer of the specified size.\r
315 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
2998af86 316 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be finalized\r
2ac68e8b
QL
317 by Sha512Final(). Behavior with invalid context is undefined.\r
318\r
319 If Sha512Context is NULL, then return FALSE.\r
320\r
321 @param[in, out] Sha512Context Pointer to the SHA-512 context.\r
322 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
323 @param[in] DataSize Size of Data buffer in bytes.\r
324\r
325 @retval TRUE SHA-512 data digest succeeded.\r
326 @retval FALSE SHA-512 data digest failed.\r
327\r
328**/\r
329BOOLEAN\r
330EFIAPI\r
331Sha512Update (\r
332 IN OUT VOID *Sha512Context,\r
333 IN CONST VOID *Data,\r
334 IN UINTN DataSize\r
335 )\r
336{\r
337 //\r
338 // Check input parameters.\r
339 //\r
340 if (Sha512Context == NULL) {\r
341 return FALSE;\r
342 }\r
343\r
344 //\r
345 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
346 //\r
7c342378 347 if ((Data == NULL) && (DataSize != 0)) {\r
2ac68e8b
QL
348 return FALSE;\r
349 }\r
350\r
351 //\r
352 // OpenSSL SHA-512 Hash Update\r
353 //\r
7c342378 354 return (BOOLEAN)(SHA512_Update ((SHA512_CTX *)Sha512Context, Data, DataSize));\r
2ac68e8b
QL
355}\r
356\r
357/**\r
358 Completes computation of the SHA-512 digest value.\r
359\r
360 This function completes SHA-512 hash computation and retrieves the digest value into\r
361 the specified memory. After this function has been called, the SHA-512 context cannot\r
362 be used again.\r
2998af86 363 SHA-512 context should be already correctly initialized by Sha512Init(), and should not be\r
2ac68e8b
QL
364 finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.\r
365\r
366 If Sha512Context is NULL, then return FALSE.\r
367 If HashValue is NULL, then return FALSE.\r
368\r
369 @param[in, out] Sha512Context Pointer to the SHA-512 context.\r
370 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest\r
371 value (64 bytes).\r
372\r
373 @retval TRUE SHA-512 digest computation succeeded.\r
374 @retval FALSE SHA-512 digest computation failed.\r
375\r
376**/\r
377BOOLEAN\r
378EFIAPI\r
379Sha512Final (\r
380 IN OUT VOID *Sha512Context,\r
381 OUT UINT8 *HashValue\r
382 )\r
383{\r
384 //\r
385 // Check input parameters.\r
386 //\r
7c342378 387 if ((Sha512Context == NULL) || (HashValue == NULL)) {\r
2ac68e8b
QL
388 return FALSE;\r
389 }\r
390\r
391 //\r
392 // OpenSSL SHA-512 Hash Finalization\r
393 //\r
7c342378 394 return (BOOLEAN)(SHA384_Final (HashValue, (SHA512_CTX *)Sha512Context));\r
2ac68e8b 395}\r
b7d1ba0a
QL
396\r
397/**\r
398 Computes the SHA-512 message digest of a input data buffer.\r
399\r
400 This function performs the SHA-512 message digest of a given data buffer, and places\r
401 the digest value into the specified memory.\r
402\r
403 If this interface is not supported, then return FALSE.\r
404\r
405 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
406 @param[in] DataSize Size of Data buffer in bytes.\r
407 @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest\r
408 value (64 bytes).\r
409\r
410 @retval TRUE SHA-512 digest computation succeeded.\r
411 @retval FALSE SHA-512 digest computation failed.\r
412 @retval FALSE This interface is not supported.\r
413\r
414**/\r
415BOOLEAN\r
416EFIAPI\r
417Sha512HashAll (\r
418 IN CONST VOID *Data,\r
419 IN UINTN DataSize,\r
420 OUT UINT8 *HashValue\r
421 )\r
422{\r
423 //\r
424 // Check input parameters.\r
425 //\r
426 if (HashValue == NULL) {\r
427 return FALSE;\r
428 }\r
7c342378
MK
429\r
430 if ((Data == NULL) && (DataSize != 0)) {\r
b7d1ba0a
QL
431 return FALSE;\r
432 }\r
433\r
434 //\r
435 // OpenSSL SHA-512 Hash Computation.\r
436 //\r
437 if (SHA512 (Data, DataSize, HashValue) == NULL) {\r
438 return FALSE;\r
439 } else {\r
440 return TRUE;\r
441 }\r
442}\r