]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - CryptoPkg/Library/BaseCryptLib/Hash/CryptSha256.c
Add new interfaces to support PKCS7#7 signed data and authenticode signature. Update...
[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 - 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 "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 ASSERT().\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 // 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
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 //\r
86 // ASSERT if Sha256Context or NewSha256Context is NULL.\r
87 //\r
88 ASSERT (Sha256Context != NULL);\r
89 ASSERT (NewSha256Context != NULL);\r
90\r
91 CopyMem (NewSha256Context, Sha256Context, sizeof (SHA256_CTX));\r
92\r
93 return TRUE;\r
94}\r
95\r
96/**\r
97 Digests the input data and updates SHA-256 context.\r
98\r
99 This function performs SHA-256 digest on a data buffer of the specified size.\r
100 It can be called multiple times to compute the digest of long or discontinuous data streams.\r
101 SHA-256 context should be already correctly intialized by Sha256Init(), and should not be finalized\r
102 by Sha256Final(). Behavior with invalid context is undefined.\r
103\r
104 If Sha256Context is NULL, then ASSERT().\r
105\r
106 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
107 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
108 @param[in] DataSize Size of Data buffer in bytes.\r
109\r
110 @retval TRUE SHA-256 data digest succeeded.\r
111 @retval FALSE SHA-256 data digest failed.\r
112\r
113**/\r
114BOOLEAN\r
115EFIAPI\r
116Sha256Update (\r
117 IN OUT VOID *Sha256Context,\r
118 IN CONST VOID *Data,\r
119 IN UINTN DataSize\r
120 )\r
121{\r
122 //\r
123 // ASSERT if Sha256Context is NULL\r
124 //\r
125 ASSERT (Sha256Context != NULL);\r
126\r
127 //\r
128 // ASSERT if invalid parameters, in case that only DataLength was checked in OpenSSL\r
129 //\r
130 if (Data == NULL) {\r
131 ASSERT (DataSize == 0);\r
132 }\r
133\r
134 //\r
135 // OpenSSL SHA-256 Hash Update\r
136 //\r
137 return (BOOLEAN) (SHA256_Update ((SHA256_CTX *)Sha256Context, Data, DataSize));\r
138}\r
139\r
140/**\r
141 Completes computation of the SHA-256 digest value.\r
142\r
143 This function completes SHA-256 hash computation and retrieves the digest value into\r
144 the specified memory. After this function has been called, the SHA-256 context cannot\r
145 be used again.\r
146 SHA-256 context should be already correctly intialized by Sha256Init(), and should not be\r
147 finalized by Sha256Final(). Behavior with invalid SHA-256 context is undefined.\r
148\r
149 If Sha256Context is NULL, then ASSERT().\r
150 If HashValue is NULL, then ASSERT().\r
151\r
152 @param[in, out] Sha256Context Pointer to the SHA-256 context.\r
153 @param[out] HashValue Pointer to a buffer that receives the SHA-256 digest\r
154 value (32 bytes).\r
155\r
156 @retval TRUE SHA-256 digest computation succeeded.\r
157 @retval FALSE SHA-256 digest computation failed.\r
158\r
159**/\r
160BOOLEAN\r
161EFIAPI\r
162Sha256Final (\r
163 IN OUT VOID *Sha256Context,\r
164 OUT UINT8 *HashValue\r
165 )\r
166{\r
167 //\r
168 // ASSERT if Sha256Context is NULL or HashValue is NULL\r
169 //\r
170 ASSERT (Sha256Context != NULL);\r
171 ASSERT (HashValue != NULL);\r
172\r
173 //\r
174 // OpenSSL SHA-256 Hash Finalization\r
175 //\r
176 return (BOOLEAN) (SHA256_Final (HashValue, (SHA256_CTX *)Sha256Context));\r
177}\r