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