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.

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