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