Browse Source

add support for kdbx4 to. command line tool

remove-cryptography-dependency
Herbert Poul 5 years ago
parent
commit
40525a08cd
  1. 50
      bin/_argon2.dart
  2. 13
      bin/kdbx.dart
  3. 1
      lib/src/kdbx_format.dart

50
bin/_argon2.dart

@ -0,0 +1,50 @@
// ignore_for_file: non_constant_identifier_names
//typedef HashStuff = Pointer<Utf8> Function(Pointer<Utf8> str);
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart';
import 'package:kdbx/kdbx.dart';
// TODO: This should be somehow combined with the test variant
// which also loads the requierd dylib/so files.
typedef Argon2HashNative = Pointer<Utf8> Function(
Pointer<Uint8> key,
IntPtr keyLen,
Pointer<Uint8> salt,
Uint64 saltlen,
Uint32 m_cost, // memory cost
Uint32 t_cost, // time cost (number iterations)
Uint32 parallelism,
IntPtr hashlen,
Uint8 type,
Uint32 version,
);
typedef Argon2Hash = Pointer<Utf8> Function(
Pointer<Uint8> key,
int keyLen,
Pointer<Uint8> salt,
int saltlen,
int m_cost, // memory cost
int t_cost, // time cost (number iterations)
int parallelism,
int hashlen,
int type,
int version,
);
class Argon2Test extends Argon2Base {
Argon2Test() {
final argon2lib = Platform.isMacOS
? DynamicLibrary.open('libargon2_ffi.dylib')
: DynamicLibrary.open('./libargon2_ffi.so');
argon2hash = argon2lib
.lookup<NativeFunction<Argon2HashNative>>('hp_argon2_hash')
.asFunction();
}
@override
Argon2Hash argon2hash;
}

13
bin/kdbx.dart

@ -11,6 +11,8 @@ import 'package:logging/logging.dart';
import 'package:logging_appenders/logging_appenders.dart'; import 'package:logging_appenders/logging_appenders.dart';
import 'package:prompts/prompts.dart' as prompts; import 'package:prompts/prompts.dart' as prompts;
import '_argon2.dart';
final _logger = Logger('kdbx'); final _logger = Logger('kdbx');
void main(List<String> arguments) { void main(List<String> arguments) {
@ -63,6 +65,12 @@ abstract class KdbxFileCommand extends Command<void> {
help: 'Input kdbx file', help: 'Input kdbx file',
valueHelp: 'foo.kdbx', valueHelp: 'foo.kdbx',
); );
argParser.addOption(
'password',
abbr: 'p',
help: 'password',
valueHelp: 'asdf',
);
} }
@override @override
@ -72,9 +80,10 @@ abstract class KdbxFileCommand extends Command<void> {
usageException('Required argument: --input'); usageException('Required argument: --input');
} }
final bytes = await File(inputFile).readAsBytes(); final bytes = await File(inputFile).readAsBytes();
final password = prompts.get('Password for $inputFile', final password = argResults['password'] as String ??
prompts.get('Password for $inputFile',
conceal: true, validate: (str) => str.isNotEmpty); conceal: true, validate: (str) => str.isNotEmpty);
final file = await KdbxFormat(null) final file = await KdbxFormat(Argon2Test())
.read(bytes, Credentials(ProtectedValue.fromString(password))); .read(bytes, Credentials(ProtectedValue.fromString(password)));
return runWithFile(file); return runWithFile(file);
} }

1
lib/src/kdbx_format.dart

@ -632,6 +632,7 @@ class KdbxFormat {
Uint8List _decryptContentV4( Uint8List _decryptContentV4(
KdbxHeader header, Uint8List cipherKey, Uint8List encryptedPayload) { KdbxHeader header, Uint8List cipherKey, Uint8List encryptedPayload) {
final encryptionIv = header.fields[HeaderFields.EncryptionIV].bytes; final encryptionIv = header.fields[HeaderFields.EncryptionIV].bytes;
final decryptCipher = CBCBlockCipher(AESFastEngine()); final decryptCipher = CBCBlockCipher(AESFastEngine());
decryptCipher.init( decryptCipher.init(
false, ParametersWithIV(KeyParameter(cipherKey), encryptionIv)); false, ParametersWithIV(KeyParameter(cipherKey), encryptionIv));

Loading…
Cancel
Save