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.

233 lines
6.0 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
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. func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
  12. err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
  13. return
  14. }
  15. //----------------------------------------
  16. type PrivKey struct {
  17. PrivKeyInner `json:"unwrap"`
  18. }
  19. // DO NOT USE THIS INTERFACE.
  20. // You probably want to use PubKey
  21. type PrivKeyInner interface {
  22. AssertIsPrivKeyInner()
  23. Bytes() []byte
  24. Sign(msg []byte) Signature
  25. PubKey() PubKey
  26. Equals(PrivKey) bool
  27. Wrap() PrivKey
  28. }
  29. func (p PrivKey) MarshalJSON() ([]byte, error) {
  30. return privKeyMapper.ToJSON(p.PrivKeyInner)
  31. }
  32. func (p *PrivKey) UnmarshalJSON(data []byte) (err error) {
  33. parsed, err := privKeyMapper.FromJSON(data)
  34. if err == nil && parsed != nil {
  35. p.PrivKeyInner = parsed.(PrivKeyInner)
  36. }
  37. return
  38. }
  39. // Unwrap recovers the concrete interface safely (regardless of levels of embeds)
  40. func (p PrivKey) Unwrap() PrivKeyInner {
  41. pk := p.PrivKeyInner
  42. for wrap, ok := pk.(PrivKey); ok; wrap, ok = pk.(PrivKey) {
  43. pk = wrap.PrivKeyInner
  44. }
  45. return pk
  46. }
  47. func (p PrivKey) Empty() bool {
  48. return p.PrivKeyInner == nil
  49. }
  50. var privKeyMapper = data.NewMapper(PrivKey{}).
  51. RegisterImplementation(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
  52. RegisterImplementation(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
  53. //-------------------------------------
  54. // Implements PrivKey
  55. type PrivKeyEd25519 [64]byte
  56. func (privKey PrivKeyEd25519) AssertIsPrivKeyInner() {}
  57. func (privKey PrivKeyEd25519) Bytes() []byte {
  58. return wire.BinaryBytes(PrivKey{privKey})
  59. }
  60. func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
  61. privKeyBytes := [64]byte(privKey)
  62. signatureBytes := ed25519.Sign(&privKeyBytes, msg)
  63. return SignatureEd25519(*signatureBytes).Wrap()
  64. }
  65. func (privKey PrivKeyEd25519) PubKey() PubKey {
  66. privKeyBytes := [64]byte(privKey)
  67. pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
  68. return PubKeyEd25519(pubBytes).Wrap()
  69. }
  70. func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
  71. if otherEd, ok := other.Unwrap().(PrivKeyEd25519); ok {
  72. return bytes.Equal(privKey[:], otherEd[:])
  73. } else {
  74. return false
  75. }
  76. }
  77. func (p PrivKeyEd25519) MarshalJSON() ([]byte, error) {
  78. return data.Encoder.Marshal(p[:])
  79. }
  80. func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error {
  81. var ref []byte
  82. err := data.Encoder.Unmarshal(&ref, enc)
  83. copy(p[:], ref)
  84. return err
  85. }
  86. func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
  87. keyCurve25519 := new([32]byte)
  88. privKeyBytes := [64]byte(privKey)
  89. extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes)
  90. return keyCurve25519
  91. }
  92. func (privKey PrivKeyEd25519) String() string {
  93. return Fmt("PrivKeyEd25519{*****}")
  94. }
  95. // Deterministically generates new priv-key bytes from key.
  96. func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
  97. newBytes := wire.BinarySha256(struct {
  98. PrivKey [64]byte
  99. Index int
  100. }{privKey, index})
  101. var newKey [64]byte
  102. copy(newKey[:], newBytes)
  103. return PrivKeyEd25519(newKey)
  104. }
  105. func (privKey PrivKeyEd25519) Wrap() PrivKey {
  106. return PrivKey{privKey}
  107. }
  108. func GenPrivKeyEd25519() PrivKeyEd25519 {
  109. privKeyBytes := new([64]byte)
  110. copy(privKeyBytes[:32], CRandBytes(32))
  111. ed25519.MakePublicKey(privKeyBytes)
  112. return PrivKeyEd25519(*privKeyBytes)
  113. }
  114. // NOTE: secret should be the output of a KDF like bcrypt,
  115. // if it's derived from user input.
  116. func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
  117. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  118. privKeyBytes := new([64]byte)
  119. copy(privKeyBytes[:32], privKey32)
  120. ed25519.MakePublicKey(privKeyBytes)
  121. return PrivKeyEd25519(*privKeyBytes)
  122. }
  123. //-------------------------------------
  124. // Implements PrivKey
  125. type PrivKeySecp256k1 [32]byte
  126. func (privKey PrivKeySecp256k1) AssertIsPrivKeyInner() {}
  127. func (privKey PrivKeySecp256k1) Bytes() []byte {
  128. return wire.BinaryBytes(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()).Wrap()
  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.Wrap()
  143. }
  144. func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
  145. if otherSecp, ok := other.Unwrap().(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. func (privKey PrivKeySecp256k1) Wrap() PrivKey {
  164. return PrivKey{privKey}
  165. }
  166. /*
  167. // Deterministically generates new priv-key bytes from key.
  168. func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
  169. newBytes := wire.BinarySha256(struct {
  170. PrivKey [64]byte
  171. Index int
  172. }{key, index})
  173. var newKey [64]byte
  174. copy(newKey[:], newBytes)
  175. return PrivKeySecp256k1(newKey)
  176. }
  177. */
  178. func GenPrivKeySecp256k1() PrivKeySecp256k1 {
  179. privKeyBytes := [32]byte{}
  180. copy(privKeyBytes[:], CRandBytes(32))
  181. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:])
  182. copy(privKeyBytes[:], priv.Serialize())
  183. return PrivKeySecp256k1(privKeyBytes)
  184. }
  185. // NOTE: secret should be the output of a KDF like bcrypt,
  186. // if it's derived from user input.
  187. func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 {
  188. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  189. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32)
  190. privKeyBytes := [32]byte{}
  191. copy(privKeyBytes[:], priv.Serialize())
  192. return PrivKeySecp256k1(privKeyBytes)
  193. }