Debian Codegraph @

components/kascrypt/Hashes/Private/kdf.pas

raw
1 unit kdf;
2
3 {(Password Based) Key Derivation Functions}
4
5
6 interface
7
8 (*************************************************************************
9
10 DESCRIPTION : (Password Based) Key Derivation Functions
11
12 REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12/D17-D18, FPC, VP, WDOSX
13
14 EXTERNAL DATA : ---
15
16 MEMORY USAGE : ---
17
18 DISPLAY MODE : ---
19
20 REFERENCES : http://tools.ietf.org/html/rfc2898
21 http://tools.ietf.org/html/rfc3211 [includes test vectors]
22 http://tools.ietf.org/html/rfc5869 [includes test vectors]
23 http://www.di-mgt.com.au/cryptoKDFs.html [includes test vectors]
24
25 Version Date Author Modification
26 ------- -------- ------- ------------------------------------------
27 0.10 17.01.06 W.Ehrhardt Initial version based on keyderiv 1.29
28 0.11 17.01.06 we error codes
29 0.12 23.01.06 we new names: unit pb_kdf, functions kdf2/s
30 0.13 22.06.08 we Make IncMSB work with FPC -dDebug
31 0.14 12.07.08 we Rename old kdf2 to pbkdf2, unit kdf
32 0.15 12.07.08 we pbkdf1
33 0.16 12.07.08 we kdf1, kdf2, kdf3, mgf1
34 0.17 12.11.08 we uses BTypes, Ptr2Inc and/or Str255
35 0.18 25.04.09 we updated RFC URL(s)
36 0.19 19.08.10 we kdf_err_nil_input
37 0.20 20.08.10 we hkdf/hkdfs
38 0.21 15.08.14 we pbkdf2 functions with longint sLen, dkLen
39 0.22 16.08.15 we Removed $ifdef DLL / stdcall
40 **************************************************************************)
41
42
43 (*-------------------------------------------------------------------------
44 (C) Copyright 2006-2015 Wolfgang Ehrhardt
45
46 This software is provided 'as-is', without any express or implied warranty.
47 In no event will the authors be held liable for any damages arising from
48 the use of this software.
49
50 Permission is granted to anyone to use this software for any purpose,
51 including commercial applications, and to alter it and redistribute it
52 freely, subject to the following restrictions:
53
54 1. The origin of this software must not be misrepresented; you must not
55 claim that you wrote the original software. If you use this software in
56 a product, an acknowledgment in the product documentation would be
57 appreciated but is not required.
58
59 2. Altered source versions must be plainly marked as such, and must not be
60 misrepresented as being the original software.
61
62 3. This notice may not be removed or altered from any source distribution.
63 ----------------------------------------------------------------------------*)
64
65 {$i STD.INC}
66
67 uses
68 BTypes,Hash,HMAC;
69
70 const
71 kdf_err_nil_pointer = $0001; {phash descriptor is nil}
72 kdf_err_digestlen = $0002; {digest length from descriptor is zero}
73 kdf_err_invalid_dKLen = $0003; {dKLen greater than hash digest length}
74 kdf_err_nil_input = $0004; {input nil pointer and non-zero length}
75
76
77 function kdf1(phash: PHashDesc; Z: pointer; zLen: word; pOtherInfo: pointer; oiLen: word; var DK; dkLen: word): integer;
78 {-Derive key DK from shared secret Z using optional OtherInfo, hash function from phash}
79
80 function kdf2(phash: PHashDesc; Z: pointer; zLen: word; pOtherInfo: pointer; oiLen: word; var DK; dkLen: word): integer;
81 {-Derive key DK from shared secret Z using optional OtherInfo, hash function from phash}
82
83 function kdf3(phash: PHashDesc; Z: pointer; zLen: word; pOtherInfo: pointer; oiLen: word; var DK; dkLen: word): integer;
84 {-Derive key DK from shared secret Z using optional OtherInfo, hash function from phash}
85
86
87 function mgf1(phash: PHashDesc; pSeed: pointer; sLen: word; var Mask; mLen: word): integer;
88 {-Derive Mask from seed, hash function from phash, Mask Generation Function 1 for PKCS #1}
89
90
91 function pbkdf1(phash: PHashDesc; pPW: pointer; pLen: word; salt: pointer; C: longint; var DK; dkLen: word): integer;
92 {-Derive key DK from password pPW using 8 byte salt and iteration count C, uses hash function from phash}
93
94 function pbkdf1s(phash: PHashDesc; sPW: Str255; salt: pointer; C: longint; var DK; dkLen: word): integer;
95 {-Derive key DK from password string sPW using 8 byte salt and iteration count C, uses hash function from phash}
96
97 function pbkdf2(phash: PHashDesc; pPW: pointer; pLen: word; salt: pointer; sLen,C: longint; var DK; dkLen: longint): integer;
98 {-Derive key DK from password pPW using salt and iteration count C, uses hash function from phash}
99
100 function pbkdf2s(phash: PHashDesc; sPW: Str255; salt: pointer; sLen,C: longint; var DK; dkLen: longint): integer;
101 {-Derive key DK from password string sPW using salt and iteration count C, uses hash function from phash}
102
103
104 function hkdf(phash: PHashDesc; {Descriptor of the Hash to use}
105 pIKM: pointer; L_IKM: word; {input key material: addr/length}
106 salt: pointer; L_salt: word; {optional salt; can be nil: see below }
107 info: pointer; L_info: word; {optional context/application specific information}
108 var DK; dkLen: word): integer; {output key material: addr/length}
109 {-Derive key DK from input key material and salt/info, uses hash function from phash}
110 { If salt=nil then phash^.HDigestLen binary zeros will be used as salt.}
111
112 function hkdfs(phash: PHashDesc; sIKM: Str255; {Hash; input key material as string}
113 salt: pointer; L_salt: word; {optional salt; can be nil: see below }
114 info: pointer; L_info: word; {optional context/application specific information}
115 var DK; dkLen: word): integer; {output key material: addr/length}
116 {-Derive key DK from input key material and salt/info, uses hash function from phash}
117 { If salt=nil then phash^.HDigestLen binary zeros will be used as salt.}
118
119
120 implementation
121
122
123 {helper type}
124 type
125 TMSBA = array[0..3] of byte;
126
127
128 {---------------------------------------------------------------------------}
129 procedure IncMSB(var CTR: TMSBA);
130 {-Increment CTR[3]..CTR[0], i.e. 32 Bit MSB counter}
131 var
132 j: integer;
133 begin
134 for j:=3 downto 0 do begin
135 if CTR[j]=$FF then CTR[j] := 0
136 else begin
137 inc(CTR[j]);
138 exit;
139 end;
140 end;
141 end;
142
143
144 {---------------------------------------------------------------------------}
145 function pbkdf1(phash: PHashDesc; pPW: pointer; pLen: word; salt: pointer; C: longint; var DK; dkLen: word): integer;
146 {-Derive key DK from password pPW using 8 byte salt and iteration count C, uses hash function from phash}
147 var
148 ctx: THashContext;
149 Digest: THashDigest;
150 hlen: word;
151 begin
152 if (phash=nil) or (phash^.HSig<>C_HashSig) then begin
153 pbkdf1 := kdf_err_nil_pointer;
154 exit;
155 end;
156 hLen := phash^.HDigestLen;
157 if hLen=0 then begin
158 pbkdf1 := kdf_err_digestlen;
159 exit;
160 end;
161 if hLen<dKLen then begin
162 pbkdf1 := kdf_err_invalid_dKLen;
163 exit;
164 end;
165 if ((pPW=nil) and (pLen>0)) or (salt=nil) then begin
166 pbkdf1 := kdf_err_nil_input;
167 exit;
168 end;
169
170 pbkdf1 := 0;
171 {calculate T_1 = hash(PW || salt)}
172 with phash^ do begin
173 HInit(ctx);
174 HUpdateXL(ctx, pPW, pLen);
175 HUpdateXL(ctx, salt, 8);
176 HFinal(ctx, Digest);
177 end;
178 while C>1 do begin
179 HashFullXL(phash, Digest, @Digest, hlen);
180 dec(C);
181 end;
182 move(Digest, DK, dKLen);
183 end;
184
185
186 {---------------------------------------------------------------------------}
187 function pbkdf1s(phash: PHashDesc; sPW: Str255; salt: pointer; C: longint; var DK; dkLen: word): integer;
188 {-Derive key DK from password string sPW using 8 byte salt and iteration count C, uses hash function from phash}
189 begin
190 pbkdf1s := pbkdf1(phash, @sPW[1], length(sPW), salt, C, DK, dkLen);
191 end;
192
193
194 {---------------------------------------------------------------------------}
195 function pbkdf2(phash: PHashDesc; pPW: pointer; pLen: word; salt: pointer; sLen,C: longint; var DK; dkLen: longint): integer;
196 {-Derive key DK from password pPW using salt and iteration count C, uses hash function from phash}
197 var
198 k, hLen: word;
199 i, j, lk: longint;
200 pDK: pByte; {pointer to DK }
201 ii: TMSBA; {i in big endian}
202 u, ucum: THashDigest;
203 ctx: THMAC_Context;
204 begin
205 if phash=nil then begin
206 pbkdf2 := kdf_err_nil_pointer;
207 exit;
208 end;
209 if phash^.HDigestLen=0 then begin
210 pbkdf2 := kdf_err_digestlen;
211 exit;
212 end;
213 if ((pPW=nil) and (pLen>0)) or ((salt=nil) and (sLen>0)) then begin
214 pbkdf2 := kdf_err_nil_input;
215 exit;
216 end;
217
218 pbkdf2 := 0;
219 hLen := phash^.HDigestLen;
220 lk := 0;
221 pDK := pByte(@DK);
222 fillchar(ii, sizeof(ii), 0);
223 for i:=1 to 1 + pred(dkLen) div hLen do begin
224 IncMSB(ii);
225 fillchar(ucum, sizeof(ucum),0);
226 for j:=1 to C do begin
227 hmac_init(ctx, phash, pPW, pLen);
228 if j=1 then begin
229 {U_1 = PRF(pPW, salt || ii)}
230 hmac_updateXL(ctx, salt, sLen);
231 hmac_updateXL(ctx, @ii, 4);
232 end
233 else begin
234 {U_i = PRF(pPW, U_(i-1)}
235 hmac_updateXL(ctx, @u, hLen);
236 end;
237 hmac_final(ctx, u);
238 {update cumulative XOR U_i}
239 for k:=0 to hLen-1 do ucum[k] := ucum[k] xor u[k];
240 end;
241 {T_i = F(P,S,C,l) = ucum}
242 for k:=0 to hLen-1 do begin
243 {concat T_i}
244 if lk<dkLen then begin
245 pDK^ := ucum[k];
246 inc(lk);
247 inc(Ptr2Inc(pDK));
248 end;
249 end;
250 end;
251 end;
252
253
254 {---------------------------------------------------------------------------}
255 function pbkdf2s(phash: PHashDesc; sPW: Str255; salt: pointer; sLen, C: longint; var DK; dkLen: longint): integer;
256 {-Derive key DK from password string sPW using salt and iteration count C, uses hash function from phash}
257 begin
258 pbkdf2s := pbkdf2(phash, @sPW[1], length(sPW), salt, sLen, C, DK, dkLen);
259 end;
260
261
262 {---------------------------------------------------------------------------}
263 function kdfx(phash: PHashDesc; x: byte; Z, pOtherInfo: pointer; zLen, oLen: word; var DK; dkLen: word): integer;
264 {-Derive key DK from shared secret Z using optional OtherInfo, hash function from phash}
265 { Internal function for all kdf1, kdf2, kdf3, mgf1}
266 var
267 ctr: TMSBA; {i in big endian}
268 ctx: THashContext;
269 pDK: pByte; {pointer to DK }
270 Digest: THashDigest;
271 i, k, lk, hLen: word;
272 begin
273
274 if (phash=nil) or (phash^.HSig<>C_HashSig) then begin
275 kdfx := kdf_err_nil_pointer;
276 exit;
277 end;
278 hLen := phash^.HDigestLen;
279 if hLen=0 then begin
280 kdfx := kdf_err_digestlen;
281 exit;
282 end;
283 if ((Z=nil) and (zLen>0)) or ((pOtherInfo=nil) and (oLen>0)) then begin
284 kdfx := kdf_err_nil_input;
285 exit;
286 end;
287
288 kdfx := 0;
289 fillchar(ctr, sizeof(ctr), 0);
290 if x=2 then IncMSB(ctr);
291 lk := 0;
292 pDK := pByte(@DK);
293 for i:=1 to 1 + pred(dkLen) div hLen do begin
294 if x=3 then begin
295 {Hash(ctr || Z || [OtherInfo])} {x=3}
296 phash^.HInit(ctx);
297 phash^.HUpdateXL(ctx, @ctr, sizeof(ctr));
298 phash^.HUpdateXL(ctx, Z, zLen);
299 end
300 else begin
301 {Hash(Z || ctr || [OtherInfo])} {x=1,2}
302 phash^.HInit(ctx);
303 phash^.HUpdateXL(ctx, Z, zLen);
304 phash^.HUpdateXL(ctx, @ctr, sizeof(ctr));
305 end;
306 if oLen<>0 then phash^.HUpdateXL(ctx, pOtherInfo, oLen);
307 phash^.HFinal(ctx, Digest);
308 for k:=0 to hLen-1 do begin
309 {store T_i}
310 if lk<dkLen then begin
311 pDK^ := Digest[k];
312 inc(lk);
313 inc(Ptr2Inc(pDK));
314 end
315 else exit;
316 end;
317 IncMSB(ctr);
318 end;
319 end;
320
321
322 {---------------------------------------------------------------------------}
323 function kdf1(phash: PHashDesc; Z: pointer; zLen: word; pOtherInfo: pointer; oiLen: word; var DK; dkLen: word): integer;
324 {-Derive key DK from shared secret Z using optional OtherInfo, hash function from phash}
325 begin
326 kdf1 := kdfx(phash, 1, Z, pOtherInfo, zLen, oiLen, DK, dkLen);
327 end;
328
329
330 {---------------------------------------------------------------------------}
331 function kdf2(phash: PHashDesc; Z: pointer; zLen: word; pOtherInfo: pointer; oiLen: word; var DK; dkLen: word): integer;
332 {-Derive key DK from shared secret Z using optional OtherInfo, hash function from phash}
333 begin
334 kdf2 := kdfx(phash, 2, Z, pOtherInfo, zLen, oiLen, DK, dkLen);
335 end;
336
337
338 {---------------------------------------------------------------------------}
339 function kdf3(phash: PHashDesc; Z: pointer; zLen: word; pOtherInfo: pointer; oiLen: word; var DK; dkLen: word): integer;
340 {-Derive key DK from shared secret Z using optional OtherInfo, hash function from phash}
341 begin
342 kdf3 := kdfx(phash, 3, Z, pOtherInfo, zLen, oiLen, DK, dkLen);
343 end;
344
345
346 {---------------------------------------------------------------------------}
347 function mgf1(phash: PHashDesc; pSeed: pointer; sLen: word; var Mask; mLen: word): integer;
348 {-Derive Mask from seed, hash function from phash, Mask Generation Function 1 for PKCS #1}
349 begin
350 mgf1 := kdfx(phash, 1, pSeed, nil, sLen, 0, Mask, mLen);
351 end;
352
353
354 {---------------------------------------------------------------------------}
355 function hkdf(phash: PHashDesc; {Descriptor of the Hash to use}
356 pIKM: pointer; L_IKM: word; {input key material: addr/length}
357 salt: pointer; L_salt: word; {optional salt; can be nil: see below }
358 info: pointer; L_info: word; {optional context/application specific information}
359 var DK; dkLen: word): integer; {output key material: addr/length}
360 {-Derive key DK from input key material and salt/info, uses hash function from phash}
361 { If salt=nil then phash^.HDigestLen binary zeros will be used as salt.}
362 var
363 PRK,TI: THashDigest;
364 ctx: THMAC_Context;
365 i,k,hLen,lt,lk: word;
366 ctr: byte;
367 pDK: pByte;
368 begin
369
370 {Ref: (IETF) rfc5869 - H. Krawczyk, P. Eronen, May 2010}
371 {HMAC-based Extract-and-Expand Key Derivation Function (HKDF)}
372
373 {Check input parameters}
374 if (phash=nil) or (phash^.HSig<>C_HashSig) then begin
375 hkdf := kdf_err_nil_pointer;
376 exit;
377 end;
378 if ((pIKM=nil) and (L_IKM>0)) or ((info=nil) and (L_info>0)) then begin
379 hkdf := kdf_err_nil_input;
380 exit;
381 end;
382
383 hLen := phash^.HDigestLen;
384 if hLen=0 then begin
385 hkdf := kdf_err_digestlen;
386 exit;
387 end;
388
389 {Stage 1: Extract}
390 hkdf := 0;
391 {if salt=nil then hLen binary zeros are used}
392 if salt=nil then begin
393 fillchar(TI, sizeof(TI), 0);
394 hmac_init(ctx, phash, @TI, hLen);
395 end
396 else hmac_init(ctx, phash, salt, L_salt);
397 hmac_update(ctx, pIKM, L_IKM);
398 hmac_final(ctx, PRK);
399
400 {Stage 2: Expand}
401 lt := 0;
402 lk := 0;
403 ctr := 1;
404 pDK := pByte(@DK);
405 for i:=1 to 1 + pred(dkLen) div hLen do begin
406 {calculate T_i from T_(i-1), info, and ctr}
407 hmac_init(ctx, phash, @PRK, hLen);
408 hmac_update(ctx, @TI, lt);
409 hmac_update(ctx, info, L_info);
410 hmac_update(ctx, @ctr, 1);
411 hmac_final(ctx, TI);
412 for k:=0 to hLen-1 do begin
413 {store T_i}
414 if lk<dkLen then begin
415 pDK^ := TI[k];
416 inc(lk);
417 inc(Ptr2Inc(pDK));
418 end
419 else exit;
420 end;
421 lt := hLen;
422 inc(ctr);
423 end;
424 end;
425
426
427 {---------------------------------------------------------------------------}
428 function hkdfs(phash: PHashDesc; sIKM: Str255; {Hash; input key material as string}
429 salt: pointer; L_salt: word; {optional salt; can be nil: see below }
430 info: pointer; L_info: word; {optional context/application specific information}
431 var DK; dkLen: word): integer; {output key material: addr/length}
432 {-Derive key DK from input key material and salt/info, uses hash function from phash}
433 { If salt=nil then phash^.HDigestLen binary zeros will be used as salt.}
434 begin
435 hkdfs := hkdf(phash,@sIKM[1],length(sIKM),salt,L_salt,info,L_info,DK,dkLen);
436 end;
437
438
439 end.