-
Notifications
You must be signed in to change notification settings - Fork 0
/
bnedit.pas
696 lines (663 loc) · 20.1 KB
/
bnedit.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
program bnedit;
(*command line program for brodnet
view and edit all brodnet files
bnedit file.dat : decode and display
bnedit prof[ile] profile.dat login.dat : edit profile file
bnedit login login.dat cert.dat : generate login from cert
bnedit host host-key.dat : generate host key or renew PoW
*)
USES Profile,Message,ObjectModel,SysUtils,DateUtils,ed25519,Crypto;
{$LINKLIB c}
{$INCLUDE gitver.inc}
const
cCertKeyIdent:array [1..8] of char='BNCertS'#26;
cMasterKeyIdent:array [1..8] of char='BNMastS'#26;
cLoginIdent:array [1..8] of char='BNLogin'#26;
cSecretIdent:array [1..8] of char='BNSecKs'#26;
cHostIdent:array [1..8] of char='BNHosSW'#26;
const helphint='Run bnedit without parameters to get help.';
function GetPubFromNodeKey(const fn:string; save_secret:pointer=nil ):tKey32;
var s:tFileStream;
var sec:tKey64;
begin
s.OpenRO(fn); try
s.read(sec,64);
finally s.done end;
CreateKeyPair(result, sec);
if assigned(save_secret) then Move(sec,save_secret^,64);
FillChar(sec,sizeof(sec),0);
end;
function UnixTimeToStr(u:Int64):ansistring;
begin
DateTimeToString(result, 'dd.mm.yyyy hh:nn', UnixToDateTime(u));
end;
procedure DumpProfile(var txt:text; var pfs:tFileStream);
var pf:tProfileRead;
var i:integer;
begin
pfs.Seek(0);
pf.ReadFrom(pfs,0);
Writeln('ID: ',string(pf.ProfID));
Writeln('Updated: ',UnixTimeToStr(pf.updated),' UTC');
Writeln('Expires: ',UnixTimeToStr(pf.expires),' UTC');
writeln('Nick: ',pf.nick);
Writeln('FullName: ',pf.FullName);
Writeln('TextNote: ',pf.TextNote);
Writeln('Encryption key: ',string(pf.encr_key));
Writeln('Old keys: ',pf.cOldKeys);
for i:=0 to pf.cHosts-1
do writeln('Host: ',string(pf.Hosts[i]));
for i:=0 to pf.cHosts-1
do writeln('BackupHost: ',string(pf.Hosts[i]));
writeln('Valid: ',pf.Valid);
//CertExpired,CertInval,SigInval:boolean;
pf.Done;
end;
const first_sec_ofs=8+6+32+64+64;
procedure ShowSecretID(var pfs:tFileStream);
var pk:tKey32;
var id:tKey24;
var use:char;
var left:LongWord;
var msg:string;
begin
pfs.Seek(8+6);
pfs.Read(pk,32);
SHA256_Buffer(id,24,pk,32);
id[23]:=id[23] and 254;
writeln('ID: ',string(id));
writeln('PUB L ',string(pk));
pfs.Seek(first_sec_ofs);
left:=pfs.Length-first_sec_ofs;
msg:='SEC X XXXXXXXXXXXXXX...';
while left>=72 do begin
pfs.Read(id,7);
use:=char(pfs.ReadByte);
pfs.Skip(64);
dec(left,72);
msg[5]:=char(use);
BinToHex(@msg[7],id,7);
writeln(msg);
end;
if left>0 then writeln('Warning: garbage at end of file.');
end;
procedure ShowHostKeyID(var pfs:tFileStream);
var sk:tKey64;
var pk:tKey32;
var id:tKey20;
begin
pfs.Read(sk,64);
ed25519.CreateKeyPair(pk,sk);
SHA256_Buffer(id,20,pk,32);
writeln('PUB: ',string(pk));
writeln('ID: ',string(id));
end;
procedure DumpMessage(var txt:text; var pfs:tFileStream);
var m:tMessageRead;
var i:integer;
var s:string[9]='XXXXXX...';
begin
pfs.Seek(0);
m.ReadFrom(pfs,0);
Writeln('SenderID: ',string(m.SenderID));
writeln('sender_encr_key: ',string(m.sender_encr_key));
Writeln('Created: ',UnixTimeToStr(m.created),' UTC');
Writeln('Expires: ',UnixTimeToStr(m.expires),' UTC');
Writeln('Flags: ',m.flags);
for i:=0 to m.cRcpts-1 do begin
Writeln('Recipient: ',string(m.rcpts[i].id));
BinToHex(@s[1],m.rcpts[i].prefix,3);
writeln(' priority=',m.rcpts[i].priority, ' key=',s);
end;
m.Done;
end;
procedure DisplayInfoAboutFile(whatfile:string);
var s:tFileStream;
var ident:packed array [1..8] of char;
var tokyoident:array [1..8] of char='ToKyO Ca';
var hkvsident:array [1..3] of char='Bd'#26;
var stpakoident:array [1..8] of char='BNStPAK'#26;
var i:integer;
begin
s.OpenRO(whatfile);
try
s.Read(ident,8);
if CompareByte(ident,Profile.cMagic,7)=0 then begin
writeln(whatfile,': BrodNet Profile v',byte(ident[8]));
DumpProfile(stdout,s);
end else
if CompareByte(ident,Message.cMagic,8)=0 then begin
writeln(whatfile,': BrodNet Message');
DumpMessage(stdout,s);
end else
if CompareByte(ident,tokyoident,8)=0 then begin
writeln(whatfile,': Tokyo Cabinet database file');
end else
if CompareByte(ident,hkvsident,3)=0 then begin
writeln(whatfile,': HKVS (old brodnet db)');
end else
if CompareByte(ident,stpakoident,8)=0 then begin
writeln(whatfile,': BrodNet Object Store Pack');
end else
if CompareByte(ident,cCertKeyIdent,8)=0 then begin
writeln(whatfile,': BrodNet Login CertSecret (old format)');
writeln('Unencrypted user master key.');
writeln('Change password to convert to new format.');
end else
if CompareByte(ident,cLoginIdent,8)=0 then begin
writeln(whatfile,': BrodNet User Login (old format)');
end else
if CompareByte(ident,cMasterKeyIdent,8)=0 then begin
writeln(whatfile,': BrodNet User Master Secret Key');
end else
if CompareByte(ident,cSecretIdent,8)=0 then begin
writeln(whatfile,': BrodNet User Secret Keyring');
writeln('This file is used to edit your profile,'#10+
'decrypt your messages and identify you.');
ShowSecretID(s);
end else
if CompareByte(ident,cHostIdent,8)=0 then begin
writeln(whatfile,': BrodNet Host key');
ShowHostKeyID(s);
end else
begin
write(whatfile,': Unknown File, ');
for i:=1 to 8 do begin
if ident[i] in [' '..'~']
then write(ident[i])
else write('_');
end;
writeln;
writeln('Try "file" command to get more information.');
end;
finally
s.Done;
end;
end;
procedure GetOSRandom(buf:pointer; cnt:word);
{$IFDEF UNIX}
var f:tHANDLE;
var q:longint;
begin
flush(output);
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
writeln('ERROR reading /dev/random');
halt(8);
end else FileClose(f);
{$ELSE}
begin
{$WARNING Random on non-UNIX unimplemented}
writeln('[WARNING: No random source! Using all zeros, generated keys will be shit.]');
FillChar(out,0,cnt);
{$ENDIF}
end;
procedure EncryptSaveMasterKey(var mf: tCommonStream; var mastersec:tKey64);
var wrapped: array [1..72] of byte;
var kek: tKey32;
var exkek: tAES_key;
var pwd:string;
begin
write('Enter new password for master key: ');
readln(pwd);
if (length(pwd)<4)or(length(pwd)>56) then begin
writeln('Error: password must be 4 to 56 bytes long');
halt(3);
end;
SHA256_Buffer( kek, 32, pwd[1], length(pwd));
AES_set_encrypt_key( kek, 256, exkek);
AES_wrap_key(exkek, nil, wrapped, mastersec, 64);
FillChar(masterSec,sizeof(masterSec),0);
mf.Write(cMasterKeyIdent,8);
mf.Write(wrapped,72);
end;
procedure LoadDecryptMasterKey(var mf: tCommonStream; out mastersec:tKey64; out master:tKey32);
var wrapped: array [1..72] of byte;
var kek: tKey32;
var exkek: tAES_key;
var pwd:string;
var ident:array [1..8] of char;
begin
mf.Read(Ident,8);
if CompareByte(ident,cCertKeyIdent,8)=0 then begin
mf.Read(mastersec,64);
ed25519.CreateKeyPair(master,mastersec);
end else
if CompareByte(ident,cMasterKeyIdent,8)=0 then begin
mf.Read(wrapped,72);
write('Enter password for master key: ');
readln(pwd);
if (length(pwd)<4)or(length(pwd)>56) then begin
writeln('Error: password must be 4 to 56 bytes long');
halt(3);
end;
SHA256_Buffer( kek, 32, pwd[1], length(pwd));
AES_set_decrypt_key( kek, 256, exkek);
if AES_unwrap_key(exkek, nil, mastersec, wrapped, 72)<>64 then begin
writeln('Decryption failed. Invalid password or corrupt file.');
halt(3);
end;
ed25519.CreateKeyPair(master,mastersec);
end else raise eFormatError.Create('Invalid Master key file');
end;
procedure KeyringAdd(var lf:tCommonStream; var pub:tKey32; var sec:tKey64; use:char; front:boolean);
var fkd: array [1..72] of byte;
var fs: LongWord;
begin
fs:=lf.Length;
assert(fs>=first_sec_ofs);
if fs<(first_sec_ofs+72) then begin
front:=false;
fs:=first_sec_ofs;
end;
if not front then begin
lf.Seek(fs);
lf.Write(pub,8);
lf.Write(sec,64);
end else begin
lf.Seek(first_sec_ofs);
lf.Read(fkd,72);
lf.Seek(first_sec_ofs);
lf.Write(pub,7);
lf.WriteByte(ord(use));
lf.Write(sec,64);
lf.Seek(fs);
lf.Write(fkd,72);
end;
end;
procedure EditKeyGen; {bnedit keygen secret.dat master.dat}
var lf,mf:tFileStream;
var genmaster:boolean;
var mastersec:tKey64;
var loginsec, encrsec: tkey64;
var encrpub: tKey32;
var signature: tKey64;
var dbg: tKey20;
var ld:packed record
expires: word6;
master: tKey32;
login: tKey32;
end;
begin
if paramcount<>3 then begin writeln(helphint); halt(9) end;
try
mf.OpenRO(paramstr(3));genmaster:=false;
except
mf.OpenRW(paramstr(3));genmaster:=true;
end;
if Genmaster then begin
writeln('Generating new master key');
GetOSRandom(@mastersec,64);
EncryptSaveMasterKey(mf, mastersec);
end;
mf.Seek(0);
LoadDecryptMasterKey(mf, mastersec, ld.master);
writeln('Master Key loaded, pub=',string(ld.master));
SHA256_Buffer(dbg,20,ld.master,32);
writeln('ID=',string(dbg));
mf.Done;
write ('Generate signature key ');
GetOSRandom(@loginsec,64);
CreateKeyPair(ld.login,loginsec);
writeln('pub=',string(ld.login));
ld.expires:=Word6(UnixNow + 15778463); {6 months}
writeln('Expiration set to 6 months.');
SHA256_Buffer(dbg,20,ld,sizeof(ld));
writeln('Signing. ',string(dbg));
Sign(signature, ld, sizeof(ld), ld.master, masterSec);
write ('Generate encryption key ');
GetOSRandom(@encrsec,64);
CreateKeyPair(encrpub,encrsec);
writeln('pub=',string(encrpub));
lf.OpenRW(paramstr(2)); //todo check ident bytes
lf.Write(cSecretIdent,8);
lf.Write(ld.expires,6);
lf.Write(ld.master,32);
lf.Write(signature,64);
lf.Write(loginsec,64);
KeyringAdd(lf,encrpub,encrsec,'M',true);
lf.Done;
FillChar(masterSec,64,0);
FillChar(encrsec,64,0);
FillChar(loginsec,64,0);
writeln('* For the Love of Gaben, store ',paramstr(3));
writeln('* on safe removable media and don''t forget your password,');
writeln('* becouse it can be used to take over your identity!');
end;
procedure EditMasterPassword; {bnedit keygen master.dat}
var mf:tFileStream;
var mastersec: tKey64;
var master: tKey32;
begin
if paramcount<>2 then begin writeln(helphint); halt(9) end;
mf.OpenRW(paramstr(2));
LoadDecryptMasterKey(mf, mastersec, master);
writeln('Master Key loaded, pub=',string(master));
mf.seek(0);
EncryptSaveMasterKey(mf, mastersec);
mf.Done;
FillChar(masterSec,sizeof(masterSec),0);
writeln('* For the Love of Gaben, store ',paramstr(2));
writeln('* on safe removable media and don''t forget your password,');
writeln('* becouse it can be used to take over your identity!');
end;
procedure KeyringToProfile(var ls:tCommonStream; var pf:tProfileRead; out LoginSec:tKey64);
var Ident:array [1..8] of char;
var fk: packed record
p: array [1..7] of byte;
u: char;
s: tKey64;
end;
var count,i,j:Integer;
begin
ls.Read(ident,8);
if CompareByte(ident,cSecretIdent,8)<>0
then raise eFormatError.Create('Keyring file invalid');
pf.expires:=ls.ReadWord6;
ls.Read(pf.master_key,32);
ls.Read(pf.master_sig,64);
ls.Read(LoginSec,64);
CreateKeyPair(pf.sign_key,{<-}LoginSec);
ls.seek(first_sec_ofs); {begin reading secret keys}
count:=((ls.Length-first_sec_ofs) div 72);
ls.Read(fk,72); {first key has special place}
CreateKeyPair(pf.encr_key,{<-}fk.s);
pf.cOldKeys:=0;
j:=0;
pf.OldKeys:=GetMem(pf.cOldKeys+1); {+1 is for regenerate}
for i:=0 to count-2 do begin
ls.Read(fk,72);
if fk.u='M' then begin
CreateKeyPair(pf.OldKeys[j],fk.s);
inc(pf.cOldKeys);
inc(j);
end;
end;
end;
function CheckNick(var nick:string):boolean;
var i:integer;
begin
result:=false;
if length(nick)>12 then exit;
for i:=1 to length(nick) do if nick[i] in [#0..#31,' ',#127..#255]
then exit;
result:=true;
end;
procedure EditProfile;
var pfs,ls:tFileStream;
var pf:tProfileRead;
var LoginSec:tKey64;
var cmd,param:string;
var tmpp:LongWord;
begin
if paramcount<>3 then begin writeln(helphint); halt(9) end;
{additional params:
... newenc
... addhost hostkey.dat
... addhost HOSTKEY
}
try
pfs.OpenRW(paramstr(3));
if pfs.Length=0 then begin
pf.InitEmpty;
writeln('Initialized empty profile');
end else begin
pf.ReadFrom(pfs,0);
end;
except
on e:exception do begin
writeln(paramstr(3)+': '+e.Message);
writeln('If you specify non-existent file as parameter 3, empty profile will be created');
halt(2);
end;
end;
try
ls.OpenRO(paramstr(2));
KeyringToProfile(ls, pf, LoginSec);
ls.Done;
except
on e:eInvalidOP{exception} do begin
writeln(paramstr(2)+': '+e.Message);
writeln('You can (re-)generate your login. ', helphint);
halt(3);
end;
end;
writeln('waiting for commands...');
repeat
readln(input,cmd);
if (cmd='')or(cmd='QUIT')or(cmd='SAVE')or(cmd='EXIT') then break;
tmpp:=pos(' ',cmd);
if tmpp>0 then begin
param:=copy(cmd,tmpp+1,255);
if cmd[tmpp-1]=':' then dec(tmpp);
cmd:=copy(cmd,1,tmpp-1);
end else param:='';
cmd:=upcase(cmd);
if cmd='NAME' then begin
pf.FullName:=param;
end else if cmd='NICK' then begin
if CheckNick(param)
then pf.nick:=param
else writeln('Error: Invalid Nickname. Max 12 characters and ASCII only [#33..#126].');
end else if cmd='NOTE' then begin
pf.TextNote:=param;
end else if cmd='NEWKEY' then begin
writeln('Error: not implemented.');
end else if (cmd='HELP')or(cmd='?') then begin
writeln('--commands available for profile editing--');
writeln(' syntax: COMMAND parameter...');
writeln(' semicolon in COMMAND is ignored, command is not case-sensitive');
writeln('NAME',' Full Name ;set your name');
writeln('NICK',' nick ;set your nick name, max 8 ASCII chars #32..#126');
writeln('NOTE',' text ;set additional text, max 255 bytes');
writeln('NEWKEY',' ;generate new encryption key');
writeln('EXIT',' ;save changes and exit (empty line equals EXIT)');
end else if (cmd='')or(cmd='QUIT')or(cmd='SAVE')or(cmd='EXIT')
then break
else writeln('Error: Unknown command: ',cmd,' (use HELP command)');
until EOF(input);
pfs.Seek(0);
pfs.Trunc(0);
pf.Updated:=UnixNow;
pf.WriteTo(pfs,LoginSec);
pfs.Done;
FillChar(loginsec,sizeof(loginsec),0);
writeln('Changes Saved.');
end;
procedure MessageEncryptData(
var inp: tCommonStream;
var outp: TCommonStream;
var seskx: tAES_FB;
var hmacx: tSHA256 );
var gzc: tGZip;
var minleft: LongWord;
var minbuf: array [1..2048] of byte;
var mencr, mdefl: array [1..16] of byte;
begin
FillChar(seskx.feedback,16,0);
gzc.InitDeflate;
gzc.avail_out:=sizeof(mdefl);
gzc.next_out:=@mdefl;
minleft:=inp.Length;
repeat
if (gzc.avail_in=0) and (minleft>0) then begin
gzc.avail_in:=sizeof(minbuf);
gzc.next_in:=@minbuf;
if minleft<sizeof(minbuf) then gzc.avail_in:=minleft;
inp.Read(minbuf,gzc.avail_in);
hmacx.Update(minbuf,gzc.avail_in);
minleft:=minleft-gzc.avail_in;
end;
gzc.Deflate;
if gzc.eof then begin
FillChar(gzc.next_out,gzc.avail_out,0);
gzc.avail_out:=0;
end;
if gzc.avail_out=0 then begin
seskx.EnCryptPCBC(mencr, mdefl);
outp.Write(mencr, sizeof(mencr));
gzc.avail_out:=sizeof(mdefl);
gzc.next_out:=@mdefl;
end;
until gzc.eof;
end;
procedure MessageEncrypt;
var myID : tKey20;
var myKey: tKey64;
var myKeyPub: tkey32;
procedure LoadKeyring;
var keyring :tFileStream;
var use:char;
begin
{read ID and latest enck from keyring}
keyring.OpenRO(paramstr(2));
keyring.Seek(8+6);
keyring.Read(myKeyPub,32);
SHA256_Buffer(myid,20,myKeyPub,32);
writeln('myID: ':10,string(myid));
keyring.Seek(first_sec_ofs+7);
use:=char(keyring.ReadByte);
keyring.Read(myKey,64);
ed25519.CreateKeyPair(myKeyPub, myKey);
writeln('myKeyPub ':10,string(myKeyPub),' use=',use);
keyring.Done;
end;
var mack: array of tKey64;
var session: tkey32;
var outmsg: tFileStream;
var hash: tSHA256;
procedure LoadRecipients;
var ob: tMemoryStream;
var i,count:integer;
var rfprof:tFileStream;
var rprof: tProfileRead;
var tmp: packed record
ss: tkey32;
ts: Word6;
ix: Word2;
end;
var kek:tKey32;
var wrap: array [1..40] of byte;
var exkek: tAES;
begin
count:=ParamCount-4;
SetLength(mack,count);
tmp.ts:=UnixNow;
ob.Init(72+(64*count));
ob.Write(Message.cMagic,8);
ob.Write(MyID,20);
ob.Write(MyKeyPub,32);
ob.Write(tmp.ts,6);
ob.WriteByte(count);
ob.WriteByte(0); {flags}
ob.WriteWord4(1209600); {expire}
for i:=0 to count-1 do begin
writeln('rcpt prof ',paramstr(5+i));
rfprof.OpenRO(paramstr(5+i));
rprof.ReadFrom(rfprof,0);
rfprof.Done;
writeln('rcpt ID ':10,string(rprof.profID),' ',rprof.Nick);
writeln('rcpt encr ':10,string(rprof.encr_key));
SharedSecret(tmp.ss, rprof.encr_key, myKey);
tmp.ix:=$424D; SHA256_Buffer( kek, 32, tmp, 40);
tmp.ix:=$424F; SHA256_Buffer( mack[i], 32, tmp, 40);
writeln('kek ':10,string(kek));
writeln('mack ':10,string(mack[i]));
exkek.InitEnCrypt(kek, 256);
exkek.Wrap(wrap, session, 32, nil);
ob.Write(rprof.profid,20);
ob.Write(rprof.encr_key,3);
ob.WriteByte(69); {priority}
ob.Write(wrap,40);
end;
hash.Update(ob.base^, ob.vlength);
outmsg.Write(ob.base^, ob.vlength);
end;
procedure WriteMAC;
var i: integer;
var h1, h2: tKey32;
var hc: tSHA256;
begin
hash.Final(h1);
writeln('hash ':10,string(h1));
for i:=0 to high(kek) do begin
hc.Init;
hc.Update(mack[i],32);
hc.Update(h1,32);
hc.Final(h2);
outmsg.Write(h2,32);
end;
end;
var inmsg: tFileStream;
var seskx: tAES_FB;
begin
writeln('WIP message encrypt');
writeln('inmsg '+paramstr(4));
inmsg.OpenRO(paramstr(4));
writeln('outmsg '+paramstr(3));
outmsg.OpenRW(paramstr(3));
outmsg.Trunc(0);
writeln('keyring '+paramstr(2));
LoadKeyring;
GetOSRandom(@session,32);
writeln('session: ':10,string(session));
hash.InitWithKey(session,32,$36);
seskx.InitEnCrypt(session, 256);
LoadRecipients;
MessageEncryptData( inmsg, outmsg, seskx, hash);
WriteMAC;
outmsg.Done;
inmsg.Done;
end;
const eoln=LineEnding;
const helptext:ansistring
=eoln
+'bnedit file.dat : decode and output contents of the file'+eoln
+'bnedit prof secret.dat profile.dat : edit profile file'+eoln
+' reads commands from stdin, use HELP to get available commands.'+eoln
+'bnedit keygen secret.dat master_secret.dat : generate secret key from master or both'+eoln
+' A random master_secret.dat will be created if it does not exist.'+eoln
+'bnedit chpasswd master_secret.dat : change password on your master key'+eoln
+'bnedit encrypt secret.dat message.dat message.txt rcpt_spec... : encrypt message'+eoln
+' rcpt_spec = prio:recipients_profile.dat, prio = 0..9 or to (8), cc(4)'+eoln
+'bnedit de[crypt] secret.dat message.dat [sender_profile.dat] : decrypt and verify a message'+eoln
{+'bnedit host host-key.dat : generate host key or renew PoW'+eoln}
+eoln
;
BEGIN
if paramcount=0 then begin
writeln('No parameters! Here read help text.');
writeln('bnedit is BrodNet file Viewer and Editor version '+GIT_VERSION);
write(helptext);
halt(9);
end;
if paramcount=1 then begin
try
DisplayInfoAboutFile(paramstr(1));
except
on e:eInOutError do begin
writeln('DisplayInfoAboutFile('+paramstr(1)+'): IO Error: '+e.Message);
writeln(helphint);
end;
end;
end;
if paramcount>=2 then case paramstr(1) of
'keygen':EditKeyGen;
'chpasswd':EditMasterPassword;
'prof','profile':EditProfile;
'encrypt': MessageEncrypt;
{prof secret profile command value...}
{de[crypt] message secret [sender_profile]}
{encrypt message input secret rcpt_profile...}
else begin
writeln('Unknown operation! Here read help text.');
write(helptext);
halt(9);
end;
end;
END.
bnedit prof profile.dat secret.dat name "Tomáš Brada" nick Brod