Mercurial > 510Connectbot
comparison src/ch/ethz/ssh2/crypto/dh/DhExchange.java @ 273:91a31873c42a ganymed
start conversion from trilead to ganymed
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 18 Jul 2014 11:21:46 -0700 |
parents | |
children | 071eccdff8ea |
comparison
equal
deleted
inserted
replaced
272:ce2f4e397703 | 273:91a31873c42a |
---|---|
1 /* | |
2 * Copyright (c) 2006-2013 Christian Plattner. All rights reserved. | |
3 * Please refer to the LICENSE.txt for licensing details. | |
4 */ | |
5 package ch.ethz.ssh2.crypto.dh; | |
6 | |
7 import java.io.IOException; | |
8 import java.math.BigInteger; | |
9 import java.security.SecureRandom; | |
10 | |
11 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types; | |
12 import ch.ethz.ssh2.log.Logger; | |
13 import ch.ethz.ssh2.util.StringEncoder; | |
14 | |
15 /** | |
16 * @author Christian Plattner | |
17 * @version $Id: DhExchange.java 152 2014-04-28 11:02:23Z dkocher@sudo.ch $ | |
18 */ | |
19 public class DhExchange { | |
20 private static final Logger log = Logger.getLogger(DhExchange.class); | |
21 | |
22 /* Given by the standard */ | |
23 | |
24 static final BigInteger p1, p14; | |
25 static final BigInteger g; | |
26 | |
27 BigInteger p; | |
28 | |
29 /* Client public and private */ | |
30 | |
31 BigInteger e; | |
32 BigInteger x; | |
33 | |
34 /* Server public and private */ | |
35 | |
36 BigInteger f; | |
37 BigInteger y; | |
38 | |
39 /* Shared secret */ | |
40 | |
41 BigInteger k; | |
42 | |
43 static { | |
44 final String p1_string = "17976931348623159077083915679378745319786029604875" | |
45 + "60117064444236841971802161585193689478337958649255415021805654859805036464" | |
46 + "40548199239100050792877003355816639229553136239076508735759914822574862575" | |
47 + "00742530207744771258955095793777842444242661733472762929938766870920560605" | |
48 + "0270810842907692932019128194467627007"; | |
49 | |
50 final String p14_string = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129" | |
51 + "024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0" | |
52 + "A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB" | |
53 + "6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A" | |
54 + "163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208" | |
55 + "552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36C" | |
56 + "E3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF69558171" | |
57 + "83995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF"; | |
58 | |
59 p1 = new BigInteger(p1_string); | |
60 p14 = new BigInteger(p14_string, 16); | |
61 g = new BigInteger("2"); | |
62 } | |
63 | |
64 public DhExchange() { | |
65 } | |
66 | |
67 public void clientInit(int group, SecureRandom rnd) { | |
68 k = null; | |
69 | |
70 if(group == 1) { | |
71 p = p1; | |
72 } | |
73 else if(group == 14) { | |
74 p = p14; | |
75 } | |
76 else { | |
77 throw new IllegalArgumentException("Unknown DH group " + group); | |
78 } | |
79 | |
80 while(true) { | |
81 x = new BigInteger(p.bitLength() - 1, rnd); | |
82 if(x.compareTo(BigInteger.ONE) > 0) { | |
83 break; | |
84 } | |
85 } | |
86 | |
87 e = g.modPow(x, p); | |
88 } | |
89 | |
90 public void serverInit(int group, SecureRandom rnd) { | |
91 k = null; | |
92 | |
93 if(group == 1) { | |
94 p = p1; | |
95 } | |
96 else if(group == 14) { | |
97 p = p14; | |
98 } | |
99 else { | |
100 throw new IllegalArgumentException("Unknown DH group " + group); | |
101 } | |
102 | |
103 y = new BigInteger(p.bitLength() - 1, rnd); | |
104 | |
105 f = g.modPow(y, p); | |
106 } | |
107 | |
108 /** | |
109 * @return Returns the e. | |
110 * @throws IllegalStateException | |
111 */ | |
112 public BigInteger getE() { | |
113 if(e == null) { | |
114 throw new IllegalStateException("DhDsaExchange not initialized!"); | |
115 } | |
116 | |
117 return e; | |
118 } | |
119 | |
120 /** | |
121 * @return Returns the f. | |
122 * @throws IllegalStateException | |
123 */ | |
124 public BigInteger getF() { | |
125 if(f == null) { | |
126 throw new IllegalStateException("DhDsaExchange not initialized!"); | |
127 } | |
128 | |
129 return f; | |
130 } | |
131 | |
132 /** | |
133 * @return Returns the shared secret k. | |
134 * @throws IllegalStateException | |
135 */ | |
136 public BigInteger getK() { | |
137 if(k == null) { | |
138 throw new IllegalStateException("Shared secret not yet known, need f first!"); | |
139 } | |
140 | |
141 return k; | |
142 } | |
143 | |
144 /** | |
145 * @param f | |
146 */ | |
147 public void setF(BigInteger f) { | |
148 if(e == null) { | |
149 throw new IllegalStateException("DhDsaExchange not initialized!"); | |
150 } | |
151 | |
152 if(BigInteger.ZERO.compareTo(f) >= 0 || p.compareTo(f) <= 0) { | |
153 throw new IllegalArgumentException("Invalid f specified!"); | |
154 } | |
155 | |
156 this.f = f; | |
157 this.k = f.modPow(x, p); | |
158 } | |
159 | |
160 /** | |
161 * @param e | |
162 */ | |
163 public void setE(BigInteger e) { | |
164 if(f == null) { | |
165 throw new IllegalStateException("DhDsaExchange not initialized!"); | |
166 } | |
167 | |
168 if(BigInteger.ZERO.compareTo(e) >= 0 || p.compareTo(e) <= 0) { | |
169 throw new IllegalArgumentException("Invalid e specified!"); | |
170 } | |
171 | |
172 this.e = e; | |
173 this.k = e.modPow(y, p); | |
174 } | |
175 | |
176 public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload, | |
177 byte[] serverKexPayload, byte[] hostKey) throws IOException { | |
178 HashForSSH2Types hash = new HashForSSH2Types("SHA1"); | |
179 | |
180 if(log.isInfoEnabled()) { | |
181 log.info("Client: '" + StringEncoder.GetString(clientversion) + "'"); | |
182 log.info("Server: '" + StringEncoder.GetString(serverversion) + "'"); | |
183 } | |
184 | |
185 hash.updateByteString(clientversion); | |
186 hash.updateByteString(serverversion); | |
187 hash.updateByteString(clientKexPayload); | |
188 hash.updateByteString(serverKexPayload); | |
189 hash.updateByteString(hostKey); | |
190 hash.updateBigInt(e); | |
191 hash.updateBigInt(f); | |
192 hash.updateBigInt(k); | |
193 | |
194 return hash.getDigest(); | |
195 } | |
196 } |