public class RSAOaepParameters extends PKCS1AlgorithmParameters
PKCS#1v2.1 defines the following parameters for the OAEP signature scheme:
RSASES-OAEP-params :: = SEQUENCE { hashAlgorithm [0] HashAlgorithm DEFAULT sha1, maskGenerationAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1, pSourceAlgorithm [2] PSourceAlgorithm DEFAULT pSpecifiedEmpty, } HashAlgorithm ::= Algorithmidentifier { {OAEP-PSSDigestAlgorithms} } MaskGenAlgorithm ::= AlgorithmIdentifier { {PKCS1MGFAlgorithms} } PSourceAlgorithm ::= AlgorithmIdentifier { {PKCS1PSourceAlgorithms} }By default OAEP uses SHA-1 as hash function, MGF1 (with SHA-1 as hash function parameter) as mask generation algorithm, and id-pSpecified as PSourceAlgorithm (with an empty OCTET STRING as parameter).
RSAOaepParameters can be generated provider independently by calling one of
the AlgorithmParameters.getInstance
methods. Subsequently the
new AlgorithmParameters object must be initialized with a proper RSA-OAEP
parameter specification or a DER encoded byte array, e.g.:
RSAOaepParameterSpec oaepParamSpec = ...; AlgorithmParameters params = AlgorithmParameters.getInstance("RSAES-OAEP", "IAIK"); params.init(oaepParamSpec);respectively:
byte[] encodedOaepParams = ...; AlgorithmParameters params = AlgorithmParameters.getInstance("RSAES-OAEP", "IAIK"); params.init(encodedOaepParams);
For obtaining OAEP parameters in transparent representation from an opaque
RSAOaepParameters object, the getParameterSpec
method can be
used; for obtaining the parameters as DER encoded ASN.1 object, use method
getEncoded
:
RSAOaepParameterSpec oaepParamSpec = (RSAOaepParameterSpec) params .getParameterSpec(RSAOaepParameterSpec.class);respectively
byte[] encodedOaepParams = params.getEncoded();
RSAOaepParameterSpec
,
AlgorithmParameters
Constructor and Description |
---|
RSAOaepParameters()
The default constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
decode(ASN1Object asn1Params)
Initializes this RSAOaepParameters object from the given ASN1Object.
|
protected byte[] |
engineGetEncoded()
Returns the parameters as DER byte array.
|
protected byte[] |
engineGetEncoded(java.lang.String format)
Returns the parameters as a DER byte array.
|
protected java.security.spec.AlgorithmParameterSpec |
engineGetParameterSpec(java.lang.Class paramSpec)
Returns the RSA-OAEP parameters as transparent parameter specification of
the given class type.
|
protected void |
engineInit(java.security.spec.AlgorithmParameterSpec paramSpec)
Initializes this RSAOaepParameters from the given RSAOaepParameterSpec.
|
protected void |
engineInit(byte[] params)
Initializes this RSAOaepParameters object from the given DER encoded byte
array.
|
protected void |
engineInit(byte[] params,
java.lang.String format)
Inits the parameters from an DER encoded byte array.
|
protected java.lang.String |
engineToString()
Returns a String representation of this object.
|
static boolean |
getEncodeDefaultValues()
This method shows if parameter values should be encoded even if they have
the default values; e.g.
|
static void |
setEncodeDefaultValues(boolean encode)
With this method, the application can define that parameter values should
be encoded even if they have the default values; e.g.
|
static void |
setUseSHA1ForMGF1WithJCAStandardName(boolean useSHA1)
Sets whether to use SHA-1 for MGF1 if hash algorithm and mask
generation function name are specified in the padding scheme name.
|
ASN1Object |
toASN1Object()
Gets an ASN.1 representation of this RSA OAEP parameters.
|
public RSAOaepParameters()
RSAOaepParameters
object. Applications shall use one of the
AlgorithmParameters.getInstance
factory methods for obtaining
RSAOaepParameters.public static void setEncodeDefaultValues(boolean encode)
Note that the settings given by the RSAOaepParameterSpec
have
higher priority.
Note that only a value of false
results in a fully correct DER
encoding, because DER encoding required default values to be omitted.
encode
- true
to encode default values, false
to
omit default values in the encoding.RSAOaepParameterSpec.setEncodeDefaultValues(Boolean)
,
RSAOaepParameterSpec.getEncodeDefaultValues()
public static boolean getEncodeDefaultValues()
Note that the settings given by the RSAOaepParameterSpec
have
higher priority.
Per default, this is false
.
Note that only a value of false
results in a fully correct DER
encoding, because DER encoding required default values to be omitted.
true
if default values are encoded, false
if default values are omitted.RSAOaepParameterSpec.setEncodeDefaultValues(Boolean)
,
RSAOaepParameterSpec.getEncodeDefaultValues()
public static void setUseSHA1ForMGF1WithJCAStandardName(boolean useSHA1)
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
AlgorithmParameterSpec
object, either as iaik.pkcs.pkcs1.RSAOaepParameterSpec
or as
javax.crypto.spec.OAEPParameterSpec object, e.g.:
-
iaik.pkcs.pkcs1.RSAOaepParameterSpec
:
// hash, mgf and pSource algorithm parameters
AlgorithmID hashID = (AlgorithmID) AlgorithmID.sha256.clone();
AlgorithmID mgfID = (AlgorithmID) AlgorithmID.mgf1.clone();
mgfID.setParameter(hashID.toASN1Object());
AlgorithmID pSourceID = (AlgorithmID) AlgorithmID.pSpecified.clone();
pSourceID.setParameter(new OCTET_STRING());
// create the RSAOaepParameterSpec
RSAOaepParameterSpec oaepParamSpec =
new RSAOaepParameterSpec(hashID, mgfID, pSourceID);
Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPPadding", "IAIK");
PublicKey pubKey = ...;
rsa.init(Cipher.ENCRYPT_MODE, pubKey, oaepParamSpec);
byte[] encrypted = rsa.doFinal(data);
-
javax.crypto.spec.OAEPParameterSpec:
String hashAlg = "SHA-256";
String mgfAlg = "MGF1";
MGF1ParameterSpec mgfParameterSpec = new MGF1ParameterSpec(hashAlg);
PSource.PSpecified pSpecified = PSource.PSpecified.DEFAULT;
OAEPParameterSpec oaepParameterSpec =
new OAEPParameterSpec(hashAlg, mgfAlg, mgfParameterSpec, pSpecified);
Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPPadding", "IAIK");
PublicKey pubKey = ...;
rsa.init(Cipher.ENCRYPT_MODE, pubKey, oaepParamSpec);
byte[] encrypted = rsa.doFinal(data);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding", "IAIK");In any other case (e.g. "OAEPWithSHA-256AndMGF1Padding") decrypting IAIK encrypted data with the SunJCE provider will fail (and vice verca). To solve the problem the parameters have to be explicitly specified at the en- or decryption side, e.g.:
Encryption:
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "IAIK"); PublicKey pubKey = ...; rsa.init(Cipher.ENCRYPT_MODE, pubKey); byte[] encrypted = rsa.doFinal(data);Decryption:
String hashAlg = "SHA-256"; String mgfAlg = "MGF1"; MGF1ParameterSpec mgfParameterSpec = new MGF1ParameterSpec(hashAlg); PSource.PSpecified pSpecified = PSource.PSpecified.DEFAULT; OAEPParameterSpec oaepParameterSpec = new OAEPParameterSpec(hashAlg, mgfAlg, mgfParameterSpec, pSpecified); Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPPadding", "SunJCE"); PrivateKey privKey = ...; rsa.init(Cipher.DECRYPT_MODE, privKey, oaepParamSpec); byte[] decrypted = rsa.doFinal(encrypted);Alternatively you may use this method to globally configure the IAIK provider to anytime use SHA-1 as hash algorithm for the mask generation function regardless of which hash algorithm is specified in the "OAEPWith<digest>And<mgf>Padding" padding scheme name:
RsaOaepParameters.setUseSHA1ForMGF1WithJCAStandardName(true);Of course this configuration setting will have no effect for Ciphers that are explicitly initialized with OAEP parameters.
Note that the problem only arises due to the possibility of specifying hash algorithm and mask generation function name in the padding scheme name. Generally the sender includes these parameters in the OAEP and MGF1 algorithm identifiers and the receiver parses and uses the parameters from the algorithm ids.
useSHA1
- whether to use SHA-1 for MGF1 or to
use the hash algorithm specified in the padding
scheme name for
MGF1, tooprotected byte[] engineGetEncoded() throws java.io.IOException
engineGetEncoded
in class java.security.AlgorithmParametersSpi
java.io.IOException
- if an encoding error occursprotected byte[] engineGetEncoded(java.lang.String format) throws java.io.IOException
Format is ignored. Only DER encoding is supported. This method only calls
engineGetEncoded()
, regardless of what is
specified in the format
string.
engineGetEncoded
in class java.security.AlgorithmParametersSpi
format
- the encoding format; ignoredjava.io.IOException
- if an encoding error occurspublic ASN1Object toASN1Object()
protected java.security.spec.AlgorithmParameterSpec engineGetParameterSpec(java.lang.Class paramSpec) throws java.security.spec.InvalidParameterSpecException
engineGetParameterSpec
in class java.security.AlgorithmParametersSpi
paramSpec
- the desired parameter specification class (RSAOaepParameterSpec)java.security.spec.InvalidParameterSpecException
- if the parameters cannot be converted to the desired parameter
specificationprotected void engineInit(java.security.spec.AlgorithmParameterSpec paramSpec) throws java.security.spec.InvalidParameterSpecException
engineInit
in class PKCS1AlgorithmParameters
paramSpec
- the parameter specification, which has to be a
RSAOaepParameterSpecjava.security.spec.InvalidParameterSpecException
- if the given parameter specification is not a RSAOaepParameterSpec
or OAEPParameterSpec or is invalidprotected void engineInit(byte[] params) throws java.io.IOException
engineInit
in class PKCS1AlgorithmParameters
params
- the DER encoded byte arrayjava.io.IOException
- if an error occurs when decoding the given byte arraypublic void decode(ASN1Object asn1Params) throws CodingException
asn1Params
- the OAEP parameters in ASN.1 representationCodingException
- if an error occurs when parsing the parametersprotected void engineInit(byte[] params, java.lang.String format) throws java.io.IOException
engineInit(params)
for initializing this
RSAOaepParameters object from the given DER encoded byte array, regardless
of what is specified in the format
string.
engineInit
in class java.security.AlgorithmParametersSpi
params
- the DER encoded byte arrayformat
- the encoding format; ignoredjava.io.IOException
- if an error occurs when decoding the given byte arrayprotected java.lang.String engineToString()
engineToString
in class java.security.AlgorithmParametersSpi