java.lang.Object
org.bouncycastle.crypto.digests.KeccakDigest
- All Implemented Interfaces:
Digest, ExtendedDigest
- Direct Known Subclasses:
SHA3Digest, SHAKEDigest
Implementation of Keccak based on following KeccakNISTInterface.c from keccak.team
Following the naming conventions used in the C source code to enable easy review of the implementation.
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intNumber of message bits currently held in dataQueue.protected byte[]Buffer holding message bits not yet absorbed into the state.protected intDigest capacity in bits, twice the output length.private static long[]The round constants of the Keccak permutation.protected final CryptoServicePurposeCrypto service purpose this digest instance was created for.protected intSponge rate in bits, the number of message bits absorbed per permutation.protected booleanTrue once padding has been applied and output is being squeezed.protected long[]The sponge state, 25 lanes of 64 bits each.Constructor Summary
ConstructorsConstructorDescriptionCreate a Keccak-288 digest for any crypto service purpose.KeccakDigest(int bitLength) Create a digest of the given capacity, for any crypto service purpose.KeccakDigest(int bitLength, CryptoServicePurpose purpose) Create a digest of the given capacity and crypto service purpose.KeccakDigest(CryptoServicePurpose purpose) Create a Keccak-288 digest for the given crypto service purpose.KeccakDigest(KeccakDigest source) Copy the state of an existing digest, including any partially absorbed data.Method Summary
Modifier and TypeMethodDescriptionprotected voidabsorb(byte data) Absorb one byte into the sponge, permuting once the rate is reached.protected voidabsorb(byte[] data, int off, int len) Absorb a byte range into the sponge, permuting once per rate-sized block.protected voidabsorbBits(int data, int bits) Absorb the trailing 1-7 bits of a message, leaving the queue not a multiple of 8 so that no further absorbing is possible.protected CryptoServicePropertiesDescribe this digest's properties for the crypto services registrar.intdoFinal(byte[] out, int outOff) Finish the digest, write it out and reset this instance.protected intdoFinal(byte[] out, int outOff, byte partialByte, int partialBits) Finish the digest, first absorbing the trailing bits of a partial byte.Get the JCA name of this algorithm.intReturn the size of block that the compression function is applied to in bytes.intGet the size of the digest this class produces.private voidinit(int bitLength) Sets up the digest for a given output size.private voidinitSponge(int rate) Sets up the sponge for a given rate.private voidKeccakAbsorb(byte[] data, int off) Absorbs one block of input.private voidCopies the current state into the output buffer.private voidRuns the permutation over the state.private voidApplies the padding and moves to the squeezing phase.voidreset()Clear all absorbed message and output state, making the instance ready for a fresh message at its existing capacity.protected voidsqueeze(byte[] output, int offset, long outputLength) Squeeze outputLength bits out of the sponge, padding first if the message has not been finished.voidupdate(byte in) Absorb one more byte of message.voidupdate(byte[] in, int inOff, int len) Absorb more message bytes.
Field Details
KeccakRoundConstants
private static long[] KeccakRoundConstantsThe round constants of the Keccak permutation.purpose
Crypto service purpose this digest instance was created for.state
protected long[] stateThe sponge state, 25 lanes of 64 bits each.dataQueue
protected byte[] dataQueueBuffer holding message bits not yet absorbed into the state.rate
protected int rateSponge rate in bits, the number of message bits absorbed per permutation.bitsInQueue
protected int bitsInQueueNumber of message bits currently held in dataQueue.fixedOutputLength
protected int fixedOutputLengthDigest capacity in bits, twice the output length.squeezing
protected boolean squeezingTrue once padding has been applied and output is being squeezed.
Constructor Details
KeccakDigest
public KeccakDigest()Create a Keccak-288 digest for any crypto service purpose.KeccakDigest
Create a Keccak-288 digest for the given crypto service purpose.- Parameters:
purpose- the crypto service purpose to register the digest under
KeccakDigest
public KeccakDigest(int bitLength) Create a digest of the given capacity, for any crypto service purpose.- Parameters:
bitLength- the capacity in bits, i.e. twice the digest length
KeccakDigest
Create a digest of the given capacity and crypto service purpose.- Parameters:
bitLength- the capacity in bits, i.e. twice the digest lengthpurpose- the crypto service purpose to register the digest under
KeccakDigest
Copy the state of an existing digest, including any partially absorbed data.- Parameters:
source- the digest to copy the state and rate from
Method Details
getAlgorithmName
Get the JCA name of this algorithm.- Specified by:
getAlgorithmNamein interfaceDigest- Returns:
- the digest length in bits, prefixed with "Keccak-"
getDigestSize
public int getDigestSize()Get the size of the digest this class produces.- Specified by:
getDigestSizein interfaceDigest- Returns:
- the digest length in bytes
update
update
doFinal
doFinal
protected int doFinal(byte[] out, int outOff, byte partialByte, int partialBits) Finish the digest, first absorbing the trailing bits of a partial byte.- Parameters:
out- the buffer to write the digest intooutOff- the offset into out at which to write the digestpartialByte- a byte holding the final 1-7 bits of the messagepartialBits- how many bits of partialByte are message, 1 to 7, or 0 if none- Returns:
- the number of bytes written to out
reset
getByteLength
public int getByteLength()Return the size of block that the compression function is applied to in bytes.- Specified by:
getByteLengthin interfaceExtendedDigest- Returns:
- internal byte length of a block.
init
private void init(int bitLength) Sets up the digest for a given output size.- Parameters:
bitLength- the output size, in bits
initSponge
private void initSponge(int rate) Sets up the sponge for a given rate.- Parameters:
rate- the sponge rate, in bits
absorb
protected void absorb(byte data) Absorb one byte into the sponge, permuting once the rate is reached.- Parameters:
data- the byte to absorb
absorb
protected void absorb(byte[] data, int off, int len) Absorb a byte range into the sponge, permuting once per rate-sized block.- Parameters:
data- the buffer holding the messageoff- the offset into data of the first byte to absorblen- the number of bytes to absorb
absorbBits
protected void absorbBits(int data, int bits) Absorb the trailing 1-7 bits of a message, leaving the queue not a multiple of 8 so that no further absorbing is possible.- Parameters:
data- the value holding the final bits, of which only the low ones countbits- how many low bits of data are message, 1 to 7
padAndSwitchToSqueezingPhase
private void padAndSwitchToSqueezingPhase()Applies the padding and moves to the squeezing phase.squeeze
protected void squeeze(byte[] output, int offset, long outputLength) Squeeze outputLength bits out of the sponge, padding first if the message has not been finished.- Parameters:
output- the buffer to write the output intooffset- the offset into output at which to writeoutputLength- the number of bits to squeeze, a multiple of 8
KeccakAbsorb
private void KeccakAbsorb(byte[] data, int off) Absorbs one block of input.- Parameters:
data- the input bytesoff- where the block starts
KeccakExtract
private void KeccakExtract()Copies the current state into the output buffer.KeccakPermutation
private void KeccakPermutation()Runs the permutation over the state.cryptoServiceProperties
Describe this digest's properties for the crypto services registrar.- Returns:
- the digest's service properties, or null if it declares none