com.fitbur.bouncycastle.crypto.commitments.GeneralHashCommitter Maven / Gradle / Ivy
package com.fitbur.bouncycastle.crypto.com.fitburmitments;
import java.security.SecureRandom;
import com.fitbur.bouncycastle.crypto.Commitment;
import com.fitbur.bouncycastle.crypto.Committer;
import com.fitbur.bouncycastle.crypto.DataLengthException;
import com.fitbur.bouncycastle.crypto.Digest;
import com.fitbur.bouncycastle.crypto.ExtendedDigest;
import com.fitbur.bouncycastle.util.Arrays;
/**
* A basic hash-com.fitburmitter based on the one com.fitburscribed in "Making Mix Nets Robust for Electronic Voting by Randomized Partial Checking",
* by Jakobsson, Juels, and Rivest (11th Usenix Security Symposium, 2002).
*
* The algorithm used by this class differs from the one given in that it includes the length of the message in the hash calculation.
*
*/
public class GeneralHashCommitter
implements Committer
{
private final Digest digest;
private final int byteLength;
private final SecureRandom random;
/**
* Base Constructor. The maximum message length that can be com.fitburmitted to is half the length of the internal
* block size for the digest (ExtendedDigest.getBlockLength()).
*
* @param digest digest to use for creating com.fitburmitments.
* @param random source of randomness for generating secrets.
*/
public GeneralHashCommitter(ExtendedDigest digest, SecureRandom random)
{
this.digest = digest;
this.byteLength = digest.getByteLength();
this.random = random;
}
/**
* Generate a com.fitburmitment for the passed in message.
*
* @param message the message to be com.fitburmitted to,
* @return a Commitment
*/
public Commitment com.fitburmit(byte[] message)
{
if (message.length > byteLength / 2)
{
throw new DataLengthException("Message to be com.fitburmitted to too large for digest.");
}
byte[] w = new byte[byteLength - message.length];
random.nextBytes(w);
return new Commitment(w, calculateCommitment(w, message));
}
/**
* Return true if the passed in com.fitburmitment represents a com.fitburmitment to the passed in message.
*
* @param com.fitburmitment a com.fitburmitment previously generated.
* @param message the message that was expected to have been com.fitburmitted to.
* @return true if com.fitburmitment matches message, false otherwise.
*/
public boolean isRevealed(Commitment com.fitburmitment, byte[] message)
{
if (message.length + com.fitburmitment.getSecret().length != byteLength)
{
throw new DataLengthException("Message and witness secret lengths do not match.");
}
byte[] calcCommitment = calculateCommitment(com.fitburmitment.getSecret(), message);
return Arrays.constantTimeAreEqual(com.fitburmitment.getCommitment(), calcCommitment);
}
private byte[] calculateCommitment(byte[] w, byte[] message)
{
byte[] com.fitburmitment = new byte[digest.getDigestSize()];
digest.update(w, 0, w.length);
digest.update(message, 0, message.length);
digest.update((byte)((message.length >>> 8)));
digest.update((byte)(message.length));
digest.doFinal(com.fitburmitment, 0);
return com.fitburmitment;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy