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.

184 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.UnmarshalBinary(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.MarshalBinary(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.MarshalBinary(struct {
  64. PrivKey [64]byte
  65. Index int
  66. }{privKey, index})
  67. if err != nil {
  68. panic(err)
  69. }
  70. newBytes := Sha256(bz)
  71. var newKey [64]byte
  72. copy(newKey[:], newBytes)
  73. return PrivKeyEd25519(newKey)
  74. }
  75. func GenPrivKeyEd25519() PrivKeyEd25519 {
  76. privKeyBytes := new([64]byte)
  77. copy(privKeyBytes[:32], CRandBytes(32))
  78. ed25519.MakePublicKey(privKeyBytes)
  79. return PrivKeyEd25519(*privKeyBytes)
  80. }
  81. // NOTE: secret should be the output of a KDF like bcrypt,
  82. // if it's derived from user input.
  83. func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
  84. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  85. privKeyBytes := new([64]byte)
  86. copy(privKeyBytes[:32], privKey32)
  87. ed25519.MakePublicKey(privKeyBytes)
  88. return PrivKeyEd25519(*privKeyBytes)
  89. }
  90. //-------------------------------------
  91. var _ PrivKey = PrivKeySecp256k1{}
  92. // Implements PrivKey
  93. type PrivKeySecp256k1 [32]byte
  94. func (privKey PrivKeySecp256k1) Bytes() []byte {
  95. bz, err := cdc.MarshalBinary(privKey)
  96. if err != nil {
  97. panic(err)
  98. }
  99. return bz
  100. }
  101. func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
  102. priv__, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  103. sig__, err := priv__.Sign(Sha256(msg))
  104. if err != nil {
  105. PanicSanity(err)
  106. }
  107. return SignatureSecp256k1(sig__.Serialize())
  108. }
  109. func (privKey PrivKeySecp256k1) PubKey() PubKey {
  110. _, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
  111. var pub PubKeySecp256k1
  112. copy(pub[:], pub__.SerializeCompressed())
  113. return pub
  114. }
  115. // Equals - you probably don't need to use this.
  116. // Runs in constant time based on length of the keys.
  117. func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
  118. if otherSecp, ok := other.(PrivKeySecp256k1); ok {
  119. return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 1
  120. } else {
  121. return false
  122. }
  123. }
  124. /*
  125. func (privKey PrivKeySecp256k1) String() string {
  126. return Fmt("PrivKeySecp256k1{*****}")
  127. }
  128. */
  129. /*
  130. // Deterministically generates new priv-key bytes from key.
  131. func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
  132. newBytes := cdc.BinarySha256(struct {
  133. PrivKey [64]byte
  134. Index int
  135. }{key, index})
  136. var newKey [64]byte
  137. copy(newKey[:], newBytes)
  138. return PrivKeySecp256k1(newKey)
  139. }
  140. */
  141. func GenPrivKeySecp256k1() PrivKeySecp256k1 {
  142. privKeyBytes := [32]byte{}
  143. copy(privKeyBytes[:], CRandBytes(32))
  144. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:])
  145. copy(privKeyBytes[:], priv.Serialize())
  146. return PrivKeySecp256k1(privKeyBytes)
  147. }
  148. // NOTE: secret should be the output of a KDF like bcrypt,
  149. // if it's derived from user input.
  150. func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 {
  151. privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
  152. priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32)
  153. privKeyBytes := [32]byte{}
  154. copy(privKeyBytes[:], priv.Serialize())
  155. return PrivKeySecp256k1(privKeyBytes)
  156. }