]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Application/Cryptest/HashVerify.c
Update CryptoPkg for new ciphers (HMAC, Block Cipher, etc) supports.
[mirror_edk2.git] / CryptoPkg / Application / Cryptest / HashVerify.c
1 /** @file
2 Application for Hash Primitives Validation.
3
4 Copyright (c) 2010, 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 MD5("abc"). (From "A.5 Test suite" of IETF RFC1321)
29 //
30 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Md5Digest[MD5_DIGEST_SIZE] = {
31 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72
32 };
33
34 //
35 // Result for SHA-1("abc"). (From "A.1 SHA-1 Example" of NIST FIPS 180-2)
36 //
37 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha1Digest[SHA1_DIGEST_SIZE] = {
38 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
39 0x9c, 0xd0, 0xd8, 0x9d
40 };
41
42 //
43 // Result for SHA-256("abc"). (From "B.1 SHA-256 Example" of NIST FIPS 180-2)
44 //
45 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha256Digest[SHA256_DIGEST_SIZE] = {
46 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
47 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
48 };
49
50 /**
51 Validate UEFI-OpenSSL Digest Interfaces.
52
53 @retval EFI_SUCCESS Validation succeeded.
54 @retval EFI_ABORTED Validation failed.
55
56 **/
57 EFI_STATUS
58 ValidateCryptDigest (
59 VOID
60 )
61 {
62 UINTN CtxSize;
63 VOID *HashCtx;
64 UINTN DataSize;
65 UINT8 Digest[MAX_DIGEST_SIZE];
66 BOOLEAN Status;
67
68 Print (L" UEFI-OpenSSL Hash Engine Testing:\n");
69 DataSize = AsciiStrLen (HashData);
70
71 Print (L"- MD5: ");
72
73 //
74 // MD5 Digest Validation
75 //
76 ZeroMem (Digest, MAX_DIGEST_SIZE);
77 CtxSize = Md5GetContextSize ();
78 HashCtx = AllocatePool (CtxSize);
79
80 Print (L"Init... ");
81 Status = Md5Init (HashCtx);
82 if (!Status) {
83 Print (L"[Fail]");
84 return EFI_ABORTED;
85 }
86
87 Print (L"Update... ");
88 Status = Md5Update (HashCtx, HashData, DataSize);
89 if (!Status) {
90 Print (L"[Fail]");
91 return EFI_ABORTED;
92 }
93
94 Print (L"Finalize... ");
95 Status = Md5Final (HashCtx, Digest);
96 if (!Status) {
97 Print (L"[Fail]");
98 return EFI_ABORTED;
99 }
100
101 FreePool (HashCtx);
102
103 Print (L"Check Value... ");
104 if (CompareMem (Digest, Md5Digest, MD5_DIGEST_SIZE) != 0) {
105 Print (L"[Fail]");
106 return EFI_ABORTED;
107 }
108
109 Print (L"[Pass]\n");
110
111 Print (L"- SHA1: ");
112
113 //
114 // SHA-1 Digest Validation
115 //
116 ZeroMem (Digest, MAX_DIGEST_SIZE);
117 CtxSize = Sha1GetContextSize ();
118 HashCtx = AllocatePool (CtxSize);
119
120 Print (L"Init... ");
121 Status = Sha1Init (HashCtx);
122 if (!Status) {
123 Print (L"[Fail]");
124 return EFI_ABORTED;
125 }
126
127 Print (L"Update... ");
128 Status = Sha1Update (HashCtx, HashData, DataSize);
129 if (!Status) {
130 Print (L"[Fail]");
131 return EFI_ABORTED;
132 }
133
134 Print (L"Finalize... ");
135 Status = Sha1Final (HashCtx, Digest);
136 if (!Status) {
137 Print (L"[Fail]");
138 return EFI_ABORTED;
139 }
140
141 FreePool (HashCtx);
142
143 Print (L"Check Value... ");
144 if (CompareMem (Digest, Sha1Digest, SHA1_DIGEST_SIZE) != 0) {
145 Print (L"[Fail]");
146 return EFI_ABORTED;
147 }
148
149 Print (L"[Pass]\n");
150
151 Print (L"- SHA256: ");
152
153 //
154 // SHA256 Digest Validation
155 //
156 ZeroMem (Digest, MAX_DIGEST_SIZE);
157 CtxSize = Sha256GetContextSize ();
158 HashCtx = AllocatePool (CtxSize);
159
160 Print (L"Init... ");
161 Status = Sha256Init (HashCtx);
162 if (!Status) {
163 Print (L"[Fail]");
164 return EFI_ABORTED;
165 }
166
167 Print (L"Update... ");
168 Status = Sha256Update (HashCtx, HashData, DataSize);
169 if (!Status) {
170 Print (L"[Fail]");
171 return EFI_ABORTED;
172 }
173
174 Print (L"Finalize... ");
175 Status = Sha256Final (HashCtx, Digest);
176 if (!Status) {
177 Print (L"[Fail]");
178 return EFI_ABORTED;
179 }
180
181 FreePool (HashCtx);
182
183 Print (L"Check Value... ");
184 if (CompareMem (Digest, Sha256Digest, SHA256_DIGEST_SIZE) != 0) {
185 Print (L"[Fail]");
186 return EFI_ABORTED;
187 }
188
189 Print (L"[Pass]\n");
190
191 return EFI_SUCCESS;
192 }