comparison src/ch/ethz/ssh2/crypto/dh/DhGroupExchange.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents 91a31873c42a
children cb179051f0f2
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
21 /* Given by the standard */ 21 /* Given by the standard */
22 22
23 private BigInteger p; 23 private BigInteger p;
24 private BigInteger g; 24 private BigInteger g;
25 25
26 /* Client public and private */ 26 /* Client public and private */
27 27
28 private BigInteger e; 28 private BigInteger e;
29 private BigInteger x; 29 private BigInteger x;
30 30
31 /* Server public */ 31 /* Server public */
32 32
33 private BigInteger f; 33 private BigInteger f;
34 34
35 /* Shared secret */ 35 /* Shared secret */
36 36
37 private BigInteger k; 37 private BigInteger k;
38 38
39 public DhGroupExchange(BigInteger p, BigInteger g) { 39 public DhGroupExchange(BigInteger p, BigInteger g) {
40 this.p = p; 40 this.p = p;
41 this.g = g; 41 this.g = g;
42 } 42 }
43 43
44 public void init(SecureRandom rnd) { 44 public void init(SecureRandom rnd) {
45 k = null; 45 k = null;
46
47 x = new BigInteger(p.bitLength() - 1, rnd); 46 x = new BigInteger(p.bitLength() - 1, rnd);
48 e = g.modPow(x, p); 47 e = g.modPow(x, p);
49 } 48 }
50 49
51 /** 50 /**
52 * @return Returns the e. 51 * @return Returns the e.
53 */ 52 */
54 public BigInteger getE() { 53 public BigInteger getE() {
55 if(e == null) { 54 if (e == null) {
56 throw new IllegalStateException("Not initialized!"); 55 throw new IllegalStateException("Not initialized!");
57 } 56 }
58 57
59 return e; 58 return e;
60 } 59 }
61 60
62 /** 61 /**
63 * @return Returns the shared secret k. 62 * @return Returns the shared secret k.
64 */ 63 */
65 public BigInteger getK() { 64 public BigInteger getK() {
66 if(k == null) { 65 if (k == null) {
67 throw new IllegalStateException("Shared secret not yet known, need f first!"); 66 throw new IllegalStateException("Shared secret not yet known, need f first!");
68 } 67 }
69 68
70 return k; 69 return k;
71 } 70 }
72 71
73 /** 72 /**
74 * Sets f and calculates the shared secret. 73 * Sets f and calculates the shared secret.
75 */ 74 */
76 public void setF(BigInteger f) { 75 public void setF(BigInteger f) {
77 if(e == null) { 76 if (e == null) {
78 throw new IllegalStateException("Not initialized!"); 77 throw new IllegalStateException("Not initialized!");
79 } 78 }
80 79
81 BigInteger zero = BigInteger.valueOf(0); 80 BigInteger zero = BigInteger.valueOf(0);
82 81
83 if(zero.compareTo(f) >= 0 || p.compareTo(f) <= 0) { 82 if (zero.compareTo(f) >= 0 || p.compareTo(f) <= 0) {
84 throw new IllegalArgumentException("Invalid f specified!"); 83 throw new IllegalArgumentException("Invalid f specified!");
85 } 84 }
86 85
87 this.f = f; 86 this.f = f;
88 this.k = f.modPow(x, p); 87 this.k = f.modPow(x, p);
89 } 88 }
90 89
91 public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload, 90 public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
92 byte[] serverKexPayload, byte[] hostKey, DHGexParameters para) throws IOException { 91 byte[] serverKexPayload, byte[] hostKey, DHGexParameters para) throws IOException {
93 HashForSSH2Types hash = new HashForSSH2Types("SHA1"); 92 HashForSSH2Types hash = new HashForSSH2Types("SHA1");
94
95 hash.updateByteString(clientversion); 93 hash.updateByteString(clientversion);
96 hash.updateByteString(serverversion); 94 hash.updateByteString(serverversion);
97 hash.updateByteString(clientKexPayload); 95 hash.updateByteString(clientKexPayload);
98 hash.updateByteString(serverKexPayload); 96 hash.updateByteString(serverKexPayload);
99 hash.updateByteString(hostKey); 97 hash.updateByteString(hostKey);
100 if(para.getMin_group_len() > 0) { 98
99 if (para.getMin_group_len() > 0) {
101 hash.updateUINT32(para.getMin_group_len()); 100 hash.updateUINT32(para.getMin_group_len());
102 } 101 }
102
103 hash.updateUINT32(para.getPref_group_len()); 103 hash.updateUINT32(para.getPref_group_len());
104 if(para.getMax_group_len() > 0) { 104
105 if (para.getMax_group_len() > 0) {
105 hash.updateUINT32(para.getMax_group_len()); 106 hash.updateUINT32(para.getMax_group_len());
106 } 107 }
108
107 hash.updateBigInt(p); 109 hash.updateBigInt(p);
108 hash.updateBigInt(g); 110 hash.updateBigInt(g);
109 hash.updateBigInt(e); 111 hash.updateBigInt(e);
110 hash.updateBigInt(f); 112 hash.updateBigInt(f);
111 hash.updateBigInt(k); 113 hash.updateBigInt(k);
112
113 return hash.getDigest(); 114 return hash.getDigest();
114 } 115 }
115 } 116 }