Example usage for org.bouncycastle.crypto Commitment Commitment

List of usage examples for org.bouncycastle.crypto Commitment Commitment

Introduction

In this page you can find the example usage for org.bouncycastle.crypto Commitment Commitment.

Prototype

public Commitment(byte[] secret, byte[] commitment) 

Source Link

Document

Base constructor.

Usage

From source file:com.vvote.verifierlibrary.utils.crypto.CryptoUtils.java

License:Open Source License

/**
 * Perform a verification on a hash commitment using bouncy castle. This
 * implementation uses the HashCommitter from bouncy castle and the
 * isRevealed function//from   w ww.  j a va  2  s . com
 * 
 * @param commitment
 * @param witness
 * @param randomValue
 * @return whether the commitment check is successful
 * @throws CommitException
 */
private static boolean bouncyCastleVerifyHashCommitment(byte[] commitment, byte[] witness, byte[] randomValue)
        throws CommitException {

    logger.debug("Verifying hash commitment using Bouncy castle implementation");

    // initialise a hash committer
    HashCommitter hashCommitter = new HashCommitter(new SHA256Digest(), new SecureRandom(witness));

    MessageDigest md = null;

    try {
        logger.debug("Initialising message digest");
        // initialise the message digest
        md = MessageDigest.getInstance(CryptoConstants.Commitments.COMMITMENT_HASH_ALGORITHM);

        // ensure the random value is the correct length
        if (randomValue.length > CryptoConstants.Commitments.RANDOM_VALUE_MAXIMUM_LENGTH) {
            logger.debug("Hashing random value to the correct length");
            md.reset();
            randomValue = md.digest(randomValue);
        }

        // initialise a new Commitment
        Commitment comm = new Commitment(witness, commitment);

        // check whether the given random value opens the commitment
        if (!hashCommitter.isRevealed(comm, randomValue)) {
            logger.error("Bouncy castle hash commitment verification failed");
            return false;
        }

    } catch (NoSuchAlgorithmException e) {
        logger.error("Could not initialise the message digest with the specified algorithm: {}",
                CryptoConstants.Commitments.COMMITMENT_HASH_ALGORITHM, e);
        throw new CommitException("Could not initialise the message digest with the specified algorithm: "
                + CryptoConstants.Commitments.COMMITMENT_HASH_ALGORITHM, e);
    }

    return true;
}

From source file:org.cryptoworkshop.ximix.client.verify.ECShuffledTranscriptVerifier.java

License:Apache License

/**
 * Verify that the transcripts are valid, throwing an exception if an issue is found.
 *
 * @throws TranscriptVerificationException on verification failure.
 *//*www  .jav  a  2s.  c  om*/
public void verify() throws TranscriptVerificationException {
    // if we've been past streams we have to read the lot in one go.
    int batchSize = (initialTranscript instanceof InputStream) ? -1 : 2000; // TODO: make batch size configurable
    boolean moreWitnesses = true;

    while (moreWitnesses) {
        moreWitnesses = loadWitnesses(witnessTranscript, batchSize);

        if (witnesses.isEmpty()) {
            break;
        }

        if (initialTranscript instanceof InputStream) {
            loadCommitments((InputStream) initialTranscript, (InputStream) finalTranscript);
        } else {
            try {
                InputStream initTranscript = new BufferedInputStream(
                        new FileInputStream((File) initialTranscript));
                InputStream finTranscript = new BufferedInputStream(
                        new FileInputStream((File) finalTranscript));

                loadCommitments(initTranscript, finTranscript);

                initTranscript.close();
                finTranscript.close();
            } catch (IOException e) {
                throw new TranscriptVerificationException("Exception validating transcripts: " + e.getMessage(),
                        e);
            }
        }

        if (initialMap.size() != witnesses.size()) {
            throw new TranscriptVerificationException("Initial transcript incomplete "
                    + (witnesses.size() - initialMap.size()) + " messages missing.");
        }

        if (!finalIndexesOfInterest.isEmpty()) {
            throw new TranscriptVerificationException(
                    "Final transcript incomplete " + finalIndexesOfInterest.size() + " messages missing.");
        }

        try {
            for (Integer msgIndex : witnesses.keySet()) {
                PostedMessage initMsg = initialMap.get(msgIndex);
                MessageCommitment comMsg = witnesses.get(msgIndex);

                BigInteger kValue = new BigInteger(1, comMsg.getDetail());
                ECPairFactorTransform transform = new ECFixedTransform(kValue);

                transform.init(pubKey);

                PairSequence ecSeq = PairSequence.getInstance(ecCurve, initMsg.getMessage());
                ECPair[] ecInit = ecSeq.getECPairs();
                ECPair[] ecRes = new ECPair[ecSeq.size()];

                for (int i = 0; i != ecRes.length; i++) {
                    ecRes[i] = transform.transform(ecInit[i]);
                }

                PostedMessage finalMsg = finalMap.get(comMsg.getNewIndex());
                Commitment commitment = new Commitment(comMsg.getSecret(), finalMsg.getCommitment());

                if (commitChecker.isRevealed(commitment, comMsg.getNewIndex())) {
                    ECPair[] ecFin = PairSequence
                            .getInstance(pubKey.getParameters().getCurve(), finalMsg.getMessage()).getECPairs();

                    if (!Arrays.equals(ecFin, ecRes)) {
                        throw new TranscriptVerificationException(
                                "Transformed cipher text does not match for relationship " + initMsg.getIndex()
                                        + " -> " + comMsg.getNewIndex());
                    }
                } else {
                    throw new TranscriptVerificationException("Commitment check failed for relationship "
                            + initMsg.getIndex() + " -> " + comMsg.getNewIndex());
                }
            }
        } catch (TranscriptVerificationException e) {
            throw e;
        } catch (Exception e) {
            throw new TranscriptVerificationException("Exception validating transcripts: " + e.getMessage(), e);
        }

        witnesses.clear();
        initialMap.clear();
        finalMap.clear();
        finalIndexesOfInterest.clear();
    }
}