Extract cipher from decryption

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-03-22 09:11:02 +01:00 committed by backportbot[bot]
parent 18f57f8e75
commit 543c0fd826
2 changed files with 12 additions and 26 deletions

View File

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

View File

@ -579,7 +579,7 @@ public final class EncryptionUtils {
InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidParameterSpecException {
File file = new File(ocFile.getStoragePath());
Cipher cipher = getEncoderCipher(encryptionKeyBytes, iv);
Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, encryptionKeyBytes, iv);
return encryptFile(file, cipher);
}
@ -591,11 +591,11 @@ public final class EncryptionUtils {
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);
Key key = new SecretKeySpec(encryptionKeyBytes, AES);
GCMParameterSpec spec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
cipher.init(mode, key, spec);
return cipher;
}
@ -616,29 +616,13 @@ public final class EncryptionUtils {
}
// 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,
ArbitraryDataProvider arbitraryDataProvider,
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");
byte[] fileBytes = new byte[(int) randomAccessFile.length()];
randomAccessFile.readFully(fileBytes);