public class RawRSAPssSignature extends RSAPssSignature
This class may be used in a similar way than parent class RSAPssSignature
except that the message
hash value has to be calculated by the application. All required parameters
(hash algorithm, mask generation algorithm, salt length) have to be supplied
as RSAPssParameterSpec
. As defined
for PSS the trailer field is fixed to 0xBC.
Generally the following steps have to be performed for calculating/verifying a PSS signature when using this "raw" signature engine:
getInstance
method, e.g.:
Signature pss = Signature.getInstance("RawRSASSA-PSS");
pss.initSign(rsaPrivateKey);
pss.initVerify(rsaPublicKey);
RSAPssParameterSpec
, e.g.:
// 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 pss.setParameter(pssParamSpec);
update
method
and subsequently the signature is created by calling the sign
method
returning the signature as byte array. Otherwise, if the Signature object has been
initialized for verifying, first again the message hash value is supplied by calling
an update
method 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:
MessageDigest hashEngine = hashID.getMessageDigestInstance(); byte[] rawHash = hashEngine.digest(data); pss.update(rawHash);
byte[] signature = pss.sign();
System.out.println("Signature " + (pss.verify(signature) ? "correct!" : "not correct!"));
Please note that it is the entire responsibility of the application to take care to
provide a proper hash value when calling an update
method; no check is
performed if the supplied hash value corresponds to the hash algorithm
in use (e.g. has the correct length).
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.RSAPssSignature
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.
|
Constructor and Description |
---|
RawRSAPssSignature()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
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.lang.String param,
java.lang.Object value)
Allows to supply a SecureRandom object if required by the underlying signature scheme
(e.g.
|
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.
|
engineGetParameter, engineGetParameters, engineSetParameter, engineSign, engineVerify, setValidateAgainstPssKeyParameters
protected java.security.MessageDigest hash
public RawRSAPssSignature()
An application shall call
Signature.getInstance("RawRSASSA-PSS");to get the required raw PSS signature engine.
protected void engineUpdate(byte b)
b
- the byte to be used for updating.protected void engineUpdate(byte[] b, int off, int len)
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 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.engineInitSign
in class RSAPssSignature
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.engineInitVerify
in class RSAPssSignature
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 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 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