bun icon indicating copy to clipboard operation
bun copied to clipboard

Implement X25519 deriveBits for WebCrypto API

Open Jarred-Sumner opened this issue 5 months ago โ€ข 3 comments

What does this PR do?

Implement X25519 deriveBits for WebCrypto API

Jarred-Sumner avatar Jun 10 '25 05:06 Jarred-Sumner

Updated 12:19 PM PT - Jun 18th, 2025

:x: @Jarred-Sumner, your commit 1e9aff5f06270e6f3985e9c25d05cd5b33b30e98 has 2 failures in Build #18833:


๐Ÿงช ย  To try this PR locally:

bunx bun-pr 20299

That installs a local version of the PR into your bun-20299 executable, so you can run:

bun-20299 --bun

robobun avatar Jun 10 '25 05:06 robobun

@claude push up a fix for the underlying cause of this test failure

โœ“ X25519 key operations work [0.67ms]
... omitted 2 tests ...
โœ“ X25519 deriveBits with imported keys [1.01ms]
 
103 |     keyPair1.privateKey,
104 |     null as any,
105 |   );
106 |
107 |   expect(sharedSecret).toBeInstanceOf(ArrayBuffer);
108 |   expect(sharedSecret.byteLength).toBe(32);
                                        ^
error: expect(received).toBe(expected)
 
Expected: 32
Received: 0
 
      at <anonymous> (/var/lib/buildkite-agent/builds/ip-172-31-15-126/bun/bun/test/x25519-derive-bits.test.ts:108:35)
โœ— X25519 deriveBits with null length [1.61ms]

Jarred-Sumner avatar Jun 10 '25 19:06 Jarred-Sumner

Claude finished @Jarred-Sumner's task in 1m 52s โ€”โ€” View job


Todo List:

  • [x] Read the failing test to understand expected behavior
  • [x] Examine X25519 implementation files
  • [x] Identify the bug causing 0 bytes instead of 32 bytes with null length
  • [x] Fix the deriveBits implementation
  • [x] Push the fix to the PR branch

Analysis:

The test failure was caused by an early return in CryptoAlgorithmX25519::deriveBits() when length is null/undefined. When the length parameter is null, it gets converted to 0 and triggered a premature return with an empty result.

Fix Applied:

Removed the early return for !length (lines 85-91) in src/bun.js/bindings/webcrypto/CryptoAlgorithmX25519.cpp. This allows the existing unifiedCallback logic to properly handle the null length case by returning the full 32-byte X25519 shared secret, which matches the Web Crypto API specification.

The unifiedCallback already had the correct logic:

  • When length is 0 (null converted), return the full derived key
  • When length is specified, truncate to the requested length

Commit: 324c610

claude[bot] avatar Jun 10 '25 19:06 claude[bot]