diff --git a/.idea/dictionaries/herbert.xml b/.idea/dictionaries/herbert.xml index c942ae1..5fdbae9 100644 --- a/.idea/dictionaries/herbert.xml +++ b/.idea/dictionaries/herbert.xml @@ -1,6 +1,7 @@ + chacha consts derivator encrypter diff --git a/lib/src/crypto/protected_salt_generator.dart b/lib/src/crypto/protected_salt_generator.dart index a30bf69..3bfa3ee 100644 --- a/lib/src/crypto/protected_salt_generator.dart +++ b/lib/src/crypto/protected_salt_generator.dart @@ -3,7 +3,6 @@ import 'dart:typed_data'; import 'package:logging/logging.dart'; import 'package:crypto/crypto.dart'; -import 'package:cryptography/cryptography.dart' as cryptography; import 'package:pointycastle/export.dart'; final _logger = Logger('protected_salt_generator'); @@ -16,7 +15,8 @@ class ProtectedSaltGenerator { return ProtectedSaltGenerator._(cipher); } factory ProtectedSaltGenerator.chacha20(Uint8List key) { - return ChachaProtectedSaltGenerator.create(key); // Chacha20(); +// return ChachaProtectedSaltGenerator.create(key); // Chacha20(); + return ChachaPointyCastleProtectedSaltGenerator.create(key); } ProtectedSaltGenerator._(this._cipher); @@ -42,39 +42,21 @@ class ProtectedSaltGenerator { } } -class ChachaProtectedSaltGenerator implements ProtectedSaltGenerator { - ChachaProtectedSaltGenerator._(this._state); +class ChachaPointyCastleProtectedSaltGenerator extends ProtectedSaltGenerator { + ChachaPointyCastleProtectedSaltGenerator._(StreamCipher state) + : super._(state); - factory ChachaProtectedSaltGenerator.create(Uint8List key) { + factory ChachaPointyCastleProtectedSaltGenerator.create(Uint8List key) { final hash = sha512.convert(key); final secretKey = hash.bytes.sublist(0, 32); final nonce = hash.bytes.sublist(32, 32 + 12); - return ChachaProtectedSaltGenerator._(cryptography.chacha20.newState( - cryptography.SecretKey(secretKey), - nonce: cryptography.SecretKey(nonce))); - } - - final cryptography.KeyStreamCipherState _state; + final chacha20 = ChaCha20Engine(); + chacha20.init( + null, + ParametersWithIV( + KeyParameter(secretKey as Uint8List), nonce as Uint8List)); - @override - StreamCipher get _cipher => throw UnimplementedError(); - - @override - String decryptBase64(String protectedValue) { - final bytes = base64.decode(protectedValue); - if (bytes.isEmpty) { - _logger.warning('decoded base64 data has length 0'); - return null; - } - final result = _state.convert(bytes); - return utf8.decode(result); - } - - @override - String encryptToBase64(String plainValue) { - final input = utf8.encode(plainValue) as Uint8List; - final encrypted = _state.convert(input); - return base64.encode(encrypted); + return ChachaPointyCastleProtectedSaltGenerator._(chacha20); } } diff --git a/lib/src/kdbx_format.dart b/lib/src/kdbx_format.dart index efa72b3..98c2321 100644 --- a/lib/src/kdbx_format.dart +++ b/lib/src/kdbx_format.dart @@ -6,7 +6,6 @@ import 'dart:typed_data'; import 'package:argon2_ffi_base/argon2_ffi_base.dart'; import 'package:convert/convert.dart' as convert; import 'package:crypto/crypto.dart' as crypto; -import 'package:cryptography/cryptography.dart' as cryptography; import 'package:kdbx/kdbx.dart'; import 'package:kdbx/src/crypto/key_encrypter_kdf.dart'; import 'package:kdbx/src/crypto/protected_salt_generator.dart'; @@ -543,9 +542,9 @@ class KdbxFormat { Uint8List transformContentV4ChaCha20( KdbxHeader header, Uint8List encrypted, Uint8List cipherKey) { final encryptionIv = header.fields[HeaderFields.EncryptionIV].bytes; - final key = cryptography.SecretKey(cipherKey); - final nonce = cryptography.SecretKey(encryptionIv); - return cryptography.chacha20.decrypt(encrypted, key, nonce: nonce); + final engine = ChaCha20Engine() + ..init(false, ParametersWithIV(KeyParameter(cipherKey), encryptionIv)); + return engine.process(encrypted); } // Uint8List _transformDataV4Aes() { diff --git a/pubspec.yaml b/pubspec.yaml index 453fcf8..b292552 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,7 +13,6 @@ dependencies: logging: '>=0.11.3+2 <1.0.0' crypto: '>=2.0.0 <3.0.0' pointycastle: '>=1.0.1 <2.0.0' - cryptography: ^0.1.2 xml: '>=4.0.0 <5.0.0' uuid: '>=2.0.0 <3.0.0' meta: '>=1.0.0 <2.0.0' @@ -34,3 +33,15 @@ dependencies: dev_dependencies: pedantic: '>=1.7.0 <2.0.0' test: '>=1.6.0 <2.0.0' + +dependency_overrides: +# pointycastle: +# git: +# url: https://github.com/bcgit/pc-dart.git +# ref: 7c8a8b47faa75432fe3fb9a739db4a289e12b341 + pointycastle: + git: + url: https://github.com/authpass/pc-dart.git + ref: 38fa40e966153b3d3cf93a9bd95228f76af3b9d7 +# pointycastle: +# path: /Users/herbert/dev/pointycastle