Documentation ¶
Overview ¶
Package sm9 handle shangmi sm9 algorithm and its curves and pairing implementation
Index ¶
- Constants
- func Decrypt(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error)
- func DecryptASN1(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error)
- func Encrypt(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, ...) ([]byte, error)
- func EncryptASN1(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, ...) ([]byte, error)
- func Sign(rand io.Reader, priv *SignPrivateKey, hash []byte) (h *big.Int, s *bn256.G1, err error)
- func SignASN1(rand io.Reader, priv *SignPrivateKey, hash []byte) ([]byte, error)
- func UnmarshalSM9KeyPackage(der []byte) ([]byte, *bn256.G1, error)
- func UnwrapKey(priv *EncryptPrivateKey, uid []byte, cipher *bn256.G1, kLen int) ([]byte, error)
- func Verify(pub *SignMasterPublicKey, uid []byte, hid byte, hash []byte, h *big.Int, ...) bool
- func VerifyASN1(pub *SignMasterPublicKey, uid []byte, hid byte, hash, sig []byte) bool
- func WrapKey(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, kLen int) (key []byte, cipher *bn256.G1, err error)
- type EncryptMasterPrivateKey
- func (master *EncryptMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*EncryptPrivateKey, error)
- func (master *EncryptMasterPrivateKey) MarshalASN1() ([]byte, error)
- func (master *EncryptMasterPrivateKey) Public() *EncryptMasterPublicKey
- func (master *EncryptMasterPrivateKey) UnmarshalASN1(der []byte) error
- type EncryptMasterPublicKey
- func (pub *EncryptMasterPublicKey) Encrypt(rand io.Reader, uid []byte, hid byte, plaintext []byte) ([]byte, error)
- func (pub *EncryptMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G1
- func (pub *EncryptMasterPublicKey) MarshalASN1() ([]byte, error)
- func (pub *EncryptMasterPublicKey) Pair() *bn256.GT
- func (pub *EncryptMasterPublicKey) ScalarBaseMult(r *big.Int) *bn256.GT
- func (pub *EncryptMasterPublicKey) UnmarshalASN1(der []byte) error
- func (pub *EncryptMasterPublicKey) WrapKey(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, []byte, error)
- func (pub *EncryptMasterPublicKey) WrapKeyASN1(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, error)
- type EncryptPrivateKey
- func (priv *EncryptPrivateKey) Decrypt(uid, ciphertext []byte) ([]byte, error)
- func (priv *EncryptPrivateKey) MarshalASN1() ([]byte, error)
- func (priv *EncryptPrivateKey) MasterPublic() *EncryptMasterPublicKey
- func (priv *EncryptPrivateKey) SetMasterPublicKey(pub *EncryptMasterPublicKey)
- func (priv *EncryptPrivateKey) UnmarshalASN1(der []byte) error
- func (priv *EncryptPrivateKey) UnwrapKey(uid, cipherDer []byte, kLen int) ([]byte, error)
- type KeyExchange
- func (ke *KeyExchange) ConfirmInitiator(s1 []byte) error
- func (ke *KeyExchange) ConfirmResponder(rB *bn256.G1, sB []byte) ([]byte, error)
- func (ke *KeyExchange) GetSharedKey() []byte
- func (ke *KeyExchange) InitKeyExchange(rand io.Reader, hid byte) (*bn256.G1, error)
- func (ke *KeyExchange) RepondKeyExchange(rand io.Reader, hid byte, rA *bn256.G1) (*bn256.G1, []byte, error)
- type SignMasterPrivateKey
- type SignMasterPublicKey
- func (pub *SignMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G2
- func (pub *SignMasterPublicKey) MarshalASN1() ([]byte, error)
- func (pub *SignMasterPublicKey) Pair() *bn256.GT
- func (pub *SignMasterPublicKey) ScalarBaseMult(r *big.Int) *bn256.GT
- func (pub *SignMasterPublicKey) UnmarshalASN1(der []byte) error
- func (pub *SignMasterPublicKey) Verify(uid []byte, hid byte, hash, sig []byte) bool
- type SignPrivateKey
- func (priv *SignPrivateKey) MarshalASN1() ([]byte, error)
- func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey
- func (priv *SignPrivateKey) SetMasterPublicKey(pub *SignMasterPublicKey)
- func (priv *SignPrivateKey) Sign(rand io.Reader, hash []byte, opts crypto.SignerOpts) ([]byte, error)
- func (priv *SignPrivateKey) UnmarshalASN1(der []byte) error
Constants ¶
const ( // hashmode used in h1: 0x01 H1 hashMode = 1 + iota // hashmode used in h2: 0x02 H2 )
const ( ENC_TYPE_XOR encryptType = 0 ENC_TYPE_ECB encryptType = 1 ENC_TYPE_CBC encryptType = 2 ENC_TYPE_OFB encryptType = 4 ENC_TYPE_CFB encryptType = 8 )
Variables ¶
This section is empty.
Functions ¶
func Decrypt ¶ added in v0.13.0
func Decrypt(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error)
Decrypt decrypt chipher, ciphertext should be with format C1||C3||C2
func DecryptASN1 ¶ added in v0.13.0
func DecryptASN1(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error)
DecryptASN1 decrypt chipher, ciphertext should be with ASN.1 format according SM9 cryptographic algorithm application specification, SM9Cipher definition.
func Encrypt ¶ added in v0.13.0
func Encrypt(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, plaintext []byte) ([]byte, error)
Encrypt encrypt plaintext, output ciphertext with format C1||C3||C2
func EncryptASN1 ¶ added in v0.13.0
func EncryptASN1(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, plaintext []byte) ([]byte, error)
EncryptASN1 encrypt plaintext and output ciphertext with ASN.1 format according SM9 cryptographic algorithm application specification, SM9Cipher definition.
func Sign ¶ added in v0.13.0
Sign signs a hash (which should be the result of hashing a larger message) using the user dsa key. It returns the signature as a pair of h and s.
func SignASN1 ¶ added in v0.13.0
SignASN1 signs a hash (which should be the result of hashing a larger message) using the private key, priv. It returns the ASN.1 encoded signature of type SM9Signature.
func UnmarshalSM9KeyPackage ¶ added in v0.13.0
UnmarshalSM9KeyPackage is an utility to unmarshal SM9KeyPackage
func Verify ¶ added in v0.13.0
func Verify(pub *SignMasterPublicKey, uid []byte, hid byte, hash []byte, h *big.Int, s *bn256.G1) bool
Verify verifies the signature in h, s of hash using the master dsa public key and user id, uid and hid. Its return value records whether the signature is valid.
func VerifyASN1 ¶ added in v0.13.0
func VerifyASN1(pub *SignMasterPublicKey, uid []byte, hid byte, hash, sig []byte) bool
VerifyASN1 verifies the ASN.1 encoded signature of type SM9Signature, sig, of hash using the public key, pub. Its return value records whether the signature is valid.
Types ¶
type EncryptMasterPrivateKey ¶ added in v0.13.0
type EncryptMasterPrivateKey struct { EncryptMasterPublicKey // master public key D *big.Int // master private key }
EncryptMasterPrivateKey master private key for encryption, generated by KGC
func GenerateEncryptMasterKey ¶ added in v0.13.0
func GenerateEncryptMasterKey(rand io.Reader) (*EncryptMasterPrivateKey, error)
GenerateEncryptMasterKey generates a master public and private key pair for encryption usage.
func (*EncryptMasterPrivateKey) GenerateUserKey ¶ added in v0.13.0
func (master *EncryptMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*EncryptPrivateKey, error)
GenerateUserKey generate an user key for encryption.
func (*EncryptMasterPrivateKey) MarshalASN1 ¶ added in v0.13.0
func (master *EncryptMasterPrivateKey) MarshalASN1() ([]byte, error)
MarshalASN1 marshal encrypt master private key to asn.1 format data according SM9 cryptographic algorithm application specification
func (*EncryptMasterPrivateKey) Public ¶ added in v0.13.0
func (master *EncryptMasterPrivateKey) Public() *EncryptMasterPublicKey
Public returns the public key corresponding to priv.
func (*EncryptMasterPrivateKey) UnmarshalASN1 ¶ added in v0.13.0
func (master *EncryptMasterPrivateKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to encrpt master private key
type EncryptMasterPublicKey ¶ added in v0.13.0
type EncryptMasterPublicKey struct { MasterPublicKey *bn256.G1 // public key // contains filtered or unexported fields }
EncryptMasterPublicKey master private key for encryption, generated by KGC
func (*EncryptMasterPublicKey) Encrypt ¶ added in v0.13.0
func (pub *EncryptMasterPublicKey) Encrypt(rand io.Reader, uid []byte, hid byte, plaintext []byte) ([]byte, error)
Encrypt encrypt plaintext and output ciphertext with ASN.1 format according SM9 cryptographic algorithm application specification, SM9Cipher definition.
func (*EncryptMasterPublicKey) GenerateUserPublicKey ¶ added in v0.13.0
func (pub *EncryptMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G1
GenerateUserPublicKey generate user encrypt public key
func (*EncryptMasterPublicKey) MarshalASN1 ¶ added in v0.13.0
func (pub *EncryptMasterPublicKey) MarshalASN1() ([]byte, error)
MarshalASN1 marshal encrypt master public key to asn.1 format data according SM9 cryptographic algorithm application specification
func (*EncryptMasterPublicKey) Pair ¶ added in v0.13.0
func (pub *EncryptMasterPublicKey) Pair() *bn256.GT
Pair generate the basepoint once
func (*EncryptMasterPublicKey) ScalarBaseMult ¶ added in v0.13.0
func (pub *EncryptMasterPublicKey) ScalarBaseMult(r *big.Int) *bn256.GT
ScalarBaseMult compute basepoint^r with precomputed table
func (*EncryptMasterPublicKey) UnmarshalASN1 ¶ added in v0.13.0
func (pub *EncryptMasterPublicKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to encrypt master public key
func (*EncryptMasterPublicKey) WrapKey ¶ added in v0.13.2
func (pub *EncryptMasterPublicKey) WrapKey(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, []byte, error)
WrapKey wrap key and marshal the cipher as ASN1 format, SM9PublicKey1 definition.
func (*EncryptMasterPublicKey) WrapKeyASN1 ¶ added in v0.13.2
func (pub *EncryptMasterPublicKey) WrapKeyASN1(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, error)
WrapKeyASN1 wrap key and marshal the result of SM9KeyPackage as ASN1 format. according SM9 cryptographic algorithm application specification, SM9KeyPackage defnition.
type EncryptPrivateKey ¶ added in v0.13.0
type EncryptPrivateKey struct { PrivateKey *bn256.G2 // user private key EncryptMasterPublicKey // master public key }
EncryptPrivateKey user private key for encryption, generated by KGC
func (*EncryptPrivateKey) Decrypt ¶ added in v0.13.2
func (priv *EncryptPrivateKey) Decrypt(uid, ciphertext []byte) ([]byte, error)
Decrypt decrypt chipher, ciphertext should be with ASN.1 format according SM9 cryptographic algorithm application specification, SM9Cipher definition.
func (*EncryptPrivateKey) MarshalASN1 ¶ added in v0.13.0
func (priv *EncryptPrivateKey) MarshalASN1() ([]byte, error)
MarshalASN1 marshal encrypt user private key to asn.1 format data according SM9 cryptographic algorithm application specification
func (*EncryptPrivateKey) MasterPublic ¶ added in v0.13.0
func (priv *EncryptPrivateKey) MasterPublic() *EncryptMasterPublicKey
MasterPublic returns the master public key corresponding to priv.
func (*EncryptPrivateKey) SetMasterPublicKey ¶ added in v0.13.0
func (priv *EncryptPrivateKey) SetMasterPublicKey(pub *EncryptMasterPublicKey)
SetMasterPublicKey bind the encrypt master public key to it.
func (*EncryptPrivateKey) UnmarshalASN1 ¶ added in v0.13.0
func (priv *EncryptPrivateKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to encrypt user private key Note, priv's EncryptMasterPublicKey should be handled separately.
func (*EncryptPrivateKey) UnwrapKey ¶ added in v0.13.2
func (priv *EncryptPrivateKey) UnwrapKey(uid, cipherDer []byte, kLen int) ([]byte, error)
UnwrapKey unwrap key from cipherDer, user id and aligned key length. cipherDer is SM9PublicKey1 format according SM9 cryptographic algorithm application specification.
type KeyExchange ¶ added in v0.13.2
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 -> InitKeyExchange -> transmission -> ConfirmResponder Responder's flow will be: NewKeyExchange -> waiting ... -> RepondKeyExchange -> transmission -> ConfirmInitiator
func NewKeyExchange ¶ added in v0.13.2
func NewKeyExchange(priv *EncryptPrivateKey, uid, peerUID []byte, keyLen int, genSignature bool) *KeyExchange
NewKeyExchange create one new KeyExchange object
func (*KeyExchange) ConfirmInitiator ¶ added in v0.13.2
func (ke *KeyExchange) ConfirmInitiator(s1 []byte) error
ConfirmInitiator for responder's step B8
func (*KeyExchange) ConfirmResponder ¶ added in v0.13.2
ConfirmResponder for initiator's step A5-A7
func (*KeyExchange) GetSharedKey ¶ added in v0.13.3
func (ke *KeyExchange) GetSharedKey() []byte
GetSharedKey return key after key agreement
func (*KeyExchange) InitKeyExchange ¶ added in v0.13.2
InitKeyExchange generate random with responder uid, for initiator's step A1-A4
type SignMasterPrivateKey ¶ added in v0.13.0
type SignMasterPrivateKey struct { SignMasterPublicKey // master public key D *big.Int // master private key }
SignMasterPrivateKey master private key for sign, generated by KGC
func GenerateSignMasterKey ¶ added in v0.13.0
func GenerateSignMasterKey(rand io.Reader) (*SignMasterPrivateKey, error)
GenerateSignMasterKey generates a master public and private key pair for DSA usage.
func (*SignMasterPrivateKey) GenerateUserKey ¶ added in v0.13.0
func (master *SignMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*SignPrivateKey, error)
GenerateUserKey generate an user dsa key.
func (*SignMasterPrivateKey) MarshalASN1 ¶ added in v0.13.0
func (master *SignMasterPrivateKey) MarshalASN1() ([]byte, error)
MarshalASN1 marshal sign master private key to asn.1 format data according SM9 cryptographic algorithm application specification
func (*SignMasterPrivateKey) Public ¶ added in v0.13.0
func (master *SignMasterPrivateKey) Public() *SignMasterPublicKey
Public returns the public key corresponding to priv.
func (*SignMasterPrivateKey) UnmarshalASN1 ¶ added in v0.13.0
func (master *SignMasterPrivateKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to sign master private key
type SignMasterPublicKey ¶ added in v0.13.0
type SignMasterPublicKey struct { MasterPublicKey *bn256.G2 // master public key // contains filtered or unexported fields }
SignMasterPublicKey master public key for sign, generated by KGC
func (*SignMasterPublicKey) GenerateUserPublicKey ¶ added in v0.13.0
func (pub *SignMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G2
GenerateUserPublicKey generate user sign public key
func (*SignMasterPublicKey) MarshalASN1 ¶ added in v0.13.0
func (pub *SignMasterPublicKey) MarshalASN1() ([]byte, error)
MarshalASN1 marshal sign master public key to asn.1 format data according SM9 cryptographic algorithm application specification
func (*SignMasterPublicKey) Pair ¶ added in v0.13.0
func (pub *SignMasterPublicKey) Pair() *bn256.GT
Pair generate the basepoint once
func (*SignMasterPublicKey) ScalarBaseMult ¶ added in v0.13.0
func (pub *SignMasterPublicKey) ScalarBaseMult(r *big.Int) *bn256.GT
ScalarBaseMult compute basepoint^r with precomputed table
func (*SignMasterPublicKey) UnmarshalASN1 ¶ added in v0.13.0
func (pub *SignMasterPublicKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to sign master public key
type SignPrivateKey ¶ added in v0.13.0
type SignPrivateKey struct { PrivateKey *bn256.G1 // user private key SignMasterPublicKey // master public key }
SignPrivateKey user private key for sign, generated by KGC
func (*SignPrivateKey) MarshalASN1 ¶ added in v0.13.0
func (priv *SignPrivateKey) MarshalASN1() ([]byte, error)
MarshalASN1 marshal sign user private key to asn.1 format data according SM9 cryptographic algorithm application specification
func (*SignPrivateKey) MasterPublic ¶ added in v0.13.0
func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey
MasterPublic returns the master public key corresponding to priv.
func (*SignPrivateKey) SetMasterPublicKey ¶ added in v0.13.0
func (priv *SignPrivateKey) SetMasterPublicKey(pub *SignMasterPublicKey)
SetMasterPublicKey bind the sign master public key to it.
func (*SignPrivateKey) Sign ¶ added in v0.13.0
func (priv *SignPrivateKey) Sign(rand io.Reader, hash []byte, opts crypto.SignerOpts) ([]byte, error)
Sign signs digest with user's DSA key, reading randomness from rand. The opts argument is not currently used but, in keeping with the crypto.Signer interface. The result is SM9Signature ASN.1 format.
func (*SignPrivateKey) UnmarshalASN1 ¶ added in v0.13.0
func (priv *SignPrivateKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to sign user private key Note, priv's SignMasterPublicKey should be handled separately.