@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); } }
public void encrypt(byte[] passiveCheckBytes, byte[] initVector, String password) { RijndaelEngine engine = new RijndaelEngine(_keyByteLength * 8); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(engine, 8), new ZeroBytePadding()); try { byte[] sessionKey = new byte[_keyByteLength]; byte[] passwordBytes = password.getBytes("US-ASCII"); System.arraycopy(passwordBytes, 0, sessionKey, 0, Math.min(_keyByteLength, passwordBytes.length)); byte[] iv = new byte[_keyByteLength]; System.arraycopy(initVector, 0, iv, 0, Math.min(_keyByteLength, initVector.length)); cipher.init(true, new ParametersWithIV(new KeyParameter(sessionKey), iv)); byte[] cipherText = new byte[cipher.getOutputSize(passiveCheckBytes.length)]; int cipherLength = cipher.processBytes(passiveCheckBytes, 0, passiveCheckBytes.length, cipherText, 0); cipherLength = cipherLength + cipher.doFinal(cipherText, cipherLength); int bytesToCopy = Math.min(passiveCheckBytes.length, cipherLength); System.arraycopy(cipherText, 0, passiveCheckBytes, 0, bytesToCopy); } catch (Exception e) { throw new RuntimeException(e); } }
@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; }
public static BufferedBlockCipher createSymmetricKeyWrapper(boolean forEncryption, BlockCipher engine, byte[] key, byte[] iv) { BufferedBlockCipher c = new BufferedBlockCipher(new CFBBlockCipher(engine, engine.getBlockSize() * 8)); c.init(forEncryption, new ParametersWithIV(new KeyParameter(key), iv)); return c; }
private void testNullCFB() throws InvalidCipherTextException { BufferedBlockCipher b = new BufferedBlockCipher(new CFBBlockCipher(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(outCFB1, out)) { fail("no match on first nullCFB 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(outCFB2, out)) { fail("no match on second nullCFB 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 void encrypt(final byte[] passiveCheckBytes, final byte[] initVector, final String password) { final BlowfishEngine engine = new BlowfishEngine(); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(engine, 8), new ZeroBytePadding()); try { final byte[] passwordBytes = password.getBytes("US-ASCII"); assertValidPasswordBytesLength(passwordBytes); final byte[] sessionKey = new byte[KEY_BYTES_LENGTH]; System.arraycopy(passwordBytes, 0, sessionKey, 0, Math.min(KEY_BYTES_LENGTH, passwordBytes.length)); final byte[] iv = new byte[KEY_BYTES_LENGTH]; System.arraycopy(initVector, 0, iv, 0, Math.min(KEY_BYTES_LENGTH, initVector.length)); cipher.init(true, new ParametersWithIV(new KeyParameter(sessionKey), iv)); final byte[] cipherText = new byte[cipher.getOutputSize(passiveCheckBytes.length)]; int cipherLength = cipher.processBytes(passiveCheckBytes, 0, passiveCheckBytes.length, cipherText, 0); cipherLength = cipherLength + cipher.doFinal(cipherText, cipherLength); final int bytesToCopy = Math.min(passiveCheckBytes.length, cipherLength); System.arraycopy(cipherText, 0, passiveCheckBytes, 0, bytesToCopy); } catch (Exception e) { throw new RuntimeException(e); } }
@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; }
/** * Create a new BufferedBlockCipher instance */ private static BufferedBlockCipher createBufferedBlockCipher(boolean par0, Key par1Key) { BufferedBlockCipher bufferedblockcipher = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8)); bufferedblockcipher.init(par0, new ParametersWithIV(new KeyParameter(par1Key.getEncoded()), par1Key.getEncoded(), 0, 16)); return bufferedblockcipher; }
/** * Create a new BufferedBlockCipher instance */ private static BufferedBlockCipher createBufferedBlockCipher(boolean par0, Key par1Key) { BufferedBlockCipher var2 = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8)); var2.init(par0, new ParametersWithIV(new KeyParameter(par1Key.getEncoded()), par1Key.getEncoded(), 0, 16)); return var2; }
public Twofish_CFB8() { super(new CFBBlockCipher(new TwofishEngine(), 8), 128); }
public DES_CFB8() { super(new CFBBlockCipher(new DESEngine(), 8), 64); }
public DESede_CFB8() { super(new CFBBlockCipher(new DESedeEngine(), 8), 64); }
public Skipjack_CFB8() { super(new CFBBlockCipher(new SkipjackEngine(), 8), 64); }
public Blowfish_CFB8() { super(new CFBBlockCipher(new BlowfishEngine(), 8), 64); }
public CFB() { super(new BufferedBlockCipher(new CFBBlockCipher(new RC6Engine(), 128)), 128); }
public CFB() { super(new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 128)), 128); }
private BufferedBlockCipher createCipher(boolean forEncryption, byte[] key, byte[] iv) { BufferedBlockCipher cipher = new BufferedBlockCipher(new CFBBlockCipher(new AESEngine(), 8)); cipher.init(forEncryption, new ParametersWithIV(new KeyParameter(key), iv)); return cipher; }
public CipherWorker newWorker() { AESEngine engine = new AESEngine(); CFBBlockCipher core = new CFBBlockCipher(engine, getIVLength() * 8); return new StreamCipherWorker(core); }
@Override protected StreamCipher getCipher() { return new CFBBlockCipher(new DESEngine(), 64); }
@Override protected StreamCipher getCipher() { return new CFBBlockCipher(new CamelliaEngine(), 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); }
private BufferedBlockCipher createCipher( boolean encryptor, byte[] key, byte[] iv ) { BufferedBlockCipher cipher = new BufferedBlockCipher( new CFBBlockCipher( new AESFastEngine(), 8 ) ); cipher.init( encryptor, new ParametersWithIV( new KeyParameter( key ), iv ) ); return cipher; }
public ByteString eciesDecrypt(PrivateKey recipientPrivateKey, ByteString cipherText) { BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) recipientPrivateKey; ECNamedCurveSpec ecNamedCurveSpec = (ECNamedCurveSpec) bcecPrivateKey.getParams(); int level = SecurityLevel.from(ecNamedCurveSpec.getName()).size(); //cipherText = ephemeralPubKeyBytes + encryptedTokBytes + macBytes //ephemeralPubKeyBytes = first ((384+7)/8)*2 + 1 bytes = first 97 bytes //hmac is sha3_384 = 48 bytes or sha3_256 = 32 bytes int ephemeralPubKeyLength = ((level + 7) / 8) * 2 + 1; int hmacLength = level >> 3; int cipherTextLength = cipherText.size(); if (cipherTextLength <= ephemeralPubKeyLength + hmacLength) throw new RuntimeException(String.format("Illegal cipherText length: %d must be > %d", cipherTextLength, ephemeralPubKeyLength + hmacLength)); ByteString ephemeralPubKey = cipherText.substring(0, ephemeralPubKeyLength); ByteString encryptedContent = cipherText.substring(ephemeralPubKeyLength, cipherTextLength - hmacLength); ByteString hmac = cipherText.substring(cipherTextLength - hmacLength); ECPrivateKeyParameters ecdhPrivateKeyParameters; try { ecdhPrivateKeyParameters = (ECPrivateKeyParameters) (PrivateKeyFactory.createKey(bcecPrivateKey.getEncoded())); } catch (IOException e) { logger.error("ECIES decrypt load private key exception", e); throw new RuntimeException(e); } ECDomainParameters ecDomainParameters = ecdhPrivateKeyParameters.getParameters(); ECCurve ecCurve = ecDomainParameters.getCurve(); ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(ecCurve.decodePoint(ephemeralPubKey.toByteArray()), ecDomainParameters); BasicAgreement agree = new ECDHBasicAgreement(); agree.init(ecdhPrivateKeyParameters); byte[] keyAgreement = agree.calculateAgreement(ecPublicKeyParameters).toByteArray(); HKDFParameters hkdfParameters = new HKDFParameters(keyAgreement, null, null); HKDFBytesGenerator hkdfBytesGenerator = new HKDFBytesGenerator(digest); hkdfBytesGenerator.init(hkdfParameters); byte[] hkdfOutputBytes = new byte[AESKEY_LENGTH + HMACKEY_LENGTH]; hkdfBytesGenerator.generateBytes(hkdfOutputBytes, 0, AESKEY_LENGTH + HMACKEY_LENGTH); ByteString hkdfOutput = ByteString.copyFrom(hkdfOutputBytes); ByteString aesKey = hkdfOutput.substring(0, AESKEY_LENGTH); ByteString hmacKey = hkdfOutput.substring(AESKEY_LENGTH, AESKEY_LENGTH + HMACKEY_LENGTH); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(hmacKey.toByteArray())); hMac.update(encryptedContent.toByteArray(), 0, encryptedContent.size()); byte[] recoveredHmac = new byte[hMac.getMacSize()]; hMac.doFinal(recoveredHmac, 0); if (!MessageDigest.isEqual(hmac.toByteArray(), recoveredHmac)) { throw new RuntimeException("HMAC verify failed"); } CFBBlockCipher aesCipher = new CFBBlockCipher( new AESEngine(), BLOCK_BIT_SIZE); ByteString iv = encryptedContent.substring(0, IV_LENGTH); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(aesKey.toByteArray()), iv.toByteArray()); aesCipher.init(false, ivAndKey); byte[] decryptedBytes = new byte[500]; aesCipher.decryptBlock(encryptedContent.substring(IV_LENGTH).toByteArray(), 0, decryptedBytes, 0); return ByteString.copyFrom(decryptedBytes); }
private BufferedBlockCipher createCipher( boolean encryptor, byte[] key, byte[] iv ) { BufferedBlockCipher cipher = new BufferedBlockCipher( new CFBBlockCipher( new RijndaelEngine( 128 ), 8 ) ); cipher.init( encryptor, new ParametersWithIV( new KeyParameter( key ), iv ) ); return cipher; }
public CFB() { super(new BufferedBlockCipher(new CFBBlockCipher(new SerpentEngine(), 128)), 128); }
@Override public BlockCipher initChainingMode(BlockCipher baseCipher) { return new CFBBlockCipher(baseCipher, 8); }