public class RSAPssKeyPairGenerator extends RSAKeyPairGenerator implements PssKeyPairGenerator
If no initialize()
method of this key pair generator is called,
it defaults to initialize(2048).
The algorithm used for key generation is according IEEE P1363.
RSASSA-PSS keys (RSAPssPublicKey
,
RSAPssPrivateKey
) differ from an
ordinary PKCS#1 RSA keys (RSAPublicKey
, RSAPrivateKey
) only in that
RSASSA-PSS keys may contain PSS parameters
. If RSASSA-PSS keys do not contain parameters they may be used
for PSS based siganture calcualtion/verification with any PSS parameters.
However, if RSASSA-PSS keys contain PSS parameters they must be only used
with the hash algorithm, mask generation function and trailer field that are
specified by their parameters (see RFC 4055):
RSASSA-PSS-params ::= SEQUENCE { hashFunc [0] AlgorithmIdentifier DEFAULT sha1Identifier, maskGenFunc [1] AlgorithmIdentifier DEFAULT mgf1SHA1Identifier, saltLength [2] INTEGER DEFAULT 20, trailerField [3] INTEGER DEFAULT 1 }
An application wishing to create a RSASSA-PSS key pair to be used for PSS
based signature calcualtion/verification with the RSA algorithm, uses a
proper getInstance
method of the
java.security.KeyPairGenerator
class, which subsequently maybe
casted to RSAPssKeyPairGenerator
for performing an algorithm-specific initialization
with proper RSASSA-PSS parameters. If an algorithm-specific initialization is
not required, the cast to RSAPssKeyPairGenerator
can be omitted
(in this case no parameters will be included in the RSA-PSS generated keys
and they maybe used with any PSS parameters).
Generally four steps have to be performed for creating a RSAPssPrivateKey by using a proper KeyPairGenerator:
KeyPairGenerator
has to be instantiated thereby
specifying "RSASSA-PSS" as algorithm name: KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSASSA-PSS", "IAIK");
initialize
method. For initializing the generator to
create keys with a modulus length of, e.g., 2048 bits, this can be explicitly
specified (when not initializing the generator explicitly, per default the
modulus length is set to 2048 bits): keyGen.initialize(2048);
generateKeyPair()
:
KeyPair keyPair = keyGen.generateKeyPair();
RSAPssPrivateKey privateKey = (RSAPssPrivateKey) keyPair.getPrivate(); RSAPssPublicKey publicKey = (RSAPssPublicKey) keyPair.getPublic();
For performing an algorithm-specific initialization with particular RSA PSS
parameters an explicit cast of the KeyPairGenerator may be necessary for
obtaining a specific RSAPssKeyPairGenerator
to be initialized with the desired RSA-PSS parameters
which have to be supplied as RSAPssParameterSpec
object, e.g.:
RSAPssKeyPairGenerator rsaPsskeyGen = (RSAPssKeyPairGenerator) keyGen; // create PSS parameters for specifying hash, mgf algorithms and salt length: // hash and mgf algorithm ids AlgorithmID hashID = (AlgorithmID) AlgorithmID.sha512.clone(); AlgorithmID mgfID = (AlgorithmID) AlgorithmID.mgf1.clone(); mgfID.setParameter(hashID.toASN1Object()); int saltLength = 64; // hash and mgf engines MessageDigest hashEngine = hashID.getMessageDigestInstance(); MaskGenerationAlgorithm mgfEngine = mgfID.getMaskGenerationAlgorithmInstance(); MGF1ParameterSpec mgf1ParamSpec = new MGF1ParameterSpec(hashID); mgf1ParamSpec.setHashEngine(hashEngine); mgfEngine.setParameters(mgf1ParamSpec); // create the RSAPssParameterSpec RSAPssParameterSpec pssParamSpec = new RSAPssParameterSpec(hashID, mgfID, saltLength); // set engines pssParamSpec.setHashEngine(hashEngine); pssParamSpec.setMGFEngine(mgfEngine); // initialize key pair generator rsaPsskeyGen.initialize(2048, pssParamSpec); KeyPair keyPair = rsaPsskeyGen.generateKeyPair(); RSAPssPublicKey publicKey = (RSAPssPublicKey) keyPair.getPublic(); RSAPssPrivateKey privateKey = (RSAPssPrivateKey) keyPair.getPrivate();
RSASSA-PSS keys must be only used for signature purposes with the RSASSA-PSS
signature scheme. For using RSASSA-PSS keys with a Signature
engine, "RSASSA-PSS" has to be specified as algorithm name when instantiating
the Signature
object:
Signature rsaPss = Signature.getInstance("RSASSA-PSS", "IAIK");When now initializing the Signature engine with an RSASSA-PSS key that contains PSS parameters, the hash algorithm, mask generation function, trailer field and salt (length) parameters are taken from the key parameters:
rsaPss.initSign(privateKey); // the data to be signed: byte[] data = ...; // sign data rsaPss.update(data); byte[] signature = rsaPss.sign();For verifying the signature you will have to use the right RSASSA-PSS public key}:
Signature rsaPss = Signature.getInstance("RSASSA-PSS", "IAIK"); rsaPss.initVerify(publicKey); // verify signature rsaPss.update(data); System.out.println("Signature " + (rsaPss.verify(signature) ? "correct!" : "not correct!"));
RSAPssPublicKey
,
RSAPssPrivateKey
,
RSAPssKeyFactory
,
RSAPssParameterSpec
,
KeyPairGenerator
,
KeyPair
initialized, keylen, public_exponent, random
Constructor and Description |
---|
RSAPssKeyPairGenerator()
Default constructor for creating a RSAPssKeyPairGenerator object.
|
Modifier and Type | Method and Description |
---|---|
void |
initialize(java.security.spec.AlgorithmParameterSpec params)
Initializes this RSAPssKeyPairGenerator with the given AlgorithmParameterSpec.
|
void |
initialize(java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random)
Initializes this RSAPssKeyPairGenerator with given RSAPssParameterSpec and
random number generator.
|
void |
initialize(int strength,
java.security.spec.AlgorithmParameterSpec params)
Initializes the RSAPssKeyPairGenerator for generating keys with the given
length and PSS parameters.
|
void |
initialize(int strength,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom secureRandom)
Initializes the RSAPssKeyPairGenerator for a certain key length with the given
random number generator and PSS parameters.
|
void |
initialize(int strength,
java.math.BigInteger publicExponent,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom secureRandom)
Initializes the key pair generator using the specified "strength" (desired
key length in bits), public exponent, source of random bits, and PSS
parameters.
|
generateKeyPair, initialize, initialize, initialize
public RSAPssKeyPairGenerator()
KeyPairGenerator.getInstance
method:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSASSA-PSS", "IAIK");
KeyPairGenerator
public void initialize(java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidAlgorithmParameterException
initialize
in interface PssKeyPairGenerator
initialize
in class RSAKeyPairGenerator
params
- the RSAPssParameterSpec for initializing this generatorjava.security.InvalidAlgorithmParameterException
- if the given parameter specification is not a
RSAPssParameterSpec or
java.security.spec.RSAKeyGenParameterSpec or the
KeyPairGenerator cannot be initialized from the parameterspublic void initialize(java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random) throws java.security.InvalidAlgorithmParameterException
initialize
in interface PssKeyPairGenerator
initialize
in class RSAKeyPairGenerator
params
- the RSAPssParameterSpec for initializing this generatorrandom
- the SecureRandom for generating random numbersjava.security.InvalidAlgorithmParameterException
- if the given parameter specification is not a
RSAPssParameterSpec or
java.security.spec.RSAKeyGenParameterSpec or the
KeyPairGenerator cannot be initialized from the parameterspublic void initialize(int strength, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidParameterException
RSAPssKeyPairGenerator
is required:
RSAPssKeyPairGenerator keyGen = (RSAPssKeyPairGenerator)KeyPairGenerator.getInstance("RSASSA-PSS", "IAIK"); keyGen.initialize(strength, pssParamSpec); ...
initialize
in interface PssKeyPairGenerator
strength
- the length of the key in bits.params
- the PSS parameters to be set for the RSASSA-PSS keysjava.security.InvalidParameterException
- if the supplied parameters do not represent PSS
parameters
or
java.security.spec.RSAKeyGenParameterSpec or the
KeyPairGenerator cannot be initialized from the parameterspublic void initialize(int strength, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom secureRandom) throws java.security.InvalidParameterException
RSAPssKeyPairGenerator
is required:
RSAPssKeyPairGenerator keyGen = (RSAPssKeyPairGenerator)KeyPairGenerator.getInstance("RSASSA-PSS", "IAIK"); keyGen.initialize(strength, pssParamSpec, secureRandom); ...
initialize
in interface PssKeyPairGenerator
strength
- the length of the key in bits.params
- the PSS parameters to be set for the RSASSA-PSS keyssecureRandom
- the random number generatorjava.security.InvalidParameterException
- if the supplied parameters do not represent PSS
parameters
or
java.security.spec.RSAKeyGenParameterSpec or the
KeyPairGenerator cannot be initialized from the parameterspublic void initialize(int strength, java.math.BigInteger publicExponent, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom secureRandom) throws java.security.InvalidParameterException
RSAPssKeyPairGenerator
is required:
RSAPssKeyPairGenerator keyGen = (RSAPssKeyPairGenerator)KeyPairGenerator.getInstance("RSASSA-PSS", "IAIK"); keyGen.initialize(strength, publicExponent, pssParamSpec, secureRandom); ...
initialize
in interface PssKeyPairGenerator
strength
- keyLength the length of the key in bits.publicExponent
- the public exponentparams
- the PSS parameters to be set for the RSASSA-PSS keyssecureRandom
- the random seedjava.security.InvalidParameterException
- if the supplied parameters do not represent PSS
parameters
or
java.security.spec.RSAKeyGenParameterSpec or the
KeyPairGenerator cannot be initialized from the parameters