annotate dcrypt/crypto/macs/HMAC.d @ 6:5cb17e09d685

Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
author Thomas Dixon <reikon@reikon.us>
date Sat, 16 Aug 2008 22:43:22 -0400
parents 3de3a2de13a0
children 5ce3012f1def
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
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
29 Hash inner, outer;
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) {
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
34 hash.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
35
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 inner = 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
37 outer = hash.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
38
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 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
40 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
41
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 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
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 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
45 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
46 }
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
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 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
49 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
50 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
51 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
52 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
53
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
54 if (keyParams.key.length > 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
55 inner.update(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
56 key = inner.digest();
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 } 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
58 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
59
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
60 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
61 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
62 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
63 }
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 inner.update(ipad);
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
66 outer.update(opad);
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.");
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
75 inner.update(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
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() {
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
79 return inner.name~"/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
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
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
82 void 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
83 ipad[] = 0x36;
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
84 opad[] = 0x5c;
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 inner.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
87 outer.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
88 }
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 uint 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
91 return inner.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
92 }
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 uint macSize() {
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
95 return inner.digestSize;
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
96 }
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
97
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
98 ubyte[] finish() {
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
99 outer.update(inner.digest());
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 ubyte[] r = outer.digest();
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 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
102 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
103 }
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
104
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
105 char[] hexFinish() {
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 return Util.ubytesToHex(finish());
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
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 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
110 // Ghetto... oh so ghetto :\
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
111 HMAC h = new HMAC(inner.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
112 h.inner = inner.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
113 h.outer = outer.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
114 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
115 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
116 }
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
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 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
119 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
120 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
121 "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
122 "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
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 "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
127 "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
128 ];
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
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 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
131 "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
132 "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
133 "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
134 "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
135 "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
136 ];
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
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 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
139 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
140 ];
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
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 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
143 "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
144 "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
145 "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
146 "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
147 ];
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
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
149 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
150 foreach (uint i, char[] k; 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
151 h.init(new SymmetricKey(Util.hexToUbytes(k)));
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
152 for (int j = 0; j < test_repeat[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
153 h.update(Util.hexToUbytes(test_inputs[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 char[] mac = h.hexFinish();
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 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
156 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
157 }
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
158 }
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
159 }
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
160 }