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.

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