2 Copyright (c) 2006, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 \brief Fortuna pseudo-random number generator
37 #include <openssl/aes.h>
38 #include <openssl/sha.h>
44 # include <wincrypt.h>
46 # include <KM_fileio.h>
47 const char* DEV_URANDOM = "/dev/urandom";
51 const ui32_t RNG_KEY_SIZE = 512UL;
52 const ui32_t RNG_KEY_SIZE_BITS = 256UL;
53 const ui32_t RNG_BLOCK_SIZE = 16UL;
54 const ui32_t MAX_SEQUENCE_LEN = 0x00040000UL;
57 // internal implementation class
60 KM_NO_COPY_CONSTRUCT(h__RNG);
64 byte_t m_ctr_buf[RNG_BLOCK_SIZE];
69 memset(m_ctr_buf, 0, RNG_BLOCK_SIZE);
70 byte_t rng_key[RNG_KEY_SIZE];
72 { // this block scopes the following AutoMutex so that it will be
73 // released before the call to set_key() below.
74 AutoMutex Lock(m_Lock);
77 HCRYPTPROV hProvider = 0;
78 CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
79 CryptGenRandom(hProvider, RNG_KEY_SIZE, rng_key);
81 // on POSIX systems we simply read some seed from /dev/urandom
84 Result_t result = URandom.OpenRead(DEV_URANDOM);
86 if ( KM_SUCCESS(result) )
89 result = URandom.Read(rng_key, RNG_KEY_SIZE, &read_count);
92 if ( KM_FAILURE(result) )
93 DefaultLogSink().Error("Error opening random device: %s\n", DEV_URANDOM);
96 } // end AutoMutex context
103 set_key(const byte_t* key_fodder)
110 SHA1_Update(&SHA, (byte_t*)&m_Context, sizeof(m_Context));
111 SHA1_Update(&SHA, key_fodder, RNG_KEY_SIZE);
112 SHA1_Final(sha_buf, &SHA);
114 AutoMutex Lock(m_Lock);
115 AES_set_encrypt_key(sha_buf, RNG_KEY_SIZE_BITS, &m_Context);
116 *(ui32_t*)(m_ctr_buf + 12) = 1;
121 fill_rand(byte_t* buf, ui32_t len)
123 assert(len <= MAX_SEQUENCE_LEN);
124 ui32_t gen_count = 0;
125 AutoMutex Lock(m_Lock);
127 while ( gen_count + RNG_BLOCK_SIZE <= len )
129 AES_encrypt(m_ctr_buf, buf + gen_count, &m_Context);
130 *(ui32_t*)(m_ctr_buf + 12) += 1;
131 gen_count += RNG_BLOCK_SIZE;
134 if ( len != gen_count ) // partial count needed?
136 byte_t tmp[RNG_BLOCK_SIZE];
137 AES_encrypt(m_ctr_buf, tmp, &m_Context);
138 *(ui32_t*)(m_ctr_buf + 12) += 1;
139 memcpy(buf, tmp, len - gen_count);
145 static h__RNG* s_RNG = 0;
148 //------------------------------------------------------------------------------------------
152 Kumu::FortunaRNG::FortunaRNG()
158 Kumu::FortunaRNG::~FortunaRNG() {}
162 Kumu::FortunaRNG::FillRandom(byte_t* buf, ui32_t len)
169 // 2^20 bytes max per seeding, use 2^19 to save
170 // room for generating reseed values
171 ui32_t gen_size = xmin(len, MAX_SEQUENCE_LEN);
172 s_RNG->fill_rand(buf, gen_size);
176 // re-seed the generator
177 byte_t rng_key[RNG_KEY_SIZE];
178 s_RNG->fill_rand(rng_key, RNG_KEY_SIZE);
179 s_RNG->set_key(rng_key);
187 Kumu::FortunaRNG::FillRandom(Kumu::ByteString& Buffer)
189 FillRandom(Buffer.Data(), Buffer.Capacity());
190 Buffer.Length(Buffer.Capacity());
191 return Buffer.Data();