Skip to content

Commit

Permalink
[dev.boringcrypto] all: merge master into dev.boringcrypto
Browse files Browse the repository at this point in the history
Add BoringCrypto-specific test data to TestAESCipherReordering
and TestAESCipherReordering13.

Change-Id: Id1def4cf166d5059920741f045e3e61bb17c23c8
  • Loading branch information
dmitshur committed Dec 2, 2020
2 parents dea96ad + 9f39a43 commit 5934c43
Show file tree
Hide file tree
Showing 44 changed files with 3,000 additions and 2,848 deletions.
2 changes: 1 addition & 1 deletion doc/articles/race_detector.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ <h2 id="Supported_Systems">Supported Systems</h2>
<code>linux/amd64</code>, <code>linux/ppc64le</code>,
<code>linux/arm64</code>, <code>freebsd/amd64</code>,
<code>netbsd/amd64</code>, <code>darwin/amd64</code>,
and <code>windows/amd64</code>.
<code>darwin/arm64</code>, and <code>windows/amd64</code>.
</p>

<h2 id="Runtime_Overheads">Runtime Overhead</h2>
Expand Down
11 changes: 11 additions & 0 deletions doc/go1.16.html
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ <h3 id="crypto/tls"><a href="/pkg/crypto/tls">crypto/tls</a></h3>
the list advertised by the client</a>.
</p>

<p><!-- CL 262857 -->
TLS servers will now prefer other AEAD cipher suites (such as ChaCha20Poly1305)
over AES-GCM cipher suites if either the client or server doesn't have AES hardware
support, unless the application set both
<a href="/pkg/crypto/tls/#Config.PreferServerCipherSuites"><code>Config.PreferServerCipherSuites</code></a>
and <a href="/pkg/crypto/tls/#Config.CipherSuites"><code>Config.CipherSuites</code></a>
or there are no other AEAD cipher suites supported.
The client is assumed not to have AES hardware support if it does not signal a
preference for AES-GCM cipher suites.
</p>

<h3 id="crypto/x509"><a href="/pkg/crypto/x509">crypto/x509</a></h3>

<p><!-- CL 235078 -->
Expand Down
74 changes: 62 additions & 12 deletions src/crypto/tls/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"internal/cpu"
"io"
"net"
"runtime"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -1463,21 +1465,21 @@ func defaultCipherSuitesTLS13() []uint16 {
return varDefaultCipherSuitesTLS13
}

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
)

func initDefaultCipherSuites() {
var topCipherSuites []uint16

// Check the cpu flags for each platform that has optimized GCM implementations.
// Worst case, these variables will just all be false.
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)

hasGCMAsm = hasGCMAsmAMD64 || hasGCMAsmARM64 || hasGCMAsmS390X
)

if hasGCMAsm || boringEnabled {
if hasAESGCMHardwareSupport || boringEnabled {
// If BoringCrypto is enabled, always prioritize AES-GCM.
// If AES-GCM hardware is provided then prioritise AES-GCM
// cipher suites.
Expand Down Expand Up @@ -1541,3 +1543,51 @@ func isSupportedSignatureAlgorithm(sigAlg SignatureScheme, supportedSignatureAlg
}
return false
}

var aesgcmCiphers = map[uint16]bool{
// 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,
// 1.3
TLS_AES_128_GCM_SHA256: true,
TLS_AES_256_GCM_SHA384: true,
}

var nonAESGCMAEADCiphers = map[uint16]bool{
// 1.2
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305: true,
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305: true,
// 1.3
TLS_CHACHA20_POLY1305_SHA256: true,
}

// aesgcmPreferred returns whether the first valid 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 {
c := cipherSuiteByID(cID)
if c == nil {
c13 := cipherSuiteTLS13ByID(cID)
if c13 == nil {
continue
}
return aesgcmCiphers[cID]
}
return aesgcmCiphers[cID]
}
return false
}

// deprioritizeAES reorders cipher preference lists by rearranging
// adjacent AEAD ciphers such that AES-GCM based ciphers are moved
// after other AEAD ciphers. It returns a fresh slice.
func deprioritizeAES(ciphers []uint16) []uint16 {
reordered := make([]uint16, len(ciphers))
copy(reordered, ciphers)
sort.SliceStable(reordered, func(i, j int) bool {
return nonAESGCMAEADCiphers[reordered[i]] && aesgcmCiphers[reordered[j]]
})
return reordered
}
15 changes: 15 additions & 0 deletions src/crypto/tls/handshake_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,24 @@ func (hs *serverHandshakeState) pickCipherSuite() error {
if c.config.PreferServerCipherSuites {
preferenceList = c.config.cipherSuites()
supportedList = hs.clientHello.cipherSuites

// If the client does not seem to have hardware support for AES-GCM,
// and the application did not specify a cipher suite preference order,
// prefer other AEAD ciphers even if we prioritized AES-GCM ciphers
// by default.
if c.config.CipherSuites == nil && !aesgcmPreferred(hs.clientHello.cipherSuites) {
preferenceList = deprioritizeAES(preferenceList)
}
} else {
preferenceList = hs.clientHello.cipherSuites
supportedList = c.config.cipherSuites()

// If we don't have hardware support for AES-GCM, prefer other AEAD
// ciphers even if the client prioritized AES-GCM.
// If BoringCrypto is enabled, always prioritize AES-GCM.
if !hasAESGCMHardwareSupport && !boringEnabled {
preferenceList = deprioritizeAES(preferenceList)
}
}

hs.suite = selectCipherSuite(preferenceList, supportedList, hs.cipherSuiteOk)
Expand Down
Loading

0 comments on commit 5934c43

Please sign in to comment.