Repository navigation
Conversation
… 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.
6598702 to
0a287b1
Compare
|
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 On the dependency itself: the description calls 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 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. |
|
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. |
|
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! |
Add a fallback when node:crypto lacks this cipher
HAP sessions are encrypted with ChaCha20-Poly1305.
hapCrypto.tsasserts at module load that node:crypto'sgetCiphers()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/ciphersfor 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?