github.com/mprishchepo/go-ethereum@v1.9.7-0.20191031044858-21506be82b68/crypto/signature_nocgo.go (about)

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