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.

224 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
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. "crypto/sha256"
  5. secp256k1 "github.com/btcsuite/btcd/btcec"
  6. "github.com/tendermint/ed25519"
  7. "github.com/tendermint/ed25519/extra25519"
  8. . "github.com/tendermint/go-common"
  9. data "github.com/tendermint/go-data"
  10. "github.com/tendermint/go-wire"
  11. "golang.org/x/crypto/ripemd160"
  12. )
  13. func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
  14. err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
  15. return
  16. }
  17. //----------------------------------------
  18. type PubKey struct {
  19. PubKeyInner `json:"unwrap"`
  20. }
  21. // DO NOT USE THIS INTERFACE.
  22. // You probably want to use PubKey
  23. type PubKeyInner interface {
  24. AssertIsPubKeyInner()
  25. Address() []byte
  26. Bytes() []byte
  27. KeyString() string
  28. VerifyBytes(msg []byte, sig Signature) bool
  29. Equals(PubKey) bool
  30. Wrap() PubKey
  31. }
  32. func (pk PubKey) MarshalJSON() ([]byte, error) {
  33. return pubKeyMapper.ToJSON(pk.PubKeyInner)
  34. }
  35. func (pk *PubKey) UnmarshalJSON(data []byte) (err error) {
  36. parsed, err := pubKeyMapper.FromJSON(data)
  37. if err == nil && parsed != nil {
  38. pk.PubKeyInner = parsed.(PubKeyInner)
  39. }
  40. return
  41. }
  42. // Unwrap recovers the concrete interface safely (regardless of levels of embeds)
  43. func (pk PubKey) Unwrap() PubKeyInner {
  44. pkI := pk.PubKeyInner
  45. for wrap, ok := pkI.(PubKey); ok; wrap, ok = pkI.(PubKey) {
  46. pkI = wrap.PubKeyInner
  47. }
  48. return pkI
  49. }
  50. func (p PubKey) Empty() bool {
  51. return p.PubKeyInner == nil
  52. }
  53. var pubKeyMapper = data.NewMapper(PubKey{}).
  54. RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519).
  55. RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
  56. //-------------------------------------
  57. var _ PubKeyInner = PubKeyEd25519{}
  58. // Implements PubKeyInner
  59. type PubKeyEd25519 [32]byte
  60. func (pubKey PubKeyEd25519) AssertIsPubKeyInner() {}
  61. func (pubKey PubKeyEd25519) Address() []byte {
  62. w, n, err := new(bytes.Buffer), new(int), new(error)
  63. wire.WriteBinary(pubKey[:], w, n, err)
  64. if *err != nil {
  65. PanicCrisis(*err)
  66. }
  67. // append type byte
  68. encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...)
  69. hasher := ripemd160.New()
  70. hasher.Write(encodedPubkey) // does not error
  71. return hasher.Sum(nil)
  72. }
  73. func (pubKey PubKeyEd25519) Bytes() []byte {
  74. return wire.BinaryBytes(PubKey{pubKey})
  75. }
  76. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  77. // make sure we use the same algorithm to sign
  78. sig, ok := sig_.Unwrap().(SignatureEd25519)
  79. if !ok {
  80. return false
  81. }
  82. pubKeyBytes := [32]byte(pubKey)
  83. sigBytes := [64]byte(sig)
  84. return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
  85. }
  86. func (p PubKeyEd25519) MarshalJSON() ([]byte, error) {
  87. return data.Encoder.Marshal(p[:])
  88. }
  89. func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error {
  90. var ref []byte
  91. err := data.Encoder.Unmarshal(&ref, enc)
  92. copy(p[:], ref)
  93. return err
  94. }
  95. // For use with golang/crypto/nacl/box
  96. // If error, returns nil.
  97. func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
  98. keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
  99. ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
  100. if !ok {
  101. return nil
  102. }
  103. return keyCurve25519
  104. }
  105. func (pubKey PubKeyEd25519) String() string {
  106. return Fmt("PubKeyEd25519{%X}", pubKey[:])
  107. }
  108. // Must return the full bytes in hex.
  109. // Used for map keying, etc.
  110. func (pubKey PubKeyEd25519) KeyString() string {
  111. return Fmt("%X", pubKey[:])
  112. }
  113. func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
  114. if otherEd, ok := other.Unwrap().(PubKeyEd25519); ok {
  115. return bytes.Equal(pubKey[:], otherEd[:])
  116. } else {
  117. return false
  118. }
  119. }
  120. func (pubKey PubKeyEd25519) Wrap() PubKey {
  121. return PubKey{pubKey}
  122. }
  123. //-------------------------------------
  124. var _ PubKeyInner = PubKeySecp256k1{}
  125. // Implements PubKey.
  126. // Compressed pubkey (just the x-cord),
  127. // prefixed with 0x02 or 0x03, depending on the y-cord.
  128. type PubKeySecp256k1 [33]byte
  129. func (pubKey PubKeySecp256k1) AssertIsPubKeyInner() {}
  130. // Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
  131. func (pubKey PubKeySecp256k1) Address() []byte {
  132. hasherSHA256 := sha256.New()
  133. hasherSHA256.Write(pubKey[:]) // does not error
  134. sha := hasherSHA256.Sum(nil)
  135. hasherRIPEMD160 := ripemd160.New()
  136. hasherRIPEMD160.Write(sha) // does not error
  137. return hasherRIPEMD160.Sum(nil)
  138. }
  139. func (pubKey PubKeySecp256k1) Bytes() []byte {
  140. return wire.BinaryBytes(PubKey{pubKey})
  141. }
  142. func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
  143. // and assert same algorithm to sign and verify
  144. sig, ok := sig_.Unwrap().(SignatureSecp256k1)
  145. if !ok {
  146. return false
  147. }
  148. pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
  149. if err != nil {
  150. return false
  151. }
  152. sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
  153. if err != nil {
  154. return false
  155. }
  156. return sig__.Verify(Sha256(msg), pub__)
  157. }
  158. func (p PubKeySecp256k1) MarshalJSON() ([]byte, error) {
  159. return data.Encoder.Marshal(p[:])
  160. }
  161. func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error {
  162. var ref []byte
  163. err := data.Encoder.Unmarshal(&ref, enc)
  164. copy(p[:], ref)
  165. return err
  166. }
  167. func (pubKey PubKeySecp256k1) String() string {
  168. return Fmt("PubKeySecp256k1{%X}", pubKey[:])
  169. }
  170. // Must return the full bytes in hex.
  171. // Used for map keying, etc.
  172. func (pubKey PubKeySecp256k1) KeyString() string {
  173. return Fmt("%X", pubKey[:])
  174. }
  175. func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
  176. if otherSecp, ok := other.Unwrap().(PubKeySecp256k1); ok {
  177. return bytes.Equal(pubKey[:], otherSecp[:])
  178. } else {
  179. return false
  180. }
  181. }
  182. func (pubKey PubKeySecp256k1) Wrap() PubKey {
  183. return PubKey{pubKey}
  184. }