From c8b49e6b5cb0f4215501238176a66e400f8e2727 Mon Sep 17 00:00:00 2001 From: Herbert Poul Date: Wed, 18 Mar 2020 20:53:13 +0100 Subject: [PATCH] support for chacha20 encryption. --- lib/src/kdbx_format.dart | 16 +++++++++++--- test/chacha20.kdbx | Bin 0 -> 1536 bytes test/kdbx4_test.dart | 45 ++++++++++++++++++++++++-------------- test_output_chacha20.kdbx | Bin 0 -> 1604 bytes 4 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 test/chacha20.kdbx create mode 100644 test_output_chacha20.kdbx diff --git a/lib/src/kdbx_format.dart b/lib/src/kdbx_format.dart index caf37db..0e926e2 100644 --- a/lib/src/kdbx_format.dart +++ b/lib/src/kdbx_format.dart @@ -5,6 +5,7 @@ import 'dart:typed_data'; 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/argon2.dart'; import 'package:kdbx/src/crypto/key_encrypter_kdf.dart'; @@ -285,8 +286,8 @@ class KdbxBody extends KdbxNode { return result; } else if (cipherId == CryptoConsts.CIPHER_IDS[Cipher.chaCha20].uuid) { _logger.fine('We need chacha20'); - // TODO can we combine this with _encryptV3? - throw UnsupportedError('Unsupported cipher chacha20 for kdbx 4.x'); + return kdbxFile.kdbxFormat + .transformContentV4ChaCha20(header, compressedBytes, cipherKey); } else { throw UnsupportedError('Unsupported cipherId $cipherId'); } @@ -509,12 +510,21 @@ class KdbxFormat { return result; } else if (cipherId == CryptoConsts.CIPHER_IDS[Cipher.chaCha20].uuid) { _logger.fine('We need chacha20'); - throw UnsupportedError('chacha20 not yet supported $cipherId'); +// throw UnsupportedError('chacha20 not yet supported $cipherId'); + return transformContentV4ChaCha20(header, encrypted, cipherKey); } else { throw UnsupportedError('Unsupported cipherId $cipherId'); } } + 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); + } + // Uint8List _transformDataV4Aes() { // } diff --git a/test/chacha20.kdbx b/test/chacha20.kdbx new file mode 100644 index 0000000000000000000000000000000000000000..bc488fa3de00a87241b514ce1876fb98acbe53a5 GIT binary patch literal 1536 zcmV+b2LJg3*`k_f`%AR|00aO65C8xG)&q(wi*HP|r6e<&G26A80|Wp70096100bZa z006h-7~w=f>cyT@6bX7ON5WRoh6`QIji)LO-W;l4MHB}N0002W+09(MY3dr#;b%n) zivR!s00BY;0000aRaHqu5C8xG?_+J>j44D*k@u;j1LFz|1pxp607(b{000UA00000 z000F60000@2mk;800004000001OWg508j(~00062002S(0000}AOHXW5F$Ust@=p) z{5ElK-7X}yq(Kj9mQ>!dyq*5EA;1OWg509FJ5000vJ000001ONa44GIkk@+a9- zxB}5o7VkcsZsKvqyodty-L2_DRQqafT>sR`%b?&>{^i7)gSH|m{d5(c&&~;|&jf~4 z#4MXxGn%@V4*Q>EwsRk2-rrRqxP96ekqdr>3u>Jrf(e*^#kc!W-mSkejS z=-+z)>7d|{xmuBRg2;vfVGvna* zzBmPbmUAS9^2tGYR!%GdKoAWc3OwPWxTq9jJ~!8Fm?KFf%*4t-&QWR81$h@pDOxiu z)&prKJMFNM%F-R#G-=X!L!lc=tnb4c7i`SEIywV8UnZKsSgex+>SN!Q+ zAz44u#oN1ncFrk>W36t^>}o{cH)!2@_Z8bN|uiIMXwUJtFZJhj@e5r zZ+}qbiNQ{b`d_GRFDT0pGWJP~Gp`+DQ~Ll^y(vCW$iEifoyOZo3N_+EtEWxu@f|c8 zv*I{FzwTSL&wC-FA94}FujA;No-g&9KLAXNFNOeMBs+1`6XK3&!l{FLtZq;t6S0IL zqMhol@>in~?2-o>J7bC$pnnHS0FrS#@?lKY5S@chhx)+0-nUNzzJL3BXCKwzcS1JJ zbyd7AISQ^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