diff options
author | Raif S. Naffah <raif@swiftdsl.com.au> | 2006-10-22 16:13:04 +0000 |
---|---|---|
committer | Raif S. Naffah <raif@swiftdsl.com.au> | 2006-10-22 16:13:04 +0000 |
commit | 3ed866282715db219e0ad64952d5202611eee093 (patch) | |
tree | ed9ec254468bf1ea0521cc2ff12db1750e826478 /gnu/javax/crypto/jce | |
parent | 24f2a92b3b3e6f971a2deca20b5d9fff125a3b1c (diff) | |
download | classpath-3ed866282715db219e0ad64952d5202611eee093.tar.gz |
2006-10-23 Marco Trudel <mtrudel@gmx.ch>
* gnu/javax/crypto/pad/PKCS7.java (unpad): Removed an unnecessary test.
* javax/crypto/CipherOutputStream.java: Re-implemented.
* gnu/javax/crypto/jce/cipher/CipherAdapter.java
(engineUpdate(byte[], int, int)): Always keep data for unpadding in padded
decryption mode and check if it is a complete block.
(engineUpdate(byte[], int, int, byte[], int)): Likewise.
(engineDoFinal(byte[], int, int)): In padded decryption mode, take
partially processed data into account.
Diffstat (limited to 'gnu/javax/crypto/jce')
-rw-r--r-- | gnu/javax/crypto/jce/cipher/CipherAdapter.java | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/gnu/javax/crypto/jce/cipher/CipherAdapter.java b/gnu/javax/crypto/jce/cipher/CipherAdapter.java index b7cd755f5..0863b1f1c 100644 --- a/gnu/javax/crypto/jce/cipher/CipherAdapter.java +++ b/gnu/javax/crypto/jce/cipher/CipherAdapter.java @@ -373,14 +373,24 @@ class CipherAdapter engineInit(opmode, key, spec, random); } - protected byte[] engineUpdate(byte[] input, int off, int len) + protected byte[] engineUpdate(byte[] input, int inOff, int inLen) { + if (inLen == 0) // nothing to process + return new byte[0]; final int blockSize = mode.currentBlockSize(); - final int count = (partLen + len) / blockSize; - final byte[] out = new byte[count * blockSize]; + int blockCount = (partLen + inLen) / blockSize; + + // always keep data for unpadding in padded decryption mode; + // might even be a complete block + if (pad != null + && ((Integer) attributes.get(IMode.STATE)).intValue() == IMode.DECRYPTION + && (partLen + inLen) % blockSize == 0) + blockCount--; + + final byte[] out = new byte[blockCount * blockSize]; try { - engineUpdate(input, off, len, out, 0); + engineUpdate(input, inOff, inLen, out, 0); } catch (ShortBufferException x) // should not happen { @@ -395,7 +405,15 @@ class CipherAdapter if (inLen == 0) // nothing to process return 0; final int blockSize = mode.currentBlockSize(); - final int blockCount = (partLen + inLen) / blockSize; + int blockCount = (partLen + inLen) / blockSize; + + // always keep data for unpadding in padded decryption mode; + // might even be a complete block + if (pad != null + && ((Integer) attributes.get(IMode.STATE)).intValue() == IMode.DECRYPTION + && (partLen + inLen) % blockSize == 0) + blockCount--; + final int result = blockCount * blockSize; if (result > out.length - outOff) throw new ShortBufferException(); @@ -447,16 +465,21 @@ class CipherAdapter break; case IMode.DECRYPTION: int padLen; + byte[] buf3 = new byte[buf.length + partLen]; try { - padLen = pad.unpad(buf, 0, buf.length); + if (partLen != mode.currentBlockSize()) + throw new WrongPaddingException(); + System.arraycopy(buf, 0, buf3, 0, buf.length); + mode.update(partBlock, 0, buf3, buf.length); + padLen = pad.unpad(buf3, 0, buf3.length); } catch (WrongPaddingException wpe) { throw new BadPaddingException(wpe.getMessage()); } - result = new byte[buf.length - padLen]; - System.arraycopy(buf, 0, result, 0, result.length); + result = new byte[buf3.length - padLen]; + System.arraycopy(buf3, 0, result, 0, result.length); break; default: throw new IllegalStateException(); |