sm2

package
v0.0.0-...-ed24dd9 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2020 License: Apache-2.0 Imports: 16 Imported by: 6

Documentation

Index

Constants

View Source
const (
	BitSize    = 256
	KeyBytes   = (BitSize + 7) / 8
	UnCompress = 0x04
)
View Source
const (
	// DefaultUID The default user id as specified in GM/T 0009-2012
	DefaultUID = "1234567812345678"
)

Variables

View Source
var RInverse, _ = new(big.Int).SetString("7ffffffd80000002fffffffe000000017ffffffe800000037ffffffc80000002", 16)

Functions

func Compress

func Compress(a *PublicKey) []byte

Compress publickey to 33 bytes.

func Decrypt

func Decrypt(priv *PrivateKey, in []byte, cipherTextType Sm2CipherTextType) ([]byte, error)

func Encrypt

func Encrypt(pub *PublicKey, in []byte, cipherTextType Sm2CipherTextType) ([]byte, error)

func GenerateKey

func GenerateKey(rand io.Reader) (*PrivateKey, *PublicKey, error)

func MarshalCipher

func MarshalCipher(in []byte, cipherTextType Sm2CipherTextType) ([]byte, error)

func ResponderConfirm

func ResponderConfirm(responderS2 []byte, initiatorS2 []byte) bool

func SigToPub

func SigToPub(hash, sig, userId []byte, ee *big.Int) (*ecdsa.PublicKey, error)

func Sign

func Sign(priv *PrivateKey, userId []byte, in []byte) ([]byte, *big.Int, error)

gm sign with privatekey, r and s covert to byte array ,According to byte array length to implementation method.

func SignToRS

func SignToRS(priv *PrivateKey, userId []byte, in []byte) (r, s, ee *big.Int, err error)

sign algorithm.

func ToECDSAPublickey

func ToECDSAPublickey(key *PublicKey) *ecdsa.PublicKey

gm publickey covert to ecdsa publickey.

func ToEcdsaPrivate

func ToEcdsaPrivate(key *PrivateKey) *ecdsa.PrivateKey

gm privatekey covert to ecdsa privatekey.

func UnmarshalCipher

func UnmarshalCipher(in []byte, cipherTextType Sm2CipherTextType) (out []byte, err error)

func ValidateSignatureValues

func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool

Validate sign value about v,r and s.

func Verify

func Verify(pub *PublicKey, userId []byte, src []byte, sign []byte) bool

Verift sign with publickey.

func VerifyByRS

func VerifyByRS(pub *PublicKey, userId []byte, src []byte, r, s *big.Int) bool

verify sign algorithm.

Types

type ExchangeResult

type ExchangeResult struct {
	Key []byte
	S1  []byte
	S2  []byte
}

func CalculateKeyWithConfirmation

func CalculateKeyWithConfirmation(initiator bool, keyBits int, confirmationTag []byte,
	selfStaticPriv *PrivateKey, selfEphemeralPriv *PrivateKey, selfId []byte,
	otherStaticPub *PublicKey, otherEphemeralPub *PublicKey, otherId []byte) (*ExchangeResult, error)

type P256V1Curve

type P256V1Curve struct {
	*elliptic.CurveParams
	A *big.Int
}

func GetSm2P256V1

func GetSm2P256V1() P256V1Curve

func (P256V1Curve) Add

func (curve P256V1Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)

func (P256V1Curve) Double

func (curve P256V1Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int)

func (P256V1Curve) IsOnCurve

func (curve P256V1Curve) IsOnCurve(X, Y *big.Int) bool

y^2 = x^3 + ax + b

func (P256V1Curve) Params

func (curve P256V1Curve) Params() *elliptic.CurveParams

func (P256V1Curve) QPlus1Div4

func (curve P256V1Curve) QPlus1Div4() *big.Int

func (P256V1Curve) ScalarBaseMult

func (curve P256V1Curve) ScalarBaseMult(k []byte) (*big.Int, *big.Int)

func (P256V1Curve) ScalarMult

func (curve P256V1Curve) ScalarMult(x1, y1 *big.Int, k []byte) (*big.Int, *big.Int)

type PrivateKey

type PrivateKey struct {
	D     *big.Int
	Curve P256V1Curve
	PublicKey
}

func ToSm2privatekey

func ToSm2privatekey(key *ecdsa.PrivateKey) *PrivateKey

ecdsa privatekey covert to gm privatekey.

func (*PrivateKey) GenerateShared

func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []byte, err error)

func (*PrivateKey) GetRawBytes

func (pri *PrivateKey) GetRawBytes() []byte

type PublicKey

type PublicKey struct {
	X, Y  *big.Int
	Curve P256V1Curve
}

func Decompress

func Decompress(a []byte) *PublicKey

Decompress transform 33 bytes publickey to publickey point struct.

func RecoverCompactSM2

func RecoverCompactSM2(curve P256V1Curve, signature,
	hash, userId []byte, ee *big.Int) (*PublicKey, bool, error)

RecoverCompact verifies the compact signature "signature" of "hash" for the Koblitz curve in "curve". If the signature matches then the recovered public key will be returned as well as a boolen if the original key was compressed or not, else an error will be returned.

func ToSm2Publickey

func ToSm2Publickey(key *ecdsa.PublicKey) *PublicKey

ecdsa publickey covert to gm publickey.

func (*PublicKey) Error

func (pub *PublicKey) Error() string

func (*PublicKey) GetRawBytes

func (pub *PublicKey) GetRawBytes() []byte

func (*PublicKey) GetUnCompressBytes

func (pub *PublicKey) GetUnCompressBytes() []byte

type Sm2CipherTextType

type Sm2CipherTextType int32
const (
	C1C2C3 Sm2CipherTextType = 1
	C1C3C2 Sm2CipherTextType = 2
)

type Sm2Signature

type Sm2Signature struct {
	R, S *big.Int
	X, Y *big.Int
}

Jump to

Keyboard shortcuts

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