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.

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