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