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