Compare commits

...

2 Commits

  1. 1
      .idea/dictionaries/herbert.xml
  2. 42
      lib/src/crypto/protected_salt_generator.dart
  3. 8
      lib/src/kdbx_format.dart
  4. 13
      pubspec.yaml

1
.idea/dictionaries/herbert.xml

@ -1,6 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="herbert">
<words>
<w>chacha</w>
<w>consts</w>
<w>derivator</w>
<w>encrypter</w>

42
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 = ChaCha7539Engine()
..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);
}
}

8
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';
@ -24,6 +23,7 @@ import 'package:kdbx/src/kdbx_xml.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:pointycastle/export.dart';
import 'package:pointycastle/stream/chacha7539.dart';
import 'package:xml/xml.dart' as xml;
final _logger = Logger('kdbx.format');
@ -543,9 +543,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 = ChaCha7539Engine()
..init(false, ParametersWithIV(KeyParameter(cipherKey), encryptionIv));
return engine.process(encrypted);
}
// Uint8List _transformDataV4Aes() {

13
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

Loading…
Cancel
Save