Skip to content

fix(derive-bits): support zero-length results#321

Open
harrshita123 wants to merge 1 commit into
google:masterfrom
harrshita123:agent/derive-bits-zero-length
Open

fix(derive-bits): support zero-length results#321
harrshita123 wants to merge 1 commit into
google:masterfrom
harrshita123:agent/derive-bits-zero-length

Conversation

@harrshita123

@harrshita123 harrshita123 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Summary

  • allow the native ECDH backend to return an empty secret for length == 0 while continuing to reject negative lengths
  • short-circuit native PBKDF2 with an empty result after validating iterations
  • add shared regression coverage that runs against native, dart2js, and dart2wasm backends

Why

The browser implementation already returns an empty Uint8List for zero-bit ECDH and PBKDF2 derivations, as defined by the Web Cryptography algorithms. The FFI implementation instead rejected the same calls, making package:webcrypto behavior platform-dependent.

For ECDH, the implementation already had an empty-result branch, but its preceding length <= 0 check made that branch unreachable. For PBKDF2, an explicit zero-length guard contradicted the algorithm's empty-result step. The replacement return follows the specification's validation order and avoids a zero-sized native allocation and isolate dispatch.

This change aligns the native backend with the browser backend and keeps negative-length validation intact.

Fixes #320.

Validation

  • dart format --output none --set-exit-if-changed ...
  • dart analyze --fatal-warnings .
  • dart test test/webcrypto_test.dart -p vm (1,441 tests)
  • dart test test/webcrypto_test.dart -p chrome -c dart2js
  • dart test test/webcrypto_test.dart -p chrome -c dart2wasm

@harrshita123 harrshita123 force-pushed the agent/derive-bits-zero-length branch from 96b0a08 to 2043b6f Compare July 13, 2026 14:46
@harrshita123 harrshita123 changed the title Fix zero-length ECDH and PBKDF2 derivation fix(derive-bits): support zero-length results Jul 13, 2026
@harrshita123 harrshita123 marked this pull request as ready for review July 13, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: native ECDH and PBKDF2 reject zero-length deriveBits

1 participant