forked from Bit-Nation/panthalassa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.go
205 lines (160 loc) · 4.35 KB
/
chat.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
package panthalassa
import (
"encoding/hex"
"encoding/json"
"errors"
chat "github.com/Bit-Nation/panthalassa/chat"
aes "github.com/Bit-Nation/panthalassa/crypto/aes"
)
// create new pre key bundle
func NewPreKeyBundle() (string, error) {
if panthalassaInstance == nil {
return "", errors.New("please start panthalassa first")
}
// create new per key bundle
bundle, err := panthalassaInstance.chat.NewPreKeyBundle()
if err != nil {
return "", err
}
// marshal public part
publicPart, err := bundle.PublicPart.Marshal()
if err != nil {
return "", err
}
// marshal private part and encrypt
privatePart, err := bundle.PrivatePart.Marshal()
if err != nil {
return "", err
}
// encrypt private part
privtePartCipherText, err := panthalassaInstance.km.AESEncrypt(privatePart)
if err != nil {
return "", err
}
// marshal pre key bundle
preKeyBundle, err := json.Marshal(struct {
PublicPart string `json:"public_part"`
PrivatePart aes.CipherText `json:"private_part"`
}{
PublicPart: string(publicPart),
PrivatePart: privtePartCipherText,
})
if err != nil {
return "", err
}
return string(preKeyBundle), nil
}
// initialize chat with given identity key and pre key bundle
func InitializeChat(identityPublicKey, preKeyBundle string) (string, error) {
if panthalassaInstance == nil {
return "", errors.New("please start panthalassa first")
}
// decode public key
pubKey, err := hex.DecodeString(identityPublicKey)
if err != nil {
return "", err
}
if len(pubKey) != 32 {
return "", errors.New("public key must have length of 32 bytes")
}
// decode pre key bundle
bundle, err := chat.UnmarshalPreKeyBundle([]byte(preKeyBundle))
if err != nil {
return "", err
}
msg, initializedProtocol, err := panthalassaInstance.chat.InitializeChat(pubKey, bundle)
if err != nil {
return "", err
}
exportedSecret, err := chat.EncryptX3DHSecret(initializedProtocol.SharedSecret, panthalassaInstance.km)
if err != nil {
return "", err
}
initialProtocol, err := json.Marshal(struct {
Message chat.Message `json:"message"`
Secret aes.CipherText `json:"secret"`
}{
Message: msg,
Secret: exportedSecret,
})
return string(initialProtocol), err
}
// create message
// secret should be a aes cipher text as string
func CreateHumanMessage(rawMsg, secret string) (string, error) {
if panthalassaInstance == nil {
return "", errors.New("please start panthalassa first")
}
// unmarshal raw secret (secret is a cipher text)
cipherText, err := aes.Unmarshal([]byte(rawMsg))
if err != nil {
return "", err
}
// shared secret
sharedSecret, err := chat.DecryptX3DHSecret(cipherText, panthalassaInstance.km)
if err != nil {
return "", err
}
// create message
msg, err := panthalassaInstance.chat.CreateHumanMessage(rawMsg, sharedSecret)
if err != nil {
return "", err
}
// marshal message
m, err := msg.Marshal()
if err != nil {
return "", err
}
return string(m), nil
}
// decrypt a chat message
// secret should be a cipher text as string
func DecryptMessage(message, secret string) (string, error) {
if panthalassaInstance == nil {
return "", errors.New("please start panthalassa first")
}
ct, err := aes.Unmarshal([]byte(secret))
if err != nil {
return "", err
}
// shared secret
sharedSecret, err := chat.DecryptX3DHSecret(ct, panthalassaInstance.km)
if err != nil {
return "", err
}
// unmarshal message
var m chat.Message
if err := json.Unmarshal([]byte(message), &m); err != nil {
return "", err
}
return panthalassaInstance.chat.DecryptMessage(sharedSecret, m)
}
// return a encrypted shared secret used by the double rachet
func HandleInitialMessage(message, preKeyBundlePrivatePart string) (string, error) {
if panthalassaInstance == nil {
return "", errors.New("please start panthalassa first")
}
// unmarshal message
var m chat.Message
if err := json.Unmarshal([]byte(message), &m); err != nil {
return "", err
}
// unmarshal pre key bundle private part
var p chat.PreKeyBundlePrivate
if err := json.Unmarshal([]byte(preKeyBundlePrivatePart), &p); err != nil {
return "", err
}
sec, err := panthalassaInstance.chat.HandleInitialMessage(m, p)
if err != nil {
return "", err
}
ct, err := chat.EncryptX3DHSecret(sec, panthalassaInstance.km)
if err != nil {
return "", err
}
ctRaw, err := ct.Marshal()
if err != nil {
return "", err
}
return string(ctRaw), err
}