Skip to content

Argon2 native-int implementation returns wrong output under dart2wasm #266

Open

Description

@pburlov

Version: pointycastle 4.0.0, Dart SDK 3.13.7 (stable), macOS arm64, Chrome

Summary
argon2_native_int_impl.dart computes a different (wrong) tag when compiled with dart2wasm.
The Register64 implementation is correct on the same platform. argon2.dart currently selects
the implementation with if (dart.library.io), so web users get Register64 and are not affected
today. But the native-int path would be the obvious fast choice for wasm (64-bit ints are native
there), and if the conditional import is ever changed, web builds would silently derive different
keys than native builds.

Reproduction (RFC 9106 §5.3 Argon2id test vector; dependencies: pointycastle: 4.0.0, test: ^1.25.0)

import 'dart:typed_data';

import 'package:pointycastle/api.dart';
import 'package:pointycastle/key_derivators/api.dart';
import 'package:pointycastle/key_derivators/argon2_native_int_impl.dart'
    as native_int;
import 'package:pointycastle/key_derivators/argon2_register64_impl.dart'
    as register64;
import 'package:test/test.dart';

String hex(Uint8List b) =>
    b.map((x) => x.toRadixString(16).padLeft(2, '0')).join();

// RFC 9106, section 5.3 (Argon2id test vector)
final rfcParams = Argon2Parameters(
  Argon2Parameters.ARGON2_id,
  Uint8List.fromList(List.filled(16, 0x02)),
  secret: Uint8List.fromList(List.filled(8, 0x03)),
  additional: Uint8List.fromList(List.filled(12, 0x04)),
  desiredKeyLength: 32,
  iterations: 3,
  memory: 32,
  lanes: 4,
  version: Argon2Parameters.ARGON2_VERSION_13,
);
const rfcTag =
    '0d640df58d78766c08c037a34a8b53c9d01ef0452d75b65eb52520e96b01e659';

Uint8List derive(KeyDerivator d) {
  d.init(rfcParams);
  return d.process(Uint8List.fromList(List.filled(32, 0x01)));
}

void main() {
  test('Register64 implementation matches RFC 9106', () {
    expect(hex(derive(register64.Argon2BytesGenerator())), rfcTag);
  });
  test('native-int implementation matches RFC 9106', () {
    expect(hex(derive(native_int.Argon2BytesGenerator())), rfcTag);
  });
}

dart test (VM): both tests pass.
dart test -p chrome -c dart2wasm: Register64 passes, native-int fails:

Expected: '0d640df58d78766c08c037a34a8b53c9d01ef0452d75b65eb52520e96b01e659'
  Actual: '82e7f335a33dfaa416e809348c08c3e717be3838165219202c0aa98f33b7e417'

The same code is unchanged on current master. I have not narrowed down the cause. Since ints are 64-bit both on the VM and under dart2wasm,
this may also be a dart2wasm compiler issue rather than a pointycastle one; happy to move it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions