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) MarshalCompressedASN1() ([]byte, error)
- 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) MarshalCompressedASN1() ([]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) MarshalCompressedASN1() ([]byte, error)
- 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) MarshalCompressedASN1() ([]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 ¶
func Decrypt(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error)
Decrypt decrypt chipher, ciphertext should be with format C1||C3||C2
func DecryptASN1 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
UnmarshalSM9KeyPackage is an utility to unmarshal SM9KeyPackage
func Verify ¶
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 ¶
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 ¶
type EncryptMasterPrivateKey struct { EncryptMasterPublicKey // master public key D *big.Int // master private key }
EncryptMasterPrivateKey master private key for encryption, generated by KGC
func GenerateEncryptMasterKey ¶
func GenerateEncryptMasterKey(rand io.Reader) (*EncryptMasterPrivateKey, error)
GenerateEncryptMasterKey generates a master public and private key pair for encryption usage.
func (*EncryptMasterPrivateKey) GenerateUserKey ¶
func (master *EncryptMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*EncryptPrivateKey, error)
GenerateUserKey generate an user key for encryption.
func (*EncryptMasterPrivateKey) MarshalASN1 ¶
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 ¶
func (master *EncryptMasterPrivateKey) Public() *EncryptMasterPublicKey
Public returns the public key corresponding to priv.
func (*EncryptMasterPrivateKey) UnmarshalASN1 ¶
func (master *EncryptMasterPrivateKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to encrpt master private key
type EncryptMasterPublicKey ¶
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 ¶
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 ¶
func (pub *EncryptMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G1
GenerateUserPublicKey generate user encrypt public key
func (*EncryptMasterPublicKey) MarshalASN1 ¶
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) MarshalCompressedASN1 ¶
func (pub *EncryptMasterPublicKey) MarshalCompressedASN1() ([]byte, error)
MarshalCompressedASN1 marshal encrypt master public key to asn.1 format data according SM9 cryptographic algorithm application specification, the curve point is in compressed form.
func (*EncryptMasterPublicKey) ScalarBaseMult ¶
func (pub *EncryptMasterPublicKey) ScalarBaseMult(r *big.Int) *bn256.GT
ScalarBaseMult compute basepoint^r with precomputed table. The base point = pair(<master public key>, Gen2)
func (*EncryptMasterPublicKey) UnmarshalASN1 ¶
func (pub *EncryptMasterPublicKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to encrypt master public key
func (*EncryptMasterPublicKey) WrapKey ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) MarshalCompressedASN1 ¶
func (priv *EncryptPrivateKey) MarshalCompressedASN1() ([]byte, error)
MarshalCompressedASN1 marshal encrypt user private key to asn.1 format data according SM9 cryptographic algorithm application specification, the curve point is in compressed form.
func (*EncryptPrivateKey) MasterPublic ¶
func (priv *EncryptPrivateKey) MasterPublic() *EncryptMasterPublicKey
MasterPublic returns the master public key corresponding to priv.
func (*EncryptPrivateKey) SetMasterPublicKey ¶
func (priv *EncryptPrivateKey) SetMasterPublicKey(pub *EncryptMasterPublicKey)
SetMasterPublicKey bind the encrypt master public key to it.
func (*EncryptPrivateKey) UnmarshalASN1 ¶
func (priv *EncryptPrivateKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to encrypt user private key Note, priv's EncryptMasterPublicKey should be handled separately.
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 -> InitKeyExchange -> transmission -> ConfirmResponder Responder's flow will be: NewKeyExchange -> waiting ... -> RepondKeyExchange -> transmission -> ConfirmInitiator
func NewKeyExchange ¶
func NewKeyExchange(priv *EncryptPrivateKey, uid, peerUID []byte, keyLen int, genSignature bool) *KeyExchange
NewKeyExchange create one new KeyExchange object
func (*KeyExchange) ConfirmInitiator ¶
func (ke *KeyExchange) ConfirmInitiator(s1 []byte) error
ConfirmInitiator for responder's step B8
func (*KeyExchange) ConfirmResponder ¶
ConfirmResponder for initiator's step A5-A7
func (*KeyExchange) GetSharedKey ¶
func (ke *KeyExchange) GetSharedKey() []byte
GetSharedKey return key after key agreement
func (*KeyExchange) InitKeyExchange ¶
InitKeyExchange generate random with responder uid, for initiator's step A1-A4
type SignMasterPrivateKey ¶
type SignMasterPrivateKey struct { SignMasterPublicKey // master public key D *big.Int // master private key }
SignMasterPrivateKey master private key for sign, generated by KGC
func GenerateSignMasterKey ¶
func GenerateSignMasterKey(rand io.Reader) (*SignMasterPrivateKey, error)
GenerateSignMasterKey generates a master public and private key pair for DSA usage.
func (*SignMasterPrivateKey) GenerateUserKey ¶
func (master *SignMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*SignPrivateKey, error)
GenerateUserKey generate an user dsa key.
func (*SignMasterPrivateKey) MarshalASN1 ¶
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 ¶
func (master *SignMasterPrivateKey) Public() *SignMasterPublicKey
Public returns the public key corresponding to priv.
func (*SignMasterPrivateKey) UnmarshalASN1 ¶
func (master *SignMasterPrivateKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to sign master private key
type SignMasterPublicKey ¶
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 ¶
func (pub *SignMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G2
GenerateUserPublicKey generate user sign public key
func (*SignMasterPublicKey) MarshalASN1 ¶
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) MarshalCompressedASN1 ¶
func (pub *SignMasterPublicKey) MarshalCompressedASN1() ([]byte, error)
MarshalCompressedASN1 marshal sign master public key to asn.1 format data according SM9 cryptographic algorithm application specification, the curve point is in compressed form.
func (*SignMasterPublicKey) ScalarBaseMult ¶
func (pub *SignMasterPublicKey) ScalarBaseMult(r *big.Int) *bn256.GT
ScalarBaseMult compute basepoint^r with precomputed table The base point = pair(Gen1, <master public key>)
func (*SignMasterPublicKey) UnmarshalASN1 ¶
func (pub *SignMasterPublicKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to sign master public key
type SignPrivateKey ¶
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 ¶
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) MarshalCompressedASN1 ¶
func (priv *SignPrivateKey) MarshalCompressedASN1() ([]byte, error)
MarshalCompressedASN1 marshal sign user private key to asn.1 format data according SM9 cryptographic algorithm application specification, the curve point is in compressed form.
func (*SignPrivateKey) MasterPublic ¶
func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey
MasterPublic returns the master public key corresponding to priv.
func (*SignPrivateKey) SetMasterPublicKey ¶
func (priv *SignPrivateKey) SetMasterPublicKey(pub *SignMasterPublicKey)
SetMasterPublicKey bind the sign master public key to it.
func (*SignPrivateKey) Sign ¶
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 ¶
func (priv *SignPrivateKey) UnmarshalASN1(der []byte) error
UnmarshalASN1 unmarsal der data to sign user private key Note, priv's SignMasterPublicKey should be handled separately.