]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha256.c
CryptoPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptSha256.c
... / ...
CommitLineData
1/** @file\r
2 SHA-256 Digest Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
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-256 hash operations.\r
14\r
15 @return The size, in bytes, of the context buffer required for SHA-256 hash operations.\r
16\r
17**/\r
18UINTN\r
19EFIAPI\r
20Sha256GetContextSize (\r
21 VOID\r
22 )\r
23{\r
24 //\r
25 // Retrieves OpenSSL SHA-256 Context Size\r
26 //\r
27 return (UINTN) (sizeof (SHA256_CTX));\r
28}\r
29\r
30/**\r
31 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for\r
32 subsequent use.\r
33\r
34 If Sha256Context is NULL, then return FALSE.\r
35\r
36 @param[out] Sha256Context Pointer to SHA-256 context being initialized.\r
37\r
38 @retval TRUE SHA-256 context initialization succeeded.\r
39 @retval FALSE SHA-256 context initialization failed.\r
40\r
41**/\r
42BOOLEAN\r
43EFIAPI\r
44Sha256Init (\r
45 OUT VOID *Sha256Context\r
46 )\r
47{\r
48 //\r
49 // Check input parameters.\r
50 //\r
51 if (Sha256Context == NULL) {\r
52 return FALSE;\r
53 }\r
54\r
55 //\r
56 // OpenSSL SHA-256 Context Initialization\r
57 //\r
58 return (BOOLEAN) (SHA256_Init ((SHA256_CTX *) Sha256Context));\r
59}\r
60\r
61/**\r
62 Makes a copy of an existing SHA-256 context.\r
63\r
64 If Sha256Context is NULL, then return FALSE.\r
65 If NewSha256Context is NULL, then return FALSE.\r
66\r
67 @param[in] Sha256Context Pointer to SHA-256 context being copied.\r
68 @param[out] NewSha256Context Pointer to new SHA-256 context.\r
69\r
70 @retval TRUE SHA-256 context copy succeeded.\r
71 @retval FALSE SHA-256 context copy failed.\r
72\r
73**/\r
74BOOLEAN\r
75EFIAPI\r
76Sha256Duplicate (\r
77 IN CONST VOID *Sha256Context,\r
78 OUT VOID *NewSha256Context\r
79 )\r
80{\r
81 //\r
82 // Check input parameters.\r
83 //\r
84 if (Sha256Context == NULL || NewSha256Context == NULL) {\r
85 return FALSE;\r
86 }\r
87\r
88 CopyMem (NewSha256Context, Sha256Context, sizeof (SHA256_CTX));\r
89\r
90 return TRUE;\r
91}\r
92\r
93/**\r
94 Digests the input data and updates SHA-256 context.\r
95\r
96 This function performs SHA-256 digest on a data buffer of the specified size.\r
97 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
98 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be finalized\r
99 by Sha256Final(). Behavior with invalid context is undefined.\r
100\r
101 If Sha256Context is NULL, then return FALSE.\r
102\r
103 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
104 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
105 @param[in] DataSize Size of Data buffer in bytes.\r
106\r
107 @retval TRUE SHA-256 data digest succeeded.\r
108 @retval FALSE SHA-256 data digest failed.\r
109\r
110**/\r
111BOOLEAN\r
112EFIAPI\r
113Sha256Update (\r
114 IN OUT VOID *Sha256Context,\r
115 IN CONST VOID *Data,\r
116 IN UINTN DataSize\r
117 )\r
118{\r
119 //\r
120 // Check input parameters.\r
121 //\r
122 if (Sha256Context == NULL) {\r
123 return FALSE;\r
124 }\r
125\r
126 //\r
127 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
128 //\r
129 if (Data == NULL && DataSize != 0) {\r
130 return FALSE;\r
131 }\r
132\r
133 //\r
134 // OpenSSL SHA-256 Hash Update\r
135 //\r
136 return (BOOLEAN) (SHA256_Update ((SHA256_CTX *) Sha256Context, Data, DataSize));\r
137}\r
138\r
139/**\r
140 Completes computation of the SHA-256 digest value.\r
141\r
142 This function completes SHA-256 hash computation and retrieves the digest value into\r
143 the specified memory. After this function has been called, the SHA-256 context cannot\r
144 be used again.\r
145 SHA-256 context should be already correctly initialized by Sha256Init(), and should not be\r
146 finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.\r
147\r
148 If Sha256Context is NULL, then return FALSE.\r
149 If HashValue is NULL, then return FALSE.\r
150\r
151 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
152 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest\r
153 value (32 bytes).\r
154\r
155 @retval TRUE SHA-256 digest computation succeeded.\r
156 @retval FALSE SHA-256 digest computation failed.\r
157\r
158**/\r
159BOOLEAN\r
160EFIAPI\r
161Sha256Final (\r
162 IN OUT VOID *Sha256Context,\r
163 OUT UINT8 *HashValue\r
164 )\r
165{\r
166 //\r
167 // Check input parameters.\r
168 //\r
169 if (Sha256Context == NULL || HashValue == NULL) {\r
170 return FALSE;\r
171 }\r
172\r
173 //\r
174 // OpenSSL SHA-256 Hash Finalization\r
175 //\r
176 return (BOOLEAN) (SHA256_Final (HashValue, (SHA256_CTX *) Sha256Context));\r
177}\r
178\r
179/**\r
180 Computes the SHA-256 message digest of a input data buffer.\r
181\r
182 This function performs the SHA-256 message digest of a given data buffer, and places\r
183 the digest value into the specified memory.\r
184\r
185 If this interface is not supported, then return FALSE.\r
186\r
187 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
188 @param[in] DataSize Size of Data buffer in bytes.\r
189 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest\r
190 value (32 bytes).\r
191\r
192 @retval TRUE SHA-256 digest computation succeeded.\r
193 @retval FALSE SHA-256 digest computation failed.\r
194 @retval FALSE This interface is not supported.\r
195\r
196**/\r
197BOOLEAN\r
198EFIAPI\r
199Sha256HashAll (\r
200 IN CONST VOID *Data,\r
201 IN UINTN DataSize,\r
202 OUT UINT8 *HashValue\r
203 )\r
204{\r
205 //\r
206 // Check input parameters.\r
207 //\r
208 if (HashValue == NULL) {\r
209 return FALSE;\r
210 }\r
211 if (Data == NULL && DataSize != 0) {\r
212 return FALSE;\r
213 }\r
214\r
215 //\r
216 // OpenSSL SHA-256 Hash Computation.\r
217 //\r
218 if (SHA256 (Data, DataSize, HashValue) == NULL) {\r
219 return FALSE;\r
220 } else {\r
221 return TRUE;\r
222 }\r
223}\r