Merge pull request #4630 from BrzVlad/feature-valloc-limit
[mono-project.git] / mono / utils / mono-rand-windows-uwp.c
blob858df2dcef1255b05598cfdbc5b2676ced6c1658
1 /**
2 * \file
3 * UWP rand support for Mono.
5 * Copyright 2016 Microsoft
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7 */
8 #include <config.h>
9 #include <glib.h>
10 #include "mono/utils/mono-compiler.h"
12 #if G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT)
13 #include <windows.h>
14 #include "mono/utils/mono-rand-windows-internals.h"
16 MONO_WIN32_CRYPT_PROVIDER_HANDLE
17 mono_rand_win_open_provider (void)
19 MONO_WIN32_CRYPT_PROVIDER_HANDLE provider = 0;
21 if (!BCRYPT_SUCCESS (BCryptOpenAlgorithmProvider (&provider, BCRYPT_RNG_ALGORITHM, NULL, 0)))
22 provider = 0;
24 return provider;
27 gboolean
28 mono_rand_win_gen (MONO_WIN32_CRYPT_PROVIDER_HANDLE provider, guchar *buffer, size_t buffer_size)
30 g_assert (provider != 0 && buffer != 0);
31 return (BCRYPT_SUCCESS (BCryptGenRandom (provider, buffer, (ULONG) buffer_size, 0))) ? TRUE : FALSE;
34 gboolean
35 mono_rand_win_seed (MONO_WIN32_CRYPT_PROVIDER_HANDLE provider, guchar *seed, size_t seed_size)
37 g_assert (provider != 0 && seed != 0);
38 return (BCRYPT_SUCCESS (BCryptGenRandom (provider, seed, (ULONG) seed_size, BCRYPT_RNG_USE_ENTROPY_IN_BUFFER))) ? TRUE : FALSE;
41 void
42 mono_rand_win_close_provider (MONO_WIN32_CRYPT_PROVIDER_HANDLE provider)
44 g_assert (provider != 0);
45 BCryptCloseAlgorithmProvider (provider, 0);
48 #else /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */
50 MONO_EMPTY_SOURCE_FILE (mono_rand_windows_uwp);
51 #endif /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */