sm9

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package sm9 implements ShangMi(SM) sm9 digital signature, encryption and key exchange algorithms.

Index

Constants

View Source
const (
	H1 hashMode = 1 + iota
	H2
)
View Source
const (
	EncTypeXor encryptType = 0
	EncTypeEcb encryptType = 1
	EncTypeCbc encryptType = 2
	EncTypeOfb encryptType = 4
	EncTypeCfb encryptType = 8
)

Variables

View Source
var DefaultEncrypterOpts = new(XOREncrypterOpts)
View Source
var ErrDecryption = errors.New("sm9: decryption error")
View Source
var ErrEmptyPlaintext = errors.New("sm9: empty plaintext")

Functions

func Decrypt

func Decrypt(priv *EncryptPrivateKey, uid, ciphertext []byte, opts EncrypterOpts) ([]byte, error)

func DecryptASN1

func DecryptASN1(priv *EncryptPrivateKey, uid, ciphertext []byte) ([]byte, error)

func Encrypt

func Encrypt(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, plaintext []byte, opts EncrypterOpts) ([]byte, error)

func EncryptASN1

func EncryptASN1(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, plaintext []byte, opts EncrypterOpts) ([]byte, error)

func Sign

func Sign(rand io.Reader, priv *SignPrivateKey, hash []byte) (h *big.Int, s *bn256.G1, err error)

func SignASN1

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

func UnmarshalSM9KeyPackage

func UnmarshalSM9KeyPackage(der []byte) ([]byte, *bn256.G1, error)

func UnwrapKey

func UnwrapKey(priv *EncryptPrivateKey, uid []byte, cipher *bn256.G1, kLen int) ([]byte, error)

func Verify

func Verify(pub *SignMasterPublicKey, uid []byte, hid byte, hash []byte, h *big.Int, s *bn256.G1) bool

func VerifyASN1

func VerifyASN1(pub *SignMasterPublicKey, uid []byte, hid byte, hash, sig []byte) bool

func WrapKey

func WrapKey(rand io.Reader, pub *EncryptMasterPublicKey, uid []byte, hid byte, kLen int) (key []byte, cipher *bn256.G1, err error)

Types

type CBCEncrypterOpts

type CBCEncrypterOpts struct {
	// contains filtered or unexported fields
}

func (*CBCEncrypterOpts) Decrypt

func (opts *CBCEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error)

func (*CBCEncrypterOpts) Encrypt

func (opts *CBCEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error)

func (*CBCEncrypterOpts) GetEncryptType

func (opts *CBCEncrypterOpts) GetEncryptType() encryptType

func (*CBCEncrypterOpts) GetKeySize

func (opts *CBCEncrypterOpts) GetKeySize(plaintext []byte) int

type CFBEncrypterOpts

type CFBEncrypterOpts struct {
	// contains filtered or unexported fields
}

func (*CFBEncrypterOpts) Decrypt

func (opts *CFBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error)

func (*CFBEncrypterOpts) Encrypt

func (opts *CFBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error)

func (*CFBEncrypterOpts) GetEncryptType

func (opts *CFBEncrypterOpts) GetEncryptType() encryptType

func (*CFBEncrypterOpts) GetKeySize

func (opts *CFBEncrypterOpts) GetKeySize(plaintext []byte) int

type ECBEncrypterOpts

type ECBEncrypterOpts struct {
	// contains filtered or unexported fields
}

func (*ECBEncrypterOpts) Decrypt

func (opts *ECBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error)

func (*ECBEncrypterOpts) Encrypt

func (opts *ECBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error)

func (*ECBEncrypterOpts) GetEncryptType

func (opts *ECBEncrypterOpts) GetEncryptType() encryptType

func (*ECBEncrypterOpts) GetKeySize

func (opts *ECBEncrypterOpts) GetKeySize(plaintext []byte) int

type EncryptMasterPrivateKey

type EncryptMasterPrivateKey struct {
	EncryptMasterPublicKey
	D *big.Int
}

func GenerateEncryptMasterKey

func GenerateEncryptMasterKey(rand io.Reader) (*EncryptMasterPrivateKey, error)

func (*EncryptMasterPrivateKey) GenerateUserKey

func (master *EncryptMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*EncryptPrivateKey, error)

func (*EncryptMasterPrivateKey) MarshalASN1

func (master *EncryptMasterPrivateKey) MarshalASN1() ([]byte, error)

func (*EncryptMasterPrivateKey) Public

func (*EncryptMasterPrivateKey) UnmarshalASN1

func (master *EncryptMasterPrivateKey) UnmarshalASN1(der []byte) error

type EncryptMasterPublicKey

type EncryptMasterPublicKey struct {
	MasterPublicKey *bn256.G1
	// contains filtered or unexported fields
}

func (*EncryptMasterPublicKey) Encrypt

func (pub *EncryptMasterPublicKey) Encrypt(rand io.Reader, uid []byte, hid byte, plaintext []byte, opts EncrypterOpts) ([]byte, error)

func (*EncryptMasterPublicKey) GenerateUserPublicKey

func (pub *EncryptMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G1

func (*EncryptMasterPublicKey) MarshalASN1

func (pub *EncryptMasterPublicKey) MarshalASN1() ([]byte, error)

func (*EncryptMasterPublicKey) MarshalCompressedASN1

func (pub *EncryptMasterPublicKey) MarshalCompressedASN1() ([]byte, error)

func (*EncryptMasterPublicKey) ParseFromPEM

func (pub *EncryptMasterPublicKey) ParseFromPEM(data []byte) error

func (*EncryptMasterPublicKey) ScalarBaseMult

func (pub *EncryptMasterPublicKey) ScalarBaseMult(scalar []byte) (*bn256.GT, error)

func (*EncryptMasterPublicKey) UnmarshalASN1

func (pub *EncryptMasterPublicKey) UnmarshalASN1(der []byte) error

func (*EncryptMasterPublicKey) UnmarshalRaw

func (pub *EncryptMasterPublicKey) UnmarshalRaw(bytes []byte) error

func (*EncryptMasterPublicKey) WrapKey

func (pub *EncryptMasterPublicKey) WrapKey(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, []byte, error)

func (*EncryptMasterPublicKey) WrapKeyASN1

func (pub *EncryptMasterPublicKey) WrapKeyASN1(rand io.Reader, uid []byte, hid byte, kLen int) ([]byte, error)

type EncryptPrivateKey

type EncryptPrivateKey struct {
	PrivateKey *bn256.G2
	EncryptMasterPublicKey
}

func (*EncryptPrivateKey) Decrypt

func (priv *EncryptPrivateKey) Decrypt(uid, ciphertext []byte, opts EncrypterOpts) ([]byte, error)

func (*EncryptPrivateKey) DecryptASN1

func (priv *EncryptPrivateKey) DecryptASN1(uid, ciphertext []byte) ([]byte, error)

func (*EncryptPrivateKey) MarshalASN1

func (priv *EncryptPrivateKey) MarshalASN1() ([]byte, error)

func (*EncryptPrivateKey) MarshalCompressedASN1

func (priv *EncryptPrivateKey) MarshalCompressedASN1() ([]byte, error)

func (*EncryptPrivateKey) MasterPublic

func (priv *EncryptPrivateKey) MasterPublic() *EncryptMasterPublicKey

func (*EncryptPrivateKey) SetMasterPublicKey

func (priv *EncryptPrivateKey) SetMasterPublicKey(pub *EncryptMasterPublicKey)

func (*EncryptPrivateKey) UnmarshalASN1

func (priv *EncryptPrivateKey) UnmarshalASN1(der []byte) error

func (*EncryptPrivateKey) UnmarshalRaw

func (priv *EncryptPrivateKey) UnmarshalRaw(bytes []byte) error

func (*EncryptPrivateKey) UnwrapKey

func (priv *EncryptPrivateKey) UnwrapKey(uid, cipherDer []byte, kLen int) ([]byte, error)

type EncrypterOpts

type EncrypterOpts interface {
	GetEncryptType() encryptType
	GetKeySize(plaintext []byte) int
	Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error)
	Decrypt(key, ciphertext []byte) ([]byte, error)
}

func NewCBCEncrypterOpts

func NewCBCEncrypterOpts(padding padding.Padding, newCipher newCipher, keySize int) EncrypterOpts

func NewCFBEncrypterOpts

func NewCFBEncrypterOpts(newCipher newCipher, keySize int) EncrypterOpts

func NewECBEncrypterOpts

func NewECBEncrypterOpts(padding padding.Padding, newCipher newCipher, keySize int) EncrypterOpts

func NewOFBEncrypterOpts

func NewOFBEncrypterOpts(newCipher newCipher, keySize int) EncrypterOpts

type KeyExchange

type KeyExchange struct {
	// contains filtered or unexported fields
}

func NewKeyExchange

func NewKeyExchange(priv *EncryptPrivateKey, uid, peerUID []byte, keyLen int, genSignature bool) *KeyExchange

func (*KeyExchange) ConfirmInitiator

func (ke *KeyExchange) ConfirmInitiator(s1 []byte) ([]byte, error)

func (*KeyExchange) ConfirmResponder

func (ke *KeyExchange) ConfirmResponder(rB *bn256.G1, sB []byte) ([]byte, []byte, error)

func (*KeyExchange) Destroy

func (ke *KeyExchange) Destroy()

func (*KeyExchange) Init

func (ke *KeyExchange) Init(rand io.Reader, hid byte) (*bn256.G1, error)

func (*KeyExchange) Respond

func (ke *KeyExchange) Respond(rand io.Reader, hid byte, rA *bn256.G1) (*bn256.G1, []byte, error)

type OFBEncrypterOpts

type OFBEncrypterOpts struct {
	// contains filtered or unexported fields
}

func (*OFBEncrypterOpts) Decrypt

func (opts *OFBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error)

func (*OFBEncrypterOpts) Encrypt

func (opts *OFBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error)

func (*OFBEncrypterOpts) GetEncryptType

func (opts *OFBEncrypterOpts) GetEncryptType() encryptType

func (*OFBEncrypterOpts) GetKeySize

func (opts *OFBEncrypterOpts) GetKeySize(plaintext []byte) int

type SignMasterPrivateKey

type SignMasterPrivateKey struct {
	SignMasterPublicKey
	D *big.Int
}

func GenerateSignMasterKey

func GenerateSignMasterKey(rand io.Reader) (*SignMasterPrivateKey, error)

func (*SignMasterPrivateKey) GenerateUserKey

func (master *SignMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*SignPrivateKey, error)

func (*SignMasterPrivateKey) MarshalASN1

func (master *SignMasterPrivateKey) MarshalASN1() ([]byte, error)

func (*SignMasterPrivateKey) Public

func (master *SignMasterPrivateKey) Public() *SignMasterPublicKey

func (*SignMasterPrivateKey) UnmarshalASN1

func (master *SignMasterPrivateKey) UnmarshalASN1(der []byte) error

type SignMasterPublicKey

type SignMasterPublicKey struct {
	MasterPublicKey *bn256.G2
	// contains filtered or unexported fields
}

func (*SignMasterPublicKey) GenerateUserPublicKey

func (pub *SignMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G2

func (*SignMasterPublicKey) MarshalASN1

func (pub *SignMasterPublicKey) MarshalASN1() ([]byte, error)

func (*SignMasterPublicKey) MarshalCompressedASN1

func (pub *SignMasterPublicKey) MarshalCompressedASN1() ([]byte, error)

func (*SignMasterPublicKey) ParseFromPEM

func (pub *SignMasterPublicKey) ParseFromPEM(data []byte) error

func (*SignMasterPublicKey) ScalarBaseMult

func (pub *SignMasterPublicKey) ScalarBaseMult(scalar []byte) (*bn256.GT, error)

func (*SignMasterPublicKey) UnmarshalASN1

func (pub *SignMasterPublicKey) UnmarshalASN1(der []byte) error

func (*SignMasterPublicKey) UnmarshalRaw

func (pub *SignMasterPublicKey) UnmarshalRaw(bytes []byte) error

func (*SignMasterPublicKey) Verify

func (pub *SignMasterPublicKey) Verify(uid []byte, hid byte, hash, sig []byte) bool

type SignPrivateKey

type SignPrivateKey struct {
	PrivateKey *bn256.G1
	SignMasterPublicKey
}

func (*SignPrivateKey) MarshalASN1

func (priv *SignPrivateKey) MarshalASN1() ([]byte, error)

func (*SignPrivateKey) MarshalCompressedASN1

func (priv *SignPrivateKey) MarshalCompressedASN1() ([]byte, error)

func (*SignPrivateKey) MasterPublic

func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey

func (*SignPrivateKey) SetMasterPublicKey

func (priv *SignPrivateKey) SetMasterPublicKey(pub *SignMasterPublicKey)

func (*SignPrivateKey) Sign

func (priv *SignPrivateKey) Sign(rand io.Reader, hash []byte, opts crypto.SignerOpts) ([]byte, error)

func (*SignPrivateKey) UnmarshalASN1

func (priv *SignPrivateKey) UnmarshalASN1(der []byte) error

func (*SignPrivateKey) UnmarshalRaw

func (priv *SignPrivateKey) UnmarshalRaw(bytes []byte) error

type XOREncrypterOpts

type XOREncrypterOpts struct{}

func (*XOREncrypterOpts) Decrypt

func (opts *XOREncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error)

func (*XOREncrypterOpts) Encrypt

func (opts *XOREncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error)

func (*XOREncrypterOpts) GetEncryptType

func (opts *XOREncrypterOpts) GetEncryptType() encryptType

func (*XOREncrypterOpts) GetKeySize

func (opts *XOREncrypterOpts) GetKeySize(plaintext []byte) int

Directories

Path Synopsis
Package bn256 defines/implements ShangMi(SM) sm9's curves and pairing.
Package bn256 defines/implements ShangMi(SM) sm9's curves and pairing.

Jump to

Keyboard shortcuts

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