mORMot and Open Source friends
Check-in [41bc882fae]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1:41bc882fae2b5684cc907c74e8e562373e82bd63
Date: 2016-05-14 08:41:59
User: ab
Comment:{2650} small refactoring of TAESPRNG.AFSplit/AFUnsplit to conform to TKS1/LUKS reference implementation
Tags And Properties
Context
2016-05-14
08:42
[e4b33fcae3] {2651} documentation update of TSynUniqueIdentifierGenerator (user: ab, tags: trunk)
08:41
[41bc882fae] {2650} small refactoring of TAESPRNG.AFSplit/AFUnsplit to conform to TKS1/LUKS reference implementation (user: ab, tags: trunk)
2016-05-13
14:23
[435bfce8b3] {2649} fixed mustache #if helper - as defined e.g. in '1+3={{tval}} - is it 4?{{#if tval=4}} yes!{{/if}}' (user: ab, tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to SynCrypto.pas.

7556
7557
7558
7559
7560
7561
7562
7563
7564
7565

7566
7567
7568
7569
7570
7571

7572
7573
7574
7575
7576
7577
7578
7579

7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608

7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
    if FillRandomAES=nil then
      GarbageCollectorFreeAndNil(FillRandomAES, TAESPRNG.Create);
    GlobalUnLock;
  end;
  result := FillRandomAES;
end;

procedure _afdiffusesha256(buf,rnd: pointer; size: integer);
var sha: TSHA256;
    dig: TSHA256Digest;

    last,i: integer;
begin
  XorMemory(buf,rnd,size);
  sha.Init;
  last := size div SizeOf(dig);
  for i := 0 to last-1 do begin

    sha.Update(@i,SizeOf(i));
    sha.Update(buf,SizeOf(dig));
    sha.Final(PSHA256Digest(buf)^);
    inc(PByte(buf),SizeOf(dig));
  end;
  dec(size,last*SizeOf(dig));
  if size=0 then
    exit;

  sha.Update(@last,SizeOf(last));
  sha.Update(buf,size);
  sha.Final(dig);
  move(dig,buf^,size);
end;
 
function TAESPRNG.AFSplit(const Buffer; BufferBytes, StripesCount: integer): RawByteString;
var P: pointer;
    tmp: TByteDynArray;
    i: integer;
begin
  result := '';
  if self<>nil then
    SetLength(result,BufferBytes*(StripesCount+1));
  if result='' then
    exit;
  P := pointer(result);
  SetLength(tmp,BufferBytes);
  for i := 1 to StripesCount do begin
    FillRandom(P,BufferBytes);
    _afdiffusesha256(pointer(tmp),P,BufferBytes);
    inc(PByte(P),BufferBytes);
  end;
  move(Buffer,P^,BufferBytes);
  XorMemory(P,pointer(tmp),BufferBytes);
end;

class function TAESPRNG.AFUnsplit(const Split: RawByteString;
  out Buffer; BufferBytes: integer): boolean;

var len,i: integer;
    P: pointer;
    tmp: TByteDynArray;
begin
  len := length(Split);
  result := (len<>0) and (len mod BufferBytes=0);
  if not result then
    exit;
  P := pointer(Split);
  SetLength(tmp,BufferBytes);
  for i := 2 to len div BufferBytes do begin
    _afdiffusesha256(pointer(tmp),P,BufferBytes);
    inc(PByte(P),BufferBytes);
  end;
  move(P^,Buffer,BufferBytes);
  XorMemory(@Buffer,pointer(tmp),BufferBytes);
end;


{ TRC4 }

procedure TRC4.Init(const aKey; aKeyLen: integer);
var i,k: integer;







|


>
|





>
|







>
|






|








|


|
|
|

<
|




>
|
|



|


|

|
|
|

<
|







7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605

7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625

7626
7627
7628
7629
7630
7631
7632
7633
    if FillRandomAES=nil then
      GarbageCollectorFreeAndNil(FillRandomAES, TAESPRNG.Create);
    GlobalUnLock;
  end;
  result := FillRandomAES;
end;

procedure _afdiffusesha256(buf,rnd: pointer; size: cardinal);
var sha: TSHA256;
    dig: TSHA256Digest;
    last, iv: cardinal;
    i: integer;
begin
  XorMemory(buf,rnd,size);
  sha.Init;
  last := size div SizeOf(dig);
  for i := 0 to last-1 do begin
    iv := bswap32(i); // host byte order independent hash IV (as in TKS1/LUKS)
    sha.Update(@iv,SizeOf(iv));
    sha.Update(buf,SizeOf(dig));
    sha.Final(PSHA256Digest(buf)^);
    inc(PByte(buf),SizeOf(dig));
  end;
  dec(size,last*SizeOf(dig));
  if size=0 then
    exit;
  iv := bswap32(last);
  sha.Update(@iv,SizeOf(iv));
  sha.Update(buf,size);
  sha.Final(dig);
  move(dig,buf^,size);
end;
 
function TAESPRNG.AFSplit(const Buffer; BufferBytes, StripesCount: integer): RawByteString;
var dst: pointer;
    tmp: TByteDynArray;
    i: integer;
begin
  result := '';
  if self<>nil then
    SetLength(result,BufferBytes*(StripesCount+1));
  if result='' then
    exit;
  dst := pointer(result);
  SetLength(tmp,BufferBytes);
  for i := 1 to StripesCount do begin
    FillRandom(dst,BufferBytes);
    _afdiffusesha256(pointer(tmp),dst,BufferBytes);
    inc(PByte(dst),BufferBytes);
  end;

  XorBlockN(@Buffer,dst,pointer(tmp),BufferBytes); // B[i] := A[i] xor C[i];
end;

class function TAESPRNG.AFUnsplit(const Split: RawByteString;
  out Buffer; BufferBytes: integer): boolean;
var len: cardinal;
    i: integer;
    src: pointer;
    tmp: TByteDynArray;
begin
  len := length(Split);
  result := (len<>0) and (len mod cardinal(BufferBytes)=0);
  if not result then
    exit;
  src := pointer(Split);
  SetLength(tmp,BufferBytes);
  for i := 2 to len div cardinal(BufferBytes) do begin
    _afdiffusesha256(pointer(tmp),src,BufferBytes);
    inc(PByte(src),BufferBytes);
  end;

  XorBlockN(src,@Buffer,pointer(tmp),BufferBytes);
end;


{ TRC4 }

procedure TRC4.Init(const aKey; aKeyLen: integer);
var i,k: integer;

Changes to SynSelfTests.pas.

8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
....
8531
8532
8533
8534
8535
8536
8537
8538
8539

8540
8541
8542
8543
8544
8545
8546
  {$endif}
end;

procedure TTestCryptographicRoutines._TAESPNRG;
var b1,b2: TAESBlock;
    a1,a2: TAESPRNG;
    s1,s2,split: RawByteString;
    i,j: integer;
begin
  TAESPRNG.Main.FillRandom(b1);
  TAESPRNG.Main.FillRandom(b2);
  Check(not CompareMem(@b1,@b2,sizeof(b1)));
  a1 := TAESPRNG.Create;
  a2 := TAESPRNG.Create;
  try
................................................................................
    end;
  finally
    a1.Free;
    a2.Free;
  end;
  s1 := TAESPRNG.Main.FillRandom(100);
  for i := 1 to length(s1) do
    for j := 1 to 10 do begin
      split := TAESPRNG.Main.AFSplit(pointer(s1)^,i,j);

      check(TAESPRNG.AFUnsplit(split,pointer(s2)^,i));
      check(CompareMem(pointer(s1),pointer(s2),i));
    end;
  check(PosEx(s1,split)=0);
end;









|







 







|
|
>







8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
....
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
  {$endif}
end;

procedure TTestCryptographicRoutines._TAESPNRG;
var b1,b2: TAESBlock;
    a1,a2: TAESPRNG;
    s1,s2,split: RawByteString;
    i,stripes: integer;
begin
  TAESPRNG.Main.FillRandom(b1);
  TAESPRNG.Main.FillRandom(b2);
  Check(not CompareMem(@b1,@b2,sizeof(b1)));
  a1 := TAESPRNG.Create;
  a2 := TAESPRNG.Create;
  try
................................................................................
    end;
  finally
    a1.Free;
    a2.Free;
  end;
  s1 := TAESPRNG.Main.FillRandom(100);
  for i := 1 to length(s1) do
    for stripes := 0 to 10 do begin
      split := TAESPRNG.Main.AFSplit(pointer(s1)^,i,stripes);
      check(length(split)=i*(stripes+1));
      check(TAESPRNG.AFUnsplit(split,pointer(s2)^,i));
      check(CompareMem(pointer(s1),pointer(s2),i));
    end;
  check(PosEx(s1,split)=0);
end;


Changes to SynopseCommit.inc.

1
'1.18.2649'
|
1
'1.18.2650'