public class RSAPssKeyPairGeneratorFIPS extends RSAKeyPairGeneratorFIPS implements PssKeyPairGenerator
The modulus of the generated KeyPair always has either 1024, 2048 or 3072
bits. If no initialize()
method of this key pair generator is
called, it defaults to initialize(2048).
The algorithm used for key generation was published in FIPS 186-3.
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 RSAPssKeyPairGeneratorFIPS
for performing an algorithm-specific
initialization with proper RSASSA-PSS parameters. If an algorithm-specific
initialization is not required, the cast to
RSAPssKeyPairGeneratorFIPS
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-FIPS-186-3" as algorithm name: KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSASSA-PSS-FIPS-186-3", "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 RSAPssKeyPairGeneratorFIPS
to be initialized with the desired RSA-PSS
parameters which have to be supplied as
RSAPssParameterSpec
object, e.g.:
RSAPssKeyPairGeneratorFIPS rsaPsskeyGen = (RSAPssKeyPairGeneratorFIPS) 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-FIPS-186-3" has to be specified as algorithm name when
instantiating the Signature
object:
Signature rsaPss = Signature.getInstance("RSASSA-PSS-FIPS-186-3", "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-FIPS-186-3", "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
KEYLENGTH_1024, KEYLENGTH_2048, KEYLENGTH_3072, LOWER_PUBLIC_EXPONENT_BOUND, UPPER_PUBLIC_EXPONENT_BOUND
initialized, keylen, public_exponent, random
Constructor and Description |
---|
RSAPssKeyPairGeneratorFIPS()
Default constructor for creating a RSAPssKeyPairGeneratorFIPS object.
|
Modifier and Type | Method and Description |
---|---|
void |
initialize(java.security.spec.AlgorithmParameterSpec params)
Initializes this RSAPssKeyPairGeneratorFIPS with the given
RSAPssParameterSpec.
|
void |
initialize(java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random)
Initializes this RSAPssKeyPairGeneratorFIPS with given RSAPssParameterSpec
and random number generator.
|
void |
initialize(int strength,
java.security.spec.AlgorithmParameterSpec params)
Initializes the RSAKeyPairGenerator 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 RSAKeyPairGenerator 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 RSAPssKeyPairGeneratorFIPS()
KeyPairGenerator.getInstance
method:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSASSA-PSS-FIPS-186-3", "IAIK");
KeyPairGenerator
public void initialize(java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidAlgorithmParameterException
initialize
methods may be called during the initialization
process, e.g.:
int strength = ...; RSAPssParameterSpec pssParamSpec = ...; KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSASSA-PSS-FIPS-186-3", "IAIK"); keyGen.initialize(strength); keyGen.initialize(pssParamSpec); ...
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
RSAPssParameterSpecpublic void initialize(java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random) throws java.security.InvalidAlgorithmParameterException
initialize
methods may be called during the initialization
process, e.g.:
int strength = ...; RSAPssParameterSpec pssParamSpec = ...; SecureRandom secureRandom = ...; KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSASSA-PSS-FIPS-186-3", "IAIK"); keyGen.initialize(strength); keyGen.initialize(pssParamSpec, secureRandom); ...
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
RSAPssParameterSpecpublic void initialize(int strength, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidParameterException
KeyPairGenerator
to
RSAPssKeyPairGeneratorFIPS
is required:
RSAPssKeyPairGeneratorFIPS keyGen = (RSAPssKeyPairGeneratorFIPS)KeyPairGenerator.getInstance("RSASSA-PSS-FIPS-186-3", "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
public void initialize(int strength, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom secureRandom) throws java.security.InvalidParameterException
KeyPairGenerator
to
RSAPssKeyPairGeneratorFIPS
is required:
RSAPssKeyPairGeneratorFIPS keyGen = (RSAPssKeyPairGeneratorFIPS)KeyPairGenerator.getInstance("RSASSA-PSS-FIPS-186-3", "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
public void initialize(int strength, java.math.BigInteger publicExponent, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom secureRandom) throws java.security.InvalidParameterException
KeyPairGenerator
to RSAPssKeyPairGeneratorFIPS
is required:
RSAPssKeyPairGeneratorFIPS keyGen = (RSAPssKeyPairGeneratorFIPS)KeyPairGenerator.getInstance("RSASSA-PSS-FIPS-186-3", "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