scrypt.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. unit scrypt;
  2. {scrypt key derivation functions}
  3. interface
  4. {$i std.inc}
  5. uses
  6. BTypes, memh, Hash, kdf;
  7. (*************************************************************************
  8. DESCRIPTION : scrypt key derivation functions
  9. REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12/D17-D18, FPC, VP, WDOSX
  10. EXTERNAL DATA : ---
  11. MEMORY USAGE : ---
  12. DISPLAY MODE : ---
  13. REMARKS : - assumes little-endian (checked for FPC)
  14. - very restricted for 16-bit because all buffer sizes must be < 64KB
  15. REFERENCES : - Colin Percival, Stronger Key Derivation via Sequential Memory-Hard
  16. Functions, http://www.tarsnap.com/scrypt/scrypt.pdf
  17. - Source code from http://www.tarsnap.com/scrypt/scrypt-1.1.6.tgz
  18. - Specification and test vectors from http://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01
  19. Version Date Author Modification
  20. ------- -------- ------- ------------------------------------------
  21. 0.10 13.08.14 W.Ehrhardt Initial BP7, test case salsa20/8 using salsa unit
  22. 0.11 14.08.14 we Test case salsa20/8 without salsa unit
  23. 0.12 14.08.14 we blockmix_salsa8
  24. 0.13 14.08.14 we smix
  25. 0.14 14.08.14 we pbkfd2_hmac_sha256
  26. 0.15 14.08.14 we scrypt_kdf
  27. 0.16 15.08.14 we Support for other compilers
  28. 0.17 15.08.14 we Removed restriction on r*p (longint sLen, dkLen in pbkdf2)
  29. 0.18 15.08.14 we String versions scrypt_kdfs, scrypt_kdfss
  30. 0.19 15.08.14 we Allow pPW=nil or salt=nil
  31. 0.20 15.08.14 we Simply parameter checks, comments
  32. 0.21 16.08.14 we Separate unit
  33. 0.22 16.08.14 we More parameter checks
  34. 0.23 26.08.15 we Faster (reordered) salsa20/8
  35. **************************************************************************)
  36. (*-------------------------------------------------------------------------
  37. (C) Copyright 2014-2015 Wolfgang Ehrhardt
  38. This software is provided 'as-is', without any express or implied warranty.
  39. In no event will the authors be held liable for any damages arising from
  40. the use of this software.
  41. Permission is granted to anyone to use this software for any purpose,
  42. including commercial applications, and to alter it and redistribute it
  43. freely, subject to the following restrictions:
  44. 1. The origin of this software must not be misrepresented; you must not
  45. claim that you wrote the original software. If you use this software in
  46. a product, an acknowledgment in the product documentation would be
  47. appreciated but is not required.
  48. 2. Altered source versions must be plainly marked as such, and must not be
  49. misrepresented as being the original software.
  50. 3. This notice may not be removed or altered from any source distribution.
  51. ----------------------------------------------------------------------------*)
  52. const
  53. sc_kdf_err_mem = -1; {Error from malloc}
  54. sc_kdf_err_inv_nrp = -2; {Invalid N,r,p. Note N must be a power of 2}
  55. sc_kdf_err_64KB = -3; {16-bit malloc with more than 64 KB}
  56. sc_kdf_err_big_endian = -4; {(FPC) compiling with big-endian}
  57. function scrypt_kdf(pPW: pointer; pLen: word; salt: pointer; sLen,N,r,p: longint; var DK; dkLen: longint): integer;
  58. {-Derive key DK from password pPW and salt using scrypt with parameters N,r,p}
  59. function scrypt_kdfs(sPW: Str255; salt: pointer; sLen,N,r,p: longint; var DK; dkLen: longint): integer;
  60. {-Derive key DK from password sPW and salt using scrypt with parameters N,r,p}
  61. function scrypt_kdfss(sPW, salt: Str255; N,r,p: longint; var DK; dkLen: longint): integer;
  62. {-Derive key DK from password sPW and salt using scrypt with parameters N,r,p}
  63. implementation
  64. uses
  65. SHA3_512; {Register SHA3_512 for HMAC_SHA3_512}
  66. type
  67. TLA16 = array[0..15] of longint;
  68. TBA64 = array[0..63] of byte;
  69. {---------------------------------------------------------------------------}
  70. procedure salsa20_8(var B: TLA16);
  71. {-Apply the salsa20/8 core to the provided block B}
  72. var
  73. i: integer;
  74. y: longint;
  75. x: TLA16;
  76. begin
  77. {This is the PurePascal version from my salsa20 unit}
  78. x := B;
  79. {$ifdef OldOrder}
  80. for i:=0 to 3 do begin
  81. y := x[ 0] + x[12]; x[ 4] := x[ 4] xor ((y shl 07) or (y shr (32-07)));
  82. y := x[ 4] + x[ 0]; x[ 8] := x[ 8] xor ((y shl 09) or (y shr (32-09)));
  83. y := x[ 8] + x[ 4]; x[12] := x[12] xor ((y shl 13) or (y shr (32-13)));
  84. y := x[12] + x[ 8]; x[ 0] := x[ 0] xor ((y shl 18) or (y shr (32-18)));
  85. y := x[ 5] + x[ 1]; x[ 9] := x[ 9] xor ((y shl 07) or (y shr (32-07)));
  86. y := x[ 9] + x[ 5]; x[13] := x[13] xor ((y shl 09) or (y shr (32-09)));
  87. y := x[13] + x[ 9]; x[ 1] := x[ 1] xor ((y shl 13) or (y shr (32-13)));
  88. y := x[ 1] + x[13]; x[ 5] := x[ 5] xor ((y shl 18) or (y shr (32-18)));
  89. y := x[10] + x[ 6]; x[14] := x[14] xor ((y shl 07) or (y shr (32-07)));
  90. y := x[14] + x[10]; x[ 2] := x[ 2] xor ((y shl 09) or (y shr (32-09)));
  91. y := x[ 2] + x[14]; x[ 6] := x[ 6] xor ((y shl 13) or (y shr (32-13)));
  92. y := x[ 6] + x[ 2]; x[10] := x[10] xor ((y shl 18) or (y shr (32-18)));
  93. y := x[15] + x[11]; x[ 3] := x[ 3] xor ((y shl 07) or (y shr (32-07)));
  94. y := x[ 3] + x[15]; x[ 7] := x[ 7] xor ((y shl 09) or (y shr (32-09)));
  95. y := x[ 7] + x[ 3]; x[11] := x[11] xor ((y shl 13) or (y shr (32-13)));
  96. y := x[11] + x[ 7]; x[15] := x[15] xor ((y shl 18) or (y shr (32-18)));
  97. y := x[ 0] + x[ 3]; x[ 1] := x[ 1] xor ((y shl 07) or (y shr (32-07)));
  98. y := x[ 1] + x[ 0]; x[ 2] := x[ 2] xor ((y shl 09) or (y shr (32-09)));
  99. y := x[ 2] + x[ 1]; x[ 3] := x[ 3] xor ((y shl 13) or (y shr (32-13)));
  100. y := x[ 3] + x[ 2]; x[ 0] := x[ 0] xor ((y shl 18) or (y shr (32-18)));
  101. y := x[ 5] + x[ 4]; x[ 6] := x[ 6] xor ((y shl 07) or (y shr (32-07)));
  102. y := x[ 6] + x[ 5]; x[ 7] := x[ 7] xor ((y shl 09) or (y shr (32-09)));
  103. y := x[ 7] + x[ 6]; x[ 4] := x[ 4] xor ((y shl 13) or (y shr (32-13)));
  104. y := x[ 4] + x[ 7]; x[ 5] := x[ 5] xor ((y shl 18) or (y shr (32-18)));
  105. y := x[10] + x[ 9]; x[11] := x[11] xor ((y shl 07) or (y shr (32-07)));
  106. y := x[11] + x[10]; x[ 8] := x[ 8] xor ((y shl 09) or (y shr (32-09)));
  107. y := x[ 8] + x[11]; x[ 9] := x[ 9] xor ((y shl 13) or (y shr (32-13)));
  108. y := x[ 9] + x[ 8]; x[10] := x[10] xor ((y shl 18) or (y shr (32-18)));
  109. y := x[15] + x[14]; x[12] := x[12] xor ((y shl 07) or (y shr (32-07)));
  110. y := x[12] + x[15]; x[13] := x[13] xor ((y shl 09) or (y shr (32-09)));
  111. y := x[13] + x[12]; x[14] := x[14] xor ((y shl 13) or (y shr (32-13)));
  112. y := x[14] + x[13]; x[15] := x[15] xor ((y shl 18) or (y shr (32-18)));
  113. end;
  114. {$else}
  115. for i:=0 to 3 do begin
  116. y := x[ 0] + x[12]; x[ 4] := x[ 4] xor ((y shl 07) or (y shr (32-07)));
  117. y := x[ 5] + x[ 1]; x[ 9] := x[ 9] xor ((y shl 07) or (y shr (32-07)));
  118. y := x[10] + x[ 6]; x[14] := x[14] xor ((y shl 07) or (y shr (32-07)));
  119. y := x[15] + x[11]; x[ 3] := x[ 3] xor ((y shl 07) or (y shr (32-07)));
  120. y := x[ 4] + x[ 0]; x[ 8] := x[ 8] xor ((y shl 09) or (y shr (32-09)));
  121. y := x[ 9] + x[ 5]; x[13] := x[13] xor ((y shl 09) or (y shr (32-09)));
  122. y := x[14] + x[10]; x[ 2] := x[ 2] xor ((y shl 09) or (y shr (32-09)));
  123. y := x[ 3] + x[15]; x[ 7] := x[ 7] xor ((y shl 09) or (y shr (32-09)));
  124. y := x[ 8] + x[ 4]; x[12] := x[12] xor ((y shl 13) or (y shr (32-13)));
  125. y := x[13] + x[ 9]; x[ 1] := x[ 1] xor ((y shl 13) or (y shr (32-13)));
  126. y := x[ 2] + x[14]; x[ 6] := x[ 6] xor ((y shl 13) or (y shr (32-13)));
  127. y := x[ 7] + x[ 3]; x[11] := x[11] xor ((y shl 13) or (y shr (32-13)));
  128. y := x[12] + x[ 8]; x[ 0] := x[ 0] xor ((y shl 18) or (y shr (32-18)));
  129. y := x[ 1] + x[13]; x[ 5] := x[ 5] xor ((y shl 18) or (y shr (32-18)));
  130. y := x[ 6] + x[ 2]; x[10] := x[10] xor ((y shl 18) or (y shr (32-18)));
  131. y := x[11] + x[ 7]; x[15] := x[15] xor ((y shl 18) or (y shr (32-18)));
  132. y := x[ 0] + x[ 3]; x[ 1] := x[ 1] xor ((y shl 07) or (y shr (32-07)));
  133. y := x[ 5] + x[ 4]; x[ 6] := x[ 6] xor ((y shl 07) or (y shr (32-07)));
  134. y := x[10] + x[ 9]; x[11] := x[11] xor ((y shl 07) or (y shr (32-07)));
  135. y := x[15] + x[14]; x[12] := x[12] xor ((y shl 07) or (y shr (32-07)));
  136. y := x[ 1] + x[ 0]; x[ 2] := x[ 2] xor ((y shl 09) or (y shr (32-09)));
  137. y := x[ 6] + x[ 5]; x[ 7] := x[ 7] xor ((y shl 09) or (y shr (32-09)));
  138. y := x[11] + x[10]; x[ 8] := x[ 8] xor ((y shl 09) or (y shr (32-09)));
  139. y := x[12] + x[15]; x[13] := x[13] xor ((y shl 09) or (y shr (32-09)));
  140. y := x[ 2] + x[ 1]; x[ 3] := x[ 3] xor ((y shl 13) or (y shr (32-13)));
  141. y := x[ 7] + x[ 6]; x[ 4] := x[ 4] xor ((y shl 13) or (y shr (32-13)));
  142. y := x[ 8] + x[11]; x[ 9] := x[ 9] xor ((y shl 13) or (y shr (32-13)));
  143. y := x[13] + x[12]; x[14] := x[14] xor ((y shl 13) or (y shr (32-13)));
  144. y := x[ 3] + x[ 2]; x[ 0] := x[ 0] xor ((y shl 18) or (y shr (32-18)));
  145. y := x[ 4] + x[ 7]; x[ 5] := x[ 5] xor ((y shl 18) or (y shr (32-18)));
  146. y := x[ 9] + x[ 8]; x[10] := x[10] xor ((y shl 18) or (y shr (32-18)));
  147. y := x[14] + x[13]; x[15] := x[15] xor ((y shl 18) or (y shr (32-18)));
  148. end;
  149. {$endif}
  150. for i:=0 to 15 do B[i] := x[i] + B[i]
  151. end;
  152. {---------------------------------------------------------------------------}
  153. procedure xorblock(dest, src: pByte; len: longint);
  154. {-xor block dest := dest xor src}
  155. begin
  156. while len > 0 do begin
  157. dest^ := dest^ xor src^;
  158. inc(Ptr2Inc(dest));
  159. inc(Ptr2Inc(src));
  160. dec(len);
  161. end;
  162. end;
  163. {---------------------------------------------------------------------------}
  164. function scrypt_kdfs(sPW: Str255; salt: pointer; sLen,N,r,p: longint; var DK; dkLen: longint): integer;
  165. {-Derive key DK from password sPW and salt using scrypt with parameters N,r,p}
  166. begin
  167. scrypt_kdfs := scrypt_kdf(@sPW[1], length(sPw), salt,sLen,N,r,p,DK,dkLen);
  168. end;
  169. {---------------------------------------------------------------------------}
  170. function scrypt_kdfss(sPW, salt: Str255; N,r,p: longint; var DK; dkLen: longint): integer;
  171. {-Derive key DK from password sPW and salt using scrypt with parameters N,r,p}
  172. begin
  173. scrypt_kdfss := scrypt_kdf(@sPW[1],length(sPw),@salt[1],length(salt),N,r,p,DK,dkLen);
  174. end;
  175. {The following scrypt Pascal functions are based on Colin Percival's C}
  176. {function crypto_scrypt-ref.c distributed with the BSD-style license: }
  177. (*-
  178. * Copyright 2009 Colin Percival
  179. * All rights reserved.
  180. *
  181. * Redistribution and use in source and binary forms, with or without
  182. * modification, are permitted provided that the following conditions
  183. * are met:
  184. * 1. Redistributions of source code must retain the above copyright
  185. * notice, this list of conditions and the following disclaimer.
  186. * 2. Redistributions in binary form must reproduce the above copyright
  187. * notice, this list of conditions and the following disclaimer in the
  188. * documentation and/or other materials provided with the distribution.
  189. *
  190. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  191. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  192. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  193. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  194. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  195. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  196. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  197. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  198. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  199. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  200. * SUCH DAMAGE.
  201. *
  202. * This file was originally written by Colin Percival as part of the Tarsnap
  203. * online backup system.
  204. *)
  205. {---------------------------------------------------------------------------}
  206. procedure blockmix_salsa8(B, Y: pByte; r: longint);
  207. {-Compute B = BlockMix_(salsa20/8, r)(B). The input B must be 128*r}
  208. { bytes in length; the temporary space Y must also be the same size.}
  209. var
  210. i: longint;
  211. pb,py: pByte;
  212. X: TBA64;
  213. begin
  214. (* Parameters:
  215. r Block size parameter.
  216. B[0], ..., B[2*r-1] input vector of 2*r 64-byte blocks
  217. B'[0], ..., B'[2*r-1] output vector of 2*r 64-byte blocks
  218. * Algorithm:
  219. 1. X = B[2*r-1]
  220. 2. for i = 0 to 2*r-1 do
  221. T = X xor B[i]
  222. X = Salsa(T)
  223. Y[i] = X
  224. end for
  225. 3. B' = (Y[0], Y[2], ..., Y[2 * r - 2],
  226. Y[1], Y[3], ..., Y[2 * r - 1])
  227. *)
  228. {Step 1}
  229. pb := B;
  230. inc(Ptr2Inc(pb), (2*r-1)*64);
  231. move(pb^, X, 64);
  232. pb := B;
  233. py := Y;
  234. {Steps 2}
  235. for i:= 0 to 2*r - 1 do begin
  236. xorblock(pByte(@X), pByte(pb), 64);
  237. inc(Ptr2Inc(pb), 64);
  238. salsa20_8(TLA16(X));
  239. move(X, py^, 64);
  240. inc(Ptr2Inc(py), 64);
  241. end;
  242. {Step 3}
  243. pb := B;
  244. py := Y;
  245. for i:=0 to r-1 do begin
  246. move(py^, pb^, 64);
  247. inc(Ptr2Inc(pb), 64);
  248. inc(Ptr2Inc(py), 128);
  249. end;
  250. py := Y;
  251. inc(Ptr2Inc(py), 64);
  252. for i:=0 to r-1 do begin
  253. move(py^, pb^, 64);
  254. inc(Ptr2Inc(pb), 64);
  255. inc(Ptr2Inc(py), 128);
  256. end;
  257. end;
  258. {---------------------------------------------------------------------------}
  259. procedure smix(B: pByte; r,N: longint; V, XY: pByte);
  260. {-Compute B = SMix_r(B, N). The input B must be 128*r bytes in length; }
  261. { the temporary storage V must be 128*r*N bytes in length; the temporary}
  262. { storage XY must be 256*r bytes in length. N must be a power of 2. }
  263. var
  264. i,j,r128: longint;
  265. px,py,pv,pj: pByte;
  266. begin
  267. (* Algorithm scryptROMix
  268. Input: r Block size parameter.
  269. B Input octet vector of length 128 * r octets.
  270. N CPU/Memory cost parameter, must be larger than 1,
  271. Output: B' Output octet vector of length 128 * r octets.
  272. *)
  273. {WE: Note that the reference performs the salsa steps as: Convert to LE,}
  274. {salsa compress, convert from LE. Skipped here assuming little-endian. }
  275. r128 := 128*r;
  276. pv := V;
  277. px := XY;
  278. py := XY;
  279. inc(Ptr2Inc(py), r128);
  280. move(B^,px^,r128);
  281. for i:=0 to N-1 do begin
  282. move(px^, pv^, r128);
  283. inc(Ptr2Inc(pv), r128);
  284. blockmix_salsa8(px, py, r);
  285. end;
  286. pj := XY;
  287. inc(Ptr2Inc(pj), (2*r - 1)*64);
  288. for i:=0 to N-1 do begin
  289. {The next line is the function Integerify(X) mod N, i.e. the remainder}
  290. {of dividing the multi-precision little-endian integer B[2*r-1] by N. }
  291. {Because we assume little-endian and N must be a power of two, this }
  292. {reduces to a simple AND operation!}
  293. j := pLongint(pj)^ and (N-1);
  294. {X = blockmix(V[j] xor X)}
  295. pv := V;
  296. inc(Ptr2Inc(pv), j*r128);
  297. xorblock(px, pv, r128);
  298. blockmix_salsa8(px, py, r);
  299. end;
  300. move(px^, B^, r128);
  301. end;
  302. {---------------------------------------------------------------------------}
  303. function pbkdf2_hmac_sha3_512(pPW: pointer; pLen: word; salt: pointer; sLen,C: longint; var DK; dkLen: longint): integer;
  304. {-Derive key DK from password pPW using salt and iteration count C using hmac_sha3_512}
  305. var
  306. phash: PHashDesc;
  307. begin
  308. {Note: pbkdf2 will return error indicator phash=nil if _SHA3_512 is not found!}
  309. phash := FindHash_by_ID(_SHA3_512);
  310. pbkdf2_hmac_sha3_512 := pbkdf2(phash,pPW,pLen,salt,sLen,C,DK,dkLen);
  311. end;
  312. {---------------------------------------------------------------------------}
  313. function scrypt_kdf(pPW: pointer; pLen: word; salt: pointer; sLen,N,r,p: longint; var DK; dkLen: longint): integer;
  314. {-Derive key DK from password pPW and salt using scrypt with parameters N,r,p}
  315. var
  316. pB,pV,pXY,pw: pByte;
  317. sB,sV,sXY,i: longint;
  318. err: integer;
  319. begin
  320. {$ifdef ENDIAN_BIG}
  321. scrypt_kdf := sc_kdf_err_big_endian;
  322. exit;
  323. {$endif}
  324. {Check parameter values and if N is a power of two}
  325. i := MaxLongint div 128;
  326. if (r<1) or (r > i div 2)
  327. or (p<1) or (p > i div r)
  328. or (N<2) or (N and (N-1) <> 0) or (N > i div r) then
  329. begin
  330. scrypt_kdf := sc_kdf_err_inv_nrp;
  331. exit;
  332. end;
  333. {Compute and store sizes, needed for releasing memory}
  334. {sB = 128*r*p, sXY = 256*r, sV = 128*r*N}
  335. i := 128*r; {128 <= i < $40000000}
  336. sB := p*i;
  337. sXY := 2*i;
  338. sV := N*i;
  339. {Simple sanity checks for possible remaining overflows}
  340. if (sV<i) or (sXY<i) or (sB<i) then begin
  341. scrypt_kdf := sc_kdf_err_inv_nrp;
  342. exit;
  343. end;
  344. {$ifdef BIT16}
  345. if (dkLen>$FF00) or (sLen>$FF00) or (sB>$FF00) or (sXY>$FF00) or (sV>$FF00) then begin
  346. scrypt_kdf := sc_kdf_err_64KB;
  347. exit;
  348. end;
  349. {$endif}
  350. pB := malloc(sB);
  351. pV := malloc(sV);
  352. pXY := malloc(sXY);
  353. if (pB<>nil) and (pV<>nil) and (pXY<>nil) then begin
  354. err := pbkdf2_hmac_sha3_512(pPW, pLen, salt, sLen, 1, pB^, sB);
  355. if err=0 then begin
  356. pw := pB;
  357. for i:=0 to p-1 do begin
  358. smix(pw, r, N, pV, pXY);
  359. inc(Ptr2Inc(pw), r*128);
  360. end;
  361. err := pbkdf2_hmac_sha3_512(pPW, pLen, pB, sB, 1, DK, dKlen);
  362. end;
  363. scrypt_kdf := err;
  364. end
  365. else scrypt_kdf := sc_kdf_err_mem;
  366. mfree(pB,sB);
  367. mfree(pV,sV);
  368. mfree(pXY,sXY);
  369. end;
  370. end.