[BCRYPT_WINETEST]
[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 static NTSTATUS (WINAPI *pBCryptGetFipsAlgorithmMode)(BOOLEAN *enabled);
31
32 static BOOL Init(void)
33 {
34 HMODULE hbcrypt = LoadLibraryA("bcrypt.dll");
35 if (!hbcrypt)
36 {
37 win_skip("bcrypt library not available\n");
38 return FALSE;
39 }
40
41 pBCryptGenRandom = (void *)GetProcAddress(hbcrypt, "BCryptGenRandom");
42 pBCryptGetFipsAlgorithmMode = (void *)GetProcAddress(hbcrypt, "BCryptGetFipsAlgorithmMode");
43
44 return TRUE;
45 }
46
47 static void test_BCryptGenRandom(void)
48 {
49 NTSTATUS ret;
50 UCHAR buffer[256];
51
52 if (!pBCryptGenRandom)
53 {
54 win_skip("BCryptGenRandom is not available\n");
55 return;
56 }
57
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);
71
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);
75
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");
81 }
82
83 static void test_BCryptGetFipsAlgorithmMode(void)
84 {
85 NTSTATUS ret;
86 BOOLEAN enabled;
87
88 if (!pBCryptGetFipsAlgorithmMode)
89 {
90 win_skip("BCryptGetFipsAlgorithmMode is not available\n");
91 return;
92 }
93
94 ret = pBCryptGetFipsAlgorithmMode(&enabled);
95 ok(ret == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got 0x%x\n", ret);
96
97 ret = pBCryptGetFipsAlgorithmMode(NULL);
98 ok(ret == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got 0x%x\n", ret);
99 }
100
101 START_TEST(bcrypt)
102 {
103 if (!Init())
104 return;
105
106 test_BCryptGenRandom();
107 test_BCryptGetFipsAlgorithmMode();
108 }