private static StreamCipher getSalsa20(byte[] key) { // Build stream cipher key MessageDigest md; try { md = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new RuntimeException("SHA 256 not supported"); } byte[] key32 = md.digest(key); KeyParameter keyParam = new KeyParameter(key32); ParametersWithIV ivParam = new ParametersWithIV(keyParam, SALSA_IV); StreamCipher cipher = new Salsa20Engine(); cipher.init(true, ivParam); return cipher; }
@Override protected StreamCipher getCipher() { String method = getMethod(); AESFastEngine engine = new AESFastEngine(); switch (method) { case CIPHER_AES_128_CFB: case CIPHER_AES_192_CFB: case CIPHER_AES_256_CFB: return new CFBBlockCipher(engine, 128); case CIPHER_AES_128_CFB8: case CIPHER_AES_192_CFB8: case CIPHER_AES_256_CFB8: return new CFBBlockCipher(engine, 8); case CIPHER_AES_128_OFB: case CIPHER_AES_192_OFB: case CIPHER_AES_256_OFB: return new OFBBlockCipher(engine, 128); default: throw new IllegalArgumentException(method); } }
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) { } }
private void Grain128Test1(CipherParameters params) { StreamCipher grain = new Grain128Engine(); byte[] in = new byte[16]; byte[] out = new byte[16]; grain.init(true, params); grain.processBytes(in, 0, in.length, out, 0); if (!areEqual(out, Hex.decode(keyStream1))) { mismatch("Keystream 1", keyStream1, out); } grain.reset(); grain.processBytes(in, 0, in.length, out, 0); if (!areEqual(out, Hex.decode(keyStream1))) { mismatch("Keystream 1", keyStream1, out); } }
private void Grain128Test2(CipherParameters params) { StreamCipher grain = new Grain128Engine(); byte[] in = new byte[16]; byte[] out = new byte[16]; grain.init(true, params); grain.processBytes(in, 0, in.length, out, 0); if (!areEqual(out, Hex.decode(keyStream2))) { mismatch("Keystream 2", keyStream2, out); } grain.reset(); grain.processBytes(in, 0, in.length, out, 0); if (!areEqual(out, Hex.decode(keyStream2))) { mismatch("Keystream 2", keyStream2, out); } }
private void Grain128Test3(CipherParameters params) { StreamCipher grain = new Grain128Engine(); byte[] in = "Encrypt me!".getBytes(); byte[] cipher = new byte[in.length]; byte[] clear = new byte[in.length]; grain.init(true, params); grain.processBytes(in, 0, in.length, cipher, 0); grain.reset(); grain.processBytes(cipher, 0, cipher.length, clear, 0); if (!areEqual(in, clear)) { mismatch("Test 3", new String(Hex.encode(in)), clear); } }
private InputStream createCipherInputStream(byte[] data, Object cipher) { ByteArrayInputStream input = new ByteArrayInputStream(data); if (cipher instanceof BufferedBlockCipher) { return new CipherInputStream(input, (BufferedBlockCipher)cipher); } else if (cipher instanceof AEADBlockCipher) { return new CipherInputStream(input, (AEADBlockCipher)cipher); } else { return new CipherInputStream(input, (StreamCipher)cipher); } }
private String getName(Object cipher) { if (cipher instanceof BufferedBlockCipher) { return ((BufferedBlockCipher)cipher).getUnderlyingCipher().getAlgorithmName(); } else if (cipher instanceof AEADBlockCipher) { return ((AEADBlockCipher)cipher).getUnderlyingCipher().getAlgorithmName(); } else if (cipher instanceof StreamCipher) { return ((StreamCipher)cipher).getAlgorithmName(); } return null; }
private int getBlockSize(Object cipher) { if (cipher instanceof BlockCipher) { return ((BlockCipher)cipher).getBlockSize(); } else if (cipher instanceof BufferedBlockCipher) { return ((BufferedBlockCipher)cipher).getBlockSize(); } else if (cipher instanceof AEADBlockCipher) { return ((AEADBlockCipher)cipher).getUnderlyingCipher().getBlockSize(); } else if (cipher instanceof StreamCipher) { return 1; } return 0; }
private void Grainv1Test1(CipherParameters params) { StreamCipher grain = new Grainv1Engine(); byte[] in = new byte[10]; byte[] out = new byte[10]; grain.init(true, params); grain.processBytes(in, 0, in.length, out, 0); if (!areEqual(out, Hex.decode(keyStream1))) { mismatch("Keystream 1", keyStream1, out); } grain.reset(); grain.processBytes(in, 0, in.length, out, 0); if (!areEqual(out, Hex.decode(keyStream1))) { mismatch("Keystream 1", keyStream1, out); } }
private void Grainv1Test2(CipherParameters params) { StreamCipher grain = new Grainv1Engine(); byte[] in = new byte[10]; byte[] out = new byte[10]; grain.init(true, params); grain.processBytes(in, 0, in.length, out, 0); if (!areEqual(out, Hex.decode(keyStream2))) { mismatch("Keystream 2", keyStream2, out); } grain.reset(); grain.processBytes(in, 0, in.length, out, 0); if (!areEqual(out, Hex.decode(keyStream2))) { mismatch("Keystream 2", keyStream2, out); } }
private void Grainv1Test3(CipherParameters params) { StreamCipher grain = new Grainv1Engine(); byte[] in = "Encrypt me!".getBytes(); byte[] cipher = new byte[in.length]; byte[] clear = new byte[in.length]; grain.init(true, params); grain.processBytes(in, 0, in.length, cipher, 0); grain.reset(); grain.processBytes(cipher, 0, cipher.length, clear, 0); if (!areEqual(in, clear)) { mismatch("Test 3", new String(Hex.encode(in)), clear); } }
private void reinitBug() { KeyParameter key = new KeyParameter(Hex.decode("80000000000000000000000000000000")); ParametersWithIV parameters = new ParametersWithIV(key, Hex.decode("0000000000000000")); StreamCipher salsa = new Salsa20Engine(); salsa.init(true, parameters); try { salsa.init(true, key); fail("Salsa20 should throw exception if no IV in Init"); } catch (IllegalArgumentException e) { } }
private void testReset(StreamCipher cipher1, StreamCipher cipher2, CipherParameters params) throws InvalidCipherTextException { cipher1.init(true, params); byte[] plaintext = new byte[1023]; byte[] ciphertext = new byte[plaintext.length]; // Establish baseline answer cipher1.processBytes(plaintext, 0, plaintext.length, ciphertext, 0); // Test encryption resets checkReset(cipher1, params, true, plaintext, ciphertext); // Test decryption resets with fresh instance cipher2.init(false, params); checkReset(cipher2, params, false, ciphertext, plaintext); }
/** * Class constructor * * @param cipherMechanism block cipher mechanism identifier * @param paddingMechanism padding mechanism identifier * @param streamCipher stream cipher object * @param key destroyable key * @param iv initial vector */ public CipherContext(final TypesProto.CipherMechanism cipherMechanism, final TypesProto.PaddingMechanism paddingMechanism, final StreamCipher streamCipher, final DestroyableKey key, final byte[] iv) { if (Objects.isNull(cipherMechanism)) { throw new IllegalArgumentException("cipher mechanism is null"); } else if (Objects.isNull(streamCipher)) { throw new IllegalArgumentException("blockCipher is null"); } else if (Objects.isNull(key)) { throw new IllegalArgumentException("key is null"); }else if (Objects.isNull(iv)) { throw new IllegalArgumentException("iv is null"); } this.cipherMechanism = cipherMechanism; this.paddingMechanism = paddingMechanism; this.streamCipher = streamCipher; this.key = key; this.iv = iv; this.blockCipher = null; this.padding = null; }
private void runAllVectors(StreamCipher hc, String fileName, PeekableLineReader r) throws IOException { for (;;) { String line = r.readLine(); if (line == null) { break; } line = line.trim(); if (line.startsWith("Set ")) { runVector(hc, fileName, r, dellChar(line, ':')); } } }
private void salsa20Test2(CipherParameters params, String v0, String v65472, String v65536) { StreamCipher salsa = new Salsa20Engine(); 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 } } }
protected JCEStreamCipher( StreamCipher engine, int ivLength) { cipher = engine; this.ivLength = ivLength; }
public static StreamCipher getInstance(CrsAlgorithm alg, byte[] key) { if ( alg == CrsAlgorithm.Salsa20 ) { return getSalsa20(key); } else { return null; } }
/** * Constructs a CipherOutputStream from an OutputStream and a * BufferedBlockCipher. */ public CipherOutputStream( OutputStream os, StreamCipher cipher) { super(os); this.streamCipher = cipher; }
public CipherInputStream( InputStream is, StreamCipher cipher) { super(is); this.streamCipher = cipher; buf = new byte[INPUT_BUF_SIZE]; inBuf = new byte[INPUT_BUF_SIZE]; }
protected BaseStreamCipher( StreamCipher engine, int ivLength) { cipher = engine; this.ivLength = ivLength; }