~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/jce/provider/JDKDSASigner.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.jce.provider;
 
2
 
 
3
import java.io.IOException;
 
4
import java.math.BigInteger;
 
5
import java.security.InvalidKeyException;
 
6
import java.security.PrivateKey;
 
7
import java.security.PublicKey;
 
8
import java.security.SecureRandom;
 
9
import java.security.SignatureException;
 
10
import java.security.SignatureSpi;
 
11
import java.security.interfaces.DSAKey;
 
12
import java.security.spec.AlgorithmParameterSpec;
 
13
 
 
14
import org.bouncycastle.asn1.ASN1Encodable;
 
15
import org.bouncycastle.asn1.ASN1Object;
 
16
import org.bouncycastle.asn1.ASN1Sequence;
 
17
import org.bouncycastle.asn1.DERInteger;
 
18
import org.bouncycastle.asn1.DERSequence;
 
19
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
 
20
import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
 
21
import org.bouncycastle.crypto.CipherParameters;
 
22
import org.bouncycastle.crypto.DSA;
 
23
import org.bouncycastle.crypto.Digest;
 
24
import org.bouncycastle.crypto.digests.NullDigest;
 
25
import org.bouncycastle.crypto.digests.SHA1Digest;
 
26
import org.bouncycastle.crypto.digests.SHA224Digest;
 
27
import org.bouncycastle.crypto.digests.SHA256Digest;
 
28
import org.bouncycastle.crypto.digests.SHA384Digest;
 
29
import org.bouncycastle.crypto.digests.SHA512Digest;
 
30
import org.bouncycastle.crypto.params.ParametersWithRandom;
 
31
import org.bouncycastle.crypto.signers.DSASigner;
 
32
import org.bouncycastle.jce.interfaces.GOST3410Key;
 
33
 
 
34
public class JDKDSASigner
 
35
    extends SignatureSpi
 
36
    implements PKCSObjectIdentifiers, X509ObjectIdentifiers
 
37
{
 
38
    private Digest                  digest;
 
39
    private DSA                     signer;
 
40
    private SecureRandom            random;
 
41
 
 
42
    protected JDKDSASigner(
 
43
        Digest                  digest,
 
44
        DSA                     signer)
 
45
    {
 
46
        this.digest = digest;
 
47
        this.signer = signer;
 
48
    }
 
49
 
 
50
    protected void engineInitVerify(
 
51
        PublicKey   publicKey)
 
52
        throws InvalidKeyException
 
53
    {
 
54
        CipherParameters    param;
 
55
 
 
56
        if (publicKey instanceof GOST3410Key)
 
57
        {
 
58
            param = GOST3410Util.generatePublicKeyParameter(publicKey);
 
59
        }
 
60
        else if (publicKey instanceof DSAKey)
 
61
        {
 
62
            param = DSAUtil.generatePublicKeyParameter(publicKey);
 
63
        }
 
64
        else
 
65
        {
 
66
            try
 
67
            {
 
68
                byte[]  bytes = publicKey.getEncoded();
 
69
 
 
70
                publicKey = JDKKeyFactory.createPublicKeyFromDERStream(bytes);
 
71
 
 
72
                if (publicKey instanceof DSAKey)
 
73
                {
 
74
                    param = DSAUtil.generatePublicKeyParameter(publicKey);
 
75
                }
 
76
                else
 
77
                {
 
78
                    throw new InvalidKeyException("can't recognise key type in DSA based signer");
 
79
                }
 
80
            }
 
81
            catch (Exception e)
 
82
            {
 
83
                throw new InvalidKeyException("can't recognise key type in DSA based signer");
 
84
            }
 
85
        }
 
86
 
 
87
        digest.reset();
 
88
        signer.init(false, param);
 
89
    }
 
90
 
 
91
    protected void engineInitSign(
 
92
        PrivateKey      privateKey,
 
93
        SecureRandom    random)
 
94
        throws InvalidKeyException
 
95
    {
 
96
        this.random = random;
 
97
        engineInitSign(privateKey);
 
98
    }
 
99
 
 
100
    protected void engineInitSign(
 
101
        PrivateKey  privateKey)
 
102
        throws InvalidKeyException
 
103
    {
 
104
        CipherParameters    param;
 
105
 
 
106
        if (privateKey instanceof GOST3410Key)
 
107
        {
 
108
            param = GOST3410Util.generatePrivateKeyParameter(privateKey);
 
109
        }
 
110
        else
 
111
        {
 
112
            param = DSAUtil.generatePrivateKeyParameter(privateKey);
 
113
        }
 
114
 
 
115
        if (random != null)
 
116
        {
 
117
            param = new ParametersWithRandom(param, random);
 
118
        }
 
119
 
 
120
        digest.reset();
 
121
        signer.init(true, param);
 
122
    }
 
123
 
 
124
    protected void engineUpdate(
 
125
        byte    b)
 
126
        throws SignatureException
 
127
    {
 
128
        digest.update(b);
 
129
    }
 
130
 
 
131
    protected void engineUpdate(
 
132
        byte[]  b,
 
133
        int     off,
 
134
        int     len) 
 
135
        throws SignatureException
 
136
    {
 
137
        digest.update(b, off, len);
 
138
    }
 
139
 
 
140
    protected byte[] engineSign()
 
141
        throws SignatureException
 
142
    {
 
143
        byte[]  hash = new byte[digest.getDigestSize()];
 
144
 
 
145
        digest.doFinal(hash, 0);
 
146
 
 
147
        try
 
148
        {
 
149
            BigInteger[]    sig = signer.generateSignature(hash);
 
150
 
 
151
            return derEncode(sig[0], sig[1]);
 
152
        }
 
153
        catch (Exception e)
 
154
        {
 
155
            throw new SignatureException(e.toString());
 
156
        }
 
157
    }
 
158
 
 
159
    protected boolean engineVerify(
 
160
        byte[]  sigBytes) 
 
161
        throws SignatureException
 
162
    {
 
163
        byte[]  hash = new byte[digest.getDigestSize()];
 
164
 
 
165
        digest.doFinal(hash, 0);
 
166
 
 
167
        BigInteger[]    sig;
 
168
 
 
169
        try
 
170
        {
 
171
            sig = derDecode(sigBytes);
 
172
        }
 
173
        catch (Exception e)
 
174
        {
 
175
            throw new SignatureException("error decoding signature bytes.");
 
176
        }
 
177
 
 
178
        return signer.verifySignature(hash, sig[0], sig[1]);
 
179
    }
 
180
 
 
181
    protected void engineSetParameter(
 
182
        AlgorithmParameterSpec params)
 
183
    {
 
184
        throw new UnsupportedOperationException("engineSetParameter unsupported");
 
185
    }
 
186
 
 
187
    /**
 
188
     * @deprecated replaced with <a href = "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
 
189
     */
 
190
    protected void engineSetParameter(
 
191
        String  param,
 
192
        Object  value)
 
193
    {
 
194
        throw new UnsupportedOperationException("engineSetParameter unsupported");
 
195
    }
 
196
 
 
197
    /**
 
198
     * @deprecated
 
199
     */
 
200
    protected Object engineGetParameter(
 
201
        String      param)
 
202
    {
 
203
        throw new UnsupportedOperationException("engineSetParameter unsupported");
 
204
    }
 
205
 
 
206
    private byte[] derEncode(
 
207
        BigInteger  r,
 
208
        BigInteger  s)
 
209
        throws IOException
 
210
    {
 
211
        DERInteger[] rs = new DERInteger[]{ new DERInteger(r), new DERInteger(s) };
 
212
        return new DERSequence(rs).getEncoded(ASN1Encodable.DER);
 
213
    }
 
214
 
 
215
    private BigInteger[] derDecode(
 
216
        byte[]  encoding)
 
217
        throws IOException
 
218
    {
 
219
        ASN1Sequence s = (ASN1Sequence)ASN1Object.fromByteArray(encoding);
 
220
        return new BigInteger[]{
 
221
            ((DERInteger)s.getObjectAt(0)).getValue(),
 
222
            ((DERInteger)s.getObjectAt(1)).getValue()
 
223
        };
 
224
    }
 
225
 
 
226
    static public class stdDSA
 
227
        extends JDKDSASigner
 
228
    {
 
229
        public stdDSA()
 
230
        {
 
231
            super(new SHA1Digest(), new DSASigner());
 
232
        }
 
233
    }
 
234
 
 
235
    static public class dsa224
 
236
        extends JDKDSASigner
 
237
    {
 
238
        public dsa224()
 
239
        {
 
240
            super(new SHA224Digest(), new DSASigner());
 
241
        }
 
242
    }
 
243
    
 
244
    static public class dsa256
 
245
        extends JDKDSASigner
 
246
    {
 
247
        public dsa256()
 
248
        {
 
249
            super(new SHA256Digest(), new DSASigner());
 
250
        }
 
251
    }
 
252
    
 
253
    static public class dsa384
 
254
        extends JDKDSASigner
 
255
    {
 
256
        public dsa384()
 
257
        {
 
258
            super(new SHA384Digest(), new DSASigner());
 
259
        }
 
260
    }
 
261
    
 
262
    static public class dsa512
 
263
        extends JDKDSASigner
 
264
    {
 
265
        public dsa512()
 
266
        {
 
267
            super(new SHA512Digest(), new DSASigner());
 
268
        }
 
269
    }
 
270
 
 
271
    static public class noneDSA
 
272
        extends JDKDSASigner
 
273
    {
 
274
        public noneDSA()
 
275
        {
 
276
            super(new NullDigest(), new DSASigner());
 
277
        }
 
278
    }
 
279
}