1 |
769 |
jeremybenn |
/* TLSHMac.java -- HMAC used in TLS.
|
2 |
|
|
Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is a part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2 of the License, or (at
|
9 |
|
|
your option) any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
19 |
|
|
USA
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.javax.net.ssl.provider;
|
40 |
|
|
|
41 |
|
|
import java.security.InvalidKeyException;
|
42 |
|
|
import java.util.Map;
|
43 |
|
|
|
44 |
|
|
import gnu.java.security.hash.IMessageDigest;
|
45 |
|
|
import gnu.javax.crypto.mac.HMac;
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* The operation of this HMac is identical to normal HMacs, but this one
|
49 |
|
|
* allows keys with short lengths (including zero).
|
50 |
|
|
*/
|
51 |
|
|
class TLSHMac extends HMac
|
52 |
|
|
{
|
53 |
|
|
|
54 |
|
|
// Constants.
|
55 |
|
|
// -------------------------------------------------------------------------
|
56 |
|
|
|
57 |
|
|
private static final byte IPAD_BYTE = 0x36;
|
58 |
|
|
private static final byte OPAD_BYTE = 0x5C;
|
59 |
|
|
|
60 |
|
|
// Constructor.
|
61 |
|
|
// -------------------------------------------------------------------------
|
62 |
|
|
|
63 |
|
|
TLSHMac(IMessageDigest hash)
|
64 |
|
|
{
|
65 |
|
|
super(hash);
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
// Instance methods.
|
69 |
|
|
// -------------------------------------------------------------------------
|
70 |
|
|
|
71 |
|
|
public void init(Map attributes)
|
72 |
|
|
throws InvalidKeyException, IllegalStateException
|
73 |
|
|
{
|
74 |
|
|
Integer ts = (Integer) attributes.get(TRUNCATED_SIZE);
|
75 |
|
|
truncatedSize = (ts == null ? macSize : ts.intValue());
|
76 |
|
|
if (truncatedSize < (macSize / 2)) {
|
77 |
|
|
throw new IllegalArgumentException("Truncated size too small");
|
78 |
|
|
} else if (truncatedSize < 10) {
|
79 |
|
|
throw new IllegalArgumentException("Truncated size less than 80 bits");
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
// we dont use/save the key outside this method
|
83 |
|
|
byte[] K = (byte[]) attributes.get(MAC_KEY_MATERIAL);
|
84 |
|
|
if (K == null) { // take it as an indication to re-use previous key if set
|
85 |
|
|
if (ipadHash == null)
|
86 |
|
|
{
|
87 |
|
|
throw new InvalidKeyException("Null key");
|
88 |
|
|
}
|
89 |
|
|
// we already went through the motions; ie. up to step #4. re-use
|
90 |
|
|
underlyingHash = (IMessageDigest) ipadHash.clone();
|
91 |
|
|
return;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
if (K.length > blockSize)
|
95 |
|
|
{
|
96 |
|
|
// (0) replace K with HASH(K) if K is larger than the hash's
|
97 |
|
|
// block size. Then pad with zeros until it is the correct
|
98 |
|
|
// size (the next `if').
|
99 |
|
|
underlyingHash.update(K, 0, K.length);
|
100 |
|
|
K = underlyingHash.digest();
|
101 |
|
|
}
|
102 |
|
|
if (K.length < blockSize)
|
103 |
|
|
{
|
104 |
|
|
// (1) append zeros to the end of K to create a B byte string
|
105 |
|
|
// (e.g., if K is of length 20 bytes and B=64, then K will be
|
106 |
|
|
// appended with 44 zero bytes 0x00)
|
107 |
|
|
int limit = (K.length > blockSize) ? blockSize : K.length;
|
108 |
|
|
byte[] newK = new byte[blockSize];
|
109 |
|
|
System.arraycopy(K, 0, newK, 0, limit);
|
110 |
|
|
K = newK;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
underlyingHash.reset();
|
114 |
|
|
opadHash = (IMessageDigest) underlyingHash.clone();
|
115 |
|
|
if (ipad == null)
|
116 |
|
|
{
|
117 |
|
|
ipad = new byte[blockSize];
|
118 |
|
|
}
|
119 |
|
|
// (2) XOR (bitwise exclusive-OR) the B byte string computed in step
|
120 |
|
|
// (1) with ipad
|
121 |
|
|
// (3) append the stream of data 'text' to the B byte string resulting
|
122 |
|
|
// from step (2)
|
123 |
|
|
// (4) apply H to the stream generated in step (3)
|
124 |
|
|
for (int i = 0; i < blockSize; i++)
|
125 |
|
|
{
|
126 |
|
|
ipad[i] = (byte)(K[i] ^ IPAD_BYTE);
|
127 |
|
|
}
|
128 |
|
|
for (int i = 0; i < blockSize; i++)
|
129 |
|
|
{
|
130 |
|
|
opadHash.update((byte)(K[i] ^ OPAD_BYTE));
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
underlyingHash.update(ipad, 0, blockSize);
|
134 |
|
|
ipadHash = (IMessageDigest) underlyingHash.clone();
|
135 |
|
|
K = null;
|
136 |
|
|
}
|
137 |
|
|
}
|