annotate dcrypt/crypto/prngs/PBKDF2.d @ 23:4589f8c5eb3c

Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
author Thomas Dixon <reikon@reikon.us>
date Sat, 14 Feb 2009 19:58:20 -0500
parents 0de48552be35
children 176c933827a8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
1 /**
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
2 * This file is part of the dcrypt project.
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
3 *
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved.
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
5 * License: MIT
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
6 * Authors: Thomas Dixon
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
7 */
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
8
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
9 module dcrypt.crypto.prngs.PBKDF2;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
10
23
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 15
diff changeset
11 import dcrypt.misc.ByteConverter;
15
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
12 import dcrypt.crypto.PRNG;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
13 import dcrypt.crypto.MAC;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
14 import dcrypt.crypto.macs.HMAC;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
15 import dcrypt.crypto.hashes.SHA1;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
16 import dcrypt.crypto.errors.LimitReachedError;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
17
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
18 /**
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
19 * Implementation of RSA Security's Password-Based Key Derivation Function 2
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
20 *
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
21 * Conforms: PKCS #5 v2.0 / RFC 2898
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
22 * References: http://www.truecrypt.org/docs/pkcs5v2-0.pdf
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
23 */
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
24 class PBKDF2 : PRNG {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
25 private {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
26 ubyte[] salt,
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
27 buffer;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
28
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
29 char[] password;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
30
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
31 MAC prf;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
32
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
33 uint iterations,
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
34 blockCount,
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
35 index;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
36 }
23
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 15
diff changeset
37
15
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
38 /**
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
39 * Params:
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
40 * password = User supplied password
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
41 * salt = (preferably random) salt
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
42 * iterations = The number of total iterations
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
43 * prf = The pseudo-random function
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
44 */
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
45 this(char[] password, void[] salt_,
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
46 uint iterations=1000, MAC prf=new HMAC(new SHA1)) {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
47
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
48 salt = cast(ubyte[])salt_;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
49 if (salt == null)
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
50 throw new InvalidParameterError(name()~": No salt specified.");
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
51
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
52 this.password = password;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
53 if (this.password == null)
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
54 throw new InvalidParameterError(name()~": No password specified.");
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
55
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
56 this.prf = prf;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
57 if (this.prf is null)
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
58 throw new InvalidParameterError(name()~": No PRF specified.");
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
59
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
60 this.iterations = iterations;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
61
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
62 prf.init(new SymmetricKey(cast(ubyte[])this.password));
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
63 blockCount = 0;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
64 buffer = new ubyte[this.prf.macSize];
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
65 index = this.prf.macSize;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
66 _initialized = true;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
67 }
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
68
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
69 void addEntropy(ubyte[] input) {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
70 throw new NotSupportedError(name()~": Not supported.");
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
71 }
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
72
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
73 /**
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
74 * Throws: LimitReachedError after 2^32 blocks.
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
75 */
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
76 uint read(ubyte[] output) {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
77 for (uint i = 0; i < output.length; i++) {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
78 if (index == buffer.length) {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
79 if (++blockCount == 0) // Catch rollover
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
80 throw new LimitReachedError(name()~": Output limit reached.");
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
81
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
82 buffer[] = 0;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
83
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
84 ubyte[] t = new ubyte[salt.length + uint.sizeof];
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
85 t[0..salt.length] = salt;
23
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 15
diff changeset
86 t[salt.length..salt.length+int.sizeof] = ByteConverter.BigEndian.from!(uint)(blockCount);
15
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
87
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
88 for (uint j = 0; j < iterations; j++) {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
89 prf.reset();
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
90 prf.update(t);
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
91 t = prf.digest();
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
92
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
93 for (uint k = 0; k < buffer.length; k++)
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
94 buffer[k] ^= t[k];
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
95 }
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
96 index = 0;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
97 }
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
98 output[i] = buffer[index++];
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
99 }
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
100
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
101 return output.length;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
102 }
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
103
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
104 char[] name() {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
105 return "PBKDF2-"~prf.name;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
106 }
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
107
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
108 version (UnitTest) {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
109 unittest {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
110 static char[][] test_passwords = [
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
111 "password",
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
112 "password",
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
113 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"~
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
114 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
115 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"~
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
116 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
117 ];
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
118
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
119 static char[][] test_salts = [
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
120 "ATHENA.MIT.EDUraeburn",
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
121 "ATHENA.MIT.EDUraeburn",
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
122 "pass phrase equals block size",
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
123 "pass phrase exceeds block size"
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
124 ];
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
125
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
126 static const int[] test_iterations = [
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
127 1, 1200, 1200, 1200
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
128 ];
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
129
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
130 static const char[][] test_results = [
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
131 "cdedb5281bb2f801565a1122b2563515",
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
132 "5c08eb61fdf71e4e4ec3cf6ba1f5512b"~
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
133 "a7e52ddbc5e5142f708a31e2e62b1e13",
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
134 "139c30c0966bc32ba55fdbf212530ac9"~
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
135 "c5ec59f1a452f5cc9ad940fea0598ed1",
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
136 "9ccad6d468770cd51b10e6a68721be61"~
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
137 "1a8b4d282601db3b36be9246915ec82a"
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
138 ];
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
139
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
140 PBKDF2 pbkdf2;
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
141 foreach (uint i, char[] p; test_passwords) {
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
142 pbkdf2 = new PBKDF2(p, test_salts[i], test_iterations[i]);
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
143 ubyte[] result = new ubyte[test_results[i].length >> 1];
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
144 pbkdf2.read(result);
23
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 15
diff changeset
145 char[] hexResult = ByteConverter.hexEncode(result);
15
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
146 assert(hexResult == test_results[i],
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
147 pbkdf2.name~": ("~hexResult~") != ("~test_results[i]~")");
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
148 }
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
149 }
0de48552be35 Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
150 }
23
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 15
diff changeset
151 }