|
|
@ -252,7 +252,9 @@ class KdbxBody extends KdbxNode { |
|
|
|
compressedBytes, |
|
|
|
compressedBytes, |
|
|
|
keys.cipherKey, |
|
|
|
keys.cipherKey, |
|
|
|
); |
|
|
|
); |
|
|
|
writer.writeBytes(encrypted); |
|
|
|
final transformed = kdbxFile.kdbxFormat |
|
|
|
|
|
|
|
.hmacBlockTransformerEncrypt(keys.hmacKey, encrypted); |
|
|
|
|
|
|
|
writer.writeBytes(transformed); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Uint8List _encryptV3(KdbxFile kdbxFile, Uint8List compressedBytes) { |
|
|
|
Uint8List _encryptV3(KdbxFile kdbxFile, Uint8List compressedBytes) { |
|
|
@ -344,15 +346,20 @@ class KdbxFormat { |
|
|
|
Credentials credentials, |
|
|
|
Credentials credentials, |
|
|
|
String name, { |
|
|
|
String name, { |
|
|
|
String generator, |
|
|
|
String generator, |
|
|
|
|
|
|
|
KdbxHeader header, |
|
|
|
}) { |
|
|
|
}) { |
|
|
|
final header = KdbxHeader.create(); |
|
|
|
|
|
|
|
final meta = KdbxMeta.create( |
|
|
|
final meta = KdbxMeta.create( |
|
|
|
databaseName: name, |
|
|
|
databaseName: name, |
|
|
|
generator: generator, |
|
|
|
generator: generator, |
|
|
|
); |
|
|
|
); |
|
|
|
final rootGroup = KdbxGroup.create(parent: null, name: name); |
|
|
|
final rootGroup = KdbxGroup.create(parent: null, name: name); |
|
|
|
final body = KdbxBody.create(meta, rootGroup); |
|
|
|
final body = KdbxBody.create(meta, rootGroup); |
|
|
|
return KdbxFile(this, credentials, header, body); |
|
|
|
return KdbxFile( |
|
|
|
|
|
|
|
this, |
|
|
|
|
|
|
|
credentials, |
|
|
|
|
|
|
|
header ?? KdbxHeader.create(), |
|
|
|
|
|
|
|
body, |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
KdbxFile read(Uint8List input, Credentials credentials) { |
|
|
|
KdbxFile read(Uint8List input, Credentials credentials) { |
|
|
@ -428,29 +435,58 @@ class KdbxFormat { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Uint8List hmacBlockTransformer(Uint8List hmacKey, ReaderHelper reader) { |
|
|
|
Uint8List hmacBlockTransformerEncrypt(Uint8List hmacKey, Uint8List data) { |
|
|
|
final ret = <int>[]; |
|
|
|
final writer = WriterHelper(); |
|
|
|
|
|
|
|
final reader = ReaderHelper(data); |
|
|
|
|
|
|
|
const blockSize = 1024 * 1024; |
|
|
|
int blockIndex = 0; |
|
|
|
int blockIndex = 0; |
|
|
|
while (true) { |
|
|
|
while (true) { |
|
|
|
|
|
|
|
final blockData = reader.readBytesUpTo(blockSize); |
|
|
|
|
|
|
|
final calculatedHash = _hmacHashForBlock(hmacKey, blockIndex, blockData); |
|
|
|
|
|
|
|
writer.writeBytes(calculatedHash); |
|
|
|
|
|
|
|
writer.writeUint32(blockData.length); |
|
|
|
|
|
|
|
if (blockData.isEmpty) { |
|
|
|
|
|
|
|
// writer.writeUint32(0); |
|
|
|
|
|
|
|
return writer.output.toBytes(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
writer.writeBytes(blockData); |
|
|
|
|
|
|
|
blockIndex++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Uint8List _hmacKeyForBlockIndex(Uint8List hmacKey, int blockIndex) { |
|
|
|
final blockKeySrc = WriterHelper() |
|
|
|
final blockKeySrc = WriterHelper() |
|
|
|
..writeUint64(blockIndex) |
|
|
|
..writeUint64(blockIndex) |
|
|
|
..writeBytes(hmacKey); |
|
|
|
..writeBytes(hmacKey); |
|
|
|
final blockKey = crypto.sha512.convert(blockKeySrc.output.toBytes()); |
|
|
|
return crypto.sha512.convert(blockKeySrc.output.toBytes()).bytes |
|
|
|
|
|
|
|
as Uint8List; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final blockHash = reader.readBytes(32); |
|
|
|
Uint8List _hmacHashForBlock( |
|
|
|
final blockLength = reader.readUint32(); |
|
|
|
Uint8List hmacKey, int blockIndex, Uint8List blockData) { |
|
|
|
final blockBytes = reader.readBytes(blockLength); |
|
|
|
final blockKey = _hmacKeyForBlockIndex(hmacKey, blockIndex); |
|
|
|
final tmp = WriterHelper(); |
|
|
|
final tmp = WriterHelper(); |
|
|
|
tmp.writeUint64(blockIndex); |
|
|
|
tmp.writeUint64(blockIndex); |
|
|
|
tmp.writeInt32(blockLength); |
|
|
|
tmp.writeInt32(blockData.length); |
|
|
|
tmp.writeBytes(blockBytes); |
|
|
|
tmp.writeBytes(blockData); |
|
|
|
// _logger.fine('blockHash: ${ByteUtils.toHexList(tmp.output.toBytes())}'); |
|
|
|
// _logger.fine('blockHash: ${ByteUtils.toHexList(tmp.output.toBytes())}'); |
|
|
|
// _logger.fine('blockKey: ${ByteUtils.toHexList(blockKey.bytes)}'); |
|
|
|
// _logger.fine('blockKey: ${ByteUtils.toHexList(blockKey.bytes)}'); |
|
|
|
final hmac = crypto.Hmac(crypto.sha256, blockKey.bytes); |
|
|
|
final hmac = crypto.Hmac(crypto.sha256, blockKey); |
|
|
|
final calculatedHash = hmac.convert(tmp.output.toBytes()); |
|
|
|
final calculatedHash = hmac.convert(tmp.output.toBytes()); |
|
|
|
|
|
|
|
return calculatedHash.bytes as Uint8List; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Uint8List hmacBlockTransformer(Uint8List hmacKey, ReaderHelper reader) { |
|
|
|
|
|
|
|
final ret = <int>[]; |
|
|
|
|
|
|
|
int blockIndex = 0; |
|
|
|
|
|
|
|
while (true) { |
|
|
|
|
|
|
|
final blockHash = reader.readBytes(32); |
|
|
|
|
|
|
|
final blockLength = reader.readUint32(); |
|
|
|
|
|
|
|
final blockBytes = reader.readBytes(blockLength); |
|
|
|
|
|
|
|
final calculatedHash = _hmacHashForBlock(hmacKey, blockIndex, blockBytes); |
|
|
|
// _logger |
|
|
|
// _logger |
|
|
|
// .fine('CalculatedHash: ${ByteUtils.toHexList(calculatedHash.bytes)}'); |
|
|
|
// .fine('CalculatedHash: ${ByteUtils.toHexList(calculatedHash.bytes)}'); |
|
|
|
if (!ByteUtils.eq(blockHash, calculatedHash.bytes)) { |
|
|
|
if (!ByteUtils.eq(blockHash, calculatedHash)) { |
|
|
|
throw KdbxCorruptedFileException('Invalid hash block.'); |
|
|
|
throw KdbxCorruptedFileException('Invalid hash block.'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -499,7 +535,9 @@ class KdbxFormat { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final credentialHash = credentials.getHash(); |
|
|
|
final credentialHash = credentials.getHash(); |
|
|
|
|
|
|
|
_logger.finest('credentialHash: ${ByteUtils.toHexList(credentialHash)}'); |
|
|
|
final key = KeyEncrypterKdf(argon2).encrypt(credentialHash, kdfParameters); |
|
|
|
final key = KeyEncrypterKdf(argon2).encrypt(credentialHash, kdfParameters); |
|
|
|
|
|
|
|
_logger.finest('key: ${ByteUtils.toHexList(key)}'); |
|
|
|
|
|
|
|
|
|
|
|
// final keyWithSeed = Uint8List(65); |
|
|
|
// final keyWithSeed = Uint8List(65); |
|
|
|
// keyWithSeed.replaceRange(0, masterSeed.length, masterSeed); |
|
|
|
// keyWithSeed.replaceRange(0, masterSeed.length, masterSeed); |
|
|
@ -589,17 +627,15 @@ class KdbxFormat { |
|
|
|
return decrypted; |
|
|
|
return decrypted; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// TODO combine this with [_decryptContentV4] |
|
|
|
/// TODO combine this with [_decryptContentV4] (or [_encryptDataAes]?) |
|
|
|
Uint8List _encryptContentV4Aes( |
|
|
|
Uint8List _encryptContentV4Aes( |
|
|
|
KdbxHeader header, Uint8List cipherKey, Uint8List bytes) { |
|
|
|
KdbxHeader header, Uint8List cipherKey, Uint8List bytes) { |
|
|
|
final encryptionIv = header.fields[HeaderFields.EncryptionIV].bytes; |
|
|
|
final encryptionIv = header.fields[HeaderFields.EncryptionIV].bytes; |
|
|
|
final decryptCipher = CBCBlockCipher(AESFastEngine()); |
|
|
|
final encryptCypher = CBCBlockCipher(AESFastEngine()); |
|
|
|
decryptCipher.init( |
|
|
|
encryptCypher.init( |
|
|
|
true, ParametersWithIV(KeyParameter(cipherKey), encryptionIv)); |
|
|
|
true, ParametersWithIV(KeyParameter(cipherKey), encryptionIv)); |
|
|
|
final paddedDecrypted = AesHelper.processBlocks(decryptCipher, bytes); |
|
|
|
final paddedBytes = AesHelper.pad(bytes, encryptCypher.blockSize); |
|
|
|
|
|
|
|
return AesHelper.processBlocks(encryptCypher, paddedBytes); |
|
|
|
final decrypted = AesHelper.unpad(paddedDecrypted); |
|
|
|
|
|
|
|
return decrypted; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Uint8List _generateMasterKeyV3( |
|
|
|
static Uint8List _generateMasterKeyV3( |
|
|
|