Commit 2d8e3bea authored by sanghg's avatar sanghg Committed by vipwzw

修复crypto中golint的警告

修复ecdsa的错误
parent a783470c
...@@ -19,43 +19,47 @@ import ( ...@@ -19,43 +19,47 @@ import (
) )
const ( const (
ECDSA_RPIVATEKEY_LENGTH = 32 privateKeyECDSALength = 32
ECDSA_PUBLICKEY_LENGTH = 65 publicKeyECDSALength = 65
) )
// Driver driver
type Driver struct{} type Driver struct{}
// Ctypto // GenKey create private key
func (d Driver) GenKey() (crypto.PrivKey, error) { func (d Driver) GenKey() (crypto.PrivKey, error) {
privKeyBytes := [ECDSA_RPIVATEKEY_LENGTH]byte{} privKeyBytes := [privateKeyECDSALength]byte{}
copy(privKeyBytes[:], crypto.CRandBytes(ECDSA_RPIVATEKEY_LENGTH)) copy(privKeyBytes[:], crypto.CRandBytes(privateKeyECDSALength))
priv, _ := privKeyFromBytes(elliptic.P256(), privKeyBytes[:]) priv, _ := privKeyFromBytes(elliptic.P256(), privKeyBytes[:])
copy(privKeyBytes[:], SerializePrivateKey(priv)) copy(privKeyBytes[:], SerializePrivateKey(priv))
return PrivKeyECDSA(privKeyBytes), nil return PrivKeyECDSA(privKeyBytes), nil
} }
// PrivKeyFromBytes create private key from bytes
func (d Driver) PrivKeyFromBytes(b []byte) (privKey crypto.PrivKey, err error) { func (d Driver) PrivKeyFromBytes(b []byte) (privKey crypto.PrivKey, err error) {
if len(b) != ECDSA_RPIVATEKEY_LENGTH { if len(b) != privateKeyECDSALength {
return nil, errors.New("invalid priv key byte") return nil, errors.New("invalid priv key byte")
} }
privKeyBytes := new([ECDSA_RPIVATEKEY_LENGTH]byte) privKeyBytes := new([privateKeyECDSALength]byte)
copy(privKeyBytes[:], b[:ECDSA_RPIVATEKEY_LENGTH]) copy(privKeyBytes[:], b[:privateKeyECDSALength])
priv, _ := privKeyFromBytes(elliptic.P256(), privKeyBytes[:]) priv, _ := privKeyFromBytes(elliptic.P256(), privKeyBytes[:])
copy(privKeyBytes[:], SerializePrivateKey(priv)) copy(privKeyBytes[:], SerializePrivateKey(priv))
return PrivKeyECDSA(*privKeyBytes), nil return PrivKeyECDSA(*privKeyBytes), nil
} }
// PubKeyFromBytes create public key from bytes
func (d Driver) PubKeyFromBytes(b []byte) (pubKey crypto.PubKey, err error) { func (d Driver) PubKeyFromBytes(b []byte) (pubKey crypto.PubKey, err error) {
if len(b) != ECDSA_PUBLICKEY_LENGTH { if len(b) != publicKeyECDSALength {
return nil, errors.New("invalid pub key byte") return nil, errors.New("invalid pub key byte")
} }
pubKeyBytes := new([ECDSA_PUBLICKEY_LENGTH]byte) pubKeyBytes := new([publicKeyECDSALength]byte)
copy(pubKeyBytes[:], b[:]) copy(pubKeyBytes[:], b[:])
return PubKeyECDSA(*pubKeyBytes), nil return PubKeyECDSA(*pubKeyBytes), nil
} }
// SignatureFromBytes create signature from bytes
func (d Driver) SignatureFromBytes(b []byte) (sig crypto.Signature, err error) { func (d Driver) SignatureFromBytes(b []byte) (sig crypto.Signature, err error) {
var certSignature crypto.CertSignature var certSignature crypto.CertSignature
_, err = asn1.Unmarshal(b, &certSignature) _, err = asn1.Unmarshal(b, &certSignature)
...@@ -70,15 +74,17 @@ func (d Driver) SignatureFromBytes(b []byte) (sig crypto.Signature, err error) { ...@@ -70,15 +74,17 @@ func (d Driver) SignatureFromBytes(b []byte) (sig crypto.Signature, err error) {
return SignatureECDSA(certSignature.Signature), nil return SignatureECDSA(certSignature.Signature), nil
} }
// PrivKey // PrivKeyECDSA PrivKey
type PrivKeyECDSA [ECDSA_RPIVATEKEY_LENGTH]byte type PrivKeyECDSA [privateKeyECDSALength]byte
// Bytes convert to bytes
func (privKey PrivKeyECDSA) Bytes() []byte { func (privKey PrivKeyECDSA) Bytes() []byte {
s := make([]byte, ECDSA_RPIVATEKEY_LENGTH) s := make([]byte, privateKeyECDSALength)
copy(s, privKey[:]) copy(s, privKey[:])
return s return s
} }
// Sign create signature
func (privKey PrivKeyECDSA) Sign(msg []byte) crypto.Signature { func (privKey PrivKeyECDSA) Sign(msg []byte) crypto.Signature {
priv, pub := privKeyFromBytes(elliptic.P256(), privKey[:]) priv, pub := privKeyFromBytes(elliptic.P256(), privKey[:])
r, s, err := ecdsa.Sign(rand.Reader, priv, crypto.Sha256(msg)) r, s, err := ecdsa.Sign(rand.Reader, priv, crypto.Sha256(msg))
...@@ -91,6 +97,7 @@ func (privKey PrivKeyECDSA) Sign(msg []byte) crypto.Signature { ...@@ -91,6 +97,7 @@ func (privKey PrivKeyECDSA) Sign(msg []byte) crypto.Signature {
return SignatureECDSA(ecdsaSigByte) return SignatureECDSA(ecdsaSigByte)
} }
// PubKey convert to public key
func (privKey PrivKeyECDSA) PubKey() crypto.PubKey { func (privKey PrivKeyECDSA) PubKey() crypto.PubKey {
_, pub := privKeyFromBytes(elliptic.P256(), privKey[:]) _, pub := privKeyFromBytes(elliptic.P256(), privKey[:])
var pubECDSA PubKeyECDSA var pubECDSA PubKeyECDSA
...@@ -98,6 +105,7 @@ func (privKey PrivKeyECDSA) PubKey() crypto.PubKey { ...@@ -98,6 +105,7 @@ func (privKey PrivKeyECDSA) PubKey() crypto.PubKey {
return pubECDSA return pubECDSA
} }
// Equals check privkey is equal
func (privKey PrivKeyECDSA) Equals(other crypto.PrivKey) bool { func (privKey PrivKeyECDSA) Equals(other crypto.PrivKey) bool {
if otherSecp, ok := other.(PrivKeyECDSA); ok { if otherSecp, ok := other.(PrivKeyECDSA); ok {
return bytes.Equal(privKey[:], otherSecp[:]) return bytes.Equal(privKey[:], otherSecp[:])
...@@ -106,20 +114,23 @@ func (privKey PrivKeyECDSA) Equals(other crypto.PrivKey) bool { ...@@ -106,20 +114,23 @@ func (privKey PrivKeyECDSA) Equals(other crypto.PrivKey) bool {
return false return false
} }
// String convert to string
func (privKey PrivKeyECDSA) String() string { func (privKey PrivKeyECDSA) String() string {
return fmt.Sprintf("PrivKeyECDSA{*****}") return fmt.Sprintf("PrivKeyECDSA{*****}")
} }
// PubKey // PubKeyECDSA PubKey
// prefixed with 0x02 or 0x03, depending on the y-cord. // prefixed with 0x02 or 0x03, depending on the y-cord.
type PubKeyECDSA [ECDSA_PUBLICKEY_LENGTH]byte type PubKeyECDSA [publicKeyECDSALength]byte
// Bytes convert to bytes
func (pubKey PubKeyECDSA) Bytes() []byte { func (pubKey PubKeyECDSA) Bytes() []byte {
s := make([]byte, ECDSA_PUBLICKEY_LENGTH) s := make([]byte, publicKeyECDSALength)
copy(s, pubKey[:]) copy(s, pubKey[:])
return s return s
} }
// VerifyBytes verify signature
func (pubKey PubKeyECDSA) VerifyBytes(msg []byte, sig crypto.Signature) bool { func (pubKey PubKeyECDSA) VerifyBytes(msg []byte, sig crypto.Signature) bool {
// unwrap if needed // unwrap if needed
if wrap, ok := sig.(SignatureS); ok { if wrap, ok := sig.(SignatureS); ok {
...@@ -148,43 +159,48 @@ func (pubKey PubKeyECDSA) VerifyBytes(msg []byte, sig crypto.Signature) bool { ...@@ -148,43 +159,48 @@ func (pubKey PubKeyECDSA) VerifyBytes(msg []byte, sig crypto.Signature) bool {
return ecdsa.Verify(pub, crypto.Sha256(msg), r, s) return ecdsa.Verify(pub, crypto.Sha256(msg), r, s)
} }
// String convert to string
func (pubKey PubKeyECDSA) String() string { func (pubKey PubKeyECDSA) String() string {
return fmt.Sprintf("PubKeyECDSA{%X}", pubKey[:]) return fmt.Sprintf("PubKeyECDSA{%X}", pubKey[:])
} }
// Must return the full bytes in hex. // KeyString Must return the full bytes in hex.
// Used for map keying, etc. // Used for map keying, etc.
func (pubKey PubKeyECDSA) KeyString() string { func (pubKey PubKeyECDSA) KeyString() string {
return fmt.Sprintf("%X", pubKey[:]) return fmt.Sprintf("%X", pubKey[:])
} }
// Equals check public key is equal
func (pubKey PubKeyECDSA) Equals(other crypto.PubKey) bool { func (pubKey PubKeyECDSA) Equals(other crypto.PubKey) bool {
if otherSecp, ok := other.(PubKeyECDSA); ok { if otherSecp, ok := other.(PubKeyECDSA); ok {
return bytes.Equal(pubKey[:], otherSecp[:]) return bytes.Equal(pubKey[:], otherSecp[:])
} else {
return false
} }
return false
} }
type ECDSASignature struct { type signatureECDSA struct {
R, S *big.Int R, S *big.Int
} }
// Signature // SignatureECDSA Signature
type SignatureECDSA []byte type SignatureECDSA []byte
// SignatureS signature struct
type SignatureS struct { type SignatureS struct {
crypto.Signature crypto.Signature
} }
// Bytes convert signature to bytes
func (sig SignatureECDSA) Bytes() []byte { func (sig SignatureECDSA) Bytes() []byte {
s := make([]byte, len(sig)) s := make([]byte, len(sig))
copy(s, sig[:]) copy(s, sig[:])
return s return s
} }
// IsZero check signature is zero
func (sig SignatureECDSA) IsZero() bool { return len(sig) == 0 } func (sig SignatureECDSA) IsZero() bool { return len(sig) == 0 }
// String convert signature to string
func (sig SignatureECDSA) String() string { func (sig SignatureECDSA) String() string {
fingerprint := make([]byte, len(sig[:])) fingerprint := make([]byte, len(sig[:]))
copy(fingerprint, sig[:]) copy(fingerprint, sig[:])
...@@ -192,6 +208,7 @@ func (sig SignatureECDSA) String() string { ...@@ -192,6 +208,7 @@ func (sig SignatureECDSA) String() string {
} }
// Equals check signature equals
func (sig SignatureECDSA) Equals(other crypto.Signature) bool { func (sig SignatureECDSA) Equals(other crypto.Signature) bool {
if otherEd, ok := other.(SignatureECDSA); ok { if otherEd, ok := other.(SignatureECDSA); ok {
return bytes.Equal(sig[:], otherEd[:]) return bytes.Equal(sig[:], otherEd[:])
...@@ -199,7 +216,10 @@ func (sig SignatureECDSA) Equals(other crypto.Signature) bool { ...@@ -199,7 +216,10 @@ func (sig SignatureECDSA) Equals(other crypto.Signature) bool {
return false return false
} }
// Name name
const Name = "auth_ecdsa" const Name = "auth_ecdsa"
// ID id
const ID = 257 const ID = 257
func init() { func init() {
......
...@@ -13,12 +13,14 @@ import ( ...@@ -13,12 +13,14 @@ import (
"math/big" "math/big"
) )
// MarshalECDSASignature marshal ECDSA signature
func MarshalECDSASignature(r, s *big.Int) ([]byte, error) { func MarshalECDSASignature(r, s *big.Int) ([]byte, error) {
return asn1.Marshal(ECDSASignature{r, s}) return asn1.Marshal(signatureECDSA{r, s})
} }
// UnmarshalECDSASignature unmarshal ECDSA signature
func UnmarshalECDSASignature(raw []byte) (*big.Int, *big.Int, error) { func UnmarshalECDSASignature(raw []byte) (*big.Int, *big.Int, error) {
sig := new(ECDSASignature) sig := new(signatureECDSA)
_, err := asn1.Unmarshal(raw, sig) _, err := asn1.Unmarshal(raw, sig)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("failed unmashalling signature [%s]", err) return nil, nil, fmt.Errorf("failed unmashalling signature [%s]", err)
...@@ -41,6 +43,7 @@ func UnmarshalECDSASignature(raw []byte) (*big.Int, *big.Int, error) { ...@@ -41,6 +43,7 @@ func UnmarshalECDSASignature(raw []byte) (*big.Int, *big.Int, error) {
return sig.R, sig.S, nil return sig.R, sig.S, nil
} }
// ToLowS convert to low int
func ToLowS(k *ecdsa.PublicKey, s *big.Int) *big.Int { func ToLowS(k *ecdsa.PublicKey, s *big.Int) *big.Int {
lowS := IsLowS(s) lowS := IsLowS(s)
if !lowS { if !lowS {
...@@ -52,6 +55,7 @@ func ToLowS(k *ecdsa.PublicKey, s *big.Int) *big.Int { ...@@ -52,6 +55,7 @@ func ToLowS(k *ecdsa.PublicKey, s *big.Int) *big.Int {
return s return s
} }
// IsLowS check is low int
func IsLowS(s *big.Int) bool { func IsLowS(s *big.Int) bool {
return s.Cmp(new(big.Int).Rsh(elliptic.P256().Params().N, 1)) != 1 return s.Cmp(new(big.Int).Rsh(elliptic.P256().Params().N, 1)) != 1
} }
...@@ -78,16 +82,18 @@ func parsePubKey(pubKeyStr []byte, curve elliptic.Curve) (key *ecdsa.PublicKey, ...@@ -78,16 +82,18 @@ func parsePubKey(pubKeyStr []byte, curve elliptic.Curve) (key *ecdsa.PublicKey,
return &pubkey, nil return &pubkey, nil
} }
// SerializePublicKey serialize public key
func SerializePublicKey(p *ecdsa.PublicKey) []byte { func SerializePublicKey(p *ecdsa.PublicKey) []byte {
b := make([]byte, 0, ECDSA_PUBLICKEY_LENGTH) b := make([]byte, 0, publicKeyECDSALength)
b = append(b, 0x4) b = append(b, 0x4)
b = paddedAppend(32, b, p.X.Bytes()) b = paddedAppend(32, b, p.X.Bytes())
return paddedAppend(32, b, p.Y.Bytes()) return paddedAppend(32, b, p.Y.Bytes())
} }
// SerializePrivateKey serialize private key
func SerializePrivateKey(p *ecdsa.PrivateKey) []byte { func SerializePrivateKey(p *ecdsa.PrivateKey) []byte {
b := make([]byte, 0, ECDSA_RPIVATEKEY_LENGTH) b := make([]byte, 0, privateKeyECDSALength)
return paddedAppend(ECDSA_RPIVATEKEY_LENGTH, b, p.D.Bytes()) return paddedAppend(privateKeyECDSALength, b, p.D.Bytes())
} }
func paddedAppend(size uint, dst, src []byte) []byte { func paddedAppend(size uint, dst, src []byte) []byte {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment