Refactor crypto RNG; add getrandom() support
[tinc] / src / windows / random.c
1 #include "../system.h"
2
3 #include <wincrypt.h>
4
5 #include "../random.h"
6
7 static HCRYPTPROV prov;
8
9 void random_init(void) {
10         if(!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
11                 fprintf(stderr, "CryptAcquireContext() failed!\n");
12                 abort();
13         }
14 }
15
16 void random_exit(void) {
17         CryptReleaseContext(prov, 0);
18 }
19
20 void randomize(void *vout, size_t outlen) {
21         if(!CryptGenRandom(prov, outlen, vout)) {
22                 fprintf(stderr, "CryptGenRandom() failed\n");
23                 abort();
24         }
25 }