2 Application for Hash Primitives Validation.
4 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "TestBaseCryptLib.h"
12 // Max Known Digest Size is SHA512 Output (64 bytes) by far
14 #define MAX_DIGEST_SIZE 64
17 // Message string for digest validation
19 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*HashData
= "abc";
22 // Result for MD5("abc"). (From "A.5 Test suite" of IETF RFC1321)
24 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Md5Digest
[MD5_DIGEST_SIZE
] = {
25 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72
30 // Result for SHA-1("abc"). (From "A.1 SHA-1 Example" of NIST FIPS 180-2)
32 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha1Digest
[SHA1_DIGEST_SIZE
] = {
33 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
34 0x9c, 0xd0, 0xd8, 0x9d
38 // Result for SHA-256("abc"). (From "B.1 SHA-256 Example" of NIST FIPS 180-2)
40 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha256Digest
[SHA256_DIGEST_SIZE
] = {
41 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
42 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
46 // Result for SHA-384("abc"). (From "D.1 SHA-384 Example" of NIST FIPS 180-2)
48 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha384Digest
[SHA384_DIGEST_SIZE
] = {
49 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
50 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
51 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
55 // Result for SHA-512("abc"). (From "C.1 SHA-512 Example" of NIST FIPS 180-2)
57 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha512Digest
[SHA512_DIGEST_SIZE
] = {
58 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
59 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
60 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
61 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
66 (EFIAPI
*EFI_HASH_GET_CONTEXT_SIZE
) (
72 (EFIAPI
*EFI_HASH_INIT
) (
78 (EFIAPI
*EFI_HASH_UPDATE
) (
79 IN OUT VOID
*HashContext
,
86 (EFIAPI
*EFI_HASH_FINAL
) (
87 IN OUT VOID
*HashContext
,
93 (EFIAPI
*EFI_HASH_ALL
) (
101 EFI_HASH_GET_CONTEXT_SIZE GetContextSize
;
102 EFI_HASH_INIT HashInit
;
103 EFI_HASH_UPDATE HashUpdate
;
104 EFI_HASH_FINAL HashFinal
;
105 EFI_HASH_ALL HashAll
;
110 HASH_TEST_CONTEXT mMd5TestCtx
= {MD5_DIGEST_SIZE
, Md5GetContextSize
, Md5Init
, Md5Update
, Md5Final
, Md5HashAll
, Md5Digest
};
111 HASH_TEST_CONTEXT mSha1TestCtx
= {SHA1_DIGEST_SIZE
, Sha1GetContextSize
, Sha1Init
, Sha1Update
, Sha1Final
, Sha1HashAll
, Sha1Digest
};
112 HASH_TEST_CONTEXT mSha256TestCtx
= {SHA256_DIGEST_SIZE
, Sha256GetContextSize
, Sha256Init
, Sha256Update
, Sha256Final
, Sha256HashAll
, Sha256Digest
};
113 HASH_TEST_CONTEXT mSha384TestCtx
= {SHA384_DIGEST_SIZE
, Sha384GetContextSize
, Sha384Init
, Sha384Update
, Sha384Final
, Sha384HashAll
, Sha384Digest
};
114 HASH_TEST_CONTEXT mSha512TestCtx
= {SHA512_DIGEST_SIZE
, Sha512GetContextSize
, Sha512Init
, Sha512Update
, Sha512Final
, Sha512HashAll
, Sha512Digest
};
118 TestVerifyHashPreReq (
119 UNIT_TEST_CONTEXT Context
122 HASH_TEST_CONTEXT
*HashTestContext
;
125 HashTestContext
= Context
;
126 CtxSize
= HashTestContext
->GetContextSize ();
127 HashTestContext
->HashCtx
= AllocatePool (CtxSize
);
128 if (HashTestContext
->HashCtx
== NULL
) {
129 return UNIT_TEST_ERROR_TEST_FAILED
;
132 return UNIT_TEST_PASSED
;
137 TestVerifyHashCleanUp (
138 UNIT_TEST_CONTEXT Context
141 HASH_TEST_CONTEXT
*HashTestContext
;
143 HashTestContext
= Context
;
144 if (HashTestContext
->HashCtx
!= NULL
) {
145 FreePool (HashTestContext
->HashCtx
);
152 IN UNIT_TEST_CONTEXT Context
156 UINT8 Digest
[MAX_DIGEST_SIZE
];
158 HASH_TEST_CONTEXT
*HashTestContext
;
160 HashTestContext
= Context
;
162 DataSize
= AsciiStrLen (HashData
);
164 ZeroMem (Digest
, MAX_DIGEST_SIZE
);
166 Status
= HashTestContext
->HashInit (HashTestContext
->HashCtx
);
167 UT_ASSERT_TRUE (Status
);
169 Status
= HashTestContext
->HashUpdate (HashTestContext
->HashCtx
, HashData
, DataSize
);
170 UT_ASSERT_TRUE (Status
);
172 Status
= HashTestContext
->HashFinal (HashTestContext
->HashCtx
, Digest
);
173 UT_ASSERT_TRUE (Status
);
175 UT_ASSERT_MEM_EQUAL (Digest
, HashTestContext
->Digest
, HashTestContext
->DigestSize
);
177 ZeroMem (Digest
, MAX_DIGEST_SIZE
);
178 Status
= HashTestContext
->HashAll (HashData
, DataSize
, Digest
);
179 UT_ASSERT_TRUE (Status
);
181 UT_ASSERT_MEM_EQUAL (Digest
, HashTestContext
->Digest
, HashTestContext
->DigestSize
);
183 return UNIT_TEST_PASSED
;
186 TEST_DESC mHashTest
[] = {
188 // -----Description----------------Class---------------------Function---------------Pre------------------Post------------Context
190 {"TestVerifyMd5()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash
, TestVerifyHashPreReq
, TestVerifyHashCleanUp
, &mMd5TestCtx
},
191 {"TestVerifySha1()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash
, TestVerifyHashPreReq
, TestVerifyHashCleanUp
, &mSha1TestCtx
},
192 {"TestVerifySha256()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash
, TestVerifyHashPreReq
, TestVerifyHashCleanUp
, &mSha256TestCtx
},
193 {"TestVerifySha384()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash
, TestVerifyHashPreReq
, TestVerifyHashCleanUp
, &mSha384TestCtx
},
194 {"TestVerifySha512()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash
, TestVerifyHashPreReq
, TestVerifyHashCleanUp
, &mSha512TestCtx
},
197 UINTN mHashTestNum
= ARRAY_SIZE(mHashTest
);