org.spongycastle.jcajce.provider.asymmetric.dsa.DSASigner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prov Show documentation
Show all versions of prov Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
The newest version!
package org.spongycastle.jcajce.provider.asymmetric.dsa;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.SignatureSpi;
import java.security.spec.AlgorithmParameterSpec;
import org.spongycastle.asn1.ASN1Encoding;
import org.spongycastle.asn1.ASN1Integer;
import org.spongycastle.asn1.ASN1Primitive;
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.DERSequence;
import org.spongycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.spongycastle.asn1.x509.X509ObjectIdentifiers;
import org.spongycastle.crypto.CipherParameters;
import org.spongycastle.crypto.DSA;
import org.spongycastle.crypto.Digest;
import org.spongycastle.crypto.digests.NullDigest;
import org.spongycastle.crypto.params.ParametersWithRandom;
import org.spongycastle.crypto.signers.HMacDSAKCalculator;
import org.spongycastle.crypto.util.DigestFactory;
import org.spongycastle.util.Arrays;
public class DSASigner
extends SignatureSpi
implements PKCSObjectIdentifiers, X509ObjectIdentifiers
{
private Digest digest;
private DSA signer;
private SecureRandom random;
protected DSASigner(
Digest digest,
DSA signer)
{
this.digest = digest;
this.signer = signer;
}
protected void engineInitVerify(
PublicKey publicKey)
throws InvalidKeyException
{
CipherParameters param = DSAUtil.generatePublicKeyParameter(publicKey);
digest.reset();
signer.init(false, param);
}
protected void engineInitSign(
PrivateKey privateKey,
SecureRandom random)
throws InvalidKeyException
{
this.random = random;
engineInitSign(privateKey);
}
protected void engineInitSign(
PrivateKey privateKey)
throws InvalidKeyException
{
CipherParameters param = DSAUtil.generatePrivateKeyParameter(privateKey);
if (random != null)
{
param = new ParametersWithRandom(param, random);
}
digest.reset();
signer.init(true, param);
}
protected void engineUpdate(
byte b)
throws SignatureException
{
digest.update(b);
}
protected void engineUpdate(
byte[] b,
int off,
int len)
throws SignatureException
{
digest.update(b, off, len);
}
protected byte[] engineSign()
throws SignatureException
{
byte[] hash = new byte[digest.getDigestSize()];
digest.doFinal(hash, 0);
try
{
BigInteger[] sig = signer.generateSignature(hash);
return derEncode(sig[0], sig[1]);
}
catch (Exception e)
{
throw new SignatureException(e.toString());
}
}
protected boolean engineVerify(
byte[] sigBytes)
throws SignatureException
{
byte[] hash = new byte[digest.getDigestSize()];
digest.doFinal(hash, 0);
BigInteger[] sig;
try
{
sig = derDecode(sigBytes);
}
catch (Exception e)
{
throw new SignatureException("error decoding signature bytes.");
}
return signer.verifySignature(hash, sig[0], sig[1]);
}
protected void engineSetParameter(
AlgorithmParameterSpec params)
{
throw new UnsupportedOperationException("engineSetParameter unsupported");
}
/**
* @deprecated replaced with
*/
protected void engineSetParameter(
String param,
Object value)
{
throw new UnsupportedOperationException("engineSetParameter unsupported");
}
/**
* @deprecated
*/
protected Object engineGetParameter(
String param)
{
throw new UnsupportedOperationException("engineSetParameter unsupported");
}
private byte[] derEncode(
BigInteger r,
BigInteger s)
throws IOException
{
ASN1Integer[] rs = new ASN1Integer[]{ new ASN1Integer(r), new ASN1Integer(s) };
return new DERSequence(rs).getEncoded(ASN1Encoding.DER);
}
private BigInteger[] derDecode(
byte[] encoding)
throws IOException
{
ASN1Sequence s = (ASN1Sequence)ASN1Primitive.fromByteArray(encoding);
if (s.size() != 2)
{
throw new IOException("malformed signature");
}
if (!Arrays.areEqual(encoding, s.getEncoded(ASN1Encoding.DER)))
{
throw new IOException("malformed signature");
}
return new BigInteger[]{
((ASN1Integer)s.getObjectAt(0)).getValue(),
((ASN1Integer)s.getObjectAt(1)).getValue()
};
}
static public class stdDSA
extends DSASigner
{
public stdDSA()
{
super(DigestFactory.createSHA1(), new org.spongycastle.crypto.signers.DSASigner());
}
}
static public class detDSA
extends DSASigner
{
public detDSA()
{
super(DigestFactory.createSHA1(), new org.spongycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(DigestFactory.createSHA1())));
}
}
static public class dsa224
extends DSASigner
{
public dsa224()
{
super(DigestFactory.createSHA224(), new org.spongycastle.crypto.signers.DSASigner());
}
}
static public class detDSA224
extends DSASigner
{
public detDSA224()
{
super(DigestFactory.createSHA224(), new org.spongycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(DigestFactory.createSHA224())));
}
}
static public class dsa256
extends DSASigner
{
public dsa256()
{
super(DigestFactory.createSHA256(), new org.spongycastle.crypto.signers.DSASigner());
}
}
static public class detDSA256
extends DSASigner
{
public detDSA256()
{
super(DigestFactory.createSHA256(), new org.spongycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(DigestFactory.createSHA256())));
}
}
static public class dsa384
extends DSASigner
{
public dsa384()
{
super(DigestFactory.createSHA384(), new org.spongycastle.crypto.signers.DSASigner());
}
}
static public class detDSA384
extends DSASigner
{
public detDSA384()
{
super(DigestFactory.createSHA384(), new org.spongycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(DigestFactory.createSHA384())));
}
}
static public class dsa512
extends DSASigner
{
public dsa512()
{
super(DigestFactory.createSHA512(), new org.spongycastle.crypto.signers.DSASigner());
}
}
static public class detDSA512
extends DSASigner
{
public detDSA512()
{
super(DigestFactory.createSHA512(), new org.spongycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(DigestFactory.createSHA512())));
}
}
static public class dsaSha3_224
extends DSASigner
{
public dsaSha3_224()
{
super(DigestFactory.createSHA3_224(), new org.spongycastle.crypto.signers.DSASigner());
}
}
static public class detDSASha3_224
extends DSASigner
{
public detDSASha3_224()
{
super(DigestFactory.createSHA3_224(), new org.spongycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(DigestFactory.createSHA3_224())));
}
}
static public class dsaSha3_256
extends DSASigner
{
public dsaSha3_256()
{
super(DigestFactory.createSHA3_256(), new org.spongycastle.crypto.signers.DSASigner());
}
}
static public class detDSASha3_256
extends DSASigner
{
public detDSASha3_256()
{
super(DigestFactory.createSHA3_256(), new org.spongycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(DigestFactory.createSHA3_256())));
}
}
static public class dsaSha3_384
extends DSASigner
{
public dsaSha3_384()
{
super(DigestFactory.createSHA3_384(), new org.spongycastle.crypto.signers.DSASigner());
}
}
static public class detDSASha3_384
extends DSASigner
{
public detDSASha3_384()
{
super(DigestFactory.createSHA3_384(), new org.spongycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(DigestFactory.createSHA3_384())));
}
}
static public class dsaSha3_512
extends DSASigner
{
public dsaSha3_512()
{
super(DigestFactory.createSHA3_512(), new org.spongycastle.crypto.signers.DSASigner());
}
}
static public class detDSASha3_512
extends DSASigner
{
public detDSASha3_512()
{
super(DigestFactory.createSHA3_512(), new org.spongycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(DigestFactory.createSHA3_512())));
}
}
static public class noneDSA
extends DSASigner
{
public noneDSA()
{
super(new NullDigest(), new org.spongycastle.crypto.signers.DSASigner());
}
}
}