[d6386d]: pwsafe / pwsafe / corelib / PWSrand.cpp Maximize Restore History

Download this file

PWSrand.cpp    175 lines (145 with data), 4.3 kB

  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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 2003-2007 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license.php
*/
#include "PwsPlatform.h"
#include <limits.h>
#include <stdlib.h>
#ifndef POCKET_PC
#include <process.h>
#endif
#ifdef POCKET_PC
#include <wce_time.h>
#define time(timer) wceex_time(timer)
#endif
#include "PWSrand.h"
// See the MSDN documentation for RtlGenRandom. We will try to load it
// and if that fails, use our own random number generator. The function
// call is indirected through a function pointer
static BOOLEAN (APIENTRY *pfnGetRandomData)(void*, ULONG) = NULL;
static bool __stdcall LoadRandomDataFunction();
PWSrand *PWSrand::self = NULL;
PWSrand *PWSrand::GetInstance()
{
if (self == NULL) {
self = new PWSrand;
}
return self;
}
void PWSrand::DeleteInstance()
{
delete self;
self = NULL;
}
PWSrand::PWSrand()
: ibRandomData(SHA256::HASHLEN)
{
m_IsInternalPRNG = !LoadRandomDataFunction();
SHA256 s;
time_t t = time(NULL);
DWORD pid = GetCurrentProcessId();
DWORD ticks = GetTickCount();
s.Update((const unsigned char *)&t, sizeof(t));
s.Update((const unsigned char *)&pid, sizeof(pid));
s.Update((const unsigned char *)&ticks, sizeof(ticks));
s.Final(K);
}
PWSrand::~PWSrand()
{
}
void PWSrand::AddEntropy(unsigned char *bytes, unsigned int numBytes)
{
ASSERT(bytes != NULL);
SHA256 s;
s.Update(K, sizeof(K));
s.Update(bytes, numBytes);
s.Final(K);
}
void PWSrand::NextRandBlock()
{
SHA256 s;
s.Update(K, sizeof(K));
s.Final(R);
/// problem: Kp is a 32 bit pointer to K, which is declared as unsigned char.
/// this in turn is 1 byte long and does not start aligned. Hence, Kp[0] is an access to unaliged data
unsigned __unaligned int *Kp = (unsigned int *)K;
unsigned __unaligned int *Rp = (unsigned int *)R;
const int N = SHA256::HASHLEN/sizeof(unsigned int);
Kp[0]++;
for (int i = 0; i < N; i++)
Kp[i] += Rp[i];
}
void PWSrand::GetRandomData( void * const buffer, unsigned long length )
{
if (!m_IsInternalPRNG) {
ASSERT(pfnGetRandomData != NULL);
(void)(*pfnGetRandomData)(buffer, length);
} else {
unsigned char *pb = (unsigned char *)buffer;
while (length > SHA256::HASHLEN) {
NextRandBlock();
for (int j = 0; j < SHA256::HASHLEN; j++)
pb[j] = R[j];
length -= SHA256::HASHLEN;
pb += SHA256::HASHLEN;
}
ASSERT(length <= SHA256::HASHLEN);
if (length > 0) {
unsigned long i = 0;
NextRandBlock();
while (i < length) {
pb[i] = R[i];
i++;
}
}
}
}
// generate random numbers from a buffer filled in by GetRandomData()
unsigned int PWSrand::RandUInt()
{
// we don't want to keep filling the random buffer for each number we
// want, so fill the buffer with random data and use it up
if( ibRandomData > ( SHA256::HASHLEN - sizeof( unsigned int ) ) ) {
// no data left, refill the buffer
GetRandomData(rgbRandomData, SHA256::HASHLEN);
ibRandomData = 0;
}
const unsigned int u =
*(reinterpret_cast<unsigned int *>(rgbRandomData + ibRandomData));
ibRandomData += sizeof(unsigned int);
return u;
}
/*
* RangeRand(len)
*
* Returns a random number in the range 0 to (len-1).
* For example, RangeRand(256) returns a value from 0 to 255.
*/
unsigned int PWSrand::RangeRand(size_t len)
{
unsigned int r;
const unsigned int ceil = UINT_MAX - (UINT_MAX % len) - 1;
while ((r = RandUInt()) > ceil)
;
return(r%len);
}
static bool __stdcall LoadRandomDataFunction()
{
#ifdef POCKET_PC
// Pocket PC does not support this random number generator funtion; returning false enables internal PRNG
return false;
#else
HMODULE hLib = LoadLibrary(_T("ADVAPI32.DLL"));
BOOLEAN (APIENTRY *pfnGetRandomDataT)(void*, ULONG) = NULL;
if (hLib != NULL) {
pfnGetRandomDataT = (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036");
if (pfnGetRandomDataT) {
pfnGetRandomData = pfnGetRandomDataT;
}
}
return (hLib != NULL && pfnGetRandomDataT != NULL);
#endif
}