3 #include <winpr/print.h>
4 #include <winpr/crypto.h>
7 static const char TEST_MD5_DATA[] =
"test";
8 static const BYTE TEST_MD5_HASH[] =
9 "\x09\x8f\x6b\xcd\x46\x21\xd3\x73\xca\xde\x4e\x83\x26\x27\xb4\xf6";
11 static BOOL test_crypto_hash_md5(
void)
14 BYTE hash[WINPR_MD5_DIGEST_LENGTH] = { 0 };
15 WINPR_DIGEST_CTX* ctx = NULL;
17 if (!(ctx = winpr_Digest_New()))
19 (void)fprintf(stderr,
"%s: winpr_Digest_New failed\n", __func__);
22 if (!winpr_Digest_Init(ctx, WINPR_MD_MD5))
24 (void)fprintf(stderr,
"%s: winpr_Digest_Init failed\n", __func__);
27 if (!winpr_Digest_Update(ctx, (
const BYTE*)TEST_MD5_DATA,
28 strnlen(TEST_MD5_DATA,
sizeof(TEST_MD5_DATA))))
30 (void)fprintf(stderr,
"%s: winpr_Digest_Update failed\n", __func__);
33 if (!winpr_Digest_Final(ctx, hash,
sizeof(hash)))
35 (void)fprintf(stderr,
"%s: winpr_Digest_Final failed\n", __func__);
38 if (memcmp(hash, TEST_MD5_HASH, WINPR_MD5_DIGEST_LENGTH) != 0)
41 char* expected = NULL;
43 actual = winpr_BinToHexString(hash, WINPR_MD5_DIGEST_LENGTH, FALSE);
44 expected = winpr_BinToHexString(TEST_MD5_HASH, WINPR_MD5_DIGEST_LENGTH, FALSE);
46 (void)fprintf(stderr,
"unexpected MD5 hash: Actual: %s Expected: %s\n", actual, expected);
56 winpr_Digest_Free(ctx);
60 static const char TEST_MD4_DATA[] =
"test";
61 static const BYTE TEST_MD4_HASH[] =
62 "\xdb\x34\x6d\x69\x1d\x7a\xcc\x4d\xc2\x62\x5d\xb1\x9f\x9e\x3f\x52";
64 static BOOL test_crypto_hash_md4(
void)
67 BYTE hash[WINPR_MD4_DIGEST_LENGTH] = { 0 };
68 WINPR_DIGEST_CTX* ctx = NULL;
70 if (!(ctx = winpr_Digest_New()))
72 (void)fprintf(stderr,
"%s: winpr_Digest_New failed\n", __func__);
75 if (!winpr_Digest_Init(ctx, WINPR_MD_MD4))
77 (void)fprintf(stderr,
"%s: winpr_Digest_Init failed\n", __func__);
80 if (!winpr_Digest_Update(ctx, (
const BYTE*)TEST_MD4_DATA,
81 strnlen(TEST_MD4_DATA,
sizeof(TEST_MD4_DATA))))
83 (void)fprintf(stderr,
"%s: winpr_Digest_Update failed\n", __func__);
86 if (!winpr_Digest_Final(ctx, hash,
sizeof(hash)))
88 (void)fprintf(stderr,
"%s: winpr_Digest_Final failed\n", __func__);
91 if (memcmp(hash, TEST_MD4_HASH, WINPR_MD4_DIGEST_LENGTH) != 0)
94 char* expected = NULL;
96 actual = winpr_BinToHexString(hash, WINPR_MD4_DIGEST_LENGTH, FALSE);
97 expected = winpr_BinToHexString(TEST_MD4_HASH, WINPR_MD4_DIGEST_LENGTH, FALSE);
99 (void)fprintf(stderr,
"unexpected MD4 hash: Actual: %s Expected: %s\n", actual, expected);
109 winpr_Digest_Free(ctx);
113 static const char TEST_SHA1_DATA[] =
"test";
114 static const BYTE TEST_SHA1_HASH[] =
115 "\xa9\x4a\x8f\xe5\xcc\xb1\x9b\xa6\x1c\x4c\x08\x73\xd3\x91\xe9\x87\x98\x2f\xbb\xd3";
117 static BOOL test_crypto_hash_sha1(
void)
120 BYTE hash[WINPR_SHA1_DIGEST_LENGTH] = { 0 };
121 WINPR_DIGEST_CTX* ctx = NULL;
123 if (!(ctx = winpr_Digest_New()))
125 (void)fprintf(stderr,
"%s: winpr_Digest_New failed\n", __func__);
128 if (!winpr_Digest_Init(ctx, WINPR_MD_SHA1))
130 (void)fprintf(stderr,
"%s: winpr_Digest_Init failed\n", __func__);
133 if (!winpr_Digest_Update(ctx, (
const BYTE*)TEST_SHA1_DATA,
134 strnlen(TEST_SHA1_DATA,
sizeof(TEST_SHA1_DATA))))
136 (void)fprintf(stderr,
"%s: winpr_Digest_Update failed\n", __func__);
139 if (!winpr_Digest_Final(ctx, hash,
sizeof(hash)))
141 (void)fprintf(stderr,
"%s: winpr_Digest_Final failed\n", __func__);
145 if (memcmp(hash, TEST_SHA1_HASH, WINPR_MD5_DIGEST_LENGTH) != 0)
148 char* expected = NULL;
150 actual = winpr_BinToHexString(hash, WINPR_SHA1_DIGEST_LENGTH, FALSE);
151 expected = winpr_BinToHexString(TEST_SHA1_HASH, WINPR_SHA1_DIGEST_LENGTH, FALSE);
153 (void)fprintf(stderr,
"unexpected SHA1 hash: Actual: %s Expected: %s\n", actual, expected);
163 winpr_Digest_Free(ctx);
167 static const char TEST_HMAC_MD5_DATA[] =
"Hi There";
168 static const BYTE TEST_HMAC_MD5_KEY[] =
169 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
170 static const BYTE TEST_HMAC_MD5_HASH[] =
171 "\xb5\x79\x91\xa2\x20\x3d\x49\x2d\x73\xfb\x71\x43\xdf\xc5\x08\x28";
173 static BOOL test_crypto_hash_hmac_md5(
void)
175 BYTE hash[WINPR_MD5_DIGEST_LENGTH] = { 0 };
176 WINPR_HMAC_CTX* ctx = NULL;
179 if (!(ctx = winpr_HMAC_New()))
181 (void)fprintf(stderr,
"%s: winpr_HMAC_New failed\n", __func__);
185 if (!winpr_HMAC_Init(ctx, WINPR_MD_MD5, TEST_HMAC_MD5_KEY, WINPR_MD5_DIGEST_LENGTH))
187 (void)fprintf(stderr,
"%s: winpr_HMAC_Init failed\n", __func__);
190 if (!winpr_HMAC_Update(ctx, (
const BYTE*)TEST_HMAC_MD5_DATA,
191 strnlen(TEST_HMAC_MD5_DATA,
sizeof(TEST_HMAC_MD5_DATA))))
193 (void)fprintf(stderr,
"%s: winpr_HMAC_Update failed\n", __func__);
196 if (!winpr_HMAC_Update(ctx, (
const BYTE*)TEST_HMAC_MD5_DATA,
197 strnlen(TEST_HMAC_MD5_DATA,
sizeof(TEST_HMAC_MD5_DATA))))
199 (void)fprintf(stderr,
"%s: winpr_HMAC_Update failed\n", __func__);
202 if (!winpr_HMAC_Final(ctx, hash,
sizeof(hash)))
204 (void)fprintf(stderr,
"%s: winpr_HMAC_Final failed\n", __func__);
208 if (memcmp(hash, TEST_HMAC_MD5_HASH, WINPR_MD5_DIGEST_LENGTH) != 0)
211 char* expected = NULL;
213 actual = winpr_BinToHexString(hash, WINPR_MD5_DIGEST_LENGTH, FALSE);
214 expected = winpr_BinToHexString(TEST_HMAC_MD5_HASH, WINPR_MD5_DIGEST_LENGTH, FALSE);
216 (void)fprintf(stderr,
"unexpected HMAC-MD5 hash: Actual: %s Expected: %s\n", actual,
227 winpr_HMAC_Free(ctx);
231 static const char TEST_HMAC_SHA1_DATA[] =
"Hi There";
232 static const BYTE TEST_HMAC_SHA1_KEY[] =
233 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
234 static const BYTE TEST_HMAC_SHA1_HASH[] =
235 "\xab\x23\x08\x2d\xca\x0c\x75\xea\xca\x60\x09\xc0\xb8\x8c\x2d\xf4\xf4\xbf\x88\xee";
237 static BOOL test_crypto_hash_hmac_sha1(
void)
239 BYTE hash[WINPR_SHA1_DIGEST_LENGTH] = { 0 };
240 WINPR_HMAC_CTX* ctx = NULL;
243 if (!(ctx = winpr_HMAC_New()))
245 (void)fprintf(stderr,
"%s: winpr_HMAC_New failed\n", __func__);
249 if (!winpr_HMAC_Init(ctx, WINPR_MD_SHA1, TEST_HMAC_SHA1_KEY, WINPR_SHA1_DIGEST_LENGTH))
251 (void)fprintf(stderr,
"%s: winpr_HMAC_Init failed\n", __func__);
254 if (!winpr_HMAC_Update(ctx, (
const BYTE*)TEST_HMAC_SHA1_DATA,
255 strnlen(TEST_HMAC_SHA1_DATA,
sizeof(TEST_HMAC_SHA1_DATA))))
257 (void)fprintf(stderr,
"%s: winpr_HMAC_Update failed\n", __func__);
260 if (!winpr_HMAC_Update(ctx, (
const BYTE*)TEST_HMAC_SHA1_DATA,
261 strnlen(TEST_HMAC_SHA1_DATA,
sizeof(TEST_HMAC_SHA1_DATA))))
263 (void)fprintf(stderr,
"%s: winpr_HMAC_Update failed\n", __func__);
266 if (!winpr_HMAC_Final(ctx, hash,
sizeof(hash)))
268 (void)fprintf(stderr,
"%s: winpr_HMAC_Final failed\n", __func__);
272 if (memcmp(hash, TEST_HMAC_SHA1_HASH, WINPR_SHA1_DIGEST_LENGTH) != 0)
275 char* expected = NULL;
277 actual = winpr_BinToHexString(hash, WINPR_SHA1_DIGEST_LENGTH, FALSE);
278 expected = winpr_BinToHexString(TEST_HMAC_SHA1_HASH, WINPR_SHA1_DIGEST_LENGTH, FALSE);
280 (void)fprintf(stderr,
"unexpected HMAC-SHA1 hash: Actual: %s Expected: %s\n", actual,
291 winpr_HMAC_Free(ctx);
295 int TestCryptoHash(
int argc,
char* argv[])
300 winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
302 if (!test_crypto_hash_md5())
305 if (!test_crypto_hash_md4())
308 if (!test_crypto_hash_sha1())
311 if (!test_crypto_hash_hmac_md5())
314 if (!test_crypto_hash_hmac_sha1())