Documentation
¶
Index ¶
- Variables
- func CalculateHash(pub *PublicKey, h hashFunc, msg, uid []byte) ([]byte, error)
- func CalculateZA(pub *PublicKey, uid []byte) ([]byte, error)
- func CalculateZALegacy(pub *PublicKey, h hashFunc, uid []byte) ([]byte, error)
- func Compress(pub *PublicKey) []byte
- func Decrypt(priv *PrivateKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func DecryptASN1(priv *PrivateKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func Encrypt(random io.Reader, pub *PublicKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func EncryptASN1(random io.Reader, pub *PublicKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func EncryptASN1UsingK(k *big.Int, pub *PublicKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func EncryptUsingK(k *big.Int, pub *PublicKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func KeyExchangeA(klen int, ida, idb []byte, priA *PrivateKey, pubB *PublicKey, rpri *PrivateKey, ...) (k, s1, s2 []byte, err error)
- func KeyExchangeB(klen int, ida, idb []byte, priB *PrivateKey, pubA *PublicKey, rpri *PrivateKey, ...) (k, s1, s2 []byte, err error)
- func MarshalPrivateKey(key *PrivateKey) ([]byte, error)
- func MarshalPublicKey(key *PublicKey) ([]byte, error)
- func MarshalSM2PrivateKey(key *PrivateKey) ([]byte, error)
- func MarshalSignatureASN1(r, s *big.Int) ([]byte, error)
- func MarshalSignatureBytes(curve elliptic.Curve, r, s *big.Int) ([]byte, error)
- func P256() elliptic.Curve
- func PrivateKeyTo(key *PrivateKey) []byte
- func PublicKeyTo(key *PublicKey) []byte
- func Sign(random io.Reader, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) ([]byte, error)
- func SignBytes(random io.Reader, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) ([]byte, error)
- func SignBytesUsingK(k *big.Int, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) ([]byte, error)
- func SignLegacy(random io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
- func SignLegacyUsingK(k *big.Int, priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
- func SignToRS(random io.Reader, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) (r, s *big.Int, err error)
- func SignUsingK(k *big.Int, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) ([]byte, error)
- func SignUsingKToRS(k *big.Int, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) (r, s *big.Int, err error)
- func UnmarshalSignatureASN1(sign []byte) (r, s *big.Int, err error)
- func UnmarshalSignatureBytes(curve elliptic.Curve, sign []byte) (r, s *big.Int, err error)
- func Verify(pub *PublicKey, msg, sign []byte, opts crypto.SignerOpts) bool
- func VerifyBytes(pub *PublicKey, msg, sign []byte, opts crypto.SignerOpts) bool
- func VerifyLegacy(pub *PublicKey, hash []byte, r, s *big.Int) bool
- func VerifyWithRS(pub *PublicKey, msg []byte, r, s *big.Int, opts crypto.SignerOpts) bool
- type Encoding
- type EncrypterOpts
- type KeyExchange
- func (ke *KeyExchange) ConfirmInitiator(s1 []byte) ([]byte, error)
- func (ke *KeyExchange) ConfirmResponder(rB *PublicKey, sB []byte) ([]byte, []byte, error)
- func (ke *KeyExchange) Init(random io.Reader) (*PublicKey, error)
- func (ke *KeyExchange) Repond(random io.Reader, rA *PublicKey) (*PublicKey, []byte, error)
- func (ke *KeyExchange) Reset()
- func (ke *KeyExchange) SetPeerParameters(peerPub *PublicKey, peerUID []byte) error
- type Mode
- type PrivateKey
- func (priv *PrivateKey) Decrypt(_ io.Reader, data []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error)
- func (priv *PrivateKey) DecryptASN1(data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool
- func (priv *PrivateKey) Public() crypto.PublicKey
- func (priv *PrivateKey) Sign(random io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error)
- func (priv *PrivateKey) SignBytes(random io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error)
- func (priv *PrivateKey) SignBytesUsingK(k *big.Int, msg []byte, opts crypto.SignerOpts) ([]byte, error)
- func (priv *PrivateKey) SignUsingK(k *big.Int, msg []byte, opts crypto.SignerOpts) ([]byte, error)
- type PublicKey
- func (pub *PublicKey) Encrypt(random io.Reader, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func (pub *PublicKey) EncryptASN1(random io.Reader, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func (pub *PublicKey) EncryptASN1UsingK(k *big.Int, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func (pub *PublicKey) EncryptUsingK(k *big.Int, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func (pub *PublicKey) Equal(x crypto.PublicKey) bool
- func (pub *PublicKey) Size() int
- func (pub *PublicKey) Verify(msg, sign []byte, opts crypto.SignerOpts) bool
- func (pub *PublicKey) VerifyBytes(msg, sign []byte, opts crypto.SignerOpts) bool
- type SignerOpts
Constants ¶
This section is empty.
Variables ¶
var ( // default Signer Opts DefaultSignerOpts = SignerOpts{ Uid: defaultUID, Hash: sm3.New, } // default Encrypter Opts DefaultEncrypterOpts = EncrypterOpts{ Mode: C1C3C2, Hash: sm3.New, } )
Functions ¶
func CalculateHash ¶
Calculate Hash
func CalculateZA ¶
CalculateZA ZA = H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
func CalculateZALegacy ¶
CalculateZALegacy ZA = H256(ENTLA || IDA || a || b || xG || yG || xA || yA)
func Decrypt ¶
func Decrypt(priv *PrivateKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
sm2 解密,解析字节拼接格式的密文内容 Decrypt bytes marshal data
func DecryptASN1 ¶
func DecryptASN1(priv *PrivateKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
sm2 解密,解析 asn.1 编码格式的密文内容 Decrypt asn.1 marshal data
func Encrypt ¶
func Encrypt(random io.Reader, pub *PublicKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
sm2 加密,返回字节拼接格式的密文内容 Encrypted and return bytes data
func EncryptASN1 ¶
func EncryptASN1(random io.Reader, pub *PublicKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
sm2 加密,返回 asn.1 编码格式的密文内容 Encrypted and return asn.1 data
func EncryptASN1UsingK ¶
func EncryptASN1UsingK(k *big.Int, pub *PublicKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
Encrypted use k and return asn.1 data
func EncryptUsingK ¶
func EncryptUsingK(k *big.Int, pub *PublicKey, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
Encrypted use k and return bytes data
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 MarshalPrivateKey ¶
func MarshalPrivateKey(key *PrivateKey) ([]byte, error)
func MarshalPublicKey ¶
func MarshalSM2PrivateKey ¶
func MarshalSM2PrivateKey(key *PrivateKey) ([]byte, error)
MarshalSM2PrivateKey converts an SM2 private key to SEC 1, ASN.1 DER form.
This kind of key is commonly encoded in PEM blocks of type "SM2 PRIVATE KEY". For a more flexible key format which is not SM2 specific, use MarshalPKCS8PrivateKey.
func MarshalSignatureBytes ¶
拼接编码
func Sign ¶
func Sign(random io.Reader, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) ([]byte, error)
签名返回 asn.1 编码数据 sign data and return asn.1 marshal data
func SignBytes ¶
func SignBytes(random io.Reader, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) ([]byte, error)
签名返回 Bytes 编码数据 sign data and return Bytes marshal data
func SignBytesUsingK ¶
func SignBytesUsingK(k *big.Int, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) ([]byte, error)
sign data use k and return Bytes marshal data
func SignLegacy ¶
sm2 sign legacy
func SignLegacyUsingK ¶
sm2 sign legacy use k
func SignToRS ¶
func SignToRS(random io.Reader, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) (r, s *big.Int, err error)
sm2 sign with SignerOpts
func SignUsingK ¶
func SignUsingK(k *big.Int, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) ([]byte, error)
sign data use k and return asn.1 marshal data
func SignUsingKToRS ¶
func SignUsingKToRS(k *big.Int, priv *PrivateKey, msg []byte, opts crypto.SignerOpts) (r, s *big.Int, err error)
sm2 sign use k with SignerOpts
func UnmarshalSignatureBytes ¶
func Verify ¶
func Verify(pub *PublicKey, msg, sign []byte, opts crypto.SignerOpts) bool
验证 asn.1 编码的数据 ans1(r, s) Verify asn.1 marshal data
func VerifyBytes ¶
func VerifyBytes(pub *PublicKey, msg, sign []byte, opts crypto.SignerOpts) bool
验证明文拼接的数据 bytes(r + s) Verify Bytes marshal data
func VerifyLegacy ¶
sm2 verify legacy
func VerifyWithRS ¶
sm2 verify with SignerOpts
Types ¶
type EncrypterOpts ¶
加密设置 Encrypter Opts
func (EncrypterOpts) GetEncoding ¶
func (this EncrypterOpts) GetEncoding() Encoding
func (EncrypterOpts) GetHash ¶
func (this EncrypterOpts) GetHash() hashFunc
func (EncrypterOpts) GetMode ¶
func (this EncrypterOpts) GetMode() Mode
type KeyExchange ¶
type KeyExchange struct {
// contains filtered or unexported fields
}
KeyExchange key exchange struct, include internal stat in whole key exchange flow. Initiator's flow will be: NewKeyExchange -> Init -> transmission -> ConfirmResponder Responder's flow will be: NewKeyExchange -> waiting ... -> Repond -> transmission -> ConfirmInitiator
func NewKeyExchange ¶
func NewKeyExchange(priv *PrivateKey, peerPub *PublicKey, uid, peerUID []byte, keyLen int, genSignature bool) (ke *KeyExchange, err error)
NewKeyExchange create one new KeyExchange object
func (*KeyExchange) ConfirmInitiator ¶
func (ke *KeyExchange) ConfirmInitiator(s1 []byte) ([]byte, error)
ConfirmInitiator for responder's step B10
func (*KeyExchange) ConfirmResponder ¶
ConfirmResponder for initiator's step A4-A10, returns keying data and optional signature.
It will check if there are peer's public key and validate the peer's Ephemeral Public Key.
If the peer's signature is not empty, then it will also validate the peer's signature and return generated signature depends on KeyExchange.genSignature value.
func (*KeyExchange) Init ¶
func (ke *KeyExchange) Init(random io.Reader) (*PublicKey, error)
Init is for initiator's step A1-A3, returns generated Ephemeral Public Key which will be passed to Reponder.
func (*KeyExchange) Repond ¶
Repond is for responder's step B1-B8, returns generated Ephemeral Public Key and optional signature depends on KeyExchange.genSignature value.
It will check if there are peer's public key and validate the peer's Ephemeral Public Key.
func (*KeyExchange) Reset ¶
func (ke *KeyExchange) Reset()
Reset clear all internal state and Ephemeral private/public keys.
func (*KeyExchange) SetPeerParameters ¶
func (ke *KeyExchange) SetPeerParameters(peerPub *PublicKey, peerUID []byte) error
SetPeerParameters when need other param
type PrivateKey ¶
SM2 PrivateKey
func GenerateKey ¶
func GenerateKey(random io.Reader) (*PrivateKey, error)
生成私钥证书 generate PrivateKey
func NewPrivateKey ¶
func NewPrivateKey(d []byte) (*PrivateKey, error)
根据私钥明文初始化私钥 New a PrivateKey from privatekey data
func ParsePrivateKey ¶
func ParsePrivateKey(der []byte) (*PrivateKey, error)
func ParseSM2PrivateKey ¶
func ParseSM2PrivateKey(der []byte) (*PrivateKey, error)
ParseSM2PrivateKey parses an SM2 private key in SEC 1, ASN.1 DER form.
This kind of key is commonly encoded in PEM blocks of type "SM2 PRIVATE KEY".
func (*PrivateKey) Decrypt ¶
func (priv *PrivateKey) Decrypt(_ io.Reader, data []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error)
crypto.Decrypter
func (*PrivateKey) DecryptASN1 ¶
func (priv *PrivateKey) DecryptASN1(data []byte, opts crypto.DecrypterOpts) ([]byte, error)
Decrypt with ASN1
func (*PrivateKey) Equal ¶
func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool
Equal reports whether priv and x have the same value.
func (*PrivateKey) Public ¶
func (priv *PrivateKey) Public() crypto.PublicKey
The SM2's private key contains the public key
func (*PrivateKey) Sign ¶
func (priv *PrivateKey) Sign(random io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error)
sign data and return asn.1 or bytes marshal data, default asn.1
func (*PrivateKey) SignBytes ¶
func (priv *PrivateKey) SignBytes(random io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error)
签名返回明文拼接数据 bytes(r + s) sign data and return Bytes marshal data bytes(r + s)
func (*PrivateKey) SignBytesUsingK ¶
func (priv *PrivateKey) SignBytesUsingK(k *big.Int, msg []byte, opts crypto.SignerOpts) ([]byte, error)
签名返回明文拼接数据 bytes(r + s) sign data and return Bytes marshal data bytes(r + s)
func (*PrivateKey) SignUsingK ¶
func (priv *PrivateKey) SignUsingK(k *big.Int, msg []byte, opts crypto.SignerOpts) ([]byte, error)
sign data use k and return asn.1 or bytes marshal data, default asn.1
type PublicKey ¶
SM2 PublicKey
func NewPublicKey ¶
根据公钥明文初始化公钥 New a PublicKey from publicKey data
func ParsePublicKey ¶
func (*PublicKey) Encrypt ¶
func (pub *PublicKey) Encrypt(random io.Reader, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
Encrypt data
func (*PublicKey) EncryptASN1 ¶
func (pub *PublicKey) EncryptASN1(random io.Reader, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
Encrypt with ASN1
func (*PublicKey) EncryptASN1UsingK ¶
func (pub *PublicKey) EncryptASN1UsingK(k *big.Int, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
Encrypt with ASN1
func (*PublicKey) EncryptUsingK ¶
func (pub *PublicKey) EncryptUsingK(k *big.Int, data []byte, opts crypto.DecrypterOpts) ([]byte, error)
Encrypt data use k
func (*PublicKey) Size ¶
Size returns the maximum length of the shared key the public key can produce.
func (*PublicKey) Verify ¶
func (pub *PublicKey) Verify(msg, sign []byte, opts crypto.SignerOpts) bool
验证 asn.1 编码的数据 ans1(r, s) Verify asn.1 marshal data ans1(r, s)
func (*PublicKey) VerifyBytes ¶
func (pub *PublicKey) VerifyBytes(msg, sign []byte, opts crypto.SignerOpts) bool
验证明文拼接的数据 bytes(r + s) Verify Bytes marshal data bytes(r + s)
type SignerOpts ¶
签名设置 Signer Opts
func (SignerOpts) GetEncoding ¶
func (this SignerOpts) GetEncoding() Encoding
func (SignerOpts) GetHash ¶
func (this SignerOpts) GetHash() hashFunc
func (SignerOpts) GetUid ¶
func (this SignerOpts) GetUid() []byte
func (SignerOpts) HashFunc ¶
func (this SignerOpts) HashFunc() crypto.Hash