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.

220 lines
5.6 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
  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-common"
  8. data "github.com/tendermint/go-data"
  9. "github.com/tendermint/go-wire"
  10. )
  11. // PrivKey is part of PrivAccount and state.PrivValidator.
  12. type PrivKey interface {
  13. Bytes() []byte
  14. Sign(msg []byte) Signature
  15. PubKey() PubKey
  16. Equals(PrivKey) bool
  17. }
  18. // Types of implementations
  19. const (
  20. TypeEd25519 = byte(0x01)
  21. TypeSecp256k1 = byte(0x02)
  22. NameEd25519 = "ed25519"
  23. NameSecp256k1 = "secp256k1"
  24. )
  25. var privKeyMapper data.Mapper
  26. // register both private key types with go-data (and thus go-wire)
  27. func init() {
  28. privKeyMapper = data.NewMapper(PrivKeyS{}).
  29. RegisterInterface(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
  30. RegisterInterface(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
  31. }
  32. // PrivKeyS add json serialization to PrivKey
  33. type PrivKeyS struct {
  34. PrivKey
  35. }
  36. func (p PrivKeyS) MarshalJSON() ([]byte, error) {
  37. return privKeyMapper.ToJSON(p.PrivKey)
  38. }
  39. func (p *PrivKeyS) UnmarshalJSON(data []byte) (err error) {
  40. parsed, err := privKeyMapper.FromJSON(data)
  41. if err == nil && parsed != nil {
  42. p.PrivKey = parsed.(PrivKey)
  43. }
  44. return
  45. }
  46. func (p PrivKeyS) Empty() bool {
  47. return p.PrivKey == nil
  48. }
  49. func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
  50. err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
  51. return
  52. }
  53. //-------------------------------------
  54. // Implements PrivKey
  55. type PrivKeyEd25519 [64]byte
  56. func (privKey PrivKeyEd25519) Bytes() []byte {
  57. return wire.BinaryBytes(struct{ PrivKey }{privKey})
  58. }
  59. func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
  60. privKeyBytes := [64]byte(privKey)
  61. signatureBytes := ed25519.Sign(&privKeyBytes, msg)
  62. return SignatureEd25519(*signatureBytes)
  63. }
  64. func (privKey PrivKeyEd25519) PubKey() PubKey {
  65. privKeyBytes := [64]byte(privKey)
  66. return PubKeyEd25519(*ed25519.MakePublicKey(&privKeyBytes))
  67. }
  68. func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
  69. if otherEd, ok := other.(PrivKeyEd25519); ok {
  70. return bytes.Equal(privKey[:], otherEd[:])
  71. } else {
  72. return false
  73. }
  74. }
  75. func (p PrivKeyEd25519) MarshalJSON() ([]byte, error) {
  76. return data.Encoder.Marshal(p[:])
  77. }
  78. func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error {
  79. var ref []byte
  80. err := data.Encoder.Unmarshal(&ref, enc)
  81. copy(p[:], ref)
  82. return err
  83. }
  84. func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
  85. keyCurve25519 := new([32]byte)
  86. privKeyBytes := [64]byte(privKey)
  87. extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes)
  88. return keyCurve25519
  89. }
  90. func (privKey PrivKeyEd25519) String() string {
  91. return Fmt("PrivKeyEd25519{*****}")
  92. }
  93. // Deterministically generates new priv-key bytes from key.
  94. func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
  95. newBytes := wire.BinarySha256(struct {
  96. PrivKey [64]byte
  97. Index int
  98. }{privKey, index})
  99. var newKey [64]byte
  100. copy(newKey[:], newBytes)
  101. return PrivKeyEd25519(newKey)
  102. }
  103. func GenPrivKeyEd25519() PrivKeyEd25519 {
  104. privKeyBytes := new([64]byte)
  105. copy(privKeyBytes[:32], CRandBytes(32))
  106. ed25519.MakePublicKey(privKeyBytes)
  107. return PrivKeyEd25519(*privKeyBytes)
  108. }
  109. // NOTE: secret should be the output of a KDF like bcrypt,
  110. // if it's derived from user input.
  111. func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
  112. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  113. privKeyBytes := new([64]byte)
  114. copy(privKeyBytes[:32], privKey32)
  115. ed25519.MakePublicKey(privKeyBytes)
  116. return PrivKeyEd25519(*privKeyBytes)
  117. }
  118. //-------------------------------------
  119. // Implements PrivKey
  120. type PrivKeySecp256k1 [32]byte
  121. func (privKey PrivKeySecp256k1) Bytes() []byte {
  122. return wire.BinaryBytes(struct{ PrivKey }{privKey})
  123. }
  124. func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
  125. priv__, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  126. sig__, err := priv__.Sign(Sha256(msg))
  127. if err != nil {
  128. PanicSanity(err)
  129. }
  130. return SignatureSecp256k1(sig__.Serialize())
  131. }
  132. func (privKey PrivKeySecp256k1) PubKey() PubKey {
  133. _, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  134. pub := [64]byte{}
  135. copy(pub[:], pub__.SerializeUncompressed()[1:])
  136. return PubKeySecp256k1(pub)
  137. }
  138. func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
  139. if otherSecp, ok := other.(PrivKeySecp256k1); ok {
  140. return bytes.Equal(privKey[:], otherSecp[:])
  141. } else {
  142. return false
  143. }
  144. }
  145. func (p PrivKeySecp256k1) MarshalJSON() ([]byte, error) {
  146. return data.Encoder.Marshal(p[:])
  147. }
  148. func (p *PrivKeySecp256k1) UnmarshalJSON(enc []byte) error {
  149. var ref []byte
  150. err := data.Encoder.Unmarshal(&ref, enc)
  151. copy(p[:], ref)
  152. return err
  153. }
  154. func (privKey PrivKeySecp256k1) String() string {
  155. return Fmt("PrivKeySecp256k1{*****}")
  156. }
  157. /*
  158. // Deterministically generates new priv-key bytes from key.
  159. func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
  160. newBytes := wire.BinarySha256(struct {
  161. PrivKey [64]byte
  162. Index int
  163. }{key, index})
  164. var newKey [64]byte
  165. copy(newKey[:], newBytes)
  166. return PrivKeySecp256k1(newKey)
  167. }
  168. */
  169. func GenPrivKeySecp256k1() PrivKeySecp256k1 {
  170. privKeyBytes := [32]byte{}
  171. copy(privKeyBytes[:], CRandBytes(32))
  172. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:])
  173. copy(privKeyBytes[:], priv.Serialize())
  174. return PrivKeySecp256k1(privKeyBytes)
  175. }
  176. // NOTE: secret should be the output of a KDF like bcrypt,
  177. // if it's derived from user input.
  178. func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 {
  179. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  180. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32)
  181. privKeyBytes := [32]byte{}
  182. copy(privKeyBytes[:], priv.Serialize())
  183. return PrivKeySecp256k1(privKeyBytes)
  184. }