Commit | Line | Data |
---|---|---|
694bfd6f MC |
1 | /** @file\r |
2 | Application for HMAC 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 | |
7c342378 | 14 | #define MAX_DIGEST_SIZE 64\r |
694bfd6f MC |
15 | \r |
16 | //\r | |
17 | // Data string for HMAC validation\r | |
18 | //\r | |
7c342378 | 19 | GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *HmacData = "Hi There";\r |
694bfd6f MC |
20 | \r |
21 | //\r | |
22 | // Key value for HMAC-MD5 validation. (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)\r | |
23 | //\r | |
7c342378 | 24 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Key[16] = {\r |
694bfd6f | 25 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b\r |
7c342378 | 26 | };\r |
694bfd6f MC |
27 | \r |
28 | //\r | |
29 | // Result for HMAC-MD5("Hi There"). (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)\r | |
30 | //\r | |
7c342378 | 31 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Digest[] = {\r |
694bfd6f | 32 | 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d\r |
7c342378 | 33 | };\r |
694bfd6f MC |
34 | \r |
35 | //\r | |
36 | // Key value for HMAC-SHA-1 validation. (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)\r | |
37 | //\r | |
7c342378 | 38 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha1Key[20] = {\r |
694bfd6f MC |
39 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,\r |
40 | 0x0b, 0x0b, 0x0b, 0x0b\r | |
7c342378 | 41 | };\r |
694bfd6f MC |
42 | \r |
43 | //\r | |
44 | // Result for HMAC-SHA-1 ("Hi There"). (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)\r | |
45 | //\r | |
7c342378 | 46 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha1Digest[] = {\r |
694bfd6f MC |
47 | 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,\r |
48 | 0xf1, 0x46, 0xbe, 0x00\r | |
7c342378 | 49 | };\r |
694bfd6f MC |
50 | \r |
51 | //\r | |
52 | // Key value for HMAC-SHA-256 validation. (From "4. Test Vectors" of IETF RFC4231)\r | |
53 | //\r | |
7c342378 | 54 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Key[20] = {\r |
694bfd6f MC |
55 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,\r |
56 | 0x0b, 0x0b, 0x0b, 0x0b\r | |
7c342378 | 57 | };\r |
694bfd6f MC |
58 | \r |
59 | //\r | |
60 | // Result for HMAC-SHA-256 ("Hi There"). (From "4. Test Vectors" of IETF RFC4231)\r | |
61 | //\r | |
7c342378 | 62 | GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Digest[] = {\r |
694bfd6f MC |
63 | 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,\r |
64 | 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7\r | |
7c342378 | 65 | };\r |
694bfd6f MC |
66 | \r |
67 | typedef\r | |
7c342378 MK |
68 | VOID *\r |
69 | (EFIAPI *EFI_HMAC_NEW)(\r | |
694bfd6f MC |
70 | VOID\r |
71 | );\r | |
72 | \r | |
73 | typedef\r | |
74 | BOOLEAN\r | |
7c342378 | 75 | (EFIAPI *EFI_HMAC_INIT)(\r |
694bfd6f MC |
76 | IN OUT VOID *HashContext,\r |
77 | IN CONST UINT8 *Key,\r | |
78 | IN UINTN KeySize\r | |
79 | );\r | |
80 | \r | |
81 | typedef\r | |
82 | BOOLEAN\r | |
7c342378 | 83 | (EFIAPI *EFI_HMAC_UPDATE)(\r |
694bfd6f MC |
84 | IN OUT VOID *HashContext,\r |
85 | IN CONST VOID *Data,\r | |
86 | IN UINTN DataSize\r | |
87 | );\r | |
88 | \r | |
89 | typedef\r | |
90 | BOOLEAN\r | |
7c342378 | 91 | (EFIAPI *EFI_HMAC_FINAL)(\r |
694bfd6f MC |
92 | IN OUT VOID *HashContext,\r |
93 | OUT UINT8 *HashValue\r | |
94 | );\r | |
95 | \r | |
96 | typedef struct {\r | |
7c342378 MK |
97 | UINT32 DigestSize;\r |
98 | EFI_HMAC_NEW HmacNew;\r | |
99 | EFI_HMAC_INIT HmacInit;\r | |
100 | EFI_HMAC_UPDATE HmacUpdate;\r | |
101 | EFI_HMAC_FINAL HmacFinal;\r | |
102 | CONST UINT8 *Key;\r | |
103 | UINTN KeySize;\r | |
104 | CONST UINT8 *Digest;\r | |
105 | VOID *HmacCtx;\r | |
694bfd6f MC |
106 | } HMAC_TEST_CONTEXT;\r |
107 | \r | |
108 | // These functions have been deprecated but they've been left commented out for future reference\r | |
7c342378 MK |
109 | // HMAC_TEST_CONTEXT mHmacMd5TestCtx = {MD5_DIGEST_SIZE, HmacMd5New, HmacMd5SetKey, HmacMd5Update, HmacMd5Final, HmacMd5Key, sizeof(HmacMd5Key), HmacMd5Digest};\r |
110 | // HMAC_TEST_CONTEXT mHmacSha1TestCtx = {SHA1_DIGEST_SIZE, HmacSha1New, HmacSha1SetKey, HmacSha1Update, HmacSha1Final, HmacSha1Key, sizeof(HmacSha1Key), HmacSha1Digest};\r | |
111 | HMAC_TEST_CONTEXT mHmacSha256TestCtx = { SHA256_DIGEST_SIZE, HmacSha256New, HmacSha256SetKey, HmacSha256Update, HmacSha256Final, HmacSha256Key, sizeof (HmacSha256Key), HmacSha256Digest };\r | |
694bfd6f MC |
112 | \r |
113 | UNIT_TEST_STATUS\r | |
114 | EFIAPI\r | |
115 | TestVerifyHmacPreReq (\r | |
7c342378 | 116 | UNIT_TEST_CONTEXT Context\r |
694bfd6f MC |
117 | )\r |
118 | {\r | |
7c342378 | 119 | HMAC_TEST_CONTEXT *HmacTestContext;\r |
694bfd6f | 120 | \r |
7c342378 MK |
121 | HmacTestContext = Context;\r |
122 | HmacTestContext->HmacCtx = HmacTestContext->HmacNew ();\r | |
694bfd6f MC |
123 | if (HmacTestContext->HmacCtx == NULL) {\r |
124 | return UNIT_TEST_ERROR_TEST_FAILED;\r | |
125 | }\r | |
126 | \r | |
127 | return UNIT_TEST_PASSED;\r | |
128 | }\r | |
129 | \r | |
130 | VOID\r | |
131 | EFIAPI\r | |
132 | TestVerifyHmacCleanUp (\r | |
7c342378 | 133 | UNIT_TEST_CONTEXT Context\r |
694bfd6f MC |
134 | )\r |
135 | {\r | |
7c342378 | 136 | HMAC_TEST_CONTEXT *HmacTestContext;\r |
694bfd6f MC |
137 | \r |
138 | HmacTestContext = Context;\r | |
139 | if (HmacTestContext->HmacCtx != NULL) {\r | |
140 | FreePool (HmacTestContext->HmacCtx);\r | |
141 | }\r | |
142 | }\r | |
143 | \r | |
144 | UNIT_TEST_STATUS\r | |
145 | EFIAPI\r | |
146 | TestVerifyHmac (\r | |
7c342378 | 147 | IN UNIT_TEST_CONTEXT Context\r |
694bfd6f MC |
148 | )\r |
149 | {\r | |
7c342378 MK |
150 | UINT8 Digest[MAX_DIGEST_SIZE];\r |
151 | BOOLEAN Status;\r | |
152 | HMAC_TEST_CONTEXT *HmacTestContext;\r | |
694bfd6f MC |
153 | \r |
154 | HmacTestContext = Context;\r | |
155 | \r | |
156 | ZeroMem (Digest, MAX_DIGEST_SIZE);\r | |
157 | \r | |
7c342378 | 158 | Status = HmacTestContext->HmacInit (HmacTestContext->HmacCtx, HmacTestContext->Key, HmacTestContext->KeySize);\r |
694bfd6f MC |
159 | UT_ASSERT_TRUE (Status);\r |
160 | \r | |
7c342378 | 161 | Status = HmacTestContext->HmacUpdate (HmacTestContext->HmacCtx, HmacData, 8);\r |
694bfd6f MC |
162 | UT_ASSERT_TRUE (Status);\r |
163 | \r | |
7c342378 | 164 | Status = HmacTestContext->HmacFinal (HmacTestContext->HmacCtx, Digest);\r |
694bfd6f MC |
165 | UT_ASSERT_TRUE (Status);\r |
166 | \r | |
167 | UT_ASSERT_MEM_EQUAL (Digest, HmacTestContext->Digest, HmacTestContext->DigestSize);\r | |
168 | \r | |
169 | return UNIT_TEST_PASSED;\r | |
170 | }\r | |
171 | \r | |
7c342378 MK |
172 | TEST_DESC mHmacTest[] = {\r |
173 | //\r | |
174 | // -----Description---------------------Class---------------------Function---------------Pre------------------Post------------Context\r | |
175 | //\r | |
176 | { "TestVerifyHmacSha256()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha256TestCtx },\r | |
177 | // These functions have been deprecated but they've been left commented out for future reference\r | |
178 | // {"TestVerifyHmacMd5()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacMd5TestCtx},\r | |
179 | // {"TestVerifyHmacSha1()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha1TestCtx},\r | |
694bfd6f MC |
180 | };\r |
181 | \r | |
7c342378 | 182 | UINTN mHmacTestNum = ARRAY_SIZE (mHmacTest);\r |