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
23 #define WIN32_NO_STATUS
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
,
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
,
46 static NTSTATUS (WINAPI
*pBCryptDecrypt
)(BCRYPT_KEY_HANDLE
, PUCHAR
, ULONG
, VOID
*, PUCHAR
, ULONG
, PUCHAR
, 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)
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','\\',
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','\\',
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};
102 DWORD value
, count
[2] = {sizeof(value
), sizeof(value
)};
105 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, policyKeyVistaW
, &hkey
) == ERROR_SUCCESS
&&
106 RegQueryValueExW(hkey
, policyValueVistaW
, NULL
, NULL
, (void *)&value
, &count
[0]) == ERROR_SUCCESS
)
110 else if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, policyKeyXPW
, &hkey
) == ERROR_SUCCESS
&&
111 RegQueryValueExW(hkey
, policyValueXPW
, NULL
, NULL
, (void *)&value
, &count
[0]) == ERROR_SUCCESS
)
119 ok(0, "Neither XP or Vista key is present\n");
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
)
135 for (i
= 0; i
< size
; i
++)
137 sprintf(buf
+ i
* 2, "%02x", bytes
[i
]);
142 static int strcmp_wa(const WCHAR
*strw
, const char *stra
)
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
)
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;
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;
202 const WCHAR
*name
= (const WCHAR
*)buf
;
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
);
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];
229 MultiByteToWideChar(CP_ACP
, 0, test
->alg
, -1, alg_name
, sizeof(alg_name
)/sizeof(WCHAR
));
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
);
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
);
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");
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
[] = {
303 "961fa64958818f767707072755d7018dcd278e94",
304 "2472cf65d0e090618d769d3e46f0d9446cf212da"
309 "ceb73749c899693706ede1e30c9929b3fd5dd926163831c2fb8bd41e6efb1126",
310 "34c1aa473a4468a91d06e7cdbc75bc4f93b830ccfc2a47ffd74e8e6ed29e4c72"
315 "62b21e90c9022b101671ba1f808f8631a8149f0f12904055839a35c1ca78ae53"
316 "63eed1e743a692d70e0504b0cfd12ef9",
317 "4b3e6d6ff2da121790ab7e7b9247583e3a7eed2db5bd4dabc680303b1608f37d"
318 "fdc836d96a704c03283bc05b4f6c5eb8"
323 "d55ced17163bf5386f2cd9ff21d6fd7fe576a915065c24744d09cfae4ec84ee1"
324 "ef6ef11bfbc5acce3639bab725b50a1fe2c204f8c820d6d7db0df0ecbc49c5ca",
325 "415fb6b10018ca03b38a1b1399c42ac0be5e8aceddb9a73103f5e543bf2d888f"
326 "2eecf91373941f9315dd730a77937fa92444450fbece86f409d9cb5ec48c6513"
331 "1bb33606ba908912a84221109d29cd7e",
332 "7f05b0638d77f4a27f3a9c4d353cd648"
337 "74b5db93c0b41e36ca7074338fc0b637",
338 "bc2e8ac4d8248ed21b8d26227a30ea3a"
343 "e2a3e68d23ce348b8f68b3079de3d4c9",
344 "7bda029b93fa8d817fcc9e13d6bdf092"
349 for(i
= 0; i
< sizeof(tests
)/sizeof(*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];
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
);
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
;
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");
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
;
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");
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
);
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
);
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
);
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
);
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
);
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};
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];
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
);
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
);
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
);
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
);
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
]);
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
);
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
);
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 nonce
[] =
568 {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60};
569 static UCHAR secret
[] =
570 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
572 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
573 static UCHAR data
[] =
574 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
575 static UCHAR data2
[] =
576 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
577 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
578 static UCHAR expected
[] =
579 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79};
580 static UCHAR expected2
[] =
581 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
582 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
583 static UCHAR expected3
[] =
584 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
585 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
586 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
587 static UCHAR expected4
[] =
588 {0xe1,0x82,0xc3,0xc0,0x24,0xfb,0x86,0x85,0xf3,0xf1,0x2b,0x7d,0x09,0xb4,0x73,0x67,
589 0x86,0x64,0xc3,0xfe,0xa3,0x07,0x61,0xf8,0x16,0xc9,0x78,0x7f,0xe7,0xb1,0xc4,0x94};
590 static UCHAR expected_tag
[] =
591 {0x89,0xb3,0x92,0x00,0x39,0x20,0x09,0xb4,0x6a,0xd6,0xaf,0xca,0x4b,0x5b,0xfd,0xd0};
592 static UCHAR expected_tag2
[] =
593 {0x9a,0x92,0x32,0x2c,0x61,0x2a,0xae,0xef,0x66,0x2a,0xfb,0x55,0xe9,0x48,0xdf,0xbd};
594 BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO auth_info
;
595 UCHAR
*buf
, ciphertext
[48], ivbuf
[16], tag
[16];
596 BCRYPT_AUTH_TAG_LENGTHS_STRUCT tag_length
;
597 BCRYPT_ALG_HANDLE aes
;
598 BCRYPT_KEY_HANDLE key
;
602 ret
= pBCryptOpenAlgorithmProvider(&aes
, BCRYPT_AES_ALGORITHM
, NULL
, 0);
603 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
611 ret
= pBCryptGetProperty(aes
, BCRYPT_OBJECT_LENGTH
, (UCHAR
*)&len
, sizeof(len
), &size
, 0);
612 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
614 buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
615 ret
= pBCryptGenerateSymmetricKey(aes
, &key
, buf
, len
, secret
, sizeof(secret
), 0);
616 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
618 /* input size is a multiple of block size */
620 memcpy(ivbuf
, iv
, sizeof(iv
));
621 ret
= pBCryptEncrypt(key
, data
, 16, NULL
, ivbuf
, 16, NULL
, 0, &size
, 0);
622 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
623 ok(size
== 16, "got %u\n", size
);
626 memcpy(ivbuf
, iv
, sizeof(iv
));
627 memset(ciphertext
, 0, sizeof(ciphertext
));
628 ret
= pBCryptEncrypt(key
, data
, 16, NULL
, ivbuf
, 16, ciphertext
, 16, &size
, 0);
629 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
630 ok(size
== 16, "got %u\n", size
);
631 ok(!memcmp(ciphertext
, expected
, sizeof(expected
)), "wrong data\n");
632 for (i
= 0; i
< 16; i
++)
633 ok(ciphertext
[i
] == expected
[i
], "%u: %02x != %02x\n", i
, ciphertext
[i
], expected
[i
]);
635 /* input size is not a multiple of block size */
637 memcpy(ivbuf
, iv
, sizeof(iv
));
638 ret
= pBCryptEncrypt(key
, data
, 17, NULL
, ivbuf
, 16, NULL
, 0, &size
, 0);
639 ok(ret
== STATUS_INVALID_BUFFER_SIZE
, "got %08x\n", ret
);
640 ok(size
== 17, "got %u\n", size
);
642 /* input size is not a multiple of block size, block padding set */
644 memcpy(ivbuf
, iv
, sizeof(iv
));
645 ret
= pBCryptEncrypt(key
, data
, 17, NULL
, ivbuf
, 16, NULL
, 0, &size
, BCRYPT_BLOCK_PADDING
);
646 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
647 ok(size
== 32, "got %u\n", size
);
650 memcpy(ivbuf
, iv
, sizeof(iv
));
651 memset(ciphertext
, 0, sizeof(ciphertext
));
652 ret
= pBCryptEncrypt(key
, data
, 17, NULL
, ivbuf
, 16, ciphertext
, 32, &size
, BCRYPT_BLOCK_PADDING
);
653 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
654 ok(size
== 32, "got %u\n", size
);
655 ok(!memcmp(ciphertext
, expected2
, sizeof(expected2
)), "wrong data\n");
656 for (i
= 0; i
< 32; i
++)
657 ok(ciphertext
[i
] == expected2
[i
], "%u: %02x != %02x\n", i
, ciphertext
[i
], expected2
[i
]);
659 /* input size is a multiple of block size, block padding set */
661 memcpy(ivbuf
, iv
, sizeof(iv
));
662 ret
= pBCryptEncrypt(key
, data2
, 32, NULL
, ivbuf
, 16, NULL
, 0, &size
, BCRYPT_BLOCK_PADDING
);
663 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
664 ok(size
== 48, "got %u\n", size
);
667 memcpy(ivbuf
, iv
, sizeof(iv
));
668 memset(ciphertext
, 0, sizeof(ciphertext
));
669 ret
= pBCryptEncrypt(key
, data2
, 32, NULL
, ivbuf
, 16, ciphertext
, 48, &size
, BCRYPT_BLOCK_PADDING
);
670 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
671 ok(size
== 48, "got %u\n", size
);
672 ok(!memcmp(ciphertext
, expected3
, sizeof(expected3
)), "wrong data\n");
673 for (i
= 0; i
< 48; i
++)
674 ok(ciphertext
[i
] == expected3
[i
], "%u: %02x != %02x\n", i
, ciphertext
[i
], expected3
[i
]);
676 /* output size too small */
678 memcpy(ivbuf
, iv
, sizeof(iv
));
679 memset(ciphertext
, 0, sizeof(ciphertext
));
680 ret
= pBCryptEncrypt(key
, data
, 17, NULL
, ivbuf
, 16, ciphertext
, 31, &size
, BCRYPT_BLOCK_PADDING
);
681 ok(ret
== STATUS_BUFFER_TOO_SMALL
, "got %08x\n", ret
);
682 ok(size
== 32, "got %u\n", size
);
685 memcpy(ivbuf
, iv
, sizeof(iv
));
686 memset(ciphertext
, 0, sizeof(ciphertext
));
687 ret
= pBCryptEncrypt(key
, data2
, 32, NULL
, ivbuf
, 16, ciphertext
, 32, &size
, BCRYPT_BLOCK_PADDING
);
688 ok(ret
== STATUS_BUFFER_TOO_SMALL
, "got %08x\n", ret
);
689 ok(size
== 48, "got %u\n", size
);
691 ret
= pBCryptDestroyKey(key
);
692 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
693 HeapFree(GetProcessHeap(), 0, buf
);
700 ret
= BCryptGetProperty(aes
, BCRYPT_AUTH_TAG_LENGTH
, NULL
, 0, &size
, 0);
701 todo_wine
ok(ret
== STATUS_NOT_SUPPORTED
, "got %08x\n", ret
);
703 ret
= BCryptSetProperty(aes
, BCRYPT_CHAINING_MODE
, (UCHAR
*)BCRYPT_CHAIN_MODE_GCM
, sizeof(BCRYPT_CHAIN_MODE_GCM
), 0);
704 todo_wine
ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
707 ret
= BCryptGetProperty(aes
, BCRYPT_AUTH_TAG_LENGTH
, NULL
, 0, &size
, 0);
708 todo_wine
ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
709 todo_wine
ok(size
== sizeof(tag_length
), "got %u\n", size
);
712 memset(&tag_length
, 0, sizeof(tag_length
));
713 ret
= BCryptGetProperty(aes
, BCRYPT_AUTH_TAG_LENGTH
, (UCHAR
*)&tag_length
, sizeof(tag_length
), &size
, 0);
714 todo_wine
ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
715 todo_wine
ok(size
== sizeof(tag_length
), "got %u\n", size
);
716 todo_wine
ok(tag_length
.dwMinLength
== 12, "Expected 12, got %d\n", tag_length
.dwMinLength
);
717 todo_wine
ok(tag_length
.dwMaxLength
== 16, "Expected 16, got %d\n", tag_length
.dwMaxLength
);
718 todo_wine
ok(tag_length
.dwIncrement
== 1, "Expected 1, got %d\n", tag_length
.dwIncrement
);
722 ret
= pBCryptGetProperty(aes
, BCRYPT_OBJECT_LENGTH
, (UCHAR
*)&len
, sizeof(len
), &size
, 0);
723 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
725 buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
726 ret
= pBCryptGenerateSymmetricKey(aes
, &key
, buf
, len
, secret
, sizeof(secret
), 0);
727 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
729 memset(&auth_info
, 0, sizeof(auth_info
));
730 auth_info
.cbSize
= sizeof(auth_info
);
731 auth_info
.dwInfoVersion
= 1;
732 auth_info
.pbNonce
= nonce
;
733 auth_info
.cbNonce
= sizeof(nonce
);
734 auth_info
.pbTag
= tag
;
735 auth_info
.cbTag
= sizeof(tag
);
737 /* input size is a multiple of block size */
739 memcpy(ivbuf
, iv
, sizeof(iv
));
740 memset(ciphertext
, 0xff, sizeof(ciphertext
));
741 memset(tag
, 0xff, sizeof(tag
));
742 ret
= pBCryptEncrypt(key
, data2
, 32, &auth_info
, ivbuf
, 16, ciphertext
, 32, &size
, 0);
743 todo_wine
ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
744 todo_wine
ok(size
== 32, "got %u\n", size
);
745 todo_wine
ok(!memcmp(ciphertext
, expected4
, sizeof(expected4
)), "wrong data\n");
746 todo_wine
ok(!memcmp(tag
, expected_tag
, sizeof(expected_tag
)), "wrong tag\n");
747 for (i
= 0; i
< 32; i
++)
748 todo_wine
ok(ciphertext
[i
] == expected4
[i
], "%u: %02x != %02x\n", i
, ciphertext
[i
], expected4
[i
]);
749 for (i
= 0; i
< 16; i
++)
750 todo_wine
ok(tag
[i
] == expected_tag
[i
], "%u: %02x != %02x\n", i
, tag
[i
], expected_tag
[i
]);
752 /* input size is not multiple of block size */
754 memcpy(ivbuf
, iv
, sizeof(iv
));
755 memset(ciphertext
, 0xff, sizeof(ciphertext
));
756 memset(tag
, 0xff, sizeof(tag
));
757 ret
= pBCryptEncrypt(key
, data2
, 24, &auth_info
, ivbuf
, 16, ciphertext
, 24, &size
, 0);
758 todo_wine
ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
759 todo_wine
ok(size
== 24, "got %u\n", size
);
760 todo_wine
ok(!memcmp(ciphertext
, expected4
, 24), "wrong data\n");
761 todo_wine
ok(!memcmp(tag
, expected_tag2
, sizeof(expected_tag2
)), "wrong tag\n");
762 for (i
= 0; i
< 24; i
++)
763 todo_wine
ok(ciphertext
[i
] == expected4
[i
], "%u: %02x != %02x\n", i
, ciphertext
[i
], expected4
[i
]);
764 for (i
= 0; i
< 16; i
++)
765 todo_wine
ok(tag
[i
] == expected_tag2
[i
], "%u: %02x != %02x\n", i
, tag
[i
], expected_tag2
[i
]);
767 /* test with padding */
768 memcpy(ivbuf
, iv
, sizeof(iv
));
769 memset(ciphertext
, 0, sizeof(ciphertext
));
770 ret
= pBCryptEncrypt(key
, data2
, 32, &auth_info
, ivbuf
, 16, ciphertext
, 32, &size
, BCRYPT_BLOCK_PADDING
);
771 todo_wine
ok(ret
== STATUS_BUFFER_TOO_SMALL
, "got %08x\n", ret
);
773 memcpy(ivbuf
, iv
, sizeof(iv
));
774 memset(ciphertext
, 0, sizeof(ciphertext
));
775 ret
= pBCryptEncrypt(key
, data2
, 32, &auth_info
, ivbuf
, 16, ciphertext
, 48, &size
, BCRYPT_BLOCK_PADDING
);
776 todo_wine
ok(ret
== STATUS_INVALID_PARAMETER
, "got %08x\n", ret
);
778 ret
= pBCryptDestroyKey(key
);
779 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
780 HeapFree(GetProcessHeap(), 0, buf
);
782 ret
= pBCryptCloseAlgorithmProvider(aes
, 0);
783 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
786 static void test_BCryptDecrypt(void)
788 static UCHAR nonce
[] =
789 {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60};
790 static UCHAR secret
[] =
791 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
793 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
794 static UCHAR expected
[] =
795 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
796 static UCHAR expected2
[] =
797 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10};
798 static UCHAR expected3
[] =
799 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
800 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};
801 static UCHAR ciphertext
[32] =
802 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
803 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
804 static UCHAR ciphertext2
[] =
805 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
806 0x28,0x73,0x3d,0xef,0x84,0x8f,0xb0,0xa6,0x5d,0x1a,0x51,0xb7,0xec,0x8f,0xea,0xe9};
807 static UCHAR ciphertext3
[] =
808 {0xc6,0xa1,0x3b,0x37,0x87,0x8f,0x5b,0x82,0x6f,0x4f,0x81,0x62,0xa1,0xc8,0xd8,0x79,
809 0xb1,0xa2,0x92,0x73,0xbe,0x2c,0x42,0x07,0xa5,0xac,0xe3,0x93,0x39,0x8c,0xb6,0xfb,
810 0x87,0x5d,0xea,0xa3,0x7e,0x0f,0xde,0xfa,0xd9,0xec,0x6c,0x4e,0x3c,0x76,0x86,0xe4};
811 static UCHAR ciphertext4
[] =
812 {0xe1,0x82,0xc3,0xc0,0x24,0xfb,0x86,0x85,0xf3,0xf1,0x2b,0x7d,0x09,0xb4,0x73,0x67,
813 0x86,0x64,0xc3,0xfe,0xa3,0x07,0x61,0xf8,0x16,0xc9,0x78,0x7f,0xe7,0xb1,0xc4,0x94};
815 {0x89,0xb3,0x92,0x00,0x39,0x20,0x09,0xb4,0x6a,0xd6,0xaf,0xca,0x4b,0x5b,0xfd,0xd0};
816 BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO auth_info
;
817 BCRYPT_KEY_LENGTHS_STRUCT key_lengths
;
818 BCRYPT_ALG_HANDLE aes
;
819 BCRYPT_KEY_HANDLE key
;
820 UCHAR
*buf
, plaintext
[48], ivbuf
[16];
824 ret
= pBCryptOpenAlgorithmProvider(&aes
, BCRYPT_AES_ALGORITHM
, NULL
, 0);
825 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
828 memset(&key_lengths
, 0, sizeof(key_lengths
));
829 ret
= BCryptGetProperty(aes
, BCRYPT_KEY_LENGTHS
, (UCHAR
*)&key_lengths
, sizeof(key_lengths
), &size
, 0);
830 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
831 ok(size
== sizeof(key_lengths
), "got %u\n", size
);
832 ok(key_lengths
.dwMinLength
== 128, "Expected 128, got %d\n", key_lengths
.dwMinLength
);
833 ok(key_lengths
.dwMaxLength
== 256, "Expected 256, got %d\n", key_lengths
.dwMaxLength
);
834 ok(key_lengths
.dwIncrement
== 64, "Expected 64, got %d\n", key_lengths
.dwIncrement
);
842 ret
= pBCryptGetProperty(aes
, BCRYPT_OBJECT_LENGTH
, (UCHAR
*)&len
, sizeof(len
), &size
, 0);
843 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
845 buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
846 ret
= pBCryptGenerateSymmetricKey(aes
, &key
, buf
, len
, secret
, sizeof(secret
), 0);
847 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
849 /* input size is a multiple of block size */
851 memcpy(ivbuf
, iv
, sizeof(iv
));
852 ret
= pBCryptDecrypt(key
, ciphertext
, 32, NULL
, ivbuf
, 16, NULL
, 0, &size
, 0);
853 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
854 ok(size
== 32, "got %u\n", size
);
857 memcpy(ivbuf
, iv
, sizeof(iv
));
858 memset(plaintext
, 0, sizeof(plaintext
));
859 ret
= pBCryptDecrypt(key
, ciphertext
, 32, NULL
, ivbuf
, 16, plaintext
, 32, &size
, 0);
860 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
861 ok(size
== 32, "got %u\n", size
);
862 ok(!memcmp(plaintext
, expected
, sizeof(expected
)), "wrong data\n");
864 /* test with padding smaller than block size */
866 memcpy(ivbuf
, iv
, sizeof(iv
));
867 ret
= pBCryptDecrypt(key
, ciphertext2
, 32, NULL
, ivbuf
, 16, NULL
, 0, &size
, 0);
868 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
869 ok(size
== 32, "got %u\n", size
);
872 memcpy(ivbuf
, iv
, sizeof(iv
));
873 memset(plaintext
, 0, sizeof(plaintext
));
874 ret
= pBCryptDecrypt(key
, ciphertext2
, 32, NULL
, ivbuf
, 16, plaintext
, 17, &size
, BCRYPT_BLOCK_PADDING
);
875 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
876 ok(size
== 17, "got %u\n", size
);
877 ok(!memcmp(plaintext
, expected2
, sizeof(expected2
)), "wrong data\n");
879 /* test with padding of block size */
881 memcpy(ivbuf
, iv
, sizeof(iv
));
882 ret
= pBCryptDecrypt(key
, ciphertext3
, 48, NULL
, ivbuf
, 16, NULL
, 0, &size
, 0);
883 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
884 ok(size
== 48, "got %u\n", size
);
887 memcpy(ivbuf
, iv
, sizeof(iv
));
888 memset(plaintext
, 0, sizeof(plaintext
));
889 ret
= pBCryptDecrypt(key
, ciphertext3
, 48, NULL
, ivbuf
, 16, plaintext
, 32, &size
, BCRYPT_BLOCK_PADDING
);
890 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
891 ok(size
== 32, "got %u\n", size
);
892 ok(!memcmp(plaintext
, expected3
, sizeof(expected3
)), "wrong data\n");
894 /* output size too small */
896 memcpy(ivbuf
, iv
, sizeof(iv
));
897 ret
= pBCryptDecrypt(key
, ciphertext
, 32, NULL
, ivbuf
, 16, plaintext
, 31, &size
, 0);
898 ok(ret
== STATUS_BUFFER_TOO_SMALL
, "got %08x\n", ret
);
899 ok(size
== 32, "got %u\n", size
);
902 memcpy(ivbuf
, iv
, sizeof(iv
));
903 ret
= pBCryptDecrypt(key
, ciphertext2
, 32, NULL
, ivbuf
, 16, plaintext
, 15, &size
, BCRYPT_BLOCK_PADDING
);
904 ok(ret
== STATUS_BUFFER_TOO_SMALL
, "got %08x\n", ret
);
905 ok(size
== 32, "got %u\n", size
);
908 memcpy(ivbuf
, iv
, sizeof(iv
));
909 ret
= pBCryptDecrypt(key
, ciphertext2
, 32, NULL
, ivbuf
, 16, plaintext
, 16, &size
, BCRYPT_BLOCK_PADDING
);
910 ok(ret
== STATUS_BUFFER_TOO_SMALL
, "got %08x\n", ret
);
911 ok(size
== 17, "got %u\n", size
);
914 memcpy(ivbuf
, iv
, sizeof(iv
));
915 ret
= pBCryptDecrypt(key
, ciphertext3
, 48, NULL
, ivbuf
, 16, plaintext
, 31, &size
, BCRYPT_BLOCK_PADDING
);
916 ok(ret
== STATUS_BUFFER_TOO_SMALL
, "got %08x\n", ret
);
917 ok(size
== 48, "got %u\n", size
);
919 /* input size is not a multiple of block size */
921 memcpy(ivbuf
, iv
, sizeof(iv
));
922 ret
= pBCryptDecrypt(key
, ciphertext
, 17, NULL
, ivbuf
, 16, NULL
, 0, &size
, 0);
923 ok(ret
== STATUS_INVALID_BUFFER_SIZE
, "got %08x\n", ret
);
924 ok(size
== 17 || broken(size
== 0 /* Win < 7 */), "got %u\n", size
);
926 /* input size is not a multiple of block size, block padding set */
928 memcpy(ivbuf
, iv
, sizeof(iv
));
929 ret
= pBCryptDecrypt(key
, ciphertext
, 17, NULL
, ivbuf
, 16, NULL
, 0, &size
, BCRYPT_BLOCK_PADDING
);
930 ok(ret
== STATUS_INVALID_BUFFER_SIZE
, "got %08x\n", ret
);
931 ok(size
== 17 || broken(size
== 0 /* Win < 7 */), "got %u\n", size
);
933 ret
= pBCryptDestroyKey(key
);
934 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
935 HeapFree(GetProcessHeap(), 0, buf
);
941 ret
= BCryptSetProperty(aes
, BCRYPT_CHAINING_MODE
, (UCHAR
*)BCRYPT_CHAIN_MODE_GCM
, sizeof(BCRYPT_CHAIN_MODE_GCM
), 0);
942 todo_wine
ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
944 buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
945 ret
= pBCryptGenerateSymmetricKey(aes
, &key
, buf
, len
, secret
, sizeof(secret
), 0);
946 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
948 memset(&auth_info
, 0, sizeof(auth_info
));
949 auth_info
.cbSize
= sizeof(auth_info
);
950 auth_info
.dwInfoVersion
= 1;
951 auth_info
.pbNonce
= nonce
;
952 auth_info
.cbNonce
= sizeof(nonce
);
953 auth_info
.pbTag
= tag
;
954 auth_info
.cbTag
= sizeof(tag
);
956 /* input size is a multiple of block size */
958 memcpy(ivbuf
, iv
, sizeof(iv
));
959 memset(plaintext
, 0, sizeof(plaintext
));
960 ret
= pBCryptDecrypt(key
, ciphertext4
, 32, &auth_info
, ivbuf
, 16, plaintext
, 32, &size
, 0);
961 todo_wine
ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
962 todo_wine
ok(size
== 32, "got %u\n", size
);
963 todo_wine
ok(!memcmp(plaintext
, expected3
, sizeof(expected3
)), "wrong data\n");
965 /* test with wrong tag */
966 memcpy(ivbuf
, iv
, sizeof(iv
));
967 auth_info
.pbTag
= iv
; /* wrong tag */
968 ret
= pBCryptDecrypt(key
, ciphertext4
, 32, &auth_info
, ivbuf
, 16, plaintext
, 32, &size
, 0);
969 todo_wine
ok(ret
== STATUS_AUTH_TAG_MISMATCH
, "got %08x\n", ret
);
970 todo_wine
ok(size
== 32, "got %u\n", size
);
972 ret
= pBCryptDestroyKey(key
);
973 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
974 HeapFree(GetProcessHeap(), 0, buf
);
976 ret
= pBCryptCloseAlgorithmProvider(aes
, 0);
977 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
980 static void test_key_import_export(void)
982 UCHAR buffer1
[sizeof(BCRYPT_KEY_DATA_BLOB_HEADER
) + 16];
983 UCHAR buffer2
[sizeof(BCRYPT_KEY_DATA_BLOB_HEADER
) + 16];
984 BCRYPT_KEY_DATA_BLOB_HEADER
*key_data1
= (void*)buffer1
;
985 BCRYPT_ALG_HANDLE aes
;
986 BCRYPT_KEY_HANDLE key
;
990 ret
= pBCryptOpenAlgorithmProvider(&aes
, BCRYPT_AES_ALGORITHM
, NULL
, 0);
991 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
993 key_data1
->dwMagic
= BCRYPT_KEY_DATA_BLOB_MAGIC
;
994 key_data1
->dwVersion
= BCRYPT_KEY_DATA_BLOB_VERSION1
;
995 key_data1
->cbKeyData
= 16;
996 memset(&key_data1
[1], 0x11, 16);
998 ret
= pBCryptImportKey(aes
, NULL
, BCRYPT_KEY_DATA_BLOB
, &key
, NULL
, 0, buffer1
, sizeof(buffer1
), 0);
999 ok(ret
== STATUS_SUCCESS
|| broken(ret
== STATUS_INVALID_PARAMETER
) /* vista */, "got %08x\n", ret
);
1000 if (ret
== STATUS_INVALID_PARAMETER
)
1002 win_skip("broken BCryptImportKey\n");
1007 ret
= pBCryptExportKey(key
, NULL
, BCRYPT_KEY_DATA_BLOB
, buffer2
, 1, &size
, 0);
1008 ok(ret
== STATUS_BUFFER_TOO_SMALL
, "got %08x\n", ret
);
1009 ok(size
== sizeof(buffer2
), "Got %u\n", size
);
1012 memset(buffer2
, 0xff, sizeof(buffer2
));
1013 ret
= pBCryptExportKey(key
, NULL
, BCRYPT_KEY_DATA_BLOB
, buffer2
, sizeof(buffer2
), &size
, 0);
1014 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
1015 ok(size
== sizeof(buffer2
), "Got %u\n", size
);
1016 ok(!memcmp(buffer1
, buffer2
, sizeof(buffer1
)), "Expected exported key to match imported key\n");
1018 ret
= pBCryptDestroyKey(key
);
1019 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
1021 ret
= pBCryptCloseAlgorithmProvider(aes
, 0);
1022 ok(ret
== STATUS_SUCCESS
, "got %08x\n", ret
);
1029 module
= LoadLibraryA("bcrypt.dll");
1032 win_skip("bcrypt.dll not found\n");
1036 pBCryptOpenAlgorithmProvider
= (void *)GetProcAddress(module
, "BCryptOpenAlgorithmProvider");
1037 pBCryptCloseAlgorithmProvider
= (void *)GetProcAddress(module
, "BCryptCloseAlgorithmProvider");
1038 pBCryptGetFipsAlgorithmMode
= (void *)GetProcAddress(module
, "BCryptGetFipsAlgorithmMode");
1039 pBCryptCreateHash
= (void *)GetProcAddress(module
, "BCryptCreateHash");
1040 pBCryptHash
= (void *)GetProcAddress(module
, "BCryptHash");
1041 pBCryptHashData
= (void *)GetProcAddress(module
, "BCryptHashData");
1042 pBCryptDuplicateHash
= (void *)GetProcAddress(module
, "BCryptDuplicateHash");
1043 pBCryptFinishHash
= (void *)GetProcAddress(module
, "BCryptFinishHash");
1044 pBCryptDestroyHash
= (void *)GetProcAddress(module
, "BCryptDestroyHash");
1045 pBCryptGenRandom
= (void *)GetProcAddress(module
, "BCryptGenRandom");
1046 pBCryptGetProperty
= (void *)GetProcAddress(module
, "BCryptGetProperty");
1047 pBCryptSetProperty
= (void *)GetProcAddress(module
, "BCryptSetProperty");
1048 pBCryptGenerateSymmetricKey
= (void *)GetProcAddress(module
, "BCryptGenerateSymmetricKey");
1049 pBCryptEncrypt
= (void *)GetProcAddress(module
, "BCryptEncrypt");
1050 pBCryptDecrypt
= (void *)GetProcAddress(module
, "BCryptDecrypt");
1051 pBCryptDestroyKey
= (void *)GetProcAddress(module
, "BCryptDestroyKey");
1052 pBCryptImportKey
= (void *)GetProcAddress(module
, "BCryptImportKey");
1053 pBCryptExportKey
= (void *)GetProcAddress(module
, "BCryptExportKey");
1055 test_BCryptGenRandom();
1056 test_BCryptGetFipsAlgorithmMode();
1060 test_BCryptGenerateSymmetricKey();
1061 test_BCryptEncrypt();
1062 test_BCryptDecrypt();
1063 test_key_import_export();
1065 if (pBCryptHash
) /* >= Win 10 */
1068 win_skip("BCryptHash is not available\n");
1070 FreeLibrary(module
);