You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2014/04/04 14:03:33 UTC
svn commit: r1584627 - in /cayenne/main/trunk/cayenne-crypto/src:
main/java/org/apache/cayenne/crypto/transformer/bytes/
test/java/org/apache/cayenne/crypto/transformer/bytes/
test/java/org/apache/cayenne/crypto/unit/
Author: aadamchik
Date: Fri Apr 4 12:03:32 2014
New Revision: 1584627
URL: http://svn.apache.org/r1584627
Log:
CAY-1916 cayenne-crypto module that enables data encryption for certain model attributes
BytesEncryptor/BytesDecryptor to encapsulate crypto protocol .. IN PROGRESS
Added:
cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtils.java
cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtilsTest.java
Modified:
cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptor.java
cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java
cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java
Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptor.java?rev=1584627&r1=1584626&r2=1584627&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptor.java (original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptor.java Fri Apr 4 12:03:32 2014
@@ -21,6 +21,7 @@ package org.apache.cayenne.crypto.transf
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
+import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
@@ -48,13 +49,19 @@ class CbcEncryptor implements BytesEncry
public CbcEncryptor(Cipher cipher, Key key, byte[] seedIv) {
this.key = key;
this.cipher = cipher;
- this.iv = seedIv;
this.blockSize = cipher.getBlockSize();
- if (iv.length != blockSize) {
- throw new CayenneCryptoException("IV size is expected to be the same as block size. Was " + iv.length
+ if (seedIv.length != blockSize) {
+
+ // TODO: perhaps we should truncate/expand it if there's a mismatch
+ throw new CayenneCryptoException("IV size is expected to be the same as block size. Was " + seedIv.length
+ "; block size was: " + blockSize);
}
+
+ // making a copy - we are modifying this array, something that should
+ // not be visible oustide this object.
+ this.iv = Arrays.copyOf(seedIv, blockSize);
+
}
@Override
@@ -72,7 +79,7 @@ class CbcEncryptor implements BytesEncry
}
}
- protected void doEncrypt(byte[] plain, byte[] encrypted, int outputOffset) throws InvalidKeyException,
+ private void doEncrypt(byte[] plain, byte[] encrypted, int outputOffset) throws InvalidKeyException,
InvalidAlgorithmParameterException, ShortBufferException, IllegalBlockSizeException, BadPaddingException {
// copy IV in the first block
Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java?rev=1584627&r1=1584626&r2=1584627&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java (original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java Fri Apr 4 12:03:32 2014
@@ -33,7 +33,7 @@ import javax.crypto.NoSuchPaddingExcepti
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
-import org.apache.cayenne.crypto.unit.CryptoTestUtils;
+import org.apache.cayenne.crypto.unit.CryptoUnitUtils;
import org.junit.Before;
import org.junit.Test;
@@ -44,7 +44,7 @@ public class CbcDecryptorTest {
@Before
public void before() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
- byte[] keyBytes = CryptoTestUtils.hexToBytes("a4cb499fa31a6a228e16b7e4741d4fa3");
+ byte[] keyBytes = CryptoUnitUtils.hexToBytes("a4cb499fa31a6a228e16b7e4741d4fa3");
this.key = new SecretKeySpec(keyBytes, "AES");
this.cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
@@ -70,7 +70,7 @@ public class CbcDecryptorTest {
CbcDecryptor decryptor = new CbcDecryptor(cipher);
byte[] plain = { 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
- byte[] ivBytes = CryptoTestUtils.hexToBytes("0591849d87c93414f4405d32f4d69220");
+ byte[] ivBytes = CryptoUnitUtils.hexToBytes("0591849d87c93414f4405d32f4d69220");
Cipher encCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
encCipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(ivBytes));
Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java?rev=1584627&r1=1584626&r2=1584627&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java (original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptorTest.java Fri Apr 4 12:03:32 2014
@@ -18,20 +18,26 @@
****************************************************************/
package org.apache.cayenne.crypto.transformer.bytes;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import java.io.UnsupportedEncodingException;
+import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.cayenne.crypto.CayenneCryptoException;
-import org.apache.cayenne.crypto.unit.CryptoTestUtils;
+import org.apache.cayenne.crypto.unit.CryptoUnitUtils;
import org.junit.Test;
public class CbcEncryptorTest {
@@ -60,7 +66,6 @@ public class CbcEncryptorTest {
cipher.init(Cipher.ENCRYPT_MODE, key);
assertEquals(8, cipher.getBlockSize());
- // try with non-standard block size too...
CbcEncryptor encryptor = new CbcEncryptor(cipher, key, iv);
assertEquals(24, encryptor.getOutputSize(11));
}
@@ -69,23 +74,45 @@ public class CbcEncryptorTest {
public void testGetOutputSize_AES() throws UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
- byte[] ivBytes = CryptoTestUtils.hexToBytes("0591849d87c93414f4405d32f4d69220");
- byte[] keyBytes = CryptoTestUtils.hexToBytes("a4cb499fa31a6a228e16b7e4741d4fa3");
+ byte[] ivBytes = CryptoUnitUtils.hexToBytes("0591849d87c93414f4405d32f4d69220");
+ byte[] keyBytes = CryptoUnitUtils.hexToBytes("a4cb499fa31a6a228e16b7e4741d4fa3");
Key key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
assertEquals(16, cipher.getBlockSize());
- // try with non-standard block size too...
CbcEncryptor encryptor = new CbcEncryptor(cipher, key, ivBytes);
assertEquals(32, encryptor.getOutputSize(11));
}
@Test
- public void testEncrypt() {
+ public void testEncrypt_AES() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
+ InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
- // CbcEncryptor encryptor = new CbcEncryptor(cipher, key, iv);
+ byte[] ivBytes = CryptoUnitUtils.hexToBytes("0591849d87c93414f4405d32f4d69220");
+ byte[] keyBytes = CryptoUnitUtils.hexToBytes("a4cb499fa31a6a228e16b7e4741d4fa3");
+ Key key = new SecretKeySpec(keyBytes, "AES");
+
+ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+ cipher.init(Cipher.ENCRYPT_MODE, key);
+ assertEquals(16, cipher.getBlockSize());
+
+ byte[] plain = { 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
+
+ // copy ivBytes, as they are reset
+ CbcEncryptor encryptor = new CbcEncryptor(cipher, key, ivBytes);
+
+ byte[] encrypted = new byte[encryptor.getOutputSize(plain.length)];
+
+ encryptor.encrypt(plain, encrypted, 0);
+
+ assertArrayEquals(ivBytes, Arrays.copyOfRange(encrypted, 0, 16));
+
+ Cipher decCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+ decCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivBytes));
+ byte[] newPlain = decCipher.doFinal(encrypted, 16, encrypted.length - 16);
+ assertArrayEquals(plain, newPlain);
}
}
Added: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtils.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtils.java?rev=1584627&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtils.java (added)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtils.java Fri Apr 4 12:03:32 2014
@@ -0,0 +1,37 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.crypto.unit;
+
+import java.math.BigInteger;
+import java.util.Arrays;
+
+public class CryptoUnitUtils {
+
+ public static byte[] hexToBytes(String hexString) {
+ byte[] bytes = new BigInteger(hexString, 16).toByteArray();
+
+ // http://stackoverflow.com/questions/4407779/biginteger-to-byte
+ if (bytes.length > 0 && bytes[0] == 0) {
+ return Arrays.copyOfRange(bytes, 1, bytes.length);
+ } else {
+ return bytes;
+ }
+ }
+
+}
Added: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtilsTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtilsTest.java?rev=1584627&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtilsTest.java (added)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtilsTest.java Fri Apr 4 12:03:32 2014
@@ -0,0 +1,41 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.crypto.unit;
+
+import static org.junit.Assert.assertArrayEquals;
+
+import org.junit.Test;
+
+public class CryptoUnitUtilsTest {
+
+ @Test
+ public void testHexToBytes() {
+
+ String hexString = "0506AB";
+ byte[] hexByte = { 5, 6, (byte) 0xAB };
+ assertArrayEquals(hexByte, CryptoUnitUtils.hexToBytes(hexString));
+
+ String hexString2 = "0591849d87c93414f4405d32f4d69220";
+ byte[] hexByte2 = { 5, (byte) 0x91, (byte) 0x84, (byte) 0x9d, (byte) 0x87, (byte) 0xc9, (byte) 0x34,
+ (byte) 0x14, (byte) 0xf4, (byte) 0x40, (byte) 0x5d, (byte) 0x32, (byte) 0xf4, (byte) 0xd6, (byte) 0x92,
+ (byte) 0x20 };
+ assertArrayEquals(hexByte2, CryptoUnitUtils.hexToBytes(hexString2));
+ }
+
+}