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