]> Cypherpunks.ru repositories - gostls13.git/blob - src/crypto/tls/cipher_suites.go
[dev.boringcrypto] all: merge master 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         "fmt"
19         "hash"
20         "internal/cpu"
21         "runtime"
22
23         "golang.org/x/crypto/chacha20poly1305"
24 )
25
26 // CipherSuite is a TLS cipher suite. Note that most functions in this package
27 // accept and expose cipher suite IDs instead of this type.
28 type CipherSuite struct {
29         ID   uint16
30         Name string
31
32         // Supported versions is the list of TLS protocol versions that can
33         // negotiate this cipher suite.
34         SupportedVersions []uint16
35
36         // Insecure is true if the cipher suite has known security issues
37         // due to its primitives, design, or implementation.
38         Insecure bool
39 }
40
41 var (
42         supportedUpToTLS12 = []uint16{VersionTLS10, VersionTLS11, VersionTLS12}
43         supportedOnlyTLS12 = []uint16{VersionTLS12}
44         supportedOnlyTLS13 = []uint16{VersionTLS13}
45 )
46
47 // CipherSuites returns a list of cipher suites currently implemented by this
48 // package, excluding those with security issues, which are returned by
49 // InsecureCipherSuites.
50 //
51 // The list is sorted by ID. Note that the default cipher suites selected by
52 // this package might depend on logic that can't be captured by a static list,
53 // and might not match those returned by this function.
54 func CipherSuites() []*CipherSuite {
55         return []*CipherSuite{
56                 {TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
57                 {TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
58                 {TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
59                 {TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
60
61                 {TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256", supportedOnlyTLS13, false},
62                 {TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384", supportedOnlyTLS13, false},
63                 {TLS_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256", supportedOnlyTLS13, false},
64
65                 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
66                 {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_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         // This list includes RC4, CBC_SHA256, and 3DES cipher suites. See
85         // cipherSuitesPreferenceOrder for details.
86         return []*CipherSuite{
87                 {TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
88                 {TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
89                 {TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
90                 {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
91                 {TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
92                 {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
93                 {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
94                 {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
95         }
96 }
97
98 // CipherSuiteName returns the standard name for the passed cipher suite ID
99 // (e.g. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"), or a fallback representation
100 // of the ID value if the cipher suite is not implemented by this package.
101 func CipherSuiteName(id uint16) string {
102         for _, c := range CipherSuites() {
103                 if c.ID == id {
104                         return c.Name
105                 }
106         }
107         for _, c := range InsecureCipherSuites() {
108                 if c.ID == id {
109                         return c.Name
110                 }
111         }
112         return fmt.Sprintf("0x%04X", id)
113 }
114
115 const (
116         // suiteECDHE indicates that the cipher suite involves elliptic curve
117         // Diffie-Hellman. This means that it should only be selected when the
118         // client indicates that it supports ECC with a curve and point format
119         // that we're happy with.
120         suiteECDHE = 1 << iota
121         // suiteECSign indicates that the cipher suite involves an ECDSA or
122         // EdDSA signature and therefore may only be selected when the server's
123         // certificate is ECDSA or EdDSA. If this is not set then the cipher suite
124         // is RSA based.
125         suiteECSign
126         // suiteTLS12 indicates that the cipher suite should only be advertised
127         // and accepted when using TLS 1.2.
128         suiteTLS12
129         // suiteSHA384 indicates that the cipher suite uses SHA384 as the
130         // handshake hash.
131         suiteSHA384
132 )
133
134 // A cipherSuite is a TLS 1.0–1.2 cipher suite, and defines the key exchange
135 // mechanism, as well as the cipher+MAC pair or the AEAD.
136 type cipherSuite struct {
137         id uint16
138         // the lengths, in bytes, of the key material needed for each component.
139         keyLen int
140         macLen int
141         ivLen  int
142         ka     func(version uint16) keyAgreement
143         // flags is a bitmask of the suite* values, above.
144         flags  int
145         cipher func(key, iv []byte, isRead bool) any
146         mac    func(key []byte) hash.Hash
147         aead   func(key, fixedNonce []byte) aead
148 }
149
150 var cipherSuites = []*cipherSuite{ // TODO: replace with a map, since the order doesn't matter.
151         {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
152         {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
153         {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
154         {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadAESGCM},
155         {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
156         {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
157         {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
158         {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
159         {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, cipherAES, macSHA256, nil},
160         {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
161         {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
162         {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
163         {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
164         {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
165         {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
166         {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
167         {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
168         {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
169         {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
170         {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, 0, cipherRC4, macSHA1, nil},
171         {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE, cipherRC4, macSHA1, nil},
172         {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherRC4, macSHA1, nil},
173 }
174
175 // selectCipherSuite returns the first TLS 1.0–1.2 cipher suite from ids which
176 // is also in supportedIDs and passes the ok filter.
177 func selectCipherSuite(ids, supportedIDs []uint16, ok func(*cipherSuite) bool) *cipherSuite {
178         for _, id := range ids {
179                 candidate := cipherSuiteByID(id)
180                 if candidate == nil || !ok(candidate) {
181                         continue
182                 }
183
184                 for _, suppID := range supportedIDs {
185                         if id == suppID {
186                                 return candidate
187                         }
188                 }
189         }
190         return nil
191 }
192
193 // A cipherSuiteTLS13 defines only the pair of the AEAD algorithm and hash
194 // algorithm to be used with HKDF. See RFC 8446, Appendix B.4.
195 type cipherSuiteTLS13 struct {
196         id     uint16
197         keyLen int
198         aead   func(key, fixedNonce []byte) aead
199         hash   crypto.Hash
200 }
201
202 var cipherSuitesTLS13 = []*cipherSuiteTLS13{ // TODO: replace with a map.
203         {TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
204         {TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
205         {TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
206 }
207
208 // cipherSuitesPreferenceOrder is the order in which we'll select (on the
209 // server) or advertise (on the client) TLS 1.0–1.2 cipher suites.
210 //
211 // Cipher suites are filtered but not reordered based on the application and
212 // peer's preferences, meaning we'll never select a suite lower in this list if
213 // any higher one is available. This makes it more defensible to keep weaker
214 // cipher suites enabled, especially on the server side where we get the last
215 // word, since there are no known downgrade attacks on cipher suites selection.
216 //
217 // The list is sorted by applying the following priority rules, stopping at the
218 // first (most important) applicable one:
219 //
220 //   - Anything else comes before RC4
221 //
222 //     RC4 has practically exploitable biases. See https://www.rc4nomore.com.
223 //
224 //   - Anything else comes before CBC_SHA256
225 //
226 //     SHA-256 variants of the CBC ciphersuites don't implement any Lucky13
227 //     countermeasures. See http://www.isg.rhul.ac.uk/tls/Lucky13.html and
228 //     https://www.imperialviolet.org/2013/02/04/luckythirteen.html.
229 //
230 //   - Anything else comes before 3DES
231 //
232 //     3DES has 64-bit blocks, which makes it fundamentally susceptible to
233 //     birthday attacks. See https://sweet32.info.
234 //
235 //   - ECDHE comes before anything else
236 //
237 //     Once we got the broken stuff out of the way, the most important
238 //     property a cipher suite can have is forward secrecy. We don't
239 //     implement FFDHE, so that means ECDHE.
240 //
241 //   - AEADs come before CBC ciphers
242 //
243 //     Even with Lucky13 countermeasures, MAC-then-Encrypt CBC cipher suites
244 //     are fundamentally fragile, and suffered from an endless sequence of
245 //     padding oracle attacks. See https://eprint.iacr.org/2015/1129,
246 //     https://www.imperialviolet.org/2014/12/08/poodleagain.html, and
247 //     https://blog.cloudflare.com/yet-another-padding-oracle-in-openssl-cbc-ciphersuites/.
248 //
249 //   - AES comes before ChaCha20
250 //
251 //     When AES hardware is available, AES-128-GCM and AES-256-GCM are faster
252 //     than ChaCha20Poly1305.
253 //
254 //     When AES hardware is not available, AES-128-GCM is one or more of: much
255 //     slower, way more complex, and less safe (because not constant time)
256 //     than ChaCha20Poly1305.
257 //
258 //     We use this list if we think both peers have AES hardware, and
259 //     cipherSuitesPreferenceOrderNoAES otherwise.
260 //
261 //   - AES-128 comes before AES-256
262 //
263 //     The only potential advantages of AES-256 are better multi-target
264 //     margins, and hypothetical post-quantum properties. Neither apply to
265 //     TLS, and AES-256 is slower due to its four extra rounds (which don't
266 //     contribute to the advantages above).
267 //
268 //   - ECDSA comes before RSA
269 //
270 //     The relative order of ECDSA and RSA cipher suites doesn't matter,
271 //     as they depend on the certificate. Pick one to get a stable order.
272 var cipherSuitesPreferenceOrder = []uint16{
273         // AEADs w/ ECDHE
274         TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
275         TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
276         TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
277
278         // CBC w/ ECDHE
279         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
280         TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
281
282         // AEADs w/o ECDHE
283         TLS_RSA_WITH_AES_128_GCM_SHA256,
284         TLS_RSA_WITH_AES_256_GCM_SHA384,
285
286         // CBC w/o ECDHE
287         TLS_RSA_WITH_AES_128_CBC_SHA,
288         TLS_RSA_WITH_AES_256_CBC_SHA,
289
290         // 3DES
291         TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
292         TLS_RSA_WITH_3DES_EDE_CBC_SHA,
293
294         // CBC_SHA256
295         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
296         TLS_RSA_WITH_AES_128_CBC_SHA256,
297
298         // RC4
299         TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
300         TLS_RSA_WITH_RC4_128_SHA,
301 }
302
303 var cipherSuitesPreferenceOrderNoAES = []uint16{
304         // ChaCha20Poly1305
305         TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
306
307         // AES-GCM w/ ECDHE
308         TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
309         TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
310
311         // The rest of cipherSuitesPreferenceOrder.
312         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
313         TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
314         TLS_RSA_WITH_AES_128_GCM_SHA256,
315         TLS_RSA_WITH_AES_256_GCM_SHA384,
316         TLS_RSA_WITH_AES_128_CBC_SHA,
317         TLS_RSA_WITH_AES_256_CBC_SHA,
318         TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
319         TLS_RSA_WITH_3DES_EDE_CBC_SHA,
320         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
321         TLS_RSA_WITH_AES_128_CBC_SHA256,
322         TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
323         TLS_RSA_WITH_RC4_128_SHA,
324 }
325
326 // disabledCipherSuites are not used unless explicitly listed in
327 // Config.CipherSuites. They MUST be at the end of cipherSuitesPreferenceOrder.
328 var disabledCipherSuites = []uint16{
329         // CBC_SHA256
330         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
331         TLS_RSA_WITH_AES_128_CBC_SHA256,
332
333         // RC4
334         TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
335         TLS_RSA_WITH_RC4_128_SHA,
336 }
337
338 var (
339         defaultCipherSuitesLen = len(cipherSuitesPreferenceOrder) - len(disabledCipherSuites)
340         defaultCipherSuites    = cipherSuitesPreferenceOrder[:defaultCipherSuitesLen]
341 )
342
343 // defaultCipherSuitesTLS13 is also the preference order, since there are no
344 // disabled by default TLS 1.3 cipher suites. The same AES vs ChaCha20 logic as
345 // cipherSuitesPreferenceOrder applies.
346 var defaultCipherSuitesTLS13 = []uint16{
347         TLS_AES_128_GCM_SHA256,
348         TLS_AES_256_GCM_SHA384,
349         TLS_CHACHA20_POLY1305_SHA256,
350 }
351
352 var defaultCipherSuitesTLS13NoAES = []uint16{
353         TLS_CHACHA20_POLY1305_SHA256,
354         TLS_AES_128_GCM_SHA256,
355         TLS_AES_256_GCM_SHA384,
356 }
357
358 var (
359         hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
360         hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
361         // Keep in sync with crypto/aes/cipher_s390x.go.
362         hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
363                 (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
364
365         hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
366                 runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
367                 runtime.GOARCH == "s390x" && hasGCMAsmS390X
368 )
369
370 var aesgcmCiphers = map[uint16]bool{
371         // TLS 1.2
372         TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:   true,
373         TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:   true,
374         TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: true,
375         TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: true,
376         // TLS 1.3
377         TLS_AES_128_GCM_SHA256: true,
378         TLS_AES_256_GCM_SHA384: true,
379 }
380
381 var nonAESGCMAEADCiphers = map[uint16]bool{
382         // TLS 1.2
383         TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305:   true,
384         TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305: true,
385         // TLS 1.3
386         TLS_CHACHA20_POLY1305_SHA256: true,
387 }
388
389 // aesgcmPreferred returns whether the first known cipher in the preference list
390 // is an AES-GCM cipher, implying the peer has hardware support for it.
391 func aesgcmPreferred(ciphers []uint16) bool {
392         for _, cID := range ciphers {
393                 if c := cipherSuiteByID(cID); c != nil {
394                         return aesgcmCiphers[cID]
395                 }
396                 if c := cipherSuiteTLS13ByID(cID); c != nil {
397                         return aesgcmCiphers[cID]
398                 }
399         }
400         return false
401 }
402
403 func cipherRC4(key, iv []byte, isRead bool) any {
404         cipher, _ := rc4.NewCipher(key)
405         return cipher
406 }
407
408 func cipher3DES(key, iv []byte, isRead bool) any {
409         block, _ := des.NewTripleDESCipher(key)
410         if isRead {
411                 return cipher.NewCBCDecrypter(block, iv)
412         }
413         return cipher.NewCBCEncrypter(block, iv)
414 }
415
416 func cipherAES(key, iv []byte, isRead bool) any {
417         block, _ := aes.NewCipher(key)
418         if isRead {
419                 return cipher.NewCBCDecrypter(block, iv)
420         }
421         return cipher.NewCBCEncrypter(block, iv)
422 }
423
424 // macSHA1 returns a SHA-1 based constant time MAC.
425 func macSHA1(key []byte) hash.Hash {
426         h := sha1.New
427         // The BoringCrypto SHA1 does not have a constant-time
428         // checksum function, so don't try to use it.
429         if !boring.Enabled {
430                 h = newConstantTimeHash(h)
431         }
432         return hmac.New(h, key)
433 }
434
435 // macSHA256 returns a SHA-256 based MAC. This is only supported in TLS 1.2 and
436 // is currently only used in disabled-by-default cipher suites.
437 func macSHA256(key []byte) hash.Hash {
438         return hmac.New(sha256.New, key)
439 }
440
441 type aead interface {
442         cipher.AEAD
443
444         // explicitNonceLen returns the number of bytes of explicit nonce
445         // included in each record. This is eight for older AEADs and
446         // zero for modern ones.
447         explicitNonceLen() int
448 }
449
450 const (
451         aeadNonceLength   = 12
452         noncePrefixLength = 4
453 )
454
455 // prefixNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
456 // each call.
457 type prefixNonceAEAD struct {
458         // nonce contains the fixed part of the nonce in the first four bytes.
459         nonce [aeadNonceLength]byte
460         aead  cipher.AEAD
461 }
462
463 func (f *prefixNonceAEAD) NonceSize() int        { return aeadNonceLength - noncePrefixLength }
464 func (f *prefixNonceAEAD) Overhead() int         { return f.aead.Overhead() }
465 func (f *prefixNonceAEAD) explicitNonceLen() int { return f.NonceSize() }
466
467 func (f *prefixNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
468         copy(f.nonce[4:], nonce)
469         return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
470 }
471
472 func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
473         copy(f.nonce[4:], nonce)
474         return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
475 }
476
477 // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
478 // before each call.
479 type xorNonceAEAD struct {
480         nonceMask [aeadNonceLength]byte
481         aead      cipher.AEAD
482 }
483
484 func (f *xorNonceAEAD) NonceSize() int        { return 8 } // 64-bit sequence number
485 func (f *xorNonceAEAD) Overhead() int         { return f.aead.Overhead() }
486 func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
487
488 func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
489         for i, b := range nonce {
490                 f.nonceMask[4+i] ^= b
491         }
492         result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
493         for i, b := range nonce {
494                 f.nonceMask[4+i] ^= b
495         }
496
497         return result
498 }
499
500 func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
501         for i, b := range nonce {
502                 f.nonceMask[4+i] ^= b
503         }
504         result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
505         for i, b := range nonce {
506                 f.nonceMask[4+i] ^= b
507         }
508
509         return result, err
510 }
511
512 func aeadAESGCM(key, noncePrefix []byte) aead {
513         if len(noncePrefix) != noncePrefixLength {
514                 panic("tls: internal error: wrong nonce length")
515         }
516         aes, err := aes.NewCipher(key)
517         if err != nil {
518                 panic(err)
519         }
520         type gcmtls interface {
521                 NewGCMTLS() (cipher.AEAD, error)
522         }
523         var aead cipher.AEAD
524         if aesTLS, ok := aes.(gcmtls); ok {
525                 aead, err = aesTLS.NewGCMTLS()
526         } else {
527                 boring.Unreachable()
528                 aead, err = cipher.NewGCM(aes)
529         }
530         if err != nil {
531                 panic(err)
532         }
533
534         ret := &prefixNonceAEAD{aead: aead}
535         copy(ret.nonce[:], noncePrefix)
536         return ret
537 }
538
539 func aeadAESGCMTLS13(key, nonceMask []byte) aead {
540         if len(nonceMask) != aeadNonceLength {
541                 panic("tls: internal error: wrong nonce length")
542         }
543         aes, err := aes.NewCipher(key)
544         if err != nil {
545                 panic(err)
546         }
547         aead, err := cipher.NewGCM(aes)
548         if err != nil {
549                 panic(err)
550         }
551
552         ret := &xorNonceAEAD{aead: aead}
553         copy(ret.nonceMask[:], nonceMask)
554         return ret
555 }
556
557 func aeadChaCha20Poly1305(key, nonceMask []byte) aead {
558         if len(nonceMask) != aeadNonceLength {
559                 panic("tls: internal error: wrong nonce length")
560         }
561         aead, err := chacha20poly1305.New(key)
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 type constantTimeHash interface {
572         hash.Hash
573         ConstantTimeSum(b []byte) []byte
574 }
575
576 // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
577 // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
578 type cthWrapper struct {
579         h constantTimeHash
580 }
581
582 func (c *cthWrapper) Size() int                   { return c.h.Size() }
583 func (c *cthWrapper) BlockSize() int              { return c.h.BlockSize() }
584 func (c *cthWrapper) Reset()                      { c.h.Reset() }
585 func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
586 func (c *cthWrapper) Sum(b []byte) []byte         { return c.h.ConstantTimeSum(b) }
587
588 func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
589         boring.Unreachable()
590         return func() hash.Hash {
591                 return &cthWrapper{h().(constantTimeHash)}
592         }
593 }
594
595 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
596 func tls10MAC(h hash.Hash, out, seq, header, data, extra []byte) []byte {
597         h.Reset()
598         h.Write(seq)
599         h.Write(header)
600         h.Write(data)
601         res := h.Sum(out)
602         if extra != nil {
603                 h.Write(extra)
604         }
605         return res
606 }
607
608 func rsaKA(version uint16) keyAgreement {
609         return rsaKeyAgreement{}
610 }
611
612 func ecdheECDSAKA(version uint16) keyAgreement {
613         return &ecdheKeyAgreement{
614                 isRSA:   false,
615                 version: version,
616         }
617 }
618
619 func ecdheRSAKA(version uint16) keyAgreement {
620         return &ecdheKeyAgreement{
621                 isRSA:   true,
622                 version: version,
623         }
624 }
625
626 // mutualCipherSuite returns a cipherSuite given a list of supported
627 // ciphersuites and the id requested by the peer.
628 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
629         for _, id := range have {
630                 if id == want {
631                         return cipherSuiteByID(id)
632                 }
633         }
634         return nil
635 }
636
637 func cipherSuiteByID(id uint16) *cipherSuite {
638         for _, cipherSuite := range cipherSuites {
639                 if cipherSuite.id == id {
640                         return cipherSuite
641                 }
642         }
643         return nil
644 }
645
646 func mutualCipherSuiteTLS13(have []uint16, want uint16) *cipherSuiteTLS13 {
647         for _, id := range have {
648                 if id == want {
649                         return cipherSuiteTLS13ByID(id)
650                 }
651         }
652         return nil
653 }
654
655 func cipherSuiteTLS13ByID(id uint16) *cipherSuiteTLS13 {
656         for _, cipherSuite := range cipherSuitesTLS13 {
657                 if cipherSuite.id == id {
658                         return cipherSuite
659                 }
660         }
661         return nil
662 }
663
664 // A list of cipher suite IDs that are, or have been, implemented by this
665 // package.
666 //
667 // See https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
668 const (
669         // TLS 1.0 - 1.2 cipher suites.
670         TLS_RSA_WITH_RC4_128_SHA                      uint16 = 0x0005
671         TLS_RSA_WITH_3DES_EDE_CBC_SHA                 uint16 = 0x000a
672         TLS_RSA_WITH_AES_128_CBC_SHA                  uint16 = 0x002f
673         TLS_RSA_WITH_AES_256_CBC_SHA                  uint16 = 0x0035
674         TLS_RSA_WITH_AES_128_CBC_SHA256               uint16 = 0x003c
675         TLS_RSA_WITH_AES_128_GCM_SHA256               uint16 = 0x009c
676         TLS_RSA_WITH_AES_256_GCM_SHA384               uint16 = 0x009d
677         TLS_ECDHE_ECDSA_WITH_RC4_128_SHA              uint16 = 0xc007
678         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA          uint16 = 0xc009
679         TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA          uint16 = 0xc00a
680         TLS_ECDHE_RSA_WITH_RC4_128_SHA                uint16 = 0xc011
681         TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA           uint16 = 0xc012
682         TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA            uint16 = 0xc013
683         TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA            uint16 = 0xc014
684         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256       uint16 = 0xc023
685         TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256         uint16 = 0xc027
686         TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256         uint16 = 0xc02f
687         TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256       uint16 = 0xc02b
688         TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384         uint16 = 0xc030
689         TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384       uint16 = 0xc02c
690         TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256   uint16 = 0xcca8
691         TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9
692
693         // TLS 1.3 cipher suites.
694         TLS_AES_128_GCM_SHA256       uint16 = 0x1301
695         TLS_AES_256_GCM_SHA384       uint16 = 0x1302
696         TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
697
698         // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
699         // that the client is doing version fallback. See RFC 7507.
700         TLS_FALLBACK_SCSV uint16 = 0x5600
701
702         // Legacy names for the corresponding cipher suites with the correct _SHA256
703         // suffix, retained for backward compatibility.
704         TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305   = TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
705         TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
706 )