fix(keys): a device refuses Argon2id costs beyond the register's ceiling

PasswordKdf.fromJson refused costs below the floor but took any above it,
so an instance could ask a device for 100 GiB and crash the app at every
sign-in. It now refuses more than the register's own ceiling (16 passes,
1 GiB; KeyRefusal.excessivePasswordKdf), and the app reads either refusal,
before deriving or sending anything, as PasswordProblem.unsafeCosts with
a text of its own (before, costs below the floor read as keys missing).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-20 20:04:37 +02:00
commit e126e1d4fb
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64
3 changed files with 126 additions and 4 deletions

View file

@ -31,6 +31,10 @@ enum KeyRefusal() {
/// have, or of another algorithm.
weakPasswordKdf,
/// Argon2id's costs named for an account are above what any account may
/// have: more memory or passes than a device may be asked to spend.
excessivePasswordKdf,
/// A recovery phrase of another length than BIP-39's.
phraseLength,

View file

@ -34,14 +34,28 @@ final class const PasswordKdf({
required final int memLimit,
}) {
/// The costs [json] names; refused ([KeyRefused]) for another algorithm or
/// costs below [minimum].
/// costs below [minimum] ([KeyRefusal.weakPasswordKdf]), and for costs
/// above [maximum] ([KeyRefusal.excessivePasswordKdf]).
factory fromJson(Map<String, Object?> json) => switch (json) {
{
'algorithm': 'argon2id13',
'opsLimit': final int opsLimit,
'memLimit': final int memLimit,
}
when opsLimit >= minimum.opsLimit && memLimit >= minimum.memLimit =>
when opsLimit < minimum.opsLimit || memLimit < minimum.memLimit =>
throw const KeyRefused(KeyRefusal.weakPasswordKdf),
{
'algorithm': 'argon2id13',
'opsLimit': final int opsLimit,
'memLimit': final int memLimit,
}
when opsLimit > maximum.opsLimit || memLimit > maximum.memLimit =>
throw const KeyRefused(KeyRefusal.excessivePasswordKdf),
{
'algorithm': 'argon2id13',
'opsLimit': final int opsLimit,
'memLimit': final int memLimit,
} =>
PasswordKdf(opsLimit: opsLimit, memLimit: memLimit),
_ => throw const KeyRefused(KeyRefusal.weakPasswordKdf),
};
@ -51,6 +65,15 @@ final class const PasswordKdf({
/// below it any more, or those accounts no longer open.
static const minimum = PasswordKdf(opsLimit: 3, memLimit: 64 * 1024 * 1024);
/// The ceiling no account's costs may go above, the register's own
/// (halkyon_base_server's `passwords.dart`): a register asking for more
/// would have the device run out of memory, or work for minutes, at every
/// sign-in. Moved together with the register's.
static const maximum = PasswordKdf(
opsLimit: 16,
memLimit: 1024 * 1024 * 1024,
);
/// What new passwords take: 64 MiB and 3 passes, a second or so on a
/// phone. Raising it leaves accounts made before at their own costs, at
/// or above [minimum].

View file

@ -103,6 +103,34 @@ Future<void> main() async {
expect(read.memLimit, PasswordKdf.minimum.memLimit);
});
test("the ceiling itself is read: the register's own, 16 passes and "
'1 GiB', () {
final read = PasswordKdf.fromJson(PasswordKdf.maximum.toJson());
expect(read.opsLimit, 16);
expect(read.memLimit, 1024 * 1024 * 1024);
});
test('above the ceiling are refused: a register asking for more would '
'make the device run out of memory, or time', () {
for (final json in <Map<String, Object?>>[
{'algorithm': 'argon2id13', 'opsLimit': 17, 'memLimit': 64 << 20},
{'algorithm': 'argon2id13', 'opsLimit': 3, 'memLimit': (1 << 30) + 1},
{'algorithm': 'argon2id13', 'opsLimit': 3, 'memLimit': 100 << 30},
]) {
expect(
() => PasswordKdf.fromJson(json),
throwsA(
isA<KeyRefused>().having(
(refused) => refused.reason,
'reason',
KeyRefusal.excessivePasswordKdf,
),
),
reason: '$json',
);
}
});
test('round-trip', () {
const raised = PasswordKdf(opsLimit: 4, memLimit: 128 * 1024 * 1024);
final json = raised.toJson();