2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
1. Redistributions of source code must retain the above copyright notice,
8
this list of conditions and the following disclaimer.
10
2. Redistributions in binary form must reproduce the above copyright
11
notice, this list of conditions and the following disclaimer in
12
the documentation and/or other materials provided with the distribution.
14
3. The names of the authors may not be used to endorse or promote products
15
derived from this software without specific prior written permission.
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
This code is based on jsch (http://www.jcraft.com/jsch).
29
All credit should go to the authors of jsch.
37
public class SignatureDSA : NSch.SignatureDSA
39
internal Signature signature;
41
internal KeyFactory keyFactory;
43
/// <exception cref="System.Exception"></exception>
44
public virtual void Init()
46
signature = Signature.GetInstance("SHA1withDSA");
47
keyFactory = KeyFactory.GetInstance("DSA");
50
/// <exception cref="System.Exception"></exception>
51
public virtual void SetPubKey(byte[] y, byte[] p, byte[] q, byte[] g)
53
DSAPublicKeySpec dsaPubKeySpec = new DSAPublicKeySpec(new BigInteger(y), new BigInteger
54
(p), new BigInteger(q), new BigInteger(g));
55
PublicKey pubKey = keyFactory.GeneratePublic(dsaPubKeySpec);
56
signature.InitVerify(pubKey);
59
/// <exception cref="System.Exception"></exception>
60
public virtual void SetPrvKey(byte[] x, byte[] p, byte[] q, byte[] g)
62
DSAPrivateKeySpec dsaPrivKeySpec = new DSAPrivateKeySpec(new BigInteger(x), new BigInteger
63
(p), new BigInteger(q), new BigInteger(g));
64
PrivateKey prvKey = keyFactory.GeneratePrivate(dsaPrivKeySpec);
65
signature.InitSign(prvKey);
68
/// <exception cref="System.Exception"></exception>
69
public virtual byte[] Sign()
71
return signature.Sign ();
74
/// <exception cref="System.Exception"></exception>
75
public virtual void Update(byte[] foo)
77
signature.Update(foo);
80
/// <exception cref="System.Exception"></exception>
81
public virtual bool Verify(byte[] sig)
86
if (sig[0] == 0 && sig[1] == 0 && sig[2] == 0)
88
j = ((sig[i++] << 24) & unchecked((int)(0xff000000))) | ((sig[i++] << 16) & unchecked(
89
(int)(0x00ff0000))) | ((sig[i++] << 8) & unchecked((int)(0x0000ff00))) | ((sig[i
90
++]) & unchecked((int)(0x000000ff)));
92
j = ((sig[i++] << 24) & unchecked((int)(0xff000000))) | ((sig[i++] << 16) & unchecked(
93
(int)(0x00ff0000))) | ((sig[i++] << 8) & unchecked((int)(0x0000ff00))) | ((sig[i
94
++]) & unchecked((int)(0x000000ff)));
96
System.Array.Copy(sig, i, tmp, 0, j);
100
int frst = ((sig[0] & unchecked((int)(0x80))) != 0 ? 1 : 0);
101
int scnd = ((sig[20] & unchecked((int)(0x80))) != 0 ? 1 : 0);
102
//System.err.println("frst: "+frst+", scnd: "+scnd);
103
int length = sig.Length + 6 + frst + scnd;
104
tmp = new byte[length];
105
tmp[0] = unchecked((byte)unchecked((int)(0x30)));
106
tmp[1] = unchecked((byte)unchecked((int)(0x2c)));
107
tmp[1] += (byte)frst;
108
tmp[1] += (byte)scnd;
109
tmp[2] = unchecked((byte)unchecked((int)(0x02)));
110
tmp[3] = unchecked((byte)unchecked((int)(0x14)));
111
tmp[3] += (byte)frst;
112
System.Array.Copy(sig, 0, tmp, 4 + frst, 20);
113
tmp[4 + tmp[3]] = unchecked((byte)unchecked((int)(0x02)));
114
tmp[5 + tmp[3]] = unchecked((byte)unchecked((int)(0x14)));
115
tmp[5 + tmp[3]] += (byte)scnd;
116
System.Array.Copy(sig, 20, tmp, 6 + tmp[3] + scnd, 20);
118
return signature.Verify(sig);