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.

164 lines
4.5 KiB

  1. package crypto
  2. import (
  3. "crypto/subtle"
  4. secp256k1 "github.com/btcsuite/btcd/btcec"
  5. "github.com/tendermint/ed25519"
  6. "github.com/tendermint/ed25519/extra25519"
  7. )
  8. func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
  9. err = cdc.UnmarshalBinaryBare(privKeyBytes, &privKey)
  10. return
  11. }
  12. //----------------------------------------
  13. type PrivKey interface {
  14. Bytes() []byte
  15. Sign(msg []byte) (Signature, error)
  16. PubKey() PubKey
  17. Equals(PrivKey) bool
  18. }
  19. //-------------------------------------
  20. var _ PrivKey = PrivKeyEd25519{}
  21. // Implements PrivKey
  22. type PrivKeyEd25519 [64]byte
  23. func (privKey PrivKeyEd25519) Bytes() []byte {
  24. return cdc.MustMarshalBinaryBare(privKey)
  25. }
  26. func (privKey PrivKeyEd25519) Sign(msg []byte) (Signature, error) {
  27. privKeyBytes := [64]byte(privKey)
  28. signatureBytes := ed25519.Sign(&privKeyBytes, msg)
  29. return SignatureEd25519(*signatureBytes), nil
  30. }
  31. func (privKey PrivKeyEd25519) PubKey() PubKey {
  32. privKeyBytes := [64]byte(privKey)
  33. pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
  34. return PubKeyEd25519(pubBytes)
  35. }
  36. // Equals - you probably don't need to use this.
  37. // Runs in constant time based on length of the keys.
  38. func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
  39. if otherEd, ok := other.(PrivKeyEd25519); ok {
  40. return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1
  41. } else {
  42. return false
  43. }
  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. // Deterministically generates new priv-key bytes from key.
  52. func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
  53. bz, err := cdc.MarshalBinaryBare(struct {
  54. PrivKey [64]byte
  55. Index int
  56. }{privKey, index})
  57. if err != nil {
  58. panic(err)
  59. }
  60. newBytes := Sha256(bz)
  61. newKey := new([64]byte)
  62. copy(newKey[:32], newBytes)
  63. ed25519.MakePublicKey(newKey)
  64. return PrivKeyEd25519(*newKey)
  65. }
  66. func GenPrivKeyEd25519() PrivKeyEd25519 {
  67. privKeyBytes := new([64]byte)
  68. copy(privKeyBytes[:32], CRandBytes(32))
  69. ed25519.MakePublicKey(privKeyBytes)
  70. return PrivKeyEd25519(*privKeyBytes)
  71. }
  72. // NOTE: secret should be the output of a KDF like bcrypt,
  73. // if it's derived from user input.
  74. func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
  75. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  76. privKeyBytes := new([64]byte)
  77. copy(privKeyBytes[:32], privKey32)
  78. ed25519.MakePublicKey(privKeyBytes)
  79. return PrivKeyEd25519(*privKeyBytes)
  80. }
  81. //-------------------------------------
  82. var _ PrivKey = PrivKeySecp256k1{}
  83. // Implements PrivKey
  84. type PrivKeySecp256k1 [32]byte
  85. func (privKey PrivKeySecp256k1) Bytes() []byte {
  86. return cdc.MustMarshalBinaryBare(privKey)
  87. }
  88. func (privKey PrivKeySecp256k1) Sign(msg []byte) (Signature, error) {
  89. priv__, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  90. sig__, err := priv__.Sign(Sha256(msg))
  91. if err != nil {
  92. return nil, err
  93. }
  94. return SignatureSecp256k1(sig__.Serialize()), nil
  95. }
  96. func (privKey PrivKeySecp256k1) PubKey() PubKey {
  97. _, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  98. var pub PubKeySecp256k1
  99. copy(pub[:], pub__.SerializeCompressed())
  100. return pub
  101. }
  102. // Equals - you probably don't need to use this.
  103. // Runs in constant time based on length of the keys.
  104. func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
  105. if otherSecp, ok := other.(PrivKeySecp256k1); ok {
  106. return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 1
  107. } else {
  108. return false
  109. }
  110. }
  111. /*
  112. // Deterministically generates new priv-key bytes from key.
  113. func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
  114. newBytes := cdc.BinarySha256(struct {
  115. PrivKey [64]byte
  116. Index int
  117. }{key, index})
  118. var newKey [64]byte
  119. copy(newKey[:], newBytes)
  120. return PrivKeySecp256k1(newKey)
  121. }
  122. */
  123. func GenPrivKeySecp256k1() PrivKeySecp256k1 {
  124. privKeyBytes := [32]byte{}
  125. copy(privKeyBytes[:], CRandBytes(32))
  126. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:])
  127. copy(privKeyBytes[:], priv.Serialize())
  128. return PrivKeySecp256k1(privKeyBytes)
  129. }
  130. // NOTE: secret should be the output of a KDF like bcrypt,
  131. // if it's derived from user input.
  132. func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 {
  133. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  134. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32)
  135. privKeyBytes := [32]byte{}
  136. copy(privKeyBytes[:], priv.Serialize())
  137. return PrivKeySecp256k1(privKeyBytes)
  138. }