cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1584624 - in /cayenne/main/trunk/cayenne-crypto/src: main/java/org/apache/cayenne/crypto/transformer/bytes/ test/java/org/apache/cayenne/crypto/transformer/bytes/
Date Fri, 04 Apr 2014 12:03:02 GMT
Author: aadamchik
Date: Fri Apr  4 12:03:01 2014
New Revision: 1584624

URL: http://svn.apache.org/r1584624
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/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesDecryptor.java
      - copied, changed from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesEncryptor.java
      - copied, changed from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptor.java
      - copied, changed from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptor.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyName.java
      - copied, changed from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java
      - copied, changed from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformerFactory.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyNameTest.java
      - copied, changed from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java
Removed:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java
Modified:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformerFactory.java
    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/CbcEncryptor.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/EncryptorWithKeyName.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java

Copied: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesDecryptor.java
(from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesDecryptor.java?p2=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesDecryptor.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java&r1=1584623&r2=1584624&rev=1584624&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesDecryptor.java
Fri Apr  4 12:03:01 2014
@@ -18,10 +18,12 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.bytes;
 
+import java.security.Key;
+
 /**
  * @since 3.2
  */
-public interface BytesTransformer {
+public interface BytesDecryptor {
 
     /**
      * Returns the size of the transformed data in bytes. This information
@@ -29,5 +31,10 @@ public interface BytesTransformer {
      */
     int getOutputSize(int inputLength);
 
-    void transform(byte[] input, byte[] output, int outputOffset);
+    /**
+     * Transform input bytes using provided encryption key. Note that some
+     * implementations may ignore the provided key and e.g. derive the key from
+     * the record.
+     */
+    void decrypt(byte[] input, byte[] output, int inputOffset, Key key);
 }

Copied: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesEncryptor.java
(from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesEncryptor.java?p2=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesEncryptor.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java&r1=1584623&r2=1584624&rev=1584624&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesEncryptor.java
Fri Apr  4 12:03:01 2014
@@ -21,7 +21,7 @@ package org.apache.cayenne.crypto.transf
 /**
  * @since 3.2
  */
-public interface BytesTransformer {
+public interface BytesEncryptor {
 
     /**
      * Returns the size of the transformed data in bytes. This information
@@ -29,5 +29,9 @@ public interface BytesTransformer {
      */
     int getOutputSize(int inputLength);
 
-    void transform(byte[] input, byte[] output, int outputOffset);
+    /**
+     * Transform input bytes using default encryption key.
+     */
+    void encrypt(byte[] input, byte[] output, int outputOffset);
+
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformerFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformerFactory.java?rev=1584624&r1=1584623&r2=1584624&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformerFactory.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformerFactory.java
Fri Apr  4 12:03:01 2014
@@ -26,7 +26,7 @@ package org.apache.cayenne.crypto.transf
  */
 public interface BytesTransformerFactory {
 
-    BytesTransformer encryptor();
+    BytesEncryptor encryptor();
 
-    BytesTransformer decryptor();
+    BytesDecryptor decryptor();
 }

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=1584624&r1=1584623&r2=1584624&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
Fri Apr  4 12:03:01 2014
@@ -41,11 +41,13 @@ class CbcBytesTransformerFactory impleme
     private Key key;
     private byte[] keyName;
     private int blockSize;
+    private KeySource keySource;
     private Queue<SecureRandom> randoms;
 
     public CbcBytesTransformerFactory(CipherFactory cipherFactory, KeySource keySource, String
keyName) {
 
         this.randoms = new ConcurrentLinkedQueue<SecureRandom>();
+        this.keySource = keySource;
         this.cipherFactory = cipherFactory;
         this.blockSize = cipherFactory.blockSize();
 
@@ -100,18 +102,22 @@ class CbcBytesTransformerFactory impleme
     }
 
     @Override
-    public BytesTransformer encryptor() {
+    public BytesEncryptor encryptor() {
         Cipher cipher = cipherFactory.cipher();
 
-        BytesTransformer cbcEncryptor = new CbcEncryptor(cipher, key, generateSeedIv());
+        BytesEncryptor cbcEncryptor = new CbcEncryptor(cipher, key, generateSeedIv());
 
         // TODO: make adding key name for versioning an optional property
         return new EncryptorWithKeyName(cbcEncryptor, keyName, blockSize);
     }
 
     @Override
-    public BytesTransformer decryptor() {
-        throw new UnsupportedOperationException("TODO");
+    public BytesDecryptor decryptor() {
+        Cipher cipher = cipherFactory.cipher();
+        BytesDecryptor cbcDecryptor = new CbcDecryptor(cipher);
+
+        // TODO: make checking for key name an optional property
+        return new DecryptorWithKeyName(cbcDecryptor, keySource, blockSize);
     }
 
 }

Copied: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptor.java
(from r1584623, 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/CbcDecryptor.java?p2=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptor.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/CbcEncryptor.java&r1=1584623&r2=1584624&rev=1584624&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/CbcDecryptor.java
Fri Apr  4 12:03:01 2014
@@ -31,59 +31,45 @@ import javax.crypto.spec.IvParameterSpec
 import org.apache.cayenne.crypto.CayenneCryptoException;
 
 /**
- * A {@link BytesTransformer} that encrypts the provided bytes. The first block
- * in the encrypted bytes is the value of IV used to seed the CBC
- * transformation. It will be needed for decryption. The object is stateful and
- * is not thread-safe.
- * 
  * @since 3.2
  */
-class CbcEncryptor implements BytesTransformer {
+class CbcDecryptor implements BytesDecryptor {
 
     private Cipher cipher;
-    private byte[] iv;
-    private Key key;
     private int blockSize;
 
-    public CbcEncryptor(Cipher cipher, Key key, byte[] seedIv) {
-        this.key = key;
+    CbcDecryptor(Cipher cipher) {
         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
-                    + "; block size was: " + blockSize);
-        }
     }
 
     @Override
     public int getOutputSize(int inputLength) {
-        // add one block for IV storage
-        return blockSize + cipher.getOutputSize(inputLength);
+        // strip the IV block size from total size...
+        return cipher.getOutputSize(inputLength) - blockSize;
     }
 
     @Override
-    public void transform(byte[] input, byte[] output, int outputOffset) {
+    public void decrypt(byte[] input, byte[] output, int inputOffset, Key key) {
         try {
-            encrypt(input, output, outputOffset);
+            doDecrypt(input, output, inputOffset, key);
         } catch (Exception e) {
-            throw new CayenneCryptoException("Error on encryption", e);
+            throw new CayenneCryptoException("Error on decryption", e);
         }
     }
 
-    protected void encrypt(byte[] plain, byte[] encrypted, int outputOffset) throws InvalidKeyException,
+    private void doDecrypt(byte[] input, byte[] output, int inputOffset, Key key) throws
InvalidKeyException,
             InvalidAlgorithmParameterException, ShortBufferException, IllegalBlockSizeException,
BadPaddingException {
 
-        // copy IV in the first block
-        System.arraycopy(iv, 0, encrypted, outputOffset, blockSize);
+        IvParameterSpec iv = iv(input, inputOffset);
 
-        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
-        int encBytes = cipher.doFinal(plain, 0, plain.length, encrypted, outputOffset + blockSize);
+        cipher.init(Cipher.DECRYPT_MODE, key, iv);
 
-        // store the last block of ciphertext to use as an IV for the next round
-        // of encryption...
-        System.arraycopy(encrypted, outputOffset + encBytes, iv, 0, blockSize);
+        int offset = inputOffset + blockSize;
+        cipher.doFinal(input, offset, input.length - offset, output, 0);
     }
 
+    IvParameterSpec iv(byte[] input, int inputOffset) {
+        return new IvParameterSpec(input, inputOffset, blockSize);
+    }
 }

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=1584624&r1=1584623&r2=1584624&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:01 2014
@@ -31,14 +31,14 @@ import javax.crypto.spec.IvParameterSpec
 import org.apache.cayenne.crypto.CayenneCryptoException;
 
 /**
- * A {@link BytesTransformer} that encrypts the provided bytes. The first block
- * in the encrypted bytes is the value of IV used to seed the CBC
- * transformation. It will be needed for decryption. The object is stateful and
- * is not thread-safe.
+ * A {@link BytesEncryptor} that encrypts the provided bytes. The first block in
+ * the encrypted bytes is the value of IV used to seed the CBC transformation.
+ * It will be needed for decryption. The object is stateful and is not
+ * thread-safe.
  * 
  * @since 3.2
  */
-class CbcEncryptor implements BytesTransformer {
+class CbcEncryptor implements BytesEncryptor {
 
     private Cipher cipher;
     private byte[] iv;
@@ -64,15 +64,15 @@ class CbcEncryptor implements BytesTrans
     }
 
     @Override
-    public void transform(byte[] input, byte[] output, int outputOffset) {
+    public void encrypt(byte[] input, byte[] output, int outputOffset) {
         try {
-            encrypt(input, output, outputOffset);
+            doEncrypt(input, output, outputOffset);
         } catch (Exception e) {
             throw new CayenneCryptoException("Error on encryption", e);
         }
     }
 
-    protected void encrypt(byte[] plain, byte[] encrypted, int outputOffset) throws InvalidKeyException,
+    protected void doEncrypt(byte[] plain, byte[] encrypted, int outputOffset) throws InvalidKeyException,
             InvalidAlgorithmParameterException, ShortBufferException, IllegalBlockSizeException,
BadPaddingException {
 
         // copy IV in the first block

Copied: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyName.java
(from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyName.java?p2=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyName.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java&r1=1584623&r2=1584624&rev=1584624&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyName.java
Fri Apr  4 12:03:01 2014
@@ -18,35 +18,52 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.bytes;
 
+import java.io.UnsupportedEncodingException;
+import java.security.Key;
+
 import org.apache.cayenne.crypto.CayenneCryptoException;
+import org.apache.cayenne.crypto.key.KeySource;
+
+/**
+ * @since 3.2
+ */
+class DecryptorWithKeyName implements BytesDecryptor {
 
-class EncryptorWithKeyName implements BytesTransformer {
+    private static final String KEY_NAME_CHARSET = "UTF-8";
 
-    private BytesTransformer delegate;
+    private KeySource keySource;
+    private BytesDecryptor delegate;
     private int blockSize;
-    private byte[] keyName;
 
-    EncryptorWithKeyName(BytesTransformer delegate, byte[] keyName, int blockSize) {
+    public DecryptorWithKeyName(BytesDecryptor delegate, KeySource keySource, int blockSize)
{
         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.keySource = keySource;
     }
 
     @Override
     public int getOutputSize(int inputLength) {
-        // add one block for key name storage
-        return blockSize + delegate.getOutputSize(inputLength);
+        // strip one block that stores the key name
+        return delegate.getOutputSize(inputLength) - blockSize;
     }
 
     @Override
-    public void transform(byte[] input, byte[] output, int outputOffset) {
-        System.arraycopy(keyName, 0, output, outputOffset, blockSize);
-        delegate.transform(input, output, outputOffset + blockSize);
+    public void decrypt(byte[] input, byte[] output, int inputOffset, Key key) {
+
+        // ignoring the parameter key... using the key from the first block
+
+        String keyName = keyName(input, inputOffset);
+        Key inRecordKey = keySource.getKey(keyName);
+        delegate.decrypt(input, output, inputOffset + blockSize, inRecordKey);
+    }
+
+    String keyName(byte[] input, int inputOffset) {
+        try {
+            // 'trim' is to get rid of 0 padding
+            return new String(input, inputOffset, blockSize, KEY_NAME_CHARSET).trim();
+        } catch (UnsupportedEncodingException e) {
+            throw new CayenneCryptoException("Can't decode with " + KEY_NAME_CHARSET, e);
+        }
     }
 
 }

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=1584624&r1=1584623&r2=1584624&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
Fri Apr  4 12:03:01 2014
@@ -60,11 +60,11 @@ public class DefaultBytesTransformerFact
         }
     }
 
-    public BytesTransformer encryptor() {
+    public BytesEncryptor encryptor() {
         return delegate.encryptor();
     }
 
-    public BytesTransformer decryptor() {
+    public BytesDecryptor decryptor() {
         return delegate.decryptor();
     }
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java?rev=1584624&r1=1584623&r2=1584624&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyName.java
Fri Apr  4 12:03:01 2014
@@ -20,13 +20,13 @@ package org.apache.cayenne.crypto.transf
 
 import org.apache.cayenne.crypto.CayenneCryptoException;
 
-class EncryptorWithKeyName implements BytesTransformer {
+class EncryptorWithKeyName implements BytesEncryptor {
 
-    private BytesTransformer delegate;
+    private BytesEncryptor delegate;
     private int blockSize;
     private byte[] keyName;
 
-    EncryptorWithKeyName(BytesTransformer delegate, byte[] keyName, int blockSize) {
+    EncryptorWithKeyName(BytesEncryptor delegate, byte[] keyName, int blockSize) {
         this.delegate = delegate;
         this.blockSize = blockSize;
         this.keyName = keyName;
@@ -44,9 +44,9 @@ class EncryptorWithKeyName implements By
     }
 
     @Override
-    public void transform(byte[] input, byte[] output, int outputOffset) {
+    public void encrypt(byte[] input, byte[] output, int outputOffset) {
         System.arraycopy(keyName, 0, output, outputOffset, blockSize);
-        delegate.transform(input, output, outputOffset + blockSize);
+        delegate.encrypt(input, output, outputOffset + blockSize);
     }
 
 }

Copied: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java
(from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformerFactory.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java?p2=cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformerFactory.java&r1=1584623&r2=1584624&rev=1584624&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformerFactory.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/CbcDecryptorTest.java
Fri Apr  4 12:03:01 2014
@@ -18,15 +18,31 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.bytes;
 
-/**
- * A class that encapsulates Cayenne cryptography protocol, which is usually
- * dependent on the encryption mode.
- * 
- * @since 3.2
- */
-public interface BytesTransformerFactory {
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
 
-    BytesTransformer encryptor();
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.Cipher;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.IvParameterSpec;
+
+import org.junit.Test;
+
+public class CbcDecryptorTest {
+
+    @Test
+    public void testIv() throws NoSuchAlgorithmException, NoSuchPaddingException {
+        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+        assertEquals(16, cipher.getBlockSize());
+
+        CbcDecryptor decryptor = new CbcDecryptor(cipher);
+
+        byte[] input = { 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
3, 2, 1 };
+        byte[] ivBytes = { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
+
+        IvParameterSpec iv = decryptor.iv(input, 5);
+        assertArrayEquals(ivBytes, iv.getIV());
+    }
 
-    BytesTransformer decryptor();
 }

Copied: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyNameTest.java
(from r1584623, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyNameTest.java?p2=cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyNameTest.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java&r1=1584623&r2=1584624&rev=1584624&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/bytes/BytesTransformer.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/DecryptorWithKeyNameTest.java
Fri Apr  4 12:03:01 2014
@@ -18,16 +18,26 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.bytes;
 
-/**
- * @since 3.2
- */
-public interface BytesTransformer {
-
-    /**
-     * Returns the size of the transformed data in bytes. This information
-     * allows the caller to pre-size the output array.
-     */
-    int getOutputSize(int inputLength);
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+
+import org.apache.cayenne.crypto.key.KeySource;
+import org.junit.Test;
+
+public class DecryptorWithKeyNameTest {
+
+    @Test
+    public void testKeyName() {
+
+        byte[] input1 = { 'a', 'b', 'c', 'd', 'e' };
+        byte[] input2 = { 'a', 'b', 'c', 0, 'e' };
+        byte[] input3 = { 'a', 'b', 0, 0, 'e' };
+
+        DecryptorWithKeyName decryptor = new DecryptorWithKeyName(mock(BytesDecryptor.class),
mock(KeySource.class), 3);
+        assertEquals("bcd", decryptor.keyName(input1, 1));
+        assertEquals("bc", decryptor.keyName(input2, 1));
+        assertEquals("b", decryptor.keyName(input3, 1));
+
+    }
 
-    void transform(byte[] input, byte[] output, int outputOffset);
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java?rev=1584624&r1=1584623&r2=1584624&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/bytes/EncryptorWithKeyNameTest.java
Fri Apr  4 12:03:01 2014
@@ -36,7 +36,7 @@ public class EncryptorWithKeyNameTest {
     public void testGetOutputSize() throws UnsupportedEncodingException {
 
         byte[] keyName = "mykey".getBytes("UTF-8");
-        BytesTransformer delegate = mock(BytesTransformer.class);
+        BytesEncryptor delegate = mock(BytesEncryptor.class);
         when(delegate.getOutputSize(8)).thenReturn(8);
 
         // try with non-standard block size..
@@ -49,7 +49,7 @@ public class EncryptorWithKeyNameTest {
 
         byte[] keyName = "mykey".getBytes("UTF-8");
 
-        BytesTransformer delegate = mock(BytesTransformer.class);
+        BytesEncryptor delegate = mock(BytesEncryptor.class);
         when(delegate.getOutputSize(8)).thenReturn(8);
 
         byte[] input = { 1, 2, 3, 4, 5, 6, 7, 8 };
@@ -70,12 +70,12 @@ public class EncryptorWithKeyNameTest {
 
                 return null;
             }
-        }).when(delegate).transform(input, output, 6);
+        }).when(delegate).encrypt(input, output, 6);
 
         // intentionally non-standard block size..
         EncryptorWithKeyName encryptor = new EncryptorWithKeyName(delegate, keyName, 5);
 
-        encryptor.transform(input, output, 1);
+        encryptor.encrypt(input, output, 1);
 
         assertArrayEquals(new byte[] { 0, 'm', 'y', 'k', 'e', 'y', 1, 1, 1, 1, 1, 1, 1, 1,
0, 0 }, output);
     }



Mime
View raw message