]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Application/Cryptest/HashVerify.c
CryptoPkg Updates to support RFC3161 timestamp signature verification.
[mirror_edk2.git] / CryptoPkg / Application / Cryptest / HashVerify.c
CommitLineData
2ac68e8b 1/** @file\r
a8c44645 2 Application for Hash Primitives Validation.\r
3\r
2ac68e8b 4Copyright (c) 2010 - 2014, 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
130 if (CompareMem (Digest, Md4Digest, MD5_DIGEST_SIZE) != 0) {\r
131 Print (L"[Fail]");\r
132 return EFI_ABORTED;\r
133 }\r
134\r
135 Print (L"[Pass]\n");\r
136\r
a8c44645 137 Print (L"- MD5: ");\r
138\r
139 //\r
140 // MD5 Digest Validation\r
141 //\r
142 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
143 CtxSize = Md5GetContextSize ();\r
144 HashCtx = AllocatePool (CtxSize);\r
145\r
146 Print (L"Init... ");\r
147 Status = Md5Init (HashCtx);\r
148 if (!Status) {\r
149 Print (L"[Fail]");\r
150 return EFI_ABORTED;\r
151 }\r
152\r
153 Print (L"Update... ");\r
154 Status = Md5Update (HashCtx, HashData, DataSize);\r
155 if (!Status) {\r
156 Print (L"[Fail]");\r
157 return EFI_ABORTED;\r
158 }\r
159\r
160 Print (L"Finalize... ");\r
161 Status = Md5Final (HashCtx, Digest);\r
162 if (!Status) {\r
163 Print (L"[Fail]");\r
164 return EFI_ABORTED;\r
165 }\r
166\r
167 FreePool (HashCtx);\r
168\r
169 Print (L"Check Value... ");\r
170 if (CompareMem (Digest, Md5Digest, MD5_DIGEST_SIZE) != 0) {\r
171 Print (L"[Fail]");\r
172 return EFI_ABORTED;\r
173 }\r
174\r
175 Print (L"[Pass]\n");\r
176\r
177 Print (L"- SHA1: ");\r
178\r
179 //\r
180 // SHA-1 Digest Validation\r
181 //\r
182 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
183 CtxSize = Sha1GetContextSize ();\r
184 HashCtx = AllocatePool (CtxSize);\r
185\r
186 Print (L"Init... ");\r
187 Status = Sha1Init (HashCtx);\r
188 if (!Status) {\r
189 Print (L"[Fail]");\r
190 return EFI_ABORTED;\r
191 }\r
192\r
193 Print (L"Update... ");\r
194 Status = Sha1Update (HashCtx, HashData, DataSize);\r
195 if (!Status) {\r
196 Print (L"[Fail]");\r
197 return EFI_ABORTED;\r
198 }\r
199\r
200 Print (L"Finalize... ");\r
201 Status = Sha1Final (HashCtx, Digest);\r
202 if (!Status) {\r
203 Print (L"[Fail]");\r
204 return EFI_ABORTED;\r
205 }\r
206\r
207 FreePool (HashCtx);\r
208\r
209 Print (L"Check Value... ");\r
210 if (CompareMem (Digest, Sha1Digest, SHA1_DIGEST_SIZE) != 0) {\r
211 Print (L"[Fail]");\r
212 return EFI_ABORTED;\r
213 }\r
214\r
215 Print (L"[Pass]\n");\r
216\r
217 Print (L"- SHA256: ");\r
218\r
219 //\r
220 // SHA256 Digest Validation\r
221 //\r
222 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
223 CtxSize = Sha256GetContextSize ();\r
224 HashCtx = AllocatePool (CtxSize);\r
225\r
226 Print (L"Init... ");\r
227 Status = Sha256Init (HashCtx);\r
228 if (!Status) {\r
229 Print (L"[Fail]");\r
230 return EFI_ABORTED;\r
231 }\r
232\r
233 Print (L"Update... ");\r
234 Status = Sha256Update (HashCtx, HashData, DataSize);\r
235 if (!Status) {\r
236 Print (L"[Fail]");\r
237 return EFI_ABORTED;\r
238 }\r
239\r
240 Print (L"Finalize... ");\r
241 Status = Sha256Final (HashCtx, Digest);\r
242 if (!Status) {\r
243 Print (L"[Fail]");\r
244 return EFI_ABORTED;\r
245 }\r
246\r
247 FreePool (HashCtx);\r
248\r
249 Print (L"Check Value... ");\r
250 if (CompareMem (Digest, Sha256Digest, SHA256_DIGEST_SIZE) != 0) {\r
251 Print (L"[Fail]");\r
252 return EFI_ABORTED;\r
253 }\r
254\r
255 Print (L"[Pass]\n");\r
2ac68e8b
QL
256\r
257 Print (L"- SHA384: ");\r
258\r
259 //\r
260 // SHA384 Digest Validation\r
261 //\r
262 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
263 CtxSize = Sha384GetContextSize ();\r
264 HashCtx = AllocatePool (CtxSize);\r
265\r
266 Print (L"Init... ");\r
267 Status = Sha384Init (HashCtx);\r
268 if (!Status) {\r
269 Print (L"[Fail]");\r
270 return EFI_ABORTED;\r
271 }\r
272\r
273 Print (L"Update... ");\r
274 Status = Sha384Update (HashCtx, HashData, DataSize);\r
275 if (!Status) {\r
276 Print (L"[Fail]");\r
277 return EFI_ABORTED;\r
278 }\r
279\r
280 Print (L"Finalize... ");\r
281 Status = Sha384Final (HashCtx, Digest);\r
282 if (!Status) {\r
283 Print (L"[Fail]");\r
284 return EFI_ABORTED;\r
285 }\r
286\r
287 FreePool (HashCtx);\r
288\r
289 Print (L"Check Value... ");\r
290 if (CompareMem (Digest, Sha384Digest, SHA384_DIGEST_SIZE) != 0) {\r
291 Print (L"[Fail]");\r
292 return EFI_ABORTED;\r
293 }\r
294\r
295 Print (L"[Pass]\n");\r
296\r
297 Print (L"- SHA512: ");\r
298\r
299 //\r
300 // SHA512 Digest Validation\r
301 //\r
302 ZeroMem (Digest, MAX_DIGEST_SIZE);\r
303 CtxSize = Sha512GetContextSize ();\r
304 HashCtx = AllocatePool (CtxSize);\r
305\r
306 Print (L"Init... ");\r
307 Status = Sha512Init (HashCtx);\r
308 if (!Status) {\r
309 Print (L"[Fail]");\r
310 return EFI_ABORTED;\r
311 }\r
312\r
313 Print (L"Update... ");\r
314 Status = Sha512Update (HashCtx, HashData, DataSize);\r
315 if (!Status) {\r
316 Print (L"[Fail]");\r
317 return EFI_ABORTED;\r
318 }\r
319\r
320 Print (L"Finalize... ");\r
321 Status = Sha512Final (HashCtx, Digest);\r
322 if (!Status) {\r
323 Print (L"[Fail]");\r
324 return EFI_ABORTED;\r
325 }\r
326\r
327 FreePool (HashCtx);\r
328\r
329 Print (L"Check Value... ");\r
330 if (CompareMem (Digest, Sha512Digest, SHA512_DIGEST_SIZE) != 0) {\r
331 Print (L"[Fail]");\r
332 return EFI_ABORTED;\r
333 }\r
334\r
335 Print (L"[Pass]\n");\r
336\r
a8c44645 337 return EFI_SUCCESS;\r
338}\r