@Test public void testPaddingCheck() throws Exception { int padding = aLength; while (padding > 256) { padding >>>= 1; } byte[] data = Arrays.copyOf(payloadData, payloadLength + padding + 256); for (int index = payloadLength; index <= payloadLength + padding; ++index) { data[index] = (byte) padding; } assertTrue(CbcBlockCipher.checkPadding(padding, data, payloadLength)); if (payloadLength > 0) { data[payloadLength - 1] ^= 0x55; assertTrue(CbcBlockCipher.checkPadding(padding, data, payloadLength)); } data[payloadLength + padding + 1] ^= 0x55; assertTrue(CbcBlockCipher.checkPadding(padding, data, payloadLength)); byte[] broken = Arrays.copyOf(data, data.length); broken[payloadLength] ^= 0x55; assertFalse(CbcBlockCipher.checkPadding(padding, broken, payloadLength)); broken = Arrays.copyOf(data, data.length); broken[payloadLength + padding] ^= 0x55; assertFalse(CbcBlockCipher.checkPadding(padding, broken, payloadLength)); }
public static boolean checkPadding(int padding, byte[] data, int offset) { if (data.length < offset + 257) { throw new IllegalArgumentException("data must contain 257 bytes from offset on!"); } byte result1 = 0; byte result2 = 0; byte pad = (byte) padding; for (int index = 0; index <= padding; ++index) { result1 |= pad ^ data[offset + index]; } for (int index = padding + 1; index < 256; ++index) { result2 |= pad ^ data[offset + index]; } data[data.length - 1] ^= result2; return result1 == 0; }
CbcBlockCipher { public static boolean checkPadding(int padding, byte[] data, int offset) { if (data.length < offset + 257) { throw new IllegalArgumentException("data must contain 257 bytes from offset on!"); } byte result1 = 0; byte result2 = 0; byte pad = (byte) padding; for (int index = 0; index <= padding; ++index) { result1 |= pad ^ data[offset + index]; } for (int index = padding + 1; index < 256; ++index) { result2 |= pad ^ data[offset + index]; } data[data.length - 1] ^= result2; return result1 == 0; } }
CbcBlockCipher { public static boolean checkPadding(int padding, byte[] data, int offset) { if (data.length < offset + 257) { throw new IllegalArgumentException("data must contain 257 bytes from offset on!"); } byte result1 = 0; byte result2 = 0; byte pad = (byte) padding; for (int index = 0; index <= padding; ++index) { result1 |= pad ^ data[offset + index]; } for (int index = padding + 1; index < 256; ++index) { result2 |= pad ^ data[offset + index]; } data[data.length - 1] ^= result2; return result1 == 0; }  }
CbcBlockCipher { public static boolean checkPadding(int padding, byte[] data, int offset) { if (data.length < offset + 257) { throw new IllegalArgumentException("data must contain 257 bytes from offset on!"); } byte result1 = 0; byte result2 = 0; byte pad = (byte) padding; for (int index = 0; index <= padding; ++index) { result1 |= pad ^ data[offset + index]; } for (int index = padding + 1; index < 256; ++index) { result2 |= pad ^ data[offset + index]; } data[data.length - 1] ^= result2; return result1 == 0; }  static byte[] decrypt(CipherSuite suite, SecretKey key, SecretKey macKey, byte[] additionalData,
			byte[] ciphertext); static byte[] encrypt(CipherSuite suite, SecretKey key, SecretKey macKey, byte[] additionalData,
			byte[] payload); static byte[] getBlockCipherMac(Mac hmac, SecretKey macKey, byte[] additionalData, byte[] content,
			int length); static boolean checkPadding(int padding, byte[] data, int offset); }
CbcBlockCipher { public static boolean checkPadding(int padding, byte[] data, int offset) { if (data.length < offset + 257) { throw new IllegalArgumentException("data must contain 257 bytes from offset on!"); } byte result1 = 0; byte result2 = 0; byte pad = (byte) padding; for (int index = 0; index <= padding; ++index) { result1 |= pad ^ data[offset + index]; } for (int index = padding + 1; index < 256; ++index) { result2 |= pad ^ data[offset + index]; } data[data.length - 1] ^= result2; return result1 == 0; }  static byte[] decrypt(CipherSuite suite, SecretKey key, SecretKey macKey, byte[] additionalData,
			byte[] ciphertext); static byte[] encrypt(CipherSuite suite, SecretKey key, SecretKey macKey, byte[] additionalData,
			byte[] payload); static byte[] getBlockCipherMac(Mac hmac, SecretKey macKey, byte[] additionalData, byte[] content,
			int length); static boolean checkPadding(int padding, byte[] data, int offset);  }