All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cryptomator.siv.CustomCtrComputer Maven / Gradle / Ivy

There is a newer version: 1.5.2
Show newest version
package org.cryptomator.siv;

import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.OutputLengthException;
import org.bouncycastle.crypto.modes.SICBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;

import java.util.function.Supplier;

/**
 * Performs CTR Mode computations facilitating BouncyCastle's {@link SICBlockCipher}.
 */
class CustomCtrComputer implements SivMode.CtrComputer {

	private final Supplier blockCipherSupplier;

	public CustomCtrComputer(Supplier blockCipherSupplier) {
		this.blockCipherSupplier = blockCipherSupplier;
	}
	
	@Override
	public byte[] computeCtr(byte[] input, byte[] key, byte[] iv) {
		SICBlockCipher cipher = new SICBlockCipher(blockCipherSupplier.get());
		CipherParameters params = new ParametersWithIV(new KeyParameter(key), iv);
		cipher.init(true, params);
		try {
			byte[] output = new byte[input.length];
			cipher.processBytes(input, 0, input.length, output, 0);
			return output;
		} catch (OutputLengthException e) {
			throw new IllegalStateException("In CTR mode output length must be equal to input length", e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy