]> Cypherpunks.ru repositories - gostls13.git/blob - src/crypto/tls/cipher_suites.go
[dev.boringcrypto] all: merge master (5b76343) into dev.boringcrypto
[gostls13.git] / src / crypto / tls / cipher_suites.go
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package tls
6
7 import "crypto/internal/boring"
8
9 import (
10         "crypto"
11         "crypto/aes"
12         "crypto/cipher"
13         "crypto/des"
14         "crypto/hmac"
15         "crypto/rc4"
16         "crypto/sha1"
17         "crypto/sha256"
18         "crypto/x509"
19         "fmt"
20         "hash"
21
22         "golang.org/x/crypto/chacha20poly1305"
23 )
24
25 // CipherSuite is a TLS cipher suite. Note that most functions in this package
26 // accept and expose cipher suite IDs instead of this type.
27 type CipherSuite struct {
28         ID   uint16
29         Name string
30
31         // Supported versions is the list of TLS protocol versions that can
32         // negotiate this cipher suite.
33         SupportedVersions []uint16
34
35         // Insecure is true if the cipher suite has known security issues
36         // due to its primitives, design, or implementation.
37         Insecure bool
38 }
39
40 var (
41         supportedUpToTLS12 = []uint16{VersionTLS10, VersionTLS11, VersionTLS12}
42         supportedOnlyTLS12 = []uint16{VersionTLS12}
43         supportedOnlyTLS13 = []uint16{VersionTLS13}
44 )
45
46 // CipherSuites returns a list of cipher suites currently implemented by this
47 // package, excluding those with security issues, which are returned by
48 // InsecureCipherSuites.
49 //
50 // The list is sorted by ID. Note that the default cipher suites selected by
51 // this package might depend on logic that can't be captured by a static list.
52 func CipherSuites() []*CipherSuite {
53         return []*CipherSuite{
54                 {TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, false},
55                 {TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
56                 {TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
57                 {TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
58                 {TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
59
60                 {TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256", supportedOnlyTLS13, false},
61                 {TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384", supportedOnlyTLS13, false},
62                 {TLS_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256", supportedOnlyTLS13, false},
63
64                 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
65                 {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
66                 {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, false},
67                 {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
68                 {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
69                 {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
70                 {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
71                 {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
72                 {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
73                 {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
74                 {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
75         }
76 }
77
78 // InsecureCipherSuites returns a list of cipher suites currently implemented by
79 // this package and which have security issues.
80 //
81 // Most applications should not use the cipher suites in this list, and should
82 // only use those returned by CipherSuites.
83 func InsecureCipherSuites() []*CipherSuite {
84         // RC4 suites are broken because RC4 is.
85         // CBC-SHA256 suites have no Lucky13 countermeasures.
86         return []*CipherSuite{
87                 {TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
88                 {TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
89                 {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
90                 {TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_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 // CipherSuiteName returns the standard name for the passed cipher suite ID
97 // (e.g. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"), or a fallback representation
98 // of the ID value if the cipher suite is not implemented by this package.
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 // a keyAgreement implements the client and server side of a TLS key agreement
114 // protocol by generating and processing key exchange messages.
115 type keyAgreement interface {
116         // On the server side, the first two methods are called in order.
117
118         // In the case that the key agreement protocol doesn't use a
119         // ServerKeyExchange message, generateServerKeyExchange can return nil,
120         // nil.
121         generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
122         processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
123
124         // On the client side, the next two methods are called in order.
125
126         // This method may not be called if the server doesn't send a
127         // ServerKeyExchange message.
128         processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
129         generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
130 }
131
132 const (
133         // suiteECDHE indicates that the cipher suite involves elliptic curve
134         // Diffie-Hellman. This means that it should only be selected when the
135         // client indicates that it supports ECC with a curve and point format
136         // that we're happy with.
137         suiteECDHE = 1 << iota
138         // suiteECSign indicates that the cipher suite involves an ECDSA or
139         // EdDSA signature and therefore may only be selected when the server's
140         // certificate is ECDSA or EdDSA. If this is not set then the cipher suite
141         // is RSA based.
142         suiteECSign
143         // suiteTLS12 indicates that the cipher suite should only be advertised
144         // and accepted when using TLS 1.2.
145         suiteTLS12
146         // suiteSHA384 indicates that the cipher suite uses SHA384 as the
147         // handshake hash.
148         suiteSHA384
149         // suiteDefaultOff indicates that this cipher suite is not included by
150         // default.
151         suiteDefaultOff
152 )
153
154 // A cipherSuite is a specific combination of key agreement, cipher and MAC function.
155 type cipherSuite struct {
156         id uint16
157         // the lengths, in bytes, of the key material needed for each component.
158         keyLen int
159         macLen int
160         ivLen  int
161         ka     func(version uint16) keyAgreement
162         // flags is a bitmask of the suite* values, above.
163         flags  int
164         cipher func(key, iv []byte, isRead bool) interface{}
165         mac    func(key []byte) hash.Hash
166         aead   func(key, fixedNonce []byte) aead
167 }
168
169 var cipherSuites = []*cipherSuite{
170         // Ciphersuite order is chosen so that ECDHE comes before plain RSA and
171         // AEADs are the top preference.
172         {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
173         {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
174         {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
175         {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadAESGCM},
176         {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
177         {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
178         {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
179         {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
180         {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
181         {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
182         {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
183         {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
184         {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
185         {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
186         {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
187         {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
188         {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
189         {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
190         {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
191
192         // RC4-based cipher suites are disabled by default.
193         {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
194         {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
195         {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteDefaultOff, cipherRC4, macSHA1, nil},
196 }
197
198 // selectCipherSuite returns the first cipher suite from ids which is also in
199 // supportedIDs and passes the ok filter.
200 func selectCipherSuite(ids, supportedIDs []uint16, ok func(*cipherSuite) bool) *cipherSuite {
201         for _, id := range ids {
202                 candidate := cipherSuiteByID(id)
203                 if candidate == nil || !ok(candidate) {
204                         continue
205                 }
206
207                 for _, suppID := range supportedIDs {
208                         if id == suppID {
209                                 return candidate
210                         }
211                 }
212         }
213         return nil
214 }
215
216 // A cipherSuiteTLS13 defines only the pair of the AEAD algorithm and hash
217 // algorithm to be used with HKDF. See RFC 8446, Appendix B.4.
218 type cipherSuiteTLS13 struct {
219         id     uint16
220         keyLen int
221         aead   func(key, fixedNonce []byte) aead
222         hash   crypto.Hash
223 }
224
225 var cipherSuitesTLS13 = []*cipherSuiteTLS13{
226         {TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
227         {TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
228         {TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
229 }
230
231 func cipherRC4(key, iv []byte, isRead bool) interface{} {
232         cipher, _ := rc4.NewCipher(key)
233         return cipher
234 }
235
236 func cipher3DES(key, iv []byte, isRead bool) interface{} {
237         block, _ := des.NewTripleDESCipher(key)
238         if isRead {
239                 return cipher.NewCBCDecrypter(block, iv)
240         }
241         return cipher.NewCBCEncrypter(block, iv)
242 }
243
244 func cipherAES(key, iv []byte, isRead bool) interface{} {
245         block, _ := aes.NewCipher(key)
246         if isRead {
247                 return cipher.NewCBCDecrypter(block, iv)
248         }
249         return cipher.NewCBCEncrypter(block, iv)
250 }
251
252 // macSHA1 returns a SHA-1 based constant time MAC.
253 func macSHA1(key []byte) hash.Hash {
254         h := sha1.New
255         // The BoringCrypto SHA1 does not have a constant-time
256         // checksum function, so don't try to use it.
257         if !boring.Enabled {
258                 h = newConstantTimeHash(h)
259         }
260         return hmac.New(h, key)
261 }
262
263 // macSHA256 returns a SHA-256 based MAC. This is only supported in TLS 1.2 and
264 // is currently only used in disabled-by-default cipher suites.
265 func macSHA256(key []byte) hash.Hash {
266         return hmac.New(sha256.New, key)
267 }
268
269 type aead interface {
270         cipher.AEAD
271
272         // explicitNonceLen returns the number of bytes of explicit nonce
273         // included in each record. This is eight for older AEADs and
274         // zero for modern ones.
275         explicitNonceLen() int
276 }
277
278 const (
279         aeadNonceLength   = 12
280         noncePrefixLength = 4
281 )
282
283 // prefixNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
284 // each call.
285 type prefixNonceAEAD struct {
286         // nonce contains the fixed part of the nonce in the first four bytes.
287         nonce [aeadNonceLength]byte
288         aead  cipher.AEAD
289 }
290
291 func (f *prefixNonceAEAD) NonceSize() int        { return aeadNonceLength - noncePrefixLength }
292 func (f *prefixNonceAEAD) Overhead() int         { return f.aead.Overhead() }
293 func (f *prefixNonceAEAD) explicitNonceLen() int { return f.NonceSize() }
294
295 func (f *prefixNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
296         copy(f.nonce[4:], nonce)
297         return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
298 }
299
300 func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
301         copy(f.nonce[4:], nonce)
302         return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
303 }
304
305 // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
306 // before each call.
307 type xorNonceAEAD struct {
308         nonceMask [aeadNonceLength]byte
309         aead      cipher.AEAD
310 }
311
312 func (f *xorNonceAEAD) NonceSize() int        { return 8 } // 64-bit sequence number
313 func (f *xorNonceAEAD) Overhead() int         { return f.aead.Overhead() }
314 func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
315
316 func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
317         for i, b := range nonce {
318                 f.nonceMask[4+i] ^= b
319         }
320         result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
321         for i, b := range nonce {
322                 f.nonceMask[4+i] ^= b
323         }
324
325         return result
326 }
327
328 func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
329         for i, b := range nonce {
330                 f.nonceMask[4+i] ^= b
331         }
332         result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
333         for i, b := range nonce {
334                 f.nonceMask[4+i] ^= b
335         }
336
337         return result, err
338 }
339
340 type gcmtls interface {
341         NewGCMTLS() (cipher.AEAD, error)
342 }
343
344 func aeadAESGCM(key, noncePrefix []byte) aead {
345         if len(noncePrefix) != noncePrefixLength {
346                 panic("tls: internal error: wrong nonce length")
347         }
348         aes, err := aes.NewCipher(key)
349         if err != nil {
350                 panic(err)
351         }
352         var aead cipher.AEAD
353         if aesTLS, ok := aes.(gcmtls); ok {
354                 aead, err = aesTLS.NewGCMTLS()
355         } else {
356                 boring.Unreachable()
357                 aead, err = cipher.NewGCM(aes)
358         }
359         if err != nil {
360                 panic(err)
361         }
362
363         ret := &prefixNonceAEAD{aead: aead}
364         copy(ret.nonce[:], noncePrefix)
365         return ret
366 }
367
368 func aeadAESGCMTLS13(key, nonceMask []byte) aead {
369         if len(nonceMask) != aeadNonceLength {
370                 panic("tls: internal error: wrong nonce length")
371         }
372         aes, err := aes.NewCipher(key)
373         if err != nil {
374                 panic(err)
375         }
376         aead, err := cipher.NewGCM(aes)
377         if err != nil {
378                 panic(err)
379         }
380
381         ret := &xorNonceAEAD{aead: aead}
382         copy(ret.nonceMask[:], nonceMask)
383         return ret
384 }
385
386 func aeadChaCha20Poly1305(key, nonceMask []byte) aead {
387         if len(nonceMask) != aeadNonceLength {
388                 panic("tls: internal error: wrong nonce length")
389         }
390         aead, err := chacha20poly1305.New(key)
391         if err != nil {
392                 panic(err)
393         }
394
395         ret := &xorNonceAEAD{aead: aead}
396         copy(ret.nonceMask[:], nonceMask)
397         return ret
398 }
399
400 type constantTimeHash interface {
401         hash.Hash
402         ConstantTimeSum(b []byte) []byte
403 }
404
405 // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
406 // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
407 type cthWrapper struct {
408         h constantTimeHash
409 }
410
411 func (c *cthWrapper) Size() int                   { return c.h.Size() }
412 func (c *cthWrapper) BlockSize() int              { return c.h.BlockSize() }
413 func (c *cthWrapper) Reset()                      { c.h.Reset() }
414 func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
415 func (c *cthWrapper) Sum(b []byte) []byte         { return c.h.ConstantTimeSum(b) }
416
417 func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
418         boring.Unreachable()
419         return func() hash.Hash {
420                 return &cthWrapper{h().(constantTimeHash)}
421         }
422 }
423
424 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
425 func tls10MAC(h hash.Hash, out, seq, header, data, extra []byte) []byte {
426         h.Reset()
427         h.Write(seq)
428         h.Write(header)
429         h.Write(data)
430         res := h.Sum(out)
431         if extra != nil {
432                 h.Write(extra)
433         }
434         return res
435 }
436
437 func rsaKA(version uint16) keyAgreement {
438         return rsaKeyAgreement{}
439 }
440
441 func ecdheECDSAKA(version uint16) keyAgreement {
442         return &ecdheKeyAgreement{
443                 isRSA:   false,
444                 version: version,
445         }
446 }
447
448 func ecdheRSAKA(version uint16) keyAgreement {
449         return &ecdheKeyAgreement{
450                 isRSA:   true,
451                 version: version,
452         }
453 }
454
455 // mutualCipherSuite returns a cipherSuite given a list of supported
456 // ciphersuites and the id requested by the peer.
457 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
458         for _, id := range have {
459                 if id == want {
460                         return cipherSuiteByID(id)
461                 }
462         }
463         return nil
464 }
465
466 func cipherSuiteByID(id uint16) *cipherSuite {
467         for _, cipherSuite := range cipherSuites {
468                 if cipherSuite.id == id {
469                         return cipherSuite
470                 }
471         }
472         return nil
473 }
474
475 func mutualCipherSuiteTLS13(have []uint16, want uint16) *cipherSuiteTLS13 {
476         for _, id := range have {
477                 if id == want {
478                         return cipherSuiteTLS13ByID(id)
479                 }
480         }
481         return nil
482 }
483
484 func cipherSuiteTLS13ByID(id uint16) *cipherSuiteTLS13 {
485         for _, cipherSuite := range cipherSuitesTLS13 {
486                 if cipherSuite.id == id {
487                         return cipherSuite
488                 }
489         }
490         return nil
491 }
492
493 // A list of cipher suite IDs that are, or have been, implemented by this
494 // package.
495 //
496 // See https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
497 const (
498         // TLS 1.0 - 1.2 cipher suites.
499         TLS_RSA_WITH_RC4_128_SHA                      uint16 = 0x0005
500         TLS_RSA_WITH_3DES_EDE_CBC_SHA                 uint16 = 0x000a
501         TLS_RSA_WITH_AES_128_CBC_SHA                  uint16 = 0x002f
502         TLS_RSA_WITH_AES_256_CBC_SHA                  uint16 = 0x0035
503         TLS_RSA_WITH_AES_128_CBC_SHA256               uint16 = 0x003c
504         TLS_RSA_WITH_AES_128_GCM_SHA256               uint16 = 0x009c
505         TLS_RSA_WITH_AES_256_GCM_SHA384               uint16 = 0x009d
506         TLS_ECDHE_ECDSA_WITH_RC4_128_SHA              uint16 = 0xc007
507         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA          uint16 = 0xc009
508         TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA          uint16 = 0xc00a
509         TLS_ECDHE_RSA_WITH_RC4_128_SHA                uint16 = 0xc011
510         TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA           uint16 = 0xc012
511         TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA            uint16 = 0xc013
512         TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA            uint16 = 0xc014
513         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256       uint16 = 0xc023
514         TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256         uint16 = 0xc027
515         TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256         uint16 = 0xc02f
516         TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256       uint16 = 0xc02b
517         TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384         uint16 = 0xc030
518         TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384       uint16 = 0xc02c
519         TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256   uint16 = 0xcca8
520         TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9
521
522         // TLS 1.3 cipher suites.
523         TLS_AES_128_GCM_SHA256       uint16 = 0x1301
524         TLS_AES_256_GCM_SHA384       uint16 = 0x1302
525         TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
526
527         // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
528         // that the client is doing version fallback. See RFC 7507.
529         TLS_FALLBACK_SCSV uint16 = 0x5600
530
531         // Legacy names for the corresponding cipher suites with the correct _SHA256
532         // suffix, retained for backward compatibility.
533         TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305   = TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
534         TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
535 )