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.

205 lines
5.7 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. "crypto/subtle"
  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. // It is essential that we constant time compare
  48. // private keys and signatures instead of bytes.Equal,
  49. // to avoid susceptibility to timing/side channel attacks.
  50. // See Issue https://github.com/tendermint/go-crypto/issues/43
  51. return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 0
  52. } else {
  53. return false
  54. }
  55. }
  56. func (p PrivKeyEd25519) MarshalJSON() ([]byte, error) {
  57. return data.Encoder.Marshal(p[:])
  58. }
  59. func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error {
  60. var ref []byte
  61. err := data.Encoder.Unmarshal(&ref, enc)
  62. copy(p[:], ref)
  63. return err
  64. }
  65. func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
  66. keyCurve25519 := new([32]byte)
  67. privKeyBytes := [64]byte(privKey)
  68. extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes)
  69. return keyCurve25519
  70. }
  71. func (privKey PrivKeyEd25519) String() string {
  72. return Fmt("PrivKeyEd25519{*****}")
  73. }
  74. // Deterministically generates new priv-key bytes from key.
  75. func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
  76. newBytes := wire.BinarySha256(struct {
  77. PrivKey [64]byte
  78. Index int
  79. }{privKey, index})
  80. var newKey [64]byte
  81. copy(newKey[:], newBytes)
  82. return PrivKeyEd25519(newKey)
  83. }
  84. func GenPrivKeyEd25519() PrivKeyEd25519 {
  85. privKeyBytes := new([64]byte)
  86. copy(privKeyBytes[:32], CRandBytes(32))
  87. ed25519.MakePublicKey(privKeyBytes)
  88. return PrivKeyEd25519(*privKeyBytes)
  89. }
  90. // NOTE: secret should be the output of a KDF like bcrypt,
  91. // if it's derived from user input.
  92. func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
  93. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  94. privKeyBytes := new([64]byte)
  95. copy(privKeyBytes[:32], privKey32)
  96. ed25519.MakePublicKey(privKeyBytes)
  97. return PrivKeyEd25519(*privKeyBytes)
  98. }
  99. //-------------------------------------
  100. var _ PrivKeyInner = PrivKeySecp256k1{}
  101. // Implements PrivKey
  102. type PrivKeySecp256k1 [32]byte
  103. func (privKey PrivKeySecp256k1) AssertIsPrivKeyInner() {}
  104. func (privKey PrivKeySecp256k1) Bytes() []byte {
  105. return wire.BinaryBytes(PrivKey{privKey})
  106. }
  107. func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
  108. priv__, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  109. sig__, err := priv__.Sign(Sha256(msg))
  110. if err != nil {
  111. PanicSanity(err)
  112. }
  113. return SignatureSecp256k1(sig__.Serialize()).Wrap()
  114. }
  115. func (privKey PrivKeySecp256k1) PubKey() PubKey {
  116. _, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  117. var pub PubKeySecp256k1
  118. copy(pub[:], pub__.SerializeCompressed())
  119. return pub.Wrap()
  120. }
  121. func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
  122. if otherSecp, ok := other.Unwrap().(PrivKeySecp256k1); ok {
  123. // It is essential that we constant time compare
  124. // private keys and signatures instead of bytes.Equal,
  125. // to avoid susceptibility to timing/side channel attacks.
  126. // See Issue https://github.com/tendermint/go-crypto/issues/43
  127. return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 0
  128. } else {
  129. return false
  130. }
  131. }
  132. func (p PrivKeySecp256k1) MarshalJSON() ([]byte, error) {
  133. return data.Encoder.Marshal(p[:])
  134. }
  135. func (p *PrivKeySecp256k1) UnmarshalJSON(enc []byte) error {
  136. var ref []byte
  137. err := data.Encoder.Unmarshal(&ref, enc)
  138. copy(p[:], ref)
  139. return err
  140. }
  141. func (privKey PrivKeySecp256k1) String() string {
  142. return Fmt("PrivKeySecp256k1{*****}")
  143. }
  144. /*
  145. // Deterministically generates new priv-key bytes from key.
  146. func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
  147. newBytes := wire.BinarySha256(struct {
  148. PrivKey [64]byte
  149. Index int
  150. }{key, index})
  151. var newKey [64]byte
  152. copy(newKey[:], newBytes)
  153. return PrivKeySecp256k1(newKey)
  154. }
  155. */
  156. func GenPrivKeySecp256k1() PrivKeySecp256k1 {
  157. privKeyBytes := [32]byte{}
  158. copy(privKeyBytes[:], CRandBytes(32))
  159. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:])
  160. copy(privKeyBytes[:], priv.Serialize())
  161. return PrivKeySecp256k1(privKeyBytes)
  162. }
  163. // NOTE: secret should be the output of a KDF like bcrypt,
  164. // if it's derived from user input.
  165. func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 {
  166. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  167. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32)
  168. privKeyBytes := [32]byte{}
  169. copy(privKeyBytes[:], priv.Serialize())
  170. return PrivKeySecp256k1(privKeyBytes)
  171. }