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