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
5.8 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 WrapPrivKey(pk PrivKey) PrivKeyS {
  37. for ppk, ok := pk.(PrivKeyS); ok; ppk, ok = pk.(PrivKeyS) {
  38. pk = ppk.PrivKey
  39. }
  40. return PrivKeyS{pk}
  41. }
  42. func (p PrivKeyS) MarshalJSON() ([]byte, error) {
  43. return privKeyMapper.ToJSON(p.PrivKey)
  44. }
  45. func (p *PrivKeyS) UnmarshalJSON(data []byte) (err error) {
  46. parsed, err := privKeyMapper.FromJSON(data)
  47. if err == nil && parsed != nil {
  48. p.PrivKey = parsed.(PrivKey)
  49. }
  50. return
  51. }
  52. func (p PrivKeyS) Empty() bool {
  53. return p.PrivKey == nil
  54. }
  55. func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
  56. err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
  57. return
  58. }
  59. //-------------------------------------
  60. // Implements PrivKey
  61. type PrivKeyEd25519 [64]byte
  62. func (privKey PrivKeyEd25519) Bytes() []byte {
  63. return wire.BinaryBytes(struct{ PrivKey }{privKey})
  64. }
  65. func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
  66. privKeyBytes := [64]byte(privKey)
  67. signatureBytes := ed25519.Sign(&privKeyBytes, msg)
  68. return SignatureEd25519(*signatureBytes)
  69. }
  70. func (privKey PrivKeyEd25519) PubKey() PubKey {
  71. privKeyBytes := [64]byte(privKey)
  72. return PubKeyEd25519(*ed25519.MakePublicKey(&privKeyBytes))
  73. }
  74. func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
  75. if otherEd, ok := other.(PrivKeyEd25519); ok {
  76. return bytes.Equal(privKey[:], otherEd[:])
  77. } else {
  78. return false
  79. }
  80. }
  81. func (p PrivKeyEd25519) MarshalJSON() ([]byte, error) {
  82. return data.Encoder.Marshal(p[:])
  83. }
  84. func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error {
  85. var ref []byte
  86. err := data.Encoder.Unmarshal(&ref, enc)
  87. copy(p[:], ref)
  88. return err
  89. }
  90. func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
  91. keyCurve25519 := new([32]byte)
  92. privKeyBytes := [64]byte(privKey)
  93. extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes)
  94. return keyCurve25519
  95. }
  96. func (privKey PrivKeyEd25519) String() string {
  97. return Fmt("PrivKeyEd25519{*****}")
  98. }
  99. // Deterministically generates new priv-key bytes from key.
  100. func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
  101. newBytes := wire.BinarySha256(struct {
  102. PrivKey [64]byte
  103. Index int
  104. }{privKey, index})
  105. var newKey [64]byte
  106. copy(newKey[:], newBytes)
  107. return PrivKeyEd25519(newKey)
  108. }
  109. func GenPrivKeyEd25519() PrivKeyEd25519 {
  110. privKeyBytes := new([64]byte)
  111. copy(privKeyBytes[:32], CRandBytes(32))
  112. ed25519.MakePublicKey(privKeyBytes)
  113. return PrivKeyEd25519(*privKeyBytes)
  114. }
  115. // NOTE: secret should be the output of a KDF like bcrypt,
  116. // if it's derived from user input.
  117. func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
  118. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  119. privKeyBytes := new([64]byte)
  120. copy(privKeyBytes[:32], privKey32)
  121. ed25519.MakePublicKey(privKeyBytes)
  122. return PrivKeyEd25519(*privKeyBytes)
  123. }
  124. //-------------------------------------
  125. // Implements PrivKey
  126. type PrivKeySecp256k1 [32]byte
  127. func (privKey PrivKeySecp256k1) Bytes() []byte {
  128. return wire.BinaryBytes(struct{ PrivKey }{privKey})
  129. }
  130. func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
  131. priv__, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  132. sig__, err := priv__.Sign(Sha256(msg))
  133. if err != nil {
  134. PanicSanity(err)
  135. }
  136. return SignatureSecp256k1(sig__.Serialize())
  137. }
  138. func (privKey PrivKeySecp256k1) PubKey() PubKey {
  139. _, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  140. var pub PubKeySecp256k1
  141. copy(pub[:], pub__.SerializeCompressed())
  142. return pub
  143. }
  144. func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
  145. if otherSecp, ok := other.(PrivKeySecp256k1); ok {
  146. return bytes.Equal(privKey[:], otherSecp[:])
  147. } else {
  148. return false
  149. }
  150. }
  151. func (p PrivKeySecp256k1) MarshalJSON() ([]byte, error) {
  152. return data.Encoder.Marshal(p[:])
  153. }
  154. func (p *PrivKeySecp256k1) UnmarshalJSON(enc []byte) error {
  155. var ref []byte
  156. err := data.Encoder.Unmarshal(&ref, enc)
  157. copy(p[:], ref)
  158. return err
  159. }
  160. func (privKey PrivKeySecp256k1) String() string {
  161. return Fmt("PrivKeySecp256k1{*****}")
  162. }
  163. /*
  164. // Deterministically generates new priv-key bytes from key.
  165. func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
  166. newBytes := wire.BinarySha256(struct {
  167. PrivKey [64]byte
  168. Index int
  169. }{key, index})
  170. var newKey [64]byte
  171. copy(newKey[:], newBytes)
  172. return PrivKeySecp256k1(newKey)
  173. }
  174. */
  175. func GenPrivKeySecp256k1() PrivKeySecp256k1 {
  176. privKeyBytes := [32]byte{}
  177. copy(privKeyBytes[:], CRandBytes(32))
  178. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:])
  179. copy(privKeyBytes[:], priv.Serialize())
  180. return PrivKeySecp256k1(privKeyBytes)
  181. }
  182. // NOTE: secret should be the output of a KDF like bcrypt,
  183. // if it's derived from user input.
  184. func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 {
  185. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  186. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32)
  187. privKeyBytes := [32]byte{}
  188. copy(privKeyBytes[:], priv.Serialize())
  189. return PrivKeySecp256k1(privKeyBytes)
  190. }