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.

167 lines
3.6 KiB

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
9 years ago
7 years ago
  1. package crypto
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/ed25519"
  8. data "github.com/tendermint/go-wire/data"
  9. )
  10. func TestSignAndValidateEd25519(t *testing.T) {
  11. privKey := GenPrivKeyEd25519()
  12. pubKey := privKey.PubKey()
  13. msg := CRandBytes(128)
  14. sig := privKey.Sign(msg)
  15. // Test the signature
  16. assert.True(t, pubKey.VerifyBytes(msg, sig))
  17. // Mutate the signature, just one bit.
  18. sigEd := sig.Unwrap().(SignatureEd25519)
  19. sigEd[7] ^= byte(0x01)
  20. sig = sigEd.Wrap()
  21. assert.False(t, pubKey.VerifyBytes(msg, sig))
  22. }
  23. func TestSignAndValidateSecp256k1(t *testing.T) {
  24. privKey := GenPrivKeySecp256k1()
  25. pubKey := privKey.PubKey()
  26. msg := CRandBytes(128)
  27. sig := privKey.Sign(msg)
  28. assert.True(t, pubKey.VerifyBytes(msg, sig))
  29. // Mutate the signature, just one bit.
  30. sigEd := sig.Unwrap().(SignatureSecp256k1)
  31. sigEd[3] ^= byte(0x01)
  32. sig = sigEd.Wrap()
  33. assert.False(t, pubKey.VerifyBytes(msg, sig))
  34. }
  35. func TestSignatureEncodings(t *testing.T) {
  36. cases := []struct {
  37. privKey PrivKey
  38. sigSize int
  39. sigType byte
  40. sigName string
  41. }{
  42. {
  43. privKey: GenPrivKeyEd25519().Wrap(),
  44. sigSize: ed25519.SignatureSize,
  45. sigType: TypeEd25519,
  46. sigName: NameEd25519,
  47. },
  48. {
  49. privKey: GenPrivKeySecp256k1().Wrap(),
  50. sigSize: 0, // unknown
  51. sigType: TypeSecp256k1,
  52. sigName: NameSecp256k1,
  53. },
  54. }
  55. for _, tc := range cases {
  56. // note we embed them from the beginning....
  57. pubKey := tc.privKey.PubKey()
  58. msg := CRandBytes(128)
  59. sig := tc.privKey.Sign(msg)
  60. // store as wire
  61. bin, err := data.ToWire(sig)
  62. require.Nil(t, err, "%+v", err)
  63. if tc.sigSize != 0 {
  64. assert.Equal(t, tc.sigSize+1, len(bin))
  65. }
  66. assert.Equal(t, tc.sigType, bin[0])
  67. // and back
  68. sig2 := Signature{}
  69. err = data.FromWire(bin, &sig2)
  70. require.Nil(t, err, "%+v", err)
  71. assert.EqualValues(t, sig, sig2)
  72. assert.True(t, pubKey.VerifyBytes(msg, sig2))
  73. // store as json
  74. js, err := data.ToJSON(sig)
  75. require.Nil(t, err, "%+v", err)
  76. assert.True(t, strings.Contains(string(js), tc.sigName))
  77. // and back
  78. sig3 := Signature{}
  79. err = data.FromJSON(js, &sig3)
  80. require.Nil(t, err, "%+v", err)
  81. assert.EqualValues(t, sig, sig3)
  82. assert.True(t, pubKey.VerifyBytes(msg, sig3))
  83. // and make sure we can textify it
  84. text, err := data.ToText(sig)
  85. require.Nil(t, err, "%+v", err)
  86. assert.True(t, strings.HasPrefix(text, tc.sigName))
  87. }
  88. }
  89. func TestWrapping(t *testing.T) {
  90. assert := assert.New(t)
  91. // construct some basic constructs
  92. msg := CRandBytes(128)
  93. priv := GenPrivKeyEd25519()
  94. pub := priv.PubKey()
  95. sig := priv.Sign(msg)
  96. // do some wrapping
  97. pubs := []PubKey{
  98. PubKey{nil},
  99. pub.Wrap(),
  100. pub.Wrap().Wrap().Wrap(),
  101. PubKey{PubKey{PubKey{pub}}}.Wrap(),
  102. }
  103. for _, p := range pubs {
  104. _, ok := p.PubKeyInner.(PubKey)
  105. assert.False(ok)
  106. }
  107. sigs := []Signature{
  108. Signature{nil},
  109. sig.Wrap(),
  110. sig.Wrap().Wrap().Wrap(),
  111. Signature{Signature{Signature{sig}}}.Wrap(),
  112. }
  113. for _, s := range sigs {
  114. _, ok := s.SignatureInner.(Signature)
  115. assert.False(ok)
  116. }
  117. }
  118. func TestPrivKeyEquality(t *testing.T) {
  119. {
  120. privKey := GenPrivKeySecp256k1().Wrap()
  121. privKey2 := GenPrivKeySecp256k1().Wrap()
  122. assert.False(t, privKey.Equals(privKey2))
  123. assert.False(t, privKey2.Equals(privKey))
  124. privKeyCopy := privKey // copy
  125. assert.True(t, privKey.Equals(privKeyCopy))
  126. assert.True(t, privKeyCopy.Equals(privKey))
  127. }
  128. {
  129. privKey := GenPrivKeyEd25519().Wrap()
  130. privKey2 := GenPrivKeyEd25519().Wrap()
  131. assert.False(t, privKey.Equals(privKey2))
  132. assert.False(t, privKey2.Equals(privKey))
  133. privKeyCopy := privKey // copy
  134. assert.True(t, privKey.Equals(privKeyCopy))
  135. assert.True(t, privKeyCopy.Equals(privKey))
  136. }
  137. }