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.

156 lines
3.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
  1. package crypto
  2. import (
  3. "bytes"
  4. secp256k1 "github.com/btcsuite/btcd/btcec"
  5. "github.com/tendermint/ed25519"
  6. "github.com/tendermint/ed25519/extra25519"
  7. . "github.com/tendermint/go-common"
  8. "github.com/tendermint/go-wire"
  9. "golang.org/x/crypto/ripemd160"
  10. )
  11. // PubKey is part of Account and Validator.
  12. type PubKey interface {
  13. Address() []byte
  14. Bytes() []byte
  15. KeyString() string
  16. VerifyBytes(msg []byte, sig Signature) bool
  17. Equals(PubKey) bool
  18. }
  19. // Types of PubKey implementations
  20. const (
  21. PubKeyTypeEd25519 = byte(0x01)
  22. PubKeyTypeSecp256k1 = byte(0x02)
  23. )
  24. // for wire.readReflect
  25. var _ = wire.RegisterInterface(
  26. struct{ PubKey }{},
  27. wire.ConcreteType{PubKeyEd25519{}, PubKeyTypeEd25519},
  28. wire.ConcreteType{PubKeySecp256k1{}, PubKeyTypeSecp256k1},
  29. )
  30. func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
  31. err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
  32. return
  33. }
  34. //-------------------------------------
  35. // Implements PubKey
  36. type PubKeyEd25519 [32]byte
  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{PubKeyTypeEd25519}, 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(struct{ PubKey }{pubKey})
  51. }
  52. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  53. sig, ok := sig_.(SignatureEd25519)
  54. if !ok {
  55. return false
  56. }
  57. pubKeyBytes := [32]byte(pubKey)
  58. sigBytes := [64]byte(sig)
  59. return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
  60. }
  61. // For use with golang/crypto/nacl/box
  62. // If error, returns nil.
  63. func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
  64. keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
  65. ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
  66. if !ok {
  67. return nil
  68. }
  69. return keyCurve25519
  70. }
  71. func (pubKey PubKeyEd25519) String() string {
  72. return Fmt("PubKeyEd25519{%X}", pubKey[:])
  73. }
  74. // Must return the full bytes in hex.
  75. // Used for map keying, etc.
  76. func (pubKey PubKeyEd25519) KeyString() string {
  77. return Fmt("%X", pubKey[:])
  78. }
  79. func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
  80. if otherEd, ok := other.(PubKeyEd25519); ok {
  81. return bytes.Equal(pubKey[:], otherEd[:])
  82. } else {
  83. return false
  84. }
  85. }
  86. //-------------------------------------
  87. // Implements PubKey
  88. type PubKeySecp256k1 [64]byte
  89. func (pubKey PubKeySecp256k1) Address() []byte {
  90. w, n, err := new(bytes.Buffer), new(int), new(error)
  91. wire.WriteBinary(pubKey[:], w, n, err)
  92. if *err != nil {
  93. PanicCrisis(*err)
  94. }
  95. // append type byte
  96. encodedPubkey := append([]byte{PubKeyTypeSecp256k1}, w.Bytes()...)
  97. hasher := ripemd160.New()
  98. hasher.Write(encodedPubkey) // does not error
  99. return hasher.Sum(nil)
  100. }
  101. func (pubKey PubKeySecp256k1) Bytes() []byte {
  102. return wire.BinaryBytes(struct{ PubKey }{pubKey})
  103. }
  104. func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
  105. pub__, err := secp256k1.ParsePubKey(append([]byte{0x04}, pubKey[:]...), secp256k1.S256())
  106. if err != nil {
  107. return false
  108. }
  109. sig, ok := sig_.(SignatureSecp256k1)
  110. if !ok {
  111. return false
  112. }
  113. sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
  114. if err != nil {
  115. return false
  116. }
  117. return sig__.Verify(Sha256(msg), pub__)
  118. }
  119. func (pubKey PubKeySecp256k1) String() string {
  120. return Fmt("PubKeySecp256k1{%X}", pubKey[:])
  121. }
  122. // Must return the full bytes in hex.
  123. // Used for map keying, etc.
  124. func (pubKey PubKeySecp256k1) KeyString() string {
  125. return Fmt("%X", pubKey[:])
  126. }
  127. func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
  128. if otherSecp, ok := other.(PubKeySecp256k1); ok {
  129. return bytes.Equal(pubKey[:], otherSecp[:])
  130. } else {
  131. return false
  132. }
  133. }