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
68 // Key value for HMAC-SHA-384 validation. (From "4. Test Vectors" of IETF RFC4231)
70 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha384Key
[20] = {
71 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
72 0x0b, 0x0b, 0x0b, 0x0b
76 // Result for HMAC-SHA-384 ("Hi There"). (From "4. Test Vectors" of IETF RFC4231)
78 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha384Digest
[] = {
79 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62, 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
80 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6, 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
81 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f, 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
86 (EFIAPI
*EFI_HMAC_NEW
)(
92 (EFIAPI
*EFI_HMAC_FREE
)(
98 (EFIAPI
*EFI_HMAC_INIT
)(
99 IN OUT VOID
*HashContext
,
106 (EFIAPI
*EFI_HMAC_UPDATE
)(
107 IN OUT VOID
*HashContext
,
114 (EFIAPI
*EFI_HMAC_FINAL
)(
115 IN OUT VOID
*HashContext
,
121 EFI_HMAC_NEW HmacNew
;
122 EFI_HMAC_FREE HmacFree
;
123 EFI_HMAC_INIT HmacInit
;
124 EFI_HMAC_UPDATE HmacUpdate
;
125 EFI_HMAC_FINAL HmacFinal
;
132 // These functions have been deprecated but they've been left commented out for future reference
133 // HMAC_TEST_CONTEXT mHmacMd5TestCtx = {MD5_DIGEST_SIZE, HmacMd5New, HmacMd5Free, HmacMd5SetKey, HmacMd5Update, HmacMd5Final, HmacMd5Key, sizeof(HmacMd5Key), HmacMd5Digest};
134 // HMAC_TEST_CONTEXT mHmacSha1TestCtx = {SHA1_DIGEST_SIZE, HmacSha1New, HmacSha1Free, HmacSha1SetKey, HmacSha1Update, HmacSha1Final, HmacSha1Key, sizeof(HmacSha1Key), HmacSha1Digest};
135 HMAC_TEST_CONTEXT mHmacSha256TestCtx
= { SHA256_DIGEST_SIZE
, HmacSha256New
, HmacSha256Free
, HmacSha256SetKey
, HmacSha256Update
, HmacSha256Final
, HmacSha256Key
, sizeof (HmacSha256Key
), HmacSha256Digest
};
136 HMAC_TEST_CONTEXT mHmacSha384TestCtx
= { SHA384_DIGEST_SIZE
, HmacSha384New
, HmacSha384Free
, HmacSha384SetKey
, HmacSha384Update
, HmacSha384Final
, HmacSha384Key
, sizeof (HmacSha384Key
), HmacSha384Digest
};
140 TestVerifyHmacPreReq (
141 UNIT_TEST_CONTEXT Context
144 HMAC_TEST_CONTEXT
*HmacTestContext
;
146 HmacTestContext
= Context
;
147 HmacTestContext
->HmacCtx
= HmacTestContext
->HmacNew ();
148 if (HmacTestContext
->HmacCtx
== NULL
) {
149 return UNIT_TEST_ERROR_TEST_FAILED
;
152 return UNIT_TEST_PASSED
;
157 TestVerifyHmacCleanUp (
158 UNIT_TEST_CONTEXT Context
161 HMAC_TEST_CONTEXT
*HmacTestContext
;
163 HmacTestContext
= Context
;
164 if (HmacTestContext
->HmacCtx
!= NULL
) {
165 HmacTestContext
->HmacFree (HmacTestContext
->HmacCtx
);
172 IN UNIT_TEST_CONTEXT Context
175 UINT8 Digest
[MAX_DIGEST_SIZE
];
177 HMAC_TEST_CONTEXT
*HmacTestContext
;
179 HmacTestContext
= Context
;
181 ZeroMem (Digest
, MAX_DIGEST_SIZE
);
183 Status
= HmacTestContext
->HmacInit (HmacTestContext
->HmacCtx
, HmacTestContext
->Key
, HmacTestContext
->KeySize
);
184 UT_ASSERT_TRUE (Status
);
186 Status
= HmacTestContext
->HmacUpdate (HmacTestContext
->HmacCtx
, HmacData
, 8);
187 UT_ASSERT_TRUE (Status
);
189 Status
= HmacTestContext
->HmacFinal (HmacTestContext
->HmacCtx
, Digest
);
190 UT_ASSERT_TRUE (Status
);
192 UT_ASSERT_MEM_EQUAL (Digest
, HmacTestContext
->Digest
, HmacTestContext
->DigestSize
);
194 return UNIT_TEST_PASSED
;
197 TEST_DESC mHmacTest
[] = {
199 // -----Description---------------------Class---------------------Function---------------Pre------------------Post------------Context
201 { "TestVerifyHmacSha256()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac
, TestVerifyHmacPreReq
, TestVerifyHmacCleanUp
, &mHmacSha256TestCtx
},
202 { "TestVerifyHmacSha384()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac
, TestVerifyHmacPreReq
, TestVerifyHmacCleanUp
, &mHmacSha384TestCtx
},
203 // These functions have been deprecated but they've been left commented out for future reference
204 // {"TestVerifyHmacMd5()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacMd5TestCtx},
205 // {"TestVerifyHmacSha1()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha1TestCtx},
208 UINTN mHmacTestNum
= ARRAY_SIZE (mHmacTest
);