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
|
// RandGen.cpp
#include "StdAfx.h"
#ifndef _7ZIP_ST
#include "../../Windows/Synchronization.h"
#endif
#include "RandGen.h"
#ifndef _WIN32
#include <unistd.h>
#define USE_POSIX_TIME
#define USE_POSIX_TIME2
#endif
#ifdef USE_POSIX_TIME
#include <time.h>
#ifdef USE_POSIX_TIME2
#include <sys/time.h>
#endif
#endif
// This is not very good random number generator.
// Please use it only for salt.
// First generated data block depends from timer and processID.
// Other generated data blocks depend from previous state
// Maybe it's possible to restore original timer value from generated value.
#define HASH_UPD(x) Sha256_Update(&hash, (const Byte *)&x, sizeof(x));
void CRandomGenerator::Init()
{
CSha256 hash;
Sha256_Init(&hash);
#ifdef _WIN32
DWORD w = ::GetCurrentProcessId();
HASH_UPD(w);
w = ::GetCurrentThreadId();
HASH_UPD(w);
#else
pid_t pid = getpid();
HASH_UPD(pid);
pid = getppid();
HASH_UPD(pid);
#endif
for (unsigned i = 0; i <
#ifdef _DEBUG
2;
#else
1000;
#endif
i++)
{
#ifdef _WIN32
LARGE_INTEGER v;
if (::QueryPerformanceCounter(&v))
HASH_UPD(v.QuadPart);
#endif
#ifdef USE_POSIX_TIME
#ifdef USE_POSIX_TIME2
timeval v;
if (gettimeofday(&v, 0) == 0)
{
HASH_UPD(v.tv_sec);
HASH_UPD(v.tv_usec);
}
#endif
time_t v2 = time(NULL);
HASH_UPD(v2);
#endif
#ifdef _WIN32
DWORD tickCount = ::GetTickCount();
HASH_UPD(tickCount);
#endif
for (unsigned j = 0; j < 100; j++)
{
Sha256_Final(&hash, _buff);
Sha256_Init(&hash);
Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE);
}
}
Sha256_Final(&hash, _buff);
_needInit = false;
}
#ifndef _7ZIP_ST
static NWindows::NSynchronization::CCriticalSection g_CriticalSection;
#define MT_LOCK NWindows::NSynchronization::CCriticalSectionLock lock(g_CriticalSection);
#else
#define MT_LOCK
#endif
void CRandomGenerator::Generate(Byte *data, unsigned size)
{
MT_LOCK
if (_needInit)
Init();
while (size != 0)
{
CSha256 hash;
Sha256_Init(&hash);
Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE);
Sha256_Final(&hash, _buff);
Sha256_Init(&hash);
UInt32 salt = 0xF672ABD1;
HASH_UPD(salt);
Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE);
Byte buff[SHA256_DIGEST_SIZE];
Sha256_Final(&hash, buff);
for (unsigned i = 0; i < SHA256_DIGEST_SIZE && size != 0; i++, size--)
*data++ = buff[i];
}
}
CRandomGenerator g_RandomGenerator;
|