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 for sign/2)
  • :public_key - 64- or 65-byte public key binary (optional for sign/2, derived from :private_key if omitted; required for verify/3 unless :peer_public_key is set)
  • :peer_public_key - 64- or 65-byte peer public key binary (used by verify/3 if 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

generate_keypair()

@spec generate_keypair() :: {<<_::512>>, <<_::256>>}

Generates a new SM2 keypair {public_key_64b, private_key_32b}.

normalize_public_key(bin)

@spec normalize_public_key(binary() | struct()) ::
  {:ok, <<_::512>>} | {:error, term()}

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{}.

public_key_from_private(arg)

@spec public_key_from_private(<<_::256>>) :: <<_::512>>

Derives the 64-byte raw public key from a 32-byte private key (1 <= d <= n - 2).