-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_server.go
More file actions
170 lines (142 loc) · 4.63 KB
/
session_server.go
File metadata and controls
170 lines (142 loc) · 4.63 KB
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
package qtunnul
import (
"crypto/mlkem"
"crypto/rand"
"fmt"
"sync"
"golang.org/x/crypto/chacha20poly1305"
)
type ServerSession struct {
attestationKey []byte
sharedSecretReceived []byte
shaedSecretSendTo []byte
cipherText []byte
nonce []byte
nonceCount uint64
nonceCountLock sync.Mutex
bitmap *Bitmap
}
func NewSession(attestationKeySeed []byte) (*ServerSession, error) {
nonce := make([]byte, chacha20poly1305.NonceSizeX-8)
if _, err := rand.Read(nonce); err != nil {
return nil, fmt.Errorf("failed to generate nonce: %w", err)
}
return &ServerSession{
attestationKey: attestationKeySeed,
bitmap: &Bitmap{},
nonce: nonce,
}, nil
}
func boxAttestionResponse(cipherText, signature []byte) []byte {
response := make([]byte, 4+len(cipherText)+len(signature))
response[0] = byte(len(cipherText) >> 24)
response[1] = byte(len(cipherText) >> 16)
response[2] = byte(len(cipherText) >> 8)
response[3] = byte(len(cipherText))
copy(response[4:], cipherText)
copy(response[4+len(cipherText):], signature)
return response
}
func unboxAttestionResponse(response []byte) (cipherText, signature []byte, err error) {
if len(response) < 4 {
return nil, nil, fmt.Errorf("response too short")
}
cipherTextLen := (uint32(response[0]) << 24) | (uint32(response[1]) << 16) | (uint32(response[2]) << 8) | uint32(response[3])
if len(response) < int(4+cipherTextLen) {
return nil, nil, fmt.Errorf("response too short for ciphertext")
}
cipherText = response[4 : 4+cipherTextLen]
signature = response[4+cipherTextLen:]
return cipherText, signature, nil
}
func (ss *ServerSession) ProcessAttestationAndKeyExchange(challenge []byte) ([]byte, error) {
if len(challenge) < mlkem.EncapsulationKeySize1024 {
return nil, fmt.Errorf("challenge too short")
}
encapsulationKey, err := mlkem.NewEncapsulationKey1024(challenge[:mlkem.EncapsulationKeySize1024])
if err != nil {
return nil, fmt.Errorf("failed to create encapsulation key: %w", err)
}
sharedSecret, cipherText := encapsulationKey.Encapsulate()
keys, err := DeriveKey(sharedSecret, []byte("qtunnul-session-keys"), 64)
if err != nil {
return nil, fmt.Errorf("failed to derive session keys: %w", err)
}
ss.sharedSecretReceived = keys[:32]
ss.shaedSecretSendTo = keys[32:]
ss.cipherText = cipherText
signature, err := SignServerAttestation(ss.attestationKey, cipherText)
if err != nil {
return nil, fmt.Errorf("failed to sign attestation: %w", err)
}
return boxAttestionResponse(cipherText, signature), nil
}
func countToBytes(count uint64) []byte {
b := make([]byte, 8)
for i := uint(0); i < 8; i++ {
b[7-i] = byte(count & 0xFF)
count >>= 8
}
return b
}
func bytesToCount(b []byte) uint64 {
var count uint64
for i := 0; i < 8; i++ {
count <<= 8
count |= uint64(b[i])
}
return count
}
func (ss *ServerSession) NextNonce() ([]byte, error) {
ss.nonceCountLock.Lock()
count := ss.nonceCount
ss.nonceCount++
if ss.nonceCount == 0 {
newNonce := make([]byte, chacha20poly1305.NonceSizeX-8)
if _, err := rand.Read(newNonce); err != nil {
ss.nonceCountLock.Unlock()
return nil, fmt.Errorf("failed to generate new nonce: %w", err)
}
ss.nonce = newNonce
}
ss.nonceCountLock.Unlock()
countBytes := countToBytes(count)
nonce := make([]byte, chacha20poly1305.NonceSizeX)
copy(nonce[:chacha20poly1305.NonceSizeX-8], ss.nonce)
copy(nonce[chacha20poly1305.NonceSizeX-8:], countBytes)
return nonce, nil
}
func (ss *ServerSession) Encrypt(plaintext []byte) ([]byte, error) {
aead, err := chacha20poly1305.NewX(ss.shaedSecretSendTo)
if err != nil {
return nil, fmt.Errorf("failed to create AEAD: %w", err)
}
nonce, err := ss.NextNonce()
if err != nil {
return nil, fmt.Errorf("failed to get next nonce: %w", err)
}
ciphertext := aead.Seal(nil, nonce, plaintext, nil)
result := make([]byte, len(nonce)+len(ciphertext))
copy(result, nonce)
copy(result[len(nonce):], ciphertext)
return result, nil
}
func (ss *ServerSession) Decrypt(data []byte) ([]byte, error) {
if len(data) < chacha20poly1305.NonceSizeX {
return nil, fmt.Errorf("data too short")
}
nonce, ciphertext := data[:chacha20poly1305.NonceSizeX], data[chacha20poly1305.NonceSizeX:]
count := bytesToCount(nonce[chacha20poly1305.NonceSizeX-8:])
if !ss.bitmap.Add(count) {
return nil, fmt.Errorf("replayed or old packet: %d", count)
}
aead, err := chacha20poly1305.NewX(ss.sharedSecretReceived)
if err != nil {
return nil, fmt.Errorf("failed to create AEAD: %w", err)
}
plaintext, err := aead.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("decryption failed: %w", err)
}
return plaintext, nil
}