]>
Commit | Line | Data |
---|---|---|
97f98500 HT |
1 | /** @file\r |
2 | SHA-256 Digest Wrapper Implementation over OpenSSL.\r | |
3 | \r | |
b7d1ba0a | 4 | Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r |
2009f6b4 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
97f98500 HT |
6 | \r |
7 | **/\r | |
8 | \r | |
a8c44645 | 9 | #include "InternalCryptLib.h"\r |
97f98500 HT |
10 | #include <openssl/sha.h>\r |
11 | \r | |
97f98500 | 12 | /**\r |
a8c44645 | 13 | Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.\r |
97f98500 | 14 | \r |
a8c44645 | 15 | @return The size, in bytes, of the context buffer required for SHA-256 hash operations.\r |
97f98500 HT |
16 | \r |
17 | **/\r | |
18 | UINTN\r | |
19 | EFIAPI\r | |
20 | Sha256GetContextSize (\r | |
21 | VOID\r | |
22 | )\r | |
23 | {\r | |
24 | //\r | |
25 | // Retrieves OpenSSL SHA-256 Context Size\r | |
26 | //\r | |
7c342378 | 27 | return (UINTN)(sizeof (SHA256_CTX));\r |
97f98500 HT |
28 | }\r |
29 | \r | |
97f98500 HT |
30 | /**\r |
31 | Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for\r | |
32 | subsequent use.\r | |
33 | \r | |
16d2c32c | 34 | If Sha256Context is NULL, then return FALSE.\r |
97f98500 | 35 | \r |
a8c44645 | 36 | @param[out] Sha256Context Pointer to SHA-256 context being initialized.\r |
97f98500 HT |
37 | \r |
38 | @retval TRUE SHA-256 context initialization succeeded.\r | |
39 | @retval FALSE SHA-256 context initialization failed.\r | |
40 | \r | |
41 | **/\r | |
42 | BOOLEAN\r | |
43 | EFIAPI\r | |
44 | Sha256Init (\r | |
a8c44645 | 45 | OUT VOID *Sha256Context\r |
97f98500 HT |
46 | )\r |
47 | {\r | |
48 | //\r | |
16d2c32c | 49 | // Check input parameters.\r |
97f98500 | 50 | //\r |
16d2c32c | 51 | if (Sha256Context == NULL) {\r |
52 | return FALSE;\r | |
53 | }\r | |
97f98500 HT |
54 | \r |
55 | //\r | |
56 | // OpenSSL SHA-256 Context Initialization\r | |
57 | //\r | |
7c342378 | 58 | return (BOOLEAN)(SHA256_Init ((SHA256_CTX *)Sha256Context));\r |
97f98500 HT |
59 | }\r |
60 | \r | |
a8c44645 | 61 | /**\r |
62 | Makes a copy of an existing SHA-256 context.\r | |
63 | \r | |
16d2c32c | 64 | If Sha256Context is NULL, then return FALSE.\r |
65 | If NewSha256Context is NULL, then return FALSE.\r | |
a8c44645 | 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 | |
74 | BOOLEAN\r | |
75 | EFIAPI\r | |
76 | Sha256Duplicate (\r | |
77 | IN CONST VOID *Sha256Context,\r | |
78 | OUT VOID *NewSha256Context\r | |
79 | )\r | |
80 | {\r | |
4a567c96 | 81 | //\r |
16d2c32c | 82 | // Check input parameters.\r |
4a567c96 | 83 | //\r |
7c342378 | 84 | if ((Sha256Context == NULL) || (NewSha256Context == NULL)) {\r |
16d2c32c | 85 | return FALSE;\r |
86 | }\r | |
4a567c96 | 87 | \r |
a8c44645 | 88 | CopyMem (NewSha256Context, Sha256Context, sizeof (SHA256_CTX));\r |
89 | \r | |
90 | return TRUE;\r | |
91 | }\r | |
97f98500 HT |
92 | \r |
93 | /**\r | |
a8c44645 | 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 | |
2998af86 | 98 | SHA-256 context should be already correctly initialized by Sha256Init(), and should not be finalized\r |
a8c44645 | 99 | by Sha256Final(). Behavior with invalid context is undefined.\r |
97f98500 | 100 | \r |
16d2c32c | 101 | If Sha256Context is NULL, then return FALSE.\r |
97f98500 HT |
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 | |
a8c44645 | 105 | @param[in] DataSize Size of Data buffer in bytes.\r |
97f98500 HT |
106 | \r |
107 | @retval TRUE SHA-256 data digest succeeded.\r | |
a8c44645 | 108 | @retval FALSE SHA-256 data digest failed.\r |
97f98500 HT |
109 | \r |
110 | **/\r | |
111 | BOOLEAN\r | |
112 | EFIAPI\r | |
113 | Sha256Update (\r | |
114 | IN OUT VOID *Sha256Context,\r | |
115 | IN CONST VOID *Data,\r | |
a8c44645 | 116 | IN UINTN DataSize\r |
97f98500 HT |
117 | )\r |
118 | {\r | |
119 | //\r | |
16d2c32c | 120 | // Check input parameters.\r |
97f98500 | 121 | //\r |
16d2c32c | 122 | if (Sha256Context == NULL) {\r |
123 | return FALSE;\r | |
124 | }\r | |
97f98500 HT |
125 | \r |
126 | //\r | |
16d2c32c | 127 | // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r |
97f98500 | 128 | //\r |
7c342378 | 129 | if ((Data == NULL) && (DataSize != 0)) {\r |
16d2c32c | 130 | return FALSE;\r |
97f98500 HT |
131 | }\r |
132 | \r | |
133 | //\r | |
134 | // OpenSSL SHA-256 Hash Update\r | |
135 | //\r | |
7c342378 | 136 | return (BOOLEAN)(SHA256_Update ((SHA256_CTX *)Sha256Context, Data, DataSize));\r |
97f98500 HT |
137 | }\r |
138 | \r | |
97f98500 | 139 | /**\r |
a8c44645 | 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 | |
2998af86 | 145 | SHA-256 context should be already correctly initialized by Sha256Init(), and should not be\r |
a8c44645 | 146 | finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.\r |
97f98500 | 147 | \r |
16d2c32c | 148 | If Sha256Context is NULL, then return FALSE.\r |
149 | If HashValue is NULL, then return FALSE.\r | |
97f98500 | 150 | \r |
a8c44645 | 151 | @param[in, out] Sha256Context Pointer to the SHA-256 context.\r |
97f98500 HT |
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 | |
159 | BOOLEAN\r | |
160 | EFIAPI\r | |
161 | Sha256Final (\r | |
162 | IN OUT VOID *Sha256Context,\r | |
163 | OUT UINT8 *HashValue\r | |
164 | )\r | |
165 | {\r | |
166 | //\r | |
16d2c32c | 167 | // Check input parameters.\r |
97f98500 | 168 | //\r |
7c342378 | 169 | if ((Sha256Context == NULL) || (HashValue == NULL)) {\r |
16d2c32c | 170 | return FALSE;\r |
171 | }\r | |
97f98500 HT |
172 | \r |
173 | //\r | |
174 | // OpenSSL SHA-256 Hash Finalization\r | |
175 | //\r | |
7c342378 | 176 | return (BOOLEAN)(SHA256_Final (HashValue, (SHA256_CTX *)Sha256Context));\r |
97f98500 | 177 | }\r |
b7d1ba0a QL |
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 | |
197 | BOOLEAN\r | |
198 | EFIAPI\r | |
199 | Sha256HashAll (\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 | |
7c342378 MK |
211 | \r |
212 | if ((Data == NULL) && (DataSize != 0)) {\r | |
b7d1ba0a QL |
213 | return FALSE;\r |
214 | }\r | |
215 | \r | |
216 | //\r | |
217 | // OpenSSL SHA-256 Hash Computation.\r | |
218 | //\r | |
219 | if (SHA256 (Data, DataSize, HashValue) == NULL) {\r | |
220 | return FALSE;\r | |
221 | } else {\r | |
222 | return TRUE;\r | |
223 | }\r | |
224 | }\r |