sm2soft

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	C1C3C2 = 0
	C1C2C3 = 1
)

Functions

func BytesCombine

func BytesCombine(pBytes ...[]byte) []byte

func CipherMarshal

func CipherMarshal(data []byte) ([]byte, error)

CipherMarshal sm2密文转asn.1编码格式

sm2密文结构如下:
- x
- y
- hash
- CipherText

func CipherUnmarshal

func CipherUnmarshal(data []byte) ([]byte, error)

CipherUnmarshal sm2密文asn.1编码格式转C1|C3|C2拼接格式

func Compress

func Compress(a *PublicKey) []byte

func Decrypt

func Decrypt(priv *PrivateKey, data []byte, mode int) ([]byte, error)

DecryptAsn1 sm2解密,解析asn.1编码格式的密文内容

func DecryptAsn1

func DecryptAsn1(pub *PrivateKey, data []byte) ([]byte, error)

DecryptAsn1 sm2解密,解析asn.1编码格式的密文内容

func Encrypt

func Encrypt(pub *PublicKey, data []byte, random io.Reader, mode int) ([]byte, error)

Encrypt sm2非对称加密,支持C1C3C2(mode = 0)与C1C2C3(mode = 1)两种模式,默认使用C1C3C2模式。 不同的模式表示不同的密文结构,其中C1C2C3的意义: C1 : sm2椭圆曲线上的某个点,每次加密得到的点不一样 C2 : 密文 C3 : 明文加盐后的摘要

func EncryptAsn1

func EncryptAsn1(pub *PublicKey, data []byte, rand io.Reader) ([]byte, error)

EncryptAsn1 sm2加密,返回asn.1编码格式的密文内容

func KeyExchangeA

func KeyExchangeA(klen int, ida, idb []byte, priA *PrivateKey, pubB *PublicKey, rpri *PrivateKey, rpubB *PublicKey) (k, s1, s2 []byte, err error)

KeyExchangeA 协商第二部,用户A调用,返回共享密钥k

func KeyExchangeB

func KeyExchangeB(klen int, ida, idb []byte, priB *PrivateKey, pubA *PublicKey, rpri *PrivateKey, rpubA *PublicKey) (k, s1, s2 []byte, err error)

KeyExchangeB 协商第二部,用户B调用, 返回共享密钥k

func P256Sm2

func P256Sm2() elliptic.Curve

P256Sm2 获取SM2P256椭圆曲线 该曲线是一个单例

func SignASN1

func SignASN1(rand io.Reader, priv *PrivateKey, hash []byte) ([]byte, error)

SignASN1 使用私钥priv对一个hash值进行签名。 返回的签名为DER字节数组,对(r,s)做了asn1编码。

func SignDataToSignDigit

func SignDataToSignDigit(sign []byte) (*big.Int, *big.Int, error)

func SignDigitToSignData

func SignDigitToSignData(r, s *big.Int) ([]byte, error)

func Sm2Sign

func Sm2Sign(priv *PrivateKey, signContentDigest, uid []byte, random io.Reader) (r, s *big.Int, err error)

Sm2Sign SM2签名

  • priv : 签名私钥 *sm2.PrivateKey
  • signContentDigest : 签名内容摘要(散列值)
  • uid : 内部混合摘要计算用uid, 长度16的字节数组,可以传 nil
  • random : 随机数获取用

func Sm2Verify

func Sm2Verify(pub *PublicKey, signContentDigest, uid []byte, r, s *big.Int) bool

Sm2Verify SM2验签

  • pub : 验签公钥, *sm2.PublicKey
  • signContentDigest : 签名内容摘要(散列值)
  • uid : 内部混合摘要计算用uid, 长度16的字节数组,可以传 nil
  • r, s : 签名

func Verify

func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool

Verify SM2验签

  • pub : 验签公钥, *sm2.PublicKey
  • hash : 签名内容摘要(散列值)
  • r, s : 签名

func VerifyASN1

func VerifyASN1(pub *PublicKey, hash, sig []byte) bool

VerifyASN1 使用公钥pub对hash和sig进行验签。

  • pub 公钥
  • hash 签名内容摘要(散列值)
  • sig 签名DER字节数组(对(r,s)做了asn1编码,因此会先做asn1解码)

func WNafReversed

func WNafReversed(wnaf []int8) []int8

func ZA

func ZA(pub *PublicKey, uid []byte) ([]byte, error)

ZA = H256(ENTLA || IDA || a || b || xG || yG || xA || yA)

Types

type PrivateKey

type PrivateKey struct {
	PublicKey          // 公钥
	D         *big.Int // 私钥,[1,n-1]区间的随机数
}

PrivateKey SM2私钥结构体

func GenerateKey

func GenerateKey(random io.Reader) (*PrivateKey, error)

GenerateKey 基于P256Sm2曲线生成sm2的公私钥

func (*PrivateKey) Decrypt

func (priv *PrivateKey) Decrypt(_ io.Reader, msg []byte, _ crypto.DecrypterOpts) (plaintext []byte, err error)

Decrypt crypto.Decrypter

func (*PrivateKey) DecryptAsn1

func (priv *PrivateKey) DecryptAsn1(data []byte) ([]byte, error)

DecryptAsn1 sm2解密,C1C3C2,asn1解码

func (*PrivateKey) Equal

func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool

func (*PrivateKey) Public

func (priv *PrivateKey) Public() crypto.PublicKey

Public The SM2's private key contains the public key

func (*PrivateKey) Sign

func (priv *PrivateKey) Sign(random io.Reader, signContentDigest []byte, signer crypto.SignerOpts) ([]byte, error)

Sign 使用priv私钥对签名内容摘要做SM2签名,参数signer目前没有使用,但仍要求传入外部对明文消息做摘要的散列算法。 使用priv私钥对签名内容摘要做SM2签名,参数signer目前没有使用,但仍要求传入外部对明文消息做摘要的散列算法。 返回的签名为DER字节数组,对(r,s)做了asn1编码。

  • random : 随机数获取用, 如 rand.Reader
  • signContentDigest : 签名内容摘要(散列值)
  • signer : 外部对签名内容进行摘要计算使用的散列函数

type PublicKey

type PublicKey struct {
	elliptic.Curve          // 椭圆曲线
	X, Y           *big.Int // 公钥座标
}

PublicKey SM2公钥结构体

func Decompress

func Decompress(a []byte) *PublicKey

func (*PublicKey) EncryptAsn1

func (pub *PublicKey) EncryptAsn1(data []byte, random io.Reader) ([]byte, error)

EncryptAsn1 sm2加密,C1C3C2,asn1编码

func (*PublicKey) Equal

func (pub *PublicKey) Equal(x crypto.PublicKey) bool

func (*PublicKey) Sm3Digest

func (pub *PublicKey) Sm3Digest(msg, uid []byte) ([]byte, error)

Sm3Digest 对签名内容进行SM3摘要计算,摘要计算前混入sm2椭圆曲线部分参数与公钥并预散列一次。

func (*PublicKey) Verify

func (pub *PublicKey) Verify(signContentDigest []byte, sig []byte) bool

Verify 使用pub公钥对签名sig做验签。

  • signContentDigest : 签名内容摘要(散列值)
  • sig : 签名DER字节数组(对(r,s)做了asn1编码,因此会先做asn1解码)

Jump to

Keyboard shortcuts

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