ReactOS 0.4.16-dev-340-g0540c21
Hash.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API tests
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Tests for basic hash APIs
5 * COPYRIGHT: Copyright 2023 Ratin Gao <ratin@knsoft.org>
6 */
7
8#include "precomp.h"
9
10typedef struct _A_SHA_CTX
11{
16
17#define A_SHA_DIGEST_LEN 20
18
19typedef struct _MD5_CTX
20{
26
27#define MD5_DIGEST_LEN 16
28
29typedef struct _MD4_CTX
30{
36
37#define MD4_DIGEST_LEN 16
38
39#ifndef RSA32API
40#define RSA32API __stdcall
41#endif
42
43typedef
44VOID
48
49typedef
50VOID
56
57typedef
58VOID
63
64typedef
65VOID
69
70typedef
71VOID
77
78typedef
79VOID
83
84typedef
85VOID
89
90typedef
91VOID
97
98typedef
99VOID
103
105static ANSI_STRING g_TestString = RTL_CONSTANT_STRING("ReactOS Hash API Test String");
106
107static ULONG g_ctxSHA1StateInit[] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
109 0xEC, 0x05, 0x43, 0xE7, 0xDE, 0x8A, 0xEE, 0xFF,
110 0xAD, 0x72, 0x2B, 0x9D, 0x55, 0x4F, 0xCA, 0x6A,
111 0x8D, 0x81, 0xF1, 0xC7
112};
113
114static ULONG g_aulMD5Or4StateInit[] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 };
116 0x3D, 0xE8, 0x23, 0x8B, 0x9D, 0xE0, 0xCE, 0x48,
117 0xB1, 0x1B, 0xDD, 0xD9, 0xC6, 0x86, 0xB2, 0xDE
118};
120 0xE0, 0xE8, 0x50, 0x8A, 0x4D, 0x11, 0x02, 0xA6,
121 0x6A, 0xF0, 0xA7, 0xAB, 0xD8, 0xC4, 0x40, 0xED
122};
123
124static void Test_SHA1(void)
125{
126 FN_A_SHAInit* pfnA_SHAInit;
127 FN_A_SHAUpdate* pfnA_SHAUpdate;
128 FN_A_SHAFinal* pfnA_SHAFinal;
131 SIZE_T ComparedSize;
132
133 /* Load functions */
134 pfnA_SHAInit = (FN_A_SHAInit*)GetProcAddress(g_hAdvapi32, "A_SHAInit");
135 pfnA_SHAUpdate = (FN_A_SHAUpdate*)GetProcAddress(g_hAdvapi32, "A_SHAUpdate");
136 pfnA_SHAFinal = (FN_A_SHAFinal*)GetProcAddress(g_hAdvapi32, "A_SHAFinal");
137
138 if (!pfnA_SHAInit || !pfnA_SHAUpdate || !pfnA_SHAFinal)
139 {
140 skip("advapi32.dll!A_SHA*** not found\n");
141 return;
142 }
143
144 /* Test A_SHAInit */
145 pfnA_SHAInit(&ctx);
146 ComparedSize = RtlCompareMemory(ctx.State,
149 ok_eq_size(ComparedSize, RTL_FIELD_SIZE(A_SHA_CTX, State));
150 ok_eq_ulong(ctx.Count[0], 0UL);
151 ok_eq_ulong(ctx.Count[1], 0UL);
152
153 /* Test A_SHAUpdate */
154 pfnA_SHAUpdate(&ctx, (PUCHAR)g_TestString.Buffer, g_TestString.Length);
155 ComparedSize = RtlCompareMemory(ctx.Buffer,
158 ok_eq_size(ComparedSize, g_TestString.Length);
159 ComparedSize = RtlCompareMemory(ctx.State,
162 ok_eq_size(ComparedSize, RTL_FIELD_SIZE(A_SHA_CTX, State));
163 ok_eq_ulong(ctx.Count[0], 0UL);
165
166 /* Test A_SHAFinal */
167 pfnA_SHAFinal(&ctx, Result);
168 ComparedSize = RtlCompareMemoryUlong(ctx.Buffer, sizeof(ctx.Buffer), 0);
169 ok_eq_size(ComparedSize, sizeof(ctx.Buffer));
170 ComparedSize = RtlCompareMemory(ctx.State,
173 ok_eq_size(ComparedSize, RTL_FIELD_SIZE(A_SHA_CTX, State));
174 ok_eq_ulong(ctx.Count[0], 0UL);
175 ok_eq_ulong(ctx.Count[1], 0UL);
177 ok_eq_size(ComparedSize, A_SHA_DIGEST_LEN);
178}
179
180static void Test_MD5(void)
181{
182 FN_MD5Init* pfnMD5Init;
183 FN_MD5Update* pfnMD5Update;
184 FN_MD5Final* pfnMD5Final;
186 SIZE_T ComparedSize;
187
188 /* Load functions */
189 pfnMD5Init = (FN_MD5Init*)GetProcAddress(g_hAdvapi32, "MD5Init");
190 pfnMD5Update = (FN_MD5Update*)GetProcAddress(g_hAdvapi32, "MD5Update");
191 pfnMD5Final = (FN_MD5Final*)GetProcAddress(g_hAdvapi32, "MD5Final");
192
193 if (!pfnMD5Init || !pfnMD5Update || !pfnMD5Final)
194 {
195 skip("advapi32.dll!MD5*** not found\n");
196 return;
197 }
198
199 /* Test MD5Init */
200 pfnMD5Init(&ctx);
201 ok_eq_ulong(ctx.Count[0], 0UL);
202 ok_eq_ulong(ctx.Count[1], 0UL);
203 ComparedSize = RtlCompareMemory(ctx.State,
206 ok_eq_size(ComparedSize, RTL_FIELD_SIZE(MD5_CTX, State));
207
208 /* Test MD5Update */
210 ComparedSize = RtlCompareMemory(ctx.Buffer,
213 ok_eq_size(ComparedSize, g_TestString.Length);
214 ComparedSize = RtlCompareMemory(ctx.State,
217 ok_eq_size(ComparedSize, RTL_FIELD_SIZE(MD5_CTX, State));
219 ok_eq_ulong(ctx.Count[1], 0UL);
220
221 /* Test MD5Final */
222 pfnMD5Final(&ctx);
223 ok_eq_ulong(ctx.Count[0], 0x200UL);
224 ok_eq_ulong(ctx.Count[1], 0UL);
225 ok_eq_ulong(ctx.State[0], 0x8B23E83DUL);
226 ok_eq_ulong(ctx.State[1], 0x48CEE09DUL);
227 ok_eq_ulong(ctx.State[2], 0xD9DD1BB1UL);
228 ok_eq_ulong(ctx.State[3], 0xDEB286C6UL);
229 ComparedSize = RtlCompareMemoryUlong(ctx.Buffer, sizeof(ctx.Buffer), 0);
230 ok_eq_size(ComparedSize, sizeof(ctx.Buffer));
231 ComparedSize = RtlCompareMemory(ctx.Hash, &g_aucMD5Result, MD5_DIGEST_LEN);
232 ok_eq_size(ComparedSize, MD5_DIGEST_LEN);
233}
234
235static void Test_MD4(void)
236{
237 FN_MD4Init* pfnMD4Init;
238 FN_MD4Update* pfnMD4Update;
239 FN_MD4Final* pfnMD4Final;
241 SIZE_T ComparedSize;
242
243 /* Load functions */
244 pfnMD4Init = (FN_MD4Init*)GetProcAddress(g_hAdvapi32, "MD4Init");
245 pfnMD4Update = (FN_MD4Update*)GetProcAddress(g_hAdvapi32, "MD4Update");
246 pfnMD4Final = (FN_MD4Final*)GetProcAddress(g_hAdvapi32, "MD4Final");
247
248 if (!pfnMD4Init || !pfnMD4Update || !pfnMD4Final)
249 {
250 skip("advapi32.dll!MD4*** not found\n");
251 return;
252 }
253
254 /* Test MD4Init */
255 pfnMD4Init(&ctx);
256 ok_eq_ulong(ctx.Count[0], 0UL);
257 ok_eq_ulong(ctx.Count[1], 0UL);
258 ComparedSize = RtlCompareMemory(ctx.State,
261 ok_eq_size(ComparedSize, RTL_FIELD_SIZE(MD4_CTX, State));
262
263 /* Test MD4Update */
265 ComparedSize = RtlCompareMemory(ctx.Buffer,
268 ok_eq_size(ComparedSize, g_TestString.Length);
269 ComparedSize = RtlCompareMemory(ctx.State,
272 ok_eq_size(ComparedSize, RTL_FIELD_SIZE(MD4_CTX, State));
274 ok_eq_ulong(ctx.Count[1], 0UL);
275
276 /* Test MD4Final */
277 pfnMD4Final(&ctx);
278 ok_eq_ulong(ctx.Count[0], 0x200UL);
279 ok_eq_ulong(ctx.Count[1], 0UL);
280 ok_eq_ulong(ctx.State[0], 0x8A50E8E0UL);
281 ok_eq_ulong(ctx.State[1], 0xA602114DUL);
282 ok_eq_ulong(ctx.State[2], 0xABA7F06AUL);
283 ok_eq_ulong(ctx.State[3], 0xED40C4D8UL);
284 ComparedSize = RtlCompareMemoryUlong(ctx.Buffer, sizeof(ctx.Buffer), 0);
285 ok_eq_size(ComparedSize, sizeof(ctx.Buffer));
286 ComparedSize = RtlCompareMemory(ctx.Hash, &g_aucMD4Result, MD4_DIGEST_LEN);
287 ok_eq_size(ComparedSize, MD4_DIGEST_LEN);
288}
289
291{
292 /* Load advapi32.dll */
293 g_hAdvapi32 = GetModuleHandleW(L"advapi32.dll");
294 if (!g_hAdvapi32)
295 {
296 skip("Module advapi32 not found\n");
297 return;
298 }
299
300 Test_SHA1();
301 Test_MD5();
302 Test_MD4();
303}
#define A_SHA_DIGEST_LEN
Definition: Hash.c:17
static void Test_SHA1(void)
Definition: Hash.c:124
static ULONG g_aulMD5Or4StateInit[]
Definition: Hash.c:114
VOID RSA32API FN_A_SHAInit(_Out_ PA_SHA_CTX Context)
Definition: Hash.c:46
static HMODULE g_hAdvapi32
Definition: Hash.c:104
struct _MD4_CTX MD4_CTX
VOID RSA32API FN_A_SHAFinal(_Inout_ PA_SHA_CTX Context, _Out_ PUCHAR Result)
Definition: Hash.c:60
VOID RSA32API FN_MD5Update(_Inout_ PMD5_CTX Context, _In_reads_(BufferSize) PUCHAR Buffer, _In_ ULONG BufferSize)
Definition: Hash.c:73
VOID RSA32API FN_MD5Init(_Out_ PMD5_CTX Context)
Definition: Hash.c:67
static void Test_MD4(void)
Definition: Hash.c:235
static UCHAR g_aucSHA1Result[A_SHA_DIGEST_LEN]
Definition: Hash.c:108
VOID RSA32API FN_MD4Final(_Inout_ PMD4_CTX Context)
Definition: Hash.c:101
static UCHAR g_aucMD4Result[MD4_DIGEST_LEN]
Definition: Hash.c:119
#define RSA32API
Definition: Hash.c:40
static void Test_MD5(void)
Definition: Hash.c:180
VOID RSA32API FN_MD5Final(_Inout_ PMD5_CTX Context)
Definition: Hash.c:81
#define MD4_DIGEST_LEN
Definition: Hash.c:37
static ANSI_STRING g_TestString
Definition: Hash.c:105
VOID RSA32API FN_A_SHAUpdate(_Inout_ PA_SHA_CTX Context, _In_reads_(BufferSize) PUCHAR Buffer, _In_ ULONG BufferSize)
Definition: Hash.c:52
VOID RSA32API FN_MD4Update(_Inout_ PMD4_CTX Context, _In_reads_(BufferSize) PUCHAR Buffer, _In_ ULONG BufferSize)
Definition: Hash.c:93
struct _A_SHA_CTX A_SHA_CTX
struct _MD5_CTX MD5_CTX
VOID RSA32API FN_MD4Init(_Out_ PMD4_CTX Context)
Definition: Hash.c:87
struct _MD4_CTX * PMD4_CTX
static UCHAR g_aucMD5Result[MD5_DIGEST_LEN]
Definition: Hash.c:115
static ULONG g_ctxSHA1StateInit[]
Definition: Hash.c:107
#define MD5_DIGEST_LEN
Definition: Hash.c:27
struct _MD5_CTX * PMD5_CTX
struct _A_SHA_CTX * PA_SHA_CTX
#define ok_eq_ulong(value, expected)
Definition: apitest.h:63
#define ok_eq_size(value, expected)
Definition: apitest.h:69
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define GetProcAddress(x, y)
Definition: compat.h:753
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
static int Hash(const char *)
Definition: reader.c:2257
#define CHAR_BIT
Definition: urlcache.c:62
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
#define RTL_FIELD_SIZE(type, field)
Definition: kdb_expr.c:86
#define _In_reads_(s)
Definition: no_sal2.h:168
#define _Inout_
Definition: no_sal2.h:162
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define DECLSPEC_ALIGN(x)
Definition: ntbasedef.h:259
#define L(x)
Definition: ntvdm.h:50
Definition: util.c:82
ULONG Count[2]
Definition: Hash.c:14
Definition: Hash.c:30
UCHAR Hash[16]
Definition: Hash.c:34
ULONG Count[2]
Definition: Hash.c:32
Definition: Hash.c:20
UCHAR Hash[16]
Definition: Hash.c:24
ULONG Count[2]
Definition: Hash.c:21
#define UL
Definition: tui.h:165
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
ULONG_PTR SIZE_T
Definition: typedefs.h:80
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
_Must_inspect_result_ NTSYSAPI SIZE_T NTAPI RtlCompareMemoryUlong(_In_reads_bytes_(Length) PVOID Source, _In_ SIZE_T Length, _In_ ULONG Pattern)
unsigned char UCHAR
Definition: xmlstorage.h:181