1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/*
* File: Random.cpp
*
* Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
* See included license file for license details.
*/
#include "Random.h"
#include <stdexcept>
#ifdef WIN32
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#include <windows.h>
#include <wincrypt.h>
#else // WIN32
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#endif // WIN32
#ifdef WIN32
MicrosoftCryptoProvider::MicrosoftCryptoProvider()
{
if(!CryptAcquireContext(&m_hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
throw std::runtime_error("CryptAcquireContext");
}
}
MicrosoftCryptoProvider::~MicrosoftCryptoProvider()
{
CryptReleaseContext(m_hProvider, 0);
}
#endif // WIN32
RandomNumberGenerator::RandomNumberGenerator()
{
#ifndef WIN32
m_fd = open("/dev/urandom",O_RDONLY);
if (m_fd == -1)
{
throw std::runtime_error("open /dev/urandom");
}
#endif // WIN32
}
RandomNumberGenerator::~RandomNumberGenerator()
{
#ifndef WIN32
close(m_fd);
#endif // WIN32
}
uint8_t RandomNumberGenerator::generateByte()
{
uint8_t result;
generateBlock(&result, 1);
return result;
}
void RandomNumberGenerator::generateBlock(uint8_t * output, unsigned count)
{
#ifdef WIN32
# ifdef WORKAROUND_MS_BUG_Q258000
static MicrosoftCryptoProvider m_provider;
# endif
if (!CryptGenRandom(m_provider.GetProviderHandle(), count, output))
{
throw std::runtime_error("CryptGenRandom");
}
#else // WIN32
if (read(m_fd, output, count) != (ssize_t)count)
{
throw std::runtime_error("read /dev/urandom");
}
#endif // WIN32
}
|