-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
303 lines (257 loc) · 7.3 KB
/
message.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package delphi
import (
"crypto"
"crypto/ecdh"
"crypto/ed25519"
"crypto/sha256"
"encoding/hex"
"encoding/pem"
"errors"
"fmt"
"io"
"github.com/sean9999/pear"
"github.com/vmihailenco/msgpack/v5"
omap "github.com/wk8/go-ordered-map/v2"
)
// KV is a key-value store whose keys are ordered, offering deterministic serialization
type KV = omap.OrderedMap[string, string]
func NewKV() *omap.OrderedMap[string, string] {
kv := omap.New[string, string]()
return kv
}
var ErrNotImplemented = errors.New("not implemented")
// a Message is a message that represents either plain text or cipher text,
// encapsulating all data and metadata necessary to perform cryptographic operations.
type Message struct {
readBuffer []byte `msgpack:"-"`
Subject string `msgpack:"subj" json:"subj"`
Recipient Key `msgpack:"to" json:"to"`
Sender Key `msgpack:"from" json:"from"`
Headers *KV `msgpack:"hdrs" json:"hdrs"` // additional authenticated data (AAD)
ephPubkey []byte `msgpack:"ephkey" json:"ephkey"`
nonce Nonce `msgpack:"nonce" json:"nonce"`
cipherText []byte `msgpack:"ctxt" json:"cipherText"`
PlainText []byte `msgpack:"ptxt" json:"plainText"`
signature []byte `msgpack:"sig" json:"sig"`
}
// RecipientEncryption() returns the recipient as a public encryption key
func (m *Message) RecipientEncryption() crypto.PublicKey {
k, err := ecdh.X25519().NewPublicKey(m.Recipient.Encryption().Bytes())
if err != nil {
panic(err)
}
return k
}
// SenderEncryption() returns the sender as a public encryption key
func (m *Message) SenderEncryption() crypto.PublicKey {
k, err := ecdh.X25519().NewPublicKey(m.Sender.Encryption().Bytes())
if err != nil {
panic(err)
}
return k
}
// Ephemeral() returns the value of the ephemeral X25519 key attached to an encrypted Message
func (m *Message) Ephemeral() crypto.PublicKey {
return ed25519.PublicKey(m.ephPubkey)
}
// Signatory() returns the public signing key of the sender
func (m *Message) Signatory() crypto.PublicKey {
k, err := ecdh.X25519().NewPublicKey(m.Sender.Signing().Bytes())
if err != nil {
panic(err)
}
return k
}
func (m *Message) Signature() []byte {
return m.signature
}
// ensureNonce ensures the Message has a [Nonce]
func (m *Message) ensureNonce(randy io.Reader) Nonce {
if !m.nonce.IsZero() {
return m.nonce
}
nonce := Nonce{}
i, err := randy.Read(nonce[:])
if i != NonceSize {
panic("wrong length")
}
if err != nil {
panic("error reading from randy into nonce")
}
m.nonce = nonce
return m.nonce
}
func (m *Message) MarshalBinary() ([]byte, error) {
return msgpack.Marshal(m)
}
func (m *Message) UnmarshalBinary(p []byte) error {
return msgpack.Unmarshal(p, m)
}
// toStringMap converts an ordered set to an unordered map
func toStringMap(msg *Message) map[string]string {
n := make(map[string]string)
for pair := msg.Headers.Oldest(); pair != nil; pair = pair.Next() {
n[pair.Key] = pair.Value
}
n["signature"] = fmt.Sprintf("%x", msg.Signature())
n["nonce"] = fmt.Sprintf("%x", msg.nonce)
n["from"] = msg.Sender.ToHex()
return n
}
func (m *Message) ToPEM() pem.Block {
var b []byte
if m.Encrypted() {
b = m.cipherText
} else {
b = m.PlainText
}
p := pem.Block{
Type: m.Subject,
Headers: toStringMap(m),
Bytes: b,
}
return p
}
func extractHex(hdrs map[string]string, key string) ([]byte, error) {
val, exists := hdrs[key]
if !exists {
return nil, pear.Errorf("key doesn't exist: %q", key)
}
b, err := hex.DecodeString(val)
if err != nil {
return nil, pear.Errorf("failed to extract hex: %w", err)
}
return b, nil
}
func (m *Message) FromPEM(p pem.Block) error {
m.Headers = NewKV()
for k, v := range p.Headers {
switch k {
case "nonce":
nonce, err := extractHex(p.Headers, "nonce")
if err != nil {
return err
}
m.nonce = Nonce(nonce)
case "signature":
sig, err := extractHex(p.Headers, "signature")
if err != nil {
return err
}
m.signature = sig
case "from":
pubKeyBytes, err := extractHex(p.Headers, "from")
if err != nil {
return err
}
m.Sender = Key{}.From(pubKeyBytes)
default:
m.Headers.Set(k, v)
}
}
m.Subject = p.Type
m.PlainText = p.Bytes
return nil
}
func (m *Message) String() string {
p := m.ToPEM()
pemBytes := pem.EncodeToMemory(&p)
return string(pemBytes)
}
func (m *Message) Read(b []byte) (int, error) {
if m.readBuffer == nil {
p := m.ToPEM()
m.readBuffer = pem.EncodeToMemory(&p)
}
if len(m.readBuffer) > 0 {
bytesWritten := copy(b, m.readBuffer)
m.readBuffer = m.readBuffer[bytesWritten:]
return bytesWritten, nil
} else {
return 0, io.EOF
}
}
func (msg *Message) Plain() bool {
return len(msg.PlainText) > 0
}
func (msg *Message) Encrypted() bool {
return len(msg.cipherText) > 0
}
func (msg *Message) Valid() bool {
// TODO: other requirements surely should be
return (msg.Plain() && !msg.Encrypted()) || (msg.Encrypted() && !msg.Plain())
}
// Digest() returns that portion of a Message which should be hashed and signed
func (msg *Message) Digest() ([]byte, error) {
// Some fields are included. Some are required. Some are intentially omitted:
// To is omitted. A messages digest is the same regardless of who it's sent to.
// From is required. Who its from is integral.
// Headers are included if they exists, but not if not. They are treated as AAD.
// Nonce is required, to ensure uniqueness
// Ephemeral Key is omitted. Nonce provides all necessary randomness
// Either plain or cipher text is included. It's an error to have both or neither.
hash := sha256.New()
if !msg.Valid() {
return nil, errors.New("message is not valid")
}
if msg.nonce.IsZero() {
return nil, errors.New("nonce is zero")
}
if msg.Sender.IsZero() {
return nil, errors.New("From is zero")
}
sum := make([]byte, 0)
sum = append(sum, msg.Sender.Bytes()...)
sum = append(sum, msg.nonce[:]...)
if msg.Encrypted() {
sum = append(sum, msg.cipherText...)
} else {
sum = append(sum, msg.PlainText...)
}
// let's no include headers because since the order cannot be known
// it's too hard to acheive determinism
// if msg.Headers.Length() > 0 {
// headers, err := msg.Headers.MarshalBinary()
// if err != nil {
// return nil, err
// }
// sum = append(sum, headers...)
// }
return hash.Sum(sum), nil
}
// msg.Sign(Signer) is another way of doing signer.Sign(*Message)
func (msg *Message) Sign(randy io.Reader, signer crypto.Signer) error {
var errSign = pear.Defer("could not sign message")
msg.ensureNonce(randy)
digest, err := msg.Digest()
if err != nil {
return fmt.Errorf("%w: %w", errSign, err)
}
sig, err := signer.Sign(randy, digest, nil)
if err != nil {
return fmt.Errorf("%w: %w", errSign, err)
}
msg.signature = sig
return nil
}
// Verify() verifies a signature
func (msg *Message) Verify() bool {
digest, err := msg.Digest()
if err != nil {
panic(err)
}
edpub := ed25519.PublicKey(msg.Sender.Signing().Bytes())
return ed25519.Verify(edpub, digest, msg.signature)
}
// msg.Encrypt(Encrypter) is another way of doing encrypter.Encrypt(*Message)
func (msg *Message) Encrypt(randy io.Reader, encrypter Encrypter, opts EncrypterOpts) error {
return encrypter.Encrypt(randy, msg, opts)
}
// NewMessage() creates a new Message
func NewMessage(randy io.Reader, plainTxt []byte) *Message {
msg := new(Message)
msg.Headers = NewKV()
msg.ensureNonce(randy)
msg.PlainText = plainTxt
return msg
}