public class RsaKem extends KeyEncapsulationMechanismSpi
This class implements the RSA-KEM through the KeyEncapsulationMechanism (KEM) interface. For an explanation on the workflow of a KEM, see KeyEncapsulationMechanism.
An application may use this class to perform the necessary steps for the RSA-KEM. The RSA-KEM, given e.g., in rfc 5990 [1], or ISO/IEC 18033-2, is based on the RSA cryptosystem.
Two parties can use the RSA-KEM to transmit keying material in a confidential way. The sender has to perform the following steps:
c = z^e mod n
The random octet string k may be used as key to encrypt any arbitrary data. The sender then sends c (along with any encrypted data) to the receiver, where they are able to decrypt c, retrieve z, and re-compute k.
Virtually any strong enough Key Derivation Function may be used to the derive the k. This implementation supports the following Key Derivation Functions:
To obtain an instance of the RSA-KEM and configure it with the KDF3, using SHA2-256, perform the following steps:
KeyEncapsulationMechanism kemEncap = KeyEncapsulationMechanism.getInstance("RsaKem", "IAIK"); final int sessionKeyLength = ...; RsaKemAlgorithmParameterSpec kemSpec = new RsaKemAlgorithmParameterSpec( new KDF3ParameterSpec(AlgorithmID.sha256, sessionKeyLength)); kem.init(rsaPub, kemSpec); byte[] sessionKeyEncap = new byte[sessionKeyLength]; byte[] cipher = rsaKem.encapsulate(sessionKeyEncap);Note that the methods engineInit(Key) and engineInit(Key, SecureRandom) will throw an UnsupportedOperationException, because the implementation has to have access to the Algorithm Parameters, to derive the desired session key length and the KDF.
key
Constructor and Description |
---|
RsaKem() |
Modifier and Type | Method and Description |
---|---|
protected void |
engineDecapsule(byte[] c,
byte[] k)
Performs the
decapsule operation of the RSA-KEM (the receiver's side) as follows:
Decrypt the integer z with the receiver's RSA private key: |
protected void |
engineDeriveKey(byte[] output,
java.util.List input)
Derives the key according to the transformation.
|
protected byte[] |
engineEncapsule(byte[] k)
Performs the
encapsule operation of the RSA-KEM (the sender's side) as follows:
Generate a random integer z between 0 and n-1, where n is the RSA modulus of the public key.
Encrypt the integer z with the receiver's RSA public key: |
protected java.security.AlgorithmParameters |
engineGetParameters()
SPI: Gets the algorithm parameters required by this KEM engine.
|
protected void |
engineInit(java.security.Key key)
Throws an UnsupportedOperationException
|
protected void |
engineInit(java.security.Key key,
java.security.spec.AlgorithmParameterSpec spec)
Initializes the KEM instance accordingly.
|
protected void |
engineInit(java.security.Key key,
java.security.spec.AlgorithmParameterSpec spec,
java.security.SecureRandom secureRandom)
Initializes the KEM instance accordingly.
|
protected void |
engineInit(java.security.Key key,
java.security.SecureRandom secureRandom)
Throws an UnsupportedOperationException
|
protected void engineInit(java.security.Key key) throws java.security.InvalidKeyException
engineInit
in class KeyEncapsulationMechanismSpi
key
- either a PublicKey or a PrivateKey depending on the wanted use-case.java.security.InvalidKeyException
- if the given key is inappropriate for initializing this KEM,
or requires algorithm parameters that cannot be determined from the given key,protected void engineInit(java.security.Key key, java.security.SecureRandom secureRandom) throws java.security.InvalidKeyException
engineInit
in class KeyEncapsulationMechanismSpi
key
- either a PublicKey or a PrivateKey depending on the wanted use-case.secureRandom
- a secure source of randomjava.security.InvalidKeyException
- if the given key is inappropriate for initializing this KEM,
or requires algorithm parameters that cannot be determined from the given key,protected void engineInit(java.security.Key key, java.security.spec.AlgorithmParameterSpec spec) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
engineInit
in class KeyEncapsulationMechanismSpi
key
- either a PublicKey or a PrivateKey
depending on the wanted use-case.spec
- a set of algorithm parameters able to initialize the KEMjava.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
protected void engineInit(java.security.Key key, java.security.spec.AlgorithmParameterSpec spec, java.security.SecureRandom secureRandom) throws java.security.InvalidKeyException, java.security.InvalidAlgorithmParameterException
engineInit
in class KeyEncapsulationMechanismSpi
key
- either a PublicKey or a PrivateKey
depending on the wanted use-case.spec
- a set of algorithm parameters able to initialize the KEMsecureRandom
- a secure source of randomjava.security.InvalidKeyException
java.security.InvalidAlgorithmParameterException
protected java.security.AlgorithmParameters engineGetParameters()
KeyEncapsulationMechanismSpi
engineGetParameters
in class KeyEncapsulationMechanismSpi
protected void engineDeriveKey(byte[] output, java.util.List input) throws java.security.DigestException
engineDeriveKey
in class KeyEncapsulationMechanismSpi
output
- a large enough byte array depending on used kdfinput
- the input to the key derivation functionjava.security.DigestException
- if the byte array has not the correct sizeprotected byte[] engineEncapsule(byte[] k) throws java.security.InvalidKeyException, java.security.DigestException
encapsule
operation of the RSA-KEM (the sender's side) as follows:
c = z^e mod n
engineEncapsule
in class KeyEncapsulationMechanismSpi
k
- the destination of the session key k
java.security.InvalidKeyException
- if a wrong key is usedjava.security.DigestException
- if the byte array k has not the correct size for the used transformationjava.lang.IllegalStateException
- if the caller provided a wrong key, e.g., a private keyKeyEncapsulationMechanismSpi.engineDecapsule(byte[], byte[])
protected void engineDecapsule(byte[] c, byte[] k) throws java.security.InvalidKeyException, java.security.DigestException, java.security.InvalidParameterException
decapsule
operation of the RSA-KEM (the receiver's side) as follows:
z = c^d mod n
engineDecapsule
in class KeyEncapsulationMechanismSpi
c
- the ciphertext obtained from encap
k
- the destination of the session key k
java.security.InvalidKeyException
- if a wrong key is usedjava.security.DigestException
- if the byte array k has not the correct size for the used transformationjava.lang.IllegalStateException
- if the caller provided a wrong key, e.g., a private keyjava.security.InvalidParameterException