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
);
31 static BOOL
Init(void)
33 HMODULE hbcrypt
= LoadLibraryA("bcrypt.dll");
36 win_skip("bcrypt library not available\n");
40 pBCryptGenRandom
= (void *)GetProcAddress(hbcrypt
, "BCryptGenRandom");
45 static void test_BCryptGenRandom(void)
50 if (!pBCryptGenRandom
)
52 win_skip("BCryptGenRandom is not available\n");
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
);
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
);
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");
86 test_BCryptGenRandom();