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.

142 lines
4.2 KiB

  1. package nano
  2. import (
  3. "encoding/hex"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. crypto "github.com/tendermint/go-crypto"
  9. )
  10. func TestLedgerKeys(t *testing.T) {
  11. assert, require := assert.New(t), require.New(t)
  12. cases := []struct {
  13. msg, pubkey, sig string
  14. valid bool
  15. }{
  16. 0: {
  17. msg: "F00D",
  18. pubkey: "8E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C93",
  19. sig: "787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400",
  20. valid: true,
  21. },
  22. 1: {
  23. msg: "DEADBEEF",
  24. pubkey: "0C45ADC887A5463F668533443C829ED13EA8E2E890C778957DC28DB9D2AD5A6C",
  25. sig: "00ED74EED8FDAC7988A14BF6BC222120CBAC249D569AF4C2ADABFC86B792F97DF73C4919BE4B6B0ACB53547273BF29FBF0A9E0992FFAB6CB6C9B09311FC86A00",
  26. valid: true,
  27. },
  28. 2: {
  29. msg: "1234567890AA",
  30. pubkey: "598FC1F0C76363D14D7480736DEEF390D85863360F075792A6975EFA149FD7EA",
  31. sig: "59AAB7D7BDC4F936B6415DE672A8B77FA6B8B3451CD95B3A631F31F9A05DAEEE5E7E4F89B64DDEBB5F63DC042CA13B8FCB8185F82AD7FD5636FFDA6B0DC9570B",
  32. valid: true,
  33. },
  34. 3: {
  35. msg: "1234432112344321",
  36. pubkey: "359E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
  37. sig: "616B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
  38. valid: true,
  39. },
  40. 4: {
  41. msg: "12344321123443",
  42. pubkey: "359E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
  43. sig: "616B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
  44. valid: false,
  45. },
  46. 5: {
  47. msg: "1234432112344321",
  48. pubkey: "459E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
  49. sig: "616B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
  50. valid: false,
  51. },
  52. 6: {
  53. msg: "1234432112344321",
  54. pubkey: "359E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
  55. sig: "716B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
  56. valid: false,
  57. },
  58. }
  59. for i, tc := range cases {
  60. bmsg, err := hex.DecodeString(tc.msg)
  61. require.NoError(err, "%d", i)
  62. priv := NewMockKey(tc.msg, tc.pubkey, tc.sig)
  63. pub := priv.PubKey()
  64. sig := priv.Sign(bmsg)
  65. valid := pub.VerifyBytes(bmsg, sig)
  66. assert.Equal(tc.valid, valid, "%d", i)
  67. }
  68. }
  69. func TestRealLedger(t *testing.T) {
  70. assert, require := assert.New(t), require.New(t)
  71. if os.Getenv("WITH_LEDGER") == "" {
  72. t.Skip("Set WITH_LEDGER to run code on real ledger")
  73. }
  74. msg := []byte("kuhehfeohg")
  75. priv, err := NewPrivKeyLedgerEd25519Ed25519()
  76. require.Nil(err, "%+v", err)
  77. pub := priv.PubKey()
  78. sig := priv.Sign(msg)
  79. valid := pub.VerifyBytes(msg, sig)
  80. assert.True(valid)
  81. // now, let's serialize the key and make sure it still works
  82. bs := priv.Bytes()
  83. priv2, err := crypto.PrivKeyFromBytes(bs)
  84. require.Nil(err, "%+v", err)
  85. // make sure we get the same pubkey when we load from disk
  86. pub2 := priv2.PubKey()
  87. require.Equal(pub, pub2)
  88. // signing with the loaded key should match the original pubkey
  89. sig = priv2.Sign(msg)
  90. valid = pub.VerifyBytes(msg, sig)
  91. assert.True(valid)
  92. // make sure pubkeys serialize properly as well
  93. bs = pub.Bytes()
  94. bpub, err := crypto.PubKeyFromBytes(bs)
  95. require.NoError(err)
  96. assert.Equal(pub, bpub)
  97. }
  98. // TestRealLedgerErrorHandling calls. These tests assume
  99. // the ledger is not plugged in....
  100. func TestRealLedgerErrorHandling(t *testing.T) {
  101. require := require.New(t)
  102. if os.Getenv("WITH_LEDGER") != "" {
  103. t.Skip("Skipping on WITH_LEDGER as it tests unplugged cases")
  104. }
  105. // first, try to generate a key, must return an error
  106. // (no panic)
  107. _, err := NewPrivKeyLedgerEd25519Ed25519()
  108. require.Error(err)
  109. led := PrivKeyLedgerEd25519{} // empty
  110. // or with some pub key
  111. ed := crypto.GenPrivKeyEd25519()
  112. led2 := PrivKeyLedgerEd25519{CachedPubKey: ed.PubKey()}
  113. // loading these should return errors
  114. bs := led.Bytes()
  115. _, err = crypto.PrivKeyFromBytes(bs)
  116. require.Error(err)
  117. bs = led2.Bytes()
  118. _, err = crypto.PrivKeyFromBytes(bs)
  119. require.Error(err)
  120. }