]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Application/Cryptest/HashVerify.c
1. Add new API supports for PEM & X509 key retrieving & verification;
[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 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 Validate UEFI-OpenSSL Digest Interfaces.
59
60 @retval EFI_SUCCESS Validation succeeded.
61 @retval EFI_ABORTED Validation failed.
62
63 **/
64 EFI_STATUS
65 ValidateCryptDigest (
66 VOID
67 )
68 {
69 UINTN CtxSize;
70 VOID *HashCtx;
71 UINTN DataSize;
72 UINT8 Digest[MAX_DIGEST_SIZE];
73 BOOLEAN Status;
74
75 Print (L" UEFI-OpenSSL Hash Engine Testing:\n");
76 DataSize = AsciiStrLen (HashData);
77
78 Print (L"- MD4: ");
79
80 //
81 // MD4 Digest Validation
82 //
83 ZeroMem (Digest, MAX_DIGEST_SIZE);
84 CtxSize = Md4GetContextSize ();
85 HashCtx = AllocatePool (CtxSize);
86
87 Print (L"Init... ");
88 Status = Md4Init (HashCtx);
89 if (!Status) {
90 Print (L"[Fail]");
91 return EFI_ABORTED;
92 }
93
94 Print (L"Update... ");
95 Status = Md4Update (HashCtx, HashData, DataSize);
96 if (!Status) {
97 Print (L"[Fail]");
98 return EFI_ABORTED;
99 }
100
101 Print (L"Finalize... ");
102 Status = Md4Final (HashCtx, Digest);
103 if (!Status) {
104 Print (L"[Fail]");
105 return EFI_ABORTED;
106 }
107
108 FreePool (HashCtx);
109
110 Print (L"Check Value... ");
111 if (CompareMem (Digest, Md4Digest, MD5_DIGEST_SIZE) != 0) {
112 Print (L"[Fail]");
113 return EFI_ABORTED;
114 }
115
116 Print (L"[Pass]\n");
117
118 Print (L"- MD5: ");
119
120 //
121 // MD5 Digest Validation
122 //
123 ZeroMem (Digest, MAX_DIGEST_SIZE);
124 CtxSize = Md5GetContextSize ();
125 HashCtx = AllocatePool (CtxSize);
126
127 Print (L"Init... ");
128 Status = Md5Init (HashCtx);
129 if (!Status) {
130 Print (L"[Fail]");
131 return EFI_ABORTED;
132 }
133
134 Print (L"Update... ");
135 Status = Md5Update (HashCtx, HashData, DataSize);
136 if (!Status) {
137 Print (L"[Fail]");
138 return EFI_ABORTED;
139 }
140
141 Print (L"Finalize... ");
142 Status = Md5Final (HashCtx, Digest);
143 if (!Status) {
144 Print (L"[Fail]");
145 return EFI_ABORTED;
146 }
147
148 FreePool (HashCtx);
149
150 Print (L"Check Value... ");
151 if (CompareMem (Digest, Md5Digest, MD5_DIGEST_SIZE) != 0) {
152 Print (L"[Fail]");
153 return EFI_ABORTED;
154 }
155
156 Print (L"[Pass]\n");
157
158 Print (L"- SHA1: ");
159
160 //
161 // SHA-1 Digest Validation
162 //
163 ZeroMem (Digest, MAX_DIGEST_SIZE);
164 CtxSize = Sha1GetContextSize ();
165 HashCtx = AllocatePool (CtxSize);
166
167 Print (L"Init... ");
168 Status = Sha1Init (HashCtx);
169 if (!Status) {
170 Print (L"[Fail]");
171 return EFI_ABORTED;
172 }
173
174 Print (L"Update... ");
175 Status = Sha1Update (HashCtx, HashData, DataSize);
176 if (!Status) {
177 Print (L"[Fail]");
178 return EFI_ABORTED;
179 }
180
181 Print (L"Finalize... ");
182 Status = Sha1Final (HashCtx, Digest);
183 if (!Status) {
184 Print (L"[Fail]");
185 return EFI_ABORTED;
186 }
187
188 FreePool (HashCtx);
189
190 Print (L"Check Value... ");
191 if (CompareMem (Digest, Sha1Digest, SHA1_DIGEST_SIZE) != 0) {
192 Print (L"[Fail]");
193 return EFI_ABORTED;
194 }
195
196 Print (L"[Pass]\n");
197
198 Print (L"- SHA256: ");
199
200 //
201 // SHA256 Digest Validation
202 //
203 ZeroMem (Digest, MAX_DIGEST_SIZE);
204 CtxSize = Sha256GetContextSize ();
205 HashCtx = AllocatePool (CtxSize);
206
207 Print (L"Init... ");
208 Status = Sha256Init (HashCtx);
209 if (!Status) {
210 Print (L"[Fail]");
211 return EFI_ABORTED;
212 }
213
214 Print (L"Update... ");
215 Status = Sha256Update (HashCtx, HashData, DataSize);
216 if (!Status) {
217 Print (L"[Fail]");
218 return EFI_ABORTED;
219 }
220
221 Print (L"Finalize... ");
222 Status = Sha256Final (HashCtx, Digest);
223 if (!Status) {
224 Print (L"[Fail]");
225 return EFI_ABORTED;
226 }
227
228 FreePool (HashCtx);
229
230 Print (L"Check Value... ");
231 if (CompareMem (Digest, Sha256Digest, SHA256_DIGEST_SIZE) != 0) {
232 Print (L"[Fail]");
233 return EFI_ABORTED;
234 }
235
236 Print (L"[Pass]\n");
237
238 return EFI_SUCCESS;
239 }