Skip to content

Introduction ​

sm-crypto-wasm compiles the hot paths of Chinese national cryptographic algorithms into WebAssembly, so front-end and Node back-end engineers can call SM2 / SM3 / SM4 / SM9 / ZUC through one API — without dealing with ASN.1, big-integer arithmetic or WASM glue.

AssemblyScript compiles to .wasm, wrapped in a thin TypeScript shim. Compared with pure-JS implementations, elliptic curves and pairings are an order of magnitude faster. Compared with C++ / Rust implementations, distributing it on npm requires no compiler toolchain on the user's machine.

The five algorithms ​

AlgorithmStandardWhat it isDocs
SM3GM/T 0004-2012256-bit hash, SHA-256 classSM3
SM4GM/T 0002-2012128-bit block cipher, six modesSM4
SM2GM/T 0003-2012Elliptic-curve public key (sign / encrypt / KEM / hybrid encryption / key exchange)SM2
SM9GM/T 0044-2016Identity-based crypto (sign / encrypt / KEM / hybrid encryption / key exchange)SM9
ZUCGM/T 0001-2012Stream cipher, EEA3 confidentiality + EIA3 integrityZUC

Design decisions ​

  • Split per algorithm, lazy-loaded: one .wasm per algorithm; only what you call is fetched. A page that only hashes never pays for SM9's pairing tables.
  • Synchronous business API, async warm-up: ensureLoaded() is async (WASM instantiation has to be); everything after it is synchronous, so you don't wrap every call in await.
  • Randomness always comes from the host: crypto.getRandomValues / crypto.randomBytes. The library ships no RNG and never writes keys to disk or logs.
  • Input sniffing: hex / base64 / PEM / Uint8Array are detected; output defaults to hex.
  • Layered errors: anything rejected because of its input derives from SmCryptoError, split into a key side and a ciphertext side; arguments that are simply the wrong type keep the native TypeError / RangeError.

Out of scope ​

Deliberately not included:

  • ZUC-256 (GM/T 0001.1-2021) — a different algorithm, not a parameterisation of ZUC-128;
  • a WebCrypto (crypto.subtle) style adapter;
  • HSM / PKCS#11 handles;
  • X.509 / PKCS#7 / CMS protocol layers;
  • "strictly streaming WASM" runtimes such as Cloudflare Workers / Vercel Edge.

Next ​

Released under the Apache-2.0 License