~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NSch/NSch.Jce/SignatureDSA.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, with or without
 
5
modification, are permitted provided that the following conditions are met:
 
6
 
 
7
  1. Redistributions of source code must retain the above copyright notice,
 
8
     this list of conditions and the following disclaimer.
 
9
 
 
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.
 
13
 
 
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.
 
16
 
 
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.
 
27
 
 
28
This code is based on jsch (http://www.jcraft.com/jsch).
 
29
All credit should go to the authors of jsch.
 
30
*/
 
31
 
 
32
using Mono.Math;
 
33
using Sharpen;
 
34
 
 
35
namespace NSch.Jce
 
36
{
 
37
        public class SignatureDSA : NSch.SignatureDSA
 
38
        {
 
39
                internal Signature signature;
 
40
 
 
41
                internal KeyFactory keyFactory;
 
42
 
 
43
                /// <exception cref="System.Exception"></exception>
 
44
                public virtual void Init()
 
45
                {
 
46
                        signature = Signature.GetInstance("SHA1withDSA");
 
47
                        keyFactory = KeyFactory.GetInstance("DSA");
 
48
                }
 
49
 
 
50
                /// <exception cref="System.Exception"></exception>
 
51
                public virtual void SetPubKey(byte[] y, byte[] p, byte[] q, byte[] g)
 
52
                {
 
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);
 
57
                }
 
58
 
 
59
                /// <exception cref="System.Exception"></exception>
 
60
                public virtual void SetPrvKey(byte[] x, byte[] p, byte[] q, byte[] g)
 
61
                {
 
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);
 
66
                }
 
67
 
 
68
                /// <exception cref="System.Exception"></exception>
 
69
                public virtual byte[] Sign()
 
70
                {
 
71
                        return signature.Sign ();
 
72
                }
 
73
 
 
74
                /// <exception cref="System.Exception"></exception>
 
75
                public virtual void Update(byte[] foo)
 
76
                {
 
77
                        signature.Update(foo);
 
78
                }
 
79
 
 
80
                /// <exception cref="System.Exception"></exception>
 
81
                public virtual bool Verify(byte[] sig)
 
82
                {
 
83
                        int i = 0;
 
84
                        int j = 0;
 
85
                        byte[] tmp;
 
86
                        if (sig[0] == 0 && sig[1] == 0 && sig[2] == 0)
 
87
                        {
 
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)));
 
91
                                i += j;
 
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)));
 
95
                                tmp = new byte[j];
 
96
                                System.Array.Copy(sig, i, tmp, 0, j);
 
97
                                sig = tmp;
 
98
                        }
 
99
                        // ASN.1
 
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);
 
117
                        sig = tmp;
 
118
                        return signature.Verify(sig);
 
119
                }
 
120
        }
 
121
}