signature_cgo.go 3.1 KB

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