ADD: scrypt_kdf function

This commit is contained in:
Alexander Koblov 2017-08-19 10:12:17 +00:00
commit cc47733fda
4 changed files with 65 additions and 10 deletions

View file

@ -37,3 +37,54 @@ Index: sha3.pas
{.$define USE_64BITCODE} {Use 64-bit for Keccak permutation}
{.$define USE_MMXCODE } {Use MMX for Keccak permutation, contributed by Eric Grange}
{.$define USE_MMX_AKP } {Use MMX for Keccak permutation, contributed by Anna Kaliszewicz / payl}
Index: scrypt.pas
===================================================================
--- scrypt.pas (revision 7740)
+++ scrypt.pas (working copy)
@@ -90,7 +90,7 @@
implementation
uses
- sha256; {Register SHA256 for HMAC-SHA256}
+ SHA3_512; {Register SHA3_512 for HMAC_SHA3_512}
type
TLA16 = array[0..15] of longint;
@@ -361,14 +361,14 @@
{---------------------------------------------------------------------------}
-function pbkfd2_hmac_sha256(pPW: pointer; pLen: word; salt: pointer; sLen,C: longint; var DK; dkLen: longint): integer;
- {-Derive key DK from password pPW using salt and iteration count C using (hmac-)sha256}
+function pbkdf2_hmac_sha3_512(pPW: pointer; pLen: word; salt: pointer; sLen,C: longint; var DK; dkLen: longint): integer;
+ {-Derive key DK from password pPW using salt and iteration count C using hmac_sha3_512}
var
phash: PHashDesc;
begin
- {Note: pbkdf2 will return error indicator phash=nil if _SHA256 is not found!}
- phash := FindHash_by_ID(_SHA256);
- pbkfd2_hmac_sha256 := pbkdf2(phash,pPW,pLen,salt,sLen,C,DK,dkLen);
+ {Note: pbkdf2 will return error indicator phash=nil if _SHA3_512 is not found!}
+ phash := FindHash_by_ID(_SHA3_512);
+ pbkdf2_hmac_sha3_512 := pbkdf2(phash,pPW,pLen,salt,sLen,C,DK,dkLen);
end;
@@ -418,7 +418,7 @@
pV := malloc(sV);
pXY := malloc(sXY);
if (pB<>nil) and (pV<>nil) and (pXY<>nil) then begin
- err := pbkfd2_hmac_sha256(pPW, pLen, salt, sLen, 1, pB^, sB);
+ err := pbkdf2_hmac_sha3_512(pPW, pLen, salt, sLen, 1, pB^, sB);
if err=0 then begin
pw := pB;
for i:=0 to p-1 do begin
@@ -425,7 +425,7 @@
smix(pw, r, N, pV, pXY);
inc(Ptr2Inc(pw), r*128);
end;
- err := pbkfd2_hmac_sha256(pPW, pLen, pB, sB, 1, DK, dKlen);
+ err := pbkdf2_hmac_sha3_512(pPW, pLen, pB, sB, 1, DK, dKlen);
end;
scrypt_kdf := err;
end

View file

@ -90,7 +90,7 @@ function scrypt_kdfss(sPW, salt: Str255; N,r,p: longint; var DK; dkLen: longint)
implementation
uses
sha256; {Register SHA256 for HMAC-SHA256}
SHA3_512; {Register SHA3_512 for HMAC_SHA3_512}
type
TLA16 = array[0..15] of longint;
@ -361,14 +361,14 @@ end;
{---------------------------------------------------------------------------}
function pbkfd2_hmac_sha256(pPW: pointer; pLen: word; salt: pointer; sLen,C: longint; var DK; dkLen: longint): integer;
{-Derive key DK from password pPW using salt and iteration count C using (hmac-)sha256}
function pbkdf2_hmac_sha3_512(pPW: pointer; pLen: word; salt: pointer; sLen,C: longint; var DK; dkLen: longint): integer;
{-Derive key DK from password pPW using salt and iteration count C using hmac_sha3_512}
var
phash: PHashDesc;
begin
{Note: pbkdf2 will return error indicator phash=nil if _SHA256 is not found!}
phash := FindHash_by_ID(_SHA256);
pbkfd2_hmac_sha256 := pbkdf2(phash,pPW,pLen,salt,sLen,C,DK,dkLen);
{Note: pbkdf2 will return error indicator phash=nil if _SHA3_512 is not found!}
phash := FindHash_by_ID(_SHA3_512);
pbkdf2_hmac_sha3_512 := pbkdf2(phash,pPW,pLen,salt,sLen,C,DK,dkLen);
end;
@ -418,14 +418,14 @@ begin
pV := malloc(sV);
pXY := malloc(sXY);
if (pB<>nil) and (pV<>nil) and (pXY<>nil) then begin
err := pbkfd2_hmac_sha256(pPW, pLen, salt, sLen, 1, pB^, sB);
err := pbkdf2_hmac_sha3_512(pPW, pLen, salt, sLen, 1, pB^, sB);
if err=0 then begin
pw := pB;
for i:=0 to p-1 do begin
smix(pw, r, N, pV, pXY);
inc(Ptr2Inc(pw), r*128);
end;
err := pbkfd2_hmac_sha256(pPW, pLen, pB, sB, 1, DK, dKlen);
err := pbkdf2_hmac_sha3_512(pPW, pLen, pB, sB, 1, DK, dKlen);
end;
scrypt_kdf := err;
end

View file

@ -34,7 +34,7 @@
www.cityinthesky.co.uk/cryptography.html
"/>
<Version Major="2" Release="4" Build="1"/>
<Files Count="27">
<Files Count="28">
<Item1>
<Filename Value="dcpbase64.pas"/>
<UnitName Value="DCPbase64"/>
@ -143,6 +143,10 @@ www.cityinthesky.co.uk/cryptography.html
<Filename Value="Random/isaac.pas"/>
<UnitName Value="ISAAC"/>
</Item27>
<Item28>
<Filename Value="Hashes/Keccak/scrypt.pas"/>
<UnitName Value="scrypt"/>
</Item28>
</Files>
<RequiredPkgs Count="2">
<Item1>

View file

@ -11,7 +11,7 @@ uses
DCPbase64, DCPblockciphers, DCPconst, DCPcrypt2, DCPhaval, DCPmd4, DCPmd5,
DCPripemd128, DCPripemd160, DCPsha1, DCPsha256, DCPsha512, DCPtiger,
DCPcrc32, DCcrc32, DCblake2, DCPblake2, DCPsha3, DCPsha224, HMAC, SHA3,
SHA3_512, ISAAC;
SHA3_512, ISAAC, scrypt;
implementation