Skip to content

crypto: support provider-only SM4 cipher modes - #65399

Open
PickBas wants to merge 1 commit into
nodejs:mainfrom
JaneaSystems:issue-64866-updated
Open

crypto: support provider-only SM4 cipher modes#65399
PickBas wants to merge 1 commit into
nodejs:mainfrom
JaneaSystems:issue-64866-updated

Conversation

@PickBas

@PickBas PickBas commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

crypto: support provider-only SM4 cipher modes

sm4-gcm, sm4-ccm and sm4-xts exist only as fetchable provider algorithms in OpenSSL 3. There is no legacy EVP_CIPHER for them, so EVP_get_cipherbyname() / EVP_get_cipherbynid() cannot resolve them and EVP_CIPHER_do_all_sorted(), which walks the legacy name table, never reports them. createCipheriv() threw Unknown cipher, getCipherInfo() returned undefined, and getCiphers() omitted them.

Changes

  • Cipher::FromName() / Cipher::FromNid() fall back to EVP_CIPHER_fetch().
  • Cipher::getNid() recovers the nid from the algorithm name, since a fetched cipher inherits its nid from the legacy implementation it does not have. Without this, getCipherInfo(name) reports no nid and cannot round-trip through getCipherInfo(nid).
  • Cipher::ForEach() probes for the provider-only modes so getCiphers() lists them.
  • Fetched instances are reference counted while Cipher is a non-owning wrapper, so they are cached for the process lifetime. The cache key includes the library context's default property state, because crypto.setFips() changes it at runtime and a cipher fetched beforehand must not stay usable afterwards.

Tests

  • test-crypto-sm4-aead.js - RFC 8998 A.1/A.2 known-answer vectors for GCM and CCM, XTS round-trip, tag tampering, getCipherInfo name/nid round-trip, case-insensitive lookup, and negative cases for unknown names and nids.
  • test-crypto-sm4-fips.js - enabling FIPS at runtime must invalidate an already fetched SM4 cipher. Skipped unless a FIPS provider is available.

Fixes: #64866

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/security-wg

@nodejs-github-bot nodejs-github-bot added dependencies Pull requests that update a dependency file. needs-ci PRs that need a full CI run. labels Aug 19, 2026
Fixes: nodejs#64866
Co-authored-by: StefanStojanovic <stefan.stojanovic@janeasystems.com>
Signed-off-by: Kirill Saied <sayed.kirill@gmail.com>
@PickBas
PickBas force-pushed the issue-64866-updated branch from 6cb1a8a to 491dc6d Compare August 19, 2026 11:27
@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.11%. Comparing base (de333e8) to head (491dc6d).
⚠️ Report is 202 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65399      +/-   ##
==========================================
- Coverage   90.30%   90.11%   -0.20%     
==========================================
  Files         759      752       -7     
  Lines      247648   251864    +4216     
  Branches    46696    47355     +659     
==========================================
+ Hits       223644   226970    +3326     
- Misses      15466    16230     +764     
- Partials     8538     8664     +126     

see 203 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jasnell
jasnell requested review from panva and tniessen August 20, 2026 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crypto: SM4-GCM and SM4-CCM not available despite OpenSSL 3.5 bundling the implementation

2 participants