]> git.sesse.net Git - vlc/blob - src/win32/rand.c
udp: fix locking (fixes #14234) and cancellation
[vlc] / src / win32 / rand.c
1 /*****************************************************************************
2  * rand.c : non-predictible random bytes generator
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_rand.h>
28
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
38
39 void vlc_rand_bytes (void *buf, size_t len)
40 {
41     size_t count = len;
42     uint8_t *p_buf = (uint8_t *)buf;
43
44     /* fill buffer with pseudo-random data */
45     while (count > 0)
46     {
47         unsigned int val;
48         val = rand();
49         if (count < sizeof (val))
50         {
51             memcpy (p_buf, &val, count);
52             break;
53         }
54
55         memcpy (p_buf, &val, sizeof (val));
56         count -= sizeof (val);
57         p_buf += sizeof (val);
58     }
59
60 #if VLC_WINSTORE_APP
61     static const WCHAR *className = L"Windows.Security.Cryptography.CryptographicBuffer";
62     const UINT32 clen = wcslen(className);
63
64     HSTRING hClassName = NULL;
65     HSTRING_HEADER header;
66     HRESULT hr = WindowsCreateStringReference(className, clen, &header, &hClassName);
67     if (hr) {
68         WindowsDeleteString(hClassName);
69         return;
70     }
71
72     ICryptographicBufferStatics *cryptoStatics = NULL;
73     hr = RoGetActivationFactory(hClassName, &IID_ICryptographicBufferStatics, (void**)&cryptoStatics);
74     WindowsDeleteString(hClassName);
75
76     if (hr)
77         return;
78
79     IBuffer *buffer = NULL;
80     hr = ICryptographicBufferStatics_GenerateRandom(cryptoStatics, len, &buffer);
81     if (hr) {
82         ICryptographicBufferStatics_Release(cryptoStatics);
83         return;
84     }
85
86     UINT32 olength;
87     unsigned char *rnd = NULL;
88     hr = ICryptographicBufferStatics_CopyToByteArray(cryptoStatics, buffer, &olength, (BYTE**)&rnd);
89     memcpy(buf, rnd, len);
90
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
102     {
103         /* fill buffer with pseudo-random data, intial buffer content
104            is used as auxillary random seed */
105         CryptGenRandom(hProv, len, buf);
106         CryptReleaseContext(hProv, 0);
107     }
108 #endif /* VLC_WINSTORE_APP */
109 }