]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha1.c
Fix PCD token value conflict issue.
[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
15#include <Library/BaseLib.h>\r
16#include <Library/DebugLib.h>\r
17\r
18#include <Library/BaseCryptLib.h>\r
19#include <openssl/sha.h>\r
20\r
21\r
22/**\r
23 Retrieves the size, in bytes, of the context buffer required for SHA-1 hash operations.\r
24\r
25 @return The size, in bytes, of the context buffer required for SHA-1 hash operations.\r
26\r
27**/\r
28UINTN\r
29EFIAPI\r
30Sha1GetContextSize (\r
31 VOID\r
32 )\r
33{\r
34 //\r
35 // Retrieves OpenSSL SHA Context Size\r
36 //\r
37 return (UINTN)(sizeof (SHA_CTX));\r
38}\r
39\r
40\r
41/**\r
42 Initializes user-supplied memory pointed by Sha1Context as the SHA-1 hash context for\r
43 subsequent use.\r
44\r
45 If Sha1Context is NULL, then ASSERT().\r
46\r
47 @param[in, out] Sha1Context Pointer to the SHA-1 Context being initialized.\r
48\r
49 @retval TRUE SHA-1 initialization succeeded.\r
50 @retval FALSE SHA-1 initialization failed.\r
51\r
52**/\r
53BOOLEAN\r
54EFIAPI\r
55Sha1Init (\r
56 IN OUT VOID *Sha1Context\r
57 )\r
58{\r
59 //\r
60 // ASSERT if Sha1Context is NULL\r
61 //\r
62 ASSERT (Sha1Context != NULL);\r
63\r
64 //\r
65 // OpenSSL SHA-1 Context Initialization\r
66 //\r
67 return (BOOLEAN) (SHA1_Init ((SHA_CTX *)Sha1Context));\r
68}\r
69\r
70\r
71/**\r
72 Performs SHA-1 digest on a data buffer of the specified length. This function can\r
73 be called multiple times to compute the digest of long or discontinuous data streams.\r
74\r
75 If Sha1Context is NULL, then ASSERT().\r
76\r
77 @param[in, out] Sha1Context Pointer to the SHA-1 context.\r
78 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
79 @param[in] DataLength Length of Data buffer in bytes.\r
80\r
81 @retval TRUE SHA-1 data digest succeeded.\r
82 @retval FALSE Invalid SHA-1 context. After Sha1Final function has been called, the\r
83 SHA-1 context cannot be reused.\r
84\r
85**/\r
86BOOLEAN\r
87EFIAPI\r
88Sha1Update (\r
89 IN OUT VOID *Sha1Context,\r
90 IN CONST VOID *Data,\r
91 IN UINTN DataLength\r
92 )\r
93{\r
94 //\r
95 // ASSERT if Sha1Context is NULL\r
96 //\r
97 ASSERT (Sha1Context != NULL);\r
98\r
99 //\r
100 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
101 //\r
102 if (Data == NULL) {\r
103 ASSERT (DataLength == 0);\r
104 }\r
105\r
106 //\r
107 // OpenSSL SHA-1 Hash Update\r
108 //\r
109 return (BOOLEAN) (SHA1_Update ((SHA_CTX *)Sha1Context, Data, DataLength));\r
110}\r
111\r
112\r
113/**\r
114 Completes SHA-1 hash computation and retrieves the digest value into the specified\r
115 memory. After this function has been called, the SHA-1 context cannot be used again.\r
116\r
117 If Sha1Context is NULL, then ASSERT().\r
118 If HashValue is NULL, then ASSERT().\r
119\r
120 @param[in, out] Sha1Context Pointer to the SHA-1 context\r
121 @param[out] HashValue Pointer to a buffer that receives the SHA-1 digest\r
122 value (20 bytes).\r
123\r
124 @retval TRUE SHA-1 digest computation succeeded.\r
125 @retval FALSE SHA-1 digest computation failed.\r
126\r
127**/\r
128BOOLEAN\r
129EFIAPI\r
130Sha1Final (\r
131 IN OUT VOID *Sha1Context,\r
132 OUT UINT8 *HashValue\r
133 )\r
134{\r
135 //\r
136 // ASSERT if Sha1Context is NULL or HashValue is NULL\r
137 //\r
138 ASSERT (Sha1Context != NULL);\r
139 ASSERT (HashValue != NULL);\r
140\r
141 //\r
142 // OpenSSL SHA-1 Hash Finalization\r
143 //\r
144 return (BOOLEAN) (SHA1_Final (HashValue, (SHA_CTX *)Sha1Context));\r
145}\r