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.

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