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