This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
/
session.go
313 lines (270 loc) · 9.21 KB
/
session.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
304
305
306
307
308
309
310
311
312
313
// client.go - Reunion Cryptographic client.
// Copyright (C) 2019 David Stainton.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package crypto
import (
"github.com/awnumar/memguard"
"github.com/katzenpost/core/crypto/rand"
"github.com/ugorji/go/codec"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/hkdf"
)
var cborHandle = new(codec.CborHandle)
const (
type1Message = "type-1"
type2Message = "type-2"
type3Message = "type-3"
// SharedEpochKeySize is the length of the shared epoch key.
SharedEpochKeySize = 32
)
type serializableSession struct {
Epoch uint64
SharedRandomValue []byte
Keypair1 *Keypair
Keypair2 *Keypair
SessionKey1 []byte
SessionKey2 []byte
SharedEpochKey []byte
}
// Session encapsulates all the key material
// and provides a few methods for performing
// core cryptographic operations that form the
// Reunion protocol. Note that this so called
// Session does NOT keep any state. Therefore
// these client methods are supplemented with
// some helper functions defined in crypto.go
type Session struct {
epoch uint64
sharedRandomValue []byte
keypair1 *Keypair
keypair2 *Keypair
sessionKey1 *memguard.LockedBuffer
sessionKey2 *memguard.LockedBuffer
sharedEpochKey *memguard.LockedBuffer
}
// NewSessionFromKey creates a new client given a shared epoch key.
func NewSessionFromKey(sharedEpochKey *[SharedEpochKeySize]byte, sharedRandomValue []byte, epoch uint64) (*Session, error) {
keypair1, err := NewKeypair(true)
if err != nil {
return nil, err
}
keypair2, err := NewKeypair(false)
if err != nil {
return nil, err
}
sk1, err := memguard.NewBufferFromReader(rand.Reader, 32)
if err != nil {
memguard.SafePanic(err)
}
sk2, err := memguard.NewBufferFromReader(rand.Reader, 32)
if err != nil {
memguard.SafePanic(err)
}
client := &Session{
epoch: epoch,
sharedRandomValue: sharedRandomValue,
keypair1: keypair1,
keypair2: keypair2,
sessionKey1: sk1,
sessionKey2: sk2,
sharedEpochKey: memguard.NewBufferFromBytes(sharedEpochKey[:]),
}
return client, nil
}
// NewSession creates a new client given a shared passphrase, shared random value and an epoch number.
func NewSession(passphrase []byte, sharedRandomValue []byte, epoch uint64) (*Session, error) {
salt := getSalt(sharedRandomValue, epoch)
// XXX how many iterations should we use?
// This makes it run for 2.2s on my crappy laptop.
t := uint32(250)
memory := uint32(9001)
threads := uint8(1)
key := argon2.IDKey(passphrase, salt, t, memory, threads, SharedEpochKeySize)
k := [SharedEpochKeySize]byte{}
copy(k[:], key)
memguard.WipeBytes(key)
return NewSessionFromKey(&k, sharedRandomValue, epoch)
}
// Epoch returns the epoch.
func (c *Session) Epoch() uint64 {
return c.epoch
}
// SharedRandom returns the shared random value.
func (c *Session) SharedRandom() []byte {
return c.sharedRandomValue
}
// Destroy destroys all the Session's key material
// and frees up the memory.
func (c *Session) Destroy() {
c.keypair1.Destroy()
c.keypair2.Destroy()
c.sessionKey1.Destroy()
c.sessionKey2.Destroy()
c.sharedEpochKey.Destroy()
}
// GenerateType1Message generates a Type 1 message.
func (c *Session) GenerateType1Message(payload []byte) ([]byte, error) {
keypair1ElligatorPub := c.keypair1.Representative().Bytes()
k1, _, err := deriveSprpKey(type1Message, c.sharedRandomValue, c.epoch, c.sharedEpochKey.Bytes())
if err != nil {
return nil, err
}
iv := [SPRPIVLength]byte{}
alpha := SPRPEncrypt(k1, &iv, keypair1ElligatorPub[:])
beta, err := newT1Beta(c.keypair2.Public().Bytes(), c.sessionKey1.ByteArray32())
if err != nil {
return nil, err
}
gamma, err := newT1Gamma(payload[:], c.sessionKey2.ByteArray32())
if err != nil {
return nil, err
}
output := []byte{}
output = append(output, alpha...)
output = append(output, beta...)
output = append(output, gamma...)
return output, nil
}
// ProcessType1MessageAlpha processes the alpha portion of a type one message.
func (c *Session) ProcessType1MessageAlpha(alpha []byte) ([]byte, *PublicKey, error) {
k1, _, err := deriveSprpKey(type1Message, c.sharedRandomValue, c.epoch, c.sharedEpochKey.Bytes())
if err != nil {
return nil, nil, err
}
iv := [SPRPIVLength]byte{}
elligatorPub1 := SPRPDecrypt(k1, &iv, alpha)
rKey := [RepresentativeLength]byte{}
copy(rKey[:], elligatorPub1)
r := Representative(rKey)
b1PubKey := r.ToPublic()
// T2 message construction:
k2Outer, hkdfContext, err := deriveSprpKey(type2Message, c.sharedRandomValue, c.epoch, c.sharedEpochKey.Bytes())
if err != nil {
return nil, nil, err
}
k2idh := [32]byte{}
c.keypair1.Private().Exp(&k2idh, b1PubKey)
salt := getSalt(c.sharedRandomValue, c.epoch)
prk2i := hkdf.Extract(HashFunc, k2idh[:], salt)
kdfReader := hkdf.Expand(HashFunc, prk2i, hkdfContext)
k2Inner := [SPRPKeyLength]byte{}
_, err = kdfReader.Read(k2Inner[:])
if err != nil {
return nil, nil, err
}
k2InnerIV := [SPRPIVLength]byte{}
k2OuterIV := [SPRPIVLength]byte{}
t2 := SPRPEncrypt(k2Outer, &k2OuterIV, SPRPEncrypt(&k2Inner, &k2InnerIV, c.sessionKey1.Bytes()))
return t2, b1PubKey, nil
}
// GetCandidateKey extracts a candidate key from a type two message.
func (c *Session) GetCandidateKey(t2 []byte, alpha *PublicKey) ([]byte, error) {
k3Outer, hkdfContext, err := deriveSprpKey(type2Message, c.sharedRandomValue, c.epoch, c.sharedEpochKey.Bytes())
if err != nil {
return nil, err
}
// DH operation
k3idh := [32]byte{}
c.keypair1.Private().Exp(&k3idh, alpha)
// HKDF extract and expand
salt := getSalt(c.sharedRandomValue, c.epoch)
prk3i := hkdf.Extract(HashFunc, k3idh[:], salt)
kdfReader := hkdf.Expand(HashFunc, prk3i, hkdfContext)
k3Inner := [SPRPKeyLength]byte{}
_, err = kdfReader.Read(k3Inner[:])
if err != nil {
return nil, err
}
k3InnerIV := [SPRPIVLength]byte{}
k3OuterIV := [SPRPIVLength]byte{}
return SPRPDecrypt(&k3Inner, &k3InnerIV, SPRPDecrypt(k3Outer, &k3OuterIV, t2)), nil
}
// ComposeType3Message composes a type three message.
func (c *Session) ComposeType3Message(beta2 *PublicKey) ([]byte, error) {
k3Outer, hkdfContext, err := deriveSprpKey(type3Message, c.sharedRandomValue, c.epoch, c.sharedEpochKey.Bytes())
if err != nil {
return nil, err
}
dh := [32]byte{}
c.keypair2.Private().Exp(&dh, beta2)
salt := getSalt(c.sharedRandomValue, c.epoch)
prk3i := hkdf.Extract(HashFunc, dh[:], salt)
kdfReader := hkdf.Expand(HashFunc, prk3i, hkdfContext)
k3Inner := [SPRPKeyLength]byte{}
_, err = kdfReader.Read(k3Inner[:])
if err != nil {
return nil, err
}
k3InnerIV := [SPRPIVLength]byte{}
k3OuterIV := [SPRPIVLength]byte{}
t3 := SPRPEncrypt(k3Outer, &k3OuterIV, SPRPEncrypt(&k3Inner, &k3InnerIV, c.sessionKey2.Bytes()))
return t3, nil
}
// ProcessType3Message processes a type three message.
func (c *Session) ProcessType3Message(t3, gamma []byte, beta2 *PublicKey) ([]byte, error) {
k3Outer, hkdfContext, err := deriveSprpKey(type3Message, c.sharedRandomValue, c.epoch, c.sharedEpochKey.Bytes())
if err != nil {
return nil, err
}
dh := [32]byte{}
c.keypair2.Private().Exp(&dh, beta2)
salt := getSalt(c.sharedRandomValue, c.epoch)
prk3i := hkdf.Extract(HashFunc, dh[:], salt)
kdfReader := hkdf.Expand(HashFunc, prk3i, hkdfContext)
k3Inner := [SPRPKeyLength]byte{}
_, err = kdfReader.Read(k3Inner[:])
if err != nil {
return nil, err
}
k3InnerIV := [SPRPIVLength]byte{}
k3OuterIV := [SPRPIVLength]byte{}
gammaKey := SPRPDecrypt(&k3Inner, &k3InnerIV, SPRPDecrypt(k3Outer, &k3OuterIV, t3))
payload, err := decryptT1Gamma(gammaKey, gamma)
if err != nil {
return nil, err
}
return payload, nil
}
// Marshal serializes the client key material.
func (c *Session) MarshalBinary() ([]byte, error) {
var serialized []byte
cc := serializableSession{
Epoch: c.epoch,
SharedRandomValue: c.sharedRandomValue,
Keypair1: c.keypair1,
Keypair2: c.keypair2,
SessionKey1: c.sessionKey1.Bytes(),
SessionKey2: c.sessionKey2.Bytes(),
SharedEpochKey: c.sharedEpochKey.Bytes(),
}
err := codec.NewEncoderBytes(&serialized, cborHandle).Encode(&cc)
return serialized, err
}
// Unmarshal deserializes the client key material.
func (c *Session) UnmarshalBinary(data []byte) error {
cc := new(serializableSession)
err := codec.NewDecoderBytes(data, cborHandle).Decode(cc)
if err != nil {
return err
}
c.epoch = cc.Epoch
c.sharedRandomValue = cc.SharedRandomValue
c.keypair1 = cc.Keypair1
c.keypair2 = cc.Keypair2
c.sessionKey1 = memguard.NewBufferFromBytes(cc.SessionKey1)
c.sessionKey2 = memguard.NewBufferFromBytes(cc.SessionKey2)
c.sharedEpochKey = memguard.NewBufferFromBytes(cc.SharedEpochKey)
return nil
}