Browse Source

Extract cipher from decryption

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 year ago
parent
commit
c2f7d00505

+ 5 - 3
app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java

@@ -51,6 +51,8 @@ import java.util.Iterator;
 import java.util.Set;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 
+import javax.crypto.Cipher;
+
 import static com.owncloud.android.utils.EncryptionUtils.decodeStringToBase64Bytes;
 import static com.owncloud.android.utils.EncryptionUtils.decodeStringToBase64Bytes;
 
 
 /**
 /**
@@ -265,9 +267,9 @@ public class DownloadFileOperation extends RemoteOperation {
                 byte[] authenticationTag = decodeStringToBase64Bytes(authenticationTagString);
                 byte[] authenticationTag = decodeStringToBase64Bytes(authenticationTagString);
 
 
                 try {
                 try {
-                    byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile,
-                                                                        key,
-                                                                        iv,
+                    Cipher cipher = EncryptionUtils.getCipher(Cipher.DECRYPT_MODE, key, iv);
+                    byte[] decryptedBytes = EncryptionUtils.decryptFile(cipher,
+                                                                        tmpFile,
                                                                         authenticationTag,
                                                                         authenticationTag,
                                                                         new ArbitraryDataProviderImpl(operationContext),
                                                                         new ArbitraryDataProviderImpl(operationContext),
                                                                         user);
                                                                         user);

+ 7 - 23
app/src/main/java/com/owncloud/android/utils/EncryptionUtils.java

@@ -579,7 +579,7 @@ public final class EncryptionUtils {
         InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidParameterSpecException {
         InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidParameterSpecException {
         File file = new File(ocFile.getStoragePath());
         File file = new File(ocFile.getStoragePath());
 
 
-        Cipher cipher = getEncoderCipher(encryptionKeyBytes, iv);
+        Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, encryptionKeyBytes, iv);
         return encryptFile(file, cipher);
         return encryptFile(file, cipher);
     }
     }
 
 
@@ -591,11 +591,11 @@ public final class EncryptionUtils {
         return new EncryptedFile(encryptedFile, authenticationTagString);
         return new EncryptedFile(encryptedFile, authenticationTagString);
     }
     }
 
 
-    private static Cipher getEncoderCipher(byte[] encryptionKeyBytes, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException {
+    public static Cipher getCipher(int mode, byte[] encryptionKeyBytes, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException {
         Cipher cipher = Cipher.getInstance(AES_CIPHER);
         Cipher cipher = Cipher.getInstance(AES_CIPHER);
         Key key = new SecretKeySpec(encryptionKeyBytes, AES);
         Key key = new SecretKeySpec(encryptionKeyBytes, AES);
         GCMParameterSpec spec = new GCMParameterSpec(128, iv);
         GCMParameterSpec spec = new GCMParameterSpec(128, iv);
-        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
+        cipher.init(mode, key, spec);
         return cipher;
         return cipher;
     }
     }
 
 
@@ -616,29 +616,13 @@ public final class EncryptionUtils {
     }
     }
 
 
     // FIXME Decryption is broken
     // FIXME Decryption is broken
-    /**
-     * @param file               encrypted file
-     * @param encryptionKeyBytes key from metadata
-     * @param iv                 initialization vector from metadata
-     * @param authenticationTag  authenticationTag from metadata
-     * @return decrypted byte[]
-     */
-    public static byte[] decryptFile(File file,
-                                     byte[] encryptionKeyBytes,
-                                     byte[] iv,
+    public static byte[] decryptFile(
+                                    Cipher cipher,
+                                    File file,
                                      byte[] authenticationTag,
                                      byte[] authenticationTag,
                                      ArbitraryDataProvider arbitraryDataProvider,
                                      ArbitraryDataProvider arbitraryDataProvider,
                                      User user)
                                      User user)
-        throws NoSuchAlgorithmException,
-        InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException,
-        BadPaddingException, IllegalBlockSizeException, IOException {
-
-
-        Cipher cipher = Cipher.getInstance(AES_CIPHER);
-        Key key = new SecretKeySpec(encryptionKeyBytes, AES);
-        GCMParameterSpec spec = new GCMParameterSpec(128, iv);
-        cipher.init(Cipher.DECRYPT_MODE, key, spec);
-
+        throws BadPaddingException, IllegalBlockSizeException, IOException {
         RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
         RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
         byte[] fileBytes = new byte[(int) randomAccessFile.length()];
         byte[] fileBytes = new byte[(int) randomAccessFile.length()];
         randomAccessFile.readFully(fileBytes);
         randomAccessFile.readFully(fileBytes);