-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
147 lines (114 loc) · 3.41 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
package e2ee
import (
"bytes"
"encoding/binary"
"errors"
)
type messageHeader struct {
packetType uint8
reserved uint8
// 0 もありえる
ciphertextLength uint16
}
func decodeMessageHeader(data []byte) (*messageHeader, *bytes.Reader, error) {
if len(data) < 4 {
// パケットが 4 バイト以下なのでパースできない
return nil, nil, errors.New("invalid data")
}
h := &messageHeader{}
buf := bytes.NewReader(data)
if err := binary.Read(buf, binary.BigEndian, &h.packetType); err != nil {
return nil, nil, err
}
if err := binary.Read(buf, binary.BigEndian, &h.reserved); err != nil {
return nil, nil, err
}
if err := binary.Read(buf, binary.BigEndian, &h.ciphertextLength); err != nil {
return nil, nil, err
}
return h, buf, nil
}
// ```erlang
// <<?E2EE_PRE_KEY_MESSAGE_TYPE:8, Reserved:8, CiphertextLength:16,
// SrcConnectionID:26/binary, DstConnectionID:26/binary,
// IdentityKey:32/binary, EphemeralKey:32/binary>>
// ```
type preKeyMessage struct {
selfConnectionID [26]byte
remoteConnectionID [26]byte
// ここで渡す identityKey は ed25519
identityKey [32]byte
ephemeralKey x25519PublicKey
}
func decodePreKeyMessage(header messageHeader, buf *bytes.Reader) (*preKeyMessage, error) {
m := &preKeyMessage{}
if err := binary.Read(buf, binary.BigEndian, &m.selfConnectionID); err != nil {
return nil, err
}
if err := binary.Read(buf, binary.BigEndian, &m.remoteConnectionID); err != nil {
return nil, err
}
if err := binary.Read(buf, binary.BigEndian, &m.identityKey); err != nil {
return nil, err
}
if err := binary.Read(buf, binary.BigEndian, &m.ephemeralKey); err != nil {
return nil, err
}
return m, nil
}
// <<?E2EE_CIPHER_MESSAGE_TYPE:8, Reserved:8, CiphertextLength:16,
// SrcConnectionID:26/binary, DstConnectionID:26/binary,
// ## CipherMessage のここはヘッダー
// RachetKey:32/binary, N:32, NP:32,
// ## 本体
// Ciphertext/binary>>
// Ciphertext 中身
// <<KeyId:32, SecretKeyMaterial:32/binary>>
type cipherMessage struct {
selfConnectionID [26]byte
remoteConnectionID [26]byte
ratchetKey x25519PublicKey
PN uint32
N uint32
ciphertext []byte
}
func decodeCipherMessage(header messageHeader, buf *bytes.Reader) (*cipherMessage, error) {
m := &cipherMessage{}
if err := binary.Read(buf, binary.BigEndian, &m.selfConnectionID); err != nil {
return nil, err
}
if err := binary.Read(buf, binary.BigEndian, &m.remoteConnectionID); err != nil {
return nil, err
}
if err := binary.Read(buf, binary.BigEndian, &m.ratchetKey); err != nil {
return nil, err
}
if err := binary.Read(buf, binary.BigEndian, &m.PN); err != nil {
return nil, err
}
if err := binary.Read(buf, binary.BigEndian, &m.N); err != nil {
return nil, err
}
var ciphertext = make([]byte, header.ciphertextLength)
if err := binary.Read(buf, binary.BigEndian, ciphertext); err != nil {
return nil, err
}
m.ciphertext = ciphertext
return m, nil
}
// ciphertext の中身
type senderKeyMessage struct {
keyID uint32
secretKeyMaterial [32]byte
}
func decodeSenderKeyMessage(plaintext []byte) (*senderKeyMessage, error) {
buf := bytes.NewReader(plaintext)
m := &senderKeyMessage{}
if err := binary.Read(buf, binary.BigEndian, &m.keyID); err != nil {
return nil, err
}
if err := binary.Read(buf, binary.BigEndian, &m.secretKeyMaterial); err != nil {
return nil, err
}
return m, nil
}