TianoCore EDK2 master
Loading...
Searching...
No Matches
HashTests.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// Message string for digest validation
18//
19GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *HashData = "abc";
20
21//
22// Result for MD5("abc"). (From "A.5 Test suite" of IETF RFC1321)
23//
24#ifdef ENABLE_MD5_DEPRECATED_INTERFACES
26 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72
27};
28#endif
29
30//
31// Result for SHA-1("abc"). (From "A.1 SHA-1 Example" of NIST FIPS 180-2)
32//
34 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
35 0x9c, 0xd0, 0xd8, 0x9d
36};
37
38//
39// Result for SHA-256("abc"). (From "B.1 SHA-256 Example" of NIST FIPS 180-2)
40//
42 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
43 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
44};
45
46//
47// Result for SHA-384("abc"). (From "D.1 SHA-384 Example" of NIST FIPS 180-2)
48//
50 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
51 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
52 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
53};
54
55//
56// Result for SHA-512("abc"). (From "C.1 SHA-512 Example" of NIST FIPS 180-2)
57//
59 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
60 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
61 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
62 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
63};
64
65//
66// Result for SM3("abc"). (From "A.1 Example 1" of
67// http://www.gmbz.org.cn/upload/2018-07-24/1532401392982079739.pdf)
68//
70 0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9, 0xd1, 0xf2, 0xd4, 0x6b, 0xdc, 0x10, 0xe4, 0xe2,
71 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2, 0xf7, 0xa2, 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0
72};
73
74typedef
76(EFIAPI *EFI_HASH_GET_CONTEXT_SIZE)(
77 VOID
78 );
79
80typedef
81BOOLEAN
82(EFIAPI *EFI_HASH_INIT)(
83 OUT VOID *HashContext
84 );
85
86typedef
87BOOLEAN
88(EFIAPI *EFI_HASH_UPDATE)(
89 IN OUT VOID *HashContext,
90 IN CONST VOID *Data,
91 IN UINTN DataSize
92 );
93
94typedef
95BOOLEAN
96(EFIAPI *EFI_HASH_DUP)(
97 IN CONST VOID *HashContext,
98 OUT VOID *NewHashContext
99 );
100
101typedef
102BOOLEAN
103(EFIAPI *EFI_HASH_FINAL)(
104 IN OUT VOID *HashContext,
105 OUT UINT8 *HashValue
106 );
107
108typedef
109BOOLEAN
110(EFIAPI *EFI_HASH_ALL)(
111 IN CONST VOID *Data,
112 IN UINTN DataSize,
113 OUT UINT8 *HashValue
114 );
115
116typedef struct {
117 UINT32 DigestSize;
118 EFI_HASH_GET_CONTEXT_SIZE GetContextSize;
119 EFI_HASH_INIT HashInit;
120 EFI_HASH_UPDATE HashUpdate;
121 EFI_HASH_DUP HashDup;
122 EFI_HASH_FINAL HashFinal;
123 EFI_HASH_ALL HashAll;
124 CONST UINT8 *Digest;
125 VOID *HashCtx;
127
128#ifdef ENABLE_MD5_DEPRECATED_INTERFACES
130#endif
136
138EFIAPI
139TestVerifyHashPreReq (
140 UNIT_TEST_CONTEXT Context
141 )
142{
143 HASH_TEST_CONTEXT *HashTestContext;
144 UINTN CtxSize;
145
146 HashTestContext = Context;
147 CtxSize = HashTestContext->GetContextSize ();
148 HashTestContext->HashCtx = AllocatePool (CtxSize);
149 if (HashTestContext->HashCtx == NULL) {
150 return UNIT_TEST_ERROR_TEST_FAILED;
151 }
152
153 return UNIT_TEST_PASSED;
154}
155
156VOID
157EFIAPI
158TestVerifyHashCleanUp (
159 UNIT_TEST_CONTEXT Context
160 )
161{
162 HASH_TEST_CONTEXT *HashTestContext;
163
164 HashTestContext = Context;
165 if (HashTestContext->HashCtx != NULL) {
166 FreePool (HashTestContext->HashCtx);
167 }
168}
169
171EFIAPI
172TestVerifyHash (
173 IN UNIT_TEST_CONTEXT Context
174 )
175{
176 UINTN DataSize;
177 UINT8 Digest[MAX_DIGEST_SIZE];
178 UINT8 DigestCopy[MAX_DIGEST_SIZE];
179 BOOLEAN Status;
180 HASH_TEST_CONTEXT *HashTestContext;
181 VOID *HashCopyContext;
182
183 HashTestContext = Context;
184
185 DataSize = AsciiStrLen (HashData);
186
187 ZeroMem (Digest, MAX_DIGEST_SIZE);
188 ZeroMem (DigestCopy, MAX_DIGEST_SIZE);
189
190 HashCopyContext = AllocatePool (HashTestContext->GetContextSize ());
191
192 Status = HashTestContext->HashInit (HashTestContext->HashCtx);
193 UT_ASSERT_TRUE (Status);
194
195 Status = HashTestContext->HashInit (HashCopyContext);
196 UT_ASSERT_TRUE (Status);
197
198 Status = HashTestContext->HashUpdate (HashTestContext->HashCtx, HashData, DataSize);
199 UT_ASSERT_TRUE (Status);
200
201 Status = HashTestContext->HashDup (HashTestContext->HashCtx, HashCopyContext);
202 UT_ASSERT_TRUE (Status);
203
204 Status = HashTestContext->HashFinal (HashTestContext->HashCtx, Digest);
205 UT_ASSERT_TRUE (Status);
206
207 Status = HashTestContext->HashFinal (HashCopyContext, DigestCopy);
208 UT_ASSERT_TRUE (Status);
209
210 UT_ASSERT_MEM_EQUAL (Digest, HashTestContext->Digest, HashTestContext->DigestSize);
211 UT_ASSERT_MEM_EQUAL (Digest, DigestCopy, HashTestContext->DigestSize);
212
213 ZeroMem (Digest, MAX_DIGEST_SIZE);
214 Status = HashTestContext->HashAll (HashData, DataSize, Digest);
215 UT_ASSERT_TRUE (Status);
216
217 UT_ASSERT_MEM_EQUAL (Digest, HashTestContext->Digest, HashTestContext->DigestSize);
218
219 return UNIT_TEST_PASSED;
220}
221
222TEST_DESC mHashTest[] = {
223 //
224 // -----Description----------------Class---------------------Function---------------Pre------------------Post------------Context
225 //
226 #ifdef ENABLE_MD5_DEPRECATED_INTERFACES
227 { "TestVerifyMd5()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mMd5TestCtx },
228 #endif
229 { "TestVerifySha1()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mSha1TestCtx },
230 { "TestVerifySha256()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mSha256TestCtx },
231 { "TestVerifySha384()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mSha384TestCtx },
232 { "TestVerifySha512()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mSha512TestCtx },
233 { "TestVerifySm3()", "CryptoPkg.BaseCryptLib.Hash", TestVerifyHash, TestVerifyHashPreReq, TestVerifyHashCleanUp, &mSm3TestCtx },
234};
235
236UINTN mHashTestNum = ARRAY_SIZE (mHashTest);
UINT64 UINTN
UINTN EFIAPI Sha256GetContextSize(VOID)
Definition: CryptSha256.c:20
UINTN EFIAPI Sha384GetContextSize(VOID)
Definition: CryptSha512.c:20
BOOLEAN EFIAPI Sha512HashAll(IN CONST VOID *Data, IN UINTN DataSize, OUT UINT8 *HashValue)
Definition: CryptSha512.c:427
BOOLEAN EFIAPI Sm3Duplicate(IN CONST VOID *Sm3Context, OUT VOID *NewSm3Context)
Definition: CryptSm3.c:79
BOOLEAN EFIAPI Sha512Final(IN OUT VOID *Sha512Context, OUT UINT8 *HashValue)
Definition: CryptSha512.c:389
BOOLEAN EFIAPI Sha384Duplicate(IN CONST VOID *Sha384Context, OUT VOID *NewSha384Context)
Definition: CryptSha512.c:78
UINTN EFIAPI Sm3GetContextSize(VOID)
Definition: CryptSm3.c:20
#define SHA1_DIGEST_SIZE
Definition: BaseCryptLib.h:39
UINTN EFIAPI Sha512GetContextSize(VOID)
Definition: CryptSha512.c:246
BOOLEAN EFIAPI Sha1Final(IN OUT VOID *Sha1Context, OUT UINT8 *HashValue)
Definition: CryptSha1.c:163
BOOLEAN EFIAPI Sm3Update(IN OUT VOID *Sm3Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptSm3.c:116
BOOLEAN EFIAPI Sha512Init(OUT VOID *Sha512Context)
Definition: CryptSha512.c:270
#define SM3_256_DIGEST_SIZE
Definition: BaseCryptLib.h:59
BOOLEAN EFIAPI Sm3Final(IN OUT VOID *Sm3Context, OUT UINT8 *HashValue)
Definition: CryptSm3.c:166
#define SHA512_DIGEST_SIZE
Definition: BaseCryptLib.h:54
BOOLEAN EFIAPI Sha256Init(OUT VOID *Sha256Context)
Definition: CryptSha256.c:44
BOOLEAN EFIAPI Sha1Duplicate(IN CONST VOID *Sha1Context, OUT VOID *NewSha1Context)
Definition: CryptSha1.c:78
BOOLEAN EFIAPI Sha384HashAll(IN CONST VOID *Data, IN UINTN DataSize, OUT UINT8 *HashValue)
Definition: CryptSha512.c:201
BOOLEAN EFIAPI Sha256Final(IN OUT VOID *Sha256Context, OUT UINT8 *HashValue)
Definition: CryptSha256.c:161
#define SHA256_DIGEST_SIZE
Definition: BaseCryptLib.h:44
BOOLEAN EFIAPI Sm3HashAll(IN CONST VOID *Data, IN UINTN DataSize, OUT UINT8 *HashValue)
Definition: CryptSm3.c:206
BOOLEAN EFIAPI Sha384Update(IN OUT VOID *Sha384Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptSha512.c:115
BOOLEAN EFIAPI Sha256Duplicate(IN CONST VOID *Sha256Context, OUT VOID *NewSha256Context)
Definition: CryptSha256.c:76
BOOLEAN EFIAPI Sm3Init(OUT VOID *Sm3Context)
Definition: CryptSm3.c:44
BOOLEAN EFIAPI Sha1Init(OUT VOID *Sha1Context)
Definition: CryptSha1.c:46
BOOLEAN EFIAPI Sha256HashAll(IN CONST VOID *Data, IN UINTN DataSize, OUT UINT8 *HashValue)
Definition: CryptSha256.c:199
BOOLEAN EFIAPI Sha512Duplicate(IN CONST VOID *Sha512Context, OUT VOID *NewSha512Context)
Definition: CryptSha512.c:304
BOOLEAN EFIAPI Sha256Update(IN OUT VOID *Sha256Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptSha256.c:113
BOOLEAN EFIAPI Sha1HashAll(IN CONST VOID *Data, IN UINTN DataSize, OUT UINT8 *HashValue)
Definition: CryptSha1.c:201
UINTN EFIAPI Sha1GetContextSize(VOID)
Definition: CryptSha1.c:22
BOOLEAN EFIAPI Sha384Final(IN OUT VOID *Sha384Context, OUT UINT8 *HashValue)
Definition: CryptSha512.c:163
#define MD5_DIGEST_SIZE
Definition: BaseCryptLib.h:34
BOOLEAN EFIAPI Sha384Init(OUT VOID *Sha384Context)
Definition: CryptSha512.c:44
BOOLEAN EFIAPI Sha512Update(IN OUT VOID *Sha512Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptSha512.c:341
BOOLEAN EFIAPI Sha1Update(IN OUT VOID *Sha1Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptSha1.c:115
#define SHA384_DIGEST_SIZE
Definition: BaseCryptLib.h:49
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID EFIAPI FreePool(IN VOID *Buffer)
BOOLEAN EFIAPI Md5Duplicate(IN CONST VOID *Md5Context, OUT VOID *NewMd5Context)
Definition: CryptMd5Null.c:65
BOOLEAN EFIAPI Md5Final(IN OUT VOID *Md5Context, OUT UINT8 *HashValue)
Definition: CryptMd5Null.c:126
BOOLEAN EFIAPI Md5Update(IN OUT VOID *Md5Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptMd5Null.c:94
BOOLEAN EFIAPI Md5Init(OUT VOID *Md5Context)
Definition: CryptMd5Null.c:42
BOOLEAN EFIAPI Md5HashAll(IN CONST VOID *Data, IN UINTN DataSize, OUT UINT8 *HashValue)
Definition: CryptMd5Null.c:155
UINTN EFIAPI Md5GetContextSize(VOID)
Definition: CryptMd5Null.c:20
EFI_STATUS EFIAPI HashUpdate(IN HASH_HANDLE HashHandle, IN VOID *DataToHash, IN UINTN DataToHashLen)
Definition: HashLibTdx.c:69
#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 *EFIAPI AllocatePool(IN UINTN AllocationSize)
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