bcrypt: Implement BCryptGetProperty for BCRYPT_AUTH_TAG_LENGTH.
[wine.git] / dlls / bcrypt / tests / bcrypt.c
blobba2011f5dfc9e4eb804b195095ab9ff59af37b83
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", wine_dbgstr_w((const WCHAR *)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 memcpy(mode, BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM));
479 ret = pBCryptSetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode), 0);
480 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
482 size = 0;
483 memset(mode, 0, sizeof(mode));
484 ret = pBCryptGetProperty(alg, BCRYPT_CHAINING_MODE, mode, sizeof(mode), &size, 0);
485 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
486 ok(!lstrcmpW((const WCHAR *)mode, BCRYPT_CHAIN_MODE_GCM), "got %s\n", wine_dbgstr_w((const WCHAR *)mode));
487 ok(size == 64, "got %u\n", size);
489 test_alg_name(alg, "AES");
491 ret = pBCryptCloseAlgorithmProvider(alg, 0);
492 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
495 static void test_BCryptGenerateSymmetricKey(void)
497 static UCHAR secret[] =
498 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
499 static UCHAR iv[] =
500 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
501 static UCHAR data[] =
502 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
503 static UCHAR expected[] =
504 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79};
505 BCRYPT_ALG_HANDLE aes;
506 BCRYPT_KEY_HANDLE key;
507 UCHAR *buf, ciphertext[16], plaintext[16], ivbuf[16];
508 ULONG size, len, i;
509 NTSTATUS ret;
511 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
512 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
514 len = size = 0xdeadbeef;
515 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
516 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
518 key = NULL;
519 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
520 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
521 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
522 ok(key != NULL, "key not set\n");
524 ret = pBCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR *)BCRYPT_CHAIN_MODE_CBC,
525 sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
526 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
528 size = 0xdeadbeef;
529 ret = pBCryptEncrypt(key, NULL, 0, NULL, NULL, 0, NULL, 0, &size, 0);
530 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
531 ok(!size, "got %u\n", size);
533 size = 0;
534 memcpy(ivbuf, iv, sizeof(iv));
535 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
536 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
537 ok(size == 16, "got %u\n", size);
539 size = 0;
540 memcpy(ivbuf, iv, sizeof(iv));
541 memset(ciphertext, 0, sizeof(ciphertext));
542 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
543 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
544 ok(size == 16, "got %u\n", size);
545 ok(!memcmp(ciphertext, expected, sizeof(expected)), "wrong data\n");
546 for (i = 0; i < 16; i++)
547 ok(ciphertext[i] == expected[i], "%u: %02x != %02x\n", i, ciphertext[i], expected[i]);
549 size = 0xdeadbeef;
550 ret = pBCryptDecrypt(key, NULL, 0, NULL, NULL, 0, NULL, 0, &size, 0);
551 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
552 ok(!size, "got %u\n", size);
554 size = 0;
555 memcpy(ivbuf, iv, sizeof(iv));
556 ret = pBCryptDecrypt(key, ciphertext, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
557 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
558 ok(size == 16, "got %u\n", size);
560 size = 0;
561 memcpy(ivbuf, iv, sizeof(iv));
562 memset(plaintext, 0, sizeof(plaintext));
563 ret = pBCryptDecrypt(key, ciphertext, 16, NULL, ivbuf, 16, plaintext, 16, &size, 0);
564 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
565 ok(size == 16, "got %u\n", size);
566 ok(!memcmp(plaintext, data, sizeof(data)), "wrong data\n");
568 ret = pBCryptDestroyKey(key);
569 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
570 HeapFree(GetProcessHeap(), 0, buf);
572 ret = pBCryptCloseAlgorithmProvider(aes, 0);
573 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
576 static void test_BCryptEncrypt(void)
578 static UCHAR nonce[] =
579 {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60};
580 static UCHAR secret[] =
581 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
582 static UCHAR iv[] =
583 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
584 static UCHAR data[] =
585 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
586 static UCHAR data2[] =
587 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
588 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
589 static UCHAR expected[] =
590 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79};
591 static UCHAR expected2[] =
592 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
593 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
594 static UCHAR expected3[] =
595 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
596 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
597 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
598 static UCHAR expected4[] =
599 {0xe1,0x82,0xc3,0xc0,0x24,0xfb,0x86,0x85,0xf3,0xf1,0x2b,0x7d,0x09,0xb4,0x73,0x67,
600 0x86,0x64,0xc3,0xfe,0xa3,0x07,0x61,0xf8,0x16,0xc9,0x78,0x7f,0xe7,0xb1,0xc4,0x94};
601 static UCHAR expected_tag[] =
602 {0x89,0xb3,0x92,0x00,0x39,0x20,0x09,0xb4,0x6a,0xd6,0xaf,0xca,0x4b,0x5b,0xfd,0xd0};
603 static UCHAR expected_tag2[] =
604 {0x9a,0x92,0x32,0x2c,0x61,0x2a,0xae,0xef,0x66,0x2a,0xfb,0x55,0xe9,0x48,0xdf,0xbd};
605 BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO auth_info;
606 UCHAR *buf, ciphertext[48], ivbuf[16], tag[16];
607 BCRYPT_AUTH_TAG_LENGTHS_STRUCT tag_length;
608 BCRYPT_ALG_HANDLE aes;
609 BCRYPT_KEY_HANDLE key;
610 ULONG size, len, i;
611 NTSTATUS ret;
613 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
614 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
616 /******************
617 * AES - CBC mode *
618 ******************/
620 len = 0xdeadbeef;
621 size = sizeof(len);
622 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
623 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
625 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
626 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
627 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
629 /* input size is a multiple of block size */
630 size = 0;
631 memcpy(ivbuf, iv, sizeof(iv));
632 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, NULL, 0, &size, 0);
633 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
634 ok(size == 16, "got %u\n", size);
636 size = 0;
637 memcpy(ivbuf, iv, sizeof(iv));
638 memset(ciphertext, 0, sizeof(ciphertext));
639 ret = pBCryptEncrypt(key, data, 16, NULL, ivbuf, 16, ciphertext, 16, &size, 0);
640 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
641 ok(size == 16, "got %u\n", size);
642 ok(!memcmp(ciphertext, expected, sizeof(expected)), "wrong data\n");
643 for (i = 0; i < 16; i++)
644 ok(ciphertext[i] == expected[i], "%u: %02x != %02x\n", i, ciphertext[i], expected[i]);
646 /* input size is not a multiple of block size */
647 size = 0;
648 memcpy(ivbuf, iv, sizeof(iv));
649 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, NULL, 0, &size, 0);
650 ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %08x\n", ret);
651 ok(size == 17, "got %u\n", size);
653 /* input size is not a multiple of block size, block padding set */
654 size = 0;
655 memcpy(ivbuf, iv, sizeof(iv));
656 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
657 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
658 ok(size == 32, "got %u\n", size);
660 size = 0;
661 memcpy(ivbuf, iv, sizeof(iv));
662 memset(ciphertext, 0, sizeof(ciphertext));
663 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
664 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
665 ok(size == 32, "got %u\n", size);
666 ok(!memcmp(ciphertext, expected2, sizeof(expected2)), "wrong data\n");
667 for (i = 0; i < 32; i++)
668 ok(ciphertext[i] == expected2[i], "%u: %02x != %02x\n", i, ciphertext[i], expected2[i]);
670 /* input size is a multiple of block size, block padding set */
671 size = 0;
672 memcpy(ivbuf, iv, sizeof(iv));
673 ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
674 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
675 ok(size == 48, "got %u\n", size);
677 size = 0;
678 memcpy(ivbuf, iv, sizeof(iv));
679 memset(ciphertext, 0, sizeof(ciphertext));
680 ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 48, &size, BCRYPT_BLOCK_PADDING);
681 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
682 ok(size == 48, "got %u\n", size);
683 ok(!memcmp(ciphertext, expected3, sizeof(expected3)), "wrong data\n");
684 for (i = 0; i < 48; i++)
685 ok(ciphertext[i] == expected3[i], "%u: %02x != %02x\n", i, ciphertext[i], expected3[i]);
687 /* output size too small */
688 size = 0;
689 memcpy(ivbuf, iv, sizeof(iv));
690 memset(ciphertext, 0, sizeof(ciphertext));
691 ret = pBCryptEncrypt(key, data, 17, NULL, ivbuf, 16, ciphertext, 31, &size, BCRYPT_BLOCK_PADDING);
692 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
693 ok(size == 32, "got %u\n", size);
695 size = 0;
696 memcpy(ivbuf, iv, sizeof(iv));
697 memset(ciphertext, 0, sizeof(ciphertext));
698 ret = pBCryptEncrypt(key, data2, 32, NULL, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
699 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
700 ok(size == 48, "got %u\n", size);
702 ret = pBCryptDestroyKey(key);
703 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
704 HeapFree(GetProcessHeap(), 0, buf);
706 /******************
707 * AES - GCM mode *
708 ******************/
710 size = 0;
711 ret = BCryptGetProperty(aes, BCRYPT_AUTH_TAG_LENGTH, NULL, 0, &size, 0);
712 ok(ret == STATUS_NOT_SUPPORTED, "got %08x\n", ret);
714 ret = BCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR*)BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM), 0);
715 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
717 size = 0;
718 ret = BCryptGetProperty(aes, BCRYPT_AUTH_TAG_LENGTH, NULL, 0, &size, 0);
719 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
720 ok(size == sizeof(tag_length), "got %u\n", size);
722 size = 0;
723 memset(&tag_length, 0, sizeof(tag_length));
724 ret = BCryptGetProperty(aes, BCRYPT_AUTH_TAG_LENGTH, (UCHAR*)&tag_length, sizeof(tag_length), &size, 0);
725 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
726 ok(size == sizeof(tag_length), "got %u\n", size);
727 ok(tag_length.dwMinLength == 12, "Expected 12, got %d\n", tag_length.dwMinLength);
728 ok(tag_length.dwMaxLength == 16, "Expected 16, got %d\n", tag_length.dwMaxLength);
729 ok(tag_length.dwIncrement == 1, "Expected 1, got %d\n", tag_length.dwIncrement);
731 len = 0xdeadbeef;
732 size = sizeof(len);
733 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
734 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
736 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
737 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
738 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
740 memset(&auth_info, 0, sizeof(auth_info));
741 auth_info.cbSize = sizeof(auth_info);
742 auth_info.dwInfoVersion = 1;
743 auth_info.pbNonce = nonce;
744 auth_info.cbNonce = sizeof(nonce);
745 auth_info.pbTag = tag;
746 auth_info.cbTag = sizeof(tag);
748 /* input size is a multiple of block size */
749 size = 0;
750 memcpy(ivbuf, iv, sizeof(iv));
751 memset(ciphertext, 0xff, sizeof(ciphertext));
752 memset(tag, 0xff, sizeof(tag));
753 ret = pBCryptEncrypt(key, data2, 32, &auth_info, ivbuf, 16, ciphertext, 32, &size, 0);
754 todo_wine ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
755 todo_wine ok(size == 32, "got %u\n", size);
756 todo_wine ok(!memcmp(ciphertext, expected4, sizeof(expected4)), "wrong data\n");
757 todo_wine ok(!memcmp(tag, expected_tag, sizeof(expected_tag)), "wrong tag\n");
758 for (i = 0; i < 32; i++)
759 todo_wine ok(ciphertext[i] == expected4[i], "%u: %02x != %02x\n", i, ciphertext[i], expected4[i]);
760 for (i = 0; i < 16; i++)
761 todo_wine ok(tag[i] == expected_tag[i], "%u: %02x != %02x\n", i, tag[i], expected_tag[i]);
763 /* input size is not multiple of block size */
764 size = 0;
765 memcpy(ivbuf, iv, sizeof(iv));
766 memset(ciphertext, 0xff, sizeof(ciphertext));
767 memset(tag, 0xff, sizeof(tag));
768 ret = pBCryptEncrypt(key, data2, 24, &auth_info, ivbuf, 16, ciphertext, 24, &size, 0);
769 todo_wine ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
770 todo_wine ok(size == 24, "got %u\n", size);
771 todo_wine ok(!memcmp(ciphertext, expected4, 24), "wrong data\n");
772 todo_wine ok(!memcmp(tag, expected_tag2, sizeof(expected_tag2)), "wrong tag\n");
773 for (i = 0; i < 24; i++)
774 todo_wine ok(ciphertext[i] == expected4[i], "%u: %02x != %02x\n", i, ciphertext[i], expected4[i]);
775 for (i = 0; i < 16; i++)
776 todo_wine ok(tag[i] == expected_tag2[i], "%u: %02x != %02x\n", i, tag[i], expected_tag2[i]);
778 /* test with padding */
779 memcpy(ivbuf, iv, sizeof(iv));
780 memset(ciphertext, 0, sizeof(ciphertext));
781 ret = pBCryptEncrypt(key, data2, 32, &auth_info, ivbuf, 16, ciphertext, 32, &size, BCRYPT_BLOCK_PADDING);
782 todo_wine ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
784 memcpy(ivbuf, iv, sizeof(iv));
785 memset(ciphertext, 0, sizeof(ciphertext));
786 ret = pBCryptEncrypt(key, data2, 32, &auth_info, ivbuf, 16, ciphertext, 48, &size, BCRYPT_BLOCK_PADDING);
787 todo_wine ok(ret == STATUS_INVALID_PARAMETER, "got %08x\n", ret);
789 ret = pBCryptDestroyKey(key);
790 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
791 HeapFree(GetProcessHeap(), 0, buf);
793 ret = pBCryptCloseAlgorithmProvider(aes, 0);
794 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
797 static void test_BCryptDecrypt(void)
799 static UCHAR nonce[] =
800 {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60};
801 static UCHAR secret[] =
802 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
803 static UCHAR iv[] =
804 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
805 static UCHAR expected[] =
806 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
807 static UCHAR expected2[] =
808 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
809 static UCHAR expected3[] =
810 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
811 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
812 static UCHAR ciphertext[32] =
813 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
814 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
815 static UCHAR ciphertext2[] =
816 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
817 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
818 static UCHAR ciphertext3[] =
819 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
820 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
821 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
822 static UCHAR ciphertext4[] =
823 {0xe1,0x82,0xc3,0xc0,0x24,0xfb,0x86,0x85,0xf3,0xf1,0x2b,0x7d,0x09,0xb4,0x73,0x67,
824 0x86,0x64,0xc3,0xfe,0xa3,0x07,0x61,0xf8,0x16,0xc9,0x78,0x7f,0xe7,0xb1,0xc4,0x94};
825 static UCHAR tag[] =
826 {0x89,0xb3,0x92,0x00,0x39,0x20,0x09,0xb4,0x6a,0xd6,0xaf,0xca,0x4b,0x5b,0xfd,0xd0};
827 BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO auth_info;
828 BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
829 BCRYPT_ALG_HANDLE aes;
830 BCRYPT_KEY_HANDLE key;
831 UCHAR *buf, plaintext[48], ivbuf[16];
832 ULONG size, len;
833 NTSTATUS ret;
835 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
836 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
838 size = 0;
839 memset(&key_lengths, 0, sizeof(key_lengths));
840 ret = BCryptGetProperty(aes, BCRYPT_KEY_LENGTHS, (UCHAR*)&key_lengths, sizeof(key_lengths), &size, 0);
841 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
842 ok(size == sizeof(key_lengths), "got %u\n", size);
843 ok(key_lengths.dwMinLength == 128, "Expected 128, got %d\n", key_lengths.dwMinLength);
844 ok(key_lengths.dwMaxLength == 256, "Expected 256, got %d\n", key_lengths.dwMaxLength);
845 ok(key_lengths.dwIncrement == 64, "Expected 64, got %d\n", key_lengths.dwIncrement);
847 /******************
848 * AES - CBC mode *
849 ******************/
851 len = 0xdeadbeef;
852 size = sizeof(len);
853 ret = pBCryptGetProperty(aes, BCRYPT_OBJECT_LENGTH, (UCHAR *)&len, sizeof(len), &size, 0);
854 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
856 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
857 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
858 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
860 /* input size is a multiple of block size */
861 size = 0;
862 memcpy(ivbuf, iv, sizeof(iv));
863 ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
864 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
865 ok(size == 32, "got %u\n", size);
867 size = 0;
868 memcpy(ivbuf, iv, sizeof(iv));
869 memset(plaintext, 0, sizeof(plaintext));
870 ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, plaintext, 32, &size, 0);
871 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
872 ok(size == 32, "got %u\n", size);
873 ok(!memcmp(plaintext, expected, sizeof(expected)), "wrong data\n");
875 /* test with padding smaller than block size */
876 size = 0;
877 memcpy(ivbuf, iv, sizeof(iv));
878 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, NULL, 0, &size, 0);
879 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
880 ok(size == 32, "got %u\n", size);
882 size = 0;
883 memcpy(ivbuf, iv, sizeof(iv));
884 memset(plaintext, 0, sizeof(plaintext));
885 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 17, &size, BCRYPT_BLOCK_PADDING);
886 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
887 ok(size == 17, "got %u\n", size);
888 ok(!memcmp(plaintext, expected2, sizeof(expected2)), "wrong data\n");
890 /* test with padding of block size */
891 size = 0;
892 memcpy(ivbuf, iv, sizeof(iv));
893 ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, NULL, 0, &size, 0);
894 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
895 ok(size == 48, "got %u\n", size);
897 size = 0;
898 memcpy(ivbuf, iv, sizeof(iv));
899 memset(plaintext, 0, sizeof(plaintext));
900 ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 32, &size, BCRYPT_BLOCK_PADDING);
901 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
902 ok(size == 32, "got %u\n", size);
903 ok(!memcmp(plaintext, expected3, sizeof(expected3)), "wrong data\n");
905 /* output size too small */
906 size = 0;
907 memcpy(ivbuf, iv, sizeof(iv));
908 ret = pBCryptDecrypt(key, ciphertext, 32, NULL, ivbuf, 16, plaintext, 31, &size, 0);
909 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
910 ok(size == 32, "got %u\n", size);
912 size = 0;
913 memcpy(ivbuf, iv, sizeof(iv));
914 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 15, &size, BCRYPT_BLOCK_PADDING);
915 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
916 ok(size == 32, "got %u\n", size);
918 size = 0;
919 memcpy(ivbuf, iv, sizeof(iv));
920 ret = pBCryptDecrypt(key, ciphertext2, 32, NULL, ivbuf, 16, plaintext, 16, &size, BCRYPT_BLOCK_PADDING);
921 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
922 ok(size == 17, "got %u\n", size);
924 size = 0;
925 memcpy(ivbuf, iv, sizeof(iv));
926 ret = pBCryptDecrypt(key, ciphertext3, 48, NULL, ivbuf, 16, plaintext, 31, &size, BCRYPT_BLOCK_PADDING);
927 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
928 ok(size == 48, "got %u\n", size);
930 /* input size is not a multiple of block size */
931 size = 0;
932 memcpy(ivbuf, iv, sizeof(iv));
933 ret = pBCryptDecrypt(key, ciphertext, 17, NULL, ivbuf, 16, NULL, 0, &size, 0);
934 ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %08x\n", ret);
935 ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %u\n", size);
937 /* input size is not a multiple of block size, block padding set */
938 size = 0;
939 memcpy(ivbuf, iv, sizeof(iv));
940 ret = pBCryptDecrypt(key, ciphertext, 17, NULL, ivbuf, 16, NULL, 0, &size, BCRYPT_BLOCK_PADDING);
941 ok(ret == STATUS_INVALID_BUFFER_SIZE, "got %08x\n", ret);
942 ok(size == 17 || broken(size == 0 /* Win < 7 */), "got %u\n", size);
944 ret = pBCryptDestroyKey(key);
945 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
946 HeapFree(GetProcessHeap(), 0, buf);
948 /******************
949 * AES - GCM mode *
950 ******************/
952 ret = BCryptSetProperty(aes, BCRYPT_CHAINING_MODE, (UCHAR*)BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM), 0);
953 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
955 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
956 ret = pBCryptGenerateSymmetricKey(aes, &key, buf, len, secret, sizeof(secret), 0);
957 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
959 memset(&auth_info, 0, sizeof(auth_info));
960 auth_info.cbSize = sizeof(auth_info);
961 auth_info.dwInfoVersion = 1;
962 auth_info.pbNonce = nonce;
963 auth_info.cbNonce = sizeof(nonce);
964 auth_info.pbTag = tag;
965 auth_info.cbTag = sizeof(tag);
967 /* input size is a multiple of block size */
968 size = 0;
969 memcpy(ivbuf, iv, sizeof(iv));
970 memset(plaintext, 0, sizeof(plaintext));
971 ret = pBCryptDecrypt(key, ciphertext4, 32, &auth_info, ivbuf, 16, plaintext, 32, &size, 0);
972 todo_wine ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
973 todo_wine ok(size == 32, "got %u\n", size);
974 todo_wine ok(!memcmp(plaintext, expected3, sizeof(expected3)), "wrong data\n");
976 /* test with wrong tag */
977 memcpy(ivbuf, iv, sizeof(iv));
978 auth_info.pbTag = iv; /* wrong tag */
979 ret = pBCryptDecrypt(key, ciphertext4, 32, &auth_info, ivbuf, 16, plaintext, 32, &size, 0);
980 todo_wine ok(ret == STATUS_AUTH_TAG_MISMATCH, "got %08x\n", ret);
981 todo_wine ok(size == 32, "got %u\n", size);
983 ret = pBCryptDestroyKey(key);
984 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
985 HeapFree(GetProcessHeap(), 0, buf);
987 ret = pBCryptCloseAlgorithmProvider(aes, 0);
988 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
991 static void test_key_import_export(void)
993 UCHAR buffer1[sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + 16];
994 UCHAR buffer2[sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + 16];
995 BCRYPT_KEY_DATA_BLOB_HEADER *key_data1 = (void*)buffer1;
996 BCRYPT_ALG_HANDLE aes;
997 BCRYPT_KEY_HANDLE key;
998 NTSTATUS ret;
999 ULONG size;
1001 ret = pBCryptOpenAlgorithmProvider(&aes, BCRYPT_AES_ALGORITHM, NULL, 0);
1002 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1004 key_data1->dwMagic = BCRYPT_KEY_DATA_BLOB_MAGIC;
1005 key_data1->dwVersion = BCRYPT_KEY_DATA_BLOB_VERSION1;
1006 key_data1->cbKeyData = 16;
1007 memset(&key_data1[1], 0x11, 16);
1009 ret = pBCryptImportKey(aes, NULL, BCRYPT_KEY_DATA_BLOB, &key, NULL, 0, buffer1, sizeof(buffer1), 0);
1010 ok(ret == STATUS_SUCCESS || broken(ret == STATUS_INVALID_PARAMETER) /* vista */, "got %08x\n", ret);
1011 if (ret == STATUS_INVALID_PARAMETER)
1013 win_skip("broken BCryptImportKey\n");
1014 return;
1017 size = 0;
1018 ret = pBCryptExportKey(key, NULL, BCRYPT_KEY_DATA_BLOB, buffer2, 1, &size, 0);
1019 ok(ret == STATUS_BUFFER_TOO_SMALL, "got %08x\n", ret);
1020 ok(size == sizeof(buffer2), "Got %u\n", size);
1022 size = 0;
1023 memset(buffer2, 0xff, sizeof(buffer2));
1024 ret = pBCryptExportKey(key, NULL, BCRYPT_KEY_DATA_BLOB, buffer2, sizeof(buffer2), &size, 0);
1025 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1026 ok(size == sizeof(buffer2), "Got %u\n", size);
1027 ok(!memcmp(buffer1, buffer2, sizeof(buffer1)), "Expected exported key to match imported key\n");
1029 ret = pBCryptDestroyKey(key);
1030 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1032 ret = pBCryptCloseAlgorithmProvider(aes, 0);
1033 ok(ret == STATUS_SUCCESS, "got %08x\n", ret);
1036 START_TEST(bcrypt)
1038 HMODULE module;
1040 module = LoadLibraryA("bcrypt.dll");
1041 if (!module)
1043 win_skip("bcrypt.dll not found\n");
1044 return;
1047 pBCryptOpenAlgorithmProvider = (void *)GetProcAddress(module, "BCryptOpenAlgorithmProvider");
1048 pBCryptCloseAlgorithmProvider = (void *)GetProcAddress(module, "BCryptCloseAlgorithmProvider");
1049 pBCryptGetFipsAlgorithmMode = (void *)GetProcAddress(module, "BCryptGetFipsAlgorithmMode");
1050 pBCryptCreateHash = (void *)GetProcAddress(module, "BCryptCreateHash");
1051 pBCryptHash = (void *)GetProcAddress(module, "BCryptHash");
1052 pBCryptHashData = (void *)GetProcAddress(module, "BCryptHashData");
1053 pBCryptDuplicateHash = (void *)GetProcAddress(module, "BCryptDuplicateHash");
1054 pBCryptFinishHash = (void *)GetProcAddress(module, "BCryptFinishHash");
1055 pBCryptDestroyHash = (void *)GetProcAddress(module, "BCryptDestroyHash");
1056 pBCryptGenRandom = (void *)GetProcAddress(module, "BCryptGenRandom");
1057 pBCryptGetProperty = (void *)GetProcAddress(module, "BCryptGetProperty");
1058 pBCryptSetProperty = (void *)GetProcAddress(module, "BCryptSetProperty");
1059 pBCryptGenerateSymmetricKey = (void *)GetProcAddress(module, "BCryptGenerateSymmetricKey");
1060 pBCryptEncrypt = (void *)GetProcAddress(module, "BCryptEncrypt");
1061 pBCryptDecrypt = (void *)GetProcAddress(module, "BCryptDecrypt");
1062 pBCryptDestroyKey = (void *)GetProcAddress(module, "BCryptDestroyKey");
1063 pBCryptImportKey = (void *)GetProcAddress(module, "BCryptImportKey");
1064 pBCryptExportKey = (void *)GetProcAddress(module, "BCryptExportKey");
1066 test_BCryptGenRandom();
1067 test_BCryptGetFipsAlgorithmMode();
1068 test_hashes();
1069 test_rng();
1070 test_aes();
1071 test_BCryptGenerateSymmetricKey();
1072 test_BCryptEncrypt();
1073 test_BCryptDecrypt();
1074 test_key_import_export();
1076 if (pBCryptHash) /* >= Win 10 */
1077 test_BcryptHash();
1078 else
1079 win_skip("BCryptHash is not available\n");
1081 FreeLibrary(module);