-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathktls_cipher_linux.go
472 lines (421 loc) · 15 KB
/
ktls_cipher_linux.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//go:build linux
// +build linux
package tls
import (
"fmt"
"net"
"syscall"
"unsafe"
)
const (
kTLS_CIPHER_AES_GCM_128 = 51
kTLS_CIPHER_AES_GCM_128_IV_SIZE = 8
kTLS_CIPHER_AES_GCM_128_KEY_SIZE = 16
kTLS_CIPHER_AES_GCM_128_SALT_SIZE = 4
kTLS_CIPHER_AES_GCM_128_TAG_SIZE = 16
kTLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE = 8
kTLS_CIPHER_AES_GCM_256 = 52
kTLS_CIPHER_AES_GCM_256_IV_SIZE = 8
kTLS_CIPHER_AES_GCM_256_KEY_SIZE = 32
kTLS_CIPHER_AES_GCM_256_SALT_SIZE = 4
kTLS_CIPHER_AES_GCM_256_TAG_SIZE = 16
kTLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE = 8
// AES_CCM_128 is not used as it has not been implemented in golang
kTLS_CIPHER_AES_CCM_128 = 53
kTLS_CIPHER_AES_CCM_128_IV_SIZE = 8
kTLS_CIPHER_AES_CCM_128_KEY_SIZE = 16
kTLS_CIPHER_AES_CCM_128_SALT_SIZE = 4
kTLS_CIPHER_AES_CCM_128_TAG_SIZE = 16
kTLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE = 8
kTLS_CIPHER_CHACHA20_POLY1305 = 54
kTLS_CIPHER_CHACHA20_POLY1305_IV_SIZE = 12
kTLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE = 32
kTLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE = 0
kTLS_CIPHER_CHACHA20_POLY1305_TAG_SIZE = 16
kTLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE = 8
)
type kTLSCryptoInfo struct {
version uint16
cipherType uint16
}
type kTLSCryptoInfoAESGCM128 struct {
info kTLSCryptoInfo
iv [kTLS_CIPHER_AES_GCM_128_IV_SIZE]byte
key [kTLS_CIPHER_AES_GCM_128_KEY_SIZE]byte
salt [kTLS_CIPHER_AES_GCM_128_SALT_SIZE]byte
recSeq [kTLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE]byte
}
type kTLSCryptoInfoAESGCM256 struct {
info kTLSCryptoInfo
iv [kTLS_CIPHER_AES_GCM_256_IV_SIZE]byte
key [kTLS_CIPHER_AES_GCM_256_KEY_SIZE]byte
salt [kTLS_CIPHER_AES_GCM_256_SALT_SIZE]byte
recSeq [kTLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE]byte
}
// AES_CCM_128 is not used as it has not been implemented in golang
type kTLSCryptoInfoAESCCM128 struct {
info kTLSCryptoInfo
iv [kTLS_CIPHER_AES_CCM_128_IV_SIZE]byte
key [kTLS_CIPHER_AES_CCM_128_KEY_SIZE]byte
salt [kTLS_CIPHER_AES_CCM_128_SALT_SIZE]byte
recSeq [kTLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE]byte
}
type kTLSCryptoInfoCHACHA20POLY1305 struct {
info kTLSCryptoInfo
iv [kTLS_CIPHER_CHACHA20_POLY1305_IV_SIZE]byte
key [kTLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE]byte
salt [kTLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE]byte
recSeq [kTLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE]byte
}
const (
kTLSCryptoInfoSize_AES_GCM_128 = 2 + 2 + kTLS_CIPHER_AES_GCM_128_IV_SIZE + kTLS_CIPHER_AES_GCM_128_KEY_SIZE +
kTLS_CIPHER_AES_GCM_128_SALT_SIZE + kTLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE
kTLSCryptoInfoSize_AES_GCM_256 = 2 + 2 + kTLS_CIPHER_AES_GCM_256_IV_SIZE + kTLS_CIPHER_AES_GCM_256_KEY_SIZE +
kTLS_CIPHER_AES_GCM_256_SALT_SIZE + kTLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE
kTLSCryptoInfoSize_AES_CCM_128 = 2 + 2 + kTLS_CIPHER_AES_CCM_128_IV_SIZE + kTLS_CIPHER_AES_CCM_128_KEY_SIZE +
kTLS_CIPHER_AES_CCM_128_SALT_SIZE + kTLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE
kTLSCryptoInfoSize_CHACHA20_POLY1305 = 2 + 2 + kTLS_CIPHER_CHACHA20_POLY1305_IV_SIZE + kTLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE +
kTLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE + kTLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE
)
func ktlsEnableAES(
c *Conn,
version uint16,
enableFunc func(c *net.TCPConn, version uint16, opt int, skip bool, key, iv, seq []byte) error,
keyLen int,
inKey, outKey, inIV, outIV []byte) error {
var ulpEnabled bool
// Try to enable Kernel TLS TX
if !kTLSSupportTX {
return nil
}
if len(outKey) == keyLen {
if tcpConn, ok := c.conn.(*net.TCPConn); ok {
if err := enableFunc(tcpConn, version, TLS_TX, ulpEnabled, outKey, outIV[:], c.out.seq[:]); err != nil {
Debugln("kTLS: TLS_TX error enabling:", err)
return err
}
ulpEnabled = true
Debugln("kTLS: TLS_TX enabled")
c.out.cipher = kTLSCipher{}
// Try to enable kTLS TX zerocopy sendfile.
// Only enabled if the hardware supports the protocol.
// Otherwise, get an error message which is fine.
ktlsEnableTxZerocopySendfile(tcpConn)
} else {
Debugln("kTLS: TLS_TX unsupported connection type")
}
} else {
Debugln("kTLS: TLS_TX unsupported key length")
}
// Try to enable Kernel TLS RX for TLS 1.2 or TLS 1.3 (TLS 1.3 RX is disabled on kernel < 5.19 )
if !kTLSSupportRX || (version == VersionTLS13 && !kTLSSupportTLS13RX) {
return nil
}
if len(inKey) == keyLen {
if tcpConn, ok := c.conn.(*net.TCPConn); ok {
if err := enableFunc(tcpConn, version, TLS_RX, ulpEnabled, inKey, inIV[:], c.in.seq[:]); err != nil {
Debugln("kTLS: TLS_RX error enabling:", err)
return err
}
Debugln("kTLS: TLS_RX enabled")
c.in.cipher = kTLSCipher{}
// Only enable the TLS_RX_EXPECT_NO_PAD for TLS 1.3
// TODO: safe to enable only if the remote end is trusted, otherwise
// it is an attack vector to doubling the TLS processing cost.
// See: https://docs.kernel.org/networking/tls.html#tls-rx-expect-no-pad
if version == VersionTLS13 {
ktlsEnableRxExpectNoPad(tcpConn)
}
} else {
Debugln("kTLS: TLS_RX unsupported connection type")
}
} else {
Debugln("kTLS: TLS_TX unsupported key length")
}
return nil
}
func ktlsEnableCHACHA20(c *Conn, version uint16, inKey, outKey, inIV, outIV []byte) error {
var ulpEnabled bool
// Try to enable Kernel TLS TX
if !kTLSSupportTX {
return nil
}
if tcpConn, ok := c.conn.(*net.TCPConn); ok {
err := ktlsEnableCHACHA20POLY1305(tcpConn, version, TLS_TX, ulpEnabled, outKey, outIV, c.out.seq[:])
if err != nil {
Debugln("kTLS: TLS_TX error enabling:", err)
return err
}
ulpEnabled = true
Debugln("kTLS: TLS_TX enabled")
c.out.cipher = kTLSCipher{}
// Try to enable kTLS TX zerocopy sendfile.
// Only enabled if the hardware supports the protocol.
// Otherwise, get an error message which is fine.
ktlsEnableTxZerocopySendfile(tcpConn)
} else {
Debugln("kTLS: TLS_TX unsupported connection type")
}
// Try to enable Kernel TLS RX for TLS 1.2 or TLS 1.3 (TLS 1.3 RX is disabled on kernel < 5.19 )
if !kTLSSupportRX || (version == VersionTLS13 && !kTLSSupportTLS13RX) {
return nil
}
if tcpConn, ok := c.conn.(*net.TCPConn); ok {
err := ktlsEnableCHACHA20POLY1305(tcpConn, version, TLS_RX, ulpEnabled, inKey[:], inIV[:], c.in.seq[:])
if err != nil {
Debugln("kTLS: TLS_RX error enabling:", err)
return err
}
Debugln("kTLS: TLS_RX enabled")
c.in.cipher = kTLSCipher{}
// Only enable the TLS_RX_EXPECT_NO_PAD for TLS 1.3
// TODO: safe to enable only if the remote end is trusted, otherwise
// it is an attack vector to doubling the TLS processing cost.
// See: https://docs.kernel.org/networking/tls.html#tls-rx-expect-no-pad
if version == VersionTLS13 {
ktlsEnableRxExpectNoPad(tcpConn)
}
} else {
Debugln("kTLS: TLS_RX unsupported connection type")
}
return nil
}
func ktlsEnableAES128GCM(c *net.TCPConn, version uint16, opt int, skip bool, key, iv, seq []byte) error {
if len(key) != kTLS_CIPHER_AES_GCM_128_KEY_SIZE {
return fmt.Errorf("kTLS: wrong key length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_128_KEY_SIZE, len(key))
}
if version == VersionTLS12 {
// The nounce of TLS 1.2 only has 4 bytes. So, compare with kTLS_CIPHER_AES_GCM_128_SALT_SIZE only
if len(iv) != kTLS_CIPHER_AES_GCM_128_SALT_SIZE {
return fmt.Errorf("kTLS: wrong iv length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_128_SALT_SIZE, len(iv))
}
if len(seq) != kTLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE {
return fmt.Errorf("kTLS: wrong seq length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE, len(seq))
}
} else {
// The nounce of TLS 1.3 only has 12 bytes. So, compare with
// kTLS_CIPHER_AES_GCM_128_SALT_SIZE + kTLS_CIPHER_AES_GCM_128_IV_SIZE
if len(iv) != kTLS_CIPHER_AES_GCM_128_SALT_SIZE+kTLS_CIPHER_AES_GCM_128_IV_SIZE {
return fmt.Errorf("kTLS: wrong iv length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_128_SALT_SIZE+kTLS_CIPHER_AES_GCM_128_IV_SIZE, len(iv))
}
if len(seq) != kTLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE {
return fmt.Errorf("kTLS: wrong seq length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE, len(seq))
}
}
cryptoInfo := kTLSCryptoInfoAESGCM128{
info: kTLSCryptoInfo{
version: version,
cipherType: kTLS_CIPHER_AES_GCM_128,
},
}
Debugf("\nkey: %x\niv: %x\nseq: %x", key, iv, seq)
copy(cryptoInfo.key[:], key)
copy(cryptoInfo.salt[:], iv[:kTLS_CIPHER_AES_GCM_128_SALT_SIZE])
// TODO https://github.com/FiloSottile/go/blob/filippo%2FkTLS/src/crypto/tls/ktls.go#L73
// the PoC of FiloSottile here is copy(cryptoInfo.iv[:], seq)
// For TLS 1.2, its IV is 0, whereas TLS 1.3 uses the rest of 8 bytes
copy(cryptoInfo.iv[:], iv[kTLS_CIPHER_AES_GCM_128_SALT_SIZE:])
copy(cryptoInfo.recSeq[:], seq)
// Assert padding isn't introduced by alignment requirements.
if unsafe.Sizeof(cryptoInfo) != kTLSCryptoInfoSize_AES_GCM_128 {
return fmt.Errorf("kTLS: wrong cryptoInfo size, desired: %d, actual: %d",
kTLSCryptoInfoSize_AES_GCM_128, unsafe.Sizeof(cryptoInfo))
}
rwc, err := c.SyscallConn()
if err != nil {
return err
}
var err0 error
err = rwc.Control(func(fd uintptr) {
if !skip {
err0 = syscall.SetsockoptString(int(fd), syscall.SOL_TCP, TCP_ULP, "tls")
if err0 != nil {
Debugln("kTLS: setsockopt(SOL_TCP, TCP_ULP) failed:", err0)
}
}
err0 = syscall.SetsockoptString(int(fd), SOL_TLS, opt,
string((*[kTLSCryptoInfoSize_AES_GCM_128]byte)(unsafe.Pointer(&cryptoInfo))[:]))
if err0 != nil {
Debugf("kTLS: setsockopt(SOL_TLS, %d) failed: %s", opt, err0)
return
}
})
if err == nil {
err = err0
}
return err
}
func ktlsEnableAES256GCM(c *net.TCPConn, version uint16, opt int, skip bool, key, iv, seq []byte) error {
if len(key) != kTLS_CIPHER_AES_GCM_256_KEY_SIZE {
return fmt.Errorf("kTLS: wrong key length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_256_KEY_SIZE, len(key))
}
if version == VersionTLS12 {
// The nounce of TLS 1.2 only has 4 bytes. So, compare with kTLS_CIPHER_AES_GCM_256_SALT_SIZE only
if len(iv) != kTLS_CIPHER_AES_GCM_256_SALT_SIZE {
return fmt.Errorf("kTLS: wrong iv length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_256_SALT_SIZE, len(iv))
}
if len(seq) != kTLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE {
return fmt.Errorf("kTLS: wrong seq length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE, len(seq))
}
} else {
// The nounce of TLS 1.3 only has 12 bytes. So, compare with
// kTLS_CIPHER_AES_GCM_256_SALT_SIZE + kTLS_CIPHER_AES_GCM_256_IV_SIZE
if len(iv) != kTLS_CIPHER_AES_GCM_256_SALT_SIZE+kTLS_CIPHER_AES_GCM_256_IV_SIZE {
return fmt.Errorf("kTLS: wrong iv length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_256_SALT_SIZE+kTLS_CIPHER_AES_GCM_256_IV_SIZE, len(iv))
}
if len(seq) != kTLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE {
return fmt.Errorf("kTLS: wrong seq length, desired: %d, actual: %d",
kTLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE, len(seq))
}
}
cryptoInfo := kTLSCryptoInfoAESGCM256{
info: kTLSCryptoInfo{
version: version,
cipherType: kTLS_CIPHER_AES_GCM_256,
},
}
Debugf("\nkey: %x\niv: %x\n seq: %x", key, iv, seq)
copy(cryptoInfo.key[:], key)
copy(cryptoInfo.salt[:], iv[:kTLS_CIPHER_AES_GCM_256_SALT_SIZE])
// TODO https://github.com/FiloSottile/go/blob/filippo%2FkTLS/src/crypto/tls/ktls.go#L73
// the PoC of FiloSottile here is copy(cryptoInfo.iv[:], seq)
// For TLS 1.2, its IV is 0, whereas TLS 1.3 uses the rest of 8 bytes
copy(cryptoInfo.iv[:], iv[kTLS_CIPHER_AES_GCM_256_SALT_SIZE:])
copy(cryptoInfo.recSeq[:], seq)
// Assert padding isn't introduced by alignment requirements.
if unsafe.Sizeof(cryptoInfo) != kTLSCryptoInfoSize_AES_GCM_256 {
return fmt.Errorf("kTLS: wrong cryptoInfo size, desired: %d, actual: %d",
kTLSCryptoInfoSize_AES_GCM_256, unsafe.Sizeof(cryptoInfo))
}
rwc, err := c.SyscallConn()
if err != nil {
return err
}
var err0 error
err = rwc.Control(func(fd uintptr) {
if !skip {
err0 = syscall.SetsockoptString(int(fd), syscall.SOL_TCP, TCP_ULP, "tls")
if err0 != nil {
Debugln("kTLS: setsockopt(SOL_TCP, TCP_ULP) failed:", err0)
return
}
}
err0 = syscall.SetsockoptString(int(fd), SOL_TLS, opt,
string((*[kTLSCryptoInfoSize_AES_GCM_256]byte)(unsafe.Pointer(&cryptoInfo))[:]))
if err0 != nil {
Debugf("kTLS: setsockopt(SOL_TLS, %d) failed: %s", opt, err0)
return
}
})
if err == nil {
err = err0
}
return err
}
func ktlsEnableCHACHA20POLY1305(c *net.TCPConn, version uint16, opt int, skip bool, key, iv, seq []byte) error {
if len(key) != kTLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE {
return fmt.Errorf("kTLS: wrong key length, desired: %d, actual: %d",
kTLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE, len(key))
}
if len(iv) != kTLS_CIPHER_CHACHA20_POLY1305_IV_SIZE {
return fmt.Errorf("kTLS: wrong iv length, desired: %d, actual: %d",
kTLS_CIPHER_CHACHA20_POLY1305_IV_SIZE, len(iv))
}
if len(seq) != kTLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE {
return fmt.Errorf("kTLS: wrong seq length, desired: %d, actual: %d",
kTLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE, len(seq))
}
cryptoInfo := kTLSCryptoInfoCHACHA20POLY1305{
info: kTLSCryptoInfo{
version: version,
cipherType: kTLS_CIPHER_CHACHA20_POLY1305,
},
}
Debugf("\nkey: %x\niv: %x\nseq: %x", key, iv, seq)
copy(cryptoInfo.key[:], key)
copy(cryptoInfo.iv[:], iv)
// the salt of CHACHA20POLY1305 is 0 bytes. So, no need to copy
copy(cryptoInfo.recSeq[:], seq)
// Assert padding isn't introduced by alignment requirements.
if unsafe.Sizeof(cryptoInfo) != kTLSCryptoInfoSize_CHACHA20_POLY1305 {
return fmt.Errorf("kTLS: wrong cryptoInfo size, desired: %d, actual: %d",
kTLSCryptoInfoSize_CHACHA20_POLY1305, unsafe.Sizeof(cryptoInfo))
}
rwc, err := c.SyscallConn()
if err != nil {
return err
}
var err0 error
err = rwc.Control(func(fd uintptr) {
if !skip {
err0 = syscall.SetsockoptString(int(fd), syscall.SOL_TCP, TCP_ULP, "tls")
if err0 != nil {
Debugln("kTLS: setsockopt(SOL_TCP, TCP_ULP) failed:", err)
return
}
}
err0 = syscall.SetsockoptString(int(fd), SOL_TLS, opt,
string((*[kTLSCryptoInfoSize_CHACHA20_POLY1305]byte)(unsafe.Pointer(&cryptoInfo))[:]))
if err0 != nil {
Debugf("kTLS: setsockopt(SOL_TLS, %d) failed: %s", opt, err)
return
}
})
if err == nil {
err = err0
}
return err
}
func ktlsEnableTxZerocopySendfile(c *net.TCPConn) (err error) {
if !kTLSSupportZEROCOPY {
return nil
}
rwc, err := c.SyscallConn()
if err != nil {
return err
}
var err0 error
err = rwc.Control(func(fd uintptr) {
err0 = syscall.SetsockoptInt(int(fd), SOL_TLS, TLS_TX_ZEROCOPY_RO, 1)
if err0 != nil {
Debugf("kTLS: TLS_TX Zerocopy Sendfile not Enabled. Error: %s", err0)
return
}
Debugln("kTLS: TLS_TX Zerocopy Sendfile Enabled")
})
if err == nil {
err = err0
}
return
}
func ktlsEnableRxExpectNoPad(c *net.TCPConn) (err error) {
if !kTLSSupportNOPAD {
return nil
}
rwc, err := c.SyscallConn()
if err != nil {
return err
}
var err0 error
err = rwc.Control(func(fd uintptr) {
err0 = syscall.SetsockoptInt(int(fd), SOL_TLS, TLS_RX_EXPECT_NO_PAD, 1)
if err0 != nil {
Debugf("kTLS: TLS_RX Expect No Pad not Enabled. Error: %s", err0)
return
}
Debugln("kTLS: TLS_RX Expect No Pad Enabled")
})
if err == nil {
err = err0
}
return
}