win32: Package vlc_branding.bmp in release builds
[vlc.git] / src / win32 / rand.c
blob1aeb656a8ab76592ce90238e2ab79870d081317d
1 /*****************************************************************************
2 * rand.c : non-predictible random bytes generator
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
5 * $Id$
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <vlc_common.h>
27 #include <vlc_rand.h>
29 #if VLC_WINSTORE_APP
30 # define COBJMACROS
31 # define INITGUID
32 # include <winstring.h>
33 # include <roapi.h>
34 # include <windows.security.cryptography.h>
35 #else
36 # include <wincrypt.h>
37 #endif
39 void vlc_rand_bytes (void *buf, size_t len)
41 size_t count = len;
42 uint8_t *p_buf = (uint8_t *)buf;
44 /* fill buffer with pseudo-random data */
45 while (count > 0)
47 unsigned int val;
48 val = rand();
49 if (count < sizeof (val))
51 memcpy (p_buf, &val, count);
52 break;
55 memcpy (p_buf, &val, sizeof (val));
56 count -= sizeof (val);
57 p_buf += sizeof (val);
60 #if VLC_WINSTORE_APP
61 static const WCHAR *className = L"Windows.Security.Cryptography.CryptographicBuffer";
62 const UINT32 clen = wcslen(className);
64 HSTRING hClassName = NULL;
65 HSTRING_HEADER header;
66 HRESULT hr = WindowsCreateStringReference(className, clen, &header, &hClassName);
67 if (hr) {
68 WindowsDeleteString(hClassName);
69 return;
72 ICryptographicBufferStatics *cryptoStatics = NULL;
73 hr = RoGetActivationFactory(hClassName, &IID_ICryptographicBufferStatics, (void**)&cryptoStatics);
74 WindowsDeleteString(hClassName);
76 if (hr)
77 return;
79 IBuffer *buffer = NULL;
80 hr = ICryptographicBufferStatics_GenerateRandom(cryptoStatics, len, &buffer);
81 if (hr) {
82 ICryptographicBufferStatics_Release(cryptoStatics);
83 return;
86 UINT32 olength;
87 unsigned char *rnd = NULL;
88 hr = ICryptographicBufferStatics_CopyToByteArray(cryptoStatics, buffer, &olength, (BYTE**)&rnd);
89 memcpy(buf, rnd, len);
91 IBuffer_Release(buffer);
92 ICryptographicBufferStatics_Release(cryptoStatics);
93 #else
94 HCRYPTPROV hProv;
95 /* acquire default encryption context */
96 if( CryptAcquireContext(
97 &hProv, // Variable to hold returned handle.
98 NULL, // Use default key container.
99 MS_DEF_PROV, // Use default CSP.
100 PROV_RSA_FULL, // Type of provider to acquire.
101 CRYPT_VERIFYCONTEXT) ) // Flag values
103 /* fill buffer with pseudo-random data, initial buffer content
104 is used as auxiliary random seed */
105 CryptGenRandom(hProv, len, buf);
106 CryptReleaseContext(hProv, 0);
108 #endif /* VLC_WINSTORE_APP */