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.

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