]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | SM3 Digest Wrapper Implementations over openssl.\r | |
3 | \r | |
4 | Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r | |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | \r | |
7 | **/\r | |
8 | \r | |
9 | #include "InternalCryptLib.h"\r | |
10 | #include "crypto/sm3.h"\r | |
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 | |
18 | UINTN\r | |
19 | EFIAPI\r | |
20 | Sm3GetContextSize (\r | |
21 | VOID\r | |
22 | )\r | |
23 | {\r | |
24 | //\r | |
25 | // Retrieves Openssl SM3 Context Size\r | |
26 | //\r | |
27 | return (UINTN) (sizeof (SM3_CTX));\r | |
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 | |
42 | BOOLEAN\r | |
43 | EFIAPI\r | |
44 | Sm3Init (\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 | |
58 | sm3_init ((SM3_CTX *) Sm3Context);\r | |
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 | |
77 | BOOLEAN\r | |
78 | EFIAPI\r | |
79 | Sm3Duplicate (\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 | |
87 | if (Sm3Context == NULL || NewSm3Context == NULL) {\r | |
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 | |
114 | BOOLEAN\r | |
115 | EFIAPI\r | |
116 | Sm3Update (\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 | |
132 | if (Data == NULL && DataSize != 0) {\r | |
133 | return FALSE;\r | |
134 | }\r | |
135 | \r | |
136 | //\r | |
137 | // Openssl SM3 Hash Update\r | |
138 | //\r | |
139 | sm3_update ((SM3_CTX *) Sm3Context, Data, DataSize);\r | |
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 | |
164 | BOOLEAN\r | |
165 | EFIAPI\r | |
166 | Sm3Final (\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 | |
174 | if (Sm3Context == NULL || HashValue == NULL) {\r | |
175 | return FALSE;\r | |
176 | }\r | |
177 | \r | |
178 | //\r | |
179 | // Openssl SM3 Hash Finalization\r | |
180 | //\r | |
181 | sm3_final (HashValue, (SM3_CTX *) Sm3Context);\r | |
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 | |
204 | BOOLEAN\r | |
205 | EFIAPI\r | |
206 | Sm3HashAll (\r | |
207 | IN CONST VOID *Data,\r | |
208 | IN UINTN DataSize,\r | |
209 | OUT UINT8 *HashValue\r | |
210 | )\r | |
211 | {\r | |
212 | SM3_CTX Ctx;\r | |
213 | \r | |
214 | //\r | |
215 | // Check input parameters.\r | |
216 | //\r | |
217 | if (HashValue == NULL) {\r | |
218 | return FALSE;\r | |
219 | }\r | |
220 | if (Data == NULL && DataSize != 0) {\r | |
221 | return FALSE;\r | |
222 | }\r | |
223 | \r | |
224 | //\r | |
225 | // SM3 Hash Computation.\r | |
226 | //\r | |
227 | sm3_init(&Ctx);\r | |
228 | \r | |
229 | sm3_update(&Ctx, Data, DataSize);\r | |
230 | \r | |
231 | sm3_final(HashValue, &Ctx);\r | |
232 | \r | |
233 | return TRUE;\r | |
234 | }\r |