Skip to content

feat: fall back to a pure-JS chacha20-poly1305 - #1142

Closed
ocean wants to merge 1 commit into
homebridge:beta-2.3.1from
ocean:feat/chacha20-poly1305-fallback
Closed

ocean wants to merge 1 commit into
homebridge:beta-2.3.1from
ocean:feat/chacha20-poly1305-fallback

Conversation

@ocean

@ocean ocean commented Nov 24, 2026

Copy link
Copy Markdown

Add a fallback when node:crypto lacks this cipher

HAP sessions are encrypted with ChaCha20-Poly1305. hapCrypto.ts asserts at module load that node:crypto's getCiphers() includes it, which is true on every supported Node.js version but false on runtimes that implement the Node.js API over another crypto backend, for example Bun's node:crypto is backed by BoringSSL, which does not expose this cipher, so HAP-NodeJS could not start there at all.

This PR addition detects the cipher once at load, and if it’s missing, used the audited pure-JavaScript implementation from @noble/ciphers for the two seal/open helpers. Node.js behaviour is unchanged (the native cipher is still used whenever present). Tests load a copy of the module with getCiphers() mocked to omit the cipher and check the fallback against the existing vectors and the RFC 8439 AEAD test vector.

Verified by pairing a Bun-hosted accessory from hap-controller and exchanging characteristic reads and writes over the encrypted session.

♻️ Current situation

Describe the current situation. Explain current problems, if there are any. Be as descriptive as possible (e.g., including examples or code snippets).

💡 Proposed solution

Describe the proposed solution and changes. How does it affect the project? How does it affect the internal structure (e.g., refactorings)?

⚙️ Release Notes

Provide a summary of the changes or features from a user's point of view. If there are breaking changes, provide migration guides using code examples of the affected features.

➕ Additional Information

If applicable, provide additional context in this section.

Testing

Which tests were added? Which existing tests were adapted/changed? Which situations are covered, and what edge cases are missing?

Reviewer Nudging

Where should the reviewer start? what is a good entry point?

@ocean
ocean changed the base branch from latest to beta-2.3.1 November 24, 2026 12:01
… the cipher

HAP sessions are encrypted with ChaCha20-Poly1305. hapCrypto.ts asserted at
module load that node:crypto's getCiphers() includes it. Every supported
Node.js version has it, unless its OpenSSL was built without it or runs in
FIPS mode, and runtimes that implement the Node.js API on another crypto
backend do not expose it at all: Bun's node:crypto is backed by BoringSSL,
which lacks this cipher, so HAP-NodeJS could not start there.

Detect the cipher once at load; when it is absent, use the audited
pure-JavaScript implementation from @noble/ciphers for the two seal/open
helpers. Node.js behaviour is unchanged: the native cipher is still used
whenever present. Tests load a copy of the module with getCiphers() mocked
to omit the cipher and check the fallback against the existing vectors and
the RFC 8439 AEAD test vector.

Verified by pairing a Bun-hosted accessory from hap-controller and
exchanging characteristic reads and writes over the encrypted session.
@ocean
ocean force-pushed the feat/chacha20-poly1305-fallback branch from 6598702 to 0a287b1 Compare November 24, 2026 12:07
@github-actions github-actions Bot added feature beta This is in some form related to the current beta release labels Nov 24, 2026
@hjdhjd

hjdhjd commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the contribution @ocean, and for the care that went into it...the tests against the existing vectors and the RFC 8439 vector are appreciated.

That said, I think we're going to pass on this one. HAP-NodeJS supports mainline Node.js releases - the ones listed in engines - and nothing else. We don't support or test against alternative runtimes like Bun, and every supported Node.js release provides ChaCha20-Poly1305 natively. This change would put a second cipher implementation and a new production dependency in the security core of every install...all for a runtime we can't validate.

On the dependency itself: the description calls @noble/ciphers audited, and that is partially true. Per its own readme, the independent audit (Cure53, September 2024) was of version 1.0.0. This PR depends on ^2.4.0, and the only review listed since then is the maintainer's own self-audits. It's a well-regarded library, but that's not the standard we want for session encryption in a core library like HAP-NodeJS.

The load-time check is also deliberate. If the cipher is missing on Node.js - a FIPS-mode build, for example - HAP-NodeJS should fail loudly rather than quietly switch to a different implementation.

This is the same place we landed in #1024 when the question came up for Electron...changing or supplementing the crypto in HAP-NodeJS has a much higher bar than enabling another runtime. The right home for this fix is Bun's node:crypto compatibility layer. Once Bun exposes the cipher, HAP-NodeJS should get past this check with no changes on our side.

I'll leave this open for a couple of days so others can weigh in...if there's a case here we haven't considered, happy to hear it. Appreciate the thought and the effort.

@ocean

ocean commented Nov 24, 2026

Copy link
Copy Markdown
Author

Hi @hjdhjd and thanks for the considered reasoning laid out here.

I understand about not wanting to introduce any non-core encryption libraries or dependencies, especially as it would increase the testing load, that's fair enough.

I'm using Bun for my own convenience and this encryption issue was the only sticking point I had when trying to run a HAP-NodeJS installation on it. However as suggested the Bun or BoringSSL layers are probably better targets for this change, so I'll try there.

I had left this PR in draft because I was going to see if I could gather any more arguments or evidence to support the idea. I appreciate you leaving it open in case any other maintainers would like to comment, but I'm also happy to close it if you like.

@hjdhjd

hjdhjd commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

I think this one is asked and answered several times over the years...but we appreciate the contribution and desire just the same. Happy coding!

@hjdhjd hjdhjd closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beta This is in some form related to the current beta release feature latest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants