Mercurial > projects > dcrypt
annotate dcrypt/crypto/macs/HMAC.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 |
rev | line source |
---|---|
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
1 /** |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
2 * This file is part of the dcrypt project. |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
3 * |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
5 * License: MIT |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
6 * Authors: Thomas Dixon |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
7 */ |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
8 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
9 module dcrypt.crypto.macs.HMAC; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
10 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
11 import dcrypt.crypto.MAC; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
12 import dcrypt.crypto.Hash; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
13 import dcrypt.crypto.params.SymmetricKey; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
14 import dcrypt.crypto.errors.NotInitializedError; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
15 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
16 version (UnitTest) { |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
17 import dcrypt.crypto.hashes.SHA1; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
18 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
19 |
6
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
4
diff
changeset
|
20 /** |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
4
diff
changeset
|
21 * Implementation of Keyed-Hash Message Authentication Code (HMAC) |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
4
diff
changeset
|
22 * |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
4
diff
changeset
|
23 * Conforms: RFC 2104 |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
4
diff
changeset
|
24 * References: http://www.faqs.org/rfcs/rfc2104.html |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
4
diff
changeset
|
25 */ |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
26 class HMAC : MAC { |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
27 private { |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
28 ubyte[] ipad, opad, key; |
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:
14
diff
changeset
|
29 Hash hash; |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
30 bool initialized; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
31 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
32 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
33 this (Hash hash, void[] key=null) { |
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:
14
diff
changeset
|
34 this.hash = hash.copy(); |
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:
14
diff
changeset
|
35 this.hash.reset(); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
36 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
37 ipad = new ubyte[blockSize]; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
38 opad = new ubyte[blockSize]; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
39 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
40 if (key) |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
41 init(new SymmetricKey(key)); // I'm lazy. |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
42 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
43 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
44 void init(CipherParameters params) { |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
45 SymmetricKey keyParams = cast(SymmetricKey)params; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
46 if (!keyParams) |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
47 throw new InvalidParameterError( |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
48 name()~": Invalid parameter passed to init"); |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
49 |
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:
14
diff
changeset
|
50 hash.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:
14
diff
changeset
|
51 |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
52 if (keyParams.key.length > blockSize) { |
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:
14
diff
changeset
|
53 hash.update(keyParams.key); |
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:
14
diff
changeset
|
54 key = hash.digest(); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
55 } else |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
56 key = keyParams.key; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
57 |
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:
14
diff
changeset
|
58 ipad[] = 0x36; |
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:
14
diff
changeset
|
59 opad[] = 0x5c; |
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:
14
diff
changeset
|
60 |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
61 foreach (uint i, ubyte j; key) { |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
62 ipad[i] ^= j; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
63 opad[i] ^= j; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
64 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
65 |
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:
14
diff
changeset
|
66 reset(); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
67 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
68 initialized = true; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
69 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
70 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
71 void update(void[] input_) { |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
72 if (!initialized) |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
73 throw new NotInitializedError( |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
74 name()~": MAC not initialized."); |
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:
14
diff
changeset
|
75 hash.update(input_); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
76 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
77 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
78 char[] name() { |
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:
14
diff
changeset
|
79 return "HMAC-"~hash.name; |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
80 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
81 |
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:
14
diff
changeset
|
82 void 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:
14
diff
changeset
|
83 hash.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:
14
diff
changeset
|
84 hash.update(ipad); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
85 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
86 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
87 uint blockSize() { |
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:
14
diff
changeset
|
88 return hash.blockSize; |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
89 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
90 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
91 uint macSize() { |
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:
14
diff
changeset
|
92 return hash.digestSize; |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
93 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
94 |
14
5ce3012f1def
Removed some redundancy in code. Added NotSupportedError, a base PRNG class and a class which creates a PRNG from a hash function. Changed the MAC class' finalization methods to digest and hexDigest instead of finish and hexFinish respectively. Also added a base Checksum class, crc32 and adler32 in dcrypt.misc as per request.
Thomas Dixon <reikon@reikon.us>
parents:
6
diff
changeset
|
95 ubyte[] digest() { |
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:
14
diff
changeset
|
96 ubyte[] t = hash.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:
14
diff
changeset
|
97 hash.update(opad); |
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:
14
diff
changeset
|
98 hash.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:
14
diff
changeset
|
99 ubyte[] r = hash.digest(); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
100 reset(); |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
101 return r; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
102 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
103 |
14
5ce3012f1def
Removed some redundancy in code. Added NotSupportedError, a base PRNG class and a class which creates a PRNG from a hash function. Changed the MAC class' finalization methods to digest and hexDigest instead of finish and hexFinish respectively. Also added a base Checksum class, crc32 and adler32 in dcrypt.misc as per request.
Thomas Dixon <reikon@reikon.us>
parents:
6
diff
changeset
|
104 char[] hexDigest() { |
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
|
105 return ByteConverter.hexEncode(digest()); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
106 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
107 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
108 HMAC copy() { |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
109 // Ghetto... oh so ghetto :\ |
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:
14
diff
changeset
|
110 HMAC h = new HMAC(hash.copy()); |
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:
14
diff
changeset
|
111 h.hash = hash.copy(); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
112 h.initialized = true; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
113 return h; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
114 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
115 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
116 version (UnitTest) { |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
117 unittest { |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
118 static char[][] test_keys = [ |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
119 "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
120 "4a656665", // Jefe? |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
121 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
122 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"~ |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
123 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"~ |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
124 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"~ |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
125 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
126 ]; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
127 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
128 static char[][] test_inputs = [ |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
129 "4869205468657265", |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
130 "7768617420646f2079612077616e7420666f72206e6f7468696e673f", |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
131 "dd", |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
132 "54657374205573696e67204c6172676572205468616e20426c6f63"~ |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
133 "6b2d53697a65204b6579202d2048617368204b6579204669727374" |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
134 ]; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
135 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
136 static const int[] test_repeat = [ |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
137 1, 1, 50, 1 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
138 ]; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
139 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
140 static const char[][] test_results = [ |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
141 "b617318655057264e28bc0b6fb378c8ef146be00", |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
142 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
143 "125d7342b9ac11cd91a39af48aa17b4f63f175d3", |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
144 "aa4ae5e15272d00e95705637ce8a3b55ed402112" |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
145 ]; |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
146 |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
147 HMAC h = new HMAC(new SHA1()); |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
148 foreach (uint i, char[] k; test_keys) { |
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
|
149 h.init(new SymmetricKey(ByteConverter.hexDecode(k))); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
150 for (int j = 0; j < test_repeat[i]; j++) |
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 h.update(ByteConverter.hexDecode(test_inputs[i])); |
14
5ce3012f1def
Removed some redundancy in code. Added NotSupportedError, a base PRNG class and a class which creates a PRNG from a hash function. Changed the MAC class' finalization methods to digest and hexDigest instead of finish and hexFinish respectively. Also added a base Checksum class, crc32 and adler32 in dcrypt.misc as per request.
Thomas Dixon <reikon@reikon.us>
parents:
6
diff
changeset
|
152 char[] mac = h.hexDigest(); |
4
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
153 assert(mac == test_results[i], |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
154 h.name~": ("~mac~") != ("~test_results[i]~")"); |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
155 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
156 } |
3de3a2de13a0
Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff
changeset
|
157 } |
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
|
158 } |