-
Notifications
You must be signed in to change notification settings - Fork 11
/
msg.go
137 lines (117 loc) · 3.15 KB
/
msg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package nymo
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/elliptic"
"math/big"
"time"
"github.com/nymo-net/nymo/pb"
"google.golang.org/protobuf/proto"
)
// Message contains a decrypted message (sent to the user).
type Message struct {
// Sender is the sender of the message.
Sender *Address
// SendTime is the sender-specified time.
SendTime time.Time
// Content contains the protocol-specified message data.
// It is a UTF-8 encoded string in vanilla implementation.
Content []byte
}
func (u *User) decryptMessage(msg *pb.Message) *Message {
if msg.TargetCohort != u.cohort {
return nil
}
eKeyX, eKeyY := elliptic.UnmarshalCompressed(curve, msg.EphemeralPub)
if eKeyX == nil {
return nil
}
secret, iv := curve.ScalarMult(eKeyX, eKeyY, u.key.D.Bytes())
cp, err := aes.NewCipher(secret.Bytes())
if err != nil {
return nil
}
encMsg := msg.EncMessage
cipher.NewCBCDecrypter(cp, iv.Bytes()[8:][:aes.BlockSize]).CryptBlocks(encMsg, encMsg)
block := trimBlock(encMsg)
if block == nil {
return nil
}
enc := new(pb.EncryptedMessage)
err = proto.Unmarshal(block, enc)
if err != nil {
return nil
}
ret := new(pb.RealMessage)
err = proto.Unmarshal(enc.Msg, ret)
if err != nil {
return nil
}
x, y := elliptic.UnmarshalCompressed(curve, ret.SenderID)
h := hasher(enc.Msg)
if x == nil || !ecdsa.Verify(
&ecdsa.PublicKey{Curve: curve, X: x, Y: y}, h[:],
new(big.Int).SetBytes(enc.Signature[:curveByteLen]),
new(big.Int).SetBytes(enc.Signature[curveByteLen:]),
) {
return nil
}
return &Message{
Sender: newAddress(x, y),
SendTime: time.UnixMilli(ret.SendTime),
Content: ret.Message,
}
}
// NewMessage send a new message with content msg to the recipient.
// Database.StoreMessage might be called synchronously.
func (u *User) NewMessage(recipient *Address, msg []byte) error {
ephemeralKey, err := ecdsa.GenerateKey(curve, cReader)
if err != nil {
return err
}
secret, iv := curve.ScalarMult(recipient.x, recipient.y, ephemeralKey.D.Bytes())
cp, err := aes.NewCipher(secret.Bytes())
if err != nil {
return err
}
rMsg := &pb.RealMessage{
Message: msg,
SendTime: time.Now().UnixMilli(),
SenderID: elliptic.MarshalCompressed(curve, u.key.X, u.key.Y),
}
rMsgBuf, err := proto.Marshal(rMsg)
if err != nil {
return err
}
h := hasher(rMsgBuf)
sigR, sigS, err := ecdsa.Sign(cReader, u.key, h[:])
if err != nil {
return err
}
enc := pb.EncryptedMessage{
Msg: rMsgBuf,
Signature: make([]byte, curveByteLen*2),
}
sigR.FillBytes(enc.Signature[:curveByteLen])
sigS.FillBytes(enc.Signature[curveByteLen:])
marshal, err := proto.Marshal(&enc)
if err != nil {
return err
}
marshal = padBlock(marshal)
cipher.NewCBCEncrypter(cp, iv.Bytes()[8:][:aes.BlockSize]).CryptBlocks(marshal, marshal)
mMsg, err := proto.Marshal(&pb.Message{
TargetCohort: recipient.cohort,
EphemeralPub: elliptic.MarshalCompressed(curve, ephemeralKey.X, ephemeralKey.Y),
EncMessage: marshal,
})
if err != nil {
return err
}
msgHash := hasher(mMsg)
return u.db.StoreMessage(msgHash, &pb.MsgContainer{
Msg: mMsg,
Pow: calcPoW(msgHash[:]),
}, func() (uint32, error) { return recipient.cohort, nil })
}