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.

227 lines
6.8 KiB

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