From 36c20d2cf81677f0bd30c01cff91a8a3e93031a5 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Wed, 24 Apr 2024 14:02:49 +0200 Subject: [PATCH] Added claim signature message --- pkg/tecdsa/inactivity/claim.go | 17 ++ pkg/tecdsa/inactivity/gen/pb/message.pb.go | 185 +++++++++++++++++++++ pkg/tecdsa/inactivity/gen/pb/message.proto | 12 ++ pkg/tecdsa/inactivity/marshalling.go | 48 +++++- pkg/tecdsa/inactivity/marshalling_test.go | 65 ++++++++ 5 files changed, 324 insertions(+), 3 deletions(-) create mode 100644 pkg/tecdsa/inactivity/gen/pb/message.pb.go create mode 100644 pkg/tecdsa/inactivity/gen/pb/message.proto create mode 100644 pkg/tecdsa/inactivity/marshalling_test.go diff --git a/pkg/tecdsa/inactivity/claim.go b/pkg/tecdsa/inactivity/claim.go index 02ce0a0f8b..b434fa09c2 100644 --- a/pkg/tecdsa/inactivity/claim.go +++ b/pkg/tecdsa/inactivity/claim.go @@ -2,6 +2,7 @@ package inactivity import ( "crypto/ecdsa" + "fmt" "math/big" "github.com/keep-network/keep-core/pkg/protocol/group" @@ -20,3 +21,19 @@ const ClaimSignatureHashByteSize = 32 // ClaimSignatureHash is a signature hash of the inactivity claim. The hashing // algorithm used depends on the client code. type ClaimSignatureHash [ClaimSignatureHashByteSize]byte + +// ClaimSignatureHashFromBytes converts bytes slice to ClaimSignatureHash. +// It requires provided bytes slice size to be exactly +// ClaimSignatureHashByteSize. +func ClaimSignatureHashFromBytes(bytes []byte) (ClaimSignatureHash, error) { + var hash ClaimSignatureHash + + if len(bytes) != ClaimSignatureHashByteSize { + return hash, fmt.Errorf( + "bytes length is not equal %v", ClaimSignatureHashByteSize, + ) + } + copy(hash[:], bytes[:]) + + return hash, nil +} diff --git a/pkg/tecdsa/inactivity/gen/pb/message.pb.go b/pkg/tecdsa/inactivity/gen/pb/message.pb.go new file mode 100644 index 0000000000..ea4eed0654 --- /dev/null +++ b/pkg/tecdsa/inactivity/gen/pb/message.pb.go @@ -0,0 +1,185 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.0 +// protoc v3.19.4 +// source: pkg/tecdsa/inactivity/gen/pb/message.proto + +package pb + +import ( + reflect "reflect" + sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type ClaimSignatureMessage struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + SenderID uint32 `protobuf:"varint,1,opt,name=senderID,proto3" json:"senderID,omitempty"` + ClaimHash []byte `protobuf:"bytes,2,opt,name=claimHash,proto3" json:"claimHash,omitempty"` + Signature []byte `protobuf:"bytes,3,opt,name=signature,proto3" json:"signature,omitempty"` + PublicKey []byte `protobuf:"bytes,4,opt,name=publicKey,proto3" json:"publicKey,omitempty"` + SessionID string `protobuf:"bytes,5,opt,name=sessionID,proto3" json:"sessionID,omitempty"` +} + +func (x *ClaimSignatureMessage) Reset() { + *x = ClaimSignatureMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_pkg_tecdsa_inactivity_gen_pb_message_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ClaimSignatureMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ClaimSignatureMessage) ProtoMessage() {} + +func (x *ClaimSignatureMessage) ProtoReflect() protoreflect.Message { + mi := &file_pkg_tecdsa_inactivity_gen_pb_message_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ClaimSignatureMessage.ProtoReflect.Descriptor instead. +func (*ClaimSignatureMessage) Descriptor() ([]byte, []int) { + return file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDescGZIP(), []int{0} +} + +func (x *ClaimSignatureMessage) GetSenderID() uint32 { + if x != nil { + return x.SenderID + } + return 0 +} + +func (x *ClaimSignatureMessage) GetClaimHash() []byte { + if x != nil { + return x.ClaimHash + } + return nil +} + +func (x *ClaimSignatureMessage) GetSignature() []byte { + if x != nil { + return x.Signature + } + return nil +} + +func (x *ClaimSignatureMessage) GetPublicKey() []byte { + if x != nil { + return x.PublicKey + } + return nil +} + +func (x *ClaimSignatureMessage) GetSessionID() string { + if x != nil { + return x.SessionID + } + return "" +} + +var File_pkg_tecdsa_inactivity_gen_pb_message_proto protoreflect.FileDescriptor + +var file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDesc = []byte{ + 0x0a, 0x2a, 0x70, 0x6b, 0x67, 0x2f, 0x74, 0x65, 0x63, 0x64, 0x73, 0x61, 0x2f, 0x69, 0x6e, 0x61, + 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x70, 0x62, 0x2f, 0x6d, + 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0a, 0x69, 0x6e, + 0x61, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x22, 0xab, 0x01, 0x0a, 0x15, 0x43, 0x6c, 0x61, + 0x69, 0x6d, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, + 0x67, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x49, 0x44, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x73, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x49, 0x44, 0x12, 0x1c, + 0x0a, 0x09, 0x63, 0x6c, 0x61, 0x69, 0x6d, 0x48, 0x61, 0x73, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x0c, 0x52, 0x09, 0x63, 0x6c, 0x61, 0x69, 0x6d, 0x48, 0x61, 0x73, 0x68, 0x12, 0x1c, 0x0a, 0x09, + 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, + 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x70, 0x75, + 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x09, 0x70, + 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x65, 0x73, 0x73, + 0x69, 0x6f, 0x6e, 0x49, 0x44, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x65, 0x73, + 0x73, 0x69, 0x6f, 0x6e, 0x49, 0x44, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDescOnce sync.Once + file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDescData = file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDesc +) + +func file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDescGZIP() []byte { + file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDescOnce.Do(func() { + file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDescData = protoimpl.X.CompressGZIP(file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDescData) + }) + return file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDescData +} + +var file_pkg_tecdsa_inactivity_gen_pb_message_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_pkg_tecdsa_inactivity_gen_pb_message_proto_goTypes = []interface{}{ + (*ClaimSignatureMessage)(nil), // 0: inactivity.ClaimSignatureMessage +} +var file_pkg_tecdsa_inactivity_gen_pb_message_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_pkg_tecdsa_inactivity_gen_pb_message_proto_init() } +func file_pkg_tecdsa_inactivity_gen_pb_message_proto_init() { + if File_pkg_tecdsa_inactivity_gen_pb_message_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_pkg_tecdsa_inactivity_gen_pb_message_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ClaimSignatureMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_pkg_tecdsa_inactivity_gen_pb_message_proto_goTypes, + DependencyIndexes: file_pkg_tecdsa_inactivity_gen_pb_message_proto_depIdxs, + MessageInfos: file_pkg_tecdsa_inactivity_gen_pb_message_proto_msgTypes, + }.Build() + File_pkg_tecdsa_inactivity_gen_pb_message_proto = out.File + file_pkg_tecdsa_inactivity_gen_pb_message_proto_rawDesc = nil + file_pkg_tecdsa_inactivity_gen_pb_message_proto_goTypes = nil + file_pkg_tecdsa_inactivity_gen_pb_message_proto_depIdxs = nil +} diff --git a/pkg/tecdsa/inactivity/gen/pb/message.proto b/pkg/tecdsa/inactivity/gen/pb/message.proto new file mode 100644 index 0000000000..e0e768a356 --- /dev/null +++ b/pkg/tecdsa/inactivity/gen/pb/message.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +option go_package = "./pb"; +package inactivity; + +message ClaimSignatureMessage { + uint32 senderID = 1; + bytes claimHash = 2; + bytes signature = 3; + bytes publicKey = 4; + string sessionID = 5; +} \ No newline at end of file diff --git a/pkg/tecdsa/inactivity/marshalling.go b/pkg/tecdsa/inactivity/marshalling.go index e104b9962e..8e7b568c2c 100644 --- a/pkg/tecdsa/inactivity/marshalling.go +++ b/pkg/tecdsa/inactivity/marshalling.go @@ -1,15 +1,57 @@ package inactivity +import ( + "fmt" + + "google.golang.org/protobuf/proto" + + "github.com/keep-network/keep-core/pkg/protocol/group" + "github.com/keep-network/keep-core/pkg/tecdsa/inactivity/gen/pb" +) + +func validateMemberIndex(protoIndex uint32) error { + // Protobuf does not have uint8 type, so we are using uint32. When + // unmarshalling message, we need to make sure we do not overflow. + if protoIndex > group.MaxMemberIndex { + return fmt.Errorf("invalid member index value: [%v]", protoIndex) + } + return nil +} + // Marshal converts this claimSignatureMessage to a byte array suitable // for network communication. func (csm *claimSignatureMessage) Marshal() ([]byte, error) { - // TODO: Implement - return nil, nil + return proto.Marshal(&pb.ClaimSignatureMessage{ + SenderID: uint32(csm.senderID), + ClaimHash: csm.claimHash[:], + Signature: csm.signature, + PublicKey: csm.publicKey, + SessionID: csm.sessionID, + }) } // Unmarshal converts a byte array produced by Marshal to a // claimSignatureMessage. func (csm *claimSignatureMessage) Unmarshal(bytes []byte) error { - // TODO: Implement + pbMsg := pb.ClaimSignatureMessage{} + if err := proto.Unmarshal(bytes, &pbMsg); err != nil { + return err + } + + if err := validateMemberIndex(pbMsg.SenderID); err != nil { + return err + } + csm.senderID = group.MemberIndex(pbMsg.SenderID) + + claimHash, err := ClaimSignatureHashFromBytes(pbMsg.ClaimHash) + if err != nil { + return err + } + csm.claimHash = claimHash + + csm.signature = pbMsg.Signature + csm.publicKey = pbMsg.PublicKey + csm.sessionID = pbMsg.SessionID + return nil } diff --git a/pkg/tecdsa/inactivity/marshalling_test.go b/pkg/tecdsa/inactivity/marshalling_test.go new file mode 100644 index 0000000000..1b5ab43e09 --- /dev/null +++ b/pkg/tecdsa/inactivity/marshalling_test.go @@ -0,0 +1,65 @@ +package inactivity + +import ( + "reflect" + "testing" + + fuzz "github.com/google/gofuzz" + + "github.com/keep-network/keep-core/pkg/internal/pbutils" + "github.com/keep-network/keep-core/pkg/protocol/group" +) + +func TestClaimSignatureMessage_MarshalingRoundtrip(t *testing.T) { + msg := &claimSignatureMessage{ + senderID: 123, + claimHash: [32]byte{0: 11, 10: 22, 31: 33}, + signature: []byte("signature"), + publicKey: []byte("pubkey"), + sessionID: "session-1", + } + unmarshaled := &claimSignatureMessage{} + + err := pbutils.RoundTrip(msg, unmarshaled) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(msg, unmarshaled) { + t.Fatalf("unexpected content of unmarshaled message") + } +} + +func TestFuzzClaimSignatureMessage_MarshalingRoundtrip(t *testing.T) { + for i := 0; i < 10; i++ { + var ( + senderID group.MemberIndex + claimHash ClaimSignatureHash + signature []byte + publicKey []byte + sessionID string + ) + + f := fuzz.New().NilChance(0.1).NumElements(0, 512) + + f.Fuzz(&senderID) + f.Fuzz(&claimHash) + f.Fuzz(&signature) + f.Fuzz(&publicKey) + f.Fuzz(&sessionID) + + message := &claimSignatureMessage{ + senderID: senderID, + claimHash: claimHash, + signature: signature, + publicKey: publicKey, + sessionID: sessionID, + } + + _ = pbutils.RoundTrip(message, &claimSignatureMessage{}) + } +} + +func TestFuzzClaimSignatureMessage_Unmarshaler(t *testing.T) { + pbutils.FuzzUnmarshaler(&claimSignatureMessage{}) +}