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.

220 lines
5.3 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. // Implements PubKeyInner
  58. type PubKeyEd25519 [32]byte
  59. func (pubKey PubKeyEd25519) AssertIsPubKeyInner() {}
  60. func (pubKey PubKeyEd25519) Address() []byte {
  61. w, n, err := new(bytes.Buffer), new(int), new(error)
  62. wire.WriteBinary(pubKey[:], w, n, err)
  63. if *err != nil {
  64. PanicCrisis(*err)
  65. }
  66. // append type byte
  67. encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...)
  68. hasher := ripemd160.New()
  69. hasher.Write(encodedPubkey) // does not error
  70. return hasher.Sum(nil)
  71. }
  72. func (pubKey PubKeyEd25519) Bytes() []byte {
  73. return wire.BinaryBytes(PubKey{pubKey})
  74. }
  75. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  76. // make sure we use the same algorithm to sign
  77. sig, ok := sig_.Unwrap().(SignatureEd25519)
  78. if !ok {
  79. return false
  80. }
  81. pubKeyBytes := [32]byte(pubKey)
  82. sigBytes := [64]byte(sig)
  83. return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
  84. }
  85. func (p PubKeyEd25519) MarshalJSON() ([]byte, error) {
  86. return data.Encoder.Marshal(p[:])
  87. }
  88. func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error {
  89. var ref []byte
  90. err := data.Encoder.Unmarshal(&ref, enc)
  91. copy(p[:], ref)
  92. return err
  93. }
  94. // For use with golang/crypto/nacl/box
  95. // If error, returns nil.
  96. func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
  97. keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
  98. ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
  99. if !ok {
  100. return nil
  101. }
  102. return keyCurve25519
  103. }
  104. func (pubKey PubKeyEd25519) String() string {
  105. return Fmt("PubKeyEd25519{%X}", pubKey[:])
  106. }
  107. // Must return the full bytes in hex.
  108. // Used for map keying, etc.
  109. func (pubKey PubKeyEd25519) KeyString() string {
  110. return Fmt("%X", pubKey[:])
  111. }
  112. func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
  113. if otherEd, ok := other.Unwrap().(PubKeyEd25519); ok {
  114. return bytes.Equal(pubKey[:], otherEd[:])
  115. } else {
  116. return false
  117. }
  118. }
  119. func (pubKey PubKeyEd25519) Wrap() PubKey {
  120. return PubKey{pubKey}
  121. }
  122. //-------------------------------------
  123. // Implements PubKey.
  124. // Compressed pubkey (just the x-cord),
  125. // prefixed with 0x02 or 0x03, depending on the y-cord.
  126. type PubKeySecp256k1 [33]byte
  127. func (pubKey PubKeySecp256k1) AssertIsPubKeyInner() {}
  128. // Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
  129. func (pubKey PubKeySecp256k1) Address() []byte {
  130. hasherSHA256 := sha256.New()
  131. hasherSHA256.Write(pubKey[:]) // does not error
  132. sha := hasherSHA256.Sum(nil)
  133. hasherRIPEMD160 := ripemd160.New()
  134. hasherRIPEMD160.Write(sha) // does not error
  135. return hasherRIPEMD160.Sum(nil)
  136. }
  137. func (pubKey PubKeySecp256k1) Bytes() []byte {
  138. return wire.BinaryBytes(PubKey{pubKey})
  139. }
  140. func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
  141. // and assert same algorithm to sign and verify
  142. sig, ok := sig_.Unwrap().(SignatureSecp256k1)
  143. if !ok {
  144. return false
  145. }
  146. pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
  147. if err != nil {
  148. return false
  149. }
  150. sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
  151. if err != nil {
  152. return false
  153. }
  154. return sig__.Verify(Sha256(msg), pub__)
  155. }
  156. func (p PubKeySecp256k1) MarshalJSON() ([]byte, error) {
  157. return data.Encoder.Marshal(p[:])
  158. }
  159. func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error {
  160. var ref []byte
  161. err := data.Encoder.Unmarshal(&ref, enc)
  162. copy(p[:], ref)
  163. return err
  164. }
  165. func (pubKey PubKeySecp256k1) String() string {
  166. return Fmt("PubKeySecp256k1{%X}", pubKey[:])
  167. }
  168. // Must return the full bytes in hex.
  169. // Used for map keying, etc.
  170. func (pubKey PubKeySecp256k1) KeyString() string {
  171. return Fmt("%X", pubKey[:])
  172. }
  173. func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
  174. if otherSecp, ok := other.Unwrap().(PubKeySecp256k1); ok {
  175. return bytes.Equal(pubKey[:], otherSecp[:])
  176. } else {
  177. return false
  178. }
  179. }
  180. func (pubKey PubKeySecp256k1) Wrap() PubKey {
  181. return PubKey{pubKey}
  182. }