]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha1.c
CryptoPkg: Apply uncrustify changes
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptSha1.c
1 /** @file
2 SHA-1 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 #ifndef DISABLE_SHA1_DEPRECATED_INTERFACES
13
14 /**
15 Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.
16
17 @return The size, in bytes, of the context buffer required for SHA-1 hash operations.
18
19 **/
20 UINTN
21 EFIAPI
22 Sha1GetContextSize (
23 VOID
24 )
25 {
26 //
27 // Retrieves OpenSSL SHA Context Size
28 //
29 return (UINTN)(sizeof (SHA_CTX));
30 }
31
32 /**
33 Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for
34 subsequent use.
35
36 If Sha1Context is NULL, then return FALSE.
37
38 @param[out] Sha1Context Pointer to SHA-1 context being initialized.
39
40 @retval TRUE SHA-1 context initialization succeeded.
41 @retval FALSE SHA-1 context initialization failed.
42
43 **/
44 BOOLEAN
45 EFIAPI
46 Sha1Init (
47 OUT VOID *Sha1Context
48 )
49 {
50 //
51 // Check input parameters.
52 //
53 if (Sha1Context == NULL) {
54 return FALSE;
55 }
56
57 //
58 // OpenSSL SHA-1 Context Initialization
59 //
60 return (BOOLEAN)(SHA1_Init ((SHA_CTX *)Sha1Context));
61 }
62
63 /**
64 Makes a copy of an existing SHA-1 context.
65
66 If Sha1Context is NULL, then return FALSE.
67 If NewSha1Context is NULL, then return FALSE.
68
69 @param[in] Sha1Context Pointer to SHA-1 context being copied.
70 @param[out] NewSha1Context Pointer to new SHA-1 context.
71
72 @retval TRUE SHA-1 context copy succeeded.
73 @retval FALSE SHA-1 context copy failed.
74
75 **/
76 BOOLEAN
77 EFIAPI
78 Sha1Duplicate (
79 IN CONST VOID *Sha1Context,
80 OUT VOID *NewSha1Context
81 )
82 {
83 //
84 // Check input parameters.
85 //
86 if ((Sha1Context == NULL) || (NewSha1Context == NULL)) {
87 return FALSE;
88 }
89
90 CopyMem (NewSha1Context, Sha1Context, sizeof (SHA_CTX));
91
92 return TRUE;
93 }
94
95 /**
96 Digests the input data and updates SHA-1 context.
97
98 This function performs SHA-1 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-1 context should be already correctly initialized by Sha1Init(), and should not be finalized
101 by Sha1Final(). Behavior with invalid context is undefined.
102
103 If Sha1Context is NULL, then return FALSE.
104
105 @param[in, out] Sha1Context Pointer to the SHA-1 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-1 data digest succeeded.
110 @retval FALSE SHA-1 data digest failed.
111
112 **/
113 BOOLEAN
114 EFIAPI
115 Sha1Update (
116 IN OUT VOID *Sha1Context,
117 IN CONST VOID *Data,
118 IN UINTN DataSize
119 )
120 {
121 //
122 // Check input parameters.
123 //
124 if (Sha1Context == 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-1 Hash Update
137 //
138 return (BOOLEAN)(SHA1_Update ((SHA_CTX *)Sha1Context, Data, DataSize));
139 }
140
141 /**
142 Completes computation of the SHA-1 digest value.
143
144 This function completes SHA-1 hash computation and retrieves the digest value into
145 the specified memory. After this function has been called, the SHA-1 context cannot
146 be used again.
147 SHA-1 context should be already correctly initialized by Sha1Init(), and should not be
148 finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.
149
150 If Sha1Context is NULL, then return FALSE.
151 If HashValue is NULL, then return FALSE.
152
153 @param[in, out] Sha1Context Pointer to the SHA-1 context.
154 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest
155 value (20 bytes).
156
157 @retval TRUE SHA-1 digest computation succeeded.
158 @retval FALSE SHA-1 digest computation failed.
159
160 **/
161 BOOLEAN
162 EFIAPI
163 Sha1Final (
164 IN OUT VOID *Sha1Context,
165 OUT UINT8 *HashValue
166 )
167 {
168 //
169 // Check input parameters.
170 //
171 if ((Sha1Context == NULL) || (HashValue == NULL)) {
172 return FALSE;
173 }
174
175 //
176 // OpenSSL SHA-1 Hash Finalization
177 //
178 return (BOOLEAN)(SHA1_Final (HashValue, (SHA_CTX *)Sha1Context));
179 }
180
181 /**
182 Computes the SHA-1 message digest of a input data buffer.
183
184 This function performs the SHA-1 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-1 digest
192 value (20 bytes).
193
194 @retval TRUE SHA-1 digest computation succeeded.
195 @retval FALSE SHA-1 digest computation failed.
196 @retval FALSE This interface is not supported.
197
198 **/
199 BOOLEAN
200 EFIAPI
201 Sha1HashAll (
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
214 if ((Data == NULL) && (DataSize != 0)) {
215 return FALSE;
216 }
217
218 //
219 // OpenSSL SHA-1 Hash Computation.
220 //
221 if (SHA1 (Data, DataSize, HashValue) == NULL) {
222 return FALSE;
223 } else {
224 return TRUE;
225 }
226 }
227
228 #endif