...

Source file src/crypto/tls/cipher_suites.go

Documentation: crypto/tls

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

View as plain text