]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha256.c
1. Remove conducting ASSERT in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptSha256.c
... / ...
CommitLineData
1/** @file\r
2 SHA-256 Digest Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2009 - 2012, 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 "InternalCryptLib.h"\r
16#include <openssl/sha.h>\r
17\r
18/**\r
19 Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.\r
20\r
21 @return The size, in bytes, of the context buffer required for SHA-256 hash operations.\r
22\r
23**/\r
24UINTN\r
25EFIAPI\r
26Sha256GetContextSize (\r
27 VOID\r
28 )\r
29{\r
30 //\r
31 // Retrieves OpenSSL SHA-256 Context Size\r
32 //\r
33 return (UINTN)(sizeof (SHA256_CTX));\r
34}\r
35\r
36/**\r
37 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for\r
38 subsequent use.\r
39\r
40 If Sha256Context is NULL, then return FALSE.\r
41\r
42 @param[out] Sha256Context Pointer to SHA-256 context being initialized.\r
43\r
44 @retval TRUE SHA-256 context initialization succeeded.\r
45 @retval FALSE SHA-256 context initialization failed.\r
46\r
47**/\r
48BOOLEAN\r
49EFIAPI\r
50Sha256Init (\r
51 OUT VOID *Sha256Context\r
52 )\r
53{\r
54 //\r
55 // Check input parameters.\r
56 //\r
57 if (Sha256Context == NULL) {\r
58 return FALSE;\r
59 }\r
60\r
61 //\r
62 // OpenSSL SHA-256 Context Initialization\r
63 //\r
64 return (BOOLEAN) (SHA256_Init ((SHA256_CTX *)Sha256Context));\r
65}\r
66\r
67/**\r
68 Makes a copy of an existing SHA-256 context.\r
69\r
70 If Sha256Context is NULL, then return FALSE.\r
71 If NewSha256Context is NULL, then return FALSE.\r
72\r
73 @param[in] Sha256Context Pointer to SHA-256 context being copied.\r
74 @param[out] NewSha256Context Pointer to new SHA-256 context.\r
75\r
76 @retval TRUE SHA-256 context copy succeeded.\r
77 @retval FALSE SHA-256 context copy failed.\r
78\r
79**/\r
80BOOLEAN\r
81EFIAPI\r
82Sha256Duplicate (\r
83 IN CONST VOID *Sha256Context,\r
84 OUT VOID *NewSha256Context\r
85 )\r
86{\r
87 //\r
88 // Check input parameters.\r
89 //\r
90 if (Sha256Context == NULL || NewSha256Context == NULL) {\r
91 return FALSE;\r
92 }\r
93\r
94 CopyMem (NewSha256Context, Sha256Context, sizeof (SHA256_CTX));\r
95\r
96 return TRUE;\r
97}\r
98\r
99/**\r
100 Digests the input data and updates SHA-256 context.\r
101\r
102 This function performs SHA-256 digest on a data buffer of the specified size.\r
103 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
104 SHA-256 context should be already correctly intialized by Sha256Init(), and should not be finalized\r
105 by Sha256Final(). Behavior with invalid context is undefined.\r
106\r
107 If Sha256Context is NULL, then return FALSE.\r
108\r
109 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
110 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
111 @param[in] DataSize Size of Data buffer in bytes.\r
112\r
113 @retval TRUE SHA-256 data digest succeeded.\r
114 @retval FALSE SHA-256 data digest failed.\r
115\r
116**/\r
117BOOLEAN\r
118EFIAPI\r
119Sha256Update (\r
120 IN OUT VOID *Sha256Context,\r
121 IN CONST VOID *Data,\r
122 IN UINTN DataSize\r
123 )\r
124{\r
125 //\r
126 // Check input parameters.\r
127 //\r
128 if (Sha256Context == NULL) {\r
129 return FALSE;\r
130 }\r
131\r
132 //\r
133 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
134 //\r
135 if (Data == NULL && DataSize != 0) {\r
136 return FALSE;\r
137 }\r
138\r
139 //\r
140 // OpenSSL SHA-256 Hash Update\r
141 //\r
142 return (BOOLEAN) (SHA256_Update ((SHA256_CTX *)Sha256Context, Data, DataSize));\r
143}\r
144\r
145/**\r
146 Completes computation of the SHA-256 digest value.\r
147\r
148 This function completes SHA-256 hash computation and retrieves the digest value into\r
149 the specified memory. After this function has been called, the SHA-256 context cannot\r
150 be used again.\r
151 SHA-256 context should be already correctly intialized by Sha256Init(), and should not be\r
152 finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.\r
153\r
154 If Sha256Context is NULL, then return FALSE.\r
155 If HashValue is NULL, then return FALSE.\r
156\r
157 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
158 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest\r
159 value (32 bytes).\r
160\r
161 @retval TRUE SHA-256 digest computation succeeded.\r
162 @retval FALSE SHA-256 digest computation failed.\r
163\r
164**/\r
165BOOLEAN\r
166EFIAPI\r
167Sha256Final (\r
168 IN OUT VOID *Sha256Context,\r
169 OUT UINT8 *HashValue\r
170 )\r
171{\r
172 //\r
173 // Check input parameters.\r
174 //\r
175 if (Sha256Context == NULL || HashValue == NULL) {\r
176 return FALSE;\r
177 }\r
178\r
179 //\r
180 // OpenSSL SHA-256 Hash Finalization\r
181 //\r
182 return (BOOLEAN) (SHA256_Final (HashValue, (SHA256_CTX *)Sha256Context));\r
183}\r