bcrypt: Support BCRYPT_KEY_LENGTHS property for AES.
[wine.git] / dlls / bcrypt / tests / bcrypt.c
blob7ac0c116298ae4929d76de3a77ae2ef5c122e89d
1 /*
2 * Unit test for bcrypt functions
4 * Copyright 2014 Bruno Jesus
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include <ntstatus.h>
23 #define WIN32_NO_STATUS
24 #include <windows.h>
25 #include <bcrypt.h>
27 #include "wine/test.h"
29 static NTSTATUS (WINAPI *pBCryptOpenAlgorithmProvider)(BCRYPT_ALG_HANDLE *, LPCWSTR, LPCWSTR, ULONG);
30 static NTSTATUS (WINAPI *pBCryptCloseAlgorithmProvider)(BCRYPT_ALG_HANDLE, ULONG);
31 static NTSTATUS (WINAPI *pBCryptGetFipsAlgorithmMode)(BOOLEAN *);
32 static NTSTATUS (WINAPI *pBCryptCreateHash)(BCRYPT_ALG_HANDLE, BCRYPT_HASH_HANDLE *, PUCHAR, ULONG, PUCHAR,
33 ULONG, ULONG);
34 static NTSTATUS (WINAPI *pBCryptHash)(BCRYPT_ALG_HANDLE, UCHAR *, ULONG, UCHAR *, ULONG, UCHAR *, ULONG);
35 static NTSTATUS (WINAPI *pBCryptHashData)(BCRYPT_HASH_HANDLE, PUCHAR, ULONG, ULONG);
36 static NTSTATUS (WINAPI *pBCryptDuplicateHash)(BCRYPT_HASH_HANDLE, BCRYPT_HASH_HANDLE *, UCHAR *, ULONG, ULONG);
37 static NTSTATUS (WINAPI *pBCryptFinishHash)(BCRYPT_HASH_HANDLE, PUCHAR, ULONG, ULONG);
38 static NTSTATUS (WINAPI *pBCryptDestroyHash)(BCRYPT_HASH_HANDLE);
39 static NTSTATUS (WINAPI *pBCryptGenRandom)(BCRYPT_ALG_HANDLE, PUCHAR, ULONG, ULONG);
40 static NTSTATUS (WINAPI *pBCryptGetProperty)(BCRYPT_HANDLE, LPCWSTR, PUCHAR, ULONG, ULONG *, ULONG);
41 static NTSTATUS (WINAPI *pBCryptSetProperty)(BCRYPT_HANDLE, LPCWSTR, PUCHAR, ULONG, ULONG);
42 static NTSTATUS (WINAPI *pBCryptGenerateSymmetricKey)(BCRYPT_ALG_HANDLE, BCRYPT_KEY_HANDLE *, PUCHAR, ULONG,
43 PUCHAR, ULONG, ULONG);
44 static NTSTATUS (WINAPI *pBCryptEncrypt)(BCRYPT_KEY_HANDLE, PUCHAR, ULONG, VOID *, PUCHAR, ULONG, PUCHAR, ULONG,
45 ULONG *, ULONG);
46 static NTSTATUS (WINAPI *pBCryptDecrypt)(BCRYPT_KEY_HANDLE, PUCHAR, ULONG, VOID *, PUCHAR, ULONG, PUCHAR, ULONG,
47 ULONG *, ULONG);
48 static NTSTATUS (WINAPI *pBCryptDestroyKey)(BCRYPT_KEY_HANDLE);
50 static void test_BCryptGenRandom(void)
52 NTSTATUS ret;
53 UCHAR buffer[256];
55 ret = pBCryptGenRandom(NULL, NULL, 0, 0);
56 ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got 0x%x\n", ret);
57 ret = pBCryptGenRandom(NULL, buffer, 0, 0);
58 ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got 0x%x\n", ret);
59 ret = pBCryptGenRandom(NULL, buffer, sizeof(buffer), 0);
60 ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got 0x%x\n", ret);
61 ret = pBCryptGenRandom(NULL, buffer, sizeof(buffer), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
62 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
63 ret = pBCryptGenRandom(NULL, buffer, sizeof(buffer),
64 BCRYPT_USE_SYSTEM_PREFERRED_RNG|BCRYPT_RNG_USE_ENTROPY_IN_BUFFER);
65 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
66 ret = pBCryptGenRandom(NULL, NULL, sizeof(buffer), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
67 ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got 0x%x\n", ret);
69 /* Zero sized buffer should work too */
70 ret = pBCryptGenRandom(NULL, buffer, 0, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
71 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
73 /* Test random number generation - It's impossible for a sane RNG to return 8 zeros */
74 memset(buffer, 0, 16);
75 ret = pBCryptGenRandom(NULL, buffer, 8, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
76 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
77 ok(memcmp(buffer, buffer + 8, 8), "Expected a random number, got 0\n");
80 static void test_BCryptGetFipsAlgorithmMode(void)
82 static const WCHAR policyKeyVistaW[] = {
83 'S','y','s','t','e','m','\\',
84 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
85 'C','o','n','t','r','o','l','\\',
86 'L','s','a','\\',
87 'F','I','P','S','A','l','g','o','r','i','t','h','m','P','o','l','i','c','y',0};
88 static const WCHAR policyValueVistaW[] = {'E','n','a','b','l','e','d',0};
89 static const WCHAR policyKeyXPW[] = {
90 'S','y','s','t','e','m','\\',
91 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
92 'C','o','n','t','r','o','l','\\',
93 'L','s','a',0};
94 static const WCHAR policyValueXPW[] = {
95 'F','I','P','S','A','l','g','o','r','i','t','h','m','P','o','l','i','c','y',0};
96 HKEY hkey = NULL;
97 BOOLEAN expected;
98 BOOLEAN enabled;
99 DWORD value, count[2] = {sizeof(value), sizeof(value)};
100 NTSTATUS ret;
102 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, policyKeyVistaW, &hkey) == ERROR_SUCCESS &&
103 RegQueryValueExW(hkey, policyValueVistaW, NULL, NULL, (void *)&value, &count[0]) == ERROR_SUCCESS)
105 expected = !!value;
107 else if (RegOpenKeyW(HKEY_LOCAL_MACHINE, policyKeyXPW, &hkey) == ERROR_SUCCESS &&
108 RegQueryValueExW(hkey, policyValueXPW, NULL, NULL, (void *)&value, &count[0]) == ERROR_SUCCESS)
110 expected = !!value;
112 else
114 expected = FALSE;
115 todo_wine
116 ok(0, "Neither XP or Vista key is present\n");
118 RegCloseKey(hkey);
120 ret = pBCryptGetFipsAlgorithmMode(&enabled);
121 ok(ret == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got 0x%x\n", ret);
122 ok(enabled == expected, "expected result %d, got %d\n", expected, enabled);
124 ret = pBCryptGetFipsAlgorithmMode(NULL);
125 ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got 0x%x\n", ret);
128 static void format_hash(const UCHAR *bytes, ULONG size, char *buf)
130 ULONG i;
131 buf[0] = '\0';
132 for (i = 0; i < size; i++)
134 sprintf(buf + i * 2, "%02x", bytes[i]);
136 return;
139 static int strcmp_wa(const WCHAR *strw, const char *stra)
141 WCHAR buf[512];
142 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(buf[0]));
143 return lstrcmpW(strw, buf);
146 #define test_object_length(a) _test_object_length(__LINE__,a)
147 static void _test_object_length(unsigned line, void *handle)
149 NTSTATUS status;
150 ULONG len, size;
152 len = size = 0xdeadbeef;
153 status = pBCryptGetProperty(NULL, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
154 ok_(__FILE__,line)(status == STATUS_INVALID_HANDLE, "BCryptGetProperty failed: %08x\n", status);
156 len = size = 0xdeadbeef;
157 status = pBCryptGetProperty(handle, NULL, (UCHAR *)&len, sizeof(len), &size, 0);
158 ok_(__FILE__,line)(status == STATUS_INVALID_PARAMETER, "BCryptGetProperty failed: %08x\n", status);
160 len = size = 0xdeadbeef;
161 status = pBCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), NULL, 0);
162 ok_(__FILE__,line)(status == STATUS_INVALID_PARAMETER, "BCryptGetProperty failed: %08x\n", status);
164 len = size = 0xdeadbeef;
165 status = pBCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, NULL, sizeof(len), &size, 0);
166 ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %08x\n", status);
167 ok_(__FILE__,line)(size == sizeof(len), "got %u\n", size);
169 len = size = 0xdeadbeef;
170 status = pBCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, 0, &size, 0);
171 ok_(__FILE__,line)(status == STATUS_BUFFER_TOO_SMALL, "BCryptGetProperty failed: %08x\n", status);
172 ok_(__FILE__,line)(len == 0xdeadbeef, "got %u\n", len);
173 ok_(__FILE__,line)(size == sizeof(len), "got %u\n", size);
175 len = size = 0xdeadbeef;
176 status = pBCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
177 ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %08x\n", status);
178 ok_(__FILE__,line)(len != 0xdeadbeef, "len not set\n");
179 ok_(__FILE__,line)(size == sizeof(len), "got %u\n", size);
182 #define test_hash_length(a,b) _test_hash_length(__LINE__,a,b)
183 static void _test_hash_length(unsigned line, void *handle, ULONG exlen)
185 ULONG len = 0xdeadbeef, size = 0xdeadbeef;
186 NTSTATUS status;
188 status = pBCryptGetProperty(handle, BCRYPT_HASH_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
189 ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %08x\n", status);
190 ok_(__FILE__,line)(size == sizeof(len), "got %u\n", size);
191 ok_(__FILE__,line)(len == exlen, "len = %u, expected %u\n", len, exlen);
194 #define test_alg_name(a,b) _test_alg_name(__LINE__,a,b)
195 static void _test_alg_name(unsigned line, void *handle, const char *exname)
197 ULONG size = 0xdeadbeef;
198 UCHAR buf[256];
199 const WCHAR *name = (const WCHAR*)buf;
200 NTSTATUS status;
202 status = pBCryptGetProperty(handle, BCRYPT_ALGORITHM_NAME, buf, sizeof(buf), &size, 0);
203 ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %08x\n", status);
204 ok_(__FILE__,line)(size == (strlen(exname)+1)*sizeof(WCHAR), "got %u\n", size);
205 ok_(__FILE__,line)(!strcmp_wa(name, exname), "alg name = %s, expected %s\n", wine_dbgstr_w(name), exname);
208 static void test_sha1(void)
210 static const char expected[] = "961fa64958818f767707072755d7018dcd278e94";
211 static const char expected_hmac[] = "2472cf65d0e090618d769d3e46f0d9446cf212da";
212 UCHAR buf[512], buf_hmac[1024], buf_hmac2[1024], sha1[20], sha1_hmac[20];
213 BCRYPT_HASH_HANDLE hash, hash2;
214 BCRYPT_ALG_HANDLE alg;
215 char str[41];
216 NTSTATUS ret;
217 ULONG len;
219 alg = NULL;
220 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA1_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
221 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
222 ok(alg != NULL, "alg not set\n");
224 test_object_length(alg);
225 test_hash_length(alg, 20);
226 test_alg_name(alg, "SHA1");
228 hash = NULL;
229 len = sizeof(buf);
230 ret = pBCryptCreateHash(alg, &hash, buf, len, NULL, 0, 0);
231 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
232 ok(hash != NULL, "hash not set\n");
234 ret = pBCryptHashData(hash, NULL, 0, 0);
235 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
237 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
238 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
240 test_hash_length(hash, 20);
241 test_alg_name(hash, "SHA1");
243 memset(sha1, 0, sizeof(sha1));
244 ret = pBCryptFinishHash(hash, sha1, sizeof(sha1), 0);
245 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
246 format_hash( sha1, sizeof(sha1), str );
247 ok(!strcmp(str, expected), "got %s\n", str);
249 ret = pBCryptDestroyHash(hash);
250 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
252 ret = pBCryptCloseAlgorithmProvider(alg, 0);
253 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
255 alg = NULL;
256 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA1_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
257 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
258 ok(alg != NULL, "alg not set\n");
260 hash = NULL;
261 len = sizeof(buf_hmac);
262 ret = pBCryptCreateHash(alg, &hash, buf_hmac, len, (UCHAR *)"key", sizeof("key"), 0);
263 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
264 ok(hash != NULL, "hash not set\n");
266 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
267 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
269 test_hash_length(hash, 20);
270 test_alg_name(hash, "SHA1");
272 len = sizeof(buf_hmac2);
273 ret = pBCryptDuplicateHash(NULL, &hash2, buf_hmac2, len, 0);
274 ok(ret == STATUS_INVALID_HANDLE, "got %08x\n", ret);
276 len = sizeof(buf_hmac2);
277 ret = pBCryptDuplicateHash(hash, NULL, buf_hmac2, len, 0);
278 ok(ret == STATUS_INVALID_PARAMETER, "got %08x\n", ret);
280 hash2 = NULL;
281 len = sizeof(buf_hmac2);
282 ret = pBCryptDuplicateHash(hash, &hash2, buf_hmac2, len, 0);
283 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
284 ok(hash2 != NULL, "hash not set\n");
286 memset(sha1_hmac, 0, sizeof(sha1_hmac));
287 ret = pBCryptFinishHash(hash2, sha1_hmac, sizeof(sha1_hmac), 0);
288 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
289 format_hash( sha1_hmac, sizeof(sha1_hmac), str );
290 ok(!strcmp(str, expected_hmac), "got %s\n", str);
292 ret = pBCryptDestroyHash(hash2);
293 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
295 memset(sha1_hmac, 0, sizeof(sha1_hmac));
296 ret = pBCryptFinishHash(hash, sha1_hmac, sizeof(sha1_hmac), 0);
297 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
298 format_hash( sha1_hmac, sizeof(sha1_hmac), str );
299 ok(!strcmp(str, expected_hmac), "got %s\n", str);
301 ret = pBCryptDestroyHash(hash);
302 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
304 ret = pBCryptCloseAlgorithmProvider(alg, 0);
305 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
308 static void test_sha256(void)
310 static const char expected[] =
311 "ceb73749c899693706ede1e30c9929b3fd5dd926163831c2fb8bd41e6efb1126";
312 static const char expected_hmac[] =
313 "34c1aa473a4468a91d06e7cdbc75bc4f93b830ccfc2a47ffd74e8e6ed29e4c72";
314 BCRYPT_ALG_HANDLE alg;
315 BCRYPT_HASH_HANDLE hash;
316 UCHAR buf[512], buf_hmac[1024], sha256[32], sha256_hmac[32];
317 char str[65];
318 NTSTATUS ret;
319 ULONG len;
321 alg = NULL;
322 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA256_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
323 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
324 ok(alg != NULL, "alg not set\n");
326 test_object_length(alg);
327 test_hash_length(alg, 32);
328 test_alg_name(alg, "SHA256");
330 hash = NULL;
331 len = sizeof(buf);
332 ret = pBCryptCreateHash(alg, &hash, buf, len, NULL, 0, 0);
333 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
334 ok(hash != NULL, "hash not set\n");
336 ret = pBCryptHashData(hash, NULL, 0, 0);
337 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
339 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
340 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
342 test_hash_length(hash, 32);
343 test_alg_name(hash, "SHA256");
345 memset(sha256, 0, sizeof(sha256));
346 ret = pBCryptFinishHash(hash, sha256, sizeof(sha256), 0);
347 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
348 format_hash( sha256, sizeof(sha256), str );
349 ok(!strcmp(str, expected), "got %s\n", str);
351 ret = pBCryptDestroyHash(hash);
352 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
354 ret = pBCryptCloseAlgorithmProvider(alg, 0);
355 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
357 alg = NULL;
358 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA256_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
359 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
360 ok(alg != NULL, "alg not set\n");
362 hash = NULL;
363 len = sizeof(buf_hmac);
364 ret = pBCryptCreateHash(alg, &hash, buf_hmac, len, (UCHAR *)"key", sizeof("key"), 0);
365 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
366 ok(hash != NULL, "hash not set\n");
368 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
369 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
371 test_hash_length(hash, 32);
372 test_alg_name(hash, "SHA256");
374 memset(sha256_hmac, 0, sizeof(sha256_hmac));
375 ret = pBCryptFinishHash(hash, sha256_hmac, sizeof(sha256_hmac), 0);
376 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
377 format_hash( sha256_hmac, sizeof(sha256_hmac), str );
378 ok(!strcmp(str, expected_hmac), "got %s\n", str);
380 ret = pBCryptDestroyHash(hash);
381 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
383 ret = pBCryptCloseAlgorithmProvider(alg, 0);
384 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
387 static void test_sha384(void)
389 static const char expected[] =
390 "62b21e90c9022b101671ba1f808f8631a8149f0f12904055839a35c1ca78ae5363eed1e743a692d70e0504b0cfd12ef9";
391 static const char expected_hmac[] =
392 "4b3e6d6ff2da121790ab7e7b9247583e3a7eed2db5bd4dabc680303b1608f37dfdc836d96a704c03283bc05b4f6c5eb8";
393 BCRYPT_ALG_HANDLE alg;
394 BCRYPT_HASH_HANDLE hash;
395 UCHAR buf[512], buf_hmac[1024], sha384[48], sha384_hmac[48];
396 char str[97];
397 NTSTATUS ret;
398 ULONG len;
400 alg = NULL;
401 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA384_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
402 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
403 ok(alg != NULL, "alg not set\n");
405 test_object_length(alg);
406 test_hash_length(alg, 48);
407 test_alg_name(alg, "SHA384");
409 hash = NULL;
410 len = sizeof(buf);
411 ret = pBCryptCreateHash(alg, &hash, buf, len, NULL, 0, 0);
412 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
413 ok(hash != NULL, "hash not set\n");
415 ret = pBCryptHashData(hash, NULL, 0, 0);
416 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
418 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
419 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
421 test_hash_length(hash, 48);
422 test_alg_name(hash, "SHA384");
424 memset(sha384, 0, sizeof(sha384));
425 ret = pBCryptFinishHash(hash, sha384, sizeof(sha384), 0);
426 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
427 format_hash( sha384, sizeof(sha384), str );
428 ok(!strcmp(str, expected), "got %s\n", str);
430 ret = pBCryptDestroyHash(hash);
431 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
433 ret = pBCryptCloseAlgorithmProvider(alg, 0);
434 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
436 alg = NULL;
437 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA384_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
438 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
439 ok(alg != NULL, "alg not set\n");
441 hash = NULL;
442 len = sizeof(buf_hmac);
443 ret = pBCryptCreateHash(alg, &hash, buf_hmac, len, (UCHAR *)"key", sizeof("key"), 0);
444 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
445 ok(hash != NULL, "hash not set\n");
447 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
448 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
450 test_hash_length(hash, 48);
451 test_alg_name(hash, "SHA384");
453 memset(sha384_hmac, 0, sizeof(sha384_hmac));
454 ret = pBCryptFinishHash(hash, sha384_hmac, sizeof(sha384_hmac), 0);
455 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
456 format_hash( sha384_hmac, sizeof(sha384_hmac), str );
457 ok(!strcmp(str, expected_hmac), "got %s\n", str);
459 ret = pBCryptDestroyHash(hash);
460 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
462 ret = pBCryptCloseAlgorithmProvider(alg, 0);
463 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
466 static void test_sha512(void)
468 static const char expected[] =
469 "d55ced17163bf5386f2cd9ff21d6fd7fe576a915065c24744d09cfae4ec84ee1e"
470 "f6ef11bfbc5acce3639bab725b50a1fe2c204f8c820d6d7db0df0ecbc49c5ca";
471 static const char expected_hmac[] =
472 "415fb6b10018ca03b38a1b1399c42ac0be5e8aceddb9a73103f5e543bf2d888f2"
473 "eecf91373941f9315dd730a77937fa92444450fbece86f409d9cb5ec48c6513";
474 BCRYPT_ALG_HANDLE alg;
475 BCRYPT_HASH_HANDLE hash;
476 UCHAR buf[512], buf_hmac[1024], sha512[64], sha512_hmac[64];
477 char str[129];
478 NTSTATUS ret;
479 ULONG len;
481 alg = NULL;
482 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA512_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
483 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
484 ok(alg != NULL, "alg not set\n");
486 test_object_length(alg);
487 test_hash_length(alg, 64);
488 test_alg_name(alg, "SHA512");
490 hash = NULL;
491 len = sizeof(buf);
492 ret = pBCryptCreateHash(alg, &hash, buf, len, NULL, 0, 0);
493 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
494 ok(hash != NULL, "hash not set\n");
496 ret = pBCryptHashData(hash, NULL, 0, 0);
497 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
499 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
500 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
502 test_hash_length(hash, 64);
503 test_alg_name(hash, "SHA512");
505 memset(sha512, 0, sizeof(sha512));
506 ret = pBCryptFinishHash(hash, sha512, sizeof(sha512), 0);
507 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
508 format_hash( sha512, sizeof(sha512), str );
509 ok(!strcmp(str, expected), "got %s\n", str);
511 ret = pBCryptDestroyHash(hash);
512 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
514 ret = pBCryptCloseAlgorithmProvider(alg, 0);
515 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
517 alg = NULL;
518 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_SHA512_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
519 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
520 ok(alg != NULL, "alg not set\n");
522 hash = NULL;
523 len = sizeof(buf_hmac);
524 ret = pBCryptCreateHash(alg, &hash, buf_hmac, len, (UCHAR *)"key", sizeof("key"), 0);
525 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
526 ok(hash != NULL, "hash not set\n");
528 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
529 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
531 test_hash_length(hash, 64);
532 test_alg_name(hash, "SHA512");
534 memset(sha512_hmac, 0, sizeof(sha512_hmac));
535 ret = pBCryptFinishHash(hash, sha512_hmac, sizeof(sha512_hmac), 0);
536 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
537 format_hash( sha512_hmac, sizeof(sha512_hmac), str );
538 ok(!strcmp(str, expected_hmac), "got %s\n", str);
540 ret = pBCryptDestroyHash(hash);
541 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
543 ret = pBCryptCloseAlgorithmProvider(alg, 0);
544 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
548 static void test_md5(void)
550 static const char expected[] =
551 "e2a3e68d23ce348b8f68b3079de3d4c9";
552 static const char expected_hmac[] =
553 "7bda029b93fa8d817fcc9e13d6bdf092";
554 BCRYPT_ALG_HANDLE alg;
555 BCRYPT_HASH_HANDLE hash;
556 UCHAR buf[512], buf_hmac[1024], md5[16], md5_hmac[16];
557 char str[65];
558 NTSTATUS ret;
559 ULONG len;
561 alg = NULL;
562 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
563 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
564 ok(alg != NULL, "alg not set\n");
566 test_object_length(alg);
567 test_hash_length(alg, 16);
568 test_alg_name(alg, "MD5");
570 hash = NULL;
571 len = sizeof(buf);
572 ret = pBCryptCreateHash(alg, &hash, buf, len, NULL, 0, 0);
573 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
574 ok(hash != NULL, "hash not set\n");
576 ret = pBCryptHashData(hash, NULL, 0, 0);
577 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
579 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
580 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
582 test_hash_length(hash, 16);
583 test_alg_name(hash, "MD5");
585 memset(md5, 0, sizeof(md5));
586 ret = pBCryptFinishHash(hash, md5, sizeof(md5), 0);
587 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
588 format_hash( md5, sizeof(md5), str );
589 ok(!strcmp(str, expected), "got %s\n", str);
591 ret = pBCryptDestroyHash(hash);
592 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
594 ret = pBCryptCloseAlgorithmProvider(alg, 0);
595 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
597 alg = NULL;
598 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
599 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
600 ok(alg != NULL, "alg not set\n");
602 hash = NULL;
603 len = sizeof(buf_hmac);
604 ret = pBCryptCreateHash(alg, &hash, buf_hmac, len, (UCHAR *)"key", sizeof("key"), 0);
605 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
606 ok(hash != NULL, "hash not set\n");
608 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
609 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
611 test_hash_length(hash, 16);
612 test_alg_name(hash, "MD5");
614 memset(md5_hmac, 0, sizeof(md5_hmac));
615 ret = pBCryptFinishHash(hash, md5_hmac, sizeof(md5_hmac), 0);
616 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
617 format_hash( md5_hmac, sizeof(md5_hmac), str );
618 ok(!strcmp(str, expected_hmac), "got %s\n", str);
620 ret = pBCryptDestroyHash(hash);
621 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
623 ret = pBCryptCloseAlgorithmProvider(alg, 0);
624 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
627 static void test_BcryptHash(void)
629 static const char expected[] =
630 "e2a3e68d23ce348b8f68b3079de3d4c9";
631 static const char expected_hmac[] =
632 "7bda029b93fa8d817fcc9e13d6bdf092";
633 BCRYPT_ALG_HANDLE alg;
634 UCHAR md5[16], md5_hmac[16];
635 char str[65];
636 NTSTATUS ret;
638 alg = NULL;
639 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
640 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
641 ok(alg != NULL, "alg not set\n");
643 test_hash_length(alg, 16);
644 test_alg_name(alg, "MD5");
646 memset(md5, 0, sizeof(md5));
647 ret = pBCryptHash(alg, NULL, 0, (UCHAR *)"test", sizeof("test"), md5, sizeof(md5));
648 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
649 format_hash( md5, sizeof(md5), str );
650 ok(!strcmp(str, expected), "got %s\n", str);
652 ret = pBCryptCloseAlgorithmProvider(alg, 0);
653 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
655 alg = NULL;
656 memset(md5_hmac, 0, sizeof(md5_hmac));
657 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
658 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
659 ok(alg != NULL, "alg not set\n");
661 ret = pBCryptHash(alg, (UCHAR *)"key", sizeof("key"), (UCHAR *)"test", sizeof("test"), md5_hmac, sizeof(md5_hmac));
662 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
663 format_hash( md5_hmac, sizeof(md5_hmac), str );
664 ok(!strcmp(str, expected_hmac), "got %s\n", str);
666 ret = pBCryptCloseAlgorithmProvider(alg, 0);
667 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
670 static void test_rng(void)
672 BCRYPT_ALG_HANDLE alg;
673 ULONG size, len;
674 UCHAR buf[16];
675 NTSTATUS ret;
677 alg = NULL;
678 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
679 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
680 ok(alg != NULL, "alg not set\n");
682 len = size = 0xdeadbeef;
683 ret = pBCryptGetProperty(alg, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
684 ok(ret == STATUS_NOT_SUPPORTED, "got %08x\n", ret);
686 len = size = 0xdeadbeef;
687 ret = pBCryptGetProperty(alg, BCRYPT_HASH_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
688 ok(ret == STATUS_NOT_SUPPORTED, "got %08x\n", ret);
690 test_alg_name(alg, "RNG");
692 memset(buf, 0, 16);
693 ret = pBCryptGenRandom(alg, buf, 8, 0);
694 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
695 ok(memcmp(buf, buf + 8, 8), "got zeroes\n");
697 ret = pBCryptCloseAlgorithmProvider(alg, 0);
698 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
701 static void test_aes(void)
703 BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
704 BCRYPT_ALG_HANDLE alg;
705 ULONG size, len;
706 UCHAR mode[64];
707 NTSTATUS ret;
709 alg = NULL;
710 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_AES_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
711 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
712 ok(alg != NULL, "alg not set\n");
714 len = size = 0;
715 ret = pBCryptGetProperty(alg, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
716 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
717 ok(len, "expected non-zero len\n");
718 ok(size == sizeof(len), "got %u\n", size);
720 len = size = 0;
721 ret = pBCryptGetProperty(alg, BCRYPT_BLOCK_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
722 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
723 ok(len == 16, "got %u\n", len);
724 ok(size == sizeof(len), "got %u\n", size);
726 size = 0;
727 ret = pBCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, 0, &size, 0);
728 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
729 ok(size == 64, "got %u\n", size);
731 size = 0;
732 ret = pBCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode), &size, 0);
733 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
734 ok(!lstrcmpW((const WCHAR *)mode, BCRYPT_CHAIN_MODE_CBC), "got %s\n", mode);
735 ok(size == 64, "got %u\n", size);
737 size = 0;
738 memset(&key_lengths, 0, sizeof(key_lengths));
739 ret = BCryptGetProperty(alg, BCRYPT_KEY_LENGTHS, (UCHAR*)&key_lengths, sizeof(key_lengths), &size, 0);
740 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
741 ok(size == sizeof(key_lengths), "got %u\n", size);
742 ok(key_lengths.dwMinLength == 128, "Expected 128, got %d\n", key_lengths.dwMinLength);
743 ok(key_lengths.dwMaxLength == 256, "Expected 256, got %d\n", key_lengths.dwMaxLength);
744 ok(key_lengths.dwIncrement == 64, "Expected 64, got %d\n", key_lengths.dwIncrement);
746 test_alg_name(alg, "AES");
748 ret = pBCryptCloseAlgorithmProvider(alg, 0);
749 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
752 static void test_BCryptGenerateSymmetricKey(void)
754 static UCHAR secret[] =
755 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
756 static UCHAR iv[] =
757 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
758 static UCHAR data[] =
759 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
760 static UCHAR expected[] =
761 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79};
762 BCRYPT_ALG_HANDLE aes;
763 BCRYPT_KEY_HANDLE key;
764 UCHAR *buf, ciphertext[16], plaintext[16], ivbuf[16];
765 ULONG size, len, i;
766 NTSTATUS ret;
768 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
769 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
771 len = size = 0xdeadbeef;
772 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
773 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
775 key = NULL;
776 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
777 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
778 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
779 ok(key != NULL, "key not set\n");
781 ret = pBCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR *)BCRYPT_CHAIN_MODE_CBC,
782 sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
783 todo_wine ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
785 size = 0xdeadbeef;
786 ret = pBCryptEncrypt(key, NULL, 0, NULL, NULL, 0, NULL, 0, &size, 0);
787 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
788 ok(!size, "got %u\n", size);
790 size = 0;
791 memcpy(ivbuf, iv, sizeof(iv));
792 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
793 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
794 ok(size == 16, "got %u\n", size);
796 size = 0;
797 memcpy(ivbuf, iv, sizeof(iv));
798 memset(ciphertext, 0, sizeof(ciphertext));
799 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
800 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
801 ok(size == 16, "got %u\n", size);
802 ok(!memcmp(ciphertext, expected, sizeof(expected)), "wrong data\n");
803 for (i = 0; i < 16; i++)
804 ok(ciphertext[i] == expected[i], "%u: %02x != %02x\n", i, ciphertext[i], expected[i]);
806 size = 0xdeadbeef;
807 ret = pBCryptDecrypt(key, NULL, 0, NULL, NULL, 0, NULL, 0, &size, 0);
808 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
809 ok(!size, "got %u\n", size);
811 size = 0;
812 memcpy(ivbuf, iv, sizeof(iv));
813 ret = pBCryptDecrypt(key, ciphertext, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
814 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
815 ok(size == 16, "got %u\n", size);
817 size = 0;
818 memcpy(ivbuf, iv, sizeof(iv));
819 memset(plaintext, 0, sizeof(plaintext));
820 ret = pBCryptDecrypt(key, ciphertext, 16, NULL, ivbuf, 16, plaintext, 16, &size, 0);
821 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
822 ok(size == 16, "got %u\n", size);
823 ok(!memcmp(plaintext, data, sizeof(data)), "wrong data\n");
825 ret = pBCryptDestroyKey(key);
826 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
827 HeapFree(GetProcessHeap(), 0, buf);
829 ret = pBCryptCloseAlgorithmProvider(aes, 0);
830 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
833 static void test_BCryptEncrypt(void)
835 static UCHAR secret[] =
836 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
837 static UCHAR iv[] =
838 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
839 static UCHAR data[] =
840 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
841 static UCHAR data2[] =
842 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
843 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
844 static UCHAR expected[] =
845 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79};
846 static UCHAR expected2[] =
847 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
848 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
849 static UCHAR expected3[] =
850 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
851 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
852 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
853 BCRYPT_ALG_HANDLE aes;
854 BCRYPT_KEY_HANDLE key;
855 UCHAR *buf, ciphertext[48], ivbuf[16];
856 ULONG size, len, i;
857 NTSTATUS ret;
859 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
860 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
862 len = 0xdeadbeef;
863 size = sizeof(len);
864 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
865 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
867 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
868 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
869 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
871 /* input size is a multiple of block size */
872 size = 0;
873 memcpy(ivbuf, iv, sizeof(iv));
874 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
875 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
876 ok(size == 16, "got %u\n", size);
878 size = 0;
879 memcpy(ivbuf, iv, sizeof(iv));
880 memset(ciphertext, 0, sizeof(ciphertext));
881 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
882 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
883 ok(size == 16, "got %u\n", size);
884 ok(!memcmp(ciphertext, expected, sizeof(expected)), "wrong data\n");
885 for (i = 0; i < 16; i++)
886 ok(ciphertext[i] == expected[i], "%u: %02x != %02x\n", i, ciphertext[i], expected[i]);
888 /* input size is not a multiple of block size */
889 size = 0;
890 memcpy(ivbuf, iv, sizeof(iv));
891 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, NULL, 0, &size, 0);
892 ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %08x\n", ret);
893 ok(size == 17, "got %u\n", size);
895 /* input size is not a multiple of block size, block padding set */
896 size = 0;
897 memcpy(ivbuf, iv, sizeof(iv));
898 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
899 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
900 ok(size == 32, "got %u\n", size);
902 size = 0;
903 memcpy(ivbuf, iv, sizeof(iv));
904 memset(ciphertext, 0, sizeof(ciphertext));
905 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
906 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
907 ok(size == 32, "got %u\n", size);
908 ok(!memcmp(ciphertext, expected2, sizeof(expected2)), "wrong data\n");
909 for (i = 0; i < 32; i++)
910 ok(ciphertext[i] == expected2[i], "%u: %02x != %02x\n", i, ciphertext[i], expected2[i]);
912 /* input size is a multiple of block size, block padding set */
913 size = 0;
914 memcpy(ivbuf, iv, sizeof(iv));
915 ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
916 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
917 ok(size == 48, "got %u\n", size);
919 size = 0;
920 memcpy(ivbuf, iv, sizeof(iv));
921 memset(ciphertext, 0, sizeof(ciphertext));
922 ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 48, &size, BCRYPT_BLOCK_PADDING);
923 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
924 ok(size == 48, "got %u\n", size);
925 ok(!memcmp(ciphertext, expected3, sizeof(expected3)), "wrong data\n");
926 for (i = 0; i < 48; i++)
927 ok(ciphertext[i] == expected3[i], "%u: %02x != %02x\n", i, ciphertext[i], expected3[i]);
929 /* output size too small */
930 size = 0;
931 memcpy(ivbuf, iv, sizeof(iv));
932 memset(ciphertext, 0, sizeof(ciphertext));
933 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, ciphertext, 31, &size, BCRYPT_BLOCK_PADDING);
934 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
935 ok(size == 32, "got %u\n", size);
937 size = 0;
938 memcpy(ivbuf, iv, sizeof(iv));
939 memset(ciphertext, 0, sizeof(ciphertext));
940 ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
941 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
942 ok(size == 48, "got %u\n", size);
944 ret = pBCryptDestroyKey(key);
945 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
946 HeapFree(GetProcessHeap(), 0, buf);
948 ret = pBCryptCloseAlgorithmProvider(aes, 0);
949 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
952 static void test_BCryptDecrypt(void)
954 static UCHAR secret[] =
955 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
956 static UCHAR iv[] =
957 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
958 static UCHAR expected[] =
959 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
960 static UCHAR expected2[] =
961 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
962 static UCHAR expected3[] =
963 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
964 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
965 static UCHAR ciphertext[32] =
966 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
967 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
968 static UCHAR ciphertext2[] =
969 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
970 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
971 static UCHAR ciphertext3[] =
972 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
973 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
974 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
975 BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
976 BCRYPT_ALG_HANDLE aes;
977 BCRYPT_KEY_HANDLE key;
978 UCHAR *buf, plaintext[48], ivbuf[16];
979 ULONG size, len;
980 NTSTATUS ret;
982 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
983 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
985 size = 0;
986 memset(&key_lengths, 0, sizeof(key_lengths));
987 ret = BCryptGetProperty(aes, BCRYPT_KEY_LENGTHS, (UCHAR*)&key_lengths, sizeof(key_lengths), &size, 0);
988 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
989 ok(size == sizeof(key_lengths), "got %u\n", size);
990 ok(key_lengths.dwMinLength == 128, "Expected 128, got %d\n", key_lengths.dwMinLength);
991 ok(key_lengths.dwMaxLength == 256, "Expected 256, got %d\n", key_lengths.dwMaxLength);
992 ok(key_lengths.dwIncrement == 64, "Expected 64, got %d\n", key_lengths.dwIncrement);
994 len = 0xdeadbeef;
995 size = sizeof(len);
996 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
997 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
999 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
1000 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
1001 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1003 /* input size is a multiple of block size */
1004 size = 0;
1005 memcpy(ivbuf, iv, sizeof(iv));
1006 ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
1007 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1008 ok(size == 32, "got %u\n", size);
1010 size = 0;
1011 memcpy(ivbuf, iv, sizeof(iv));
1012 memset(plaintext, 0, sizeof(plaintext));
1013 ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, plaintext, 32, &size, 0);
1014 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1015 ok(size == 32, "got %u\n", size);
1016 ok(!memcmp(plaintext, expected, sizeof(expected)), "wrong data\n");
1018 /* test with padding smaller than block size */
1019 size = 0;
1020 memcpy(ivbuf, iv, sizeof(iv));
1021 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
1022 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1023 ok(size == 32, "got %u\n", size);
1025 size = 0;
1026 memcpy(ivbuf, iv, sizeof(iv));
1027 memset(plaintext, 0, sizeof(plaintext));
1028 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 17, &size, BCRYPT_BLOCK_PADDING);
1029 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1030 ok(size == 17, "got %u\n", size);
1031 ok(!memcmp(plaintext, expected2, sizeof(expected2)), "wrong data\n");
1033 /* test with padding of block size */
1034 size = 0;
1035 memcpy(ivbuf, iv, sizeof(iv));
1036 ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, NULL, 0, &size, 0);
1037 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1038 ok(size == 48, "got %u\n", size);
1040 size = 0;
1041 memcpy(ivbuf, iv, sizeof(iv));
1042 memset(plaintext, 0, sizeof(plaintext));
1043 ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 32, &size, BCRYPT_BLOCK_PADDING);
1044 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1045 ok(size == 32, "got %u\n", size);
1046 ok(!memcmp(plaintext, expected3, sizeof(expected3)), "wrong data\n");
1048 /* output size too small */
1049 size = 0;
1050 memcpy(ivbuf, iv, sizeof(iv));
1051 ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, plaintext, 31, &size, 0);
1052 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
1053 ok(size == 32, "got %u\n", size);
1055 size = 0;
1056 memcpy(ivbuf, iv, sizeof(iv));
1057 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 15, &size, BCRYPT_BLOCK_PADDING);
1058 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
1059 ok(size == 32, "got %u\n", size);
1061 size = 0;
1062 memcpy(ivbuf, iv, sizeof(iv));
1063 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 16, &size, BCRYPT_BLOCK_PADDING);
1064 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
1065 ok(size == 17, "got %u\n", size);
1067 size = 0;
1068 memcpy(ivbuf, iv, sizeof(iv));
1069 ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 31, &size, BCRYPT_BLOCK_PADDING);
1070 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
1071 ok(size == 48, "got %u\n", size);
1073 /* input size is not a multiple of block size */
1074 size = 0;
1075 memcpy(ivbuf, iv, sizeof(iv));
1076 ret = pBCryptDecrypt(key, ciphertext, 17, NULL, ivbuf, 16, NULL, 0, &size, 0);
1077 ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %08x\n", ret);
1078 ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %u\n", size);
1080 /* input size is not a multiple of block size, block padding set */
1081 size = 0;
1082 memcpy(ivbuf, iv, sizeof(iv));
1083 ret = pBCryptDecrypt(key, ciphertext, 17, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
1084 ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %08x\n", ret);
1085 ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %u\n", size);
1087 ret = pBCryptDestroyKey(key);
1088 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1089 HeapFree(GetProcessHeap(), 0, buf);
1091 ret = pBCryptCloseAlgorithmProvider(aes, 0);
1092 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1095 START_TEST(bcrypt)
1097 HMODULE module;
1099 module = LoadLibraryA("bcrypt.dll");
1100 if (!module)
1102 win_skip("bcrypt.dll not found\n");
1103 return;
1106 pBCryptOpenAlgorithmProvider = (void *)GetProcAddress(module, "BCryptOpenAlgorithmProvider");
1107 pBCryptCloseAlgorithmProvider = (void *)GetProcAddress(module, "BCryptCloseAlgorithmProvider");
1108 pBCryptGetFipsAlgorithmMode = (void *)GetProcAddress(module, "BCryptGetFipsAlgorithmMode");
1109 pBCryptCreateHash = (void *)GetProcAddress(module, "BCryptCreateHash");
1110 pBCryptHash = (void *)GetProcAddress(module, "BCryptHash");
1111 pBCryptHashData = (void *)GetProcAddress(module, "BCryptHashData");
1112 pBCryptDuplicateHash = (void *)GetProcAddress(module, "BCryptDuplicateHash");
1113 pBCryptFinishHash = (void *)GetProcAddress(module, "BCryptFinishHash");
1114 pBCryptDestroyHash = (void *)GetProcAddress(module, "BCryptDestroyHash");
1115 pBCryptGenRandom = (void *)GetProcAddress(module, "BCryptGenRandom");
1116 pBCryptGetProperty = (void *)GetProcAddress(module, "BCryptGetProperty");
1117 pBCryptSetProperty = (void *)GetProcAddress(module, "BCryptSetProperty");
1118 pBCryptGenerateSymmetricKey = (void *)GetProcAddress(module, "BCryptGenerateSymmetricKey");
1119 pBCryptEncrypt = (void *)GetProcAddress(module, "BCryptEncrypt");
1120 pBCryptDecrypt = (void *)GetProcAddress(module, "BCryptDecrypt");
1121 pBCryptDestroyKey = (void *)GetProcAddress(module, "BCryptDestroyKey");
1123 test_BCryptGenRandom();
1124 test_BCryptGetFipsAlgorithmMode();
1125 test_sha1();
1126 test_sha256();
1127 test_sha384();
1128 test_sha512();
1129 test_md5();
1130 test_rng();
1131 test_aes();
1132 test_BCryptGenerateSymmetricKey();
1133 test_BCryptEncrypt();
1134 test_BCryptDecrypt();
1136 if (pBCryptHash) /* >= Win 10 */
1137 test_BcryptHash();
1138 else
1139 win_skip("BCryptHash is not available\n");
1141 FreeLibrary(module);