diff --git a/CHANGELOG.md b/CHANGELOG.md index bb4bdce..32db7f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.8 + +- Normalize encryption passphrases with NFKD, and use at most 575 bytes, matching Monero + ## 0.0.7 - Improve compatibility with japanese strings using U+0020 separators diff --git a/lib/src/polyseed.dart b/lib/src/polyseed.dart index 9e3cc87..4304a94 100644 --- a/lib/src/polyseed.dart +++ b/lib/src/polyseed.dart @@ -31,6 +31,7 @@ import 'package:polyseed/src/polyseed_features.dart'; import 'package:polyseed/src/polyseed_storage.dart'; import 'package:polyseed/src/utils/exceptions.dart'; import 'package:polyseed/src/utils/store_uint8list_extension.dart'; +import 'package:unorm_dart/unorm_dart.dart' as unorm; class Polyseed { /// The number of words required for a valid Polyseed phrase @@ -96,7 +97,6 @@ class Polyseed { final words = lang.normalizeSeparator(str).split(lang.separator); final poly = GFPoly(); - // split into words if (words.length != numberOfWords) { throw WordNumberException(); @@ -179,8 +179,7 @@ class Polyseed { salt[14] = 0xff; salt[15] = 0xff; - final mask = - _deriveKey(Uint8List.fromList(utf8.encode(password)), salt, 32); + final mask = _deriveKey(_polyseedPasswordBytes(password), salt, 32); // apply mask for (var i = 0; i < GFPoly.secretSize; ++i) { @@ -222,3 +221,35 @@ class Polyseed { return derivator.process(password); } } + +// polyseed.h POLYSEED_STR_SIZE, including the terminating null. +const _polyseedStrSize = 576; + +// Bytes passed to PBKDF2. Matches utf8_nfkd_lazy in tevador/polyseed: +// an all-ASCII passphrase is truncated to 575 bytes, and any non-ASCII +// passphrase is NFKD-normalized. Monero rejects a normalized passphrase +// that does not fit in that buffer. +Uint8List _polyseedPasswordBytes(String password) { + final raw = utf8.encode(password); + final scan = + raw.length < _polyseedStrSize - 1 ? raw.length : _polyseedStrSize - 1; + var nonAscii = false; + for (var i = 0; i < scan; i++) { + if (raw[i] >= 0x80) { + nonAscii = true; + break; + } + } + if (!nonAscii) { + if (raw.length <= _polyseedStrSize - 1) { + return Uint8List.fromList(raw); + } + return Uint8List.fromList(raw.sublist(0, _polyseedStrSize - 1)); + } + + final normalized = utf8.encode(unorm.nfkd(password)); + if (normalized.length > _polyseedStrSize - 1) { + throw PasswordTooLongException(); + } + return Uint8List.fromList(normalized); +} diff --git a/lib/src/utils/exceptions.dart b/lib/src/utils/exceptions.dart index 7cfdc36..e2df7f7 100644 --- a/lib/src/utils/exceptions.dart +++ b/lib/src/utils/exceptions.dart @@ -30,3 +30,8 @@ class UnsupportedSeedFeatureException extends PolyseedException { class InvalidSeedFormatException extends PolyseedException { InvalidSeedFormatException() : super("Invalid seed format"); } + +class PasswordTooLongException extends PolyseedException { + PasswordTooLongException() + : super("Passphrase is too long after NFKD normalization"); +} diff --git a/pubspec.yaml b/pubspec.yaml index af8a47d..d83ce9b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: polyseed description: A pure dart implementation of the 16-word seed scheme for monero -version: 0.0.7 +version: 0.0.8 homepage: https://cakelabs.com repository: https://github.com/cake-tech/polyseed_dart issue_tracker: https://github.com/cake-tech/polyseed_dart/issues diff --git a/test/polyseed_password_test.dart b/test/polyseed_password_test.dart new file mode 100644 index 0000000..5a13456 --- /dev/null +++ b/test/polyseed_password_test.dart @@ -0,0 +1,48 @@ +import 'dart:convert'; + +import 'package:polyseed/polyseed.dart'; +import 'package:polyseed/src/utils/exceptions.dart'; +import 'package:test/test.dart'; +import 'package:unorm_dart/unorm_dart.dart' as unorm; + +void main() { + const phrase = + "unaware yard donate shallow slot sing oil oxygen loyal bench near hill surround forum execute lamp"; + final coin = PolyseedCoin.POLYSEED_MONERO; + final english = PolyseedLang.getByEnglishName("English"); + + String encryptedPhrase(String password) { + final seed = Polyseed.decode(phrase, english, coin); + seed.crypt(password); + return seed.encode(english, coin); + } + + test('precomposed and decomposed passphrases encrypt the same phrase', () { + const precomposed = 'contraseña'; + final decomposed = unorm.nfkd(precomposed); + expect(decomposed, isNot(precomposed)); + expect(encryptedPhrase(precomposed), encryptedPhrase(decomposed)); + }); + + test('Hangul and kana passphrases match their NFKD form', () { + for (final password in ['비밀번호', 'パスワード', 'pässwörd']) { + final decomposed = unorm.nfkd(password); + expect(decomposed, isNot(password)); + expect(encryptedPhrase(password), encryptedPhrase(decomposed)); + } + }); + + test('an ASCII passphrase longer than 575 bytes uses the first 575', () { + final prefix = 'a' * 575; + expect(encryptedPhrase(prefix), encryptedPhrase('${prefix}b')); + }); + + test('NFKD output longer than 575 bytes is rejected', () { + final password = 'ñ' * 200; + expect(utf8.encode(unorm.nfkd(password)).length, greaterThan(575)); + expect( + () => encryptedPhrase(password), + throwsA(isA()), + ); + }); +}