本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.4KB

  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // In Go 1.13, the ed25519 package was promoted to the standard library as
  5. // crypto/ed25519, and this package became a wrapper for the standard library one.
  6. //
  7. // +build !go1.13
  8. // Package ed25519 implements the Ed25519 signature algorithm. See
  9. // https://ed25519.cr.yp.to/.
  10. //
  11. // These functions are also compatible with the “Ed25519” function defined in
  12. // RFC 8032. However, unlike RFC 8032's formulation, this package's private key
  13. // representation includes a public key suffix to make multiple signing
  14. // operations with the same key more efficient. This package refers to the RFC
  15. // 8032 private key as the “seed”.
  16. package ed25519
  17. // This code is a port of the public domain, “ref10” implementation of ed25519
  18. // from SUPERCOP.
  19. import (
  20. "bytes"
  21. "crypto"
  22. cryptorand "crypto/rand"
  23. "crypto/sha512"
  24. "errors"
  25. "io"
  26. "strconv"
  27. "golang.org/x/crypto/ed25519/internal/edwards25519"
  28. )
  29. const (
  30. // PublicKeySize is the size, in bytes, of public keys as used in this package.
  31. PublicKeySize = 32
  32. // PrivateKeySize is the size, in bytes, of private keys as used in this package.
  33. PrivateKeySize = 64
  34. // SignatureSize is the size, in bytes, of signatures generated and verified by this package.
  35. SignatureSize = 64
  36. // SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032.
  37. SeedSize = 32
  38. )
  39. // PublicKey is the type of Ed25519 public keys.
  40. type PublicKey []byte
  41. // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
  42. type PrivateKey []byte
  43. // Public returns the PublicKey corresponding to priv.
  44. func (priv PrivateKey) Public() crypto.PublicKey {
  45. publicKey := make([]byte, PublicKeySize)
  46. copy(publicKey, priv[32:])
  47. return PublicKey(publicKey)
  48. }
  49. // Seed returns the private key seed corresponding to priv. It is provided for
  50. // interoperability with RFC 8032. RFC 8032's private keys correspond to seeds
  51. // in this package.
  52. func (priv PrivateKey) Seed() []byte {
  53. seed := make([]byte, SeedSize)
  54. copy(seed, priv[:32])
  55. return seed
  56. }
  57. // Sign signs the given message with priv.
  58. // Ed25519 performs two passes over messages to be signed and therefore cannot
  59. // handle pre-hashed messages. Thus opts.HashFunc() must return zero to
  60. // indicate the message hasn't been hashed. This can be achieved by passing
  61. // crypto.Hash(0) as the value for opts.
  62. func (priv PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error) {
  63. if opts.HashFunc() != crypto.Hash(0) {
  64. return nil, errors.New("ed25519: cannot sign hashed message")
  65. }
  66. return Sign(priv, message), nil
  67. }
  68. // GenerateKey generates a public/private key pair using entropy from rand.
  69. // If rand is nil, crypto/rand.Reader will be used.
  70. func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) {
  71. if rand == nil {
  72. rand = cryptorand.Reader
  73. }
  74. seed := make([]byte, SeedSize)
  75. if _, err := io.ReadFull(rand, seed); err != nil {
  76. return nil, nil, err
  77. }
  78. privateKey := NewKeyFromSeed(seed)
  79. publicKey := make([]byte, PublicKeySize)
  80. copy(publicKey, privateKey[32:])
  81. return publicKey, privateKey, nil
  82. }
  83. // NewKeyFromSeed calculates a private key from a seed. It will panic if
  84. // len(seed) is not SeedSize. This function is provided for interoperability
  85. // with RFC 8032. RFC 8032's private keys correspond to seeds in this
  86. // package.
  87. func NewKeyFromSeed(seed []byte) PrivateKey {
  88. if l := len(seed); l != SeedSize {
  89. panic("ed25519: bad seed length: " + strconv.Itoa(l))
  90. }
  91. digest := sha512.Sum512(seed)
  92. digest[0] &= 248
  93. digest[31] &= 127
  94. digest[31] |= 64
  95. var A edwards25519.ExtendedGroupElement
  96. var hBytes [32]byte
  97. copy(hBytes[:], digest[:])
  98. edwards25519.GeScalarMultBase(&A, &hBytes)
  99. var publicKeyBytes [32]byte
  100. A.ToBytes(&publicKeyBytes)
  101. privateKey := make([]byte, PrivateKeySize)
  102. copy(privateKey, seed)
  103. copy(privateKey[32:], publicKeyBytes[:])
  104. return privateKey
  105. }
  106. // Sign signs the message with privateKey and returns a signature. It will
  107. // panic if len(privateKey) is not PrivateKeySize.
  108. func Sign(privateKey PrivateKey, message []byte) []byte {
  109. if l := len(privateKey); l != PrivateKeySize {
  110. panic("ed25519: bad private key length: " + strconv.Itoa(l))
  111. }
  112. h := sha512.New()
  113. h.Write(privateKey[:32])
  114. var digest1, messageDigest, hramDigest [64]byte
  115. var expandedSecretKey [32]byte
  116. h.Sum(digest1[:0])
  117. copy(expandedSecretKey[:], digest1[:])
  118. expandedSecretKey[0] &= 248
  119. expandedSecretKey[31] &= 63
  120. expandedSecretKey[31] |= 64
  121. h.Reset()
  122. h.Write(digest1[32:])
  123. h.Write(message)
  124. h.Sum(messageDigest[:0])
  125. var messageDigestReduced [32]byte
  126. edwards25519.ScReduce(&messageDigestReduced, &messageDigest)
  127. var R edwards25519.ExtendedGroupElement
  128. edwards25519.GeScalarMultBase(&R, &messageDigestReduced)
  129. var encodedR [32]byte
  130. R.ToBytes(&encodedR)
  131. h.Reset()
  132. h.Write(encodedR[:])
  133. h.Write(privateKey[32:])
  134. h.Write(message)
  135. h.Sum(hramDigest[:0])
  136. var hramDigestReduced [32]byte
  137. edwards25519.ScReduce(&hramDigestReduced, &hramDigest)
  138. var s [32]byte
  139. edwards25519.ScMulAdd(&s, &hramDigestReduced, &expandedSecretKey, &messageDigestReduced)
  140. signature := make([]byte, SignatureSize)
  141. copy(signature[:], encodedR[:])
  142. copy(signature[32:], s[:])
  143. return signature
  144. }
  145. // Verify reports whether sig is a valid signature of message by publicKey. It
  146. // will panic if len(publicKey) is not PublicKeySize.
  147. func Verify(publicKey PublicKey, message, sig []byte) bool {
  148. if l := len(publicKey); l != PublicKeySize {
  149. panic("ed25519: bad public key length: " + strconv.Itoa(l))
  150. }
  151. if len(sig) != SignatureSize || sig[63]&224 != 0 {
  152. return false
  153. }
  154. var A edwards25519.ExtendedGroupElement
  155. var publicKeyBytes [32]byte
  156. copy(publicKeyBytes[:], publicKey)
  157. if !A.FromBytes(&publicKeyBytes) {
  158. return false
  159. }
  160. edwards25519.FeNeg(&A.X, &A.X)
  161. edwards25519.FeNeg(&A.T, &A.T)
  162. h := sha512.New()
  163. h.Write(sig[:32])
  164. h.Write(publicKey[:])
  165. h.Write(message)
  166. var digest [64]byte
  167. h.Sum(digest[:0])
  168. var hReduced [32]byte
  169. edwards25519.ScReduce(&hReduced, &digest)
  170. var R edwards25519.ProjectiveGroupElement
  171. var s [32]byte
  172. copy(s[:], sig[32:])
  173. // https://tools.ietf.org/html/rfc8032#section-5.1.7 requires that s be in
  174. // the range [0, order) in order to prevent signature malleability.
  175. if !edwards25519.ScMinimal(&s) {
  176. return false
  177. }
  178. edwards25519.GeDoubleScalarMultVartime(&R, &hReduced, &A, &s)
  179. var checkR [32]byte
  180. R.ToBytes(&checkR)
  181. return bytes.Equal(sig[:32], checkR[:])
  182. }
上海开阖软件有限公司 沪ICP备12045867号-1