-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHostKey.pas
204 lines (191 loc) · 5.42 KB
/
HostKey.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
unit HostKey;
{BUG: PublicPoWReady is set to false when PoW starts regenerating}
INTERFACE
uses ed25519,Crypto,ObjectModel;
type tEccKey=tKey32;
type tPoWRec=packed record
data:tKey32;
stamp:Word6;
end;
var SecretKey:tKey64;
var PublicKey:tEccKey;
var PublicPoW:tPoWRec;
var PublicPoWReady:boolean;
var ZeroDigest:tSha512Digest;
{-DEFINE POW_ALT}
{$IFNDEF POW_ALT}
{$IFDEF ENDIAN_LITTLE}{Intel x86}
const cPowMask0:DWORD=$FF0FFFFF;
{$ELSE}{ARM shit}
const cPowMask0:DWORD=$FFFF0FFF;
{$ENDIF}
const cPoWValidFor=5*{days}86400;
{$ELSE}
const cPowMask0:DWORD=$00FF0000;
const cPoWValidFor={1 hour}3600;
{$ENDIF}
const cHostIdent:array [1..8] of char='BNHosSW'#26;
procedure CreateChallenge(out Challenge: tKey32);
procedure CreateResponse(const Challenge: tKey32; out Response: tKey32; const srce:tEccKey);
function VerifyPoW(const proof:tPoWRec; const RemotePub:tEccKey):boolean;
IMPLEMENTATION
uses SysUtils{,DateUtils},ServerLoop,Classes,Database;
var log:tEventLog;
procedure CreateChallenge(out Challenge: tKey32);
var i:byte;
begin
for i:=0 to 31 do challenge[i]:=Random(256);
end;
procedure CreateResponse(const Challenge: tKey32; out Response: tKey32; const srce:tEccKey);
var Shared:tKey32;
var shactx:tSha256Context;
begin
ed25519.SharedSecret(shared,srce,secretkey);
SHA256_Init(shactx);
SHA256_Update(shactx,challenge,sizeof(challenge));
SHA256_Update(shactx,shared,sizeof(shared));
SHA256_Final(Response,shactx);
end;
function VerifyPoW(const proof:tPoWRec; const RemotePub:tEccKey ):boolean;
var shactx:tSha256Context;
var delta:Int64;
var digest: tKey32;
var dig_4dw: dword absolute digest[0];
begin
delta:=UnixNow-Int64(proof.stamp);
if (delta<=cPoWValidFor) and (delta>=-600) then begin
SHA256_Init(shactx);
SHA256_Update(shactx,proof,sizeof(proof));
SHA256_Update(shactx,RemotePub,sizeof(RemotePub));
SHA256_Final(digest,shactx);
result:= (dig_4dw and cPoWMask0) =0;
end else result:=false;
end;
procedure GetOSRandom(buf:pointer; cnt:word);
{$IFDEF UNIX}
var f:tHANDLE;
var q:longint;
begin
f:=FileOpen('/dev/random',fmOpenRead or fmShareDenyNone);
while (cnt>0)and(f>=0) do begin
q:=FileRead(f,buf^,cnt);
if q<=0 then break; cnt:=cnt-q; buf:=buf+q;
end;
if (f<0) or (q<=0) then begin
raise eInOutError.Create('ERROR reading /dev/random');
halt(8);
end else FileClose(f);
{$ELSE}
begin
{$WARNING Random on non-UNIX unimplemented}
Log.Error(' No random source! Using all zeros, generated keys will be shit',[]);
FillChar(out,0,cnt);
{$ENDIF}
end;
type tPoWRefreshObj=object procedure Timer; end;
var PoWGenBg:tPoWRefreshObj;
const cSeckeyFN='hostkey.dat';
const cPowDK='hostpow';
function PoWGenThr(param:pointer):ptrint;
var i:byte;
var start:tDateTime;
var shactx_initial:tSha256Context;
var shactx:tSha256Context;
var wp:packed record
cnt:DWord;
data:packed array [4..31] of byte;
stamp:Word6;
pk:tKey32;
end;
var digest: tKey32;
var dig_4dw: dword absolute digest[0];
var speed:qword;
begin result:=0;
assert(sizeof(wp)=70);
assert(sizeof(PublicPoW)=38);
SetThreadName('PoW_Gen');
wp.stamp:=Word6(UnixNow);
for i:=4 to 31 do wp.data[i]:=Random(256);
SHA256_Init(shactx_initial);
wp.pk:=PublicKey;
Start:=Now; wp.cnt:=0;
repeat
if wp.cnt=high(wp.cnt) then raise eXception.Create('some shit happend');
inc(wp.cnt);
shactx:=shactx_initial;
SHA256_Update(shactx,wp,sizeof(wp));
SHA256_Final(digest,shactx);
until (dig_4dw and cPowMask0) =0;
EnterCriticalSection(GlobalLock);
Move(wp,{->}PublicPoW,38);
try
speed:=trunc(wp.cnt*1000/(Now-start));
except
speed:=0;
end;
log.info(' PoW found in %.1Fs speed=%Sh/s',[(Now-start)*SecsPerDay,SizeToString(speed)]);
log.debug(' pow %S',[string(digest)]);
Assert(VerifyPoW(PublicPoW,PublicKey));
Database.dbSet(dbMisc,cPowDK[1],length(cPowDK),@PublicPoW,sizeof(PublicPoW));
PoWGenBg.Timer;
LeaveCriticalSection(GlobalLock);
EndThread;
end;
procedure tPoWRefreshObj.Timer;
begin
PublicPoWReady:=VerifyPoW(PublicPoW,PublicKey);
if (not PublicPoWReady)
or ( (Int64(PublicPoW.stamp)+cPoWValidFor-UnixNow) <600 )
then begin
log.info(' Started generating PoW',[]);
//PublicPoWReady:=false;
BeginThread(@PoWGenThr,nil);
end
else Shedule(120000,@timer);
end;
procedure Load;
var f:tStream;
var ident:array [1..8] of char;
begin
PublicPoWReady:=false;
{$IFDEF POW_ALT}
log.info(' Using aternate pow settings',[]);
{$endif}
try
f:=tFileStream.Create(cSeckeyFN,fmOpenRead);
try
f.Read(ident,sizeof(cHostIdent));
if CompareByte(ident,cHostIdent,8)<>0 then raise eInOutError.Create(cSeckeyFN+' invalid');
f.Read(SecretKey,sizeof(SecretKey));
finally
f.Free;
end;
except on e:eInOutError do begin
log.warn('.Load %S %S, Generating',[cSeckeyFN,e.message]);
f:=tFileStream.Create(cSeckeyFN,fmCreate);
try
GetOSRandom(@SecretKey,64);
f.Write(cHostIdent,8);
f.Write(SecretKey,64);
finally
f.Free;
end;
end end;
CreatekeyPair(PublicKey,SecretKey);
log.info(' %S %S',[cSeckeyFN,string(PublicKey)]);
f:=dbGet(dbMisc,cPoWDK,length(cPoWDK));
try
try f.Read(PublicPoW,sizeof(PublicPoW));
except ; end;
finally
f.Free;
end;
PoWGenBg.Timer;
end;
BEGIN
FillChar(ZeroDigest,sizeof(ZeroDigest),0);
ServerLoop.CreateLog(log,'HostKey');
//writeln('HostKey: Today is D',TSNow);
Load;
//writeln('HostKey: ProofOfWork valid for W',BEtoN(PublicPow.Stamp));
END.