001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.transport.hmac;
027:
028: import com.sshtools.j2ssh.transport.AlgorithmInitializationException;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import java.security.InvalidKeyException;
034: import java.security.NoSuchAlgorithmException;
035:
036: import javax.crypto.Mac;
037: import javax.crypto.spec.SecretKeySpec;
038:
039: /**
040: *
041: *
042: * @author $author$
043: * @version $Revision: 1.20 $
044: */
045: public class HmacSha implements SshHmac {
046: private static Log log = LogFactory.getLog(HmacSha.class);
047: private Mac mac;
048:
049: /**
050: * Creates a new HmacSha object.
051: */
052: public HmacSha() {
053: }
054:
055: /**
056: *
057: *
058: * @return
059: */
060: public int getMacLength() {
061: return mac.getMacLength();
062: }
063:
064: /**
065: *
066: *
067: * @param sequenceNo
068: * @param data
069: * @param offset
070: * @param len
071: *
072: * @return
073: */
074: public byte[] generate(long sequenceNo, byte[] data, int offset,
075: int len) {
076: // Write the sequence no
077: byte[] sequenceBytes = new byte[4];
078: sequenceBytes[0] = (byte) (sequenceNo >> 24);
079: sequenceBytes[1] = (byte) (sequenceNo >> 16);
080: sequenceBytes[2] = (byte) (sequenceNo >> 8);
081: sequenceBytes[3] = (byte) (sequenceNo >> 0);
082: mac.update(sequenceBytes);
083: mac.update(data, offset, len);
084:
085: return mac.doFinal();
086: }
087:
088: /**
089: *
090: *
091: * @param keydata
092: *
093: * @throws AlgorithmInitializationException
094: */
095: public void init(byte[] keydata)
096: throws AlgorithmInitializationException {
097: try {
098: mac = Mac.getInstance("HmacSha1");
099:
100: byte[] key = new byte[20];
101: System.arraycopy(keydata, 0, key, 0, 20);
102:
103: SecretKeySpec keyspec = new SecretKeySpec(key, "HmacSha1");
104: mac.init(keyspec);
105: } catch (NoSuchAlgorithmException nsae) {
106: throw new AlgorithmInitializationException(
107: "No provider exists for the HmacSha1 algorithm");
108: } catch (InvalidKeyException ike) {
109: throw new AlgorithmInitializationException("Invalid key");
110: }
111: }
112:
113: /**
114: *
115: *
116: * @param sequenceNo
117: * @param data
118: *
119: * @return
120: */
121: public boolean verify(long sequenceNo, byte[] data) {
122: int len = getMacLength();
123:
124: //log.debug("MAC Data length: " + String.valueOf(data.length));
125: byte[] generated = generate(sequenceNo, data, 0, data.length
126: - len);
127: String compare1 = new String(generated);
128: String compare2 = new String(data, data.length - len, len);
129:
130: //log.debug("Generated: " + compare1);
131: //log.debug("Actual : " + compare2);
132: boolean result = compare1.equals(compare2);
133:
134: /*if (!result) {
135: /**
136: * Output some debug stuff
137: */
138: /* String genhex = "";
139: String acthex = "";
140: boolean verify = true;
141: for(int i=0;i<generated.length;i++) {
142: genhex += (genhex.length()==0?"":",") + Integer.toHexString(generated[i] & 0xFF);
143: acthex += (acthex.length()==0?"":",") + Integer.toHexString(data[data.length-len+i] & 0xFF);
144: verify = (generated[i] == data[data.length-len+i]);
145: }
146: log.debug("Byte Verify: " + String.valueOf(verify));
147: log.debug("Generated: " + genhex);
148: log.debug("Actual: " + acthex);
149: }*/
150: return result;
151: }
152: }
|