Explorar el Código

Remove all instances of kekHash

Vishnu Mohandas hace 4 años
padre
commit
317bbf8f18

+ 5 - 10
lib/core/configuration.dart

@@ -119,16 +119,12 @@ class Configuration {
     // Encrypt the key with this derived key
     final encryptedKeyData = CryptoUtil.encryptSync(key, kek);
 
-    // Hash the password so that its correctness can be compared later
-    final kekHash = await CryptoUtil.hash(kek);
-
     // Generate a public-private keypair and encrypt the latter
     final keyPair = await CryptoUtil.generateKeyPair();
     final encryptedSecretKeyData = CryptoUtil.encryptSync(keyPair.sk, key);
 
     final attributes = KeyAttributes(
       Sodium.bin2base64(kekSalt),
-      kekHash,
       Sodium.bin2base64(encryptedKeyData.encryptedData),
       Sodium.bin2base64(encryptedKeyData.nonce),
       Sodium.bin2base64(keyPair.pk),
@@ -144,14 +140,13 @@ class Configuration {
       String password, KeyAttributes attributes) async {
     final kek = CryptoUtil.deriveKey(
         utf8.encode(password), Sodium.base642bin(attributes.kekSalt));
-    bool correctPassword = await CryptoUtil.verifyHash(kek, attributes.kekHash);
-    if (!correctPassword) {
+    var key;
+    try {
+      key = CryptoUtil.decryptSync(Sodium.base642bin(attributes.encryptedKey),
+          kek, Sodium.base642bin(attributes.keyDecryptionNonce));
+    } catch (e) {
       throw Exception("Incorrect password");
     }
-    final key = CryptoUtil.decryptSync(
-        Sodium.base642bin(attributes.encryptedKey),
-        kek,
-        Sodium.base642bin(attributes.keyDecryptionNonce));
     final secretKey = CryptoUtil.decryptSync(
         Sodium.base642bin(attributes.encryptedSecretKey),
         key,

+ 1 - 9
lib/models/key_attributes.dart

@@ -2,7 +2,6 @@ import 'dart:convert';
 
 class KeyAttributes {
   final String kekSalt;
-  final String kekHash;
   final String encryptedKey;
   final String keyDecryptionNonce;
   final String publicKey;
@@ -11,7 +10,6 @@ class KeyAttributes {
 
   KeyAttributes(
     this.kekSalt,
-    this.kekHash,
     this.encryptedKey,
     this.keyDecryptionNonce,
     this.publicKey,
@@ -21,7 +19,6 @@ class KeyAttributes {
 
   KeyAttributes copyWith({
     String kekSalt,
-    String kekHash,
     String encryptedKey,
     String keyDecryptionNonce,
     String publicKey,
@@ -30,7 +27,6 @@ class KeyAttributes {
   }) {
     return KeyAttributes(
       kekSalt ?? this.kekSalt,
-      kekHash ?? this.kekHash,
       encryptedKey ?? this.encryptedKey,
       keyDecryptionNonce ?? this.keyDecryptionNonce,
       publicKey ?? this.publicKey,
@@ -42,7 +38,6 @@ class KeyAttributes {
   Map<String, dynamic> toMap() {
     return {
       'kekSalt': kekSalt,
-      'kekHash': kekHash,
       'encryptedKey': encryptedKey,
       'keyDecryptionNonce': keyDecryptionNonce,
       'publicKey': publicKey,
@@ -56,7 +51,6 @@ class KeyAttributes {
 
     return KeyAttributes(
       map['kekSalt'],
-      map['kekHash'],
       map['encryptedKey'],
       map['keyDecryptionNonce'],
       map['publicKey'],
@@ -72,7 +66,7 @@ class KeyAttributes {
 
   @override
   String toString() {
-    return 'KeyAttributes(kekSalt: $kekSalt, kekHash: $kekHash, encryptedKey: $encryptedKey, keyDecryptionNonce: $keyDecryptionNonce, publicKey: $publicKey, encryptedSecretKey: $encryptedSecretKey, secretKeyDecryptionNonce: $secretKeyDecryptionNonce)';
+    return 'KeyAttributes(kekSalt: $kekSalt, encryptedKey: $encryptedKey, keyDecryptionNonce: $keyDecryptionNonce, publicKey: $publicKey, encryptedSecretKey: $encryptedSecretKey, secretKeyDecryptionNonce: $secretKeyDecryptionNonce)';
   }
 
   @override
@@ -81,7 +75,6 @@ class KeyAttributes {
 
     return o is KeyAttributes &&
         o.kekSalt == kekSalt &&
-        o.kekHash == kekHash &&
         o.encryptedKey == encryptedKey &&
         o.keyDecryptionNonce == keyDecryptionNonce &&
         o.publicKey == publicKey &&
@@ -92,7 +85,6 @@ class KeyAttributes {
   @override
   int get hashCode {
     return kekSalt.hashCode ^
-        kekHash.hashCode ^
         encryptedKey.hashCode ^
         keyDecryptionNonce.hashCode ^
         publicKey.hashCode ^

+ 0 - 3
lib/ui/settings_page.dart

@@ -525,9 +525,6 @@ class DebugWidget extends StatelessWidget {
           Text("KEK Salt", style: TextStyle(fontWeight: FontWeight.bold)),
           Text(keyAttributes.kekSalt),
           Padding(padding: EdgeInsets.all(12)),
-          Text("KEK Hash", style: TextStyle(fontWeight: FontWeight.bold)),
-          Text(keyAttributes.kekHash),
-          Padding(padding: EdgeInsets.all(12)),
         ]),
       ),
       actions: [

+ 0 - 26
lib/utils/crypto_util.dart

@@ -1,4 +1,3 @@
-import 'dart:convert';
 import 'dart:typed_data';
 
 import 'dart:io' as io;
@@ -20,15 +19,6 @@ Uint8List cryptoSecretboxOpenEasy(Map<String, dynamic> args) {
       args["cipher"], args["nonce"], args["key"]);
 }
 
-Uint8List cryptoPwhashStr(Map<String, dynamic> args) {
-  return Sodium.cryptoPwhashStr(
-      args["input"], args["opsLimit"], args["memLimit"]);
-}
-
-bool cryptoPwhashStrVerify(Map<String, dynamic> args) {
-  return Sodium.cryptoPwhashStrVerify(args["hash"], args["input"]) == 0;
-}
-
 EncryptionResult chachaEncryptFile(Map<String, dynamic> args) {
   final encryptionStartTime = DateTime.now().millisecondsSinceEpoch;
   final logger = Logger("ChaChaEncrypt");
@@ -203,22 +193,6 @@ class CryptoUtil {
         Sodium.cryptoPwhashAlgDefault);
   }
 
-  static Future<String> hash(Uint8List input) async {
-    Sodium.init();
-    final args = Map<String, dynamic>();
-    args["input"] = input;
-    args["opsLimit"] = Sodium.cryptoPwhashOpslimitSensitive;
-    args["memLimit"] = Sodium.cryptoPwhashMemlimitModerate;
-    return utf8.decode(await _computer.compute(cryptoPwhashStr, param: args));
-  }
-
-  static Future<bool> verifyHash(Uint8List input, String hash) async {
-    final args = Map<String, dynamic>();
-    args["input"] = input;
-    args["hash"] = utf8.encode(hash);
-    return await _computer.compute(cryptoPwhashStrVerify, param: args);
-  }
-
   static Future<KeyPair> generateKeyPair() async {
     return Sodium.cryptoBoxKeypair();
   }