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
22 #define WIN32_NO_STATUS
26 #include "wine/test.h"
28 static NTSTATUS (WINAPI
*pBCryptGenRandom
)(BCRYPT_ALG_HANDLE hAlgorithm
, PUCHAR pbBuffer
,
29 ULONG cbBuffer
, ULONG dwFlags
);
30 static NTSTATUS (WINAPI
*pBCryptGetFipsAlgorithmMode
)(BOOLEAN
*enabled
);
32 static BOOL
Init(void)
34 HMODULE hbcrypt
= LoadLibraryA("bcrypt.dll");
37 win_skip("bcrypt library not available\n");
41 pBCryptGenRandom
= (void *)GetProcAddress(hbcrypt
, "BCryptGenRandom");
42 pBCryptGetFipsAlgorithmMode
= (void *)GetProcAddress(hbcrypt
, "BCryptGetFipsAlgorithmMode");
47 static void test_BCryptGenRandom(void)
52 if (!pBCryptGenRandom
)
54 win_skip("BCryptGenRandom is not available\n");
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)
88 if (!pBCryptGetFipsAlgorithmMode
)
90 win_skip("BCryptGetFipsAlgorithmMode is not available\n");
94 ret
= pBCryptGetFipsAlgorithmMode(&enabled
);
95 ok(ret
== STATUS_SUCCESS
, "Expected STATUS_SUCCESS, got 0x%x\n", ret
);
97 ret
= pBCryptGetFipsAlgorithmMode(NULL
);
98 ok(ret
== STATUS_INVALID_PARAMETER
, "Expected STATUS_INVALID_PARAMETER, got 0x%x\n", ret
);
106 test_BCryptGenRandom();
107 test_BCryptGetFipsAlgorithmMode();