private void testMode(Object cipher, CipherParameters params) throws Exception { testWriteRead(cipher, params, false); testWriteRead(cipher, params, true); testReadWrite(cipher, params, false); testReadWrite(cipher, params, true); if (!(cipher instanceof CTSBlockCipher || cipher instanceof NISTCTSBlockCipher)) { testWriteReadEmpty(cipher, params, false); testWriteReadEmpty(cipher, params, true); } if (cipher instanceof AEADBlockCipher) { testTamperedRead((AEADBlockCipher)cipher, params); testTruncatedRead((AEADBlockCipher)cipher, params); testTamperedWrite((AEADBlockCipher)cipher, params); } }
private void testCTS( int id, BlockCipher cipher, CipherParameters params, byte[] input, byte[] output) throws Exception { byte[] out = new byte[input.length]; BufferedBlockCipher engine = new CTSBlockCipher(cipher); engine.init(true, params); int len = engine.processBytes(input, 0, input.length, out, 0); engine.doFinal(out, len); if (!areEqual(output, out)) { fail("failed encryption expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(out))); } engine.init(false, params); len = engine.processBytes(output, 0, output.length, out, 0); engine.doFinal(out, len); if (!areEqual(input, out)) { fail("failed decryption expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out))); } }
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; }
protected void engineSetPadding( String padding) throws NoSuchPaddingException { String paddingName = Strings.toUpperCase(padding); if (paddingName.equals("NOPADDING")) { if (cipher.wrapOnNoPadding()) { cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(cipher.getUnderlyingCipher())); } } else if (paddingName.equals("WITHCTS")) { cipher = new BufferedGenericBlockCipher(new CTSBlockCipher(cipher.getUnderlyingCipher())); } else { padded = true; if (isAEADModeName(modeName)) { throw new NoSuchPaddingException("Only NoPadding can be used with AEAD modes."); } else if (paddingName.equals("PKCS5PADDING") || paddingName.equals("PKCS7PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher()); } /* else if (paddingName.equals("ZEROBYTEPADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ZeroBytePadding()); } else if (paddingName.equals("ISO10126PADDING") || paddingName.equals("ISO10126-2PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO10126d2Padding()); } else if (paddingName.equals("X9.23PADDING") || paddingName.equals("X923PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new X923Padding()); } else if (paddingName.equals("ISO7816-4PADDING") || paddingName.equals("ISO9797-1PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO7816d4Padding()); } else if (paddingName.equals("TBCPADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new TBCPadding()); } */ else { throw new NoSuchPaddingException("Padding " + padding + " unknown."); } } }
public boolean wrapOnNoPadding() { return !(cipher instanceof CTSBlockCipher); }
protected void engineSetPadding( String padding) throws NoSuchPaddingException { String paddingName = Strings.toUpperCase(padding); if (paddingName.equals("NOPADDING")) { if (cipher.wrapOnNoPadding()) { cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(cipher.getUnderlyingCipher())); } } else if (paddingName.equals("WITHCTS")) { cipher = new BufferedGenericBlockCipher(new CTSBlockCipher(cipher.getUnderlyingCipher())); } else { padded = true; if (isAEADModeName(modeName)) { throw new NoSuchPaddingException("Only NoPadding can be used with AEAD modes."); } else if (paddingName.equals("PKCS5PADDING") || paddingName.equals("PKCS7PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher()); } else if (paddingName.equals("ZEROBYTEPADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ZeroBytePadding()); } else if (paddingName.equals("ISO10126PADDING") || paddingName.equals("ISO10126-2PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO10126d2Padding()); } else if (paddingName.equals("X9.23PADDING") || paddingName.equals("X923PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new X923Padding()); } else if (paddingName.equals("ISO7816-4PADDING") || paddingName.equals("ISO9797-1PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO7816d4Padding()); } else if (paddingName.equals("TBCPADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new TBCPadding()); } else { throw new NoSuchPaddingException("Padding " + padding + " unknown."); } } }
public static BufferedBlockCipher getAesCtsCipher() { BlockCipher engine = new AESEngine(); return new CTSBlockCipher(new CBCBlockCipher(engine)); }