Debian Codegraph @

components/kascrypt/Hashes/Private/scrypt.pas

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