]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha256.c
Update CryptoPkg for new ciphers (HMAC, Block Cipher, etc) supports.
[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
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
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
40 If Sha256Context is NULL, then ASSERT().\r
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
55 // ASSERT if Sha256Context is NULL\r
56 //\r
57 ASSERT (Sha256Context != NULL);\r
58\r
59 //\r
60 // OpenSSL SHA-256 Context Initialization\r
61 //\r
62 return (BOOLEAN) (SHA256_Init ((SHA256_CTX *)Sha256Context));\r
63}\r
64\r
a8c44645 65/**\r
66 Makes a copy of an existing SHA-256 context.\r
67\r
68 If Sha256Context is NULL, then ASSERT().\r
69 If NewSha256Context is NULL, then ASSERT().\r
70\r
71 @param[in] Sha256Context Pointer to SHA-256 context being copied.\r
72 @param[out] NewSha256Context Pointer to new SHA-256 context.\r
73\r
74 @retval TRUE SHA-256 context copy succeeded.\r
75 @retval FALSE SHA-256 context copy failed.\r
76\r
77**/\r
78BOOLEAN\r
79EFIAPI\r
80Sha256Duplicate (\r
81 IN CONST VOID *Sha256Context,\r
82 OUT VOID *NewSha256Context\r
83 )\r
84{\r
85 CopyMem (NewSha256Context, Sha256Context, sizeof (SHA256_CTX));\r
86\r
87 return TRUE;\r
88}\r
97f98500
HT
89\r
90/**\r
a8c44645 91 Digests the input data and updates SHA-256 context.\r
92\r
93 This function performs SHA-256 digest on a data buffer of the specified size.\r
94 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
95 SHA-256 context should be already correctly intialized by Sha256Init(), and should not be finalized\r
96 by Sha256Final(). Behavior with invalid context is undefined.\r
97f98500
HT
97\r
98 If Sha256Context is NULL, then ASSERT().\r
99\r
100 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
101 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
a8c44645 102 @param[in] DataSize Size of Data buffer in bytes.\r
97f98500
HT
103\r
104 @retval TRUE SHA-256 data digest succeeded.\r
a8c44645 105 @retval FALSE SHA-256 data digest failed.\r
97f98500
HT
106\r
107**/\r
108BOOLEAN\r
109EFIAPI\r
110Sha256Update (\r
111 IN OUT VOID *Sha256Context,\r
112 IN CONST VOID *Data,\r
a8c44645 113 IN UINTN DataSize\r
97f98500
HT
114 )\r
115{\r
116 //\r
117 // ASSERT if Sha256Context is NULL\r
118 //\r
119 ASSERT (Sha256Context != NULL);\r
120\r
121 //\r
122 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
123 //\r
124 if (Data == NULL) {\r
a8c44645 125 ASSERT (DataSize == 0);\r
97f98500
HT
126 }\r
127\r
128 //\r
129 // OpenSSL SHA-256 Hash Update\r
130 //\r
a8c44645 131 return (BOOLEAN) (SHA256_Update ((SHA256_CTX *)Sha256Context, Data, DataSize));\r
97f98500
HT
132}\r
133\r
97f98500 134/**\r
a8c44645 135 Completes computation of the SHA-256 digest value.\r
136\r
137 This function completes SHA-256 hash computation and retrieves the digest value into\r
138 the specified memory. After this function has been called, the SHA-256 context cannot\r
139 be used again.\r
140 SHA-256 context should be already correctly intialized by Sha256Init(), and should not be\r
141 finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.\r
97f98500
HT
142\r
143 If Sha256Context is NULL, then ASSERT().\r
144 If HashValue is NULL, then ASSERT().\r
145\r
a8c44645 146 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
97f98500
HT
147 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest\r
148 value (32 bytes).\r
149\r
150 @retval TRUE SHA-256 digest computation succeeded.\r
151 @retval FALSE SHA-256 digest computation failed.\r
152\r
153**/\r
154BOOLEAN\r
155EFIAPI\r
156Sha256Final (\r
157 IN OUT VOID *Sha256Context,\r
158 OUT UINT8 *HashValue\r
159 )\r
160{\r
161 //\r
162 // ASSERT if Sha256Context is NULL or HashValue is NULL\r
163 //\r
164 ASSERT (Sha256Context != NULL);\r
165 ASSERT (HashValue != NULL);\r
166\r
167 //\r
168 // OpenSSL SHA-256 Hash Finalization\r
169 //\r
170 return (BOOLEAN) (SHA256_Final (HashValue, (SHA256_CTX *)Sha256Context));\r
171}\r