public abstract class MaskGenerationAlgorithm extends MaskGenerationAlgorithmSpi
Mask generation functions are used by the PKCS#1v2.1 PSS signature and OAEP encryption schemes:
A mask generation function takes an octet string of variable length and a desired output length as input, and outputs an octet string of the desired length. There may be restrictions on the length of the input and output octet strings, but such bounds are generally very large. Mask generation functions are deterministic; the octet string output is completely determined by the input octet string. The output of a mask generation function should be pseudo-random: Given one part of the output but not the input, it should be infeasible to predict another part of the output. The provable security of RSAES- OAEP and RSASSA-PSS relies on the random nature of the output of the mask generation function, which in turn relies on the random nature of the underlying hash.This class implements both interfaces of the JCA, the API as used by the application and the SPI (
engine
methods) as implemented by the
provider.
An application wishing to use some specific mask generation function, first tries to get an engine instance for the desired algorithm (e.g. MGF1):
MaskGenerationAlgorithm mgf = MaskGenerationAlgorithm.getInstance("MGF1", "IAIK");After supplying the engine with parameters (if required), method
mask
has to be called to "mask" the input data by XORing it
with the mask generated from some seed by using the mask generation
algorithm. For example, the only mask generation function specified by
PKCS#1, MGF1
, is based on a hash algorithm which
may be supplied as MGF1ParameterSpec
:
AlgorithmID hashAlgID = ...; MGF1ParameterSpec paramSpec = new MGF1ParameterSpec(hashAlgID); // optionally set the hash engine to be used: MessageDigest hashEngine = ...; paramSpec.setHashEngine(hashEngine); // set the parameters mgf.setParameters(paramSpec); // mask the data byte[] data = ...; byte[] dataOff = ...; byte[] seed = ...; int seedOff = ...; int seedLen = ...; int maskLen = ...; mgf.mask(seed, seedOff, seedLen, maskLen, data, dataOff);
Modifier | Constructor and Description |
---|---|
protected |
MaskGenerationAlgorithm(java.lang.String algorithm)
Creates a MaskGenerationAlgorithm with the given algorithm name.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getAlgorithm()
Gets the name of the mask generation algorithm this engine implements.
|
static MaskGenerationAlgorithm |
getInstance(java.lang.String algorithm)
Creates an MaskGenerationAlgorithm engine for the given algorithm name.
|
static MaskGenerationAlgorithm |
getInstance(java.lang.String algorithm,
java.security.Provider provider)
Creates a provider-specific MaskGenerationAlgorithm engine for the given
algorithm name.
|
static MaskGenerationAlgorithm |
getInstance(java.lang.String algorithm,
java.lang.String providerName)
Creates a provider-specific MaskGenerationAlgorithm engine for the given
algorithm name.
|
java.security.AlgorithmParameters |
getParameters()
Gets the algorithm parameters required by this MGF engine.
|
void |
mask(byte[] mgfSeed,
int mgfSeedOff,
int mgfSeedLen,
int maskLen,
byte[] dst,
int dstOff)
Uses the mask generation function to generate maskLen bytes from the
supplied seed mgfSeed[mgfSeedOff:mgfSeedLen] and XORs the result with
dst[off:maskLen].
|
void |
reset()
Resets this engine to the state before
mask has been called. |
void |
setParameters(java.security.AlgorithmParameters params)
Sets any algorithm parameters required by this MGF engine.
|
void |
setParameters(java.security.spec.AlgorithmParameterSpec paramSpec)
Sets any algorithm parameters required by this MGF engine.
|
java.lang.String |
toString()
Returns a string representation.
|
clone, engineGetParameters, engineMask, engineReset, engineSetParameters, engineSetParameters
protected MaskGenerationAlgorithm(java.lang.String algorithm)
public static final MaskGenerationAlgorithm getInstance(java.lang.String algorithm) throws java.security.NoSuchAlgorithmException
algorithm
- the name of the mgf algorithm for which an engine should be
createdjava.security.NoSuchAlgorithmException
- if no implementation for the requested algorithm is availablepublic static final MaskGenerationAlgorithm getInstance(java.lang.String algorithm, java.lang.String providerName) throws java.security.NoSuchAlgorithmException, java.security.NoSuchProviderException
algorithm
- the name of the mgf algorithm for which an engine should be
createdproviderName
- the name of the provider whose MGF implementation is requestedjava.security.NoSuchAlgorithmException
- if no implementation for the requested algorithm is availablejava.security.NoSuchProviderException
- if the requested provider is not availablepublic static final MaskGenerationAlgorithm getInstance(java.lang.String algorithm, java.security.Provider provider) throws java.security.NoSuchAlgorithmException, java.security.NoSuchProviderException
algorithm
- the name of the mgf algorithm for which an engine should be
createdprovider
- the provider whose MGF implementation is requestedjava.security.NoSuchAlgorithmException
- if no implementation for the requested algorithm is availablejava.lang.IllegalArgumentException
- if provider
is nulljava.security.NoSuchProviderException
public final void mask(byte[] mgfSeed, int mgfSeedOff, int mgfSeedLen, int maskLen, byte[] dst, int dstOff)
A call to this method resets this engine to the state before
mask
has been called.
mgfSeed
- the seed from which the mask is generatedmgfSeedOff
- the offset indicating the start position within the seed arraymgfSeedLen
- the actual number of seed bytesmaskLen
- the number of required mask bytesdst
- the destination array to which to XOR the mask resultdstOff
- the offset indicating the start position within the destination
arraypublic final void reset()
mask
has been called.public final java.lang.String getAlgorithm()
public java.lang.String toString()
toString
in class java.lang.Object
public final void setParameters(java.security.spec.AlgorithmParameterSpec paramSpec) throws java.security.InvalidAlgorithmParameterException
paramSpec
- the parameters to be setjava.security.InvalidAlgorithmParameterException
- if the parameters are inappropriate for this engine or are
invalidpublic final void setParameters(java.security.AlgorithmParameters params) throws java.security.InvalidAlgorithmParameterException
params
- the parameters to be setjava.security.InvalidAlgorithmParameterException
- if the parameters are inappropriate for this engine or are
invalidpublic final java.security.AlgorithmParameters getParameters()