cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1586979 - 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/
Date Sun, 13 Apr 2014 13:28:32 GMT
Author: aadamchik
Date: Sun Apr 13 13:28:32 2014
New Revision: 1586979

URL: http://svn.apache.org/r1586979
Log:
CAY-1925 cayenne-crypto: add optional compression to the encryption pipeline

using the new Header class in byte encryptors

Modified:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DefaultBytesTransformerFactory.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptorTest.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtils.java

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java?rev=1586979&r1=1586978&r2=1586979&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcBytesTransformerFactory.java
Sun Apr 13 13:28:32 2014
@@ -18,7 +18,6 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.bytes;
 
-import java.io.UnsupportedEncodingException;
 import java.security.Key;
 import java.security.SecureRandom;
 import java.util.Queue;
@@ -26,7 +25,6 @@ import java.util.concurrent.ConcurrentLi
 
 import javax.crypto.Cipher;
 
-import org.apache.cayenne.crypto.CayenneCryptoException;
 import org.apache.cayenne.crypto.cipher.CipherFactory;
 import org.apache.cayenne.crypto.key.KeySource;
 
@@ -35,41 +33,24 @@ import org.apache.cayenne.crypto.key.Key
  */
 class CbcBytesTransformerFactory implements BytesTransformerFactory {
 
-    private static final String KEY_NAME_CHARSET = "UTF-8";
-
     private CipherFactory cipherFactory;
     private Key key;
-    private byte[] keyName;
+    private Header encryptionHeader;
     private int blockSize;
     private KeySource keySource;
     private Queue<SecureRandom> randoms;
 
-    public CbcBytesTransformerFactory(CipherFactory cipherFactory, KeySource keySource) {
-
-        String keyName = keySource.getDefaultKeyAlias();
+    CbcBytesTransformerFactory(CipherFactory cipherFactory, KeySource keySource, Header encryptionHeader)
{
 
         this.randoms = new ConcurrentLinkedQueue<SecureRandom>();
         this.keySource = keySource;
-        this.key = keySource.getKey(keyName);
+
         this.cipherFactory = cipherFactory;
         this.blockSize = cipherFactory.blockSize();
+        this.encryptionHeader = encryptionHeader;
 
-        byte[] keyNameBytes;
-        try {
-            keyNameBytes = keyName.getBytes(KEY_NAME_CHARSET);
-        } catch (UnsupportedEncodingException e) {
-            throw new CayenneCryptoException("Can't encode in " + KEY_NAME_CHARSET, e);
-        }
-
-        if (keyNameBytes.length == blockSize) {
-            this.keyName = keyNameBytes;
-        } else if (keyNameBytes.length < blockSize) {
-            this.keyName = new byte[blockSize];
-            System.arraycopy(keyNameBytes, 0, this.keyName, 0, keyNameBytes.length);
-        } else {
-            throw new CayenneCryptoException("Key name '" + keyName + "' is too long. Its
byte form should not exceed "
-                    + blockSize + " bytes");
-        }
+        String keyName = keySource.getDefaultKeyAlias();
+        this.key = keySource.getKey(keyName);
     }
 
     protected byte[] generateSeedIv() {
@@ -107,11 +88,8 @@ class CbcBytesTransformerFactory impleme
     @Override
     public BytesEncryptor encryptor() {
         Cipher cipher = cipherFactory.cipher();
-
         BytesEncryptor cbcEncryptor = new CbcEncryptor(cipher, key, generateSeedIv());
-
-        // TODO: make adding key name for versioning an optional property
-        return new HeaderEncryptor(cbcEncryptor, keyName, blockSize);
+        return new HeaderEncryptor(cbcEncryptor, encryptionHeader);
     }
 
     @Override

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DefaultBytesTransformerFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DefaultBytesTransformerFactory.java?rev=1586979&r1=1586978&r2=1586979&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DefaultBytesTransformerFactory.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DefaultBytesTransformerFactory.java
Sun Apr 13 13:28:32 2014
@@ -45,7 +45,8 @@ public class DefaultBytesTransformerFact
         }
 
         if ("CBC".equals(mode)) {
-            this.delegate = new CbcBytesTransformerFactory(cipherFactory, keySource);
+            this.delegate = new CbcBytesTransformerFactory(cipherFactory, keySource, Header.create(keySource
+                    .getDefaultKeyAlias()));
         }
         // TODO: ECB and other modes...
         else {

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java?rev=1586979&r1=1586978&r2=1586979&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/Header.java
Sun Apr 13 13:28:32 2014
@@ -28,33 +28,33 @@ import org.apache.cayenne.crypto.Cayenne
  * 
  * @since 3.2
  */
-class Header {
+public class Header {
 
     private static final String KEY_NAME_CHARSET = "UTF-8";
 
     /**
      * The size of a header byte[] block.
      */
-    static final int HEADER_SIZE = 16;
+    public static final int HEADER_SIZE = 16;
 
     /**
      * The size of a key name within the header block.
      */
-    static final int KEY_NAME_SIZE = 8;
+    public static final int KEY_NAME_SIZE = 8;
 
     /**
      * Position of the key name within the header block.
      */
-    static final int KEY_NAME_OFFSET = 8;
+    public static final int KEY_NAME_OFFSET = 8;
 
     /**
      * Position of the "flags" byte in the header.
      */
-    static final int FLAGS_OFFSET = 0;
+    public static final int FLAGS_OFFSET = 0;
 
     private byte[] data;
 
-    static Header create(String keyName) {
+    public static Header create(String keyName) {
         byte[] keyNameBytes;
         try {
             keyNameBytes = keyName.getBytes(KEY_NAME_CHARSET);
@@ -72,10 +72,9 @@ class Header {
         }
 
         return create(data);
-
     }
 
-    static Header create(byte[] data) {
+    public static Header create(byte[] data) {
 
         if (data.length != HEADER_SIZE) {
             throw new CayenneCryptoException("Unexpected header data size: " + data.length
+ ", expected size is "
@@ -92,7 +91,7 @@ class Header {
 
     }
 
-    byte[] getData() {
+    public byte[] getData() {
         return data;
     }
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.java?rev=1586979&r1=1586978&r2=1586979&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptor.java
Sun Apr 13 13:28:32 2014
@@ -18,31 +18,20 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.bytes;
 
-import org.apache.cayenne.crypto.CayenneCryptoException;
-
 class HeaderEncryptor implements BytesEncryptor {
 
     private BytesEncryptor delegate;
-    private int blockSize;
-    private byte[] keyName;
+    private Header header;
 
-    HeaderEncryptor(BytesEncryptor delegate, byte[] keyName, int blockSize) {
+    HeaderEncryptor(BytesEncryptor delegate, Header header) {
         this.delegate = delegate;
-        this.blockSize = blockSize;
-        this.keyName = keyName;
-
-        if (blockSize != keyName.length) {
-            throw new CayenneCryptoException("keyName size is expected to be the same as
block size. Was "
-                    + keyName.length + "; block size was: " + blockSize);
-        }
+        this.header = header;
     }
 
     @Override
     public byte[] encrypt(byte[] input, int outputOffset) {
-        byte[] output = delegate.encrypt(input, outputOffset + blockSize);
-
-        System.arraycopy(keyName, 0, output, outputOffset, blockSize);
-
+        byte[] output = delegate.encrypt(input, outputOffset + Header.HEADER_SIZE);
+        System.arraycopy(header.getData(), 0, output, outputOffset, Header.HEADER_SIZE);
         return output;
     }
 

Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptorTest.java?rev=1586979&r1=1586978&r2=1586979&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptorTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/HeaderEncryptorTest.java
Sun Apr 13 13:28:32 2014
@@ -30,17 +30,18 @@ public class HeaderEncryptorTest {
     @Test
     public void testTransform() throws UnsupportedEncodingException {
 
-        byte[] keyName = "mykey".getBytes("UTF-8");
+        Header encryptionHeader = Header.create("mykey");
 
         BytesEncryptor delegate = SwapBytesTransformer.encryptor();
 
         byte[] input = { 1, 2, 3, 4, 5, 6, 7, 8 };
 
         // intentionally non-standard block size..
-        HeaderEncryptor encryptor = new HeaderEncryptor(delegate, keyName, 5);
+        HeaderEncryptor encryptor = new HeaderEncryptor(delegate, encryptionHeader);
 
         byte[] output = encryptor.encrypt(input, 1);
-        assertArrayEquals(new byte[] { 0, 'm', 'y', 'k', 'e', 'y', 8, 7, 6, 5, 4, 3, 2, 1
}, output);
+        assertArrayEquals(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 'm', 'y', 'k', 'e', 'y',
0, 0, 0, 8, 7, 6, 5, 4, 3,
+                2, 1 }, output);
     }
 
 }

Modified: 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=1586979&r1=1586978&r2=1586979&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtils.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/unit/CryptoUnitUtils.java
Sun Apr 13 13:28:32 2014
@@ -27,6 +27,7 @@ import javax.crypto.spec.IvParameterSpec
 
 import org.apache.cayenne.configuration.server.ServerRuntime;
 import org.apache.cayenne.crypto.key.KeySource;
+import org.apache.cayenne.crypto.transformer.bytes.Header;
 
 public class CryptoUnitUtils {
 
@@ -48,12 +49,12 @@ public class CryptoUnitUtils {
             Cipher decCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
 
             int blockSize = decCipher.getBlockSize();
-            byte[] keyNameBytes = Arrays.copyOfRange(source, 0, blockSize);
-            byte[] ivBytes = Arrays.copyOfRange(source, blockSize, blockSize * 2);
-            byte[] cipherText = Arrays.copyOfRange(source, blockSize * 2, source.length);
+            byte[] keyNameBytes = Arrays.copyOfRange(source, 0, Header.HEADER_SIZE);
+            byte[] ivBytes = Arrays.copyOfRange(source, Header.HEADER_SIZE, Header.HEADER_SIZE
+ blockSize);
+            byte[] cipherText = Arrays.copyOfRange(source, Header.HEADER_SIZE + blockSize,
source.length);
 
             // 'trim' is to get rid of 0 padding
-            String keyName = new String(keyNameBytes, "UTF-8").trim();
+            String keyName = new String(keyNameBytes, Header.KEY_NAME_OFFSET, Header.KEY_NAME_SIZE,
"UTF-8").trim();
             Key key = runtime.getInjector().getInstance(KeySource.class).getKey(keyName);
 
             decCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivBytes));



Mime
View raw message