gitee.com/lh-her-team/common@v1.5.1/helper/libp2pcrypto/sm2.go (about) 1 package libp2pcrypto 2 3 import ( 4 "crypto/rand" 5 "encoding/asn1" 6 "math/big" 7 8 tjx509 "github.com/tjfoc/gmsm/x509" 9 10 "gitee.com/lh-her-team/common/helper/libp2pcrypto/pb" 11 "github.com/tjfoc/gmsm/sm2" 12 13 sha256 "github.com/minio/sha256-simd" 14 ) 15 16 var _ PrivKey = (*SM2PrivateKey)(nil) 17 18 // SM2PrivateKey is an implementation of an SM2 private key 19 type SM2PrivateKey struct { 20 priv *sm2.PrivateKey 21 } 22 23 var _ PubKey = (*SM2PublicKey)(nil) 24 25 // SM2PublicKey is an implementation of an SM2 public key 26 type SM2PublicKey struct { 27 pub *sm2.PublicKey 28 } 29 30 func NewSM2PublicKey(pub *sm2.PublicKey) *SM2PublicKey { 31 return &SM2PublicKey{pub: pub} 32 } 33 34 // ECDSASig holds the r and s values of an ECDSA signature 35 type SM2Sig struct { 36 R, S *big.Int 37 } 38 39 // GenerateSM2KeyPair generates a new sm2 private and public key 40 func GenerateSM2KeyPair() (PrivKey, PubKey, error) { 41 priv, err := sm2.GenerateKey(rand.Reader) 42 if err != nil { 43 return nil, nil, err 44 } 45 return &SM2PrivateKey{priv}, &SM2PublicKey{&priv.PublicKey}, nil 46 } 47 48 // SM2KeyPairFromKey generates a new SM2 private and public key from an input private key 49 func SM2KeyPairFromKey(priv *sm2.PrivateKey) (PrivKey, PubKey, error) { 50 if priv == nil { 51 return nil, nil, ErrNilPrivateKey 52 } 53 return &SM2PrivateKey{priv}, &SM2PublicKey{&priv.PublicKey}, nil 54 } 55 56 // MarshalSM2PrivateKey returns x509 bytes from a private key 57 func MarshalSM2PrivateKey(ePriv SM2PrivateKey) ([]byte, error) { 58 return tjx509.MarshalSm2UnecryptedPrivateKey(ePriv.priv) 59 } 60 61 // MarshalSM2PublicKey returns x509 bytes from a public key 62 func MarshalSM2PublicKey(ePub SM2PublicKey) ([]byte, error) { 63 return tjx509.MarshalSm2PublicKey(ePub.pub) 64 } 65 66 // UnmarshalSM2PrivateKey returns a private key from x509 bytes 67 func UnmarshalSM2PrivateKey(data []byte) (PrivKey, error) { 68 priv, err := tjx509.ParsePKCS8UnecryptedPrivateKey(data) 69 if err != nil { 70 return nil, err 71 } 72 return &SM2PrivateKey{priv}, nil 73 } 74 75 // UnmarshalSM2PublicKey returns the public key from x509 bytes 76 func UnmarshalSM2PublicKey(data []byte) (PubKey, error) { 77 pub, err := tjx509.ParseSm2PublicKey(data) 78 if err != nil { 79 return nil, err 80 } 81 return &SM2PublicKey{pub}, nil 82 } 83 84 // Bytes returns the private key as protobuf bytes 85 func (ePriv *SM2PrivateKey) Bytes() ([]byte, error) { 86 return MarshalPrivateKey(ePriv) 87 } 88 89 // Type returns the key type 90 func (ePriv *SM2PrivateKey) Type() pb.KeyType { 91 return pb.KeyType_SM2 92 } 93 94 // Raw returns x509 bytes from a private key 95 func (ePriv *SM2PrivateKey) Raw() ([]byte, error) { 96 return tjx509.MarshalSm2UnecryptedPrivateKey(ePriv.priv) 97 } 98 99 // Equals compares two private keys 100 func (ePriv *SM2PrivateKey) Equals(o Key) bool { 101 return basicEquals(ePriv, o) 102 } 103 104 // Sign returns the signature of the input data 105 func (ePriv *SM2PrivateKey) Sign(data []byte) ([]byte, error) { 106 hash := sha256.Sum256(data) 107 return ePriv.priv.Sign(rand.Reader, hash[:], nil) 108 } 109 110 // GetPublic returns a public key 111 func (ePriv *SM2PrivateKey) GetPublic() PubKey { 112 return &SM2PublicKey{&ePriv.priv.PublicKey} 113 } 114 115 // Bytes returns the public key as protobuf bytes 116 func (ePub *SM2PublicKey) Bytes() ([]byte, error) { 117 return MarshalPublicKey(ePub) 118 } 119 120 // Type returns the key type 121 func (ePub *SM2PublicKey) Type() pb.KeyType { 122 return pb.KeyType_SM2 123 } 124 125 // Raw returns x509 bytes from a public key 126 func (ePub *SM2PublicKey) Raw() ([]byte, error) { 127 return tjx509.MarshalPKIXPublicKey(ePub.pub) 128 } 129 130 // Equals compares to public keys 131 func (ePub *SM2PublicKey) Equals(o Key) bool { 132 return basicEquals(ePub, o) 133 } 134 135 // Verify compares data to a signature 136 func (ePub *SM2PublicKey) Verify(data, sigBytes []byte) (bool, error) { 137 sig := new(SM2Sig) 138 if _, err := asn1.Unmarshal(sigBytes, sig); err != nil { 139 return false, err 140 } 141 if sig == nil { 142 return false, ErrNilSig 143 } 144 hash := sha256.Sum256(data) 145 return sm2.Verify(ePub.pub, hash[:], sig.R, sig.S), nil 146 }