KickJava   Java API By Example, From Geeks To Geeks.

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


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.DHGexParameters;
8 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types;
9
10 /**
11  * DhGroupExchange.
12  *
13  * @author Christian Plattner, plattner@inf.ethz.ch
14  * @version $Id: DhGroupExchange.java,v 1.6 2006/09/20 12:51:37 cplattne Exp $
15  */

16 public class DhGroupExchange
17 {
18     /* Given by the standard */
19
20     private BigInteger JavaDoc p;
21     private BigInteger JavaDoc g;
22
23     /* Client public and private */
24
25     private BigInteger JavaDoc e;
26     private BigInteger JavaDoc x;
27
28     /* Server public */
29
30     private BigInteger JavaDoc f;
31
32     /* Shared secret */
33
34     private BigInteger JavaDoc k;
35
36     public DhGroupExchange(BigInteger JavaDoc p, BigInteger JavaDoc g)
37     {
38         this.p = p;
39         this.g = g;
40     }
41
42     public void init(SecureRandom JavaDoc rnd)
43     {
44         k = null;
45
46         x = new BigInteger JavaDoc(p.bitLength() - 1, rnd);
47         e = g.modPow(x, p);
48     }
49
50     /**
51      * @return Returns the e.
52      */

53     public BigInteger JavaDoc getE()
54     {
55         if (e == null)
56             throw new IllegalStateException JavaDoc("Not initialized!");
57
58         return e;
59     }
60
61     /**
62      * @return Returns the shared secret k.
63      */

64     public BigInteger JavaDoc getK()
65     {
66         if (k == null)
67             throw new IllegalStateException JavaDoc("Shared secret not yet known, need f first!");
68
69         return k;
70     }
71
72     /**
73      * Sets f and calculates the shared secret.
74      */

75     public void setF(BigInteger JavaDoc f)
76     {
77         if (e == null)
78             throw new IllegalStateException JavaDoc("Not initialized!");
79
80         BigInteger JavaDoc zero = BigInteger.valueOf(0);
81
82         if (zero.compareTo(f) >= 0 || p.compareTo(f) <= 0)
83             throw new IllegalArgumentException JavaDoc("Invalid f specified!");
84
85         this.f = f;
86         this.k = f.modPow(x, p);
87     }
88
89     public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
90             byte[] serverKexPayload, byte[] hostKey, DHGexParameters para)
91     {
92         HashForSSH2Types hash = new HashForSSH2Types("SHA1");
93
94         hash.updateByteString(clientversion);
95         hash.updateByteString(serverversion);
96         hash.updateByteString(clientKexPayload);
97         hash.updateByteString(serverKexPayload);
98         hash.updateByteString(hostKey);
99         if (para.getMin_group_len() > 0)
100             hash.updateUINT32(para.getMin_group_len());
101         hash.updateUINT32(para.getPref_group_len());
102         if (para.getMax_group_len() > 0)
103             hash.updateUINT32(para.getMax_group_len());
104         hash.updateBigInt(p);
105         hash.updateBigInt(g);
106         hash.updateBigInt(e);
107         hash.updateBigInt(f);
108         hash.updateBigInt(k);
109
110         return hash.getDigest();
111     }
112 }
113
Popular Tags