KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > crypto > dh > DhExchange


1
2 package ch.ethz.ssh2.crypto.dh;
3
4 import java.math.BigInteger JavaDoc;
5 import java.security.SecureRandom JavaDoc;
6
7 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types;
8 import ch.ethz.ssh2.log.Logger;
9
10 /**
11  * DhExchange.
12  *
13  * @author Christian Plattner, plattner@inf.ethz.ch
14  * @version $Id: DhExchange.java,v 1.5 2006/02/14 19:43:15 cplattne Exp $
15  */

16 public class DhExchange
17 {
18     private static final Logger log = Logger.getLogger(DhExchange.class);
19
20     /* Given by the standard */
21
22     static final BigInteger JavaDoc p1, p14;
23     static final BigInteger JavaDoc g;
24
25     BigInteger JavaDoc p;
26
27     /* Client public and private */
28
29     BigInteger JavaDoc e;
30     BigInteger JavaDoc x;
31
32     /* Server public */
33
34     BigInteger JavaDoc f;
35
36     /* Shared secret */
37
38     BigInteger JavaDoc k;
39
40     static
41     {
42         final String JavaDoc p1_string = "17976931348623159077083915679378745319786029604875"
43                 + "60117064444236841971802161585193689478337958649255415021805654859805036464"
44                 + "40548199239100050792877003355816639229553136239076508735759914822574862575"
45                 + "00742530207744771258955095793777842444242661733472762929938766870920560605"
46                 + "0270810842907692932019128194467627007";
47
48         final String JavaDoc p14_string = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129"
49                 + "024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0"
50                 + "A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB"
51                 + "6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A"
52                 + "163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208"
53                 + "552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36C"
54                 + "E3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF69558171"
55                 + "83995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF";
56
57         p1 = new BigInteger JavaDoc(p1_string);
58         p14 = new BigInteger JavaDoc(p14_string, 16);
59         g = new BigInteger JavaDoc("2");
60     }
61
62     public DhExchange()
63     {
64     }
65
66     public void init(int group, SecureRandom JavaDoc rnd)
67     {
68         k = null;
69
70         if (group == 1)
71             p = p1;
72         else if (group == 14)
73             p = p14;
74         else
75             throw new IllegalArgumentException JavaDoc("Unknown DH group " + group);
76
77         x = new BigInteger JavaDoc(p.bitLength() - 1, rnd);
78
79         e = g.modPow(x, p);
80     }
81
82     /**
83      * @return Returns the e.
84      * @throws IllegalStateException
85      */

86     public BigInteger JavaDoc getE()
87     {
88         if (e == null)
89             throw new IllegalStateException JavaDoc("DhDsaExchange not initialized!");
90
91         return e;
92     }
93
94     /**
95      * @return Returns the shared secret k.
96      * @throws IllegalStateException
97      */

98     public BigInteger JavaDoc getK()
99     {
100         if (k == null)
101             throw new IllegalStateException JavaDoc("Shared secret not yet known, need f first!");
102
103         return k;
104     }
105
106     /**
107      * @param f
108      */

109     public void setF(BigInteger JavaDoc f)
110     {
111         if (e == null)
112             throw new IllegalStateException JavaDoc("DhDsaExchange not initialized!");
113
114         BigInteger JavaDoc zero = BigInteger.valueOf(0);
115
116         if (zero.compareTo(f) >= 0 || p.compareTo(f) <= 0)
117             throw new IllegalArgumentException JavaDoc("Invalid f specified!");
118
119         this.f = f;
120         this.k = f.modPow(x, p);
121     }
122
123     public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
124             byte[] serverKexPayload, byte[] hostKey)
125     {
126         HashForSSH2Types hash = new HashForSSH2Types("SHA1");
127
128         if (log.isEnabled())
129         {
130             log.log(90, "Client: '" + new String JavaDoc(clientversion) + "'");
131             log.log(90, "Server: '" + new String JavaDoc(serverversion) + "'");
132         }
133
134         hash.updateByteString(clientversion);
135         hash.updateByteString(serverversion);
136         hash.updateByteString(clientKexPayload);
137         hash.updateByteString(serverKexPayload);
138         hash.updateByteString(hostKey);
139         hash.updateBigInt(e);
140         hash.updateBigInt(f);
141         hash.updateBigInt(k);
142
143         return hash.getDigest();
144     }
145 }
146
Popular Tags