]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Hash/CryptSm3.c
CryptoPkg: Apply uncrustify changes
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Hash / CryptSm3.c
CommitLineData
f0718d1d
LX
1/** @file\r
2 SM3 Digest Wrapper Implementations over openssl.\r
3\r
4Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include "InternalCryptLib.h"\r
8c30327d 10#include "crypto/sm3.h"\r
f0718d1d
LX
11\r
12/**\r
13 Retrieves the size, in bytes, of the context buffer required for SM3 hash operations.\r
14\r
15 @return The size, in bytes, of the context buffer required for SM3 hash operations.\r
16\r
17**/\r
18UINTN\r
19EFIAPI\r
20Sm3GetContextSize (\r
21 VOID\r
22 )\r
23{\r
24 //\r
25 // Retrieves Openssl SM3 Context Size\r
26 //\r
7c342378 27 return (UINTN)(sizeof (SM3_CTX));\r
f0718d1d
LX
28}\r
29\r
30/**\r
31 Initializes user-supplied memory pointed by Sm3Context as SM3 hash context for\r
32 subsequent use.\r
33\r
34 If Sm3Context is NULL, then return FALSE.\r
35\r
36 @param[out] Sm3Context Pointer to SM3 context being initialized.\r
37\r
38 @retval TRUE SM3 context initialization succeeded.\r
39 @retval FALSE SM3 context initialization failed.\r
40\r
41**/\r
42BOOLEAN\r
43EFIAPI\r
44Sm3Init (\r
45 OUT VOID *Sm3Context\r
46 )\r
47{\r
48 //\r
49 // Check input parameters.\r
50 //\r
51 if (Sm3Context == NULL) {\r
52 return FALSE;\r
53 }\r
54\r
55 //\r
56 // Openssl SM3 Context Initialization\r
57 //\r
7c342378 58 sm3_init ((SM3_CTX *)Sm3Context);\r
f0718d1d
LX
59 return TRUE;\r
60}\r
61\r
62/**\r
63 Makes a copy of an existing SM3 context.\r
64\r
65 If Sm3Context is NULL, then return FALSE.\r
66 If NewSm3Context is NULL, then return FALSE.\r
67 If this interface is not supported, then return FALSE.\r
68\r
69 @param[in] Sm3Context Pointer to SM3 context being copied.\r
70 @param[out] NewSm3Context Pointer to new SM3 context.\r
71\r
72 @retval TRUE SM3 context copy succeeded.\r
73 @retval FALSE SM3 context copy failed.\r
74 @retval FALSE This interface is not supported.\r
75\r
76**/\r
77BOOLEAN\r
78EFIAPI\r
79Sm3Duplicate (\r
80 IN CONST VOID *Sm3Context,\r
81 OUT VOID *NewSm3Context\r
82 )\r
83{\r
84 //\r
85 // Check input parameters.\r
86 //\r
7c342378 87 if ((Sm3Context == NULL) || (NewSm3Context == NULL)) {\r
f0718d1d
LX
88 return FALSE;\r
89 }\r
90\r
91 CopyMem (NewSm3Context, Sm3Context, sizeof (SM3_CTX));\r
92\r
93 return TRUE;\r
94}\r
95\r
96/**\r
97 Digests the input data and updates SM3 context.\r
98\r
99 This function performs SM3 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 SM3 context should be already correctly initialized by Sm3Init(), and should not be finalized\r
102 by Sm3Final(). Behavior with invalid context is undefined.\r
103\r
104 If Sm3Context is NULL, then return FALSE.\r
105\r
106 @param[in, out] Sm3Context Pointer to the SM3 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 SM3 data digest succeeded.\r
111 @retval FALSE SM3 data digest failed.\r
112\r
113**/\r
114BOOLEAN\r
115EFIAPI\r
116Sm3Update (\r
117 IN OUT VOID *Sm3Context,\r
118 IN CONST VOID *Data,\r
119 IN UINTN DataSize\r
120 )\r
121{\r
122 //\r
123 // Check input parameters.\r
124 //\r
125 if (Sm3Context == NULL) {\r
126 return FALSE;\r
127 }\r
128\r
129 //\r
130 // Check invalid parameters, in case that only DataLength was checked in Openssl\r
131 //\r
7c342378 132 if ((Data == NULL) && (DataSize != 0)) {\r
f0718d1d
LX
133 return FALSE;\r
134 }\r
135\r
136 //\r
137 // Openssl SM3 Hash Update\r
138 //\r
7c342378 139 sm3_update ((SM3_CTX *)Sm3Context, Data, DataSize);\r
f0718d1d
LX
140\r
141 return TRUE;\r
142}\r
143\r
144/**\r
145 Completes computation of the SM3 digest value.\r
146\r
147 This function completes SM3 hash computation and retrieves the digest value into\r
148 the specified memory. After this function has been called, the SM3 context cannot\r
149 be used again.\r
150 SM3 context should be already correctly initialized by Sm3Init(), and should not be\r
151 finalized by Sm3Final(). Behavior with invalid SM3 context is undefined.\r
152\r
153 If Sm3Context is NULL, then return FALSE.\r
154 If HashValue is NULL, then return FALSE.\r
155\r
156 @param[in, out] Sm3Context Pointer to the SM3 context.\r
157 @param[out] HashValue Pointer to a buffer that receives the SM3 digest\r
158 value (32 bytes).\r
159\r
160 @retval TRUE SM3 digest computation succeeded.\r
161 @retval FALSE SM3 digest computation failed.\r
162\r
163**/\r
164BOOLEAN\r
165EFIAPI\r
166Sm3Final (\r
167 IN OUT VOID *Sm3Context,\r
168 OUT UINT8 *HashValue\r
169 )\r
170{\r
171 //\r
172 // Check input parameters.\r
173 //\r
7c342378 174 if ((Sm3Context == NULL) || (HashValue == NULL)) {\r
f0718d1d
LX
175 return FALSE;\r
176 }\r
177\r
178 //\r
179 // Openssl SM3 Hash Finalization\r
180 //\r
7c342378 181 sm3_final (HashValue, (SM3_CTX *)Sm3Context);\r
f0718d1d
LX
182\r
183 return TRUE;\r
184}\r
185\r
186/**\r
187 Computes the SM3 message digest of a input data buffer.\r
188\r
189 This function performs the SM3 message digest of a given data buffer, and places\r
190 the digest value into the specified memory.\r
191\r
192 If this interface is not supported, then return FALSE.\r
193\r
194 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
195 @param[in] DataSize Size of Data buffer in bytes.\r
196 @param[out] HashValue Pointer to a buffer that receives the SM3 digest\r
197 value (32 bytes).\r
198\r
199 @retval TRUE SM3 digest computation succeeded.\r
200 @retval FALSE SM3 digest computation failed.\r
201 @retval FALSE This interface is not supported.\r
202\r
203**/\r
204BOOLEAN\r
205EFIAPI\r
206Sm3HashAll (\r
207 IN CONST VOID *Data,\r
208 IN UINTN DataSize,\r
209 OUT UINT8 *HashValue\r
210 )\r
211{\r
7c342378 212 SM3_CTX Ctx;\r
f0718d1d
LX
213\r
214 //\r
215 // Check input parameters.\r
216 //\r
217 if (HashValue == NULL) {\r
218 return FALSE;\r
219 }\r
7c342378
MK
220\r
221 if ((Data == NULL) && (DataSize != 0)) {\r
f0718d1d
LX
222 return FALSE;\r
223 }\r
224\r
225 //\r
226 // SM3 Hash Computation.\r
227 //\r
7c342378 228 sm3_init (&Ctx);\r
f0718d1d 229\r
7c342378 230 sm3_update (&Ctx, Data, DataSize);\r
f0718d1d 231\r
7c342378 232 sm3_final (HashValue, &Ctx);\r
f0718d1d
LX
233\r
234 return TRUE;\r
235}\r