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