-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrand.pas
264 lines (227 loc) · 5.93 KB
/
rand.pas
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
unit Rand;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
{ TEntropyAccumulator }
TEntropyAccumulator = class(TObject)
private
FArray: array[1..8] of Integer;
FMaxBits: Integer;
FStream: TMemoryStream;
FCounter: Byte;
FAccumulator: Cardinal;
FQuality: Double;
FOnEntropy: TNotifyEvent;
function GetData: Pointer;
function GetNumBits: Integer;
function GetSize: Integer;
procedure UpdateStats(DW: Cardinal);
public
constructor Create;
destructor Destroy;
//
procedure Reset;
procedure MouseMove(X,Y: Integer);
function Read: Cardinal;
property Data: Pointer read GetData;
property Size: Integer read GetSize;
property NumBits: Integer read GetNumBits;
property Quality: Double read FQuality;
property MaxBits: Integer read FMaxBits write FMaxBits;
property OnEntropy: TNotifyEvent read FOnEntropy write FOnEntropy;
end;
{ TLinearFeedbackShiftRegister }
TLinearFeedbackShiftRegister = class(TObject)
private
FSeed: Cardinal;
FCurrent: Cardinal;
procedure SetSeed(AValue: Cardinal);
public
constructor Create(ASeed: Cardinal = 0);
function Next: Cardinal;
procedure Reset;
property Seed: Cardinal read FSeed write SetSeed;
end;
{ TRandomNumberGenerator }
TRandomNumberGenerator = class(TObject)
private
FLFSR: TLinearFeedbackShiftRegister;
FEntropy: TEntropyAccumulator;
public
constructor Create;
destructor Destroy; override;
function Generate: Cardinal;
//
property Entropy: TEntropyAccumulator read FEntropy write FEntropy;
end;
var
RNG: TRandomNumberGenerator;
procedure FillRandom(Data: Pointer; Size: Integer);
implementation
uses
DateUtils, CryptoUtils;
procedure FillRandom(Data: Pointer; Size: Integer);
var
X: Integer;
R: Cardinal;
begin
for X := 1 to Size div 4 do
begin
R := RNG.Generate;
System.Move(R,Pointer(PtrUInt(Data)+((X-1) * SizeOf(R)))^,SizeOf(R));
end;
X := Size mod 4;
if X > 0 then
begin
R := RNG.Generate;
System.Move(R,Pointer(PtrUInt(Data)+((Size div 4) * SizeOf(R)))^,X);
end;
end;
{ TLinearFeedbackShiftRegister }
constructor TLinearFeedbackShiftRegister.Create(ASeed: Cardinal = 0);
begin
if ASeed = 0 then
Seed := Random($FFFFFFFE) + 1
else
Seed := ASeed;
end;
procedure TLinearFeedbackShiftRegister.SetSeed(AValue: Cardinal);
begin
if AValue = 0 then
AValue := $FFFFFFFF;
FSeed := AValue;
FCurrent := AValue;
end;
function TLinearFeedbackShiftRegister.Next: Cardinal;
begin
Result := LFSR(FCurrent);
if FCurrent = 0 then
FCurrent := $FFFFFFFF;
end;
procedure TLinearFeedbackShiftRegister.Reset;
begin
FCurrent := FSeed;
end;
{ TRandomNumberGenerator }
constructor TRandomNumberGenerator.Create;
begin
FLFSR := TLinearFeedbackShiftRegister.Create;
end;
destructor TRandomNumberGenerator.Destroy;
begin
FLFSR.Free;
inherited Destroy;
end;
function TRandomNumberGenerator.Generate: Cardinal;
begin
Result := FLFSR.Next xor Random($FFFFFFFF);
if (FEntropy <> nil) and (FEntropy.Size > SizeOf(Cardinal)) then
Result := FEntropy.Read xor Result;
end;
{ TEntropyAccumulator }
constructor TEntropyAccumulator.Create;
begin
FStream := TMemoryStream.Create;
FMaxBits := 2048;
FAccumulator := Random($FFFFFFFF);
end;
destructor TEntropyAccumulator.Destroy;
begin
FStream.Destroy;
end;
procedure TEntropyAccumulator.Reset;
var
X: Integer;
begin
FStream.Size := 0;
FQuality := 0;
FCounter := 0;
for X := 1 to 8 do
FArray[X] := 0;
end;
function TEntropyAccumulator.GetData: Pointer;
begin
Result := FStream.Memory;
end;
function TEntropyAccumulator.GetNumBits: Integer;
begin
Result := FStream.Size * 8;
end;
function TEntropyAccumulator.GetSize: Integer;
begin
Result := FStream.Size;
end;
procedure TEntropyAccumulator.UpdateStats(DW: Cardinal);
var
D: array[1..4] of Integer;
Q: Double;
X: Integer;
B: Byte;
begin
D[1] := DW and $FF;
D[2] := (DW and $FF00) shr 8;
D[3] := (DW and $FF0000) shr 16;
D[4] := (DW and $FF000000) shr 24;
// inc/dec bit array
for X := 1 to 4 do
begin
B := D[X];
if B and $80 > 0 then inc(FArray[1]) else dec(FArray[1]);
if B and $40 > 0 then inc(FArray[2]) else dec(FArray[2]);
if B and $20 > 0 then inc(FArray[3]) else dec(FArray[3]);
if B and $10 > 0 then inc(FArray[4]) else dec(FArray[4]);
if B and $8 > 0 then inc(FArray[5]) else dec(FArray[5]);
if B and $4 > 0 then inc(FArray[6]) else dec(FArray[6]);
if B and $2 > 0 then inc(FArray[7]) else dec(FArray[7]);
if B and $1 > 0 then inc(FArray[8]) else dec(FArray[8]);
end;
// Calculate a quality score
Q := 0;
for X := 1 to 8 do
Q := Q + (1-(Abs(FArray[X])/FStream.Size))*100;
FQuality := Q / 8;
end;
procedure TEntropyAccumulator.MouseMove(X, Y: Integer);
var
B,S: Byte;
Q: Cardinal = 0;
begin
if FStream.Size < (FMaxBits div 8) then
begin
if FCounter mod 2 = 1 then
B := ((X mod 16) shl 4) + (y mod 16)
else
B := ((Y mod 16) shl 4) + (X mod 16);
S := (FCounter mod 4) * 8;
Q := B shl S;
FAccumulator := LFSR(FAccumulator) xor Q;
inc(FCounter);
if FCounter mod 16 = 0 then
begin
FStream.Write(FAccumulator,SizeOf(Cardinal));
UpdateStats(FAccumulator);
end;
if FCounter = 128 then FCounter := 0;
if Assigned(FOnEntropy) and (FStream.Size >= (FMaxBits div 8)) then
FOnEntropy(Self);
end;
end;
function TEntropyAccumulator.Read: Cardinal;
begin
if FStream.Size > SizeOf(Cardinal) then
begin
FStream.Position := FStream.Size - SizeOf(Cardinal);
FStream.Read(Result,SizeOf(Result));
FStream.Size := FStream.Size - SizeOf(Cardinal);
end
else
Result := Random($FFFFFFFF);
end;
initialization
Randomize;
RNG := TRandomNumberGenerator.Create;
finalization
RNG.Free;
end.