288e745d4bed57c98dcf732b5178675d6779e9fd
[reactos.git] / rostests / winetests / bcrypt / bcrypt.c
1 /*
2 * Unit test for bcrypt functions
3 *
4 * Copyright 2014 Bruno Jesus
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <ntstatus.h>
22 #define WIN32_NO_STATUS
23 #include <windows.h>
24 #include <bcrypt.h>
25
26 #include "wine/test.h"
27
28 static NTSTATUS (WINAPI *pBCryptGenRandom)(BCRYPT_ALG_HANDLE hAlgorithm, PUCHAR pbBuffer,
29 ULONG cbBuffer, ULONG dwFlags);
30
31 static BOOL Init(void)
32 {
33 HMODULE hbcrypt = LoadLibraryA("bcrypt.dll");
34 if (!hbcrypt)
35 {
36 win_skip("bcrypt library not available\n");
37 return FALSE;
38 }
39
40 pBCryptGenRandom = (void *)GetProcAddress(hbcrypt, "BCryptGenRandom");
41
42 return TRUE;
43 }
44
45 static void test_BCryptGenRandom(void)
46 {
47 NTSTATUS ret;
48 UCHAR buffer[256];
49
50 if (!pBCryptGenRandom)
51 {
52 win_skip("BCryptGenRandom is not available\n");
53 return;
54 }
55
56 ret = pBCryptGenRandom(NULL, NULL, 0, 0);
57 ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got 0x%x\n", ret);
58 ret = pBCryptGenRandom(NULL, buffer, 0, 0);
59 ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got 0x%x\n", ret);
60 ret = pBCryptGenRandom(NULL, buffer, sizeof(buffer), 0);
61 ok(ret == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got 0x%x\n", ret);
62 ret = pBCryptGenRandom(NULL, buffer, sizeof(buffer), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
63 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
64 ret = pBCryptGenRandom(NULL, buffer, sizeof(buffer),
65 BCRYPT_USE_SYSTEM_PREFERRED_RNG|BCRYPT_RNG_USE_ENTROPY_IN_BUFFER);
66 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
67 ret = pBCryptGenRandom(NULL, NULL, sizeof(buffer), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
68 ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got 0x%x\n", ret);
69
70 /* Zero sized buffer should work too */
71 ret = pBCryptGenRandom(NULL, buffer, 0, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
72 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
73
74 /* Test random number generation - It's impossible for a sane RNG to return 8 zeros */
75 memset(buffer, 0, 16);
76 ret = pBCryptGenRandom(NULL, buffer, 8, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
77 ok(ret == STATUS_SUCCESS, "Expected success, got 0x%x\n", ret);
78 ok(memcmp(buffer, buffer + 8, 8), "Expected a random number, got 0\n");
79 }
80
81 START_TEST(bcrypt)
82 {
83 if (!Init())
84 return;
85
86 test_BCryptGenRandom();
87 }