001: /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
002: /*
003: Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are met:
007:
008: 1. Redistributions of source code must retain the above copyright notice,
009: this list of conditions and the following disclaimer.
010:
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in
013: the documentation and/or other materials provided with the distribution.
014:
015: 3. The names of the authors may not be used to endorse or promote products
016: derived from this software without specific prior written permission.
017:
018: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
019: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
020: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
021: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
022: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
024: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
027: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jcraft.jsch.jce;
031:
032: import java.math.BigInteger;
033: import java.security.*;
034: import javax.crypto.*;
035: import javax.crypto.spec.*;
036:
037: public class DH implements com.jcraft.jsch.DH {
038: BigInteger p;
039: BigInteger g;
040: BigInteger e; // my public key
041: byte[] e_array;
042: BigInteger f; // your public key
043: BigInteger K; // shared secret key
044: byte[] K_array;
045:
046: private KeyPairGenerator myKpairGen;
047: private KeyAgreement myKeyAgree;
048:
049: public void init() throws Exception {
050: myKpairGen = KeyPairGenerator.getInstance("DH");
051: // myKpairGen=KeyPairGenerator.getInstance("DiffieHellman");
052: myKeyAgree = KeyAgreement.getInstance("DH");
053: // myKeyAgree=KeyAgreement.getInstance("DiffieHellman");
054: }
055:
056: public byte[] getE() throws Exception {
057: if (e == null) {
058: DHParameterSpec dhSkipParamSpec = new DHParameterSpec(p, g);
059: myKpairGen.initialize(dhSkipParamSpec);
060: KeyPair myKpair = myKpairGen.generateKeyPair();
061: myKeyAgree.init(myKpair.getPrivate());
062: // BigInteger x=((javax.crypto.interfaces.DHPrivateKey)(myKpair.getPrivate())).getX();
063: byte[] myPubKeyEnc = myKpair.getPublic().getEncoded();
064: e = ((javax.crypto.interfaces.DHPublicKey) (myKpair
065: .getPublic())).getY();
066: e_array = e.toByteArray();
067: }
068: return e_array;
069: }
070:
071: public byte[] getK() throws Exception {
072: if (K == null) {
073: KeyFactory myKeyFac = KeyFactory.getInstance("DH");
074: DHPublicKeySpec keySpec = new DHPublicKeySpec(f, p, g);
075: PublicKey yourPubKey = myKeyFac.generatePublic(keySpec);
076: myKeyAgree.doPhase(yourPubKey, true);
077: byte[] mySharedSecret = myKeyAgree.generateSecret();
078: K = new BigInteger(mySharedSecret);
079: K_array = K.toByteArray();
080:
081: //System.err.println("K.signum(): "+K.signum()+
082: // " "+Integer.toHexString(mySharedSecret[0]&0xff)+
083: // " "+Integer.toHexString(K_array[0]&0xff));
084:
085: K_array = mySharedSecret;
086: }
087: return K_array;
088: }
089:
090: public void setP(byte[] p) {
091: setP(new BigInteger(p));
092: }
093:
094: public void setG(byte[] g) {
095: setG(new BigInteger(g));
096: }
097:
098: public void setF(byte[] f) {
099: setF(new BigInteger(f));
100: }
101:
102: void setP(BigInteger p) {
103: this .p = p;
104: }
105:
106: void setG(BigInteger g) {
107: this .g = g;
108: }
109:
110: void setF(BigInteger f) {
111: this.f = f;
112: }
113: }
|