From 74878ee3130cff56dcd096a69ed7a3b97e7d233f Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 12 Sep 2017 09:50:07 +0200 Subject: [PATCH] Update code to work with current nano implementation --- nano/sign.go | 9 ++++++++- nano/sign_test.go | 19 ++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/nano/sign.go b/nano/sign.go index b98b7f469..50b874d15 100644 --- a/nano/sign.go +++ b/nano/sign.go @@ -2,9 +2,11 @@ package nano import ( "bytes" + "crypto/sha512" "fmt" "github.com/pkg/errors" + crypto "github.com/tendermint/go-crypto" ) @@ -14,7 +16,7 @@ const ( Update = 0x01 Digest = 0x02 MaxChunk = 253 - KeyLength = 65 + KeyLength = 32 SigLength = 64 ) @@ -76,3 +78,8 @@ func parseSig(data []byte) (key crypto.Signature, err error) { copy(ed[:], data) return ed.Wrap(), nil } + +func hashMsg(data []byte) []byte { + res := sha512.Sum512(data) + return res[:] +} diff --git a/nano/sign_test.go b/nano/sign_test.go index be1e66c86..2f94c5e6f 100644 --- a/nano/sign_test.go +++ b/nano/sign_test.go @@ -18,9 +18,9 @@ func TestParseDigest(t *testing.T) { valid bool }{ { - output: "800204338EB1DD3CCDEE1F6FB586F66E640F56FFDD14537A3F0ED9EEEDF10B528FE4195FD17AC9EDAE9718A50196A1459E2434C1E53F1238F4CFDF177FAFBA8B39249B00CAFE00FFDEA42A699205B217004E7E2FFB884E174A548D644116F4B20469CBC32F60A9CB0EEB5BB6A7F266BD0F6A0A99A45B4F18F0F477AED7C854C404EF43530DAB00", - key: "04338EB1DD3CCDEE1F6FB586F66E640F56FFDD14537A3F0ED9EEEDF10B528FE4195FD17AC9EDAE9718A50196A1459E2434C1E53F1238F4CFDF177FAFBA8B39249B", - sig: "FFDEA42A699205B217004E7E2FFB884E174A548D644116F4B20469CBC32F60A9CB0EEB5BB6A7F266BD0F6A0A99A45B4F18F0F477AED7C854C404EF43530DAB00", + output: "80028E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C9300CAFE00787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400", + key: "8E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C93", + sig: "787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400", valid: true, }, { @@ -75,9 +75,9 @@ func TestCryptoConvert(t *testing.T) { cases := []cryptoCase{ { - msg: "00", - key: "04338EB1DD3CCDEE1F6FB586F66E640F56FFDD14537A3F0ED9EEEDF10B528FE4195FD17AC9EDAE9718A50196A1459E2434C1E53F1238F4CFDF177FAFBA8B39249B", - sig: "FFDEA42A699205B217004E7E2FFB884E174A548D644116F4B20469CBC32F60A9CB0EEB5BB6A7F266BD0F6A0A99A45B4F18F0F477AED7C854C404EF43530DAB00", + msg: "F00D", + key: "8E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C93", + sig: "787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400", valid: true, }, } @@ -91,8 +91,13 @@ func TestCryptoConvert(t *testing.T) { psig, err := parseSig(sig) require.Nil(err, "%d: %+v", i, err) - // how do i make this valid? + // it is not the signature of the message itself valid := pk.VerifyBytes(msg, psig) + assert.NotEqual(tc.valid, valid, "%d", i) + + // but rather of the hash of the msg + hmsg := hashMsg(msg) + valid = pk.VerifyBytes(hmsg, psig) assert.Equal(tc.valid, valid, "%d", i) } }