public class RSAPssSignature
extends java.security.SignatureSpi
This class follows the guidelines presented in PKCS#1 (v.2.1)) for implementing the RSASSA-PSS signature algorithm based on the RSA encryption method.
This class may be used to create/verify PSS based signatures according the specification where all parameters (hash algorithm, mask generation function, salt length, and trailer field) maybe supplied by the calling application. The trailer field is fixed to the 0xBC which is the only trailer field supported by PSS. Generally the following steps have to be performed for calculating/verifying a PSS signature:
getInstance
method, e.g.:
Signature pss = Signature.getInstance("RSASSA-PSS");
pss.initSign(rsaPrivateKey);
pss.initVerify(rsaPublicKey);
RSAPssParameterSpec
(default: SHA-1, MGF1, 20), e.g.:
If RSASSA-PSS keys (// hash algorithm AlgorithmID hashID = (AlgorithmID)AlgorithmID.ripeMd160.clone(); // mask generation function ID AlgorithmID mgfID = (AlgorithmID)AlgorithmID.mgf1.clone(); mgfID.setParameter(hashID.toASN1Object()); // salt length int saltLength = 20; // create a RSAPssParameterSpec RSAPssParameterSpec pssParamSpec = new RSAPssParameterSpec(hashID, mgfID, saltLength); // optionally set hash and mgf engines MessageDigest hashEngine = MessageDigest.getInstance("SHA-1"); pssParamSpec.setHashEngine(hashEngine); MaskGenerationAlgorithm mgfEngine = MaskGenerationAlgorithm.getInstance("MGF1"); MGF1ParameterSpec mgf1Spec = new MGF1ParameterSpec(hashID); mgf1Spec.setHashEngine(hashEngine); mgfEngine.setParameters(mgf1Spec); pssParamSpec.setMGFEngine(mgfEngine); // set parameters (for JDK 1.1.8 use pss.setParameter(null, pssParamSpec);) pss.setParameter(pssParamSpec);
RSAPssPrivateKey
,
RSAPssPublicKey
) are used for initializing this
Signature engine, they may contain PSS parameters
. In this case the parameters are taken from the keys,
except for the application explicitly specifies parameters by calling
method setParameter
. If parameters are specified by
calling method setParameter
they may have to be validated
against the parameters contained in the key (according to RFC 4055, the Signature engine
must be only used with the hash algorithm, mask generation function and
trailer field parameters that are specified in the key). By default no
such validation is performed since it may be the responsibility
of the application to take care for proper parameters (or the
application may wish to verify a signature even if parameters
are used that differ from the key parameters). Parameter validation
can be turned on/off by calling static method setValidateAgainstPssKeyParameters
.
sign
method returning the signature as 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 byte array holding the corresponding signature value:
pss.update(data); byte[] signature = pss.sign();
pss.update(data); System.out.println("Signature " + (pss.verify(signature) ? "correct!" : "not correct!"));
Please note that the Java Cryptography Architecture only allows to set the salt length
as parameter and specifies all the other parameters by the algorithm standard name to be implemented by a
corresponding PSS signature engine: A signature engine that implements the, for instance,
"SHA1withRSAandMGF1" (in general: <digest>with<encryption>and<mgf>) PSS signature
algorithm has to use SHA-1 as hash- and MGF1 as mask generation algorithm. The trailer field
(0xBC) is fixed by the PKCS#1v2.1 standard and the salt length may be supplied as
parameter; if not, a default salt length (20 for the SHA-1 hash algorithm) will be used.
IAIK-JCE implements JCA PSS based signature engine for all hash algorithms supported
by the IAIK provider. The only mask generation in use is MGF1 as specified by
PKCS#1 (v.2.1). For
creating an instance of the PSS based signature engine call Signature.getInstance
thereby using the JCA PSS naming convention (<digest>with<encryption>and<mgf>), e.g.:
Signature shaRsaMgf1 = Signature.getInstance("SHA1withRSAandMGF1", "IAIK");The only parameter allowed to be set for such an JCA engine is the salt length. Because of the JDK1.1.x compatibility of IAIK-JCE there is no proper way to use the
java.security.spec.PSSParameterSpec
class for modeling the saltLength
parameter. The same functionality is provided by class RSAPssSaltParameterSpec
which may be used to supply the saltLength to this PSS based
signature engine; if no salt length is explicitly supplied, the defined default salt length
for the underlying signature engine will be used.
SHA1withRSAandMGF1
, for instance, uses
the SHA-1 algorithm for both message hashing and MGF1 hash operations. When using this general
RSASSA-PSS signature engine, hash algorithm and mask generation function have to be supplied
by the calling application. In this case it is the responsibility of the application to take
care of hash function substitution issues - if desired. This may be done by, for instance, setting
the same hash algorithm parameter for message hashing and MGF hashing, or, for instance, using
one and only hash algorithm in any case, or following any other suitable strategy.SHAwithRSAandMGF1Signature
Modifier and Type | Field and Description |
---|---|
protected java.security.MessageDigest |
hash
The MessageDigest engine used to hash the data; supplied with an instance of the desired MessageDigest
algorithm by any extending subclass.
|
Modifier | Constructor and Description |
---|---|
|
RSAPssSignature()
Default constructor.
|
protected |
RSAPssSignature(java.lang.String name)
Creates a RSAPssSignature engine with the given name.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
engineGetParameter(java.lang.String param)
Returns the PSS parameters (hashAlgorithm, mask generation algorithm, salt length, trailer field)
as
RSAPssParameterSpec . |
protected java.security.AlgorithmParameters |
engineGetParameters()
Returns the PSS parameters (hashAlgorithm, mask generation algorithm, salt length, trailer field)
as
RSAPssParameterSpec . |
protected void |
engineInitSign(java.security.PrivateKey pk)
SPI: Initializes this Signature object with the given
RSA private key for going to sign some data.
|
protected void |
engineInitSign(java.security.PrivateKey pk,
java.security.SecureRandom random)
SPI: Initializes this Signature object with the given
RSA private key for going to sign some data.
|
protected void |
engineInitVerify(java.security.PublicKey pk)
SPI: Initializes this Signature object with the given
RSA public key for performing a signature verification.
|
protected void |
engineSetParameter(java.security.spec.AlgorithmParameterSpec params)
Sets the parameters (hashAlgorithm, mask generation algorithm, salt length, trailer field)
for this RSA PSS signature engine.
|
protected void |
engineSetParameter(java.lang.String param,
java.lang.Object value)
Allows to supply a SecureRandom object if required by the underlying signature scheme
(e.g.
|
protected byte[] |
engineSign()
SPI: Calculates the signature.
|
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)
Verifies the given signature.
|
static void |
setValidateAgainstPssKeyParameters(boolean validate)
Decides whether parameters that are supplied by calling method
setParameter shall be validated against parameters
that maybe contained in RSASSA-PSS keys. |
protected java.security.MessageDigest hash
public RSAPssSignature()
Signature.getInstance("RSASSA-PSS");to create a JCA Signature engine for the RSASSA-PSS signature scheme.
Default salt length is set to -1.
protected RSAPssSignature(java.lang.String name)
SHAwithRSAandMGF1
.
Default salt length is set to -1.
name
- the name of the enginepublic static void setValidateAgainstPssKeyParameters(boolean validate)
setParameter
shall be validated against parameters
that maybe contained in RSASSA-PSS keys. If RSASSA-PSS keys are
used for initializing this Signature engine, they may contain PSS
parameters
. In this
case, according to
RFC 4055, this Signature engine must be only used with the
hash algorithm, mask generation function and trailer field parameters
that are specified in the key. However, an application
may want to use method setParameter
to specify
the parameters for this Signature engine. When doing
so it may be required to validate the given parameters (hash
algorithm, mask generation function, trailer field) against
the parameters contained in the RSA-PSS key. By default no
such validation is performed since it may be the responsibility
of the application to take care for proper parameters (or the
application may wish to verify a signature even if parameters
are used that differ from the key parameters).
RSAPssSignature.setValidateAgainstPssKeyParameters(true);
validate
- whether to validate against key parameters or notprotected void engineInitSign(java.security.PrivateKey pk) throws java.security.InvalidKeyException
RSAPssPrivateKey
that contains parameters
, these key parameters (hash algorithm, mask generation function,
salt (length), trailer field) are used for initializing this
Signature engine.pk
- the RSA private key to be used for signing.java.security.InvalidKeyException
- if a key encoding error occurs or the
given key is a RSAPssPrivateKey
with invalid PSS parameters,
or the application has set parameters by calling
setParameter
before initialization
and these parameters cannot be
validated
against the parameters contained in the PSS keyprotected void engineInitVerify(java.security.PublicKey pk) throws java.security.InvalidKeyException
RSAPssPublicKey
that contains parameters
, these key parameters (hash algorithm, mask generation function,
salt (length), trailer field) are used for initializing this
Signature engine.pk
- the RSA public key belonging to the RSA private key that has been used for signing.java.security.InvalidKeyException
- if a key encoding error occurs
or the given key is a RSAPssPublicKey
with invalid PSS parameters,
or the application has set parameters by calling
setParameter
before initialization
and these parameters cannot be
validated
against the parameters contained in the PSS keyprotected void engineSetParameter(java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidAlgorithmParameterException
PKCS#1 (v.2.1) requires the following parameters for the RSA PSS signature scheme:
RSASSA-PSS-params :: = SEQUENCE { hashAlgorithm [0] HashAlgorithm DEFAULT sha1, maskGenerationAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1, saltLength [2] INTEGER DEFAULT 20, trailerField [3] TrailerField DEFAULT trailerFieldBC } HashAlgorithm ::= Algorithmidentifier { {OAEP-PSSDigestAlgorithms} } MaskGenAlgorithm ::= AlgorithmIdentifier { {PKCS1MGFAlgorithms} } TrailerField ::= INTEGER { trailerFieldBC(1) }This class does not use any default parameters. All parameters have to be supplied as
RSAPssParameterSpec
; the trailer field has
to be set to 1 which corresponds to the trailer field byte 0xBC, which is the only
trailer field supported by the RSA-PSS signature scheme.
An application also may use the RSAPssParameterSpec
to provide a SecureRandom object for supplying any random numbers as required by the
PSS signature algorithm. JDK 1.2 (or later) based applications may prefer to use method
initSign(PrivateKey, SecureRandom)
to supply a SecureRandom object if required.
If a SecureRandom never has been supplied by the application, the signature engine will use
a default SecureRandom for generating random numbers.
params
- the PSS parameters supplied as RSAPssParameterSpecjava.security.InvalidParameterException
- if the parameters are not supplied as RSAPssParameterSpec, the
trailer field is invalid (not 1), any of the required hash
or mgf engines is not available, or a PSS-key is used with
this engine and that the given parameters cannot be
validated
against
the parameters contained in the keyjava.security.InvalidAlgorithmParameterException
protected java.lang.Object engineGetParameter(java.lang.String param) throws java.security.InvalidParameterException
RSAPssParameterSpec
.param
- ignoredRSAPssParameterSpec
.java.security.InvalidParameterException
- should not occurprotected java.security.AlgorithmParameters engineGetParameters()
RSAPssParameterSpec
.engineGetParameters
in class java.security.SignatureSpi
RSAPssParameterSpec
.protected byte[] engineSign() throws java.security.SignatureException
engineSign
in class java.security.SignatureSpi
java.security.SignatureException
- if an error occurs when creating the signatureprotected boolean engineVerify(byte[] sigBytes) throws java.security.SignatureException
engineVerify
in class java.security.SignatureSpi
sigBytes
- the signature bytes to be verifiedtrue
if signature is OK, false
otherwisejava.security.SignatureException
- if an error occurs when verifying the signatureprotected void engineInitSign(java.security.PrivateKey pk, java.security.SecureRandom random) throws java.security.InvalidKeyException
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
pk
- the RSA private key to be used for signing.random
- the SecureRandom if random numbers are required by the signature engine (e.g. PSS)java.security.InvalidKeyException
- if a key encoding error occursprotected void engineUpdate(byte b)
engineUpdate
in class java.security.SignatureSpi
b
- the byte to be used for updating.protected void engineUpdate(byte[] b, int off, int len)
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.protected void engineSetParameter(java.lang.String param, java.lang.Object value) throws java.security.InvalidParameterException
initSign(PrivateKey, SecureRandom)
is not available. If required by the underlying signature scheme (e.g. PSS) an
application may supply a SecureRandom object as parameter
.
If a SecureRandom never has been supplied by the application, the signature engine will use
a default SecureRandom, if required.engineSetParameter
in class java.security.SignatureSpi
param
- ignoredvalue
- the SecureRandom supplied as PKCS1AlgorithmParameterSpecjava.security.InvalidParameterException
- if the SecureRandom is not supplied as PKCS1AlgorithmParameterSpec