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.

184 lines
4.5 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
  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-wire"
  9. data "github.com/tendermint/go-wire/data"
  10. . "github.com/tendermint/tmlibs/common"
  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. // DO NOT USE THIS INTERFACE.
  19. // You probably want to use PubKey
  20. // +gen wrapper:"PubKey,Impl[PubKeyEd25519,PubKeySecp256k1],ed25519,secp256k1"
  21. type PubKeyInner interface {
  22. AssertIsPubKeyInner()
  23. Address() []byte
  24. Bytes() []byte
  25. KeyString() string
  26. VerifyBytes(msg []byte, sig Signature) bool
  27. Equals(PubKey) bool
  28. Wrap() PubKey
  29. }
  30. //-------------------------------------
  31. var _ PubKeyInner = PubKeyEd25519{}
  32. // Implements PubKeyInner
  33. type PubKeyEd25519 [32]byte
  34. func (pubKey PubKeyEd25519) AssertIsPubKeyInner() {}
  35. func (pubKey PubKeyEd25519) Address() []byte {
  36. w, n, err := new(bytes.Buffer), new(int), new(error)
  37. wire.WriteBinary(pubKey[:], w, n, err)
  38. if *err != nil {
  39. PanicCrisis(*err)
  40. }
  41. // append type byte
  42. encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...)
  43. hasher := ripemd160.New()
  44. hasher.Write(encodedPubkey) // does not error
  45. return hasher.Sum(nil)
  46. }
  47. func (pubKey PubKeyEd25519) Bytes() []byte {
  48. return wire.BinaryBytes(PubKey{pubKey})
  49. }
  50. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  51. // make sure we use the same algorithm to sign
  52. sig, ok := sig_.Unwrap().(SignatureEd25519)
  53. if !ok {
  54. return false
  55. }
  56. pubKeyBytes := [32]byte(pubKey)
  57. sigBytes := [64]byte(sig)
  58. return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
  59. }
  60. func (p PubKeyEd25519) MarshalJSON() ([]byte, error) {
  61. return data.Encoder.Marshal(p[:])
  62. }
  63. func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error {
  64. var ref []byte
  65. err := data.Encoder.Unmarshal(&ref, enc)
  66. copy(p[:], ref)
  67. return err
  68. }
  69. // For use with golang/crypto/nacl/box
  70. // If error, returns nil.
  71. func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
  72. keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
  73. ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
  74. if !ok {
  75. return nil
  76. }
  77. return keyCurve25519
  78. }
  79. func (pubKey PubKeyEd25519) String() string {
  80. return Fmt("PubKeyEd25519{%X}", pubKey[:])
  81. }
  82. // Must return the full bytes in hex.
  83. // Used for map keying, etc.
  84. func (pubKey PubKeyEd25519) KeyString() string {
  85. return Fmt("%X", pubKey[:])
  86. }
  87. func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
  88. if otherEd, ok := other.Unwrap().(PubKeyEd25519); ok {
  89. return bytes.Equal(pubKey[:], otherEd[:])
  90. } else {
  91. return false
  92. }
  93. }
  94. //-------------------------------------
  95. var _ PubKeyInner = PubKeySecp256k1{}
  96. // Implements PubKey.
  97. // Compressed pubkey (just the x-cord),
  98. // prefixed with 0x02 or 0x03, depending on the y-cord.
  99. type PubKeySecp256k1 [33]byte
  100. func (pubKey PubKeySecp256k1) AssertIsPubKeyInner() {}
  101. // Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
  102. func (pubKey PubKeySecp256k1) Address() []byte {
  103. hasherSHA256 := sha256.New()
  104. hasherSHA256.Write(pubKey[:]) // does not error
  105. sha := hasherSHA256.Sum(nil)
  106. hasherRIPEMD160 := ripemd160.New()
  107. hasherRIPEMD160.Write(sha) // does not error
  108. return hasherRIPEMD160.Sum(nil)
  109. }
  110. func (pubKey PubKeySecp256k1) Bytes() []byte {
  111. return wire.BinaryBytes(PubKey{pubKey})
  112. }
  113. func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
  114. // and assert same algorithm to sign and verify
  115. sig, ok := sig_.Unwrap().(SignatureSecp256k1)
  116. if !ok {
  117. return false
  118. }
  119. pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
  120. if err != nil {
  121. return false
  122. }
  123. sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
  124. if err != nil {
  125. return false
  126. }
  127. return sig__.Verify(Sha256(msg), pub__)
  128. }
  129. func (p PubKeySecp256k1) MarshalJSON() ([]byte, error) {
  130. return data.Encoder.Marshal(p[:])
  131. }
  132. func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error {
  133. var ref []byte
  134. err := data.Encoder.Unmarshal(&ref, enc)
  135. copy(p[:], ref)
  136. return err
  137. }
  138. func (pubKey PubKeySecp256k1) String() string {
  139. return Fmt("PubKeySecp256k1{%X}", pubKey[:])
  140. }
  141. // Must return the full bytes in hex.
  142. // Used for map keying, etc.
  143. func (pubKey PubKeySecp256k1) KeyString() string {
  144. return Fmt("%X", pubKey[:])
  145. }
  146. func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
  147. if otherSecp, ok := other.Unwrap().(PubKeySecp256k1); ok {
  148. return bytes.Equal(pubKey[:], otherSecp[:])
  149. } else {
  150. return false
  151. }
  152. }