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.

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