github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/crypto/ed25519/ed25519.go (about)

     1  // Package ed25519 implements the Ed25519 signature algorithm. See
     2  // http://ed25519.cr.yp.to/.
     3  //
     4  // These functions are also compatible with the “Ed25519” function defined in
     5  // https://tools.ietf.org/html/draft-irtf-cfrg-eddsa-05.
     6  package ed25519
     7  
     8  // This code is a port of the public domain, “ref10” implementation of ed25519
     9  // from SUPERCOP.
    10  
    11  import (
    12  	cryptorand "crypto/rand"
    13  	"crypto/sha512"
    14  	"crypto/subtle"
    15  	"encoding/hex"
    16  	"io"
    17  	"strconv"
    18  
    19  	"github.com/bytom/bytom/crypto/ed25519/internal/edwards25519"
    20  )
    21  
    22  const (
    23  	// PublicKeySize is the size, in bytes, of public keys as used in this package.
    24  	PublicKeySize = 32
    25  	// PrivateKeySize is the size, in bytes, of private keys as used in this package.
    26  	PrivateKeySize = 64
    27  	// SignatureSize is the size, in bytes, of signatures generated and verified by this package.
    28  	SignatureSize = 64
    29  	// SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032.
    30  	SeedSize = 32
    31  )
    32  
    33  // PublicKey is the type of Ed25519 public keys.
    34  type PublicKey []byte
    35  
    36  // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
    37  type PrivateKey []byte
    38  
    39  // Public returns the PublicKey corresponding to priv.
    40  func (priv PrivateKey) Public() PublicKey {
    41  	publicKey := make([]byte, PublicKeySize)
    42  	copy(publicKey, priv[32:])
    43  	return PublicKey(publicKey)
    44  }
    45  
    46  // Seed returns the private key seed corresponding to priv. It is provided for
    47  // interoperability with RFC 8032. RFC 8032's private keys correspond to seeds
    48  // in this package.
    49  func (priv PrivateKey) Seed() []byte {
    50  	seed := make([]byte, SeedSize)
    51  	copy(seed, priv[:32])
    52  	return seed
    53  }
    54  
    55  func (priv PrivateKey) String() string {
    56  	return hex.EncodeToString(priv)
    57  }
    58  
    59  // GenerateKey generates a public/private key pair using entropy from rand.
    60  // If rand is nil, crypto/rand.Reader will be used.
    61  func GenerateKey(rand io.Reader) (publicKey PublicKey, privateKey PrivateKey, err error) {
    62  	if rand == nil {
    63  		rand = cryptorand.Reader
    64  	}
    65  
    66  	privateKey = make([]byte, PrivateKeySize)
    67  	publicKey = make([]byte, PublicKeySize)
    68  	_, err = io.ReadFull(rand, privateKey[:32])
    69  	if err != nil {
    70  		return nil, nil, err
    71  	}
    72  
    73  	digest := sha512.Sum512(privateKey[:32])
    74  	digest[0] &= 248
    75  	digest[31] &= 127
    76  	digest[31] |= 64
    77  
    78  	var A edwards25519.ExtendedGroupElement
    79  	var hBytes [32]byte
    80  	copy(hBytes[:], digest[:])
    81  	edwards25519.GeScalarMultBase(&A, &hBytes)
    82  	var publicKeyBytes [32]byte
    83  	A.ToBytes(&publicKeyBytes)
    84  
    85  	copy(privateKey[32:], publicKeyBytes[:])
    86  	copy(publicKey, publicKeyBytes[:])
    87  
    88  	return publicKey, privateKey, nil
    89  }
    90  
    91  // NewKeyFromSeed calculates a private key from a seed. It will panic if
    92  // len(seed) is not SeedSize. This function is provided for interoperability
    93  // with RFC 8032. RFC 8032's private keys correspond to seeds in this
    94  // package.
    95  func NewKeyFromSeed(seed []byte) PrivateKey {
    96  	if l := len(seed); l != SeedSize {
    97  		panic("ed25519: bad seed length: " + strconv.Itoa(l))
    98  	}
    99  
   100  	digest := sha512.Sum512(seed)
   101  	digest[0] &= 248
   102  	digest[31] &= 127
   103  	digest[31] |= 64
   104  
   105  	var A edwards25519.ExtendedGroupElement
   106  	var hBytes [32]byte
   107  	copy(hBytes[:], digest[:])
   108  	edwards25519.GeScalarMultBase(&A, &hBytes)
   109  	var publicKeyBytes [32]byte
   110  	A.ToBytes(&publicKeyBytes)
   111  
   112  	privateKey := make([]byte, PrivateKeySize)
   113  	copy(privateKey, seed)
   114  	copy(privateKey[32:], publicKeyBytes[:])
   115  
   116  	return privateKey
   117  }
   118  
   119  // Sign signs the message with privateKey and returns a signature. It will
   120  // panic if len(privateKey) is not PrivateKeySize.
   121  func Sign(privateKey PrivateKey, message []byte) []byte {
   122  	if l := len(privateKey); l != PrivateKeySize {
   123  		panic("ed25519: bad private key length: " + strconv.Itoa(l))
   124  	}
   125  
   126  	h := sha512.New()
   127  	h.Write(privateKey[:32])
   128  
   129  	var digest1, messageDigest, hramDigest [64]byte
   130  	var expandedSecretKey [32]byte
   131  	h.Sum(digest1[:0])
   132  	copy(expandedSecretKey[:], digest1[:])
   133  	expandedSecretKey[0] &= 248
   134  	expandedSecretKey[31] &= 63
   135  	expandedSecretKey[31] |= 64
   136  
   137  	h.Reset()
   138  	h.Write(digest1[32:])
   139  	h.Write(message)
   140  	h.Sum(messageDigest[:0])
   141  
   142  	var messageDigestReduced [32]byte
   143  	edwards25519.ScReduce(&messageDigestReduced, &messageDigest)
   144  	var R edwards25519.ExtendedGroupElement
   145  	edwards25519.GeScalarMultBase(&R, &messageDigestReduced)
   146  
   147  	var encodedR [32]byte
   148  	R.ToBytes(&encodedR)
   149  
   150  	h.Reset()
   151  	h.Write(encodedR[:])
   152  	h.Write(privateKey[32:])
   153  	h.Write(message)
   154  	h.Sum(hramDigest[:0])
   155  	var hramDigestReduced [32]byte
   156  	edwards25519.ScReduce(&hramDigestReduced, &hramDigest)
   157  
   158  	var s [32]byte
   159  	edwards25519.ScMulAdd(&s, &hramDigestReduced, &expandedSecretKey, &messageDigestReduced)
   160  
   161  	signature := make([]byte, SignatureSize)
   162  	copy(signature[:], encodedR[:])
   163  	copy(signature[32:], s[:])
   164  
   165  	return signature
   166  }
   167  
   168  // Verify reports whether sig is a valid signature of message by publicKey. It
   169  // will panic if len(publicKey) is not PublicKeySize.
   170  func Verify(publicKey PublicKey, message, sig []byte) bool {
   171  	if l := len(publicKey); l != PublicKeySize {
   172  		panic("ed25519: bad public key length: " + strconv.Itoa(l))
   173  	}
   174  
   175  	if len(sig) != SignatureSize || sig[63]&224 != 0 {
   176  		return false
   177  	}
   178  
   179  	var A edwards25519.ExtendedGroupElement
   180  	var publicKeyBytes [32]byte
   181  	copy(publicKeyBytes[:], publicKey)
   182  	if !A.FromBytes(&publicKeyBytes) {
   183  		return false
   184  	}
   185  	edwards25519.FeNeg(&A.X, &A.X)
   186  	edwards25519.FeNeg(&A.T, &A.T)
   187  
   188  	h := sha512.New()
   189  	h.Write(sig[:32])
   190  	h.Write(publicKey[:])
   191  	h.Write(message)
   192  	var digest [64]byte
   193  	h.Sum(digest[:0])
   194  
   195  	var hReduced [32]byte
   196  	edwards25519.ScReduce(&hReduced, &digest)
   197  
   198  	var R edwards25519.ProjectiveGroupElement
   199  	var b [32]byte
   200  	copy(b[:], sig[32:])
   201  	edwards25519.GeDoubleScalarMultVartime(&R, &hReduced, &A, &b)
   202  
   203  	var checkR [32]byte
   204  	R.ToBytes(&checkR)
   205  	return subtle.ConstantTimeCompare(sig[:32], checkR[:]) == 1
   206  }