sm2

package
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(priv *ecdsa.PrivateKey, ciphertext []byte, order CipherOrder, window int) ([]byte, error)

Decrypt decrypts ciphertext using SM2 private key decryption. The ciphertext format follows GM/T 0003-2012 standard.

func Encrypt

func Encrypt(random io.Reader, pub *ecdsa.PublicKey, plaintext []byte, order CipherOrder, window int) ([]byte, error)

Encrypt encrypts plaintext using SM2 public key encryption. The ciphertext format follows GM/T 0003-2012 standard.

func MarshalPKCS8PrivateKey

func MarshalPKCS8PrivateKey(pri *ecdsa.PrivateKey) ([]byte, error)

MarshalPKCS8PrivateKey encodes a PKCS#8 PrivateKeyInfo for the given SM2 private key.

func MarshalSPKIPublicKey

func MarshalSPKIPublicKey(pub *ecdsa.PublicKey) ([]byte, error)

MarshalSPKIPublicKey encodes a SubjectPublicKeyInfo (SPKI) for the given SM2 public key.

func NewCurve

func NewCurve() elliptic.Curve

NewCurve returns a new SM2-P-256 curve instance.

func ParseBitStringPrivateKey

func ParseBitStringPrivateKey(key []byte) (*ecdsa.PrivateKey, error)

ParseBitStringPrivateKey parses a BIT_STRING PrivateKeyInfo and returns an SM2 private key.

func ParseBitStringPublicKey

func ParseBitStringPublicKey(key []byte) (*ecdsa.PublicKey, error)

ParseBitStringPublicKey parses a BIT_STRING PublicKeyInfo and returns an SM2 public key.

func ParsePKCS8PrivateKey

func ParsePKCS8PrivateKey(der []byte) (*ecdsa.PrivateKey, error)

ParsePKCS8PrivateKey parses a PKCS#8 PrivateKeyInfo and returns an SM2 private key. Simplified: ignores optional parameters/publicKey fields inside ECPrivateKey.

func ParseSPKIPublicKey

func ParseSPKIPublicKey(der []byte) (*ecdsa.PublicKey, error)

ParseSPKIPublicKey parses a SubjectPublicKeyInfo (SPKI) and returns an SM2 public key.

func RandScalar

func RandScalar(curve elliptic.Curve, random io.Reader) (*big.Int, error)

RandScalar generates random scalar in [1, N-1] using rejection sampling.

func SetWindow

func SetWindow(cv elliptic.Curve, w int)

SetWindow sets wNAF window size (2-6).

func Sign

func Sign(random io.Reader, priv *ecdsa.PrivateKey, message []byte, uid []byte) ([]byte, error)

Sign generates an SM2 signature for the given message It internally calculates ZA and digest (e = SM3(ZA || M)) Returns the signature in ASN.1 DER format

func Verify

func Verify(pub *ecdsa.PublicKey, message []byte, uid []byte, sig []byte) bool

Verify verifies an SM2 signature It internally calculates ZA and digest (e = SM3(ZA || M)) sig is the signature in ASN.1 DER format

Types

type CipherOrder

type CipherOrder string

CipherOrder represents the order of ciphertext components

const (
	// C1C2C3 represents ciphertext order: C1 || C2 || C3
	C1C2C3 CipherOrder = "c1c2c3"
	// C1C3C2 represents ciphertext order: C1 || C3 || C2
	C1C3C2 CipherOrder = "c1c3c2"
)

Source Files

  • asn1.go
  • curve.go
  • field.go
  • pool.go
  • sm2.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL