feature/signature (#4)

* signing messages

* updated, added errors

* using uint32
This commit is contained in:
Dean Eigenmann
2019-08-02 10:06:20 +02:00
committed by GitHub
parent 62f47ecf2e
commit c598ef2e36
3 changed files with 42 additions and 14 deletions

View File

@@ -2,9 +2,14 @@
package client
import (
"crypto/ecdsa"
"crypto/sha256"
"encoding/binary"
"log"
"github.com/ethereum/go-ethereum/crypto"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/vacp2p/dasy/protobuf"
mvds "github.com/vacp2p/mvds/node"
mvdsproto "github.com/vacp2p/mvds/protobuf"
@@ -23,6 +28,8 @@ type Client struct {
node mvds.Node
store store.MessageStore // @todo we probably need a different message store, not sure tho
identity *ecdsa.PrivateKey
lastMessage state.MessageID // @todo maybe make type
}
@@ -64,16 +71,19 @@ func (c *Client) send(chat Chat, t protobuf.Message_MessageType, body []byte) er
PreviousMessage: c.lastMessage[:],
}
// @todo sign
err := c.sign(msg)
if err != nil {
return errors.Wrap(err, "failed to sign message")
}
buf, err := proto.Marshal(msg)
if err != nil {
return err
return errors.Wrap(err, "failed to marshall message")
}
id, err := c.node.AppendMessage(state.GroupID(chat), buf)
if err != nil {
return err
return errors.Wrap(err, "failed to append message")
}
c.lastMessage = id
@@ -111,3 +121,21 @@ func (c *Client) handlePreviousMessage(group state.GroupID, previousMessage stat
log.Printf("error while requesting message: %s", err.Error())
}
}
// sign signs generates a signature of the message and adds it to the message.
func (c *Client) sign(m *protobuf.Message) error {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, uint32(m.MessageType))
b = append(b, m.Body...)
b = append(b, m.PreviousMessage...)
hash := sha256.Sum256(b)
sig, err := crypto.Sign(hash[:], c.identity)
if err != nil {
return err
}
m.Signature = sig
return nil
}

View File

@@ -61,7 +61,7 @@ type Message struct {
MessageType Message_MessageType `protobuf:"varint,1,opt,name=message_type,json=messageType,proto3,enum=mvds.Message_MessageType" json:"message_type,omitempty"`
Body []byte `protobuf:"bytes,2,opt,name=body,proto3" json:"body,omitempty"`
PreviousMessage []byte `protobuf:"bytes,3,opt,name=previous_message,json=previousMessage,proto3" json:"previous_message,omitempty"`
Signature [][]byte `protobuf:"bytes,4,rep,name=signature,proto3" json:"signature,omitempty"`
Signature []byte `protobuf:"bytes,4,opt,name=signature,proto3" json:"signature,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@@ -113,7 +113,7 @@ func (m *Message) GetPreviousMessage() []byte {
return nil
}
func (m *Message) GetSignature() [][]byte {
func (m *Message) GetSignature() []byte {
if m != nil {
return m.Signature
}
@@ -128,7 +128,7 @@ func init() {
func init() { proto.RegisterFile("protobuf/message.proto", fileDescriptor_8368f5d77b0b9b7b) }
var fileDescriptor_8368f5d77b0b9b7b = []byte{
// 231 bytes of a gzipped FileDescriptorProto
// 228 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2b, 0x28, 0xca, 0x2f,
0xc9, 0x4f, 0x2a, 0x4d, 0xd3, 0xcf, 0x4d, 0x2d, 0x2e, 0x4e, 0x4c, 0x4f, 0xd5, 0x03, 0x0b, 0x08,
0xb1, 0xe4, 0x96, 0xa5, 0x14, 0x2b, 0x7d, 0x64, 0xe4, 0x62, 0xf7, 0x85, 0x88, 0x0b, 0xd9, 0x70,
@@ -137,11 +137,11 @@ var fileDescriptor_8368f5d77b0b9b7b = []byte{
0x47, 0x48, 0x88, 0x8b, 0x25, 0x29, 0x3f, 0xa5, 0x52, 0x82, 0x49, 0x81, 0x51, 0x83, 0x27, 0x08,
0xcc, 0x16, 0xd2, 0xe4, 0x12, 0x28, 0x28, 0x4a, 0x2d, 0xcb, 0xcc, 0x2f, 0x2d, 0x8e, 0x87, 0xaa,
0x95, 0x60, 0x06, 0xcb, 0xf3, 0xc3, 0xc4, 0x61, 0x96, 0xcb, 0x70, 0x71, 0x16, 0x67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92, 0xc4, 0x06, 0x66, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x41, 0x70, 0xaf, 0x4e,
0x26, 0x01, 0x00, 0x00,
}

View File

@@ -17,5 +17,5 @@ message Message {
MessageType message_type = 1;
bytes body = 2;
bytes previous_message = 3;
repeated bytes signature = 4;
bytes signature = 4;
}