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.

211 lines
5.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package crypto
  2. import (
  3. "bytes"
  4. secp256k1 "github.com/btcsuite/btcd/btcec"
  5. "github.com/tendermint/ed25519"
  6. "github.com/tendermint/ed25519/extra25519"
  7. "github.com/tendermint/go-wire"
  8. data "github.com/tendermint/go-wire/data"
  9. . "github.com/tendermint/tmlibs/common"
  10. )
  11. func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
  12. err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
  13. if err == nil {
  14. // add support for a ValidateKey method on PrivKeys
  15. // to make sure they load correctly
  16. val, ok := privKey.Unwrap().(validatable)
  17. if ok {
  18. err = val.ValidateKey()
  19. }
  20. }
  21. return
  22. }
  23. // validatable is an optional interface for keys that want to
  24. // check integrity
  25. type validatable interface {
  26. ValidateKey() error
  27. }
  28. //----------------------------------------
  29. // DO NOT USE THIS INTERFACE.
  30. // You probably want to use PrivKey
  31. // +gen wrapper:"PrivKey,Impl[PrivKeyEd25519,PrivKeySecp256k1],ed25519,secp256k1"
  32. type PrivKeyInner interface {
  33. AssertIsPrivKeyInner()
  34. Bytes() []byte
  35. Sign(msg []byte) Signature
  36. PubKey() PubKey
  37. Equals(PrivKey) bool
  38. Wrap() PrivKey
  39. }
  40. //-------------------------------------
  41. var _ PrivKeyInner = PrivKeyEd25519{}
  42. // Implements PrivKey
  43. type PrivKeyEd25519 [64]byte
  44. func (privKey PrivKeyEd25519) AssertIsPrivKeyInner() {}
  45. func (privKey PrivKeyEd25519) Bytes() []byte {
  46. return wire.BinaryBytes(PrivKey{privKey})
  47. }
  48. func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
  49. privKeyBytes := [64]byte(privKey)
  50. signatureBytes := ed25519.Sign(&privKeyBytes, msg)
  51. return SignatureEd25519(*signatureBytes).Wrap()
  52. }
  53. func (privKey PrivKeyEd25519) PubKey() PubKey {
  54. privKeyBytes := [64]byte(privKey)
  55. pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
  56. return PubKeyEd25519(pubBytes).Wrap()
  57. }
  58. func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
  59. if otherEd, ok := other.Unwrap().(PrivKeyEd25519); ok {
  60. return bytes.Equal(privKey[:], otherEd[:])
  61. } else {
  62. return false
  63. }
  64. }
  65. func (p PrivKeyEd25519) MarshalJSON() ([]byte, error) {
  66. return data.Encoder.Marshal(p[:])
  67. }
  68. func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error {
  69. var ref []byte
  70. err := data.Encoder.Unmarshal(&ref, enc)
  71. copy(p[:], ref)
  72. return err
  73. }
  74. func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
  75. keyCurve25519 := new([32]byte)
  76. privKeyBytes := [64]byte(privKey)
  77. extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes)
  78. return keyCurve25519
  79. }
  80. func (privKey PrivKeyEd25519) String() string {
  81. return Fmt("PrivKeyEd25519{*****}")
  82. }
  83. // Deterministically generates new priv-key bytes from key.
  84. func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
  85. newBytes := wire.BinarySha256(struct {
  86. PrivKey [64]byte
  87. Index int
  88. }{privKey, index})
  89. var newKey [64]byte
  90. copy(newKey[:], newBytes)
  91. return PrivKeyEd25519(newKey)
  92. }
  93. func GenPrivKeyEd25519() PrivKeyEd25519 {
  94. privKeyBytes := new([64]byte)
  95. copy(privKeyBytes[:32], CRandBytes(32))
  96. ed25519.MakePublicKey(privKeyBytes)
  97. return PrivKeyEd25519(*privKeyBytes)
  98. }
  99. // NOTE: secret should be the output of a KDF like bcrypt,
  100. // if it's derived from user input.
  101. func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
  102. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  103. privKeyBytes := new([64]byte)
  104. copy(privKeyBytes[:32], privKey32)
  105. ed25519.MakePublicKey(privKeyBytes)
  106. return PrivKeyEd25519(*privKeyBytes)
  107. }
  108. //-------------------------------------
  109. var _ PrivKeyInner = PrivKeySecp256k1{}
  110. // Implements PrivKey
  111. type PrivKeySecp256k1 [32]byte
  112. func (privKey PrivKeySecp256k1) AssertIsPrivKeyInner() {}
  113. func (privKey PrivKeySecp256k1) Bytes() []byte {
  114. return wire.BinaryBytes(PrivKey{privKey})
  115. }
  116. func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
  117. priv__, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  118. sig__, err := priv__.Sign(Sha256(msg))
  119. if err != nil {
  120. PanicSanity(err)
  121. }
  122. return SignatureSecp256k1(sig__.Serialize()).Wrap()
  123. }
  124. func (privKey PrivKeySecp256k1) PubKey() PubKey {
  125. _, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  126. var pub PubKeySecp256k1
  127. copy(pub[:], pub__.SerializeCompressed())
  128. return pub.Wrap()
  129. }
  130. func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
  131. if otherSecp, ok := other.Unwrap().(PrivKeySecp256k1); ok {
  132. return bytes.Equal(privKey[:], otherSecp[:])
  133. } else {
  134. return false
  135. }
  136. }
  137. func (p PrivKeySecp256k1) MarshalJSON() ([]byte, error) {
  138. return data.Encoder.Marshal(p[:])
  139. }
  140. func (p *PrivKeySecp256k1) UnmarshalJSON(enc []byte) error {
  141. var ref []byte
  142. err := data.Encoder.Unmarshal(&ref, enc)
  143. copy(p[:], ref)
  144. return err
  145. }
  146. func (privKey PrivKeySecp256k1) String() string {
  147. return Fmt("PrivKeySecp256k1{*****}")
  148. }
  149. /*
  150. // Deterministically generates new priv-key bytes from key.
  151. func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
  152. newBytes := wire.BinarySha256(struct {
  153. PrivKey [64]byte
  154. Index int
  155. }{key, index})
  156. var newKey [64]byte
  157. copy(newKey[:], newBytes)
  158. return PrivKeySecp256k1(newKey)
  159. }
  160. */
  161. func GenPrivKeySecp256k1() PrivKeySecp256k1 {
  162. privKeyBytes := [32]byte{}
  163. copy(privKeyBytes[:], CRandBytes(32))
  164. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:])
  165. copy(privKeyBytes[:], priv.Serialize())
  166. return PrivKeySecp256k1(privKeyBytes)
  167. }
  168. // NOTE: secret should be the output of a KDF like bcrypt,
  169. // if it's derived from user input.
  170. func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 {
  171. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  172. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32)
  173. privKeyBytes := [32]byte{}
  174. copy(privKeyBytes[:], priv.Serialize())
  175. return PrivKeySecp256k1(privKeyBytes)
  176. }