1package org.bouncycastle.jcajce.provider.asymmetric.util;
2
3import java.math.BigInteger;
4import java.security.SignatureException;
5import java.security.SignatureSpi;
6import java.security.spec.AlgorithmParameterSpec;
7
8import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
9import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
10import org.bouncycastle.crypto.DSA;
11import org.bouncycastle.crypto.Digest;
12
13public abstract class DSABase
14    extends SignatureSpi
15    implements PKCSObjectIdentifiers, X509ObjectIdentifiers
16{
17    protected Digest digest;
18    protected DSA                     signer;
19    protected DSAEncoder              encoder;
20
21    protected DSABase(
22        Digest                  digest,
23        DSA                     signer,
24        DSAEncoder              encoder)
25    {
26        this.digest = digest;
27        this.signer = signer;
28        this.encoder = encoder;
29    }
30
31    protected void engineUpdate(
32        byte    b)
33        throws SignatureException
34    {
35        digest.update(b);
36    }
37
38    protected void engineUpdate(
39        byte[]  b,
40        int     off,
41        int     len)
42        throws SignatureException
43    {
44        digest.update(b, off, len);
45    }
46
47    protected byte[] engineSign()
48        throws SignatureException
49    {
50        byte[]  hash = new byte[digest.getDigestSize()];
51
52        digest.doFinal(hash, 0);
53
54        try
55        {
56            BigInteger[]    sig = signer.generateSignature(hash);
57
58            return encoder.encode(sig[0], sig[1]);
59        }
60        catch (Exception e)
61        {
62            throw new SignatureException(e.toString());
63        }
64    }
65
66    protected boolean engineVerify(
67        byte[]  sigBytes)
68        throws SignatureException
69    {
70        byte[]  hash = new byte[digest.getDigestSize()];
71
72        digest.doFinal(hash, 0);
73
74        BigInteger[]    sig;
75
76        try
77        {
78            sig = encoder.decode(sigBytes);
79        }
80        catch (Exception e)
81        {
82            throw new SignatureException("error decoding signature bytes.");
83        }
84
85        return signer.verifySignature(hash, sig[0], sig[1]);
86    }
87
88    protected void engineSetParameter(
89        AlgorithmParameterSpec params)
90    {
91        throw new UnsupportedOperationException("engineSetParameter unsupported");
92    }
93
94    /**
95     * @deprecated replaced with <a href = "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
96     */
97    protected void engineSetParameter(
98        String  param,
99        Object  value)
100    {
101        throw new UnsupportedOperationException("engineSetParameter unsupported");
102    }
103
104    /**
105     * @deprecated
106     */
107    protected Object engineGetParameter(
108        String      param)
109    {
110        throw new UnsupportedOperationException("engineSetParameter unsupported");
111    }
112}
113