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; }
private static void BlockMix(int[] B, int[] X1, int[] X2, int[] Y, int r) { System.arraycopy(B, B.length - 16, X1, 0, 16); int BOff = 0, YOff = 0, halfLen = B.length >>> 1; for (int i = 2 * r; i > 0; --i) { Xor(X1, B, BOff, X2); Salsa20Engine.salsaCore(8, X2, X1); System.arraycopy(X1, 0, Y, YOff, 16); YOff = halfLen + BOff - YOff; BOff += 16; } System.arraycopy(Y, 0, B, 0, Y.length); }
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) { } }
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 public int getIVSize() { if (ivSize < 0) { if (delegate instanceof Grainv1Engine) ivSize = 8; else if (delegate instanceof Grain128Engine) ivSize = 12; else if (delegate instanceof HC128Engine) ivSize = 16; else if (delegate instanceof HC256Engine) ivSize = 32; else if (delegate instanceof ISAACEngine) ivSize = 0; else if (delegate instanceof RC4Engine) ivSize = 0; else if (delegate instanceof Salsa20Engine) ivSize = 8; else throw new UnsupportedOperationException("For this delegate cipher type, this operation is not yet supported!"); } return ivSize; }
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 } } }
static void hsalsa20(byte[] out, byte[] in, byte[] k) { final int[] x = new int[16]; x[0] = littleEndianToInt(HSalsa20.SIGMA, 0); x[1] = littleEndianToInt(k, 0); x[2] = littleEndianToInt(k, 4); x[3] = littleEndianToInt(k, 8); x[4] = littleEndianToInt(k, 12); x[5] = littleEndianToInt(HSalsa20.SIGMA, 4); x[6] = littleEndianToInt(in, 0); x[7] = littleEndianToInt(in, 4); x[8] = littleEndianToInt(in, 8); x[9] = littleEndianToInt(in, 12); x[10] = littleEndianToInt(HSalsa20.SIGMA, 8); x[11] = littleEndianToInt(k, 16); x[12] = littleEndianToInt(k, 20); x[13] = littleEndianToInt(k, 24); x[14] = littleEndianToInt(k, 28); x[15] = littleEndianToInt(HSalsa20.SIGMA, 12); Salsa20Engine.salsaCore(20, x, x); x[0] -= littleEndianToInt(HSalsa20.SIGMA, 0); x[5] -= littleEndianToInt(HSalsa20.SIGMA, 4); x[10] -= littleEndianToInt(HSalsa20.SIGMA, 8); x[15] -= littleEndianToInt(HSalsa20.SIGMA, 12); x[6] -= littleEndianToInt(in, 0); x[7] -= littleEndianToInt(in, 4); x[8] -= littleEndianToInt(in, 8); x[9] -= littleEndianToInt(in, 12); intToLittleEndian(x[0], out, 0); intToLittleEndian(x[5], out, 4); intToLittleEndian(x[10], out, 8); intToLittleEndian(x[15], out, 12); intToLittleEndian(x[6], out, 16); intToLittleEndian(x[7], out, 20); intToLittleEndian(x[8], out, 24); intToLittleEndian(x[9], out, 28); }
@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); } }
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])); }
public Salsa20Cipher(byte[] pbKey32, byte[] pbIV8) { engine = new Salsa20Engine(); KeyParameter key = new KeyParameter(pbKey32); ParametersWithIV iv = new ParametersWithIV(key, pbIV8); engine.init(true, iv); }
public Salsa20() { super(new Salsa20Engine(), 8); }
public Base() { super(new Salsa20Engine(), 8); }
protected StreamCipher createSalsa20StreamCipher(int rounds) { return new Salsa20Engine(rounds); }
private void salsa20Test1(int rounds, CipherParameters params, String v0, String v192, String v256, String v448) { StreamCipher salsa = new Salsa20Engine(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); } }
private void salsa20Test1(CipherParameters params, String v0, String v192, String v256, String v448) { StreamCipher salsa = new Salsa20Engine(); 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", v0, buf); } break; case 3: if (!areEqual(buf, Hex.decode(v192))) { mismatch("v192", v192, buf); } break; case 4: if (!areEqual(buf, Hex.decode(v256))) { mismatch("v256", 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); } }