protected KeyParameter initRecordMAC(ChaChaEngine cipher, boolean forEncryption, long seqNo) { byte[] nonce = new byte[8]; TlsUtils.writeUint64(seqNo, nonce, 0); cipher.init(forEncryption, new ParametersWithIV(null, nonce)); byte[] firstBlock = new byte[64]; cipher.processBytes(firstBlock, 0, firstBlock.length, firstBlock, 0); // NOTE: The BC implementation puts 'r' after 'k' System.arraycopy(firstBlock, 0, firstBlock, 32, 16); KeyParameter macKey = new KeyParameter(firstBlock, 16, 32); Poly1305KeyGenerator.clamp(macKey.getKey()); return macKey; }
private void reinitBug() { KeyParameter key = new KeyParameter(Hex.decode("80000000000000000000000000000000")); ParametersWithIV parameters = new ParametersWithIV(key, Hex.decode("0000000000000000")); StreamCipher salsa = new ChaChaEngine(); salsa.init(true, parameters); try { salsa.init(true, key); fail("Salsa20 should throw exception if no IV in Init"); } catch (IllegalArgumentException e) { } }
public void performTest() throws Exception { testReset(new Salsa20Engine(), new Salsa20Engine(), new ParametersWithIV(new KeyParameter(random(32)), random(8))); testReset(new Salsa20Engine(), new Salsa20Engine(), new ParametersWithIV(new KeyParameter(random(16)), random(8))); testReset(new XSalsa20Engine(), new XSalsa20Engine(), new ParametersWithIV(new KeyParameter(random(32)), random(24))); testReset(new ChaChaEngine(), new ChaChaEngine(), new ParametersWithIV(new KeyParameter(random(32)), random(8))); testReset(new ChaChaEngine(), new ChaChaEngine(), new ParametersWithIV(new KeyParameter(random(16)), random(8))); testReset(new RC4Engine(), new RC4Engine(), new KeyParameter(random(16))); testReset(new ISAACEngine(), new ISAACEngine(), new KeyParameter(random(16))); testReset(new HC128Engine(), new HC128Engine(), new ParametersWithIV(new KeyParameter(random(16)), random(16))); testReset(new HC256Engine(), new HC256Engine(), new ParametersWithIV(new KeyParameter(random(16)), random(16))); testReset(new Grainv1Engine(), new Grainv1Engine(), new ParametersWithIV(new KeyParameter(random(16)), random(8))); testReset(new Grain128Engine(), new Grain128Engine(), new ParametersWithIV(new KeyParameter(random(16)), random(12))); }
@Override protected StreamCipher getCipher() { String method = getMethod(); switch (method) { case CIPHER_SALSA20: return new Salsa20Engine(); case CIPHER_CHACHA20: return new ChaChaEngine(); default: throw new IllegalArgumentException(method); } }
@Override protected StreamCipher getCipher(boolean isEncrypted) throws InvalidAlgorithmParameterException { if (_name.equals(CIPHER_CHACHA20)) { return new ChaChaEngine(); } else if (_name.equals(CIPHER_CHACHA20_IETF)) { return new ChaCha7539Engine(); } return null; }
public Chacha20Poly1305(TlsContext context) throws IOException { if (!TlsUtils.isTLSv12(context)) { throw new TlsFatalAlert(AlertDescription.internal_error); } this.context = context; byte[] key_block = TlsUtils.calculateKeyBlock(context, 64); KeyParameter client_write_key = new KeyParameter(key_block, 0, 32); KeyParameter server_write_key = new KeyParameter(key_block, 32, 32); this.encryptCipher = new ChaChaEngine(20); this.decryptCipher = new ChaChaEngine(20); KeyParameter encryptKey, decryptKey; if (context.isServer()) { encryptKey = server_write_key; decryptKey = client_write_key; } else { encryptKey = client_write_key; decryptKey = server_write_key; } byte[] dummyNonce = new byte[8]; this.encryptCipher.init(true, new ParametersWithIV(encryptKey, dummyNonce)); this.decryptCipher.init(false, new ParametersWithIV(decryptKey, dummyNonce)); }
private void chachaTest2(CipherParameters params, String v0, String v65472, String v65536) { StreamCipher chaCha = new ChaChaEngine(); byte[] buf = new byte[64]; chaCha.init(true, params); for (int i = 0; i != 1025; i++) { chaCha.processBytes(zeroes, 0, 64, buf, 0); switch (i) { case 0: if (!areEqual(buf, Hex.decode(v0))) { mismatch("v0", v0, buf); } break; case 1023: if (!areEqual(buf, Hex.decode(v65472))) { mismatch("v65472", v65472, buf); } break; case 1024: if (!areEqual(buf, Hex.decode(v65536))) { mismatch("v65536", v65536, buf); } break; default: // ignore } } }
private void performTests() throws Exception { testModes(new BlowfishEngine(), new BlowfishEngine(), 16); testModes(new DESEngine(), new DESEngine(), 8); testModes(new DESedeEngine(), new DESedeEngine(), 24); testModes(new TEAEngine(), new TEAEngine(), 16); testModes(new CAST5Engine(), new CAST5Engine(), 16); testModes(new RC2Engine(), new RC2Engine(), 16); testModes(new XTEAEngine(), new XTEAEngine(), 16); testModes(new AESEngine(), new AESEngine(), 16); testModes(new NoekeonEngine(), new NoekeonEngine(), 16); testModes(new TwofishEngine(), new TwofishEngine(), 16); testModes(new CAST6Engine(), new CAST6Engine(), 16); testModes(new SEEDEngine(), new SEEDEngine(), 16); testModes(new SerpentEngine(), new SerpentEngine(), 16); testModes(new RC6Engine(), new RC6Engine(), 16); testModes(new CamelliaEngine(), new CamelliaEngine(), 16); testModes(new ThreefishEngine(ThreefishEngine.BLOCKSIZE_512), new ThreefishEngine(ThreefishEngine.BLOCKSIZE_512), 64); testMode(new RC4Engine(), new KeyParameter(new byte[16])); testMode(new Salsa20Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[8])); testMode(new XSalsa20Engine(), new ParametersWithIV(new KeyParameter(new byte[32]), new byte[24])); testMode(new ChaChaEngine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[8])); testMode(new Grainv1Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[8])); testMode(new Grain128Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[12])); testMode(new HC128Engine(), new KeyParameter(new byte[16])); testMode(new HC256Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testSkipping(new Salsa20Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[8])); testSkipping(new SICBlockCipher(new AESEngine()), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); }
protected byte[] bouncyCastle(byte[] plain, byte[] key, byte[] nonce) { StreamCipher chaCha = new ChaChaEngine(20); chaCha.init(true, new ParametersWithIV(new KeyParameter(key), nonce)); byte[] buf = new byte[plain.length]; chaCha.processBytes(plain, 0, plain.length, buf, 0); return buf; }
private KeyParameter initRecordMAC(ChaChaEngine cipher) { byte[] firstBlock = new byte[64]; cipher.processBytes(firstBlock, 0, firstBlock.length, firstBlock, 0); // NOTE: The BC implementation puts 'r' after 'k' System.arraycopy(firstBlock, 0, firstBlock, 32, 16); KeyParameter macKey = new KeyParameter(firstBlock, 16, 32); Poly1305KeyGenerator.clamp(macKey.getKey()); return macKey; }
private void chachaTest2(CipherParameters params, String v0, String v65472, String v65536) { StreamCipher salsa = new ChaChaEngine(); byte[] buf = new byte[64]; salsa.init(true, params); for (int i = 0; i != 1025; i++) { salsa.processBytes(zeroes, 0, 64, buf, 0); switch (i) { case 0: if (!areEqual(buf, Hex.decode(v0))) { mismatch("v0", v0, buf); } break; case 1023: if (!areEqual(buf, Hex.decode(v65472))) { mismatch("v65472", v65472, buf); } break; case 1024: if (!areEqual(buf, Hex.decode(v65536))) { mismatch("v65536", v65536, buf); } break; default: // ignore } } }
@Override public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException, InvalidAlgorithmParameterException, InvalidCipherTextException { ChaChaEngine cipher = new ChaChaEngine(); cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes)); byte[] result = new byte[data.length]; cipher.processBytes(data, 0, data.length, result, 0); return result; }
@Override public byte[] decrypt(byte[] data, byte[] randomKeyBytes) throws InvalidKeyException, InvalidAlgorithmParameterException, IOException, IllegalStateException, InvalidCipherTextException { ChaChaEngine cipher = new ChaChaEngine(); cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes)); byte[] result = new byte[data.length]; cipher.processBytes(data, 0, data.length, result, 0); return result; }
/** * Special method to decrypt partial data * * @param data to decrypt * @param randomKeyBytes key to use * @param startingByte the starting position within the (complete) data * @return decrypted data */ public byte[] decrypt(byte[] data, byte[] randomKeyBytes, long startingByte) { ChaChaEngine cipher = new ChaChaEngine(); cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes)); cipher.skip(startingByte); byte[] result = new byte[data.length]; cipher.processBytes(data, 0, data.length, result, 0); return result; }
private void chachaTest1(int rounds, CipherParameters params, String v0, String v192, String v256, String v448) { StreamCipher chaCha = new ChaChaEngine(rounds); byte[] buf = new byte[64]; chaCha.init(true, params); for (int i = 0; i != 7; i++) { chaCha.processBytes(zeroes, 0, 64, buf, 0); switch (i) { case 0: if (!areEqual(buf, Hex.decode(v0))) { mismatch("v0/" + rounds, v0, buf); } break; case 3: if (!areEqual(buf, Hex.decode(v192))) { mismatch("v192/" + rounds, v192, buf); } break; case 4: if (!areEqual(buf, Hex.decode(v256))) { mismatch("v256/" + rounds, v256, buf); } break; default: // ignore } } for (int i = 0; i != 64; i++) { buf[i] = chaCha.returnByte(zeroes[i]); } if (!areEqual(buf, Hex.decode(v448))) { mismatch("v448", v448, buf); } }
public Base() { super(new ChaChaEngine(), 8); }
@Override protected StreamCipher getCipher(boolean isEncrypted) throws InvalidAlgorithmParameterException { StreamCipher engine = new ChaChaEngine(); return engine; }
private void chachaTest1(int rounds, CipherParameters params, String v0, String v192, String v256, String v448) { StreamCipher salsa = new ChaChaEngine(rounds); byte[] buf = new byte[64]; salsa.init(true, params); for (int i = 0; i != 7; i++) { salsa.processBytes(zeroes, 0, 64, buf, 0); switch (i) { case 0: if (!areEqual(buf, Hex.decode(v0))) { mismatch("v0/" + rounds, v0, buf); } break; case 3: if (!areEqual(buf, Hex.decode(v192))) { mismatch("v192/" + rounds, v192, buf); } break; case 4: if (!areEqual(buf, Hex.decode(v256))) { mismatch("v256/" + rounds, v256, buf); } break; default: // ignore } } for (int i = 0; i != 64; i++) { buf[i] = salsa.returnByte(zeroes[i]); } if (!areEqual(buf, Hex.decode(v448))) { mismatch("v448", v448, buf); } }
public ChachaDecoder(byte[] key, byte[] nonce) throws IOException { this.decryptCipher = new ChaChaEngine(20); this.decryptCipher.init(false, new ParametersWithIV(new KeyParameter(key), nonce)); }
public ChachaEncoder(byte[] key, byte[] nonce) throws IOException { this.encryptCipher = new ChaChaEngine(20); this.encryptCipher.init(true, new ParametersWithIV(new KeyParameter(key), nonce)); }