Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
37 changes: 34 additions & 3 deletions lib/src/polyseed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
5 changes: 5 additions & 0 deletions lib/src/utils/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
48 changes: 48 additions & 0 deletions test/polyseed_password_test.dart
Original file line number Diff line number Diff line change
@@ -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<PasswordTooLongException>()),
);
});
}