]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha1.c
Add new interfaces to support PKCS7#7 signed data and authenticode signature. Update...
[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
4Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
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
41 If Sha1Context is NULL, then ASSERT().\r
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
56 // ASSERT if Sha1Context is NULL\r
57 //\r
58 ASSERT (Sha1Context != NULL);\r
59\r
60 //\r
61 // OpenSSL SHA-1 Context Initialization\r
62 //\r
63 return (BOOLEAN) (SHA1_Init ((SHA_CTX *)Sha1Context));\r
64}\r
65\r
a8c44645 66/**\r
67 Makes a copy of an existing SHA-1 context.\r
68\r
69 If Sha1Context is NULL, then ASSERT().\r
70 If NewSha1Context is NULL, then ASSERT().\r
71\r
72 @param[in] Sha1Context Pointer to SHA-1 context being copied.\r
73 @param[out] NewSha1Context Pointer to new SHA-1 context.\r
74\r
75 @retval TRUE SHA-1 context copy succeeded.\r
76 @retval FALSE SHA-1 context copy failed.\r
77\r
78**/\r
79BOOLEAN\r
80EFIAPI\r
81Sha1Duplicate (\r
82 IN CONST VOID *Sha1Context,\r
83 OUT VOID *NewSha1Context\r
84 )\r
85{\r
4a567c96 86 //\r
87 // ASSERT if Sha1Context or NewSha1Context is NULL.\r
88 //\r
89 ASSERT (Sha1Context != NULL);\r
90 ASSERT (NewSha1Context != NULL);\r
91\r
a8c44645 92 CopyMem (NewSha1Context, Sha1Context, sizeof (SHA_CTX));\r
93\r
94 return TRUE;\r
95}\r
97f98500
HT
96\r
97/**\r
a8c44645 98 Digests the input data and updates SHA-1 context.\r
99\r
100 This function performs SHA-1 digest on a data buffer of the specified size.\r
101 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
102 SHA-1 context should be already correctly intialized by Sha1Init(), and should not be finalized\r
103 by Sha1Final(). Behavior with invalid context is undefined.\r
97f98500
HT
104\r
105 If Sha1Context is NULL, then ASSERT().\r
106\r
107 @param[in, out] Sha1Context Pointer to the SHA-1 context.\r
108 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
a8c44645 109 @param[in] DataSize Size of Data buffer in bytes.\r
97f98500
HT
110\r
111 @retval TRUE SHA-1 data digest succeeded.\r
a8c44645 112 @retval FALSE SHA-1 data digest failed.\r
97f98500
HT
113\r
114**/\r
115BOOLEAN\r
116EFIAPI\r
117Sha1Update (\r
118 IN OUT VOID *Sha1Context,\r
119 IN CONST VOID *Data,\r
a8c44645 120 IN UINTN DataSize\r
97f98500
HT
121 )\r
122{\r
123 //\r
124 // ASSERT if Sha1Context is NULL\r
125 //\r
126 ASSERT (Sha1Context != NULL);\r
127\r
128 //\r
129 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
130 //\r
131 if (Data == NULL) {\r
a8c44645 132 ASSERT (DataSize == 0);\r
97f98500
HT
133 }\r
134\r
135 //\r
136 // OpenSSL SHA-1 Hash Update\r
137 //\r
a8c44645 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
147 SHA-1 context should be already correctly intialized by Sha1Init(), and should not be\r
148 finalized by Sha1Final(). Behavior with invalid SHA-1 context is undefined.\r
97f98500
HT
149\r
150 If Sha1Context is NULL, then ASSERT().\r
151 If HashValue is NULL, then ASSERT().\r
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
169 // ASSERT if Sha1Context is NULL or HashValue is NULL\r
170 //\r
171 ASSERT (Sha1Context != NULL);\r
172 ASSERT (HashValue != NULL);\r
173\r
174 //\r
175 // OpenSSL SHA-1 Hash Finalization\r
176 //\r
177 return (BOOLEAN) (SHA1_Final (HashValue, (SHA_CTX *)Sha1Context));\r
178}\r