]>
Commit | Line | Data |
---|---|---|
0763e16d JW |
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ |
2 | /* | |
3 | Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are met: | |
7 | ||
8 | 1. Redistributions of source code must retain the above copyright notice, | |
9 | this list of conditions and the following disclaimer. | |
10 | ||
11 | 2. Redistributions in binary form must reproduce the above copyright | |
12 | notice, this list of conditions and the following disclaimer in | |
13 | the documentation and/or other materials provided with the distribution. | |
14 | ||
15 | 3. The names of the authors may not be used to endorse or promote products | |
16 | derived from this software without specific prior written permission. | |
17 | ||
18 | THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, | |
19 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, | |
21 | INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, | |
22 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
24 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
27 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | package com.jcraft.jsch.jce; | |
31 | ||
32 | import java.math.BigInteger; | |
33 | import java.security.*; | |
34 | import javax.crypto.*; | |
35 | import javax.crypto.spec.*; | |
36 | ||
37 | public class DH implements com.jcraft.jsch.DH{ | |
38 | BigInteger p; | |
39 | BigInteger g; | |
40 | BigInteger e; // my public key | |
41 | byte[] e_array; | |
42 | BigInteger f; // your public key | |
43 | BigInteger K; // shared secret key | |
44 | byte[] K_array; | |
45 | ||
46 | private KeyPairGenerator myKpairGen; | |
47 | private KeyAgreement myKeyAgree; | |
48 | public void init() throws Exception{ | |
49 | myKpairGen=KeyPairGenerator.getInstance("DH"); | |
50 | // myKpairGen=KeyPairGenerator.getInstance("DiffieHellman"); | |
51 | myKeyAgree=KeyAgreement.getInstance("DH"); | |
52 | // myKeyAgree=KeyAgreement.getInstance("DiffieHellman"); | |
53 | } | |
54 | public byte[] getE() throws Exception{ | |
55 | if(e==null){ | |
56 | DHParameterSpec dhSkipParamSpec=new DHParameterSpec(p, g); | |
57 | myKpairGen.initialize(dhSkipParamSpec); | |
58 | KeyPair myKpair=myKpairGen.generateKeyPair(); | |
59 | myKeyAgree.init(myKpair.getPrivate()); | |
60 | // BigInteger x=((javax.crypto.interfaces.DHPrivateKey)(myKpair.getPrivate())).getX(); | |
61 | byte[] myPubKeyEnc=myKpair.getPublic().getEncoded(); | |
62 | e=((javax.crypto.interfaces.DHPublicKey)(myKpair.getPublic())).getY(); | |
63 | e_array=e.toByteArray(); | |
64 | } | |
65 | return e_array; | |
66 | } | |
67 | public byte[] getK() throws Exception{ | |
68 | if(K==null){ | |
69 | KeyFactory myKeyFac=KeyFactory.getInstance("DH"); | |
70 | DHPublicKeySpec keySpec=new DHPublicKeySpec(f, p, g); | |
71 | PublicKey yourPubKey=myKeyFac.generatePublic(keySpec); | |
72 | myKeyAgree.doPhase(yourPubKey, true); | |
73 | byte[] mySharedSecret=myKeyAgree.generateSecret(); | |
74 | K=new BigInteger(mySharedSecret); | |
75 | K_array=K.toByteArray(); | |
76 | ||
77 | //System.err.println("K.signum(): "+K.signum()+ | |
78 | // " "+Integer.toHexString(mySharedSecret[0]&0xff)+ | |
79 | // " "+Integer.toHexString(K_array[0]&0xff)); | |
80 | ||
81 | K_array=mySharedSecret; | |
82 | } | |
83 | return K_array; | |
84 | } | |
85 | public void setP(byte[] p){ setP(new BigInteger(p)); } | |
86 | public void setG(byte[] g){ setG(new BigInteger(g)); } | |
87 | public void setF(byte[] f){ setF(new BigInteger(f)); } | |
88 | void setP(BigInteger p){this.p=p;} | |
89 | void setG(BigInteger g){this.g=g;} | |
90 | void setF(BigInteger f){this.f=f;} | |
91 | } |