ExCEP. Crypto. SM2
(ex_cep v0.1.1)
Copy Markdown
Pure Elixir implementation of SM2 digital signatures (GB/T 32918.2-2016) over sm2p256v1.
Implements ExCEP.Crypto.Signer and produces/verifies standard 64-byte <<r::256, s::256>>
signatures required by CSG CEP TS2.2 Section 4.8 and Section 5 (Authentication).
Key Format
- Private key: 32-byte big-endian binary (
<<d::unsigned-big-256>>) - Public key: 64-byte uncompressed raw coordinates (
<<x::256, y::256>>) or 65-byte SEC1 uncompressed (<<0x04, x::256, y::256>>)
Signer Options
:private_key- 32-byte private key binary (required forsign/2):public_key- 64- or 65-byte public key binary (optional forsign/2, derived from:private_keyif omitted; required forverify/3unless:peer_public_keyis set):peer_public_key- 64- or 65-byte peer public key binary (used byverify/3if present):user_id- SM2 user ID binary (default"1234567812345678"per GB/T 32918.2)
Summary
Functions
Generates a new SM2 keypair {public_key_64b, private_key_32b}.
Normalizes and validates an SM2 public key into a 64-byte raw binary <<X::256, Y::256>>.
Derives the 64-byte raw public key from a 32-byte private key (1 <= d <= n - 2).
Functions
@spec generate_keypair() :: {<<_::512>>, <<_::256>>}
Generates a new SM2 keypair {public_key_64b, private_key_32b}.
Normalizes and validates an SM2 public key into a 64-byte raw binary <<X::256, Y::256>>.
Accepts 64-byte raw, 65-byte uncompressed (0x04 || X || Y), 33-byte compressed
(0x02/0x03 || X), or %ExCEP.Crypto.Cert{}.
@spec public_key_from_private(<<_::256>>) :: <<_::512>>
Derives the 64-byte raw public key from a 32-byte private key (1 <= d <= n - 2).