public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException { if (params instanceof TweakableBlockCipherParameters) { init(forEncryption, (TweakableBlockCipherParameters)params); } else if (params instanceof KeyParameter) { init(forEncryption, new TweakableBlockCipherParameters((KeyParameter)params, new byte[TWEAK_SIZE])); } else { throw new IllegalArgumentException("Invalid parameter passed to Threefish init - " + params.getClass().getName()); } }
public void init(boolean forEncryption, TweakableBlockCipherParameters params) throws IllegalArgumentException { // TODO: Remove some of the NPEs that can be avoided in the Params // classes if ((params.getKey() == null) || (params.getKey().getKey() == null) || (params.getKey().getKey().length != blocksize)) { throw new IllegalArgumentException("Threefish key must be same size as block (%d bytes)" + blocksize); } if ((params.getTweak() == null) || (params.getTweak().length != TWEAK_SIZE)) { throw new IllegalArgumentException("Threefish tweak must be %d bytes" + TWEAK_SIZE); } this.forEncryption = forEncryption; generateKeySchedule(params.getKey().getKey(), params.getTweak()); }
private static void runTestVector(String name, byte[] key, byte[] tweak, byte[] plaintext, byte[] expected, BlockCipher cipher) { System.out.println("===="); System.out.println(name + ": "); cipher.init(true, new TweakableBlockCipherParameters(new KeyParameter(key), tweak)); byte[] ciphertext = new byte[key.length]; cipher.processBlock(plaintext, 0, ciphertext, 0); System.out.println("Plaintext : " + new String(Hex.encode(plaintext))); System.out.println("Expected : " + new String(Hex.encode(expected))); System.out.println("Ciphertext : " + new String(Hex.encode(ciphertext))); System.out.println(" Encrypt : " + org.bouncycastle.util.Arrays.areEqual(expected, ciphertext)); cipher.init(false, new TweakableBlockCipherParameters(new KeyParameter(key), tweak)); byte[] replain = new byte[plaintext.length]; cipher.processBlock(ciphertext, 0, replain, 0); System.out.println("Replain : " + new String(Hex.encode(replain))); System.out.println(" Decrypt : " + org.bouncycastle.util.Arrays.areEqual(plaintext, replain)); }
/** * Initialise the engine. * * @param params an instance of {@link TweakableBlockCipherParameters}, or {@link KeyParameter} (to * use a 0 tweak) */ public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException { final byte[] keyBytes; final byte[] tweakBytes; if (params instanceof TweakableBlockCipherParameters) { TweakableBlockCipherParameters tParams = (TweakableBlockCipherParameters)params; keyBytes = tParams.getKey().getKey(); tweakBytes = tParams.getTweak(); } else if (params instanceof KeyParameter) { keyBytes = ((KeyParameter)params).getKey(); tweakBytes = null; } else { throw new IllegalArgumentException("Invalid parameter passed to Threefish init - " + params.getClass().getName()); } long[] keyWords = null; long[] tweakWords = null; if (keyBytes != null) { if (keyBytes.length != this.blocksizeBytes) { throw new IllegalArgumentException("Threefish key must be same size as block (" + blocksizeBytes + " bytes)"); } keyWords = new long[blocksizeWords]; for (int i = 0; i < keyWords.length; i++) { keyWords[i] = bytesToWord(keyBytes, i * 8); } } if (tweakBytes != null) { if (tweakBytes.length != TWEAK_SIZE_BYTES) { throw new IllegalArgumentException("Threefish tweak must be " + TWEAK_SIZE_BYTES + " bytes"); } tweakWords = new long[]{bytesToWord(tweakBytes, 0), bytesToWord(tweakBytes, 8)}; } init(forEncryption, keyWords, tweakWords); }