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.

196 lines
6.0 KiB

  1. package ed25519
  2. import (
  3. "bytes"
  4. "crypto/subtle"
  5. "fmt"
  6. "io"
  7. "github.com/tendermint/ed25519"
  8. "github.com/tendermint/ed25519/extra25519"
  9. amino "github.com/tendermint/go-amino"
  10. "github.com/tendermint/tendermint/crypto"
  11. "github.com/tendermint/tendermint/crypto/tmhash"
  12. )
  13. //-------------------------------------
  14. var _ crypto.PrivKey = PrivKeyEd25519{}
  15. const (
  16. Ed25519PrivKeyAminoRoute = "tendermint/PrivKeyEd25519"
  17. Ed25519PubKeyAminoRoute = "tendermint/PubKeyEd25519"
  18. // Size of an Edwards25519 signature. Namely the size of a compressed
  19. // Edwards25519 point, and a field element. Both of which are 32 bytes.
  20. SignatureEd25519Size = 64
  21. )
  22. var cdc = amino.NewCodec()
  23. func init() {
  24. cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
  25. cdc.RegisterConcrete(PubKeyEd25519{},
  26. Ed25519PubKeyAminoRoute, nil)
  27. cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
  28. cdc.RegisterConcrete(PrivKeyEd25519{},
  29. Ed25519PrivKeyAminoRoute, nil)
  30. }
  31. // PrivKeyEd25519 implements crypto.PrivKey.
  32. type PrivKeyEd25519 [64]byte
  33. // Bytes marshals the privkey using amino encoding.
  34. func (privKey PrivKeyEd25519) Bytes() []byte {
  35. return cdc.MustMarshalBinaryBare(privKey)
  36. }
  37. // Sign produces a signature on the provided message.
  38. func (privKey PrivKeyEd25519) Sign(msg []byte) ([]byte, error) {
  39. privKeyBytes := [64]byte(privKey)
  40. signatureBytes := ed25519.Sign(&privKeyBytes, msg)
  41. return signatureBytes[:], nil
  42. }
  43. // PubKey gets the corresponding public key from the private key.
  44. func (privKey PrivKeyEd25519) PubKey() crypto.PubKey {
  45. privKeyBytes := [64]byte(privKey)
  46. initialized := false
  47. // If the latter 32 bytes of the privkey are all zero, compute the pubkey
  48. // otherwise privkey is initialized and we can use the cached value inside
  49. // of the private key.
  50. for _, v := range privKeyBytes[32:] {
  51. if v != 0 {
  52. initialized = true
  53. break
  54. }
  55. }
  56. if initialized {
  57. var pubkeyBytes [PubKeyEd25519Size]byte
  58. copy(pubkeyBytes[:], privKeyBytes[32:])
  59. return PubKeyEd25519(pubkeyBytes)
  60. }
  61. pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
  62. return PubKeyEd25519(pubBytes)
  63. }
  64. // Equals - you probably don't need to use this.
  65. // Runs in constant time based on length of the keys.
  66. func (privKey PrivKeyEd25519) Equals(other crypto.PrivKey) bool {
  67. if otherEd, ok := other.(PrivKeyEd25519); ok {
  68. return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1
  69. } else {
  70. return false
  71. }
  72. }
  73. // ToCurve25519 takes a private key and returns its representation on
  74. // Curve25519. Curve25519 is birationally equivalent to Edwards25519,
  75. // which Ed25519 uses internally. This method is intended for use in
  76. // an X25519 Diffie Hellman key exchange.
  77. func (privKey PrivKeyEd25519) ToCurve25519() *[PubKeyEd25519Size]byte {
  78. keyCurve25519 := new([32]byte)
  79. privKeyBytes := [64]byte(privKey)
  80. extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes)
  81. return keyCurve25519
  82. }
  83. // GenPrivKey generates a new ed25519 private key.
  84. // It uses OS randomness in conjunction with the current global random seed
  85. // in tendermint/libs/common to generate the private key.
  86. func GenPrivKey() PrivKeyEd25519 {
  87. return genPrivKey(crypto.CReader())
  88. }
  89. // genPrivKey generates a new ed25519 private key using the provided reader.
  90. func genPrivKey(rand io.Reader) PrivKeyEd25519 {
  91. privKey := new([64]byte)
  92. _, err := io.ReadFull(rand, privKey[:32])
  93. if err != nil {
  94. panic(err)
  95. }
  96. // ed25519.MakePublicKey(privKey) alters the last 32 bytes of privKey.
  97. // It places the pubkey in the last 32 bytes of privKey, and returns the
  98. // public key.
  99. ed25519.MakePublicKey(privKey)
  100. return PrivKeyEd25519(*privKey)
  101. }
  102. // GenPrivKeyFromSecret hashes the secret with SHA2, and uses
  103. // that 32 byte output to create the private key.
  104. // NOTE: secret should be the output of a KDF like bcrypt,
  105. // if it's derived from user input.
  106. func GenPrivKeyFromSecret(secret []byte) PrivKeyEd25519 {
  107. privKey32 := crypto.Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  108. privKey := new([64]byte)
  109. copy(privKey[:32], privKey32)
  110. // ed25519.MakePublicKey(privKey) alters the last 32 bytes of privKey.
  111. // It places the pubkey in the last 32 bytes of privKey, and returns the
  112. // public key.
  113. ed25519.MakePublicKey(privKey)
  114. return PrivKeyEd25519(*privKey)
  115. }
  116. //-------------------------------------
  117. var _ crypto.PubKey = PubKeyEd25519{}
  118. // PubKeyEd25519Size is the number of bytes in an Ed25519 signature.
  119. const PubKeyEd25519Size = 32
  120. // PubKeyEd25519 implements crypto.PubKey for the Ed25519 signature scheme.
  121. type PubKeyEd25519 [PubKeyEd25519Size]byte
  122. // Address is the SHA256-20 of the raw pubkey bytes.
  123. func (pubKey PubKeyEd25519) Address() crypto.Address {
  124. return crypto.Address(tmhash.Sum(pubKey[:]))
  125. }
  126. // Bytes marshals the PubKey using amino encoding.
  127. func (pubKey PubKeyEd25519) Bytes() []byte {
  128. bz, err := cdc.MarshalBinaryBare(pubKey)
  129. if err != nil {
  130. panic(err)
  131. }
  132. return bz
  133. }
  134. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ []byte) bool {
  135. // make sure we use the same algorithm to sign
  136. if len(sig_) != SignatureEd25519Size {
  137. return false
  138. }
  139. sig := new([SignatureEd25519Size]byte)
  140. copy(sig[:], sig_)
  141. pubKeyBytes := [PubKeyEd25519Size]byte(pubKey)
  142. return ed25519.Verify(&pubKeyBytes, msg, sig)
  143. }
  144. // ToCurve25519 takes a public key and returns its representation on
  145. // Curve25519. Curve25519 is birationally equivalent to Edwards25519,
  146. // which Ed25519 uses internally. This method is intended for use in
  147. // an X25519 Diffie Hellman key exchange.
  148. //
  149. // If there is an error, then this function returns nil.
  150. func (pubKey PubKeyEd25519) ToCurve25519() *[PubKeyEd25519Size]byte {
  151. keyCurve25519, pubKeyBytes := new([PubKeyEd25519Size]byte), [PubKeyEd25519Size]byte(pubKey)
  152. ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
  153. if !ok {
  154. return nil
  155. }
  156. return keyCurve25519
  157. }
  158. func (pubKey PubKeyEd25519) String() string {
  159. return fmt.Sprintf("PubKeyEd25519{%X}", pubKey[:])
  160. }
  161. // nolint: golint
  162. func (pubKey PubKeyEd25519) Equals(other crypto.PubKey) bool {
  163. if otherEd, ok := other.(PubKeyEd25519); ok {
  164. return bytes.Equal(pubKey[:], otherEd[:])
  165. } else {
  166. return false
  167. }
  168. }