bcrypt: Partial implementation of BCryptImportKey and BCryptExportKey.
[wine.git] / dlls / bcrypt / tests / bcrypt.c
blob6e283487b5d0e373b33e3c4b97bc5fbf0f3d024d
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);
49 static NTSTATUS (WINAPI *pBCryptImportKey)(BCRYPT_ALG_HANDLE, BCRYPT_KEY_HANDLE, LPCWSTR, BCRYPT_KEY_HANDLE *,
50 PUCHAR, ULONG, PUCHAR, ULONG, ULONG);
51 static NTSTATUS (WINAPI *pBCryptExportKey)(BCRYPT_KEY_HANDLE, BCRYPT_KEY_HANDLE, LPCWSTR, PUCHAR, ULONG, ULONG *, ULONG);
53 static void test_BCryptGenRandom(void)
55 NTSTATUS ret;
56 UCHAR buffer[256];
58 ret = pBCryptGenRandom(NULL, NULL, 0, 0);
59 ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got 0x%x\n", ret);
60 ret = pBCryptGenRandom(NULL, buffer, 0, 0);
61 ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got 0x%x\n", ret);
62 ret = pBCryptGenRandom(NULL, buffer, sizeof(buffer), 0);
63 ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got 0x%x\n", ret);
64 ret = pBCryptGenRandom(NULL, buffer, sizeof(buffer), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
65 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
66 ret = pBCryptGenRandom(NULL, buffer, sizeof(buffer),
67 BCRYPT_USE_SYSTEM_PREFERRED_RNG|BCRYPT_RNG_USE_ENTROPY_IN_BUFFER);
68 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
69 ret = pBCryptGenRandom(NULL, NULL, sizeof(buffer), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
70 ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got 0x%x\n", ret);
72 /* Zero sized buffer should work too */
73 ret = pBCryptGenRandom(NULL, buffer, 0, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
74 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
76 /* Test random number generation - It's impossible for a sane RNG to return 8 zeros */
77 memset(buffer, 0, 16);
78 ret = pBCryptGenRandom(NULL, buffer, 8, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
79 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
80 ok(memcmp(buffer, buffer + 8, 8), "Expected a random number, got 0\n");
83 static void test_BCryptGetFipsAlgorithmMode(void)
85 static const WCHAR policyKeyVistaW[] = {
86 'S','y','s','t','e','m','\\',
87 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
88 'C','o','n','t','r','o','l','\\',
89 'L','s','a','\\',
90 'F','I','P','S','A','l','g','o','r','i','t','h','m','P','o','l','i','c','y',0};
91 static const WCHAR policyValueVistaW[] = {'E','n','a','b','l','e','d',0};
92 static const WCHAR policyKeyXPW[] = {
93 'S','y','s','t','e','m','\\',
94 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
95 'C','o','n','t','r','o','l','\\',
96 'L','s','a',0};
97 static const WCHAR policyValueXPW[] = {
98 'F','I','P','S','A','l','g','o','r','i','t','h','m','P','o','l','i','c','y',0};
99 HKEY hkey = NULL;
100 BOOLEAN expected;
101 BOOLEAN enabled;
102 DWORD value, count[2] = {sizeof(value), sizeof(value)};
103 NTSTATUS ret;
105 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, policyKeyVistaW, &hkey) == ERROR_SUCCESS &&
106 RegQueryValueExW(hkey, policyValueVistaW, NULL, NULL, (void *)&value, &count[0]) == ERROR_SUCCESS)
108 expected = !!value;
110 else if (RegOpenKeyW(HKEY_LOCAL_MACHINE, policyKeyXPW, &hkey) == ERROR_SUCCESS &&
111 RegQueryValueExW(hkey, policyValueXPW, NULL, NULL, (void *)&value, &count[0]) == ERROR_SUCCESS)
113 expected = !!value;
115 else
117 expected = FALSE;
118 todo_wine
119 ok(0, "Neither XP or Vista key is present\n");
121 RegCloseKey(hkey);
123 ret = pBCryptGetFipsAlgorithmMode(&enabled);
124 ok(ret == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got 0x%x\n", ret);
125 ok(enabled == expected, "expected result %d, got %d\n", expected, enabled);
127 ret = pBCryptGetFipsAlgorithmMode(NULL);
128 ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got 0x%x\n", ret);
131 static void format_hash(const UCHAR *bytes, ULONG size, char *buf)
133 ULONG i;
134 buf[0] = '\0';
135 for (i = 0; i < size; i++)
137 sprintf(buf + i * 2, "%02x", bytes[i]);
139 return;
142 static int strcmp_wa(const WCHAR *strw, const char *stra)
144 WCHAR buf[512];
145 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(buf[0]));
146 return lstrcmpW(strw, buf);
149 #define test_object_length(a) _test_object_length(__LINE__,a)
150 static void _test_object_length(unsigned line, void *handle)
152 NTSTATUS status;
153 ULONG len, size;
155 len = size = 0xdeadbeef;
156 status = pBCryptGetProperty(NULL, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
157 ok_(__FILE__,line)(status == STATUS_INVALID_HANDLE, "BCryptGetProperty failed: %08x\n", status);
159 len = size = 0xdeadbeef;
160 status = pBCryptGetProperty(handle, NULL, (UCHAR *)&len, sizeof(len), &size, 0);
161 ok_(__FILE__,line)(status == STATUS_INVALID_PARAMETER, "BCryptGetProperty failed: %08x\n", status);
163 len = size = 0xdeadbeef;
164 status = pBCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), NULL, 0);
165 ok_(__FILE__,line)(status == STATUS_INVALID_PARAMETER, "BCryptGetProperty failed: %08x\n", status);
167 len = size = 0xdeadbeef;
168 status = pBCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, NULL, sizeof(len), &size, 0);
169 ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %08x\n", status);
170 ok_(__FILE__,line)(size == sizeof(len), "got %u\n", size);
172 len = size = 0xdeadbeef;
173 status = pBCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, 0, &size, 0);
174 ok_(__FILE__,line)(status == STATUS_BUFFER_TOO_SMALL, "BCryptGetProperty failed: %08x\n", status);
175 ok_(__FILE__,line)(len == 0xdeadbeef, "got %u\n", len);
176 ok_(__FILE__,line)(size == sizeof(len), "got %u\n", size);
178 len = size = 0xdeadbeef;
179 status = pBCryptGetProperty(handle, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
180 ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %08x\n", status);
181 ok_(__FILE__,line)(len != 0xdeadbeef, "len not set\n");
182 ok_(__FILE__,line)(size == sizeof(len), "got %u\n", size);
185 #define test_hash_length(a,b) _test_hash_length(__LINE__,a,b)
186 static void _test_hash_length(unsigned line, void *handle, ULONG exlen)
188 ULONG len = 0xdeadbeef, size = 0xdeadbeef;
189 NTSTATUS status;
191 status = pBCryptGetProperty(handle, BCRYPT_HASH_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
192 ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %08x\n", status);
193 ok_(__FILE__,line)(size == sizeof(len), "got %u\n", size);
194 ok_(__FILE__,line)(len == exlen, "len = %u, expected %u\n", len, exlen);
197 #define test_alg_name(a,b) _test_alg_name(__LINE__,a,b)
198 static void _test_alg_name(unsigned line, void *handle, const char *exname)
200 ULONG size = 0xdeadbeef;
201 UCHAR buf[256];
202 const WCHAR *name = (const WCHAR*)buf;
203 NTSTATUS status;
205 status = pBCryptGetProperty(handle, BCRYPT_ALGORITHM_NAME, buf, sizeof(buf), &size, 0);
206 ok_(__FILE__,line)(status == STATUS_SUCCESS, "BCryptGetProperty failed: %08x\n", status);
207 ok_(__FILE__,line)(size == (strlen(exname)+1)*sizeof(WCHAR), "got %u\n", size);
208 ok_(__FILE__,line)(!strcmp_wa(name, exname), "alg name = %s, expected %s\n", wine_dbgstr_w(name), exname);
211 struct hash_test
213 const char *alg;
214 unsigned hash_size;
215 const char *hash;
216 const char *hmac_hash;
219 static void test_hash(const struct hash_test *test)
221 BCRYPT_ALG_HANDLE alg;
222 BCRYPT_HASH_HANDLE hash;
223 UCHAR buf[512], buf_hmac[1024], hash_buf[128], hmac_hash[128];
224 WCHAR alg_name[64];
225 char str[512];
226 NTSTATUS ret;
227 ULONG len;
229 MultiByteToWideChar(CP_ACP, 0, test->alg, -1, alg_name, sizeof(alg_name)/sizeof(WCHAR));
231 alg = NULL;
232 ret = pBCryptOpenAlgorithmProvider(&alg, alg_name, MS_PRIMITIVE_PROVIDER, 0);
233 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
234 ok(alg != NULL, "alg not set\n");
236 test_object_length(alg);
237 test_hash_length(alg, test->hash_size);
238 test_alg_name(alg, test->alg);
240 hash = NULL;
241 len = sizeof(buf);
242 ret = pBCryptCreateHash(alg, &hash, buf, len, NULL, 0, 0);
243 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
244 ok(hash != NULL, "hash not set\n");
246 ret = pBCryptHashData(hash, NULL, 0, 0);
247 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
249 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
250 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
252 test_hash_length(hash, test->hash_size);
253 test_alg_name(hash, test->alg);
255 memset(hash_buf, 0, sizeof(hash_buf));
256 ret = pBCryptFinishHash(hash, hash_buf, test->hash_size, 0);
257 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
258 format_hash( hash_buf, test->hash_size, str );
259 ok(!strcmp(str, test->hash), "got %s\n", str);
261 ret = pBCryptDestroyHash(hash);
262 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
264 ret = pBCryptCloseAlgorithmProvider(alg, 0);
265 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
267 alg = NULL;
268 ret = pBCryptOpenAlgorithmProvider(&alg, alg_name, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
269 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
270 ok(alg != NULL, "alg not set\n");
272 hash = NULL;
273 len = sizeof(buf_hmac);
274 ret = pBCryptCreateHash(alg, &hash, buf_hmac, len, (UCHAR *)"key", sizeof("key"), 0);
275 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
276 ok(hash != NULL, "hash not set\n");
278 ret = pBCryptHashData(hash, (UCHAR *)"test", sizeof("test"), 0);
279 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
281 test_hash_length(hash, test->hash_size);
282 test_alg_name(hash, test->alg);
284 memset(hmac_hash, 0, sizeof(hmac_hash));
285 ret = pBCryptFinishHash(hash, hmac_hash, test->hash_size, 0);
286 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
287 format_hash( hmac_hash, test->hash_size, str );
288 ok(!strcmp(str, test->hmac_hash), "got %s\n", str);
290 ret = pBCryptDestroyHash(hash);
291 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
293 ret = pBCryptCloseAlgorithmProvider(alg, 0);
294 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
297 static void test_hashes(void)
299 static const struct hash_test tests[] = {
301 "SHA1",
303 "961fa64958818f767707072755d7018dcd278e94",
304 "2472cf65d0e090618d769d3e46f0d9446cf212da"
307 "SHA256",
309 "ceb73749c899693706ede1e30c9929b3fd5dd926163831c2fb8bd41e6efb1126",
310 "34c1aa473a4468a91d06e7cdbc75bc4f93b830ccfc2a47ffd74e8e6ed29e4c72"
313 "SHA384",
315 "62b21e90c9022b101671ba1f808f8631a8149f0f12904055839a35c1ca78ae53"
316 "63eed1e743a692d70e0504b0cfd12ef9",
317 "4b3e6d6ff2da121790ab7e7b9247583e3a7eed2db5bd4dabc680303b1608f37d"
318 "fdc836d96a704c03283bc05b4f6c5eb8"
321 "SHA512",
323 "d55ced17163bf5386f2cd9ff21d6fd7fe576a915065c24744d09cfae4ec84ee1"
324 "ef6ef11bfbc5acce3639bab725b50a1fe2c204f8c820d6d7db0df0ecbc49c5ca",
325 "415fb6b10018ca03b38a1b1399c42ac0be5e8aceddb9a73103f5e543bf2d888f"
326 "2eecf91373941f9315dd730a77937fa92444450fbece86f409d9cb5ec48c6513"
329 "MD2",
331 "1bb33606ba908912a84221109d29cd7e",
332 "7f05b0638d77f4a27f3a9c4d353cd648"
335 "MD4",
337 "74b5db93c0b41e36ca7074338fc0b637",
338 "bc2e8ac4d8248ed21b8d26227a30ea3a"
341 "MD5",
343 "e2a3e68d23ce348b8f68b3079de3d4c9",
344 "7bda029b93fa8d817fcc9e13d6bdf092"
347 unsigned i;
349 for(i = 0; i < sizeof(tests)/sizeof(*tests); i++)
350 test_hash(tests+i);
353 static void test_BcryptHash(void)
355 static const char expected[] =
356 "e2a3e68d23ce348b8f68b3079de3d4c9";
357 static const char expected_hmac[] =
358 "7bda029b93fa8d817fcc9e13d6bdf092";
359 BCRYPT_ALG_HANDLE alg;
360 UCHAR md5[16], md5_hmac[16];
361 char str[65];
362 NTSTATUS ret;
364 alg = NULL;
365 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
366 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
367 ok(alg != NULL, "alg not set\n");
369 test_hash_length(alg, 16);
370 test_alg_name(alg, "MD5");
372 memset(md5, 0, sizeof(md5));
373 ret = pBCryptHash(alg, NULL, 0, (UCHAR *)"test", sizeof("test"), md5, sizeof(md5));
374 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
375 format_hash( md5, sizeof(md5), str );
376 ok(!strcmp(str, expected), "got %s\n", str);
378 ret = pBCryptCloseAlgorithmProvider(alg, 0);
379 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
381 alg = NULL;
382 memset(md5_hmac, 0, sizeof(md5_hmac));
383 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
384 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
385 ok(alg != NULL, "alg not set\n");
387 ret = pBCryptHash(alg, (UCHAR *)"key", sizeof("key"), (UCHAR *)"test", sizeof("test"), md5_hmac, sizeof(md5_hmac));
388 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
389 format_hash( md5_hmac, sizeof(md5_hmac), str );
390 ok(!strcmp(str, expected_hmac), "got %s\n", str);
392 ret = pBCryptCloseAlgorithmProvider(alg, 0);
393 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
396 static void test_rng(void)
398 BCRYPT_ALG_HANDLE alg;
399 ULONG size, len;
400 UCHAR buf[16];
401 NTSTATUS ret;
403 alg = NULL;
404 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
405 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
406 ok(alg != NULL, "alg not set\n");
408 len = size = 0xdeadbeef;
409 ret = pBCryptGetProperty(alg, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
410 ok(ret == STATUS_NOT_SUPPORTED, "got %08x\n", ret);
412 len = size = 0xdeadbeef;
413 ret = pBCryptGetProperty(alg, BCRYPT_HASH_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
414 ok(ret == STATUS_NOT_SUPPORTED, "got %08x\n", ret);
416 test_alg_name(alg, "RNG");
418 memset(buf, 0, 16);
419 ret = pBCryptGenRandom(alg, buf, 8, 0);
420 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
421 ok(memcmp(buf, buf + 8, 8), "got zeroes\n");
423 ret = pBCryptCloseAlgorithmProvider(alg, 0);
424 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
427 static void test_aes(void)
429 BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
430 BCRYPT_ALG_HANDLE alg;
431 ULONG size, len;
432 UCHAR mode[64];
433 NTSTATUS ret;
435 alg = NULL;
436 ret = pBCryptOpenAlgorithmProvider(&alg, BCRYPT_AES_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
437 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
438 ok(alg != NULL, "alg not set\n");
440 len = size = 0;
441 ret = pBCryptGetProperty(alg, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
442 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
443 ok(len, "expected non-zero len\n");
444 ok(size == sizeof(len), "got %u\n", size);
446 len = size = 0;
447 ret = pBCryptGetProperty(alg, BCRYPT_BLOCK_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
448 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
449 ok(len == 16, "got %u\n", len);
450 ok(size == sizeof(len), "got %u\n", size);
452 size = 0;
453 ret = pBCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, 0, &size, 0);
454 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
455 ok(size == 64, "got %u\n", size);
457 size = 0;
458 ret = pBCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode) - 1, &size, 0);
459 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
460 ok(size == 64, "got %u\n", size);
462 size = 0;
463 memset(mode, 0, sizeof(mode));
464 ret = pBCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode), &size, 0);
465 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
466 ok(!lstrcmpW((const WCHAR *)mode, BCRYPT_CHAIN_MODE_CBC), "got %s\n", mode);
467 ok(size == 64, "got %u\n", size);
469 size = 0;
470 memset(&key_lengths, 0, sizeof(key_lengths));
471 ret = BCryptGetProperty(alg, BCRYPT_KEY_LENGTHS, (UCHAR*)&key_lengths, sizeof(key_lengths), &size, 0);
472 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
473 ok(size == sizeof(key_lengths), "got %u\n", size);
474 ok(key_lengths.dwMinLength == 128, "Expected 128, got %d\n", key_lengths.dwMinLength);
475 ok(key_lengths.dwMaxLength == 256, "Expected 256, got %d\n", key_lengths.dwMaxLength);
476 ok(key_lengths.dwIncrement == 64, "Expected 64, got %d\n", key_lengths.dwIncrement);
478 test_alg_name(alg, "AES");
480 ret = pBCryptCloseAlgorithmProvider(alg, 0);
481 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
484 static void test_BCryptGenerateSymmetricKey(void)
486 static UCHAR secret[] =
487 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
488 static UCHAR iv[] =
489 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
490 static UCHAR data[] =
491 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
492 static UCHAR expected[] =
493 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79};
494 BCRYPT_ALG_HANDLE aes;
495 BCRYPT_KEY_HANDLE key;
496 UCHAR *buf, ciphertext[16], plaintext[16], ivbuf[16];
497 ULONG size, len, i;
498 NTSTATUS ret;
500 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
501 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
503 len = size = 0xdeadbeef;
504 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
505 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
507 key = NULL;
508 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
509 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
510 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
511 ok(key != NULL, "key not set\n");
513 ret = pBCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR *)BCRYPT_CHAIN_MODE_CBC,
514 sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
515 todo_wine ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
517 size = 0xdeadbeef;
518 ret = pBCryptEncrypt(key, NULL, 0, NULL, NULL, 0, NULL, 0, &size, 0);
519 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
520 ok(!size, "got %u\n", size);
522 size = 0;
523 memcpy(ivbuf, iv, sizeof(iv));
524 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
525 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
526 ok(size == 16, "got %u\n", size);
528 size = 0;
529 memcpy(ivbuf, iv, sizeof(iv));
530 memset(ciphertext, 0, sizeof(ciphertext));
531 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
532 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
533 ok(size == 16, "got %u\n", size);
534 ok(!memcmp(ciphertext, expected, sizeof(expected)), "wrong data\n");
535 for (i = 0; i < 16; i++)
536 ok(ciphertext[i] == expected[i], "%u: %02x != %02x\n", i, ciphertext[i], expected[i]);
538 size = 0xdeadbeef;
539 ret = pBCryptDecrypt(key, NULL, 0, NULL, NULL, 0, NULL, 0, &size, 0);
540 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
541 ok(!size, "got %u\n", size);
543 size = 0;
544 memcpy(ivbuf, iv, sizeof(iv));
545 ret = pBCryptDecrypt(key, ciphertext, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
546 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
547 ok(size == 16, "got %u\n", size);
549 size = 0;
550 memcpy(ivbuf, iv, sizeof(iv));
551 memset(plaintext, 0, sizeof(plaintext));
552 ret = pBCryptDecrypt(key, ciphertext, 16, NULL, ivbuf, 16, plaintext, 16, &size, 0);
553 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
554 ok(size == 16, "got %u\n", size);
555 ok(!memcmp(plaintext, data, sizeof(data)), "wrong data\n");
557 ret = pBCryptDestroyKey(key);
558 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
559 HeapFree(GetProcessHeap(), 0, buf);
561 ret = pBCryptCloseAlgorithmProvider(aes, 0);
562 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
565 static void test_BCryptEncrypt(void)
567 static UCHAR secret[] =
568 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
569 static UCHAR iv[] =
570 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
571 static UCHAR data[] =
572 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
573 static UCHAR data2[] =
574 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
575 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
576 static UCHAR expected[] =
577 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79};
578 static UCHAR expected2[] =
579 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
580 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
581 static UCHAR expected3[] =
582 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
583 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
584 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
585 BCRYPT_ALG_HANDLE aes;
586 BCRYPT_KEY_HANDLE key;
587 UCHAR *buf, ciphertext[48], ivbuf[16];
588 ULONG size, len, i;
589 NTSTATUS ret;
591 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
592 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
594 len = 0xdeadbeef;
595 size = sizeof(len);
596 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
597 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
599 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
600 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
601 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
603 /* input size is a multiple of block size */
604 size = 0;
605 memcpy(ivbuf, iv, sizeof(iv));
606 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
607 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
608 ok(size == 16, "got %u\n", size);
610 size = 0;
611 memcpy(ivbuf, iv, sizeof(iv));
612 memset(ciphertext, 0, sizeof(ciphertext));
613 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
614 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
615 ok(size == 16, "got %u\n", size);
616 ok(!memcmp(ciphertext, expected, sizeof(expected)), "wrong data\n");
617 for (i = 0; i < 16; i++)
618 ok(ciphertext[i] == expected[i], "%u: %02x != %02x\n", i, ciphertext[i], expected[i]);
620 /* input size is not a multiple of block size */
621 size = 0;
622 memcpy(ivbuf, iv, sizeof(iv));
623 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, NULL, 0, &size, 0);
624 ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %08x\n", ret);
625 ok(size == 17, "got %u\n", size);
627 /* input size is not a multiple of block size, block padding set */
628 size = 0;
629 memcpy(ivbuf, iv, sizeof(iv));
630 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
631 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
632 ok(size == 32, "got %u\n", size);
634 size = 0;
635 memcpy(ivbuf, iv, sizeof(iv));
636 memset(ciphertext, 0, sizeof(ciphertext));
637 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
638 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
639 ok(size == 32, "got %u\n", size);
640 ok(!memcmp(ciphertext, expected2, sizeof(expected2)), "wrong data\n");
641 for (i = 0; i < 32; i++)
642 ok(ciphertext[i] == expected2[i], "%u: %02x != %02x\n", i, ciphertext[i], expected2[i]);
644 /* input size is a multiple of block size, block padding set */
645 size = 0;
646 memcpy(ivbuf, iv, sizeof(iv));
647 ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
648 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
649 ok(size == 48, "got %u\n", size);
651 size = 0;
652 memcpy(ivbuf, iv, sizeof(iv));
653 memset(ciphertext, 0, sizeof(ciphertext));
654 ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 48, &size, BCRYPT_BLOCK_PADDING);
655 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
656 ok(size == 48, "got %u\n", size);
657 ok(!memcmp(ciphertext, expected3, sizeof(expected3)), "wrong data\n");
658 for (i = 0; i < 48; i++)
659 ok(ciphertext[i] == expected3[i], "%u: %02x != %02x\n", i, ciphertext[i], expected3[i]);
661 /* output size too small */
662 size = 0;
663 memcpy(ivbuf, iv, sizeof(iv));
664 memset(ciphertext, 0, sizeof(ciphertext));
665 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, ciphertext, 31, &size, BCRYPT_BLOCK_PADDING);
666 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
667 ok(size == 32, "got %u\n", size);
669 size = 0;
670 memcpy(ivbuf, iv, sizeof(iv));
671 memset(ciphertext, 0, sizeof(ciphertext));
672 ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
673 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
674 ok(size == 48, "got %u\n", size);
676 ret = pBCryptDestroyKey(key);
677 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
678 HeapFree(GetProcessHeap(), 0, buf);
680 ret = pBCryptCloseAlgorithmProvider(aes, 0);
681 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
684 static void test_BCryptDecrypt(void)
686 static UCHAR secret[] =
687 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
688 static UCHAR iv[] =
689 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
690 static UCHAR expected[] =
691 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
692 static UCHAR expected2[] =
693 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
694 static UCHAR expected3[] =
695 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
696 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
697 static UCHAR ciphertext[32] =
698 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
699 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
700 static UCHAR ciphertext2[] =
701 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
702 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
703 static UCHAR ciphertext3[] =
704 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
705 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
706 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
707 BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
708 BCRYPT_ALG_HANDLE aes;
709 BCRYPT_KEY_HANDLE key;
710 UCHAR *buf, plaintext[48], ivbuf[16];
711 ULONG size, len;
712 NTSTATUS ret;
714 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
715 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
717 size = 0;
718 memset(&key_lengths, 0, sizeof(key_lengths));
719 ret = BCryptGetProperty(aes, BCRYPT_KEY_LENGTHS, (UCHAR*)&key_lengths, sizeof(key_lengths), &size, 0);
720 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
721 ok(size == sizeof(key_lengths), "got %u\n", size);
722 ok(key_lengths.dwMinLength == 128, "Expected 128, got %d\n", key_lengths.dwMinLength);
723 ok(key_lengths.dwMaxLength == 256, "Expected 256, got %d\n", key_lengths.dwMaxLength);
724 ok(key_lengths.dwIncrement == 64, "Expected 64, got %d\n", key_lengths.dwIncrement);
726 len = 0xdeadbeef;
727 size = sizeof(len);
728 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
729 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
731 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
732 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
733 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
735 /* input size is a multiple of block size */
736 size = 0;
737 memcpy(ivbuf, iv, sizeof(iv));
738 ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
739 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
740 ok(size == 32, "got %u\n", size);
742 size = 0;
743 memcpy(ivbuf, iv, sizeof(iv));
744 memset(plaintext, 0, sizeof(plaintext));
745 ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, plaintext, 32, &size, 0);
746 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
747 ok(size == 32, "got %u\n", size);
748 ok(!memcmp(plaintext, expected, sizeof(expected)), "wrong data\n");
750 /* test with padding smaller than block size */
751 size = 0;
752 memcpy(ivbuf, iv, sizeof(iv));
753 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
754 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
755 ok(size == 32, "got %u\n", size);
757 size = 0;
758 memcpy(ivbuf, iv, sizeof(iv));
759 memset(plaintext, 0, sizeof(plaintext));
760 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 17, &size, BCRYPT_BLOCK_PADDING);
761 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
762 ok(size == 17, "got %u\n", size);
763 ok(!memcmp(plaintext, expected2, sizeof(expected2)), "wrong data\n");
765 /* test with padding of block size */
766 size = 0;
767 memcpy(ivbuf, iv, sizeof(iv));
768 ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, NULL, 0, &size, 0);
769 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
770 ok(size == 48, "got %u\n", size);
772 size = 0;
773 memcpy(ivbuf, iv, sizeof(iv));
774 memset(plaintext, 0, sizeof(plaintext));
775 ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 32, &size, BCRYPT_BLOCK_PADDING);
776 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
777 ok(size == 32, "got %u\n", size);
778 ok(!memcmp(plaintext, expected3, sizeof(expected3)), "wrong data\n");
780 /* output size too small */
781 size = 0;
782 memcpy(ivbuf, iv, sizeof(iv));
783 ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, plaintext, 31, &size, 0);
784 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
785 ok(size == 32, "got %u\n", size);
787 size = 0;
788 memcpy(ivbuf, iv, sizeof(iv));
789 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 15, &size, BCRYPT_BLOCK_PADDING);
790 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
791 ok(size == 32, "got %u\n", size);
793 size = 0;
794 memcpy(ivbuf, iv, sizeof(iv));
795 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 16, &size, BCRYPT_BLOCK_PADDING);
796 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
797 ok(size == 17, "got %u\n", size);
799 size = 0;
800 memcpy(ivbuf, iv, sizeof(iv));
801 ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 31, &size, BCRYPT_BLOCK_PADDING);
802 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
803 ok(size == 48, "got %u\n", size);
805 /* input size is not a multiple of block size */
806 size = 0;
807 memcpy(ivbuf, iv, sizeof(iv));
808 ret = pBCryptDecrypt(key, ciphertext, 17, NULL, ivbuf, 16, NULL, 0, &size, 0);
809 ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %08x\n", ret);
810 ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %u\n", size);
812 /* input size is not a multiple of block size, block padding set */
813 size = 0;
814 memcpy(ivbuf, iv, sizeof(iv));
815 ret = pBCryptDecrypt(key, ciphertext, 17, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
816 ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %08x\n", ret);
817 ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %u\n", size);
819 ret = pBCryptDestroyKey(key);
820 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
821 HeapFree(GetProcessHeap(), 0, buf);
823 ret = pBCryptCloseAlgorithmProvider(aes, 0);
824 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
827 static void test_key_import_export(void)
829 UCHAR buffer1[sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + 16];
830 UCHAR buffer2[sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + 16];
831 BCRYPT_KEY_DATA_BLOB_HEADER *key_data1 = (void*)buffer1;
832 BCRYPT_ALG_HANDLE aes;
833 BCRYPT_KEY_HANDLE key;
834 NTSTATUS ret;
835 ULONG size;
837 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
838 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
840 key_data1->dwMagic = BCRYPT_KEY_DATA_BLOB_MAGIC;
841 key_data1->dwVersion = BCRYPT_KEY_DATA_BLOB_VERSION1;
842 key_data1->cbKeyData = 16;
843 memset(&key_data1[1], 0x11, 16);
845 ret = pBCryptImportKey(aes, NULL, BCRYPT_KEY_DATA_BLOB, &key, NULL, 0, buffer1, sizeof(buffer1), 0);
846 ok(ret == STATUS_SUCCESS || broken(ret == STATUS_INVALID_PARAMETER) /* vista */, "got %08x\n", ret);
847 if (ret == STATUS_INVALID_PARAMETER)
849 win_skip("broken BCryptImportKey\n");
850 return;
853 size = 0;
854 ret = pBCryptExportKey(key, NULL, BCRYPT_KEY_DATA_BLOB, buffer2, 1, &size, 0);
855 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
856 ok(size == sizeof(buffer2), "Got %u\n", size);
858 size = 0;
859 memset(buffer2, 0xff, sizeof(buffer2));
860 ret = pBCryptExportKey(key, NULL, BCRYPT_KEY_DATA_BLOB, buffer2, sizeof(buffer2), &size, 0);
861 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
862 ok(size == sizeof(buffer2), "Got %u\n", size);
863 ok(!memcmp(buffer1, buffer2, sizeof(buffer1)), "Expected exported key to match imported key\n");
865 ret = pBCryptDestroyKey(key);
866 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
868 ret = pBCryptCloseAlgorithmProvider(aes, 0);
869 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
872 START_TEST(bcrypt)
874 HMODULE module;
876 module = LoadLibraryA("bcrypt.dll");
877 if (!module)
879 win_skip("bcrypt.dll not found\n");
880 return;
883 pBCryptOpenAlgorithmProvider = (void *)GetProcAddress(module, "BCryptOpenAlgorithmProvider");
884 pBCryptCloseAlgorithmProvider = (void *)GetProcAddress(module, "BCryptCloseAlgorithmProvider");
885 pBCryptGetFipsAlgorithmMode = (void *)GetProcAddress(module, "BCryptGetFipsAlgorithmMode");
886 pBCryptCreateHash = (void *)GetProcAddress(module, "BCryptCreateHash");
887 pBCryptHash = (void *)GetProcAddress(module, "BCryptHash");
888 pBCryptHashData = (void *)GetProcAddress(module, "BCryptHashData");
889 pBCryptDuplicateHash = (void *)GetProcAddress(module, "BCryptDuplicateHash");
890 pBCryptFinishHash = (void *)GetProcAddress(module, "BCryptFinishHash");
891 pBCryptDestroyHash = (void *)GetProcAddress(module, "BCryptDestroyHash");
892 pBCryptGenRandom = (void *)GetProcAddress(module, "BCryptGenRandom");
893 pBCryptGetProperty = (void *)GetProcAddress(module, "BCryptGetProperty");
894 pBCryptSetProperty = (void *)GetProcAddress(module, "BCryptSetProperty");
895 pBCryptGenerateSymmetricKey = (void *)GetProcAddress(module, "BCryptGenerateSymmetricKey");
896 pBCryptEncrypt = (void *)GetProcAddress(module, "BCryptEncrypt");
897 pBCryptDecrypt = (void *)GetProcAddress(module, "BCryptDecrypt");
898 pBCryptDestroyKey = (void *)GetProcAddress(module, "BCryptDestroyKey");
899 pBCryptImportKey = (void *)GetProcAddress(module, "BCryptImportKey");
900 pBCryptExportKey = (void *)GetProcAddress(module, "BCryptExportKey");
902 test_BCryptGenRandom();
903 test_BCryptGetFipsAlgorithmMode();
904 test_hashes();
905 test_rng();
906 test_aes();
907 test_BCryptGenerateSymmetricKey();
908 test_BCryptEncrypt();
909 test_BCryptDecrypt();
910 test_key_import_export();
912 if (pBCryptHash) /* >= Win 10 */
913 test_BcryptHash();
914 else
915 win_skip("BCryptHash is not available\n");
917 FreeLibrary(module);