001: package org.bouncycastle.crypto.test;
002:
003: import java.security.SecureRandom;
004:
005: import org.bouncycastle.crypto.engines.DESEngine;
006: import org.bouncycastle.crypto.paddings.BlockCipherPadding;
007: import org.bouncycastle.crypto.paddings.ISO10126d2Padding;
008: import org.bouncycastle.crypto.paddings.ISO7816d4Padding;
009: import org.bouncycastle.crypto.paddings.PKCS7Padding;
010: import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
011: import org.bouncycastle.crypto.paddings.TBCPadding;
012: import org.bouncycastle.crypto.paddings.X923Padding;
013: import org.bouncycastle.crypto.paddings.ZeroBytePadding;
014: import org.bouncycastle.crypto.params.KeyParameter;
015: import org.bouncycastle.util.encoders.Hex;
016: import org.bouncycastle.util.test.SimpleTest;
017:
018: /**
019: * General Padding tests.
020: */
021: public class PaddingTest extends SimpleTest {
022: public PaddingTest() {
023: }
024:
025: private void blockCheck(PaddedBufferedBlockCipher cipher,
026: BlockCipherPadding padding, KeyParameter key, byte[] data) {
027: byte[] out = new byte[data.length + 8];
028: byte[] dec = new byte[data.length];
029:
030: try {
031: cipher.init(true, key);
032:
033: int len = cipher.processBytes(data, 0, data.length, out, 0);
034:
035: len += cipher.doFinal(out, len);
036:
037: cipher.init(false, key);
038:
039: int decLen = cipher.processBytes(out, 0, len, dec, 0);
040:
041: decLen += cipher.doFinal(dec, decLen);
042:
043: if (!areEqual(data, dec)) {
044: fail("failed to decrypt - i = " + data.length
045: + ", padding = " + padding.getPaddingName());
046: }
047: } catch (Exception e) {
048: fail("Exception - " + e.toString(), e);
049: }
050: }
051:
052: public void testPadding(BlockCipherPadding padding,
053: SecureRandom rand, byte[] ffVector, byte[] ZeroVector) {
054: PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
055: new DESEngine(), padding);
056: KeyParameter key = new KeyParameter(Hex
057: .decode("0011223344556677"));
058:
059: //
060: // ff test
061: //
062: byte[] data = { (byte) 0xff, (byte) 0xff, (byte) 0xff,
063: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
064:
065: if (ffVector != null) {
066: padding.addPadding(data, 3);
067:
068: if (!areEqual(data, ffVector)) {
069: fail("failed ff test for " + padding.getPaddingName());
070: }
071: }
072:
073: //
074: // zero test
075: //
076: if (ZeroVector != null) {
077: data = new byte[8];
078: padding.addPadding(data, 4);
079:
080: if (!areEqual(data, ZeroVector)) {
081: fail("failed zero test for " + padding.getPaddingName());
082: }
083: }
084:
085: for (int i = 1; i != 200; i++) {
086: data = new byte[i];
087:
088: rand.nextBytes(data);
089:
090: blockCheck(cipher, padding, key, data);
091: }
092: }
093:
094: public void performTest() {
095: SecureRandom rand = new SecureRandom(new byte[20]);
096:
097: rand.setSeed(System.currentTimeMillis());
098:
099: testPadding(new PKCS7Padding(), rand, Hex
100: .decode("ffffff0505050505"), Hex
101: .decode("0000000004040404"));
102:
103: testPadding(new ISO10126d2Padding(), rand, null, null);
104:
105: testPadding(new X923Padding(), rand, null, null);
106:
107: testPadding(new TBCPadding(), rand, Hex
108: .decode("ffffff0000000000"), Hex
109: .decode("00000000ffffffff"));
110:
111: testPadding(new ZeroBytePadding(), rand, Hex
112: .decode("ffffff0000000000"), null);
113:
114: testPadding(new ISO7816d4Padding(), rand, Hex
115: .decode("ffffff8000000000"), Hex
116: .decode("0000000080000000"));
117: }
118:
119: public String getName() {
120: return "PaddingTest";
121: }
122:
123: public static void main(String[] args) {
124: runTest(new PaddingTest());
125: }
126: }
|