Source file
src/crypto/tls/cipher_suites.go
1
2
3
4
5 package tls
6
7 import (
8 "crypto"
9 "crypto/aes"
10 "crypto/cipher"
11 "crypto/des"
12 "crypto/hmac"
13 "crypto/internal/boring"
14 "crypto/rc4"
15 "crypto/sha1"
16 "crypto/sha256"
17 "fmt"
18 "hash"
19 "internal/cpu"
20 "runtime"
21
22 "golang.org/x/crypto/chacha20poly1305"
23 )
24
25
26
27 type CipherSuite struct {
28 ID uint16
29 Name string
30
31
32
33 SupportedVersions []uint16
34
35
36
37 Insecure bool
38 }
39
40 var (
41 supportedUpToTLS12 = []uint16{VersionTLS10, VersionTLS11, VersionTLS12}
42 supportedOnlyTLS12 = []uint16{VersionTLS12}
43 supportedOnlyTLS13 = []uint16{VersionTLS13}
44 )
45
46
47
48
49
50
51
52
53 func CipherSuites() []*CipherSuite {
54 return []*CipherSuite{
55 {TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256", supportedOnlyTLS13, false},
56 {TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384", supportedOnlyTLS13, false},
57 {TLS_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256", supportedOnlyTLS13, false},
58
59 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
60 {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
61 {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
62 {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
63 {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
64 {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
65 {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
66 {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
67 {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
68 {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
69 }
70 }
71
72
73
74
75
76
77 func InsecureCipherSuites() []*CipherSuite {
78
79
80 return []*CipherSuite{
81 {TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
82 {TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
83 {TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, true},
84 {TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, true},
85 {TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
86 {TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, true},
87 {TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, true},
88 {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
89 {TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
90 {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
91 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
92 {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
93 }
94 }
95
96
97
98
99 func CipherSuiteName(id uint16) string {
100 for _, c := range CipherSuites() {
101 if c.ID == id {
102 return c.Name
103 }
104 }
105 for _, c := range InsecureCipherSuites() {
106 if c.ID == id {
107 return c.Name
108 }
109 }
110 return fmt.Sprintf("0x%04X", id)
111 }
112
113 const (
114
115
116
117
118 suiteECDHE = 1 << iota
119
120
121
122
123 suiteECSign
124
125
126 suiteTLS12
127
128
129 suiteSHA384
130 )
131
132
133
134 type cipherSuite struct {
135 id uint16
136
137 keyLen int
138 macLen int
139 ivLen int
140 ka func(version uint16) keyAgreement
141
142 flags int
143 cipher func(key, iv []byte, isRead bool) any
144 mac func(key []byte) hash.Hash
145 aead func(key, fixedNonce []byte) aead
146 }
147
148 var cipherSuites = []*cipherSuite{
149 {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
150 {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
151 {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
152 {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadAESGCM},
153 {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
154 {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
155 {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
156 {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
157 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, cipherAES, macSHA256, nil},
158 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
159 {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
160 {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
161 {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
162 {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
163 {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
164 {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
165 {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
166 {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
167 {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
168 {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, 0, cipherRC4, macSHA1, nil},
169 {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE, cipherRC4, macSHA1, nil},
170 {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherRC4, macSHA1, nil},
171 }
172
173
174
175 func selectCipherSuite(ids, supportedIDs []uint16, ok func(*cipherSuite) bool) *cipherSuite {
176 for _, id := range ids {
177 candidate := cipherSuiteByID(id)
178 if candidate == nil || !ok(candidate) {
179 continue
180 }
181
182 for _, suppID := range supportedIDs {
183 if id == suppID {
184 return candidate
185 }
186 }
187 }
188 return nil
189 }
190
191
192
193 type cipherSuiteTLS13 struct {
194 id uint16
195 keyLen int
196 aead func(key, fixedNonce []byte) aead
197 hash crypto.Hash
198 }
199
200 var cipherSuitesTLS13 = []*cipherSuiteTLS13{
201 {TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
202 {TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
203 {TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
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 var cipherSuitesPreferenceOrder = []uint16{
271
272 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
273 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
274 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
275
276
277 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
278 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
279
280
281 TLS_RSA_WITH_AES_128_GCM_SHA256,
282 TLS_RSA_WITH_AES_256_GCM_SHA384,
283
284
285 TLS_RSA_WITH_AES_128_CBC_SHA,
286 TLS_RSA_WITH_AES_256_CBC_SHA,
287
288
289 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
290 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
291
292
293 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
294 TLS_RSA_WITH_AES_128_CBC_SHA256,
295
296
297 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
298 TLS_RSA_WITH_RC4_128_SHA,
299 }
300
301 var cipherSuitesPreferenceOrderNoAES = []uint16{
302
303 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
304
305
306 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
307 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
308
309
310 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
311 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
312 TLS_RSA_WITH_AES_128_GCM_SHA256,
313 TLS_RSA_WITH_AES_256_GCM_SHA384,
314 TLS_RSA_WITH_AES_128_CBC_SHA,
315 TLS_RSA_WITH_AES_256_CBC_SHA,
316 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
317 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
318 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
319 TLS_RSA_WITH_AES_128_CBC_SHA256,
320 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
321 TLS_RSA_WITH_RC4_128_SHA,
322 }
323
324
325 var disabledCipherSuites = map[uint16]bool{
326
327 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256: true,
328 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256: true,
329 TLS_RSA_WITH_AES_128_CBC_SHA256: true,
330
331
332 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA: true,
333 TLS_ECDHE_RSA_WITH_RC4_128_SHA: true,
334 TLS_RSA_WITH_RC4_128_SHA: true,
335 }
336
337
338
339 var rsaKexCiphers = map[uint16]bool{
340 TLS_RSA_WITH_RC4_128_SHA: true,
341 TLS_RSA_WITH_3DES_EDE_CBC_SHA: true,
342 TLS_RSA_WITH_AES_128_CBC_SHA: true,
343 TLS_RSA_WITH_AES_256_CBC_SHA: true,
344 TLS_RSA_WITH_AES_128_CBC_SHA256: true,
345 TLS_RSA_WITH_AES_128_GCM_SHA256: true,
346 TLS_RSA_WITH_AES_256_GCM_SHA384: true,
347 }
348
349 var defaultCipherSuites []uint16
350 var defaultCipherSuitesWithRSAKex []uint16
351
352 func init() {
353 defaultCipherSuites = make([]uint16, 0, len(cipherSuitesPreferenceOrder))
354 defaultCipherSuitesWithRSAKex = make([]uint16, 0, len(cipherSuitesPreferenceOrder))
355 for _, c := range cipherSuitesPreferenceOrder {
356 if disabledCipherSuites[c] {
357 continue
358 }
359 if !rsaKexCiphers[c] {
360 defaultCipherSuites = append(defaultCipherSuites, c)
361 }
362 defaultCipherSuitesWithRSAKex = append(defaultCipherSuitesWithRSAKex, c)
363 }
364 }
365
366
367
368
369 var defaultCipherSuitesTLS13 = []uint16{
370 TLS_AES_128_GCM_SHA256,
371 TLS_AES_256_GCM_SHA384,
372 TLS_CHACHA20_POLY1305_SHA256,
373 }
374
375 var defaultCipherSuitesTLS13NoAES = []uint16{
376 TLS_CHACHA20_POLY1305_SHA256,
377 TLS_AES_128_GCM_SHA256,
378 TLS_AES_256_GCM_SHA384,
379 }
380
381 var (
382 hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
383 hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
384
385 hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
386 (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
387
388 hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
389 runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
390 runtime.GOARCH == "s390x" && hasGCMAsmS390X
391 )
392
393 var aesgcmCiphers = map[uint16]bool{
394
395 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: true,
396 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: true,
397 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: true,
398 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: true,
399
400 TLS_AES_128_GCM_SHA256: true,
401 TLS_AES_256_GCM_SHA384: true,
402 }
403
404
405
406 func aesgcmPreferred(ciphers []uint16) bool {
407 for _, cID := range ciphers {
408 if c := cipherSuiteByID(cID); c != nil {
409 return aesgcmCiphers[cID]
410 }
411 if c := cipherSuiteTLS13ByID(cID); c != nil {
412 return aesgcmCiphers[cID]
413 }
414 }
415 return false
416 }
417
418 func cipherRC4(key, iv []byte, isRead bool) any {
419 cipher, _ := rc4.NewCipher(key)
420 return cipher
421 }
422
423 func cipher3DES(key, iv []byte, isRead bool) any {
424 block, _ := des.NewTripleDESCipher(key)
425 if isRead {
426 return cipher.NewCBCDecrypter(block, iv)
427 }
428 return cipher.NewCBCEncrypter(block, iv)
429 }
430
431 func cipherAES(key, iv []byte, isRead bool) any {
432 block, _ := aes.NewCipher(key)
433 if isRead {
434 return cipher.NewCBCDecrypter(block, iv)
435 }
436 return cipher.NewCBCEncrypter(block, iv)
437 }
438
439
440 func macSHA1(key []byte) hash.Hash {
441 h := sha1.New
442
443
444 if !boring.Enabled {
445 h = newConstantTimeHash(h)
446 }
447 return hmac.New(h, key)
448 }
449
450
451
452 func macSHA256(key []byte) hash.Hash {
453 return hmac.New(sha256.New, key)
454 }
455
456 type aead interface {
457 cipher.AEAD
458
459
460
461
462 explicitNonceLen() int
463 }
464
465 const (
466 aeadNonceLength = 12
467 noncePrefixLength = 4
468 )
469
470
471
472 type prefixNonceAEAD struct {
473
474 nonce [aeadNonceLength]byte
475 aead cipher.AEAD
476 }
477
478 func (f *prefixNonceAEAD) NonceSize() int { return aeadNonceLength - noncePrefixLength }
479 func (f *prefixNonceAEAD) Overhead() int { return f.aead.Overhead() }
480 func (f *prefixNonceAEAD) explicitNonceLen() int { return f.NonceSize() }
481
482 func (f *prefixNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
483 copy(f.nonce[4:], nonce)
484 return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
485 }
486
487 func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
488 copy(f.nonce[4:], nonce)
489 return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
490 }
491
492
493
494 type xorNonceAEAD struct {
495 nonceMask [aeadNonceLength]byte
496 aead cipher.AEAD
497 }
498
499 func (f *xorNonceAEAD) NonceSize() int { return 8 }
500 func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
501 func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
502
503 func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
504 for i, b := range nonce {
505 f.nonceMask[4+i] ^= b
506 }
507 result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
508 for i, b := range nonce {
509 f.nonceMask[4+i] ^= b
510 }
511
512 return result
513 }
514
515 func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
516 for i, b := range nonce {
517 f.nonceMask[4+i] ^= b
518 }
519 result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
520 for i, b := range nonce {
521 f.nonceMask[4+i] ^= b
522 }
523
524 return result, err
525 }
526
527 func aeadAESGCM(key, noncePrefix []byte) aead {
528 if len(noncePrefix) != noncePrefixLength {
529 panic("tls: internal error: wrong nonce length")
530 }
531 aes, err := aes.NewCipher(key)
532 if err != nil {
533 panic(err)
534 }
535 var aead cipher.AEAD
536 if boring.Enabled {
537 aead, err = boring.NewGCMTLS(aes)
538 } else {
539 boring.Unreachable()
540 aead, err = cipher.NewGCM(aes)
541 }
542 if err != nil {
543 panic(err)
544 }
545
546 ret := &prefixNonceAEAD{aead: aead}
547 copy(ret.nonce[:], noncePrefix)
548 return ret
549 }
550
551 func aeadAESGCMTLS13(key, nonceMask []byte) aead {
552 if len(nonceMask) != aeadNonceLength {
553 panic("tls: internal error: wrong nonce length")
554 }
555 aes, err := aes.NewCipher(key)
556 if err != nil {
557 panic(err)
558 }
559 aead, err := cipher.NewGCM(aes)
560 if err != nil {
561 panic(err)
562 }
563
564 ret := &xorNonceAEAD{aead: aead}
565 copy(ret.nonceMask[:], nonceMask)
566 return ret
567 }
568
569 func aeadChaCha20Poly1305(key, nonceMask []byte) aead {
570 if len(nonceMask) != aeadNonceLength {
571 panic("tls: internal error: wrong nonce length")
572 }
573 aead, err := chacha20poly1305.New(key)
574 if err != nil {
575 panic(err)
576 }
577
578 ret := &xorNonceAEAD{aead: aead}
579 copy(ret.nonceMask[:], nonceMask)
580 return ret
581 }
582
583 type constantTimeHash interface {
584 hash.Hash
585 ConstantTimeSum(b []byte) []byte
586 }
587
588
589
590 type cthWrapper struct {
591 h constantTimeHash
592 }
593
594 func (c *cthWrapper) Size() int { return c.h.Size() }
595 func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
596 func (c *cthWrapper) Reset() { c.h.Reset() }
597 func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
598 func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
599
600 func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
601 boring.Unreachable()
602 return func() hash.Hash {
603 return &cthWrapper{h().(constantTimeHash)}
604 }
605 }
606
607
608 func tls10MAC(h hash.Hash, out, seq, header, data, extra []byte) []byte {
609 h.Reset()
610 h.Write(seq)
611 h.Write(header)
612 h.Write(data)
613 res := h.Sum(out)
614 if extra != nil {
615 h.Write(extra)
616 }
617 return res
618 }
619
620 func rsaKA(version uint16) keyAgreement {
621 return rsaKeyAgreement{}
622 }
623
624 func ecdheECDSAKA(version uint16) keyAgreement {
625 return &ecdheKeyAgreement{
626 isRSA: false,
627 version: version,
628 }
629 }
630
631 func ecdheRSAKA(version uint16) keyAgreement {
632 return &ecdheKeyAgreement{
633 isRSA: true,
634 version: version,
635 }
636 }
637
638
639
640 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
641 for _, id := range have {
642 if id == want {
643 return cipherSuiteByID(id)
644 }
645 }
646 return nil
647 }
648
649 func cipherSuiteByID(id uint16) *cipherSuite {
650 for _, cipherSuite := range cipherSuites {
651 if cipherSuite.id == id {
652 return cipherSuite
653 }
654 }
655 return nil
656 }
657
658 func mutualCipherSuiteTLS13(have []uint16, want uint16) *cipherSuiteTLS13 {
659 for _, id := range have {
660 if id == want {
661 return cipherSuiteTLS13ByID(id)
662 }
663 }
664 return nil
665 }
666
667 func cipherSuiteTLS13ByID(id uint16) *cipherSuiteTLS13 {
668 for _, cipherSuite := range cipherSuitesTLS13 {
669 if cipherSuite.id == id {
670 return cipherSuite
671 }
672 }
673 return nil
674 }
675
676
677
678
679
680 const (
681
682 TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
683 TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
684 TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
685 TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
686 TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
687 TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
688 TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
689 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
690 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
691 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
692 TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
693 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
694 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
695 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
696 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
697 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
698 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
699 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
700 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
701 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
702 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca8
703 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9
704
705
706 TLS_AES_128_GCM_SHA256 uint16 = 0x1301
707 TLS_AES_256_GCM_SHA384 uint16 = 0x1302
708 TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
709
710
711
712 TLS_FALLBACK_SCSV uint16 = 0x5600
713
714
715
716 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
717 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
718 )
719
View as plain text