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.

159 lines
4.7 KiB

  1. package nano
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. "github.com/pkg/errors"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. crypto "github.com/tendermint/go-crypto"
  9. )
  10. func parseEdKey(data []byte) (key crypto.PubKey, err error) {
  11. ed := crypto.PubKeyEd25519{}
  12. if len(data) < len(ed) {
  13. return key, errors.Errorf("Key length too short: %d", len(data))
  14. }
  15. copy(ed[:], data)
  16. return ed.Wrap(), nil
  17. }
  18. func parseSig(data []byte) (key crypto.Signature, err error) {
  19. ed := crypto.SignatureEd25519{}
  20. if len(data) < len(ed) {
  21. return key, errors.Errorf("Sig length too short: %d", len(data))
  22. }
  23. copy(ed[:], data)
  24. return ed.Wrap(), nil
  25. }
  26. func TestParseDigest(t *testing.T) {
  27. assert, require := assert.New(t), require.New(t)
  28. cases := []struct {
  29. output string
  30. key string
  31. sig string
  32. valid bool
  33. }{
  34. {
  35. output: "80028E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C9300CAFE00787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400",
  36. key: "8E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C93",
  37. sig: "787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400",
  38. valid: true,
  39. },
  40. {
  41. output: "800235467890876543525437890796574535467890",
  42. key: "",
  43. sig: "",
  44. valid: false,
  45. },
  46. }
  47. for i, tc := range cases {
  48. msg, err := hex.DecodeString(tc.output)
  49. require.Nil(err, "%d: %+v", i, err)
  50. lKey, lSig, err := parseDigest(msg)
  51. if !tc.valid {
  52. assert.NotNil(err, "%d", i)
  53. } else if assert.Nil(err, "%d: %+v", i, err) {
  54. key, err := hex.DecodeString(tc.key)
  55. require.Nil(err, "%d: %+v", i, err)
  56. sig, err := hex.DecodeString(tc.sig)
  57. require.Nil(err, "%d: %+v", i, err)
  58. assert.Equal(key, lKey, "%d", i)
  59. assert.Equal(sig, lSig, "%d", i)
  60. }
  61. }
  62. }
  63. type cryptoCase struct {
  64. msg string
  65. key string
  66. sig string
  67. valid bool
  68. }
  69. func toBytes(c cryptoCase) (msg, key, sig []byte, err error) {
  70. msg, err = hex.DecodeString(c.msg)
  71. if err != nil {
  72. return
  73. }
  74. key, err = hex.DecodeString(c.key)
  75. if err != nil {
  76. return
  77. }
  78. sig, err = hex.DecodeString(c.sig)
  79. return
  80. }
  81. func TestCryptoConvert(t *testing.T) {
  82. assert, require := assert.New(t), require.New(t)
  83. cases := []cryptoCase{
  84. 0: {
  85. msg: "F00D",
  86. key: "8E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C93",
  87. sig: "787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400",
  88. valid: true,
  89. },
  90. 1: {
  91. msg: "DEADBEEF",
  92. key: "0C45ADC887A5463F668533443C829ED13EA8E2E890C778957DC28DB9D2AD5A6C",
  93. sig: "00ED74EED8FDAC7988A14BF6BC222120CBAC249D569AF4C2ADABFC86B792F97DF73C4919BE4B6B0ACB53547273BF29FBF0A9E0992FFAB6CB6C9B09311FC86A00",
  94. valid: true,
  95. },
  96. 2: {
  97. msg: "1234567890AA",
  98. key: "598FC1F0C76363D14D7480736DEEF390D85863360F075792A6975EFA149FD7EA",
  99. sig: "59AAB7D7BDC4F936B6415DE672A8B77FA6B8B3451CD95B3A631F31F9A05DAEEE5E7E4F89B64DDEBB5F63DC042CA13B8FCB8185F82AD7FD5636FFDA6B0DC9570B",
  100. valid: true,
  101. },
  102. 3: {
  103. msg: "1234432112344321",
  104. key: "359E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
  105. sig: "616B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
  106. valid: true,
  107. },
  108. 4: {
  109. msg: "12344321123443",
  110. key: "359E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
  111. sig: "616B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
  112. valid: false,
  113. },
  114. 5: {
  115. msg: "1234432112344321",
  116. key: "459E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
  117. sig: "616B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
  118. valid: false,
  119. },
  120. 6: {
  121. msg: "1234432112344321",
  122. key: "359E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
  123. sig: "716B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
  124. valid: false,
  125. },
  126. }
  127. for i, tc := range cases {
  128. msg, key, sig, err := toBytes(tc)
  129. require.Nil(err, "%d: %+v", i, err)
  130. pk, err := parseEdKey(key)
  131. require.Nil(err, "%d: %+v", i, err)
  132. psig, err := parseSig(sig)
  133. require.Nil(err, "%d: %+v", i, err)
  134. // it is not the signature of the message itself
  135. valid := pk.VerifyBytes(msg, psig)
  136. assert.False(valid, "%d", i)
  137. // but rather of the hash of the msg
  138. hmsg := hashMsg(msg)
  139. valid = pk.VerifyBytes(hmsg, psig)
  140. assert.Equal(tc.valid, valid, "%d", i)
  141. }
  142. }