Repository: commons-crypto Updated Branches: refs/heads/master c66387b15 -> e278580f5
CRYPTO-87 Reduce util package API footprint calculateIV is only used by CRTCrypto classes Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/e278580f Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/e278580f Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/e278580f Branch: refs/heads/master Commit: e278580f5aa93ba385829a5dedadd06296e4df1f Parents: c66387b Author: Sebb <s...@apache.org> Authored: Sat Jun 25 13:37:06 2016 +0100 Committer: Sebb <s...@apache.org> Committed: Sat Jun 25 13:37:06 2016 +0100 ---------------------------------------------------------------------- .../crypto/stream/CTRCryptoInputStream.java | 51 +++++++++++++++++++- .../crypto/stream/CTRCryptoOutputStream.java | 2 +- .../stream/PositionedCryptoInputStream.java | 2 +- .../org/apache/commons/crypto/utils/Utils.java | 49 ------------------- 4 files changed, 52 insertions(+), 52 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/e278580f/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java b/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java index c7fbf32..6c6dacc 100644 --- a/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java +++ b/src/main/java/org/apache/commons/crypto/stream/CTRCryptoInputStream.java @@ -80,6 +80,14 @@ public class CTRCryptoInputStream extends CryptoInputStream { private boolean cipherReset = false; /** + * For AES, the algorithm block is fixed size of 128 bits. + * + * @see <a href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard"> + * http://en.wikipedia.org/wiki/Advanced_Encryption_Standard</a> + */ + private static final int AES_BLOCK_SIZE = 16; + + /** * Constructs a {@link CTRCryptoInputStream}. * * @param props The <code>Properties</code> class represents a set of @@ -565,7 +573,7 @@ public class CTRCryptoInputStream extends CryptoInputStream { */ protected void resetCipher(long position) throws IOException { final long counter = getCounter(position); - Utils.calculateIV(initIV, counter, iv); + CTRCryptoInputStream.calculateIV(initIV, counter, iv); try { cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); } catch (InvalidKeyException e) { @@ -620,4 +628,45 @@ public class CTRCryptoInputStream extends CryptoInputStream { throw new IOException(e); } } + + /** + * <p> + * This method is only for Counter (CTR) mode. Generally the CryptoCipher + * calculates the IV and maintain encryption context internally.For example + * a Cipher will maintain its encryption context internally when we do + * encryption/decryption using the CryptoCipher#update interface. + * </p> + * <p> + * Encryption/Decryption is not always on the entire file. For example, in + * Hadoop, a node may only decrypt a portion of a file (i.e. a split). In + * these situations, the counter is derived from the file position. + * </p> + * The IV can be calculated by combining the initial IV and the counter with + * a lossless operation (concatenation, addition, or XOR). + * + * @see <a + * href="http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29"> + * http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29</a> + * + * @param initIV initial IV + * @param counter counter for input stream position + * @param IV the IV for input stream position + */ + static void calculateIV(byte[] initIV, long counter, byte[] IV) { + Utils.checkArgument(initIV.length == CTRCryptoInputStream.AES_BLOCK_SIZE); + Utils.checkArgument(IV.length == CTRCryptoInputStream.AES_BLOCK_SIZE); + + int i = IV.length; // IV length + int j = 0; // counter bytes index + int sum = 0; + while (i-- > 0) { + // (sum >>> Byte.SIZE) is the carry for addition + sum = (initIV[i] & 0xff) + (sum >>> Byte.SIZE); // NOPMD + if (j++ < 8) { // Big-endian, and long is 8 bytes length + sum += (byte) counter & 0xff; + counter >>>= 8; + } + IV[i] = (byte) sum; + } + } } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/e278580f/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java b/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java index 6c54715..6f9ea6b 100644 --- a/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java +++ b/src/main/java/org/apache/commons/crypto/stream/CTRCryptoOutputStream.java @@ -325,7 +325,7 @@ public class CTRCryptoOutputStream extends CryptoOutputStream { padding = (byte) (streamOffset % cipher.getBlockSize()); inBuffer.position(padding); // Set proper position for input data. - Utils.calculateIV(initIV, counter, iv); + CTRCryptoInputStream.calculateIV(initIV, counter, iv); try { cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); } catch (InvalidKeyException e) { http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/e278580f/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java index eddae71..983769c 100644 --- a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java +++ b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java @@ -296,7 +296,7 @@ public class PositionedCryptoInputStream extends CTRCryptoInputStream { private void resetCipher(CipherState state, long position, byte[] iv) throws IOException { final long counter = getCounter(position); - Utils.calculateIV(getInitIV(), counter, iv); + CTRCryptoInputStream.calculateIV(getInitIV(), counter, iv); try { state.getCipher().init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/e278580f/src/main/java/org/apache/commons/crypto/utils/Utils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/utils/Utils.java b/src/main/java/org/apache/commons/crypto/utils/Utils.java index 3862fda..9aeb8a8 100644 --- a/src/main/java/org/apache/commons/crypto/utils/Utils.java +++ b/src/main/java/org/apache/commons/crypto/utils/Utils.java @@ -35,14 +35,6 @@ import org.apache.commons.crypto.conf.ConfigurationKeys; public final class Utils { /** - * For AES, the algorithm block is fixed size of 128 bits. - * - * @see <a href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard"> - * http://en.wikipedia.org/wiki/Advanced_Encryption_Standard</a> - */ - private static final int AES_BLOCK_SIZE = 16; - - /** * The private constructor of {@Link Utils}. */ private Utils() { @@ -84,47 +76,6 @@ public final class Utils { } /** - * <p> - * This method is only for Counter (CTR) mode. Generally the CryptoCipher - * calculates the IV and maintain encryption context internally.For example - * a Cipher will maintain its encryption context internally when we do - * encryption/decryption using the CryptoCipher#update interface. - * </p> - * <p> - * Encryption/Decryption is not always on the entire file. For example, in - * Hadoop, a node may only decrypt a portion of a file (i.e. a split). In - * these situations, the counter is derived from the file position. - * </p> - * The IV can be calculated by combining the initial IV and the counter with - * a lossless operation (concatenation, addition, or XOR). - * - * @see <a - * href="http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29"> - * http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29</a> - * - * @param initIV initial IV - * @param counter counter for input stream position - * @param IV the IV for input stream position - */ - public static void calculateIV(byte[] initIV, long counter, byte[] IV) { - checkArgument(initIV.length == AES_BLOCK_SIZE); - checkArgument(IV.length == AES_BLOCK_SIZE); - - int i = IV.length; // IV length - int j = 0; // counter bytes index - int sum = 0; - while (i-- > 0) { - // (sum >>> Byte.SIZE) is the carry for addition - sum = (initIV[i] & 0xff) + (sum >>> Byte.SIZE); // NOPMD - if (j++ < 8) { // Big-endian, and long is 8 bytes length - sum += (byte) counter & 0xff; - counter >>>= 8; - } - IV[i] = (byte) sum; - } - } - - /** * Helper method to create a CryptoCipher instance and throws only * IOException. *