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