public class DSA
extends java.security.SignatureSpi
NIST's Digital Signature Standard (DSS) specifies the DSA algorithm as public-key algorithm for being used for digital signing applications. For first calculating a hash value of any data to be signed, the Secure Hash Algorithm (SHA-1) is proposed to be used along with the DSA algorithm. FIPS 186-3 (June 2009) adapted the Digital Signature Algorithm for use with the SHA-2 hash algorithm suite.
This class only adds the functionality of the SHA hash algorithm to the pure
DSA algorithm, which is implemented by the RawDSA
class. An
application wishing to let the Signature engine hash the message before signing it,
shall specify the hash algorithm name (SHA1, SHA224, SHA256) when creating the
Signature engine, e.g.:
An application that already has hashed the data to be signed, may use the raw DSA implementation:Signature dsa = Signature.getInstance("SHA1withDSA");
Signature raw_dsa = Signature.getInstance("RawDSA");
Generally an application intending to sign some message respectively to verify a signature, has to perform three steps:
getInstance
method, e.g.:Signature dsa = Signature.getInstance("SHA1withDSA");
dsa.initSign(dsaPrivateKey);
dsa.initVerify(dsaPublicKey);
sign
method returning the signature as DER encoded byte array.
Otherwise, if the Signature object has been initialized for verifying, first the
data to be verified is supplied to the Signature object, and subsequently the
signature is verified by calling the verify
method, supplied with the
DER encoded byte array holding the corresponding signature:
dsa.update(data); byte[] signature = dsa.sign();
dsa.update(data); System.out.println("Signature " + (dsa.verify(signature) ? "correct!" : "not correct!"));
Constructor and Description |
---|
DSA()
The default constructor.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
engineGetParameter(java.lang.String param)
Returns a previously set KSEED parameter as a byte array.
|
protected java.security.AlgorithmParameters |
engineGetParameters()
Returns the DSA parameters (p, q, g) as
DSAParameters
object. |
protected void |
engineInitSign(java.security.PrivateKey privateKey)
SPI: Initializes this DSA Signature object with the given
DSA private key for going to sign some data.
|
protected void |
engineInitSign(java.security.PrivateKey privateKey,
java.security.SecureRandom random)
SPI: Initializes this Signature object with the given DSA private
key and the given SecureRandom generator for going to sign some data.
|
protected void |
engineInitVerify(java.security.PublicKey publicKey)
SPI: Initializes this DSA Signature object with the given
DSA public key for performing a signature verification.
|
protected void |
engineSetParameter(java.security.spec.AlgorithmParameterSpec params)
Initializes this DSA signature engine with the given parameter set.
|
protected void |
engineSetParameter(java.lang.String param,
java.lang.Object value)
Sets the KSEED parameter for DSA signing.
|
protected byte[] |
engineSign()
SPI: Returns the signature bytes of all the data
updated so far.
|
protected void |
engineUpdate(byte b)
SPI: Updates the data to be signed or verified
with the specified byte.
|
protected void |
engineUpdate(byte[] b,
int off,
int len)
SPI: Updates the data to be signed or verified with the
specified number of bytes, beginning at the specified offset within the given byte array.
|
protected boolean |
engineVerify(byte[] sigBytes)
SPI: Verifies the passed-in signature.
|
static void |
setCheckSigValueForDERCompliance(boolean checkForDER)
Decides whether to check if the encoding of the DSA signature complies
with the Distinguished Encoding Rules (DER).
|
static void |
setDoVerifySignature(boolean doVerify)
Decides whether to verify a DSA signature immediately after having been
created.
|
static void |
setUseBachwardsCompatibilityMode(boolean enableBackwardsCompatibility)
Decides whether to use DSA in backwards compatibility mode.
|
static void |
setUseBlinding(boolean useBlinding)
Decides whether to use blinding for signature generation as countermeasure against
timing attacks.
|
public static void setUseBlinding(boolean useBlinding)
useBlinding
- whether to use blinding (default: true
)public static void setUseBachwardsCompatibilityMode(boolean enableBackwardsCompatibility)
DSA KeyPairGenerator
generates 2048 bit keys and the DSA Signature
engine checks if the security strength of the hash algorithm
complies with the key size in use. This means that when generating
a default key pair (of 2048 bits) and trying to use it with a
"SHA1withDSA" Signature engine the Signature engine will reject the
signature generation because SHA-1 is not appropriate for use
with 2048 DSA keys. When setting
DSA.setUseBackwardsCompatibilityMode(true);the default key length used by the DSAKeyPairGenerator is set to 1024 and the DSA signature engine does not check for the security strength of the hash algorithm. Enabling the backwards compatibility mode is not recommended. More appropriate you should use a SHA224withDSA or SHA256withDSA Signature engine and KeyPairGenerator. Even better you should switch to the elliptic curve variant ECDSA since the usage of DSA is deprecated by security protocols like, for instance, TLS.
Note that this parameter only affects the key pair and signature generation not the signature verification and not the compatibility with other applications.
enableBackwardsCompatibility
- whether to enable backwards compatibility
(default: false
)public static void setCheckSigValueForDERCompliance(boolean checkForDER)
true
a SignatureException is thrown when calling
signature.verify(sigValue)
and the signature value is not
not properly DER encoded (for instance, if the length has not been encoded
in the minimum number of octets).checkForDER
- whether to check if the encoding of the DSA signature complies
with the Distinguished Encoding Rules (DER) (default: true
the signature value is checked)public static void setDoVerifySignature(boolean doVerify)
Verification of an DSA signature maybe appropriate as countermeasure
against fault attacks on signatures (one that is correct and a
second that has a fault) that are produced with the same nonce value.
Verifying a signature immediately after creation may be appropriate
escpecially when DSA is used in deterministic
mode. For that reason this implementation by default verifies deterministic
DSA signatures immediately after creation and (for performance reasons)
does not verify non-deterministic DSA signatures.
An application can change the default behavior by either enabling automatic DSA signature verification for all (deterministic and non-deterministic) signatures (for the sake of security):
DSA.setDoVerifySignature(true);or by disabling it for all (deterministic and non-deterministic) signatures (for the sake of performance):
DSA.setDoVerifySignature(false);
doVerify
- true
to verify any DSA signature immediately after creation,
false
to not verify any DSA signature immediately after creationprotected void engineInitVerify(java.security.PublicKey publicKey) throws java.security.InvalidKeyException
engineInitVerify
in class java.security.SignatureSpi
publicKey
- the DSA public key belonging to the DSA private key that has been used for signingjava.security.InvalidKeyException
- if a key encoding error occursprotected void engineInitSign(java.security.PrivateKey privateKey) throws java.security.InvalidKeyException
engineInitSign
in class java.security.SignatureSpi
privateKey
- the DSA private key to be used for signing.java.security.InvalidKeyException
- if a key encoding error occursprotected void engineInitSign(java.security.PrivateKey privateKey, java.security.SecureRandom random) throws java.security.InvalidKeyException
Note that this method is not available for JDK versions prior JDK 1.2. If a SecureRandom never has been supplied by the application, the signature engine will use a default SecureRandom, if required.
engineInitSign
in class java.security.SignatureSpi
privateKey
- the DSA private key to be used for signing.random
- the random number generatorjava.security.InvalidKeyException
- if a key decoding error occursprotected void engineUpdate(byte b) throws java.security.SignatureException
engineUpdate
in class java.security.SignatureSpi
b
- the byte to be used for updating.java.security.SignatureException
- if the engine has not been properly initializedprotected void engineUpdate(byte[] b, int off, int len) throws java.security.SignatureException
engineUpdate
in class java.security.SignatureSpi
b
- the byte array holding the data to be used for this update operation.off
- the offset, indicating the start position within the given byte array.len
- the number of bytes to be obtained from the given byte array, starting at the given position.java.security.SignatureException
- if the engine has not been properly initialized.protected byte[] engineSign() throws java.security.SignatureException
This method updates the underlying RawDSA with the hashed data and signs
it using the RawDSA engineSign
implementation.
engineSign
in class java.security.SignatureSpi
java.security.SignatureException
- if the engine has not been properly initialized.RawDSA.engineSign()
protected boolean engineVerify(byte[] sigBytes) throws java.security.SignatureException
This method updates the underlying RawDSA with the hashed data and verifies
the given signature using the RawDSA engineVerify
implementation.
engineVerify
in class java.security.SignatureSpi
sigBytes
- the signature bytes to be verified.true
if the signature is o.k., false
if not.java.security.SignatureException
- if the engine is not initialized
properly, or the passed-in signature is improperly encoded or
of the wrong type, etc.RawDSA.engineVerify(byte[])
protected void engineSetParameter(java.lang.String param, java.lang.Object value) throws java.security.InvalidParameterException
The generation of a DSA signature involves a random, secret number k that is usually newly generated for each signature. This method allows you to set this number to a specified value. Use "KSEED" as the name and a byte array or a BigInteger as parameter. Use a null value to unset a previously set seed.
CAUTION: Use of this feature is recommended for testing purposes only.
This method only delegates the given parameter to the underlying RawDSA.
You alternatively may supply a java.security.spec.DSAParameterSpec
parameter identified by name "DSAParameterSpec". This may be useful for
initializing the signature verification in situations where there are no
parameters are included in the subjectPublicKeyInfo field of a
X.509 certificate and therefore have to be supplied by other means.
engineSetParameter
in class java.security.SignatureSpi
param
- the name of the parameter ("KSEED") or ("DSAParameterSpec")value
- the value of the parameter to be setjava.security.InvalidParameterException
- if the given parameter name is not "KSEED" respectively "DSAParameterSpec"
or the given parameter value is not specified as byte array or BigInteger
respectively DSAParameterSpecInstanceRawDSA.engineSetParameter(java.lang.String, java.lang.Object)
,
Signature.setParameter(java.lang.String, java.lang.Object)
protected void engineSetParameter(java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidAlgorithmParameterException
java.security.spec.DSAParameterSpec
. This method
only can be used with JDK1.2.x. This method may be useful for initializing
the signature verification in situations where there are no
parameters are included in the subjectPublicKeyInfo field of a
X.509 certificate and therefore have to be supplied by other means.engineSetParameter
in class java.security.SignatureSpi
params
- the parameters as instance of java.security.spec.DSAParameterSpecjava.security.InvalidAlgorithmParameterException
- if the given parameters
are not supplied as java.security.spec.DSAParameterSpecprotected java.lang.Object engineGetParameter(java.lang.String param) throws java.security.InvalidParameterException
setParameter()
,
not an internally generated one while signing.
This method only asks the underlying RawDSA for the requested parameter value.engineGetParameter
in class java.security.SignatureSpi
param
- the name of the parameter which value is to be obtained ("KSEED")setParameter
java.security.InvalidParameterException
- if the given parameter name is not "KSEED"Signature.getParameter(java.lang.String)
,
Signature.setParameter(java.lang.String, java.lang.Object)
,
RawDSA.engineGetParameter(java.lang.String)
protected java.security.AlgorithmParameters engineGetParameters()
DSAParameters
object.engineGetParameters
in class java.security.SignatureSpi
DSAParameters