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