1 /*****************************************************************************
2 * rand.c : non-predictible random bytes generator
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
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 *****************************************************************************/
26 #include <vlc_common.h>
32 # include <winstring.h>
34 # include <windows.security.cryptography.h>
36 # include <wincrypt.h>
39 void vlc_rand_bytes (void *buf
, size_t len
)
42 uint8_t *p_buf
= (uint8_t *)buf
;
44 /* fill buffer with pseudo-random data */
49 if (count
< sizeof (val
))
51 memcpy (p_buf
, &val
, count
);
55 memcpy (p_buf
, &val
, sizeof (val
));
56 count
-= sizeof (val
);
57 p_buf
+= sizeof (val
);
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
);
68 WindowsDeleteString(hClassName
);
72 ICryptographicBufferStatics
*cryptoStatics
= NULL
;
73 hr
= RoGetActivationFactory(hClassName
, &IID_ICryptographicBufferStatics
, (void**)&cryptoStatics
);
74 WindowsDeleteString(hClassName
);
79 IBuffer
*buffer
= NULL
;
80 hr
= ICryptographicBufferStatics_GenerateRandom(cryptoStatics
, len
, &buffer
);
82 ICryptographicBufferStatics_Release(cryptoStatics
);
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
);
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, intial buffer content
104 is used as auxillary random seed */
105 CryptGenRandom(hProv
, len
, buf
);
106 CryptReleaseContext(hProv
, 0);
108 #endif /* VLC_WINSTORE_APP */