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