aboutsummaryrefslogtreecommitdiffstats
path: root/lib/src/main/java/com/trilead/ssh2/signature/DSASHA1Verify.java
blob: c838ebdb1f04d9e88d5325c979238b7dd6d99d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.trilead.ssh2.signature;

import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;

import com.trilead.ssh2.crypto.digest.SHA1;
import com.trilead.ssh2.log.Logger;
import com.trilead.ssh2.packets.TypesReader;
import com.trilead.ssh2.packets.TypesWriter;


/**
 * DSASHA1Verify.
 * 
 * @author Christian Plattner, plattner@trilead.com
 * @version $Id: DSASHA1Verify.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
 */
public class DSASHA1Verify
{
	private static final Logger log = Logger.getLogger(DSASHA1Verify.class);

	public static DSAPublicKey decodeSSHDSAPublicKey(byte[] key) throws IOException
	{
		TypesReader tr = new TypesReader(key);

		String key_format = tr.readString();

		if (key_format.equals("ssh-dss") == false)
			throw new IllegalArgumentException("This is not a ssh-dss public key!");

		BigInteger p = tr.readMPINT();
		BigInteger q = tr.readMPINT();
		BigInteger g = tr.readMPINT();
		BigInteger y = tr.readMPINT();

		if (tr.remain() != 0)
			throw new IOException("Padding in DSA public key!");

		return new DSAPublicKey(p, q, g, y);
	}

	public static byte[] encodeSSHDSAPublicKey(DSAPublicKey pk) throws IOException
	{
		TypesWriter tw = new TypesWriter();

		tw.writeString("ssh-dss");
		tw.writeMPInt(pk.getP());
		tw.writeMPInt(pk.getQ());
		tw.writeMPInt(pk.getG());
		tw.writeMPInt(pk.getY());

		return tw.getBytes();
	}

	public static byte[] encodeSSHDSASignature(DSASignature ds)
	{
		TypesWriter tw = new TypesWriter();

		tw.writeString("ssh-dss");

		byte[] r = ds.getR().toByteArray();
		byte[] s = ds.getS().toByteArray();

		byte[] a40 = new byte[40];

		/* Patch (unsigned) r and s into the target array. */

		int r_copylen = (r.length < 20) ? r.length : 20;
		int s_copylen = (s.length < 20) ? s.length : 20;

		System.arraycopy(r, r.length - r_copylen, a40, 20 - r_copylen, r_copylen);
		System.arraycopy(s, s.length - s_copylen, a40, 40 - s_copylen, s_copylen);

		tw.writeString(a40, 0, 40);

		return tw.getBytes();
	}

	public static DSASignature decodeSSHDSASignature(byte[] sig) throws IOException
	{
		byte[] rsArray = null;
		
		if (sig.length == 40)
		{
			/* OK, another broken SSH server. */
			rsArray = sig;	
		}
		else
		{
			/* Hopefully a server obeing the standard... */
			TypesReader tr = new TypesReader(sig);

			String sig_format = tr.readString();

			if (sig_format.equals("ssh-dss") == false)
				throw new IOException("Peer sent wrong signature format");

			rsArray = tr.readByteString();

			if (rsArray.length != 40)
				throw new IOException("Peer sent corrupt signature");

			if (tr.remain() != 0)
				throw new IOException("Padding in DSA signature!");
		}

		/* Remember, s and r are unsigned ints. */

		byte[] tmp = new byte[20];

		System.arraycopy(rsArray, 0, tmp, 0, 20);
		BigInteger r = new BigInteger(1, tmp);

		System.arraycopy(rsArray, 20, tmp, 0, 20);
		BigInteger s = new BigInteger(1, tmp);

		if (log.isEnabled())
		{
			log.log(30, "decoded ssh-dss signature: first bytes r(" + ((rsArray[0]) & 0xff) + "), s("
					+ ((rsArray[20]) & 0xff) + ")");
		}

		return new DSASignature(r, s);
	}

	public static boolean verifySignature(byte[] message, DSASignature ds, DSAPublicKey dpk) throws IOException
	{
		/* Inspired by Bouncycastle's DSASigner class */

		SHA1 md = new SHA1();
		md.update(message);
		byte[] sha_message = new byte[md.getDigestLength()];
		md.digest(sha_message);

		BigInteger m = new BigInteger(1, sha_message);

		BigInteger r = ds.getR();
		BigInteger s = ds.getS();

		BigInteger g = dpk.getG();
		BigInteger p = dpk.getP();
		BigInteger q = dpk.getQ();
		BigInteger y = dpk.getY();

		BigInteger zero = BigInteger.ZERO;

		if (log.isEnabled())
		{
			log.log(60, "ssh-dss signature: m: " + m.toString(16));
			log.log(60, "ssh-dss signature: r: " + r.toString(16));
			log.log(60, "ssh-dss signature: s: " + s.toString(16));
			log.log(60, "ssh-dss signature: g: " + g.toString(16));
			log.log(60, "ssh-dss signature: p: " + p.toString(16));
			log.log(60, "ssh-dss signature: q: " + q.toString(16));
			log.log(60, "ssh-dss signature: y: " + y.toString(16));
		}

		if (zero.compareTo(r) >= 0 || q.compareTo(r) <= 0)
		{
			log.log(20, "ssh-dss signature: zero.compareTo(r) >= 0 || q.compareTo(r) <= 0");
			return false;
		}

		if (zero.compareTo(s) >= 0 || q.compareTo(s) <= 0)
		{
			log.log(20, "ssh-dss signature: zero.compareTo(s) >= 0 || q.compareTo(s) <= 0");
			return false;
		}

		BigInteger w = s.modInverse(q);

		BigInteger u1 = m.multiply(w).mod(q);
		BigInteger u2 = r.multiply(w).mod(q);

		u1 = g.modPow(u1, p);
		u2 = y.modPow(u2, p);

		BigInteger v = u1.multiply(u2).mod(p).mod(q);

		return v.equals(r);
	}

	public static DSASignature generateSignature(byte[] message, DSAPrivateKey pk, SecureRandom rnd)
	{
		SHA1 md = new SHA1();
		md.update(message);
		byte[] sha_message = new byte[md.getDigestLength()];
		md.digest(sha_message);

		BigInteger m = new BigInteger(1, sha_message);
		BigInteger k;
		int qBitLength = pk.getQ().bitLength();

		do
		{
			k = new BigInteger(qBitLength, rnd);
		}
		while (k.compareTo(pk.getQ()) >= 0);

		BigInteger r = pk.getG().modPow(k, pk.getP()).mod(pk.getQ());

		k = k.modInverse(pk.getQ()).multiply(m.add((pk).getX().multiply(r)));

		BigInteger s = k.mod(pk.getQ());

		return new DSASignature(r, s);
	}
}