]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha1.c
1. Remove conducting ASSERT in BaseCryptLib.
[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
16d2c32c 4Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
97f98500
HT
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
a8c44645 15#include "InternalCryptLib.h"\r
97f98500
HT
16#include <openssl/sha.h>\r
17\r
18\r
19/**\r
20 Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.\r
21\r
22 @return The size, in bytes, of the context buffer required for SHA-1 hash operations.\r
23\r
24**/\r
25UINTN\r
26EFIAPI\r
27Sha1GetContextSize (\r
28 VOID\r
29 )\r
30{\r
31 //\r
32 // Retrieves OpenSSL SHA Context Size\r
33 //\r
34 return (UINTN)(sizeof (SHA_CTX));\r
35}\r
36\r
97f98500 37/**\r
a8c44645 38 Initializes user-supplied memory pointed by Sha1Context as SHA-1 hash context for\r
97f98500
HT
39 subsequent use.\r
40\r
16d2c32c 41 If Sha1Context is NULL, then return FALSE.\r
97f98500 42\r
a8c44645 43 @param[out] Sha1Context Pointer to SHA-1 context being initialized.\r
97f98500 44\r
a8c44645 45 @retval TRUE SHA-1 context initialization succeeded.\r
46 @retval FALSE SHA-1 context initialization failed.\r
97f98500
HT
47\r
48**/\r
49BOOLEAN\r
50EFIAPI\r
51Sha1Init (\r
a8c44645 52 OUT VOID *Sha1Context\r
97f98500
HT
53 )\r
54{\r
55 //\r
16d2c32c 56 // Check input parameters.\r
97f98500 57 //\r
16d2c32c 58 if (Sha1Context == NULL) {\r
59 return FALSE;\r
60 }\r
97f98500
HT
61\r
62 //\r
63 // OpenSSL SHA-1 Context Initialization\r
64 //\r
65 return (BOOLEAN) (SHA1_Init ((SHA_CTX *)Sha1Context));\r
66}\r
67\r
a8c44645 68/**\r
69 Makes a copy of an existing SHA-1 context.\r
70\r
16d2c32c 71 If Sha1Context is NULL, then return FALSE.\r
72 If NewSha1Context is NULL, then return FALSE.\r
a8c44645 73\r
74 @param[in] Sha1Context Pointer to SHA-1 context being copied.\r
75 @param[out] NewSha1Context Pointer to new SHA-1 context.\r
76\r
77 @retval TRUE SHA-1 context copy succeeded.\r
78 @retval FALSE SHA-1 context copy failed.\r
79\r
80**/\r
81BOOLEAN\r
82EFIAPI\r
83Sha1Duplicate (\r
84 IN CONST VOID *Sha1Context,\r
85 OUT VOID *NewSha1Context\r
86 )\r
87{\r
4a567c96 88 //\r
16d2c32c 89 // Check input parameters.\r
4a567c96 90 //\r
16d2c32c 91 if (Sha1Context == NULL || NewSha1Context == NULL) {\r
92 return FALSE;\r
93 }\r
4a567c96 94\r
a8c44645 95 CopyMem (NewSha1Context, Sha1Context, sizeof (SHA_CTX));\r
96\r
97 return TRUE;\r
98}\r
97f98500
HT
99\r
100/**\r
a8c44645 101 Digests the input data and updates SHA-1 context.\r
102\r
103 This function performs SHA-1 digest on a data buffer of the specified size.\r
104 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
105 SHA-1 context should be already correctly intialized by Sha1Init(), and should not be finalized\r
106 by Sha1Final(). Behavior with invalid context is undefined.\r
97f98500 107\r
16d2c32c 108 If Sha1Context is NULL, then return FALSE.\r
97f98500
HT
109\r
110 @param[in, out] Sha1Context Pointer to the SHA-1 context.\r
111 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
a8c44645 112 @param[in] DataSize Size of Data buffer in bytes.\r
97f98500
HT
113\r
114 @retval TRUE SHA-1 data digest succeeded.\r
a8c44645 115 @retval FALSE SHA-1 data digest failed.\r
97f98500
HT
116\r
117**/\r
118BOOLEAN\r
119EFIAPI\r
120Sha1Update (\r
121 IN OUT VOID *Sha1Context,\r
122 IN CONST VOID *Data,\r
a8c44645 123 IN UINTN DataSize\r
97f98500
HT
124 )\r
125{\r
126 //\r
16d2c32c 127 // Check input parameters.\r
97f98500 128 //\r
16d2c32c 129 if (Sha1Context == NULL) {\r
130 return FALSE;\r
131 }\r
97f98500
HT
132\r
133 //\r
16d2c32c 134 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
97f98500 135 //\r
16d2c32c 136 if (Data == NULL && DataSize != 0) {\r
137 return FALSE;\r
97f98500
HT
138 }\r
139\r
140 //\r
141 // OpenSSL SHA-1 Hash Update\r
142 //\r
a8c44645 143 return (BOOLEAN) (SHA1_Update ((SHA_CTX *)Sha1Context, Data, DataSize));\r
97f98500
HT
144}\r
145\r
97f98500 146/**\r
a8c44645 147 Completes computation of the SHA-1 digest value.\r
148\r
149 This function completes SHA-1 hash computation and retrieves the digest value into\r
150 the specified memory. After this function has been called, the SHA-1 context cannot\r
151 be used again.\r
152 SHA-1 context should be already correctly intialized by Sha1Init(), and should not be\r
153 finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.\r
97f98500 154\r
16d2c32c 155 If Sha1Context is NULL, then return FALSE.\r
156 If HashValue is NULL, then return FALSE.\r
97f98500 157\r
a8c44645 158 @param[in, out] Sha1Context Pointer to the SHA-1 context.\r
97f98500
HT
159 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest\r
160 value (20 bytes).\r
161\r
162 @retval TRUE SHA-1 digest computation succeeded.\r
163 @retval FALSE SHA-1 digest computation failed.\r
164\r
165**/\r
166BOOLEAN\r
167EFIAPI\r
168Sha1Final (\r
169 IN OUT VOID *Sha1Context,\r
170 OUT UINT8 *HashValue\r
171 )\r
172{\r
173 //\r
16d2c32c 174 // Check input parameters.\r
97f98500 175 //\r
16d2c32c 176 if (Sha1Context == NULL || HashValue == NULL) {\r
177 return FALSE;\r
178 }\r
97f98500
HT
179\r
180 //\r
181 // OpenSSL SHA-1 Hash Finalization\r
182 //\r
183 return (BOOLEAN) (SHA1_Final (HashValue, (SHA_CTX *)Sha1Context));\r
184}\r