@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); } }
@Override public int getIVSize() { int ivSize = this.ivSize; if (ivSize < 0) { final BlockCipher underlyingCipher = delegate.getUnderlyingCipher(); if (underlyingCipher instanceof CFBBlockCipher) ivSize = ((CFBBlockCipher)underlyingCipher).getUnderlyingCipher().getBlockSize(); else if (underlyingCipher instanceof OFBBlockCipher) ivSize = ((OFBBlockCipher)underlyingCipher).getUnderlyingCipher().getBlockSize(); else ivSize = underlyingCipher.getBlockSize(); if (delegate instanceof CCMBlockCipher) --ivSize; this.ivSize = ivSize; } return ivSize; }
private void testNullOFB() throws InvalidCipherTextException { BufferedBlockCipher b = new BufferedBlockCipher(new OFBBlockCipher(new AESEngine(), 128)); KeyParameter kp = new KeyParameter(Hex.decode("5F060D3716B345C253F6749ABAC10917")); b.init(true, new ParametersWithIV(kp, new byte[16])); byte[] out = new byte[b.getOutputSize(tData.length)]; int len = b.processBytes(tData, 0, tData.length, out, 0); len += b.doFinal(out, len); if (!areEqual(outOFB1, out)) { fail("no match on first nullOFB check"); } b.init(true, new ParametersWithIV(null, Hex.decode("000102030405060708090a0b0c0d0e0f"))); len = b.processBytes(tData, 0, tData.length, out, 0); len += b.doFinal(out, len); if (!areEqual(outOFB2, out)) { fail("no match on second nullOFB check"); } }
private void testModes(BlockCipher cipher1, BlockCipher cipher2, int keySize) throws Exception { final KeyParameter key = new KeyParameter(new byte[keySize]); final int blockSize = getBlockSize(cipher1); final CipherParameters withIv = new ParametersWithIV(key, new byte[blockSize]); if (blockSize > 1) { testMode(new PaddedBufferedBlockCipher(cipher1, new PKCS7Padding()), key); testMode(new PaddedBufferedBlockCipher(new CBCBlockCipher(cipher1), new PKCS7Padding()), withIv); testMode(new BufferedBlockCipher(new OFBBlockCipher(cipher1, blockSize)), withIv); testMode(new BufferedBlockCipher(new CFBBlockCipher(cipher1, blockSize)), withIv); testMode(new BufferedBlockCipher(new SICBlockCipher(cipher1)), withIv); } // CTS requires at least one block if (blockSize <= 16 && streamSize >= blockSize) { testMode(new CTSBlockCipher(cipher1), key); } if (blockSize <= 16 && streamSize >= blockSize) { testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS1, cipher1), key); testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS2, cipher1), key); testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS3, cipher1), key); } if (blockSize == 8 || blockSize == 16) { testMode(new EAXBlockCipher(cipher1), withIv); } if (blockSize == 16) { testMode(new CCMBlockCipher(cipher1), new ParametersWithIV(key, new byte[7])); testMode(new GCMBlockCipher(cipher1), withIv); testMode(new OCBBlockCipher(cipher1, cipher2), new ParametersWithIV(key, new byte[15])); } }
@Override public int getIVSize() { if (ivSize < 0) { final String mode = CryptoRegistry.splitTransformation(getTransformation())[1]; if ("".equals(mode) || "ECB".equals(mode)) ivSize = 0; // No block cipher mode (i.e. ECB) => no IV. else { if (delegate instanceof CTSBlockCipher) { final CTSBlockCipher cts = (CTSBlockCipher) delegate; if (cts.getUnderlyingCipher() instanceof CBCBlockCipher) ivSize = cts.getUnderlyingCipher().getBlockSize(); else ivSize = 0; } else { final BlockCipher underlyingCipher = delegate.getUnderlyingCipher(); if (underlyingCipher instanceof CFBBlockCipher) ivSize = ((CFBBlockCipher)underlyingCipher).getUnderlyingCipher().getBlockSize(); else if (underlyingCipher instanceof OFBBlockCipher) ivSize = ((OFBBlockCipher)underlyingCipher).getUnderlyingCipher().getBlockSize(); else ivSize = underlyingCipher.getBlockSize(); } } } return ivSize; }
public Twofish_OFB8() { super(new OFBBlockCipher(new TwofishEngine(), 8), 128); }
public DES_OFB8() { super(new OFBBlockCipher(new DESEngine(), 8), 64); }
public DESede_OFB8() { super(new OFBBlockCipher(new DESedeEngine(), 8), 64); }
public Skipjack_OFB8() { super(new OFBBlockCipher(new SkipjackEngine(), 8), 64); }
public Blowfish_OFB8() { super(new OFBBlockCipher(new BlowfishEngine(), 8), 64); }
public OFB() { super(new BufferedBlockCipher(new OFBBlockCipher(new RC6Engine(), 128)), 128); }
public OFB() { super(new BufferedBlockCipher(new OFBBlockCipher(new AESFastEngine(), 128)), 128); }
public void performTest() throws Exception { // 128 bit block ciphers testReset("AESFastEngine", new AESFastEngine(), new AESFastEngine(), new KeyParameter(new byte[16])); testReset("AESEngine", new AESEngine(), new AESEngine(), new KeyParameter(new byte[16])); testReset("AESLightEngine", new AESLightEngine(), new AESLightEngine(), new KeyParameter(new byte[16])); testReset("Twofish", new TwofishEngine(), new TwofishEngine(), new KeyParameter(new byte[16])); testReset("NoekeonEngine", new NoekeonEngine(), new NoekeonEngine(), new KeyParameter(new byte[16])); testReset("SerpentEngine", new SerpentEngine(), new SerpentEngine(), new KeyParameter(new byte[16])); testReset("SEEDEngine", new SEEDEngine(), new SEEDEngine(), new KeyParameter(new byte[16])); testReset("CAST6Engine", new CAST6Engine(), new CAST6Engine(), new KeyParameter(new byte[16])); testReset("RC6Engine", new RC6Engine(), new RC6Engine(), new KeyParameter(new byte[16])); // 64 bit block ciphers testReset("DESEngine", new DESEngine(), new DESEngine(), new KeyParameter(new byte[8])); testReset("BlowfishEngine", new BlowfishEngine(), new BlowfishEngine(), new KeyParameter(new byte[8])); testReset("CAST5Engine", new CAST5Engine(), new CAST5Engine(), new KeyParameter(new byte[8])); testReset("DESedeEngine", new DESedeEngine(), new DESedeEngine(), new KeyParameter(new byte[24])); testReset("TEAEngine", new TEAEngine(), new TEAEngine(), new KeyParameter(new byte[16])); testReset("XTEAEngine", new XTEAEngine(), new XTEAEngine(), new KeyParameter(new byte[16])); // primitive block cipher modes (don't reset on processBlock) testModeReset("AES/CBC", new CBCBlockCipher(new AESEngine()), new CBCBlockCipher(new AESEngine()), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testModeReset("AES/SIC", new SICBlockCipher(new AESEngine()), new SICBlockCipher(new AESEngine()), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testModeReset("AES/CFB", new CFBBlockCipher(new AESEngine(), 128), new CFBBlockCipher(new AESEngine(), 128), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testModeReset("AES/OFB", new OFBBlockCipher(new AESEngine(), 128), new OFBBlockCipher(new AESEngine(), 128), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testModeReset("AES/GCTR", new GOFBBlockCipher(new DESEngine()), new GOFBBlockCipher(new DESEngine()), new ParametersWithIV(new KeyParameter(new byte[8]), new byte[8])); testModeReset("AES/OpenPGPCFB", new OpenPGPCFBBlockCipher(new AESEngine()), new OpenPGPCFBBlockCipher( new AESEngine()), new KeyParameter(new byte[16])); testModeReset("AES/PGPCFB", new PGPCFBBlockCipher(new AESEngine(), false), new PGPCFBBlockCipher( new AESEngine(), false), new KeyParameter(new byte[16])); // PGPCFB with IV is broken (it's also not a PRP, so probably shouldn't be a BlockCipher) // testModeReset("AES/PGPCFBwithIV", new PGPCFBBlockCipher(new AESEngine(), true), new // PGPCFBBlockCipher( // new AESEngine(), true), new ParametersWithIV(new KeyParameter(new byte[16]), new // byte[16])); // testModeReset("AES/PGPCFBwithIV_NoIV", new PGPCFBBlockCipher(new AESEngine(), true), new // PGPCFBBlockCipher( // new AESEngine(), true), new KeyParameter(new byte[16])); }
public TestResult perform() { KeyParameter key = new KeyParameter(Hex.decode("0011223344556677")); byte[] input = Hex.decode("4e6f7720"); byte[] out1 = new byte[4]; byte[] out2 = new byte[4]; BlockCipher ofb = new OFBBlockCipher(new DESEngine(), 32); ofb.init(true, new ParametersWithIV(key, Hex.decode("1122334455667788"))); ofb.processBlock(input, 0, out1, 0); ofb.init(false, new ParametersWithIV(key, Hex.decode("1122334455667788"))); ofb.processBlock(out1, 0, out2, 0); if (!isEqualTo(out2, input)) { return new SimpleTestResult(false, getName() + ": test 1 - in != out"); } ofb.init(true, new ParametersWithIV(key, Hex.decode("11223344"))); ofb.processBlock(input, 0, out1, 0); ofb.init(false, new ParametersWithIV(key, Hex.decode("0000000011223344"))); ofb.processBlock(out1, 0, out2, 0); if (!isEqualTo(out2, input)) { return new SimpleTestResult(false, getName() + ": test 2 - in != out"); } BlockCipher cfb = new CFBBlockCipher(new DESEngine(), 32); cfb.init(true, new ParametersWithIV(key, Hex.decode("1122334455667788"))); cfb.processBlock(input, 0, out1, 0); cfb.init(false, new ParametersWithIV(key, Hex.decode("1122334455667788"))); cfb.processBlock(out1, 0, out2, 0); if (!isEqualTo(out2, input)) { return new SimpleTestResult(false, getName() + ": test 3 - in != out"); } cfb.init(true, new ParametersWithIV(key, Hex.decode("11223344"))); cfb.processBlock(input, 0, out1, 0); cfb.init(false, new ParametersWithIV(key, Hex.decode("0000000011223344"))); cfb.processBlock(out1, 0, out2, 0); if (!isEqualTo(out2, input)) { return new SimpleTestResult(false, getName() + ": test 4 - in != out"); } return new SimpleTestResult(true, getName() + ": Okay"); }
private static void initBlockCipherModes() { blockCipherMode.put("CBC", CBCBlockCipher.class); blockCipherMode.put("CFB", CFBBlockCipher.class); blockCipherMode.put("OFB", OFBBlockCipher.class); blockCipherMode.put("CTR", SICBlockCipher.class); }
public OFB() { super(new BufferedBlockCipher(new OFBBlockCipher(new SerpentEngine(), 128)), 128); }
@Override public void performTest() throws Exception { // 128 bit block ciphers testReset("AESFastEngine", new AESFastEngine(), new AESFastEngine(), new KeyParameter(new byte[16])); testReset("AESEngine", new AESEngine(), new AESEngine(), new KeyParameter(new byte[16])); testReset("AESLightEngine", new AESLightEngine(), new AESLightEngine(), new KeyParameter(new byte[16])); testReset("Twofish", new TwofishEngine(), new TwofishEngine(), new KeyParameter(new byte[16])); testReset("NoekeonEngine", new NoekeonEngine(), new NoekeonEngine(), new KeyParameter(new byte[16])); testReset("SerpentEngine", new SerpentEngine(), new SerpentEngine(), new KeyParameter(new byte[16])); testReset("SEEDEngine", new SEEDEngine(), new SEEDEngine(), new KeyParameter(new byte[16])); testReset("CAST6Engine", new CAST6Engine(), new CAST6Engine(), new KeyParameter(new byte[16])); testReset("RC6Engine", new RC6Engine(), new RC6Engine(), new KeyParameter(new byte[16])); // 64 bit block ciphers testReset("DESEngine", new DESEngine(), new DESEngine(), new KeyParameter(new byte[8])); testReset("BlowfishEngine", new BlowfishEngine(), new BlowfishEngine(), new KeyParameter(new byte[8])); testReset("CAST5Engine", new CAST5Engine(), new CAST5Engine(), new KeyParameter(new byte[8])); testReset("DESedeEngine", new DESedeEngine(), new DESedeEngine(), new KeyParameter(new byte[24])); testReset("TEAEngine", new TEAEngine(), new TEAEngine(), new KeyParameter(new byte[16])); testReset("XTEAEngine", new XTEAEngine(), new XTEAEngine(), new KeyParameter(new byte[16])); // primitive block cipher modes (don't reset on processBlock) testModeReset("AES/CBC", new CBCBlockCipher(new AESEngine()), new CBCBlockCipher(new AESEngine()), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testModeReset("AES/SIC", new SICBlockCipher(new AESEngine()), new SICBlockCipher(new AESEngine()), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testModeReset("AES/CFB", new CFBBlockCipher(new AESEngine(), 128), new CFBBlockCipher(new AESEngine(), 128), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testModeReset("AES/OFB", new OFBBlockCipher(new AESEngine(), 128), new OFBBlockCipher(new AESEngine(), 128), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testModeReset("AES/GCTR", new GOFBBlockCipher(new DESEngine()), new GOFBBlockCipher(new DESEngine()), new ParametersWithIV(new KeyParameter(new byte[8]), new byte[8])); testModeReset("AES/OpenPGPCFB", new OpenPGPCFBBlockCipher(new AESEngine()), new OpenPGPCFBBlockCipher( new AESEngine()), new KeyParameter(new byte[16])); testModeReset("AES/PGPCFB", new PGPCFBBlockCipher(new AESEngine(), false), new PGPCFBBlockCipher( new AESEngine(), false), new KeyParameter(new byte[16])); // PGPCFB with IV is broken (it's also not a PRP, so probably shouldn't be a BlockCipher) // testModeReset("AES/PGPCFBwithIV", new PGPCFBBlockCipher(new AESEngine(), true), new // PGPCFBBlockCipher( // new AESEngine(), true), new ParametersWithIV(new KeyParameter(new byte[16]), new // byte[16])); // testModeReset("AES/PGPCFBwithIV_NoIV", new PGPCFBBlockCipher(new AESEngine(), true), new // PGPCFBBlockCipher( // new AESEngine(), true), new KeyParameter(new byte[16])); }