]>
Commit | Line | Data |
---|---|---|
694bfd6f MC |
1 | /** @file\r |
2 | Application for Hash Primitives Validation.\r | |
3 | \r | |
388f9a93 | 4 | Copyright (c) 2010 - 2020, Intel Corporation. All rights reserved.<BR>\r |
694bfd6f MC |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
6 | \r | |
7 | **/\r | |
8 | \r | |
9 | #include "TestBaseCryptLib.h"\r | |
10 | \r | |
11 | //\r | |
12 | // Max Known Digest Size is SHA512 Output (64 bytes) by far\r | |
13 | //\r | |
7c342378 | 14 | #define MAX_DIGEST_SIZE 64\r |
694bfd6f MC |
15 | \r |
16 | //\r | |
17 | // Message string for digest validation\r | |
18 | //\r | |
7c342378 | 19 | GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *HashData = "abc";\r |
694bfd6f MC |
20 | \r |
21 | //\r | |
22 | // Result for MD5("abc"). (From "A.5 Test suite" of IETF RFC1321)\r | |
23 | //\r | |
388f9a93 | 24 | #ifdef ENABLE_MD5_DEPRECATED_INTERFACES\r |
7c342378 | 25 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Md5Digest[MD5_DIGEST_SIZE] = {\r |
694bfd6f | 26 | 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72\r |
7c342378 | 27 | };\r |
388f9a93 | 28 | #endif\r |
694bfd6f MC |
29 | \r |
30 | //\r | |
31 | // Result for SHA-1("abc"). (From "A.1 SHA-1 Example" of NIST FIPS 180-2)\r | |
32 | //\r | |
7c342378 | 33 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha1Digest[SHA1_DIGEST_SIZE] = {\r |
694bfd6f MC |
34 | 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,\r |
35 | 0x9c, 0xd0, 0xd8, 0x9d\r | |
7c342378 | 36 | };\r |
694bfd6f MC |
37 | \r |
38 | //\r | |
39 | // Result for SHA-256("abc"). (From "B.1 SHA-256 Example" of NIST FIPS 180-2)\r | |
40 | //\r | |
7c342378 | 41 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha256Digest[SHA256_DIGEST_SIZE] = {\r |
694bfd6f MC |
42 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,\r |
43 | 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad\r | |
7c342378 | 44 | };\r |
694bfd6f MC |
45 | \r |
46 | //\r | |
47 | // Result for SHA-384("abc"). (From "D.1 SHA-384 Example" of NIST FIPS 180-2)\r | |
48 | //\r | |
7c342378 | 49 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha384Digest[SHA384_DIGEST_SIZE] = {\r |
694bfd6f MC |
50 | 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,\r |
51 | 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,\r | |
52 | 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7\r | |
7c342378 | 53 | };\r |
694bfd6f MC |
54 | \r |
55 | //\r | |
56 | // Result for SHA-512("abc"). (From "C.1 SHA-512 Example" of NIST FIPS 180-2)\r | |
57 | //\r | |
7c342378 | 58 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Sha512Digest[SHA512_DIGEST_SIZE] = {\r |
694bfd6f MC |
59 | 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,\r |
60 | 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,\r | |
61 | 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,\r | |
62 | 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f\r | |
7c342378 | 63 | };\r |
694bfd6f MC |
64 | \r |
65 | typedef\r | |
66 | UINTN\r | |
7c342378 | 67 | (EFIAPI *EFI_HASH_GET_CONTEXT_SIZE)(\r |
694bfd6f MC |
68 | VOID\r |
69 | );\r | |
70 | \r | |
71 | typedef\r | |
72 | BOOLEAN\r | |
7c342378 | 73 | (EFIAPI *EFI_HASH_INIT)(\r |
694bfd6f MC |
74 | OUT VOID *HashContext\r |
75 | );\r | |
76 | \r | |
77 | typedef\r | |
78 | BOOLEAN\r | |
7c342378 | 79 | (EFIAPI *EFI_HASH_UPDATE)(\r |
694bfd6f MC |
80 | IN OUT VOID *HashContext,\r |
81 | IN CONST VOID *Data,\r | |
82 | IN UINTN DataSize\r | |
83 | );\r | |
84 | \r | |
85 | typedef\r | |
86 | BOOLEAN\r | |
7c342378 | 87 | (EFIAPI *EFI_HASH_FINAL)(\r |
694bfd6f MC |
88 | IN OUT VOID *HashContext,\r |
89 | OUT UINT8 *HashValue\r | |
90 | );\r | |
91 | \r | |
92 | typedef\r | |
93 | BOOLEAN\r | |
7c342378 | 94 | (EFIAPI *EFI_HASH_ALL)(\r |
694bfd6f MC |
95 | IN CONST VOID *Data,\r |
96 | IN UINTN DataSize,\r | |
97 | OUT UINT8 *HashValue\r | |
98 | );\r | |
99 | \r | |
100 | typedef struct {\r | |
7c342378 MK |
101 | UINT32 DigestSize;\r |
102 | EFI_HASH_GET_CONTEXT_SIZE GetContextSize;\r | |
103 | EFI_HASH_INIT HashInit;\r | |
104 | EFI_HASH_UPDATE HashUpdate;\r | |
105 | EFI_HASH_FINAL HashFinal;\r | |
106 | EFI_HASH_ALL HashAll;\r | |
107 | CONST UINT8 *Digest;\r | |
108 | VOID *HashCtx;\r | |
694bfd6f MC |
109 | } HASH_TEST_CONTEXT;\r |
110 | \r | |
388f9a93 | 111 | #ifdef ENABLE_MD5_DEPRECATED_INTERFACES\r |
7c342378 | 112 | HASH_TEST_CONTEXT mMd5TestCtx = { MD5_DIGEST_SIZE, Md5GetContextSize, Md5Init, Md5Update, Md5Final, Md5HashAll, Md5Digest };\r |
388f9a93 | 113 | #endif\r |
7c342378 MK |
114 | HASH_TEST_CONTEXT mSha1TestCtx = { SHA1_DIGEST_SIZE, Sha1GetContextSize, Sha1Init, Sha1Update, Sha1Final, Sha1HashAll, Sha1Digest };\r |
115 | HASH_TEST_CONTEXT mSha256TestCtx = { SHA256_DIGEST_SIZE, Sha256GetContextSize, Sha256Init, Sha256Update, Sha256Final, Sha256HashAll, Sha256Digest };\r | |
116 | HASH_TEST_CONTEXT mSha384TestCtx = { SHA384_DIGEST_SIZE, Sha384GetContextSize, Sha384Init, Sha384Update, Sha384Final, Sha384HashAll, Sha384Digest };\r | |
117 | HASH_TEST_CONTEXT mSha512TestCtx = { SHA512_DIGEST_SIZE, Sha512GetContextSize, Sha512Init, Sha512Update, Sha512Final, Sha512HashAll, Sha512Digest };\r | |
694bfd6f MC |
118 | \r |
119 | UNIT_TEST_STATUS\r | |
120 | EFIAPI\r | |
121 | TestVerifyHashPreReq (\r | |
7c342378 | 122 | UNIT_TEST_CONTEXT Context\r |
694bfd6f MC |
123 | )\r |
124 | {\r | |
7c342378 MK |
125 | HASH_TEST_CONTEXT *HashTestContext;\r |
126 | UINTN CtxSize;\r | |
694bfd6f | 127 | \r |
7c342378 MK |
128 | HashTestContext = Context;\r |
129 | CtxSize = HashTestContext->GetContextSize ();\r | |
694bfd6f MC |
130 | HashTestContext->HashCtx = AllocatePool (CtxSize);\r |
131 | if (HashTestContext->HashCtx == NULL) {\r | |
132 | return UNIT_TEST_ERROR_TEST_FAILED;\r | |
133 | }\r | |
134 | \r | |
135 | return UNIT_TEST_PASSED;\r | |
136 | }\r | |
137 | \r | |
138 | VOID\r | |
139 | EFIAPI\r | |
140 | TestVerifyHashCleanUp (\r | |
7c342378 | 141 | UNIT_TEST_CONTEXT Context\r |
694bfd6f MC |
142 | )\r |
143 | {\r | |
7c342378 | 144 | HASH_TEST_CONTEXT *HashTestContext;\r |
694bfd6f MC |
145 | \r |
146 | HashTestContext = Context;\r | |
147 | if (HashTestContext->HashCtx != NULL) {\r | |
148 | FreePool (HashTestContext->HashCtx);\r | |
149 | }\r | |
150 | }\r | |
151 | \r | |
152 | UNIT_TEST_STATUS\r | |
153 | EFIAPI\r | |
154 | TestVerifyHash (\r | |
7c342378 | 155 | IN UNIT_TEST_CONTEXT Context\r |
694bfd6f MC |
156 | )\r |
157 | {\r | |
7c342378 MK |
158 | UINTN DataSize;\r |
159 | UINT8 Digest[MAX_DIGEST_SIZE];\r | |
160 | BOOLEAN Status;\r | |
161 | HASH_TEST_CONTEXT *HashTestContext;\r | |
694bfd6f MC |
162 | \r |
163 | HashTestContext = Context;\r | |
164 | \r | |
165 | DataSize = AsciiStrLen (HashData);\r | |
166 | \r | |
167 | ZeroMem (Digest, MAX_DIGEST_SIZE);\r | |
168 | \r | |
7c342378 | 169 | Status = HashTestContext->HashInit (HashTestContext->HashCtx);\r |
694bfd6f MC |
170 | UT_ASSERT_TRUE (Status);\r |
171 | \r | |
7c342378 | 172 | Status = HashTestContext->HashUpdate (HashTestContext->HashCtx, HashData, DataSize);\r |
694bfd6f MC |
173 | UT_ASSERT_TRUE (Status);\r |
174 | \r | |
7c342378 | 175 | Status = HashTestContext->HashFinal (HashTestContext->HashCtx, Digest);\r |
694bfd6f MC |
176 | UT_ASSERT_TRUE (Status);\r |
177 | \r | |
178 | UT_ASSERT_MEM_EQUAL (Digest, HashTestContext->Digest, HashTestContext->DigestSize);\r | |
179 | \r | |
180 | ZeroMem (Digest, MAX_DIGEST_SIZE);\r | |
7c342378 | 181 | Status = HashTestContext->HashAll (HashData, DataSize, Digest);\r |
694bfd6f MC |
182 | UT_ASSERT_TRUE (Status);\r |
183 | \r | |
184 | UT_ASSERT_MEM_EQUAL (Digest, HashTestContext->Digest, HashTestContext->DigestSize);\r | |
185 | \r | |
186 | return UNIT_TEST_PASSED;\r | |
187 | }\r | |
188 | \r | |
7c342378 MK |
189 | TEST_DESC mHashTest[] = {\r |
190 | //\r | |
191 | // -----Description----------------Class---------------------Function---------------Pre------------------Post------------Context\r | |
192 | //\r | |
193 | #ifdef ENABLE_MD5_DEPRECATED_INTERFACES\r | |
194 | { "TestVerifyMd5()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mMd5TestCtx },\r | |
195 | #endif\r | |
196 | { "TestVerifySha1()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mSha1TestCtx },\r | |
197 | { "TestVerifySha256()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mSha256TestCtx },\r | |
198 | { "TestVerifySha384()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mSha384TestCtx },\r | |
199 | { "TestVerifySha512()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mSha512TestCtx },\r | |
694bfd6f MC |
200 | };\r |
201 | \r | |
7c342378 | 202 | UINTN mHashTestNum = ARRAY_SIZE (mHashTest);\r |