01: package ch.ethz.ssh2.crypto.digest;
02:
03: /**
04: * MAC.
05: *
06: * @author Christian Plattner, plattner@inf.ethz.ch
07: * @version $Id: MAC.java,v 1.4 2006/02/02 09:11:03 cplattne Exp $
08: */
09: public final class MAC {
10: Digest mac;
11: int size;
12:
13: public final static String[] getMacList() {
14: /* Higher Priority First */
15:
16: return new String[] { "hmac-sha1-96", "hmac-sha1",
17: "hmac-md5-96", "hmac-md5" };
18: }
19:
20: public final static void checkMacList(String[] macs) {
21: for (int i = 0; i < macs.length; i++)
22: getKeyLen(macs[i]);
23: }
24:
25: public final static int getKeyLen(String type) {
26: if (type.equals("hmac-sha1"))
27: return 20;
28: if (type.equals("hmac-sha1-96"))
29: return 20;
30: if (type.equals("hmac-md5"))
31: return 16;
32: if (type.equals("hmac-md5-96"))
33: return 16;
34: throw new IllegalArgumentException("Unkown algorithm " + type);
35: }
36:
37: public MAC(String type, byte[] key) {
38: if (type.equals("hmac-sha1")) {
39: mac = new HMAC(new SHA1(), key, 20);
40: } else if (type.equals("hmac-sha1-96")) {
41: mac = new HMAC(new SHA1(), key, 12);
42: } else if (type.equals("hmac-md5")) {
43: mac = new HMAC(new MD5(), key, 16);
44: } else if (type.equals("hmac-md5-96")) {
45: mac = new HMAC(new MD5(), key, 12);
46: } else
47: throw new IllegalArgumentException("Unkown algorithm "
48: + type);
49:
50: size = mac.getDigestLength();
51: }
52:
53: public final void initMac(int seq) {
54: mac.reset();
55: mac.update((byte) (seq >> 24));
56: mac.update((byte) (seq >> 16));
57: mac.update((byte) (seq >> 8));
58: mac.update((byte) (seq));
59: }
60:
61: public final void update(byte[] packetdata, int off, int len) {
62: mac.update(packetdata, off, len);
63: }
64:
65: public final void getMac(byte[] out, int off) {
66: mac.digest(out, off);
67: }
68:
69: public final int size() {
70: return size;
71: }
72: }
|