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.

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