]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Application/Cryptest/HashVerify.c
CryptoPkg: Add xxxxHashAll APIs to facilitate the digest computation
[mirror_edk2.git] / CryptoPkg / Application / Cryptest / HashVerify.c
CommitLineData
2ac68e8b 1/** @file\r
a8c44645 2 Application for Hash Primitives Validation.\r
3\r
b7d1ba0a 4Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
a8c44645 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 "Cryptest.h"\r
16\r
17//\r
18// Max Known Digest Size is SHA512 Output (64 bytes) by far\r
19//\r
20#define MAX_DIGEST_SIZE 64\r
21\r
22//\r
23// Message string for digest validation\r
24//\r
25GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *HashData = "abc";\r
26\r
4a567c96 27//\r
28// Result for MD4("abc"). (From "A.5 Test suite" of IETF RFC1320)\r
29//\r
30GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Md4Digest[MD4_DIGEST_SIZE] = {\r
31 0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d\r
32 };\r
33\r
a8c44645 34//\r
35// Result for MD5("abc"). (From "A.5 Test suite" of IETF RFC1321)\r
36//\r
37GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Md5Digest[MD5_DIGEST_SIZE] = {\r
38 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72\r
39 };\r
40\r
41//\r
42// Result for SHA-1("abc"). (From "A.1 SHA-1 Example" of NIST FIPS 180-2)\r
43//\r
44GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha1Digest[SHA1_DIGEST_SIZE] = {\r
45 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,\r
46 0x9c, 0xd0, 0xd8, 0x9d\r
47 };\r
48\r
49//\r
50// Result for SHA-256("abc"). (From "B.1 SHA-256 Example" of NIST FIPS 180-2)\r
51//\r
52GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha256Digest[SHA256_DIGEST_SIZE] = {\r
53 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,\r
54 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad\r
55 };\r
56\r
2ac68e8b
QL
57//\r
58// Result for SHA-384("abc"). (From "D.1 SHA-384 Example" of NIST FIPS 180-2)\r
59//\r
60GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha384Digest[SHA384_DIGEST_SIZE] = {\r
61 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,\r
62 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,\r
63 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7\r
64 };\r
65\r
66//\r
67// Result for SHA-512("abc"). (From "C.1 SHA-512 Example" of NIST FIPS 180-2)\r
68//\r
69GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha512Digest[SHA512_DIGEST_SIZE] = {\r
70 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,\r
71 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,\r
72 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,\r
73 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f\r
74 };\r
75\r
a8c44645 76/**\r
77 Validate UEFI-OpenSSL Digest Interfaces.\r
78\r
79 @retval EFI_SUCCESS Validation succeeded.\r
80 @retval EFI_ABORTED Validation failed.\r
81\r
82**/\r
83EFI_STATUS\r
84ValidateCryptDigest (\r
85 VOID\r
86 )\r
87{\r
88 UINTN CtxSize;\r
89 VOID *HashCtx;\r
90 UINTN DataSize;\r
91 UINT8 Digest[MAX_DIGEST_SIZE];\r
92 BOOLEAN Status;\r
93\r
94 Print (L" UEFI-OpenSSL Hash Engine Testing:\n");\r
95 DataSize = AsciiStrLen (HashData);\r
96\r
4a567c96 97 Print (L"- MD4: ");\r
2ac68e8b 98\r
4a567c96 99 //\r
100 // MD4 Digest Validation\r
101 //\r
102 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
103 CtxSize = Md4GetContextSize ();\r
104 HashCtx = AllocatePool (CtxSize);\r
105\r
106 Print (L"Init... ");\r
107 Status = Md4Init (HashCtx);\r
108 if (!Status) {\r
109 Print (L"[Fail]");\r
110 return EFI_ABORTED;\r
111 }\r
112\r
113 Print (L"Update... ");\r
114 Status = Md4Update (HashCtx, HashData, DataSize);\r
115 if (!Status) {\r
116 Print (L"[Fail]");\r
117 return EFI_ABORTED;\r
118 }\r
119\r
120 Print (L"Finalize... ");\r
121 Status = Md4Final (HashCtx, Digest);\r
122 if (!Status) {\r
123 Print (L"[Fail]");\r
124 return EFI_ABORTED;\r
125 }\r
126\r
127 FreePool (HashCtx);\r
128\r
129 Print (L"Check Value... ");\r
b7d1ba0a
QL
130 if (CompareMem (Digest, Md4Digest, MD4_DIGEST_SIZE) != 0) {\r
131 Print (L"[Fail]");\r
132 return EFI_ABORTED;\r
133 }\r
134\r
135 Print (L"HashAll... ");\r
136 ZeroMem (Digest, MD5_DIGEST_SIZE);\r
137 Status = Md4HashAll (HashData, DataSize, Digest);\r
138 if (!Status) {\r
139 Print (L"[Fail]");\r
140 return EFI_ABORTED;\r
141 }\r
142 if (CompareMem (Digest, Md4Digest, MD4_DIGEST_SIZE) != 0) {\r
4a567c96 143 Print (L"[Fail]");\r
144 return EFI_ABORTED;\r
145 }\r
146\r
147 Print (L"[Pass]\n");\r
148\r
a8c44645 149 Print (L"- MD5: ");\r
150\r
151 //\r
152 // MD5 Digest Validation\r
153 //\r
154 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
155 CtxSize = Md5GetContextSize ();\r
156 HashCtx = AllocatePool (CtxSize);\r
157\r
158 Print (L"Init... ");\r
159 Status = Md5Init (HashCtx);\r
160 if (!Status) {\r
161 Print (L"[Fail]");\r
162 return EFI_ABORTED;\r
163 }\r
164\r
165 Print (L"Update... ");\r
166 Status = Md5Update (HashCtx, HashData, DataSize);\r
167 if (!Status) {\r
168 Print (L"[Fail]");\r
169 return EFI_ABORTED;\r
170 }\r
171\r
172 Print (L"Finalize... ");\r
173 Status = Md5Final (HashCtx, Digest);\r
174 if (!Status) {\r
175 Print (L"[Fail]");\r
176 return EFI_ABORTED;\r
177 }\r
178\r
179 FreePool (HashCtx);\r
180\r
181 Print (L"Check Value... ");\r
182 if (CompareMem (Digest, Md5Digest, MD5_DIGEST_SIZE) != 0) {\r
183 Print (L"[Fail]");\r
184 return EFI_ABORTED;\r
185 }\r
186\r
b7d1ba0a
QL
187 Print (L"HashAll... ");\r
188 ZeroMem (Digest, MD5_DIGEST_SIZE);\r
189 Status = Md5HashAll (HashData, DataSize, Digest);\r
190 if (!Status) {\r
191 Print (L"[Fail]");\r
192 return EFI_ABORTED;\r
193 }\r
194 if (CompareMem (Digest, Md5Digest, MD5_DIGEST_SIZE) != 0) {\r
195 Print (L"[Fail]");\r
196 return EFI_ABORTED;\r
197 }\r
198\r
a8c44645 199 Print (L"[Pass]\n");\r
200\r
201 Print (L"- SHA1: ");\r
202\r
203 //\r
204 // SHA-1 Digest Validation\r
205 //\r
206 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
207 CtxSize = Sha1GetContextSize ();\r
208 HashCtx = AllocatePool (CtxSize);\r
209\r
210 Print (L"Init... ");\r
211 Status = Sha1Init (HashCtx);\r
212 if (!Status) {\r
213 Print (L"[Fail]");\r
214 return EFI_ABORTED;\r
215 }\r
216\r
217 Print (L"Update... ");\r
218 Status = Sha1Update (HashCtx, HashData, DataSize);\r
219 if (!Status) {\r
220 Print (L"[Fail]");\r
221 return EFI_ABORTED;\r
222 }\r
223\r
224 Print (L"Finalize... ");\r
225 Status = Sha1Final (HashCtx, Digest);\r
226 if (!Status) {\r
227 Print (L"[Fail]");\r
228 return EFI_ABORTED;\r
229 }\r
230\r
231 FreePool (HashCtx);\r
232\r
233 Print (L"Check Value... ");\r
234 if (CompareMem (Digest, Sha1Digest, SHA1_DIGEST_SIZE) != 0) {\r
235 Print (L"[Fail]");\r
236 return EFI_ABORTED;\r
237 }\r
238\r
b7d1ba0a
QL
239 Print (L"HashAll... ");\r
240 ZeroMem (Digest, SHA1_DIGEST_SIZE);\r
241 Status = Sha1HashAll (HashData, DataSize, Digest);\r
242 if (!Status) {\r
243 Print (L"[Fail]");\r
244 return EFI_ABORTED;\r
245 }\r
246 if (CompareMem (Digest, Sha1Digest, SHA1_DIGEST_SIZE) != 0) {\r
247 Print (L"[Fail]");\r
248 return EFI_ABORTED;\r
249 }\r
250\r
a8c44645 251 Print (L"[Pass]\n");\r
252\r
253 Print (L"- SHA256: ");\r
254\r
255 //\r
256 // SHA256 Digest Validation\r
257 //\r
258 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
259 CtxSize = Sha256GetContextSize ();\r
260 HashCtx = AllocatePool (CtxSize);\r
261\r
262 Print (L"Init... ");\r
263 Status = Sha256Init (HashCtx);\r
264 if (!Status) {\r
265 Print (L"[Fail]");\r
266 return EFI_ABORTED;\r
267 }\r
268\r
269 Print (L"Update... ");\r
270 Status = Sha256Update (HashCtx, HashData, DataSize);\r
271 if (!Status) {\r
272 Print (L"[Fail]");\r
273 return EFI_ABORTED;\r
274 }\r
275\r
276 Print (L"Finalize... ");\r
277 Status = Sha256Final (HashCtx, Digest);\r
278 if (!Status) {\r
279 Print (L"[Fail]");\r
280 return EFI_ABORTED;\r
281 }\r
282\r
283 FreePool (HashCtx);\r
284\r
285 Print (L"Check Value... ");\r
286 if (CompareMem (Digest, Sha256Digest, SHA256_DIGEST_SIZE) != 0) {\r
287 Print (L"[Fail]");\r
288 return EFI_ABORTED;\r
289 }\r
290\r
b7d1ba0a
QL
291 Print (L"HashAll... ");\r
292 ZeroMem (Digest, SHA256_DIGEST_SIZE);\r
293 Status = Sha256HashAll (HashData, DataSize, Digest);\r
294 if (!Status) {\r
295 Print (L"[Fail]");\r
296 return EFI_ABORTED;\r
297 }\r
298 if (CompareMem (Digest, Sha256Digest, SHA256_DIGEST_SIZE) != 0) {\r
299 Print (L"[Fail]");\r
300 return EFI_ABORTED;\r
301 }\r
302\r
a8c44645 303 Print (L"[Pass]\n");\r
2ac68e8b
QL
304\r
305 Print (L"- SHA384: ");\r
306\r
307 //\r
308 // SHA384 Digest Validation\r
309 //\r
310 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
311 CtxSize = Sha384GetContextSize ();\r
312 HashCtx = AllocatePool (CtxSize);\r
313\r
314 Print (L"Init... ");\r
315 Status = Sha384Init (HashCtx);\r
316 if (!Status) {\r
317 Print (L"[Fail]");\r
318 return EFI_ABORTED;\r
319 }\r
320\r
321 Print (L"Update... ");\r
322 Status = Sha384Update (HashCtx, HashData, DataSize);\r
323 if (!Status) {\r
324 Print (L"[Fail]");\r
325 return EFI_ABORTED;\r
326 }\r
327\r
328 Print (L"Finalize... ");\r
329 Status = Sha384Final (HashCtx, Digest);\r
330 if (!Status) {\r
331 Print (L"[Fail]");\r
332 return EFI_ABORTED;\r
333 }\r
334\r
335 FreePool (HashCtx);\r
336\r
337 Print (L"Check Value... ");\r
338 if (CompareMem (Digest, Sha384Digest, SHA384_DIGEST_SIZE) != 0) {\r
339 Print (L"[Fail]");\r
340 return EFI_ABORTED;\r
341 }\r
342\r
b7d1ba0a
QL
343 Print (L"HashAll... ");\r
344 ZeroMem (Digest, SHA384_DIGEST_SIZE);\r
345 Status = Sha384HashAll (HashData, DataSize, Digest);\r
346 if (!Status) {\r
347 Print (L"[Fail]");\r
348 return EFI_ABORTED;\r
349 }\r
350 if (CompareMem (Digest, Sha384Digest, SHA384_DIGEST_SIZE) != 0) {\r
351 Print (L"[Fail]");\r
352 return EFI_ABORTED;\r
353 }\r
354\r
2ac68e8b
QL
355 Print (L"[Pass]\n");\r
356\r
357 Print (L"- SHA512: ");\r
358\r
359 //\r
360 // SHA512 Digest Validation\r
361 //\r
362 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
363 CtxSize = Sha512GetContextSize ();\r
364 HashCtx = AllocatePool (CtxSize);\r
365\r
366 Print (L"Init... ");\r
367 Status = Sha512Init (HashCtx);\r
368 if (!Status) {\r
369 Print (L"[Fail]");\r
370 return EFI_ABORTED;\r
371 }\r
372\r
373 Print (L"Update... ");\r
374 Status = Sha512Update (HashCtx, HashData, DataSize);\r
375 if (!Status) {\r
376 Print (L"[Fail]");\r
377 return EFI_ABORTED;\r
378 }\r
379\r
380 Print (L"Finalize... ");\r
381 Status = Sha512Final (HashCtx, Digest);\r
382 if (!Status) {\r
383 Print (L"[Fail]");\r
384 return EFI_ABORTED;\r
385 }\r
386\r
387 FreePool (HashCtx);\r
388\r
389 Print (L"Check Value... ");\r
390 if (CompareMem (Digest, Sha512Digest, SHA512_DIGEST_SIZE) != 0) {\r
391 Print (L"[Fail]");\r
392 return EFI_ABORTED;\r
393 }\r
394\r
b7d1ba0a
QL
395 Print (L"HashAll... ");\r
396 ZeroMem (Digest, SHA512_DIGEST_SIZE);\r
397 Status = Sha512HashAll (HashData, DataSize, Digest);\r
398 if (!Status) {\r
399 Print (L"[Fail]");\r
400 return EFI_ABORTED;\r
401 }\r
402 if (CompareMem (Digest, Sha512Digest, SHA512_DIGEST_SIZE) != 0) {\r
403 Print (L"[Fail]");\r
404 return EFI_ABORTED;\r
405 }\r
406\r
2ac68e8b
QL
407 Print (L"[Pass]\n");\r
408\r
a8c44645 409 return EFI_SUCCESS;\r
410}\r