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.

109 lines
2.4 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: SignatureTypeEd25519,
  46. sigName: SignatureNameEd25519,
  47. },
  48. {
  49. privKey: PrivKeyS{GenPrivKeySecp256k1()},
  50. sigSize: 0, // unknown
  51. sigType: SignatureTypeSecp256k1,
  52. sigName: SignatureNameSecp256k1,
  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. }