-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatter.go
232 lines (191 loc) · 7.24 KB
/
chatter.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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
// Implementation of a forward-secure, end-to-end encrypted messaging client
// supporting key compromise recovery and out-of-order message delivery.
// Directly inspired by Signal/Double-ratchet protocol but missing a few
// features. No asynchronous handshake support (pre-keys) for example.
//
// SECURITY WARNING: This code is meant for educational purposes and may
// contain vulnerabilities or other bugs. Please do not use it for
// security-critical applications.
//
// GRADING NOTES: This is the only file you need to modify for this assignment.
// You may add additional support files if desired. You should modify this file
// to implement the intended protocol, but preserve the function signatures
// for the following methods to ensure your implementation will work with
// standard test code:
//
// *NewChatter
// *EndSession
// *InitiateHandshake
// *ReturnHandshake
// *FinalizeHandshake
// *SendMessage
// *ReceiveMessage
//
// In addition, you'll need to keep all of the following structs' fields:
//
// *Chatter
// *Session
// *Message
//
// You may add fields if needed (not necessary) but don't rename or delete
// any existing fields.
//
// Original version
// Joseph Bonneau February 2019
package chatterbox
import (
// "bytes" //un-comment for helpers like bytes.equal
"encoding/binary"
"errors"
// "fmt" //un-comment if you want to do any debug printing.
)
// Labels for key derivation
// Label for generating a check key from the initial root.
// Used for verifying the results of a handshake out-of-band.
const HANDSHAKE_CHECK_LABEL byte = 0x01
// Label for ratcheting the main chain of keys
const CHAIN_LABEL = 0x02
// Label for deriving message keys from chain keys.
const KEY_LABEL = 0x03
// Chatter represents a chat participant. Each Chatter has a single long-term
// key Identity, and a map of open sessions with other users (indexed by their
// identity keys). You should not need to modify this.
type Chatter struct {
Identity *KeyPair
Sessions map[PublicKey]*Session
}
// Session represents an open session between one chatter and another.
// You should not need to modify this, though you can add additional fields
// if you want to.
type Session struct {
MyDHRatchet *KeyPair
PartnerDHRatchet *PublicKey
RootChain *SymmetricKey
SendChain *SymmetricKey
ReceiveChain *SymmetricKey
StaleReceiveKeys map[int]*SymmetricKey
SendCounter int
LastUpdate int
ReceiveCounter int
}
// Message represents a message as sent over an untrusted network.
// The first 5 fields are send unencrypted (but should be authenticated).
// The ciphertext contains the (encrypted) communication payload.
// You should not need to modify this.
type Message struct {
Sender *PublicKey
Receiver *PublicKey
NextDHRatchet *PublicKey
Counter int
LastUpdate int
Ciphertext []byte
IV []byte
}
// EncodeAdditionalData encodes all of the non-ciphertext fields of a message
// into a single byte array, suitable for use as additional authenticated data
// in an AEAD scheme. You should not need to modify this code.
func (m *Message) EncodeAdditionalData() []byte {
buf := make([]byte, 8+3*FINGERPRINT_LENGTH)
binary.LittleEndian.PutUint32(buf, uint32(m.Counter))
binary.LittleEndian.PutUint32(buf[4:], uint32(m.LastUpdate))
if m.Sender != nil {
copy(buf[8:], m.Sender.Fingerprint())
}
if m.Receiver != nil {
copy(buf[8+FINGERPRINT_LENGTH:], m.Receiver.Fingerprint())
}
if m.NextDHRatchet != nil {
copy(buf[8+2*FINGERPRINT_LENGTH:], m.NextDHRatchet.Fingerprint())
}
return buf
}
// NewChatter creates and initializes a new Chatter object. A long-term
// identity key is created and the map of sessions is initialized.
// You should not need to modify this code.
func NewChatter() *Chatter {
c := new(Chatter)
c.Identity = NewKeyPair()
c.Sessions = make(map[PublicKey]*Session)
return c
}
// EndSession erases all data for a session with the designated partner.
// All outstanding key material should be zeroized and the session erased.
func (c *Chatter) EndSession(partnerIdentity *PublicKey) error {
if _, exists := c.Sessions[*partnerIdentity]; !exists {
return errors.New("Don't have that session open to tear down")
}
// TODO: your code here
return nil
}
// InitiateHandshake prepares the first message sent in a handshake, containing
// an ephemeral DH share. The partner which initiates should be
// the first to choose a new DH ratchet value. Part of this code has been
// provided for you, you will need to fill in the key derivation code.
func (c *Chatter) InitiateHandshake(partnerIdentity *PublicKey) (*PublicKey, error) {
if _, exists := c.Sessions[*partnerIdentity]; exists {
return nil, errors.New("Already have session open")
}
c.Sessions[*partnerIdentity] = &Session{
StaleReceiveKeys: make(map[int]*SymmetricKey),
// TODO: your code here
}
// TODO: your code here
return nil, errors.New("Not implemented")
}
// ReturnHandshake prepares the first message sent in a handshake, containing
// an ephemeral DH share. Part of this code has been provided for you, you will
// need to fill in the key derivation code. The partner which calls this
// method is the responder.
func (c *Chatter) ReturnHandshake(partnerIdentity,
partnerEphemeral *PublicKey) (*PublicKey, *SymmetricKey, error) {
if _, exists := c.Sessions[*partnerIdentity]; exists {
return nil, nil, errors.New("Already have session open")
}
c.Sessions[*partnerIdentity] = &Session{
StaleReceiveKeys: make(map[int]*SymmetricKey),
// TODO: your code here
}
// TODO: your code here
return nil, nil, errors.New("Not implemented")
}
// FinalizeHandshake lets the initiator receive the responder's ephemeral key
// and finalize the handshake. Part of this code has been provided, you will
// need to fill in the key derivation code. The partner which calls this
// method is the initiator.
func (c *Chatter) FinalizeHandshake(partnerIdentity,
partnerEphemeral *PublicKey) (*SymmetricKey, error) {
if _, exists := c.Sessions[*partnerIdentity]; !exists {
return nil, errors.New("Can't finalize session, not yet open")
}
// TODO: your code here
return nil, errors.New("Not implemented")
}
// SendMessage is used to send the given plaintext string as a message.
// You'll need to implement the code to ratchet, derive keys and encrypt this message.
func (c *Chatter) SendMessage(partnerIdentity *PublicKey,
plaintext string) (*Message, error) {
if _, exists := c.Sessions[*partnerIdentity]; !exists {
return nil, errors.New("Can't send message to partner with no open session")
}
message := &Message{
Sender: &c.Identity.PublicKey,
Receiver: partnerIdentity,
// TODO: your code here
}
// TODO: your code here
return message, errors.New("Not implemented")
}
// ReceiveMessage is used to receive the given message and return the correct
// plaintext. This method is where most of the key derivation, ratcheting
// and out-of-order message handling logic happens.
func (c *Chatter) ReceiveMessage(message *Message) (string, error) {
if _, exists := c.Sessions[*message.Sender]; !exists {
return "", errors.New("Can't receive message from partner with no open session")
}
// TODO: your code here
return "", errors.New("Not implemented")
}