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