1 2 package ch.ethz.ssh2.crypto.dh; 3 4 import java.math.BigInteger ; 5 import java.security.SecureRandom ; 6 7 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types; 8 import ch.ethz.ssh2.log.Logger; 9 10 16 public class DhExchange 17 { 18 private static final Logger log = Logger.getLogger(DhExchange.class); 19 20 21 22 static final BigInteger p1, p14; 23 static final BigInteger g; 24 25 BigInteger p; 26 27 28 29 BigInteger e; 30 BigInteger x; 31 32 33 34 BigInteger f; 35 36 37 38 BigInteger k; 39 40 static 41 { 42 final String p1_string = "17976931348623159077083915679378745319786029604875" 43 + "60117064444236841971802161585193689478337958649255415021805654859805036464" 44 + "40548199239100050792877003355816639229553136239076508735759914822574862575" 45 + "00742530207744771258955095793777842444242661733472762929938766870920560605" 46 + "0270810842907692932019128194467627007"; 47 48 final String p14_string = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129" 49 + "024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0" 50 + "A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB" 51 + "6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A" 52 + "163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208" 53 + "552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36C" 54 + "E3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF69558171" 55 + "83995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF"; 56 57 p1 = new BigInteger (p1_string); 58 p14 = new BigInteger (p14_string, 16); 59 g = new BigInteger ("2"); 60 } 61 62 public DhExchange() 63 { 64 } 65 66 public void init(int group, SecureRandom 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 ("Unknown DH group " + group); 76 77 x = new BigInteger (p.bitLength() - 1, rnd); 78 79 e = g.modPow(x, p); 80 } 81 82 86 public BigInteger getE() 87 { 88 if (e == null) 89 throw new IllegalStateException ("DhDsaExchange not initialized!"); 90 91 return e; 92 } 93 94 98 public BigInteger getK() 99 { 100 if (k == null) 101 throw new IllegalStateException ("Shared secret not yet known, need f first!"); 102 103 return k; 104 } 105 106 109 public void setF(BigInteger f) 110 { 111 if (e == null) 112 throw new IllegalStateException ("DhDsaExchange not initialized!"); 113 114 BigInteger zero = BigInteger.valueOf(0); 115 116 if (zero.compareTo(f) >= 0 || p.compareTo(f) <= 0) 117 throw new IllegalArgumentException ("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 (clientversion) + "'"); 131 log.log(90, "Server: '" + new String (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 |