]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/crypto/tls/cipher_suites.go
crypto/tls: remove RSA KEX ciphers from the default list
[gostls13.git] / src / crypto / tls / cipher_suites.go
index cbe14f8dbd79094bcd9ca16239d383eb98210a82..eaeb7e04e6ccfdd118823151c375c9a50d00dd44 100644 (file)
@@ -4,20 +4,21 @@
 
 package tls
 
-import "crypto/internal/boring"
-
 import (
        "crypto"
        "crypto/aes"
        "crypto/cipher"
        "crypto/des"
        "crypto/hmac"
+       "crypto/internal/boring"
        "crypto/rc4"
        "crypto/sha1"
        "crypto/sha256"
-       "crypto/x509"
        "fmt"
        "hash"
+       "internal/cpu"
+       "internal/godebug"
+       "runtime"
 
        "golang.org/x/crypto/chacha20poly1305"
 )
@@ -45,13 +46,13 @@ var (
 
 // CipherSuites returns a list of cipher suites currently implemented by this
 // package, excluding those with security issues, which are returned by
-// InsecureCipherSuites.
+// [InsecureCipherSuites].
 //
 // The list is sorted by ID. Note that the default cipher suites selected by
-// this package might depend on logic that can't be captured by a static list.
+// this package might depend on logic that can't be captured by a static list,
+// and might not match those returned by this function.
 func CipherSuites() []*CipherSuite {
        return []*CipherSuite{
-               {TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, false},
                {TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
                {TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
                {TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
@@ -63,7 +64,6 @@ func CipherSuites() []*CipherSuite {
 
                {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
                {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
-               {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, false},
                {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
                {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
                {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
@@ -79,15 +79,17 @@ func CipherSuites() []*CipherSuite {
 // this package and which have security issues.
 //
 // Most applications should not use the cipher suites in this list, and should
-// only use those returned by CipherSuites.
+// only use those returned by [CipherSuites].
 func InsecureCipherSuites() []*CipherSuite {
-       // RC4 suites are broken because RC4 is.
-       // CBC-SHA256 suites have no Lucky13 countermeasures.
+       // This list includes RC4, CBC_SHA256, and 3DES cipher suites. See
+       // cipherSuitesPreferenceOrder for details.
        return []*CipherSuite{
                {TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
+               {TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
                {TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
                {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
                {TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
+               {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
                {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
                {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
        }
@@ -110,25 +112,6 @@ func CipherSuiteName(id uint16) string {
        return fmt.Sprintf("0x%04X", id)
 }
 
-// a keyAgreement implements the client and server side of a TLS key agreement
-// protocol by generating and processing key exchange messages.
-type keyAgreement interface {
-       // On the server side, the first two methods are called in order.
-
-       // In the case that the key agreement protocol doesn't use a
-       // ServerKeyExchange message, generateServerKeyExchange can return nil,
-       // nil.
-       generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
-       processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
-
-       // On the client side, the next two methods are called in order.
-
-       // This method may not be called if the server doesn't send a
-       // ServerKeyExchange message.
-       processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
-       generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
-}
-
 const (
        // suiteECDHE indicates that the cipher suite involves elliptic curve
        // Diffie-Hellman. This means that it should only be selected when the
@@ -146,12 +129,10 @@ const (
        // suiteSHA384 indicates that the cipher suite uses SHA384 as the
        // handshake hash.
        suiteSHA384
-       // suiteDefaultOff indicates that this cipher suite is not included by
-       // default.
-       suiteDefaultOff
 )
 
-// A cipherSuite is a specific combination of key agreement, cipher and MAC function.
+// A cipherSuite is a TLS 1.0–1.2 cipher suite, and defines the key exchange
+// mechanism, as well as the cipher+MAC pair or the AEAD.
 type cipherSuite struct {
        id uint16
        // the lengths, in bytes, of the key material needed for each component.
@@ -161,42 +142,38 @@ type cipherSuite struct {
        ka     func(version uint16) keyAgreement
        // flags is a bitmask of the suite* values, above.
        flags  int
-       cipher func(key, iv []byte, isRead bool) interface{}
-       mac    func(version uint16, macKey []byte) macFunction
+       cipher func(key, iv []byte, isRead bool) any
+       mac    func(key []byte) hash.Hash
        aead   func(key, fixedNonce []byte) aead
 }
 
-var cipherSuites = []*cipherSuite{
-       // Ciphersuite order is chosen so that ECDHE comes before plain RSA and
-       // AEADs are the top preference.
+var cipherSuites = []*cipherSuite{ // TODO: replace with a map, since the order doesn't matter.
        {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
        {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
        {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
        {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadAESGCM},
        {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
        {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
-       {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
+       {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
        {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
-       {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
+       {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, cipherAES, macSHA256, nil},
        {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
        {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
        {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
        {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
        {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
-       {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
+       {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
        {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
        {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
        {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
        {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
-
-       // RC4-based cipher suites are disabled by default.
-       {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
-       {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
-       {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteDefaultOff, cipherRC4, macSHA1, nil},
+       {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, 0, cipherRC4, macSHA1, nil},
+       {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE, cipherRC4, macSHA1, nil},
+       {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherRC4, macSHA1, nil},
 }
 
-// selectCipherSuite returns the first cipher suite from ids which is also in
-// supportedIDs and passes the ok filter.
+// selectCipherSuite returns the first TLS 1.0–1.2 cipher suite from ids which
+// is also in supportedIDs and passes the ok filter.
 func selectCipherSuite(ids, supportedIDs []uint16, ok func(*cipherSuite) bool) *cipherSuite {
        for _, id := range ids {
                candidate := cipherSuiteByID(id)
@@ -222,18 +199,230 @@ type cipherSuiteTLS13 struct {
        hash   crypto.Hash
 }
 
-var cipherSuitesTLS13 = []*cipherSuiteTLS13{
+var cipherSuitesTLS13 = []*cipherSuiteTLS13{ // TODO: replace with a map.
        {TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
        {TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
        {TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
 }
 
-func cipherRC4(key, iv []byte, isRead bool) interface{} {
+// cipherSuitesPreferenceOrder is the order in which we'll select (on the
+// server) or advertise (on the client) TLS 1.0–1.2 cipher suites.
+//
+// Cipher suites are filtered but not reordered based on the application and
+// peer's preferences, meaning we'll never select a suite lower in this list if
+// any higher one is available. This makes it more defensible to keep weaker
+// cipher suites enabled, especially on the server side where we get the last
+// word, since there are no known downgrade attacks on cipher suites selection.
+//
+// The list is sorted by applying the following priority rules, stopping at the
+// first (most important) applicable one:
+//
+//   - Anything else comes before RC4
+//
+//     RC4 has practically exploitable biases. See https://www.rc4nomore.com.
+//
+//   - Anything else comes before CBC_SHA256
+//
+//     SHA-256 variants of the CBC ciphersuites don't implement any Lucky13
+//     countermeasures. See http://www.isg.rhul.ac.uk/tls/Lucky13.html and
+//     https://www.imperialviolet.org/2013/02/04/luckythirteen.html.
+//
+//   - Anything else comes before 3DES
+//
+//     3DES has 64-bit blocks, which makes it fundamentally susceptible to
+//     birthday attacks. See https://sweet32.info.
+//
+//   - ECDHE comes before anything else
+//
+//     Once we got the broken stuff out of the way, the most important
+//     property a cipher suite can have is forward secrecy. We don't
+//     implement FFDHE, so that means ECDHE.
+//
+//   - AEADs come before CBC ciphers
+//
+//     Even with Lucky13 countermeasures, MAC-then-Encrypt CBC cipher suites
+//     are fundamentally fragile, and suffered from an endless sequence of
+//     padding oracle attacks. See https://eprint.iacr.org/2015/1129,
+//     https://www.imperialviolet.org/2014/12/08/poodleagain.html, and
+//     https://blog.cloudflare.com/yet-another-padding-oracle-in-openssl-cbc-ciphersuites/.
+//
+//   - AES comes before ChaCha20
+//
+//     When AES hardware is available, AES-128-GCM and AES-256-GCM are faster
+//     than ChaCha20Poly1305.
+//
+//     When AES hardware is not available, AES-128-GCM is one or more of: much
+//     slower, way more complex, and less safe (because not constant time)
+//     than ChaCha20Poly1305.
+//
+//     We use this list if we think both peers have AES hardware, and
+//     cipherSuitesPreferenceOrderNoAES otherwise.
+//
+//   - AES-128 comes before AES-256
+//
+//     The only potential advantages of AES-256 are better multi-target
+//     margins, and hypothetical post-quantum properties. Neither apply to
+//     TLS, and AES-256 is slower due to its four extra rounds (which don't
+//     contribute to the advantages above).
+//
+//   - ECDSA comes before RSA
+//
+//     The relative order of ECDSA and RSA cipher suites doesn't matter,
+//     as they depend on the certificate. Pick one to get a stable order.
+var cipherSuitesPreferenceOrder = []uint16{
+       // AEADs w/ ECDHE
+       TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
+       TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
+       TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
+
+       // CBC w/ ECDHE
+       TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
+       TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
+
+       // AEADs w/o ECDHE
+       TLS_RSA_WITH_AES_128_GCM_SHA256,
+       TLS_RSA_WITH_AES_256_GCM_SHA384,
+
+       // CBC w/o ECDHE
+       TLS_RSA_WITH_AES_128_CBC_SHA,
+       TLS_RSA_WITH_AES_256_CBC_SHA,
+
+       // 3DES
+       TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
+       TLS_RSA_WITH_3DES_EDE_CBC_SHA,
+
+       // CBC_SHA256
+       TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
+       TLS_RSA_WITH_AES_128_CBC_SHA256,
+
+       // RC4
+       TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
+       TLS_RSA_WITH_RC4_128_SHA,
+}
+
+var cipherSuitesPreferenceOrderNoAES = []uint16{
+       // ChaCha20Poly1305
+       TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
+
+       // AES-GCM w/ ECDHE
+       TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
+       TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
+
+       // The rest of cipherSuitesPreferenceOrder.
+       TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
+       TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
+       TLS_RSA_WITH_AES_128_GCM_SHA256,
+       TLS_RSA_WITH_AES_256_GCM_SHA384,
+       TLS_RSA_WITH_AES_128_CBC_SHA,
+       TLS_RSA_WITH_AES_256_CBC_SHA,
+       TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
+       TLS_RSA_WITH_3DES_EDE_CBC_SHA,
+       TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
+       TLS_RSA_WITH_AES_128_CBC_SHA256,
+       TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
+       TLS_RSA_WITH_RC4_128_SHA,
+}
+
+// disabledCipherSuites are not used unless explicitly listed in
+// Config.CipherSuites. They MUST be at the end of cipherSuitesPreferenceOrder.
+var disabledCipherSuites = []uint16{
+       // CBC_SHA256
+       TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
+       TLS_RSA_WITH_AES_128_CBC_SHA256,
+
+       // RC4
+       TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
+       TLS_RSA_WITH_RC4_128_SHA,
+}
+
+var (
+       defaultCipherSuitesLen int
+       defaultCipherSuites    []uint16
+)
+
+// rsaKexCiphers contains the ciphers which use RSA based key exchange,
+// which we disable by default.
+var rsaKexCiphers = map[uint16]bool{
+       TLS_RSA_WITH_RC4_128_SHA:        true,
+       TLS_RSA_WITH_3DES_EDE_CBC_SHA:   true,
+       TLS_RSA_WITH_AES_128_CBC_SHA:    true,
+       TLS_RSA_WITH_AES_256_CBC_SHA:    true,
+       TLS_RSA_WITH_AES_128_CBC_SHA256: true,
+       TLS_RSA_WITH_AES_128_GCM_SHA256: true,
+       TLS_RSA_WITH_AES_256_GCM_SHA384: true,
+}
+
+var rsaKEXgodebug = godebug.New("tlsrsakex")
+
+func init() {
+       rsaKexEnabled := rsaKEXgodebug.Value() == "1"
+       for _, c := range cipherSuitesPreferenceOrder[:len(cipherSuitesPreferenceOrder)-len(disabledCipherSuites)] {
+               if !rsaKexEnabled && rsaKexCiphers[c] {
+                       continue
+               }
+               defaultCipherSuites = append(defaultCipherSuites, c)
+       }
+       defaultCipherSuitesLen = len(defaultCipherSuites)
+}
+
+// defaultCipherSuitesTLS13 is also the preference order, since there are no
+// disabled by default TLS 1.3 cipher suites. The same AES vs ChaCha20 logic as
+// cipherSuitesPreferenceOrder applies.
+var defaultCipherSuitesTLS13 = []uint16{
+       TLS_AES_128_GCM_SHA256,
+       TLS_AES_256_GCM_SHA384,
+       TLS_CHACHA20_POLY1305_SHA256,
+}
+
+var defaultCipherSuitesTLS13NoAES = []uint16{
+       TLS_CHACHA20_POLY1305_SHA256,
+       TLS_AES_128_GCM_SHA256,
+       TLS_AES_256_GCM_SHA384,
+}
+
+var (
+       hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
+       hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
+       // Keep in sync with crypto/aes/cipher_s390x.go.
+       hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
+               (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
+
+       hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
+               runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
+               runtime.GOARCH == "s390x" && hasGCMAsmS390X
+)
+
+var aesgcmCiphers = map[uint16]bool{
+       // TLS 1.2
+       TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:   true,
+       TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:   true,
+       TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: true,
+       TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: true,
+       // TLS 1.3
+       TLS_AES_128_GCM_SHA256: true,
+       TLS_AES_256_GCM_SHA384: true,
+}
+
+// aesgcmPreferred returns whether the first known cipher in the preference list
+// is an AES-GCM cipher, implying the peer has hardware support for it.
+func aesgcmPreferred(ciphers []uint16) bool {
+       for _, cID := range ciphers {
+               if c := cipherSuiteByID(cID); c != nil {
+                       return aesgcmCiphers[cID]
+               }
+               if c := cipherSuiteTLS13ByID(cID); c != nil {
+                       return aesgcmCiphers[cID]
+               }
+       }
+       return false
+}
+
+func cipherRC4(key, iv []byte, isRead bool) any {
        cipher, _ := rc4.NewCipher(key)
        return cipher
 }
 
-func cipher3DES(key, iv []byte, isRead bool) interface{} {
+func cipher3DES(key, iv []byte, isRead bool) any {
        block, _ := des.NewTripleDESCipher(key)
        if isRead {
                return cipher.NewCBCDecrypter(block, iv)
@@ -241,7 +430,7 @@ func cipher3DES(key, iv []byte, isRead bool) interface{} {
        return cipher.NewCBCEncrypter(block, iv)
 }
 
-func cipherAES(key, iv []byte, isRead bool) interface{} {
+func cipherAES(key, iv []byte, isRead bool) any {
        block, _ := aes.NewCipher(key)
        if isRead {
                return cipher.NewCBCDecrypter(block, iv)
@@ -249,30 +438,21 @@ func cipherAES(key, iv []byte, isRead bool) interface{} {
        return cipher.NewCBCEncrypter(block, iv)
 }
 
-// macSHA1 returns a macFunction for the given protocol version.
-func macSHA1(version uint16, key []byte) macFunction {
+// macSHA1 returns a SHA-1 based constant time MAC.
+func macSHA1(key []byte) hash.Hash {
        h := sha1.New
        // The BoringCrypto SHA1 does not have a constant-time
        // checksum function, so don't try to use it.
        if !boring.Enabled {
                h = newConstantTimeHash(h)
        }
-       return tls10MAC{h: hmac.New(h, key)}
+       return hmac.New(h, key)
 }
 
-// macSHA256 returns a SHA-256 based MAC. These are only supported in TLS 1.2
-// so the given version is ignored.
-func macSHA256(version uint16, key []byte) macFunction {
-       return tls10MAC{h: hmac.New(sha256.New, key)}
-}
-
-type macFunction interface {
-       // Size returns the length of the MAC.
-       Size() int
-       // MAC appends the MAC of (seq, header, data) to out. The extra data is fed
-       // into the MAC after obtaining the result to normalize timing. The result
-       // is only valid until the next invocation of MAC as the buffer is reused.
-       MAC(seq, header, data, extra []byte) []byte
+// macSHA256 returns a SHA-256 based MAC. This is only supported in TLS 1.2 and
+// is currently only used in disabled-by-default cipher suites.
+func macSHA256(key []byte) hash.Hash {
+       return hmac.New(sha256.New, key)
 }
 
 type aead interface {
@@ -311,7 +491,7 @@ func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([
        return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
 }
 
-// xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
+// xorNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
 // before each call.
 type xorNonceAEAD struct {
        nonceMask [aeadNonceLength]byte
@@ -346,10 +526,6 @@ func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]by
        return result, err
 }
 
-type gcmtls interface {
-       NewGCMTLS() (cipher.AEAD, error)
-}
-
 func aeadAESGCM(key, noncePrefix []byte) aead {
        if len(noncePrefix) != noncePrefixLength {
                panic("tls: internal error: wrong nonce length")
@@ -359,8 +535,8 @@ func aeadAESGCM(key, noncePrefix []byte) aead {
                panic(err)
        }
        var aead cipher.AEAD
-       if aesTLS, ok := aes.(gcmtls); ok {
-               aead, err = aesTLS.NewGCMTLS()
+       if boring.Enabled {
+               aead, err = boring.NewGCMTLS(aes)
        } else {
                boring.Unreachable()
                aead, err = cipher.NewGCM(aes)
@@ -431,26 +607,14 @@ func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
 }
 
 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
-type tls10MAC struct {
-       h   hash.Hash
-       buf []byte
-}
-
-func (s tls10MAC) Size() int {
-       return s.h.Size()
-}
-
-// MAC is guaranteed to take constant time, as long as
-// len(seq)+len(header)+len(data)+len(extra) is constant. extra is not fed into
-// the MAC, but is only provided to make the timing profile constant.
-func (s tls10MAC) MAC(seq, header, data, extra []byte) []byte {
-       s.h.Reset()
-       s.h.Write(seq)
-       s.h.Write(header)
-       s.h.Write(data)
-       res := s.h.Sum(s.buf[:0])
+func tls10MAC(h hash.Hash, out, seq, header, data, extra []byte) []byte {
+       h.Reset()
+       h.Write(seq)
+       h.Write(header)
+       h.Write(data)
+       res := h.Sum(out)
        if extra != nil {
-               s.h.Write(extra)
+               h.Write(extra)
        }
        return res
 }