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.

143 lines
3.1 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
  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-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.(SignatureEd25519)
  19. sigEd[0] ^= byte(0x01)
  20. sig = Signature(sigEd)
  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.(SignatureSecp256k1)
  31. sigEd[0] ^= byte(0x01)
  32. sig = Signature(sigEd)
  33. assert.False(t, pubKey.VerifyBytes(msg, sig))
  34. }
  35. func TestSignatureEncodings(t *testing.T) {
  36. cases := []struct {
  37. privKey PrivKeyS
  38. sigSize int
  39. sigType byte
  40. sigName string
  41. }{
  42. {
  43. privKey: PrivKeyS{GenPrivKeyEd25519()},
  44. sigSize: ed25519.SignatureSize,
  45. sigType: TypeEd25519,
  46. sigName: NameEd25519,
  47. },
  48. {
  49. privKey: PrivKeyS{GenPrivKeySecp256k1()},
  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 := PubKeyS{tc.privKey.PubKey()}
  58. msg := CRandBytes(128)
  59. sig := SignatureS{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 := SignatureS{}
  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 := SignatureS{}
  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 := []PubKeyS{
  98. WrapPubKey(nil),
  99. WrapPubKey(pub),
  100. WrapPubKey(WrapPubKey(WrapPubKey(WrapPubKey(pub)))),
  101. WrapPubKey(PubKeyS{PubKeyS{PubKeyS{pub}}}),
  102. }
  103. for _, p := range pubs {
  104. _, ok := p.PubKey.(PubKeyS)
  105. assert.False(ok)
  106. }
  107. sigs := []SignatureS{
  108. WrapSignature(nil),
  109. WrapSignature(sig),
  110. WrapSignature(WrapSignature(WrapSignature(WrapSignature(sig)))),
  111. WrapSignature(SignatureS{SignatureS{SignatureS{sig}}}),
  112. }
  113. for _, s := range sigs {
  114. _, ok := s.Signature.(SignatureS)
  115. assert.False(ok)
  116. }
  117. }