signature_nocgo.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 !cgo
  17. package crypto
  18. import (
  19. "crypto/ecdsa"
  20. "crypto/elliptic"
  21. "errors"
  22. "fmt"
  23. "math/big"
  24. "github.com/btcsuite/btcd/btcec"
  25. )
  26. // Ecrecover returns the uncompressed public key that created the given signature.
  27. func Ecrecover(hash, sig []byte) ([]byte, error) {
  28. pub, err := SigToPub(hash, sig)
  29. if err != nil {
  30. return nil, err
  31. }
  32. bytes := (*btcec.PublicKey)(pub).SerializeUncompressed()
  33. return bytes, err
  34. }
  35. // SigToPub returns the public key that created the given signature.
  36. func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
  37. // Convert to btcec input format with 'recovery id' v at the beginning.
  38. btcsig := make([]byte, SignatureLength)
  39. btcsig[0] = sig[64] + 27
  40. copy(btcsig[1:], sig)
  41. pub, _, err := btcec.RecoverCompact(btcec.S256(), btcsig, hash)
  42. return (*ecdsa.PublicKey)(pub), err
  43. }
  44. // Sign calculates an ECDSA signature.
  45. //
  46. // This function is susceptible to chosen plaintext attacks that can leak
  47. // information about the private key that is used for signing. Callers must
  48. // be aware that the given hash cannot be chosen by an adversery. Common
  49. // solution is to hash any input before calculating the signature.
  50. //
  51. // The produced signature is in the [R || S || V] format where V is 0 or 1.
  52. func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error) {
  53. if len(hash) != 32 {
  54. return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
  55. }
  56. if prv.Curve != btcec.S256() {
  57. return nil, fmt.Errorf("private key curve is not secp256k1")
  58. }
  59. sig, err := btcec.SignCompact(btcec.S256(), (*btcec.PrivateKey)(prv), hash, false)
  60. if err != nil {
  61. return nil, err
  62. }
  63. // Convert to Ethereum signature format with 'recovery id' v at the end.
  64. v := sig[0] - 27
  65. copy(sig, sig[1:])
  66. sig[64] = v
  67. return sig, nil
  68. }
  69. // VerifySignature checks that the given public key created signature over hash.
  70. // The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
  71. // The signature should have the 64 byte [R || S] format.
  72. func VerifySignature(pubkey, hash, signature []byte) bool {
  73. if len(signature) != 64 {
  74. return false
  75. }
  76. sig := &btcec.Signature{R: new(big.Int).SetBytes(signature[:32]), S: new(big.Int).SetBytes(signature[32:])}
  77. key, err := btcec.ParsePubKey(pubkey, btcec.S256())
  78. if err != nil {
  79. return false
  80. }
  81. // Reject malleable signatures. libsecp256k1 does this check but btcec doesn't.
  82. if sig.S.Cmp(secp256k1halfN) > 0 {
  83. return false
  84. }
  85. return sig.Verify(hash, key)
  86. }
  87. // DecompressPubkey parses a public key in the 33-byte compressed format.
  88. func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
  89. if len(pubkey) != 33 {
  90. return nil, errors.New("invalid compressed public key length")
  91. }
  92. key, err := btcec.ParsePubKey(pubkey, btcec.S256())
  93. if err != nil {
  94. return nil, err
  95. }
  96. return key.ToECDSA(), nil
  97. }
  98. // CompressPubkey encodes a public key to the 33-byte compressed format.
  99. func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
  100. return (*btcec.PublicKey)(pubkey).SerializeCompressed()
  101. }
  102. // S256 returns an instance of the secp256k1 curve.
  103. func S256() elliptic.Curve {
  104. return btcec.S256()
  105. }