Skip to content

Exception testing for CTS mode #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public class CTSBlockCipher
public CTSBlockCipher(
BlockCipher cipher)
{
if ((cipher instanceof OFBBlockCipher) || (cipher instanceof CFBBlockCipher))
if ((cipher instanceof OFBBlockCipher) || (cipher instanceof CFBBlockCipher) || (cipher instanceof SICBlockCipher))
{
// TODO: This is broken - need to introduce marker interface to differentiate block cipher primitive from mode?
throw new IllegalArgumentException("CTSBlockCipher can only accept ECB, or CBC ciphers");
}

Expand Down Expand Up @@ -239,6 +240,11 @@ public int doFinal(
}
else
{
if (bufOff < blockSize)
{
throw new DataLengthException("need at least one block of input for CTS");
}

byte[] lastBlock = new byte[blockSize];

if (cipher instanceof CBCBlockCipher)
Expand Down
65 changes: 65 additions & 0 deletions core/src/test/java/org/bouncycastle/crypto/test/CTSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.engines.DESEngine;
import org.bouncycastle.crypto.engines.SkipjackEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.modes.CTSBlockCipher;
import org.bouncycastle.crypto.modes.OldCTSBlockCipher;
import org.bouncycastle.crypto.modes.SICBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Hex;
Expand Down Expand Up @@ -94,6 +97,66 @@ private void testOldCTS(
}
}

private void testExceptions() throws InvalidCipherTextException
{
BufferedBlockCipher engine = new CTSBlockCipher(new DESEngine());
CipherParameters params = new KeyParameter(new byte[engine.getBlockSize()]);
engine.init(true, params);

byte[] out = new byte[engine.getOutputSize(engine.getBlockSize())];

engine.processBytes(new byte[engine.getBlockSize() - 1], 0, engine.getBlockSize() - 1, out, 0);
try
{
engine.doFinal(out, 0);
fail("Expected CTS encrypt error on < 1 block input");
} catch(DataLengthException e)
{
// Expected
}

engine.init(true, params);
engine.processBytes(new byte[engine.getBlockSize()], 0, engine.getBlockSize(), out, 0);
try
{
engine.doFinal(out, 0);
} catch(DataLengthException e)
{
fail("Unexpected CTS encrypt error on == 1 block input");
}

engine.init(false, params);
engine.processBytes(new byte[engine.getBlockSize() - 1], 0, engine.getBlockSize() - 1, out, 0);
try
{
engine.doFinal(out, 0);
fail("Expected CTS decrypt error on < 1 block input");
} catch(DataLengthException e)
{
// Expected
}

engine.init(false, params);
engine.processBytes(new byte[engine.getBlockSize()], 0, engine.getBlockSize(), out, 0);
try
{
engine.doFinal(out, 0);
} catch(DataLengthException e)
{
fail("Unexpected CTS decrypt error on == 1 block input");
}

try
{
new CTSBlockCipher(new SICBlockCipher(new AESEngine()));
fail("Expected CTS construction error - only ECB/CBC supported.");
} catch(IllegalArgumentException e)
{
// Expected
}

}

public String getName()
{
return "CTS";
Expand Down Expand Up @@ -135,6 +198,8 @@ public void performTest()

testCTS(7, new CBCBlockCipher(new AESEngine()), new ParametersWithIV(new KeyParameter(aes128), new byte[16]), aes1Block, pstErrata);
testOldCTS(7, new CBCBlockCipher(new AESEngine()), new ParametersWithIV(new KeyParameter(aes128), new byte[16]), aes1Block, preErrata);

testExceptions();
}

public static void main(
Expand Down