2 Application for HMAC 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 // Data string for HMAC validation
19 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*HmacData
= "Hi There";
22 // Key value for HMAC-MD5 validation. (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)
24 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Key
[16] = {
25 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
29 // Result for HMAC-MD5("Hi There"). (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)
31 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Digest
[] = {
32 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d
36 // Key value for HMAC-SHA-1 validation. (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)
38 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha1Key
[20] = {
39 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
40 0x0b, 0x0b, 0x0b, 0x0b
44 // Result for HMAC-SHA-1 ("Hi There"). (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)
46 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha1Digest
[] = {
47 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
48 0xf1, 0x46, 0xbe, 0x00
52 // Key value for HMAC-SHA-256 validation. (From "4. Test Vectors" of IETF RFC4231)
54 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Key
[20] = {
55 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
56 0x0b, 0x0b, 0x0b, 0x0b
60 // Result for HMAC-SHA-256 ("Hi There"). (From "4. Test Vectors" of IETF RFC4231)
62 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Digest
[] = {
63 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
64 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
69 (EFIAPI
*EFI_HMAC_NEW
) (
75 (EFIAPI
*EFI_HMAC_INIT
) (
76 IN OUT VOID
*HashContext
,
83 (EFIAPI
*EFI_HMAC_UPDATE
) (
84 IN OUT VOID
*HashContext
,
91 (EFIAPI
*EFI_HMAC_FINAL
) (
92 IN OUT VOID
*HashContext
,
99 EFI_HMAC_INIT HmacInit
;
100 EFI_HMAC_UPDATE HmacUpdate
;
101 EFI_HMAC_FINAL HmacFinal
;
108 // These functions have been deprecated but they've been left commented out for future reference
109 //HMAC_TEST_CONTEXT mHmacMd5TestCtx = {MD5_DIGEST_SIZE, HmacMd5New, HmacMd5SetKey, HmacMd5Update, HmacMd5Final, HmacMd5Key, sizeof(HmacMd5Key), HmacMd5Digest};
110 //HMAC_TEST_CONTEXT mHmacSha1TestCtx = {SHA1_DIGEST_SIZE, HmacSha1New, HmacSha1SetKey, HmacSha1Update, HmacSha1Final, HmacSha1Key, sizeof(HmacSha1Key), HmacSha1Digest};
111 HMAC_TEST_CONTEXT mHmacSha256TestCtx
= {SHA256_DIGEST_SIZE
, HmacSha256New
, HmacSha256SetKey
, HmacSha256Update
, HmacSha256Final
, HmacSha256Key
, sizeof(HmacSha256Key
), HmacSha256Digest
};
116 TestVerifyHmacPreReq (
117 UNIT_TEST_CONTEXT Context
120 HMAC_TEST_CONTEXT
*HmacTestContext
;
122 HmacTestContext
= Context
;
123 HmacTestContext
->HmacCtx
= HmacTestContext
->HmacNew();
124 if (HmacTestContext
->HmacCtx
== NULL
) {
125 return UNIT_TEST_ERROR_TEST_FAILED
;
128 return UNIT_TEST_PASSED
;
133 TestVerifyHmacCleanUp (
134 UNIT_TEST_CONTEXT Context
137 HMAC_TEST_CONTEXT
*HmacTestContext
;
139 HmacTestContext
= Context
;
140 if (HmacTestContext
->HmacCtx
!= NULL
) {
141 FreePool (HmacTestContext
->HmacCtx
);
148 IN UNIT_TEST_CONTEXT Context
151 UINT8 Digest
[MAX_DIGEST_SIZE
];
153 HMAC_TEST_CONTEXT
*HmacTestContext
;
155 HmacTestContext
= Context
;
157 ZeroMem (Digest
, MAX_DIGEST_SIZE
);
159 Status
= HmacTestContext
->HmacInit (HmacTestContext
->HmacCtx
, HmacTestContext
->Key
, HmacTestContext
->KeySize
);
160 UT_ASSERT_TRUE (Status
);
162 Status
= HmacTestContext
->HmacUpdate (HmacTestContext
->HmacCtx
, HmacData
, 8);
163 UT_ASSERT_TRUE (Status
);
165 Status
= HmacTestContext
->HmacFinal (HmacTestContext
->HmacCtx
, Digest
);
166 UT_ASSERT_TRUE (Status
);
168 UT_ASSERT_MEM_EQUAL (Digest
, HmacTestContext
->Digest
, HmacTestContext
->DigestSize
);
170 return UNIT_TEST_PASSED
;
173 TEST_DESC mHmacTest
[] = {
175 // -----Description---------------------Class---------------------Function---------------Pre------------------Post------------Context
177 {"TestVerifyHmacSha256()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac
, TestVerifyHmacPreReq
, TestVerifyHmacCleanUp
, &mHmacSha256TestCtx
},
178 // These functions have been deprecated but they've been left commented out for future reference
179 //{"TestVerifyHmacMd5()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacMd5TestCtx},
180 //{"TestVerifyHmacSha1()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha1TestCtx},
184 UINTN mHmacTestNum
= ARRAY_SIZE(mHmacTest
);