API
#crypto/sm2
Xu 0.8.8 API
import: #crypto/sm2
counts: 34 fns, 6 methods, 2 types, 3 consts, 0 ifaces
SM2 keys, sign, encrypt, key exchange. Pure Xu, not commercial-crypto certified.
const public_key_size
pub const public_key_size = 65
Purpose
SM2 (GM/T 0003). Xu addition — has no crypto/sm2. Recommended curve sm2p256v1 only. Signatures are raw r||s (32+32) or GM/T 0009 DER SEQUENCE{r,s}. Ciphertext is c1||C3||c2 (uncompressed c1, SM3 C3) or DER SM2Cipher. Default user id is ASCII 1234567812345678. Key exchange is GM/T 0003.3 (see exch.xu). PKCS#8 / SEC1 private keys and PKIX public keys (see pkcs8.xu). No compressed points, no C1C2C3 alias. Zero C, not a commercial-crypto certification. XU-CT-CLASS: variable-time XU-CT-USE: fixed-shape-only math/big curve arithmetic is variable-time; crypto/subtle covers only final fixed-shape integrity/equality checks. Value `65`.
Parameters
none
const private_key_size
pub const private_key_size = 32
Purpose
Public constant `private_key_size`. Value `32`.
Parameters
none
const signature_size
pub const signature_size = 64
Purpose
Public constant `signature_size`. Value `64`.
Parameters
none
public
pub fn public(d &string) string
Purpose
public; returns string. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
Returns
string — result string
generate_key
pub fn generate_key() string
Purpose
Generate key. Throws on failure.
Parameters
none
Returns
string — result string
sign_id
pub fn sign_id(d &string, id &string, msg &string) string
Purpose
Sign id. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
id &string- id (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
Returns
string — result string
sign
pub fn sign(d &string, msg &string) string
Purpose
Sign. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
Returns
string — result string
verify_id
pub fn verify_id(pubk &string, id &string, msg &string, sig &string) bool
Purpose
Verify id.
Parameters
pubk &string- pubk (borrowed; caller keeps it; type &string)
id &string- id (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
sig &string- sig (borrowed; caller keeps it; type &string)
Returns
bool — true or false
verify
pub fn verify(pubk &string, msg &string, sig &string) bool
Purpose
Verify.
Parameters
pubk &string- pubk (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
sig &string- sig (borrowed; caller keeps it; type &string)
Returns
bool — true or false
encrypt
pub fn encrypt(pubk &string, msg &string) string
Purpose
Encrypt. Throws on failure.
Parameters
pubk &string- pubk (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
Returns
string — result string
decrypt
pub fn decrypt(d &string, ct &string) string
Purpose
Decrypt. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
ct &string- ct (borrowed; caller keeps it; type &string)
Returns
string — result string
marshal_sig
pub fn marshal_sig(sig &string) string
Purpose
Marshal sig. Throws on failure.
Parameters
sig &string- sig (borrowed; caller keeps it; type &string)
Returns
string — result string
parse_sig
pub fn parse_sig(der &string) string
Purpose
Parse as sig. Throws on failure.
Parameters
der &string- der (borrowed; caller keeps it; type &string)
Returns
string — result string
sign_id_asn1
pub fn sign_id_asn1(d &string, id &string, msg &string) string
Purpose
Sign id asn1. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
id &string- id (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
Returns
string — result string
sign_asn1
pub fn sign_asn1(d &string, msg &string) string
Purpose
Sign asn1. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
Returns
string — result string
verify_id_asn1
pub fn verify_id_asn1(pubk &string, id &string, msg &string, der &string) bool
Purpose
Verify id asn1.
Parameters
pubk &string- pubk (borrowed; caller keeps it; type &string)
id &string- id (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
der &string- der (borrowed; caller keeps it; type &string)
Returns
bool — true or false
verify_asn1
pub fn verify_asn1(pubk &string, msg &string, der &string) bool
Purpose
Verify asn1.
Parameters
pubk &string- pubk (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
der &string- der (borrowed; caller keeps it; type &string)
Returns
bool — true or false
encrypt_asn1
pub fn encrypt_asn1(pubk &string, msg &string) string
Purpose
Encrypt asn1. Throws on failure.
Parameters
pubk &string- pubk (borrowed; caller keeps it; type &string)
msg &string- message (borrowed; caller keeps it; type &string)
Returns
string — result string
decrypt_asn1
pub fn decrypt_asn1(d &string, der &string) string
Purpose
Decrypt asn1. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
der &string- der (borrowed; caller keeps it; type &string)
Returns
string — result string
struct ExchCap
pub struct ExchCap {
pub point string
pub mac string
pub key string
}
Purpose
GM/T 0003.3 / GB/T 32918.3 SM2 key exchange. w=127. Initiator: exch_new → init_r → confirm_responder. Responder: exch_new → respond_r → confirm_initiator. Optional SM3 MACs use prefixes 0x02 / 0x03. No ASN.1.
Parameters
point string- field (string)
mac string- field (string)
key string- key
struct Exch
Purpose
Public struct `Exch`.
Parameters
none
exch_new
pub fn exch_new(d &string, peer_pub &string, uid &string, peer &string, klen int, gen_sig bool) Exch
Purpose
Exch new. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
peer_pub &string- peer_pub (borrowed; caller keeps it; type &string)
uid &string- uid (borrowed; caller keeps it; type &string)
peer &string- peer (borrowed; caller keeps it; type &string)
klen int- klen (int)
gen_sig bool- gen_sig (bool)
Returns
Exch — a value of this type
(&mut Exch) init_r
pub fn (e &mut Exch) init_r(r &string) string
Purpose
Method on `&mut Exch`: init r. Throws on failure.
Parameters
e &mut Exch- receiver &mut Exch (exclusive borrow)
r &string- rune (Unicode code point) (borrowed; caller keeps it; type &string)
Returns
string — result string
(&mut Exch) init
pub fn (e &mut Exch) init() string
Purpose
Call `init` on `&mut Exch`. Throws on failure.
Parameters
e &mut Exch- receiver &mut Exch (exclusive borrow)
Returns
string — result string
(&mut Exch) respond_r
pub fn (e &mut Exch) respond_r(r &string, ra &string) ExchCap
Purpose
Method on `&mut Exch`: respond r. Throws on failure.
Parameters
e &mut Exch- receiver &mut Exch (exclusive borrow)
r &string- rune (Unicode code point) (borrowed; caller keeps it; type &string)
ra &string- ra (borrowed; caller keeps it; type &string)
Returns
ExchCap — a value of this type
(&mut Exch) respond
pub fn (e &mut Exch) respond(ra &string) ExchCap
Purpose
Call `respond` on `&mut Exch`. Throws on failure.
Parameters
e &mut Exch- receiver &mut Exch (exclusive borrow)
ra &string- ra (borrowed; caller keeps it; type &string)
Returns
ExchCap — a value of this type
(&mut Exch) confirm_responder
pub fn (e &mut Exch) confirm_responder(rb &string, sb &string) ExchCap
Purpose
Method on `&mut Exch`: confirm responder. Throws on failure.
Parameters
e &mut Exch- receiver &mut Exch (exclusive borrow)
rb &string- rb (borrowed; caller keeps it; type &string)
sb &string- sb (borrowed; caller keeps it; type &string)
Returns
ExchCap — a value of this type
(&mut Exch) confirm_initiator
pub fn (e &mut Exch) confirm_initiator(sa &string) string
Purpose
Method on `&mut Exch`: confirm initiator. Throws on failure.
Parameters
e &mut Exch- receiver &mut Exch (exclusive borrow)
sa &string- sa (borrowed; caller keeps it; type &string)
Returns
string — result string
nist_generate_key
pub fn nist_generate_key(curve int) string
Purpose
These helpers are exported for crypto/ecdsa. Xu currently has no package-private visibility, so the nist_ prefix keeps the shared surface explicit without moving cryptographic algorithms into compiler/core. Throws on failure.
Parameters
curve int- curve (int)
Returns
string — result string
nist_public
pub fn nist_public(curve int, private_key &string) string
Purpose
Nist public. Throws on failure.
Parameters
curve int- curve (int)
private_key &string- private_key (borrowed; caller keeps it; type &string)
Returns
string — result string
nist_check_public
pub fn nist_check_public(curve int, public_key &string) int
Purpose
Nist check public. Throws on failure.
Parameters
curve int- curve (int)
public_key &string- public_key (borrowed; caller keeps it; type &string)
Returns
int — a value of this type
nist_ecdh
pub fn nist_ecdh(curve int, private_key &string, peer_public &string) string
Purpose
NIST ECDH primitive shared by crypto/ecdsa and protocol packages. The result is the fixed-width affine X coordinate defined by SEC 1. Public-key parsing rejects compressed/off-curve points; all supported curves have cofactor one. Throws on failure.
Parameters
curve int- curve (int)
private_key &string- private_key (borrowed; caller keeps it; type &string)
peer_public &string- peer_public (borrowed; caller keeps it; type &string)
Returns
string — result string
nist_sign_asn1
pub fn nist_sign_asn1(curve int, private_key &string, hash &string) string
Purpose
Nist sign asn1. Throws on failure.
Parameters
curve int- curve (int)
private_key &string- private_key (borrowed; caller keeps it; type &string)
hash &string- hash (borrowed; caller keeps it; type &string)
Returns
string — result string
verify_p256_asn1
pub fn verify_p256_asn1(public_key &string, hash &string, signature &string) bool
Purpose
verify_p256_asn1 verifies a DER ECDSA signature over a precomputed hash. It deliberately returns false for malformed inputs, matching ecdsa.VerifyASN1 contract.
Parameters
public_key &string- public_key (borrowed; caller keeps it; type &string)
hash &string- hash (borrowed; caller keeps it; type &string)
signature &string- signature (borrowed; caller keeps it; type &string)
Returns
bool — true or false
verify_p384_asn1
pub fn verify_p384_asn1(public_key &string, hash &string, signature &string) bool
Purpose
Verify p384 asn1.
Parameters
public_key &string- public_key (borrowed; caller keeps it; type &string)
hash &string- hash (borrowed; caller keeps it; type &string)
signature &string- signature (borrowed; caller keeps it; type &string)
Returns
bool — true or false
verify_p521_asn1
pub fn verify_p521_asn1(public_key &string, hash &string, signature &string) bool
Purpose
Verify p521 asn1.
Parameters
public_key &string- public_key (borrowed; caller keeps it; type &string)
hash &string- hash (borrowed; caller keeps it; type &string)
signature &string- signature (borrowed; caller keeps it; type &string)
Returns
bool — true or false
parse_private_key
pub fn parse_private_key(der &string) string
Purpose
Parse as private key. Throws on failure.
Parameters
der &string- der (borrowed; caller keeps it; type &string)
Returns
string — result string
marshal_sec1
pub fn marshal_sec1(d &string) string
Purpose
Marshal sec1.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
Returns
string — result string
marshal_pkcs8
pub fn marshal_pkcs8(d &string) string
Purpose
Marshal pkcs8. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
Returns
string — result string
parse_private_pem
pub fn parse_private_pem(text &string) string
Purpose
Parse as private pem. Throws on failure.
Parameters
text &string- text (borrowed; caller keeps it; type &string)
Returns
string — result string
marshal_pkcs8_pem
pub fn marshal_pkcs8_pem(d &string) string
Purpose
Marshal pkcs8 pem. Throws on failure.
Parameters
d &string- d (borrowed; caller keeps it; type &string)
Returns
string — result string
parse_public_key
pub fn parse_public_key(der &string) string
Purpose
Parse as public key. Throws on failure.
Parameters
der &string- der (borrowed; caller keeps it; type &string)
Returns
string — result string
marshal_pkix
pub fn marshal_pkix(pubk &string) string
Purpose
Marshal pkix. Throws on failure.
Parameters
pubk &string- pubk (borrowed; caller keeps it; type &string)
Returns
string — result string
parse_public_pem
pub fn parse_public_pem(text &string) string
Purpose
Parse as public pem. Throws on failure.
Parameters
text &string- text (borrowed; caller keeps it; type &string)
Returns
string — result string
marshal_pkix_pem
pub fn marshal_pkix_pem(pubk &string) string
Purpose
Marshal pkix pem. Throws on failure.
Parameters
pubk &string- pubk (borrowed; caller keeps it; type &string)
Returns
string — result string