annotate dcrypt/crypto/macs/HMAC.d @ 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.
author Thomas Dixon <reikon@reikon.us>
date Thu, 14 Aug 2008 01:45:24 -0400
parents
children 5cb17e09d685
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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
20 /** Conforms: RFC2104 */
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher API. Running on fumes, so hopefully there isn't too much stupid mixed into the code.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
21 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
22 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
23 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
24 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
25 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
26 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
29 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
30
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
32 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
33
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
35 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
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 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
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 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
40 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
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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
44 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
45 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
46 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
47 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
48
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
50 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
51 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
52 } 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
53 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
54
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
56 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
57 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
58 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
61 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
62
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
67 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
68 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
69 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
70 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
71 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
74 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
75 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
78 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
79 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
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 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
82 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
83 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
86 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
87 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
90 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
91 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
94 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
95 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
96 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
97 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
98 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
101 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
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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
105 // 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
106 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
107 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
108 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
109 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
110 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
111 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
114 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
115 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
116 "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
117 "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
118 "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
119 "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
120 "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
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 ];
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
126 "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
127 "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
128 "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
129 "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
130 "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
131 ];
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
134 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
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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
138 "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
139 "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
140 "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
141 "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
142 ];
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 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
145 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
146 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
147 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
148 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
149 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
150 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
151 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
152 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 }
3de3a2de13a0 Added MAC base class and HMAC. Added StreamCipherWrapper as part of the work on the high-level cipher 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 }