TianoCore EDK2 master
Loading...
Searching...
No Matches
HmacTests.c
Go to the documentation of this file.
1
9#include "TestBaseCryptLib.h"
10
11//
12// Max Known Digest Size is SHA512 Output (64 bytes) by far
13//
14#define MAX_DIGEST_SIZE 64
15
16//
17// Data string for HMAC validation
18//
19GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *HmacData = "Hi There";
20
21//
22// Key value for HMAC-MD5 validation. (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)
23//
24GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Key[16] = {
25 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
26};
27
28//
29// Result for HMAC-MD5("Hi There"). (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)
30//
31GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Digest[] = {
32 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d
33};
34
35//
36// Key value for HMAC-SHA-1 validation. (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)
37//
38GLOBAL_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
41};
42
43//
44// Result for HMAC-SHA-1 ("Hi There"). (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)
45//
46GLOBAL_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
49};
50
51//
52// Key value for HMAC-SHA-256 validation. (From "4. Test Vectors" of IETF RFC4231)
53//
54GLOBAL_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
57};
58
59//
60// Result for HMAC-SHA-256 ("Hi There"). (From "4. Test Vectors" of IETF RFC4231)
61//
62GLOBAL_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
65};
66
67//
68// Key value for HMAC-SHA-384 validation. (From "4. Test Vectors" of IETF RFC4231)
69//
70GLOBAL_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
73};
74
75//
76// Result for HMAC-SHA-384 ("Hi There"). (From "4. Test Vectors" of IETF RFC4231)
77//
78GLOBAL_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
82};
83
84typedef
85 VOID *
86(EFIAPI *EFI_HMAC_NEW)(
87 VOID
88 );
89
90typedef
91 VOID
92(EFIAPI *EFI_HMAC_FREE)(
93 IN VOID *HashContext
94 );
95
96typedef
97 BOOLEAN
98(EFIAPI *EFI_HMAC_INIT)(
99 IN OUT VOID *HashContext,
100 IN CONST UINT8 *Key,
101 IN UINTN KeySize
102 );
103
104typedef
105 BOOLEAN
106(EFIAPI *EFI_HMAC_DUP)(
107 IN CONST VOID *HashContext,
108 OUT VOID *NewHashContext
109 );
110
111typedef
112 BOOLEAN
113(EFIAPI *EFI_HMAC_UPDATE)(
114 IN OUT VOID *HashContext,
115 IN CONST VOID *Data,
116 IN UINTN DataSize
117 );
118
119typedef
120 BOOLEAN
121(EFIAPI *EFI_HMAC_FINAL)(
122 IN OUT VOID *HashContext,
123 OUT UINT8 *HashValue
124 );
125
126typedef
127 BOOLEAN
128(EFIAPI *EFI_HMAC_ALL)(
129 IN CONST VOID *Data,
130 IN UINTN DataSize,
131 IN CONST UINT8 *Key,
132 IN UINTN KeySize,
133 OUT UINT8 *HashValue
134 );
135
136typedef struct {
137 UINT32 DigestSize;
138 EFI_HMAC_NEW HmacNew;
139 EFI_HMAC_FREE HmacFree;
140 EFI_HMAC_INIT HmacInit;
141 EFI_HMAC_DUP HmacDup;
142 EFI_HMAC_UPDATE HmacUpdate;
143 EFI_HMAC_FINAL HmacFinal;
144 EFI_HMAC_ALL HmacAll;
145 CONST UINT8 *Key;
146 UINTN KeySize;
147 CONST UINT8 *Digest;
148 VOID *HmacCtx;
150
151HMAC_TEST_CONTEXT mHmacSha256TestCtx = { SHA256_DIGEST_SIZE, HmacSha256New, HmacSha256Free, HmacSha256SetKey, HmacSha256Duplicate, HmacSha256Update, HmacSha256Final, HmacSha256All, HmacSha256Key, sizeof (HmacSha256Key), HmacSha256Digest };
152HMAC_TEST_CONTEXT mHmacSha384TestCtx = { SHA384_DIGEST_SIZE, HmacSha384New, HmacSha384Free, HmacSha384SetKey, HmacSha384Duplicate, HmacSha384Update, HmacSha384Final, HmacSha384All, HmacSha384Key, sizeof (HmacSha384Key), HmacSha384Digest };
153
155EFIAPI
156TestVerifyHmacPreReq (
157 UNIT_TEST_CONTEXT Context
158 )
159{
160 HMAC_TEST_CONTEXT *HmacTestContext;
161
162 HmacTestContext = Context;
163 HmacTestContext->HmacCtx = HmacTestContext->HmacNew ();
164 if (HmacTestContext->HmacCtx == NULL) {
165 return UNIT_TEST_ERROR_TEST_FAILED;
166 }
167
168 return UNIT_TEST_PASSED;
169}
170
171VOID
172EFIAPI
173TestVerifyHmacCleanUp (
174 UNIT_TEST_CONTEXT Context
175 )
176{
177 HMAC_TEST_CONTEXT *HmacTestContext;
178
179 HmacTestContext = Context;
180 if (HmacTestContext->HmacCtx != NULL) {
181 HmacTestContext->HmacFree (HmacTestContext->HmacCtx);
182 }
183}
184
186EFIAPI
187TestVerifyHmac (
188 IN UNIT_TEST_CONTEXT Context
189 )
190{
191 UINT8 Digest[MAX_DIGEST_SIZE];
192 UINT8 DigestCopy[MAX_DIGEST_SIZE];
193 UINT8 DigestByAll[MAX_DIGEST_SIZE];
194 VOID *HmacCopyContext;
195 BOOLEAN Status;
196 HMAC_TEST_CONTEXT *HmacTestContext;
197
198 HmacTestContext = Context;
199
200 ZeroMem (Digest, MAX_DIGEST_SIZE);
201 ZeroMem (DigestCopy, MAX_DIGEST_SIZE);
202 ZeroMem (DigestByAll, MAX_DIGEST_SIZE);
203
204 HmacCopyContext = HmacTestContext->HmacNew ();
205
206 Status = HmacTestContext->HmacInit (HmacTestContext->HmacCtx, HmacTestContext->Key, HmacTestContext->KeySize);
207 UT_ASSERT_TRUE (Status);
208
209 Status = HmacTestContext->HmacInit (HmacCopyContext, HmacTestContext->Key, HmacTestContext->KeySize);
210 UT_ASSERT_TRUE (Status);
211
212 Status = HmacTestContext->HmacUpdate (HmacTestContext->HmacCtx, HmacData, 8);
213 UT_ASSERT_TRUE (Status);
214
215 Status = HmacTestContext->HmacDup (HmacTestContext->HmacCtx, HmacCopyContext);
216 UT_ASSERT_TRUE (Status);
217
218 Status = HmacTestContext->HmacFinal (HmacTestContext->HmacCtx, Digest);
219 UT_ASSERT_TRUE (Status);
220
221 Status = HmacTestContext->HmacFinal (HmacCopyContext, DigestCopy);
222 UT_ASSERT_TRUE (Status);
223
224 Status = HmacTestContext->HmacAll (HmacData, 8, HmacTestContext->Key, HmacTestContext->KeySize, DigestByAll);
225 UT_ASSERT_TRUE (Status);
226
227 UT_ASSERT_MEM_EQUAL (Digest, HmacTestContext->Digest, HmacTestContext->DigestSize);
228 UT_ASSERT_MEM_EQUAL (Digest, DigestCopy, HmacTestContext->DigestSize);
229 UT_ASSERT_MEM_EQUAL (Digest, DigestByAll, HmacTestContext->DigestSize);
230
231 return UNIT_TEST_PASSED;
232}
233
234TEST_DESC mHmacTest[] = {
235 //
236 // -----Description---------------------Class---------------------Function---------------Pre------------------Post------------Context
237 //
238 { "TestVerifyHmacSha256()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha256TestCtx },
239 { "TestVerifyHmacSha384()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha384TestCtx },
240};
241
242UINTN mHmacTestNum = ARRAY_SIZE (mHmacTest);
UINT64 UINTN
BOOLEAN EFIAPI HmacSha384Update(IN OUT VOID *HmacSha384Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptHmac.c:564
BOOLEAN EFIAPI HmacSha384SetKey(OUT VOID *HmacSha384Context, IN CONST UINT8 *Key, IN UINTN KeySize)
Definition: CryptHmac.c:508
VOID *EFIAPI HmacSha256New(VOID)
Definition: CryptHmac.c:297
BOOLEAN EFIAPI HmacSha256Duplicate(IN CONST VOID *HmacSha256Context, OUT VOID *NewHmacSha256Context)
Definition: CryptHmac.c:359
VOID *EFIAPI HmacSha384New(VOID)
Definition: CryptHmac.c:468
BOOLEAN EFIAPI HmacSha384All(IN CONST VOID *Data, IN UINTN DataSize, IN CONST UINT8 *Key, IN UINTN KeySize, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:627
VOID EFIAPI HmacSha384Free(IN VOID *HmacSha384Ctx)
Definition: CryptHmac.c:483
VOID EFIAPI HmacSha256Free(IN VOID *HmacSha256Ctx)
Definition: CryptHmac.c:312
BOOLEAN EFIAPI HmacSha384Duplicate(IN CONST VOID *HmacSha384Context, OUT VOID *NewHmacSha384Context)
Definition: CryptHmac.c:534
#define SHA256_DIGEST_SIZE
Definition: BaseCryptLib.h:44
BOOLEAN EFIAPI HmacSha384Final(IN OUT VOID *HmacSha384Context, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:597
BOOLEAN EFIAPI HmacSha256SetKey(OUT VOID *HmacSha256Context, IN CONST UINT8 *Key, IN UINTN KeySize)
Definition: CryptHmac.c:335
BOOLEAN EFIAPI HmacSha256Update(IN OUT VOID *HmacSha256Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptHmac.c:387
BOOLEAN EFIAPI HmacSha256All(IN CONST VOID *Data, IN UINTN DataSize, IN CONST UINT8 *Key, IN UINTN KeySize, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:448
BOOLEAN EFIAPI HmacSha256Final(IN OUT VOID *HmacSha256Context, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:418
#define SHA384_DIGEST_SIZE
Definition: BaseCryptLib.h:49
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_ASSERT_MEM_EQUAL(BufferA, BufferB, Length)
Definition: UnitTestLib.h:389
#define UT_ASSERT_TRUE(Expression)
Definition: UnitTestLib.h:350
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16