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.

172 lines
4.6 KiB

9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 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. )
  10. // PrivKey is part of PrivAccount and state.PrivValidator.
  11. type PrivKey interface {
  12. Bytes() []byte
  13. Sign(msg []byte) Signature
  14. PubKey() PubKey
  15. Equals(PrivKey) bool
  16. }
  17. // Types of PrivKey implementations
  18. const (
  19. PrivKeyTypeEd25519 = byte(0x01)
  20. PrivKeyTypeSecp256k1 = byte(0x02)
  21. )
  22. // for wire.readReflect
  23. var _ = wire.RegisterInterface(
  24. struct{ PrivKey }{},
  25. wire.ConcreteType{PrivKeyEd25519{}, PrivKeyTypeEd25519},
  26. wire.ConcreteType{PrivKeySecp256k1{}, PrivKeyTypeSecp256k1},
  27. )
  28. func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
  29. err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
  30. return
  31. }
  32. //-------------------------------------
  33. // Implements PrivKey
  34. type PrivKeyEd25519 [64]byte
  35. func (privKey PrivKeyEd25519) Bytes() []byte {
  36. return wire.BinaryBytes(struct{ PrivKey }{privKey})
  37. }
  38. func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
  39. privKeyBytes := [64]byte(privKey)
  40. signatureBytes := ed25519.Sign(&privKeyBytes, msg)
  41. return SignatureEd25519(*signatureBytes)
  42. }
  43. func (privKey PrivKeyEd25519) PubKey() PubKey {
  44. privKeyBytes := [64]byte(privKey)
  45. return PubKeyEd25519(*ed25519.MakePublicKey(&privKeyBytes))
  46. }
  47. func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
  48. if otherEd, ok := other.(PrivKeyEd25519); ok {
  49. return bytes.Equal(privKey[:], otherEd[:])
  50. } else {
  51. return false
  52. }
  53. }
  54. func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
  55. keyCurve25519 := new([32]byte)
  56. privKeyBytes := [64]byte(privKey)
  57. extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes)
  58. return keyCurve25519
  59. }
  60. func (privKey PrivKeyEd25519) String() string {
  61. return Fmt("PrivKeyEd25519{*****}")
  62. }
  63. // Deterministically generates new priv-key bytes from key.
  64. func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
  65. newBytes := wire.BinarySha256(struct {
  66. PrivKey [64]byte
  67. Index int
  68. }{privKey, index})
  69. var newKey [64]byte
  70. copy(newKey[:], newBytes)
  71. return PrivKeyEd25519(newKey)
  72. }
  73. func GenPrivKeyEd25519() PrivKeyEd25519 {
  74. privKeyBytes := new([64]byte)
  75. copy(privKeyBytes[:32], CRandBytes(32))
  76. ed25519.MakePublicKey(privKeyBytes)
  77. return PrivKeyEd25519(*privKeyBytes)
  78. }
  79. // NOTE: secret should be the output of a KDF like bcrypt,
  80. // if it's derived from user input.
  81. func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
  82. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  83. privKeyBytes := new([64]byte)
  84. copy(privKeyBytes[:32], privKey32)
  85. ed25519.MakePublicKey(privKeyBytes)
  86. return PrivKeyEd25519(*privKeyBytes)
  87. }
  88. //-------------------------------------
  89. // Implements PrivKey
  90. type PrivKeySecp256k1 [32]byte
  91. func (privKey PrivKeySecp256k1) Bytes() []byte {
  92. return wire.BinaryBytes(struct{ PrivKey }{privKey})
  93. }
  94. func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
  95. priv__, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  96. sig__, err := priv__.Sign(Sha256(msg))
  97. if err != nil {
  98. PanicSanity(err)
  99. }
  100. return SignatureSecp256k1(sig__.Serialize())
  101. }
  102. func (privKey PrivKeySecp256k1) PubKey() PubKey {
  103. _, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  104. pub := [64]byte{}
  105. copy(pub[:], pub__.SerializeUncompressed()[1:])
  106. return PubKeySecp256k1(pub)
  107. }
  108. func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
  109. if otherSecp, ok := other.(PrivKeySecp256k1); ok {
  110. return bytes.Equal(privKey[:], otherSecp[:])
  111. } else {
  112. return false
  113. }
  114. }
  115. func (privKey PrivKeySecp256k1) String() string {
  116. return Fmt("PrivKeySecp256k1{*****}")
  117. }
  118. /*
  119. // Deterministically generates new priv-key bytes from key.
  120. func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
  121. newBytes := wire.BinarySha256(struct {
  122. PrivKey [64]byte
  123. Index int
  124. }{key, index})
  125. var newKey [64]byte
  126. copy(newKey[:], newBytes)
  127. return PrivKeySecp256k1(newKey)
  128. }
  129. */
  130. func GenPrivKeySecp256k1() PrivKeySecp256k1 {
  131. privKeyBytes := [32]byte{}
  132. copy(privKeyBytes[:], CRandBytes(32))
  133. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:])
  134. copy(privKeyBytes[:], priv.Serialize())
  135. return PrivKeySecp256k1(privKeyBytes)
  136. }
  137. // NOTE: secret should be the output of a KDF like bcrypt,
  138. // if it's derived from user input.
  139. func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 {
  140. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  141. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32)
  142. privKeyBytes := [32]byte{}
  143. copy(privKeyBytes[:], priv.Serialize())
  144. return PrivKeySecp256k1(privKeyBytes)
  145. }