Repository: scionproto/scion
Pull request #3529 (2019-12-16 14:13:09)

infra: Refactor signer/verifier
Use `[]byte` instead of `common.RawBytes`

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/scionproto/scion/3529)
<!-- Reviewable:end -->



Patch:
diff --git a/go/lib/ctrl/path_mgmt/rev_info.go b/go/lib/ctrl/path_mgmt/rev_info.go
index 8d1016a13..fc29e58c3 100644
--- a/go/lib/ctrl/path_mgmt/rev_info.go
+++ b/go/lib/ctrl/path_mgmt/rev_info.go
@@ -141,14 +141,14 @@ func (r *RevInfo) SameIntf(other *RevInfo) bool {
 
 // Signer is used to sign raw bytes.
 type Signer interface {
-	Sign(msg common.RawBytes) (*proto.SignS, error)
+	Sign(msg []byte) (*proto.SignS, error)
 }
 
 // Verifier is used to verify signatures.
 type Verifier interface {
 	// Verify verifies the packed signed revocation based on the signature meta
 	// data.
-	Verify(ctx context.Context, msg common.RawBytes, sign *proto.SignS) error
+	Verify(ctx context.Context, msg []byte, sign *proto.SignS) error
 }
 
 var _ proto.Cerealizable = (*SignedRevInfo)(nil)
diff --git a/go/lib/ctrl/seg/mock_seg/BUILD.bazel b/go/lib/ctrl/seg/mock_seg/BUILD.bazel
index 641a467b3..a1969198a 100644
--- a/go/lib/ctrl/seg/mock_seg/BUILD.bazel
+++ b/go/lib/ctrl/seg/mock_seg/BUILD.bazel
@@ -6,7 +6,6 @@ go_library(
     importpath = "github.com/scionproto/scion/go/lib/ctrl/seg/mock_seg",
     visibility = ["//visibility:public"],
     deps = [
-        "//go/lib/common:go_default_library",
         "//go/proto:go_default_library",
         "@com_github_golang_mock//gomock:go_default_library",
     ],
diff --git a/go/lib/ctrl/seg/mock_seg/seg.go b/go/lib/ctrl/seg/mock_seg/seg.go
index a6f6ea0ca..ea763ec89 100644
--- a/go/lib/ctrl/seg/mock_seg/seg.go
+++ b/go/lib/ctrl/seg/mock_seg/seg.go
@@ -6,7 +6,6 @@ package mock_seg
 
 import (
 	gomock "github.com/golang/mock/gomock"
-	common "github.com/scionproto/scion/go/lib/common"
 	proto "github.com/scionproto/scion/go/proto"
 	reflect "reflect"
 )
@@ -35,7 +34,7 @@ func (m *MockSigner) EXPECT() *MockSignerMockRecorder {
 }
 
 // Sign mocks base method
-func (m *MockSigner) Sign(arg0 common.RawBytes) (*proto.SignS, error) {
+func (m *MockSigner) Sign(arg0 []byte) (*proto.SignS, error) {
 	m.ctrl.T.Helper()
 	ret := m.ctrl.Call(m, "Sign", arg0)
 	ret0, _ := ret[0].(*proto.SignS)
diff --git a/go/lib/ctrl/seg/seg.go b/go/lib/ctrl/seg/seg.go
index 2d37c6ead..e6631e96f 100644
--- a/go/lib/ctrl/seg/seg.go
+++ b/go/lib/ctrl/seg/seg.go
@@ -37,13 +37,13 @@ import (
 // Signer signs path segments.
 type Signer interface {
 	// Sign signs the packed segment and returns the signature meta data.
-	Sign(packedSegment common.RawBytes) (*proto.SignS, error)
+	Sign(packedSegment []byte) (*proto.SignS, error)
 }
 
 // Verifier verifies path segments.
 type Verifier interface {
 	// Verify verifies the packed segment based on the signature meta data.
-	Verify(ctx context.Context, packedSegment common.RawBytes, sign *proto.SignS) error
+	Verify(ctx context.Context, packedSegment []byte, sign *proto.SignS) error
 }
 
 var _ proto.Cerealizable = (*Beacon)(nil)
diff --git a/go/lib/ctrl/signed_util.go b/go/lib/ctrl/signed_util.go
index 0519a335b..9ec17581f 100644
--- a/go/lib/ctrl/signed_util.go
+++ b/go/lib/ctrl/signed_util.go
@@ -27,7 +27,7 @@ import (
 
 // Signer takes a message and signs it, producing the signature metadata.
 type Signer interface {
-	Sign(msg common.RawBytes) (*proto.SignS, error)
+	Sign(msg []byte) (*proto.SignS, error)
 }
 
 // Verifier verifies the signature of a signed payload.
diff --git a/go/lib/infra/common.go b/go/lib/infra/common.go
index b059dfc31..e8a318e55 100644
--- a/go/lib/infra/common.go
+++ b/go/lib/infra/common.go
@@ -427,7 +427,7 @@ type SignatureTimestampRange struct {
 // certificates.
 type Verifier interface {
 	ctrl.Verifier
-	Verify(ctx context.Context, msg common.RawBytes, sign *proto.SignS) error
+	Verify(ctx context.Context, msg []byte, sign *proto.SignS) error
 	// WithServer returns a verifier that fetches the necessary crypto
 	// objects from the specified server.
 	WithServer(server net.Addr) Verifier
@@ -556,7 +556,7 @@ var _ Signer = nullSigner{}
 
 type nullSigner struct{}
 
-func (nullSigner) Sign(raw common.RawBytes) (*proto.SignS, error) {
+func (nullSigner) Sign(raw []byte) (*proto.SignS, error) {
 	return &proto.SignS{}, nil
 }
 
@@ -568,7 +568,7 @@ var _ Verifier = nullSigVerifier{}
 
 type nullSigVerifier struct{}
 
-func (nullSigVerifier) Verify(_ context.Context, _ common.RawBytes, _ *proto.SignS) error {
+func (nullSigVerifier) Verify(_ context.Context, _ []byte, _ *proto.SignS) error {
 	return nil
 }
 
diff --git a/go/lib/infra/mock_infra/infra.go b/go/lib/infra/mock_infra/infra.go
index 2c3239f89..76b35e358 100644
--- a/go/lib/infra/mock_infra/infra.go
+++ b/go/lib/infra/mock_infra/infra.go
@@ -772,7 +772,7 @@ func (m *MockVerifier) EXPECT() *MockVerifierMockRecorder {
 }
 
 // Verify mocks base method
-func (m *MockVerifier) Verify(arg0 context.Context, arg1 common.RawBytes, arg2 *proto.SignS) error {
+func (m *MockVerifier) Verify(arg0 context.Context, arg1 []byte, arg2 *proto.SignS) error {
 	m.ctrl.T.Helper()
 	ret := m.ctrl.Call(m, "Verify", arg0, arg1, arg2)
 	ret0, _ := ret[0].(error)
diff --git a/go/lib/infra/modules/trust/signhelper.go b/go/lib/infra/modules/trust/signhelper.go
index 81b331dbf..ad99768b2 100644
--- a/go/lib/infra/modules/trust/signhelper.go
+++ b/go/lib/infra/modules/trust/signhelper.go
@@ -45,13 +45,13 @@ var _ infra.Signer = (*BasicSigner)(nil)
 type BasicSigner struct {
 	meta      infra.SignerMeta
 	signType  proto.SignType
-	packedSrc common.RawBytes
-	key       common.RawBytes
+	packedSrc []byte
+	key       []byte
 }
 
 // NewBasicSigner creates a Signer that uses the supplied meta to sign
 // messages.
-func NewBasicSigner(key common.RawBytes, meta infra.SignerMeta) (*BasicSigner, error) {
+func NewBasicSigner(key []byte, meta infra.SignerMeta) (*BasicSigner, error) {
 	if meta.Src.IA.IsWildcard() {
 		return nil, common.NewBasicError("IA must not contain wildcard", nil, "ia", meta.Src.IA)
 	}
@@ -76,9 +76,9 @@ func NewBasicSigner(key common.RawBytes, meta infra.SignerMeta) (*BasicSigner, e
 }
 
 // Sign signs the message.
-func (b *BasicSigner) Sign(msg common.RawBytes) (*proto.SignS, error) {
+func (b *BasicSigner) Sign(msg []byte) (*proto.SignS, error) {
 	var err error
-	sign := proto.NewSignS(b.signType, append(common.RawBytes(nil), b.packedSrc...))
+	sign := proto.NewSignS(b.signType, append([]byte(nil), b.packedSrc...))
 	sign.Signature, err = scrypto.Sign(sign.SigInput(msg, true), b.key, b.meta.Algo)
 	return sign, err
 }
@@ -147,7 +147,7 @@ func (v *BasicVerifier) WithSignatureTimestampRange(
 }
 
 // Verify verifies the message based on the provided sign meta data.
-func (v *BasicVerifier) Verify(ctx context.Context, msg common.RawBytes, sign *proto.SignS) error {
+func (v *BasicVerifier) Verify(ctx context.Context, msg []byte, sign *proto.SignS) error {
 	if err := v.sanityChecks(sign, false); err != nil {
 		return err
 	}
@@ -221,7 +221,7 @@ func (v *BasicVerifier) sanityChecks(sign *proto.SignS, isPldSignature bool) err
 	return nil
 }
 
-func (v *BasicVerifier) verify(ctx context.Context, msg common.RawBytes,
+func (v *BasicVerifier) verify(ctx context.Context, msg []byte,
 	sign *proto.SignS) error {
 
 	var err error
diff --git a/go/lib/infra/modules/trust/v2/mock_v2/v2.go b/go/lib/infra/modules/trust/v2/mock_v2/v2.go
index 43ec2ee0f..d0b20dc3b 100644
--- a/go/lib/infra/modules/trust/v2/mock_v2/v2.go
+++ b/go/lib/infra/modules/trust/v2/mock_v2/v2.go
@@ -611,15 +611,3 @@ func (mr *MockRPCMockRecorder) SendTRC(arg0, arg1, arg2 interface{}) *gomock.Cal
 	mr.mock.ctrl.T.Helper()
 	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendTRC", reflect.TypeOf((*MockRPC)(nil).SendTRC), arg0, arg1, arg2)
 }
-
-// SetMsgr mocks base method
-func (m *MockRPC) SetMsgr(arg0 infra.Messenger) {
-	m.ctrl.T.Helper()
-	m.ctrl.Call(m, "SetMsgr", arg0)
-}
-
-// SetMsgr indicates an expected call of SetMsgr
-func (mr *MockRPCMockRecorder) SetMsgr(arg0 interface{}) *gomock.Call {
-	mr.mock.ctrl.T.Helper()
-	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetMsgr", reflect.TypeOf((*MockRPC)(nil).SetMsgr), arg0)
-}
diff --git a/go/lib/infra/modules/trust/v2/verifier.go b/go/lib/infra/modules/trust/v2/verifier.go
index 2d484e9ee..f11fc6449 100644
--- a/go/lib/infra/modules/trust/v2/verifier.go
+++ b/go/lib/infra/modules/trust/v2/verifier.go
@@ -20,7 +20,6 @@ import (
 	"time"
 
 	"github.com/scionproto/scion/go/lib/addr"
-	"github.com/scionproto/scion/go/lib/common"
 	"github.com/scionproto/scion/go/lib/ctrl"
 	"github.com/scionproto/scion/go/lib/ctrl/cert_mgmt"
 	"github.com/scionproto/scion/go/lib/infra"
@@ -84,7 +83,7 @@ func (v *verifier) VerifyPld(ctx context.Context, spld *ctrl.SignedPld) (*ctrl.P
 	return cpld, nil
 }
 
-func (v *verifier) Verify(ctx context.Context, msg common.RawBytes, sign *proto.SignS) error {
+func (v *verifier) Verify(ctx context.Context, msg []byte, sign *proto.SignS) error {
 	if err := sign.Valid(v.AllowSkew); err != nil {
 		return err
 	}
diff --git a/go/lib/snet/mock_snet/snet.go b/go/lib/snet/mock_snet/snet.go
index f256e3eb6..3c54684f6 100644
--- a/go/lib/snet/mock_snet/snet.go
+++ b/go/lib/snet/mock_snet/snet.go
@@ -38,20 +38,6 @@ func (m *MockConn) EXPECT() *MockConnMockRecorder {
 	return m.recorder
 }
 
-// BindAddr mocks base method
-func (m *MockConn) BindAddr() net.Addr {
-	m.ctrl.T.Helper()
-	ret := m.ctrl.Call(m, "BindAddr")
-	ret0, _ := ret[0].(net.Addr)
-	return ret0
-}
-
-// BindAddr indicates an expected call of BindAddr
-func (mr *MockConnMockRecorder) BindAddr() *gomock.Call {
-	mr.mock.ctrl.T.Helper()
-	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BindAddr", reflect.TypeOf((*MockConn)(nil).BindAddr))
-}
-
 // Close mocks base method
 func (m *MockConn) Close() error {
 	m.ctrl.T.Helper()
@@ -304,34 +290,34 @@ func (m *MockNetwork) EXPECT() *MockNetworkMockRecorder {
 	return m.recorder
 }
 
-// DialSCIONWithBindSVC mocks base method
-func (m *MockNetwork) DialSCIONWithBindSVC(arg0 string, arg1, arg2, arg3 *snet.Addr, arg4 addr.HostSVC, arg5 time.Duration) (snet.Conn, error) {
+// Dial mocks base method
+func (m *MockNetwork) Dial(arg0 string, arg1 *net.UDPAddr, arg2 *snet.UDPAddr, arg3 addr.HostSVC, arg4 time.Duration) (snet.Conn, error) {
 	m.ctrl.T.Helper()
-	ret := m.ctrl.Call(m, "DialSCIONWithBindSVC", arg0, arg1, arg2, arg3, arg4, arg5)
+	ret := m.ctrl.Call(m, "Dial", arg0, arg1, arg2, arg3, arg4)
 	ret0, _ := ret[0].(snet.Conn)
 	ret1, _ := ret[1].(error)
 	return ret0, ret1
 }
 
-// DialSCIONWithBindSVC indicates an expected call of DialSCIONWithBindSVC
-func (mr *MockNetworkMockRecorder) DialSCIONWithBindSVC(arg0, arg1, arg2, arg3, arg4, arg5 interface{}) *gomock.Call {
+// Dial indicates an expected call of Dial
+func (mr *MockNetworkMockRecorder) Dial(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
 	mr.mock.ctrl.T.Helper()
-	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DialSCIONWithBindSVC", reflect.TypeOf((*MockNetwork)(nil).DialSCIONWithBindSVC), arg0, arg1, arg2, arg3, arg4, arg5)
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Dial", reflect.TypeOf((*MockNetwork)(nil).Dial), arg0, arg1, arg2, arg3, arg4)
 }
 
-// ListenSCIONWithBindSVC mocks base method
-func (m *MockNetwork) ListenSCIONWithBindSVC(arg0 string, arg1, arg2 *snet.Addr, arg3 addr.HostSVC, arg4 time.Duration) (snet.Conn, error) {
+// Listen mocks base method
+func (m *MockNetwork) Listen(arg0 string, arg1 *net.UDPAddr, arg2 addr.HostSVC, arg3 time.Duration) (snet.Conn, error) {
 	m.ctrl.T.Helper()
-	ret := m.ctrl.Call(m, "ListenSCIONWithBindSVC", arg0, arg1, arg2, arg3, arg4)
+	ret := m.ctrl.Call(m, "Listen", arg0, arg1, arg2, arg3)
 	ret0, _ := ret[0].(snet.Conn)
 	ret1, _ := ret[1].(error)
 	return ret0, ret1
 }
 
-// ListenSCIONWithBindSVC indicates an expected call of ListenSCIONWithBindSVC
-func (mr *MockNetworkMockRecorder) ListenSCIONWithBindSVC(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
+// Listen indicates an expected call of Listen
+func (mr *MockNetworkMockRecorder) Listen(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
 	mr.mock.ctrl.T.Helper()
-	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListenSCIONWithBindSVC", reflect.TypeOf((*MockNetwork)(nil).ListenSCIONWithBindSVC), arg0, arg1, arg2, arg3, arg4)
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Listen", reflect.TypeOf((*MockNetwork)(nil).Listen), arg0, arg1, arg2, arg3)
 }
 
 // MockPacketConn is a mock of PacketConn interface


Test patch:
diff --git a/go/beacon_srv/internal/beaconing/extender_test.go b/go/beacon_srv/internal/beaconing/extender_test.go
index a36e78737..54e2cc29d 100644
--- a/go/beacon_srv/internal/beaconing/extender_test.go
+++ b/go/beacon_srv/internal/beaconing/extender_test.go
@@ -324,7 +324,7 @@ type failSigner struct {
 	infra.Signer
 }
 
-func (f *failSigner) Sign(msg common.RawBytes) (*proto.SignS, error) {
+func (f *failSigner) Sign(msg []byte) (*proto.SignS, error) {
 	return nil, errors.New("fail")
 }
 
diff --git a/go/beacon_srv/internal/beaconing/originator_test.go b/go/beacon_srv/internal/beaconing/originator_test.go
index b6e01cce5..7daf0eeff 100644
--- a/go/beacon_srv/internal/beaconing/originator_test.go
+++ b/go/beacon_srv/internal/beaconing/originator_test.go
@@ -191,9 +191,8 @@ func checkMsg(t *testing.T, msg msg, pub common.RawBytes, infos topology.IfInfoM
 	})
 }
 
-type segVerifier common.RawBytes
+type segVerifier []byte
 
-func (v segVerifier) Verify(_ context.Context, msg common.RawBytes, sign *proto.SignS) error {
-	return scrypto.Verify(sign.SigInput(msg, false), sign.Signature,
-		common.RawBytes(v), scrypto.Ed25519)
+func (v segVerifier) Verify(_ context.Context, msg []byte, sign *proto.SignS) error {
+	return scrypto.Verify(sign.SigInput(msg, false), sign.Signature, []byte(v), scrypto.Ed25519)
 }
diff --git a/go/beacon_srv/internal/ifstate/revoker_test.go b/go/beacon_srv/internal/ifstate/revoker_test.go
index 17b176aca..d02f781af 100644
--- a/go/beacon_srv/internal/ifstate/revoker_test.go
+++ b/go/beacon_srv/internal/ifstate/revoker_test.go
@@ -360,9 +360,8 @@ func createTestSigner(t *testing.T, key common.RawBytes) infra.Signer {
 	return signer
 }
 
-type revVerifier common.RawBytes
+type revVerifier []byte
 
-func (v revVerifier) Verify(_ context.Context, msg common.RawBytes, sign *proto.SignS) error {
-	return scrypto.Verify(sign.SigInput(msg, false), sign.Signature,
-		common.RawBytes(v), scrypto.Ed25519)
+func (v revVerifier) Verify(_ context.Context, msg []byte, sign *proto.SignS) error {
+	return scrypto.Verify(sign.SigInput(msg, false), sign.Signature, []byte(v), scrypto.Ed25519)
 }
diff --git a/go/beacon_srv/internal/revocation/handler_test.go b/go/beacon_srv/internal/revocation/handler_test.go
index 217c7f8f6..581daf79f 100644
--- a/go/beacon_srv/internal/revocation/handler_test.go
+++ b/go/beacon_srv/internal/revocation/handler_test.go
@@ -147,9 +147,8 @@ func createTestSigner(t *testing.T, key common.RawBytes) infra.Signer {
 	return signer
 }
 
-type revVerifier common.RawBytes
+type revVerifier []byte
 
-func (v revVerifier) Verify(_ context.Context, msg common.RawBytes, sign *proto.SignS) error {
-	return scrypto.Verify(sign.SigInput(msg, false), sign.Signature,
-		common.RawBytes(v), scrypto.Ed25519)
+func (v revVerifier) Verify(_ context.Context, msg []byte, sign *proto.SignS) error {
+	return scrypto.Verify(sign.SigInput(msg, false), sign.Signature, []byte(v), scrypto.Ed25519)
 }
diff --git a/go/lib/ctrl/seg/seg_test.go b/go/lib/ctrl/seg/seg_test.go
index 9fc270de3..f3a7d76e4 100644
--- a/go/lib/ctrl/seg/seg_test.go
+++ b/go/lib/ctrl/seg/seg_test.go
@@ -167,8 +167,8 @@ func getIds(t *testing.T, seg *PathSegment) (common.RawBytes, common.RawBytes) {
 }
 
 type keyPair struct {
-	pubKey  common.RawBytes
-	privKey common.RawBytes
+	pubKey  []byte
+	privKey []byte
 }
 
 func newKeyPair(t *testing.T) *keyPair {
@@ -180,21 +180,21 @@ func newKeyPair(t *testing.T) *keyPair {
 	}
 }
 
-func (t *keyPair) Sign(packedSegment common.RawBytes) (*proto.SignS, error) {
+func (t *keyPair) Sign(packedSegment []byte) (*proto.SignS, error) {
 	sign := &proto.SignS{
-		Src: common.RawBytes{1, 4, 4, 2},
+		Src: []byte{1, 4, 4, 2},
 	}
 	signature, err := scrypto.Sign(packedSegment, t.privKey, scrypto.Ed25519)
 	sign.Signature = signature
 	return sign, err
 }
 
-func (t *keyPair) Verify(_ context.Context, msg common.RawBytes,
+func (t *keyPair) Verify(_ context.Context, msg []byte,
 	sign *proto.SignS) error {
 
-	if !bytes.Equal(sign.Src, common.RawBytes{1, 4, 4, 2}) {
+	if !bytes.Equal(sign.Src, []byte{1, 4, 4, 2}) {
 		return common.NewBasicError("Invalid sign", nil,
-			"expected", common.RawBytes{1, 4, 4, 2}, "actual", sign.Src)
+			"expected", []byte{1, 4, 4, 2}, "actual", sign.Src)
 	}
 	return scrypto.Verify(msg, sign.Signature, t.pubKey, scrypto.Ed25519)
 }