]>
Commit | Line | Data |
---|---|---|
2ac68e8b QL |
1 | /** @file\r |
2 | SHA-384 and SHA-512 Digest Wrapper Implementations over OpenSSL.\r | |
3 | \r | |
b7d1ba0a | 4 | Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved.<BR>\r |
2009f6b4 | 5 | SPDX-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 | |
18 | UINTN\r | |
19 | EFIAPI\r | |
20 | Sha384GetContextSize (\r | |
21 | VOID\r | |
22 | )\r | |
23 | {\r | |
24 | //\r | |
25 | // Retrieves OpenSSL SHA-384 Context Size\r | |
26 | //\r | |
27 | return (UINTN) (sizeof (SHA512_CTX));\r | |
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 | |
42 | BOOLEAN\r | |
43 | EFIAPI\r | |
44 | Sha384Init (\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 | |
58 | return (BOOLEAN) (SHA384_Init ((SHA512_CTX *) Sha384Context));\r | |
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 | |
76 | BOOLEAN\r | |
77 | EFIAPI\r | |
78 | Sha384Duplicate (\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 | |
86 | if (Sha384Context == NULL || NewSha384Context == NULL) {\r | |
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 | |
113 | BOOLEAN\r | |
114 | EFIAPI\r | |
115 | Sha384Update (\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 | |
131 | if (Data == NULL && DataSize != 0) {\r | |
132 | return FALSE;\r | |
133 | }\r | |
134 | \r | |
135 | //\r | |
136 | // OpenSSL SHA-384 Hash Update\r | |
137 | //\r | |
138 | return (BOOLEAN) (SHA384_Update ((SHA512_CTX *) Sha384Context, Data, DataSize));\r | |
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 | |
161 | BOOLEAN\r | |
162 | EFIAPI\r | |
163 | Sha384Final (\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 | |
171 | if (Sha384Context == NULL || HashValue == NULL) {\r | |
172 | return FALSE;\r | |
173 | }\r | |
174 | \r | |
175 | //\r | |
176 | // OpenSSL SHA-384 Hash Finalization\r | |
177 | //\r | |
178 | return (BOOLEAN) (SHA384_Final (HashValue, (SHA512_CTX *) Sha384Context));\r | |
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 | |
199 | BOOLEAN\r | |
200 | EFIAPI\r | |
201 | Sha384HashAll (\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 | |
213 | if (Data == NULL && DataSize != 0) {\r | |
214 | return FALSE;\r | |
215 | }\r | |
216 | \r | |
217 | //\r | |
218 | // OpenSSL SHA-384 Hash Computation.\r | |
219 | //\r | |
220 | if (SHA384 (Data, DataSize, HashValue) == NULL) {\r | |
221 | return FALSE;\r | |
222 | } else {\r | |
223 | return TRUE;\r | |
224 | }\r | |
225 | }\r | |
226 | \r | |
2ac68e8b QL |
227 | /**\r |
228 | Retrieves the size, in bytes, of the context buffer required for SHA-512 hash operations.\r | |
229 | \r | |
230 | @return The size, in bytes, of the context buffer required for SHA-512 hash operations.\r | |
231 | \r | |
232 | **/\r | |
233 | UINTN\r | |
234 | EFIAPI\r | |
235 | Sha512GetContextSize (\r | |
236 | VOID\r | |
237 | )\r | |
238 | {\r | |
239 | //\r | |
240 | // Retrieves OpenSSL SHA-512 Context Size\r | |
241 | //\r | |
242 | return (UINTN) (sizeof (SHA512_CTX));\r | |
243 | }\r | |
244 | \r | |
245 | /**\r | |
246 | Initializes user-supplied memory pointed by Sha512Context as SHA-512 hash context for\r | |
247 | subsequent use.\r | |
248 | \r | |
249 | If Sha512Context is NULL, then return FALSE.\r | |
250 | \r | |
251 | @param[out] Sha512Context Pointer to SHA-512 context being initialized.\r | |
252 | \r | |
253 | @retval TRUE SHA-512 context initialization succeeded.\r | |
254 | @retval FALSE SHA-512 context initialization failed.\r | |
255 | \r | |
256 | **/\r | |
257 | BOOLEAN\r | |
258 | EFIAPI\r | |
259 | Sha512Init (\r | |
260 | OUT VOID *Sha512Context\r | |
261 | )\r | |
262 | {\r | |
263 | //\r | |
264 | // Check input parameters.\r | |
265 | //\r | |
266 | if (Sha512Context == NULL) {\r | |
267 | return FALSE;\r | |
268 | }\r | |
269 | \r | |
270 | //\r | |
271 | // OpenSSL SHA-512 Context Initialization\r | |
272 | //\r | |
273 | return (BOOLEAN) (SHA512_Init ((SHA512_CTX *) Sha512Context));\r | |
274 | }\r | |
275 | \r | |
276 | /**\r | |
277 | Makes a copy of an existing SHA-512 context.\r | |
278 | \r | |
279 | If Sha512Context is NULL, then return FALSE.\r | |
280 | If NewSha512Context is NULL, then return FALSE.\r | |
281 | If this interface is not supported, then return FALSE.\r | |
282 | \r | |
283 | @param[in] Sha512Context Pointer to SHA-512 context being copied.\r | |
284 | @param[out] NewSha512Context Pointer to new SHA-512 context.\r | |
285 | \r | |
286 | @retval TRUE SHA-512 context copy succeeded.\r | |
287 | @retval FALSE SHA-512 context copy failed.\r | |
288 | @retval FALSE This interface is not supported.\r | |
289 | \r | |
290 | **/\r | |
291 | BOOLEAN\r | |
292 | EFIAPI\r | |
293 | Sha512Duplicate (\r | |
294 | IN CONST VOID *Sha512Context,\r | |
295 | OUT VOID *NewSha512Context\r | |
296 | )\r | |
297 | {\r | |
298 | //\r | |
299 | // Check input parameters.\r | |
300 | //\r | |
301 | if (Sha512Context == NULL || NewSha512Context == NULL) {\r | |
302 | return FALSE;\r | |
303 | }\r | |
304 | \r | |
305 | CopyMem (NewSha512Context, Sha512Context, sizeof (SHA512_CTX));\r | |
306 | \r | |
307 | return TRUE;\r | |
308 | }\r | |
309 | \r | |
310 | /**\r | |
311 | Digests the input data and updates SHA-512 context.\r | |
312 | \r | |
313 | This function performs SHA-512 digest on a data buffer of the specified size.\r | |
314 | It can be called multiple times to compute the digest of long or discontinuous data streams.\r | |
2998af86 | 315 | SHA-512 context should be already correctly initialized by Sha512Init(), and should not be finalized\r |
2ac68e8b QL |
316 | by Sha512Final(). Behavior with invalid context is undefined.\r |
317 | \r | |
318 | If Sha512Context is NULL, then return FALSE.\r | |
319 | \r | |
320 | @param[in, out] Sha512Context Pointer to the SHA-512 context.\r | |
321 | @param[in] Data Pointer to the buffer containing the data to be hashed.\r | |
322 | @param[in] DataSize Size of Data buffer in bytes.\r | |
323 | \r | |
324 | @retval TRUE SHA-512 data digest succeeded.\r | |
325 | @retval FALSE SHA-512 data digest failed.\r | |
326 | \r | |
327 | **/\r | |
328 | BOOLEAN\r | |
329 | EFIAPI\r | |
330 | Sha512Update (\r | |
331 | IN OUT VOID *Sha512Context,\r | |
332 | IN CONST VOID *Data,\r | |
333 | IN UINTN DataSize\r | |
334 | )\r | |
335 | {\r | |
336 | //\r | |
337 | // Check input parameters.\r | |
338 | //\r | |
339 | if (Sha512Context == NULL) {\r | |
340 | return FALSE;\r | |
341 | }\r | |
342 | \r | |
343 | //\r | |
344 | // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r | |
345 | //\r | |
346 | if (Data == NULL && DataSize != 0) {\r | |
347 | return FALSE;\r | |
348 | }\r | |
349 | \r | |
350 | //\r | |
351 | // OpenSSL SHA-512 Hash Update\r | |
352 | //\r | |
353 | return (BOOLEAN) (SHA512_Update ((SHA512_CTX *) Sha512Context, Data, DataSize));\r | |
354 | }\r | |
355 | \r | |
356 | /**\r | |
357 | Completes computation of the SHA-512 digest value.\r | |
358 | \r | |
359 | This function completes SHA-512 hash computation and retrieves the digest value into\r | |
360 | the specified memory. After this function has been called, the SHA-512 context cannot\r | |
361 | be used again.\r | |
2998af86 | 362 | SHA-512 context should be already correctly initialized by Sha512Init(), and should not be\r |
2ac68e8b QL |
363 | finalized by Sha512Final(). Behavior with invalid SHA-512 context is undefined.\r |
364 | \r | |
365 | If Sha512Context is NULL, then return FALSE.\r | |
366 | If HashValue is NULL, then return FALSE.\r | |
367 | \r | |
368 | @param[in, out] Sha512Context Pointer to the SHA-512 context.\r | |
369 | @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest\r | |
370 | value (64 bytes).\r | |
371 | \r | |
372 | @retval TRUE SHA-512 digest computation succeeded.\r | |
373 | @retval FALSE SHA-512 digest computation failed.\r | |
374 | \r | |
375 | **/\r | |
376 | BOOLEAN\r | |
377 | EFIAPI\r | |
378 | Sha512Final (\r | |
379 | IN OUT VOID *Sha512Context,\r | |
380 | OUT UINT8 *HashValue\r | |
381 | )\r | |
382 | {\r | |
383 | //\r | |
384 | // Check input parameters.\r | |
385 | //\r | |
386 | if (Sha512Context == NULL || HashValue == NULL) {\r | |
387 | return FALSE;\r | |
388 | }\r | |
389 | \r | |
390 | //\r | |
391 | // OpenSSL SHA-512 Hash Finalization\r | |
392 | //\r | |
393 | return (BOOLEAN) (SHA384_Final (HashValue, (SHA512_CTX *) Sha512Context));\r | |
394 | }\r | |
b7d1ba0a QL |
395 | \r |
396 | /**\r | |
397 | Computes the SHA-512 message digest of a input data buffer.\r | |
398 | \r | |
399 | This function performs the SHA-512 message digest of a given data buffer, and places\r | |
400 | the digest value into the specified memory.\r | |
401 | \r | |
402 | If this interface is not supported, then return FALSE.\r | |
403 | \r | |
404 | @param[in] Data Pointer to the buffer containing the data to be hashed.\r | |
405 | @param[in] DataSize Size of Data buffer in bytes.\r | |
406 | @param[out] HashValue Pointer to a buffer that receives the SHA-512 digest\r | |
407 | value (64 bytes).\r | |
408 | \r | |
409 | @retval TRUE SHA-512 digest computation succeeded.\r | |
410 | @retval FALSE SHA-512 digest computation failed.\r | |
411 | @retval FALSE This interface is not supported.\r | |
412 | \r | |
413 | **/\r | |
414 | BOOLEAN\r | |
415 | EFIAPI\r | |
416 | Sha512HashAll (\r | |
417 | IN CONST VOID *Data,\r | |
418 | IN UINTN DataSize,\r | |
419 | OUT UINT8 *HashValue\r | |
420 | )\r | |
421 | {\r | |
422 | //\r | |
423 | // Check input parameters.\r | |
424 | //\r | |
425 | if (HashValue == NULL) {\r | |
426 | return FALSE;\r | |
427 | }\r | |
428 | if (Data == NULL && DataSize != 0) {\r | |
429 | return FALSE;\r | |
430 | }\r | |
431 | \r | |
432 | //\r | |
433 | // OpenSSL SHA-512 Hash Computation.\r | |
434 | //\r | |
435 | if (SHA512 (Data, DataSize, HashValue) == NULL) {\r | |
436 | return FALSE;\r | |
437 | } else {\r | |
438 | return TRUE;\r | |
439 | }\r | |
440 | }\r |