]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha256.c
1. Remove conducting ASSERT in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptSha256.c
CommitLineData
97f98500
HT
1/** @file\r
2 SHA-256 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
97f98500 18/**\r
a8c44645 19 Retrieves the size, in bytes, of the context buffer required for SHA-256 hash operations.\r
97f98500 20\r
a8c44645 21 @return The size, in bytes, of the context buffer required for SHA-256 hash operations.\r
97f98500
HT
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
97f98500
HT
36/**\r
37 Initializes user-supplied memory pointed by Sha256Context as SHA-256 hash context for\r
38 subsequent use.\r
39\r
16d2c32c 40 If Sha256Context is NULL, then return FALSE.\r
97f98500 41\r
a8c44645 42 @param[out] Sha256Context Pointer to SHA-256 context being initialized.\r
97f98500
HT
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
a8c44645 51 OUT VOID *Sha256Context\r
97f98500
HT
52 )\r
53{\r
54 //\r
16d2c32c 55 // Check input parameters.\r
97f98500 56 //\r
16d2c32c 57 if (Sha256Context == NULL) {\r
58 return FALSE;\r
59 }\r
97f98500
HT
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
a8c44645 67/**\r
68 Makes a copy of an existing SHA-256 context.\r
69\r
16d2c32c 70 If Sha256Context is NULL, then return FALSE.\r
71 If NewSha256Context is NULL, then return FALSE.\r
a8c44645 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
4a567c96 87 //\r
16d2c32c 88 // Check input parameters.\r
4a567c96 89 //\r
16d2c32c 90 if (Sha256Context == NULL || NewSha256Context == NULL) {\r
91 return FALSE;\r
92 }\r
4a567c96 93\r
a8c44645 94 CopyMem (NewSha256Context, Sha256Context, sizeof (SHA256_CTX));\r
95\r
96 return TRUE;\r
97}\r
97f98500
HT
98\r
99/**\r
a8c44645 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
97f98500 106\r
16d2c32c 107 If Sha256Context is NULL, then return FALSE.\r
97f98500
HT
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
a8c44645 111 @param[in] DataSize Size of Data buffer in bytes.\r
97f98500
HT
112\r
113 @retval TRUE SHA-256 data digest succeeded.\r
a8c44645 114 @retval FALSE SHA-256 data digest failed.\r
97f98500
HT
115\r
116**/\r
117BOOLEAN\r
118EFIAPI\r
119Sha256Update (\r
120 IN OUT VOID *Sha256Context,\r
121 IN CONST VOID *Data,\r
a8c44645 122 IN UINTN DataSize\r
97f98500
HT
123 )\r
124{\r
125 //\r
16d2c32c 126 // Check input parameters.\r
97f98500 127 //\r
16d2c32c 128 if (Sha256Context == NULL) {\r
129 return FALSE;\r
130 }\r
97f98500
HT
131\r
132 //\r
16d2c32c 133 // Check invalid parameters, in case that only DataLength was checked in OpenSSL\r
97f98500 134 //\r
16d2c32c 135 if (Data == NULL && DataSize != 0) {\r
136 return FALSE;\r
97f98500
HT
137 }\r
138\r
139 //\r
140 // OpenSSL SHA-256 Hash Update\r
141 //\r
a8c44645 142 return (BOOLEAN) (SHA256_Update ((SHA256_CTX *)Sha256Context, Data, DataSize));\r
97f98500
HT
143}\r
144\r
97f98500 145/**\r
a8c44645 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
97f98500 153\r
16d2c32c 154 If Sha256Context is NULL, then return FALSE.\r
155 If HashValue is NULL, then return FALSE.\r
97f98500 156\r
a8c44645 157 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
97f98500
HT
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
16d2c32c 173 // Check input parameters.\r
97f98500 174 //\r
16d2c32c 175 if (Sha256Context == NULL || HashValue == NULL) {\r
176 return FALSE;\r
177 }\r
97f98500
HT
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