github.com/klaytn/klaytn@v1.12.1/crypto/signature_cgo.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2017 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from crypto/signature_cgo.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 //go:build !nacl && !js && !nocgo 22 // +build !nacl,!js,!nocgo 23 24 package crypto 25 26 import ( 27 "crypto/ecdsa" 28 "crypto/elliptic" 29 "fmt" 30 31 "github.com/klaytn/klaytn/common/math" 32 "github.com/klaytn/klaytn/crypto/secp256k1" 33 ) 34 35 // Ecrecover returns the uncompressed public key that created the given signature. 36 func Ecrecover(hash, sig []byte) ([]byte, error) { 37 return secp256k1.RecoverPubkey(hash, sig) 38 } 39 40 // SigToPub returns the public key that created the given signature. 41 func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) { 42 s, err := Ecrecover(hash, sig) 43 if err != nil { 44 return nil, err 45 } 46 47 x, y := elliptic.Unmarshal(S256(), s) 48 return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y}, nil 49 } 50 51 // Sign calculates an ECDSA signature. 52 // 53 // This function is susceptible to chosen plaintext attacks that can leak 54 // information about the private key that is used for signing. Callers must 55 // be aware that the given digest cannot be chosen by an adversery. Common 56 // solution is to hash any input before calculating the signature. 57 // 58 // The produced signature is in the [R || S || V] format where V is 0 or 1. 59 func Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) { 60 if len(digestHash) != DigestLength { 61 return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", DigestLength, len(digestHash)) 62 } 63 seckey := math.PaddedBigBytes(prv.D, prv.Params().BitSize/8) 64 defer zeroBytes(seckey) 65 return secp256k1.Sign(digestHash, seckey) 66 } 67 68 // VerifySignature checks that the given public key created signature over digest. 69 // The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format. 70 // The signature should have the 64 byte [R || S] format. 71 func VerifySignature(pubkey, digestHash, signature []byte) bool { 72 return secp256k1.VerifySignature(pubkey, digestHash, signature) 73 } 74 75 // DecompressPubkey parses a public key in the 33-byte compressed format. 76 func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) { 77 x, y := secp256k1.DecompressPubkey(pubkey) 78 if x == nil { 79 return nil, fmt.Errorf("invalid public key") 80 } 81 return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil 82 } 83 84 // CompressPubkey encodes a public key to the 33-byte compressed format. 85 func CompressPubkey(pubkey *ecdsa.PublicKey) []byte { 86 return secp256k1.CompressPubkey(pubkey.X, pubkey.Y) 87 } 88 89 // S256 returns an instance of the secp256k1 curve. 90 func S256() elliptic.Curve { 91 return secp256k1.S256() 92 }