Remove all assumptions related to key attribute encoding

This commit is contained in:
Vishnu Mohandas 2020-09-10 02:39:41 +05:30
parent 5fffea1824
commit 94a41a72c9
5 changed files with 50 additions and 48 deletions

View file

@ -73,9 +73,9 @@ class Configuration {
if (!correctPassphrase) {
throw Exception("Incorrect passphrase");
}
final key = CryptoUtil.decryptFromBase64(
attributes.encryptedKey, base64.encode(kek), attributes.encryptedKeyIV);
await setKey(key);
final key = CryptoUtil.aesDecrypt(base64.decode(attributes.encryptedKey),
kek, base64.decode(attributes.encryptedKeyIV));
await setKey(base64.encode(key));
}
String getHttpEndpoint() {

View file

@ -53,7 +53,7 @@ class FileUploadManager {
final tempDirectory = Configuration.instance.getTempDirectory();
final encryptedFilePath = tempDirectory + encryptedFileName;
await CryptoUtil.encryptDataToFile(
await getBytesFromDisk(file), encryptedFilePath, base64EncodedKey);
await getBytesFromDisk(file), encryptedFilePath, key);
final fileUploadURL = await getUploadURL();
String fileObjectKey =
@ -65,7 +65,7 @@ class FileUploadManager {
file.generatedID.toString() + "_thumbnail.aes";
final encryptedThumbnailPath = tempDirectory + encryptedThumbnailName;
await CryptoUtil.encryptDataToFile(
thumbnailData, encryptedThumbnailPath, base64EncodedKey);
thumbnailData, encryptedThumbnailPath, key);
final thumbnailUploadURL = await getUploadURL();
String thumbnailObjectKey =
@ -73,7 +73,7 @@ class FileUploadManager {
final metadata = jsonEncode(file.getMetadata());
final encryptedMetadata = await CryptoUtil.encryptDataToData(
utf8.encode(metadata), base64EncodedKey);
utf8.encode(metadata), key);
final data = {
"fileObjectKey": fileObjectKey,
"thumbnailObjectKey": thumbnailObjectKey,

View file

@ -1,3 +1,6 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:photo_manager/photo_manager.dart';
import 'package:path/path.dart';
import 'package:photos/core/configuration.dart';
@ -135,12 +138,12 @@ class File {
Configuration.instance.getToken();
}
String getKey() {
Uint8List getKey() {
if (encryptedKey == null) {
return null;
}
return CryptoUtil.decryptFromBase64(
encryptedKey, Configuration.instance.getBase64EncodedKey(), encryptedKeyIV);
return CryptoUtil.aesDecrypt(base64.decode(encryptedKey),
Configuration.instance.getKey(), base64.decode(encryptedKeyIV));
}
@override

View file

@ -281,18 +281,20 @@ class PhotoSyncManager {
if (response != null) {
Bus.instance.fire(RemoteSyncEvent(true));
final diff = response.data["diff"] as List;
for (final json in diff) {
for (final fileItem in diff) {
final file = File();
file.uploadedFileID = json["id"];
file.ownerID = json["ownerID"];
file.updationTime = json["updationTime"];
file.uploadedFileID = fileItem["id"];
file.ownerID = fileItem["ownerID"];
file.updationTime = fileItem["updationTime"];
file.isEncrypted = true;
file.encryptedKey = json["encryptedKey"];
file.encryptedKeyIV = json["encryptedKeyIV"];
final key = CryptoUtil.decryptFromBase64(file.encryptedKey,
Configuration.instance.getBase64EncodedKey(), file.encryptedKeyIV);
file.encryptedKey = fileItem["encryptedKey"];
file.encryptedKeyIV = fileItem["encryptedKeyIV"];
final key = CryptoUtil.aesDecrypt(
base64.decode(file.encryptedKey),
Configuration.instance.getKey(),
base64.decode(file.encryptedKeyIV));
Map<String, dynamic> metadata = jsonDecode(utf8.decode(
await CryptoUtil.decryptDataToData(json["metadata"], key)));
await CryptoUtil.decryptDataToData(fileItem["metadata"], key)));
file.applyMetadata(metadata);
files.add(file);
}

View file

@ -31,18 +31,18 @@ class CryptoUtil {
.bytes;
}
static String decryptFromBase64(
String base64CipherText, String base64Key, String base64IV) {
final encrypter = AES(Key.fromBase64(base64Key), mode: AESMode.cbc);
return utf8.decode(encrypter.decrypt(
Encrypted.fromBase64(base64CipherText),
iv: IV.fromBase64(base64IV),
));
static Uint8List aesDecrypt(
Uint8List cipherText, Uint8List key, Uint8List iv) {
final encrypter = AES(Key(key), mode: AESMode.cbc);
return encrypter.decrypt(
Encrypted(cipherText),
iv: IV(iv),
);
}
static Future<String> encryptFileToFile(
String sourcePath, String destinationPath, String key) async {
final args = Map<String, String>();
String sourcePath, String destinationPath, Uint8List key) async {
final args = Map<String, dynamic>();
args["key"] = key;
args["source"] = sourcePath;
args["destination"] = destinationPath;
@ -50,20 +50,19 @@ class CryptoUtil {
}
static Future<String> encryptDataToFile(
Uint8List source, String destinationPath, String base64EncodedKey) async {
Uint8List source, String destinationPath, Uint8List key) async {
final args = Map<String, dynamic>();
args["key"] = base64EncodedKey;
args["key"] = key;
args["source"] = source;
args["destination"] = destinationPath;
return Computer().compute(runEncryptDataToFile, param: args);
}
static Future<String> encryptDataToData(
Uint8List source, String base64EncodedKey) async {
Uint8List source, Uint8List key) async {
final destinationPath =
Configuration.instance.getTempDirectory() + Uuid().v4();
return encryptDataToFile(source, destinationPath, base64EncodedKey)
.then((value) {
return encryptDataToFile(source, destinationPath, key).then((value) {
final file = io.File(destinationPath);
final data = file.readAsBytesSync();
file.deleteSync();
@ -71,52 +70,50 @@ class CryptoUtil {
});
}
static Future<void> decryptFileToFile(String sourcePath,
String destinationPath, String base64EncodedKey) async {
final args = Map<String, String>();
args["key"] = base64EncodedKey;
static Future<void> decryptFileToFile(
String sourcePath, String destinationPath, Uint8List key) async {
final args = Map<String, dynamic>();
args["key"] = key;
args["source"] = sourcePath;
args["destination"] = destinationPath;
return Computer().compute(runDecryptFileToFile, param: args);
}
static Future<Uint8List> decryptFileToData(
String sourcePath, String base64EncodedKey) {
final args = Map<String, String>();
args["key"] = base64EncodedKey;
static Future<Uint8List> decryptFileToData(String sourcePath, Uint8List key) {
final args = Map<String, dynamic>();
args["key"] = key;
args["source"] = sourcePath;
return Computer().compute(runDecryptFileToData, param: args);
}
static Future<Uint8List> decryptDataToData(
Uint8List source, String base64EncodedKey) {
static Future<Uint8List> decryptDataToData(Uint8List source, Uint8List key) {
final sourcePath = Configuration.instance.getTempDirectory() + Uuid().v4();
final file = io.File(sourcePath);
file.writeAsBytesSync(source);
return decryptFileToData(sourcePath, base64EncodedKey).then((value) {
return decryptFileToData(sourcePath, key).then((value) {
file.deleteSync();
return value;
});
}
}
Future<String> runEncryptFileToFile(Map<String, String> args) {
final encrypter = getEncrypter(args["key"]);
Future<String> runEncryptFileToFile(Map<String, dynamic> args) {
final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List));
return encrypter.encryptFile(args["source"], args["destination"]);
}
Future<String> runEncryptDataToFile(Map<String, dynamic> args) {
final encrypter = getEncrypter(args["key"]);
final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List));
return encrypter.encryptDataToFile(args["source"], args["destination"]);
}
Future<String> runDecryptFileToFile(Map<String, dynamic> args) async {
final encrypter = getEncrypter(args["key"]);
final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List));
return encrypter.decryptFile(args["source"], args["destination"]);
}
Future<Uint8List> runDecryptFileToData(Map<String, dynamic> args) async {
final encrypter = getEncrypter(args["key"]);
final encrypter = getEncrypter(base64.encode(args["key"] as Uint8List));
return encrypter.decryptDataFromFile(args["source"]);
}