comparison src/ch/ethz/ssh2/crypto/dh/DhGroupExchange.java @ 309:cb179051f0f2 ganymed

add ecdsa key support everywhere
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:29:39 -0700
parents 071eccdff8ea
children 1442be38051b
comparison
equal deleted inserted replaced
308:42b15aaa7ac7 309:cb179051f0f2
1 /* 1
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2.crypto.dh; 2 package ch.ethz.ssh2.crypto.dh;
6 3
7 import java.io.IOException;
8 import java.math.BigInteger; 4 import java.math.BigInteger;
9 import java.security.SecureRandom; 5 import java.security.SecureRandom;
10 6
11 import ch.ethz.ssh2.DHGexParameters; 7 import ch.ethz.ssh2.DHGexParameters;
12 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types; 8 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types;
13 9
10
14 /** 11 /**
15 * DhGroupExchange. 12 * DhGroupExchange.
16 * 13 *
17 * @author Christian Plattner 14 * @author Christian Plattner, plattner@trilead.com
18 * @version 2.50, 03/15/10 15 * @version $Id: DhGroupExchange.java,v 1.1 2007/10/15 12:49:57 cplattne Exp $
19 */ 16 */
20 public class DhGroupExchange { 17 public class DhGroupExchange {
21 /* Given by the standard */ 18 /* Given by the standard */
22 19
23 private BigInteger p; 20 private BigInteger p;
49 46
50 /** 47 /**
51 * @return Returns the e. 48 * @return Returns the e.
52 */ 49 */
53 public BigInteger getE() { 50 public BigInteger getE() {
54 if (e == null) { 51 if (e == null)
55 throw new IllegalStateException("Not initialized!"); 52 throw new IllegalStateException("Not initialized!");
56 }
57 53
58 return e; 54 return e;
59 } 55 }
60 56
61 /** 57 /**
62 * @return Returns the shared secret k. 58 * @return Returns the shared secret k.
63 */ 59 */
64 public BigInteger getK() { 60 public BigInteger getK() {
65 if (k == null) { 61 if (k == null)
66 throw new IllegalStateException("Shared secret not yet known, need f first!"); 62 throw new IllegalStateException("Shared secret not yet known, need f first!");
67 }
68 63
69 return k; 64 return k;
70 } 65 }
71 66
72 /** 67 /**
73 * Sets f and calculates the shared secret. 68 * Sets f and calculates the shared secret.
74 */ 69 */
75 public void setF(BigInteger f) { 70 public void setF(BigInteger f) {
76 if (e == null) { 71 if (e == null)
77 throw new IllegalStateException("Not initialized!"); 72 throw new IllegalStateException("Not initialized!");
78 }
79 73
80 BigInteger zero = BigInteger.valueOf(0); 74 BigInteger zero = BigInteger.valueOf(0);
81 75
82 if (zero.compareTo(f) >= 0 || p.compareTo(f) <= 0) { 76 if (zero.compareTo(f) >= 0 || p.compareTo(f) <= 0)
83 throw new IllegalArgumentException("Invalid f specified!"); 77 throw new IllegalArgumentException("Invalid f specified!");
84 }
85 78
86 this.f = f; 79 this.f = f;
87 this.k = f.modPow(x, p); 80 this.k = f.modPow(x, p);
88 } 81 }
89 82
90 public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload, 83 public byte[] calculateH(String hashAlgo, byte[] clientversion, byte[] serverversion,
91 byte[] serverKexPayload, byte[] hostKey, DHGexParameters para) throws IOException { 84 byte[] clientKexPayload, byte[] serverKexPayload, byte[] hostKey, DHGexParameters para) {
92 HashForSSH2Types hash = new HashForSSH2Types("SHA1"); 85 HashForSSH2Types hash = new HashForSSH2Types(hashAlgo);
93 hash.updateByteString(clientversion); 86 hash.updateByteString(clientversion);
94 hash.updateByteString(serverversion); 87 hash.updateByteString(serverversion);
95 hash.updateByteString(clientKexPayload); 88 hash.updateByteString(clientKexPayload);
96 hash.updateByteString(serverKexPayload); 89 hash.updateByteString(serverKexPayload);
97 hash.updateByteString(hostKey); 90 hash.updateByteString(hostKey);
98 91
99 if (para.getMin_group_len() > 0) { 92 if (para.getMin_group_len() > 0)
100 hash.updateUINT32(para.getMin_group_len()); 93 hash.updateUINT32(para.getMin_group_len());
101 }
102 94
103 hash.updateUINT32(para.getPref_group_len()); 95 hash.updateUINT32(para.getPref_group_len());
104 96
105 if (para.getMax_group_len() > 0) { 97 if (para.getMax_group_len() > 0)
106 hash.updateUINT32(para.getMax_group_len()); 98 hash.updateUINT32(para.getMax_group_len());
107 }
108 99
109 hash.updateBigInt(p); 100 hash.updateBigInt(p);
110 hash.updateBigInt(g); 101 hash.updateBigInt(g);
111 hash.updateBigInt(e); 102 hash.updateBigInt(e);
112 hash.updateBigInt(f); 103 hash.updateBigInt(f);