public void testPadding(TestHarness harness)
{
harness.checkPoint("testPadding");
String padName = null;
IMode gnu = ModeFactory.getInstance("ECB", "AES", 16);
IPad pad;
Cipher jce;
byte[] kb = new byte[32];
for (int i = 0; i < kb.length; i++)
kb[i] = (byte) i;
byte[] pt = new byte[42];
for (int i = 0; i < pt.length; i++)
pt[i] = (byte) i;
byte[] ppt = new byte[48]; // padded plaintext.
System.arraycopy(pt, 0, ppt, 0, 42);
byte[] ct1 = new byte[48], ct2 = new byte[48];
byte[] cpt1 = new byte[42], cpt2 = new byte[42];
HashMap attrib = new HashMap();
attrib.put(IBlockCipher.KEY_MATERIAL, kb);
try
{
for (Iterator it = PadFactory.getNames().iterator(); it.hasNext();)
{
padName = (String) it.next();
// skip EME-PKCS1-V1.5 padding since it's not a true block cipher
// padding algorithm
if (padName.equalsIgnoreCase(Registry.EME_PKCS1_V1_5_PAD))
continue;
pad = PadFactory.getInstance(padName);
pad.reset();
pad.init(16);
byte[] padding = pad.pad(pt, 0, pt.length);
System.arraycopy(padding, 0, ppt, 42, padding.length);
attrib.put(IMode.STATE, new Integer(IMode.ENCRYPTION));
gnu.reset();
gnu.init(attrib);
for (int i = 0; i < ppt.length; i += 16)
gnu.update(ppt, i, ct1, i);
jce = Cipher.getInstance("AES/ECB/" + padName, Registry.GNU_CRYPTO);
jce.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kb, "AES"));
jce.doFinal(ct1, 0, ct1.length, cpt1, 0);
harness.check(Arrays.equals(pt, cpt1),
"testPadding(" + padName + ")");
jce.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kb, "AES"));
jce.doFinal(pt, 0, pt.length, ct2, 0);
attrib.put(IMode.STATE, new Integer(IMode.DECRYPTION));
gnu.reset();
gnu.init(attrib);
byte[] pcpt = new byte[48];
for (int i = 0; i < ct2.length; i += 16)
gnu.update(ct2, i, pcpt, i);
int trim = pad.unpad(pcpt, 0, pcpt.length);
System.arraycopy(pcpt, 0, cpt2, 0, pcpt.length - trim);
harness.check(Arrays.equals(pt, cpt2),
"testPadding(" + padName + ")");
}