From: Martin Möhrmann Date: Wed, 3 May 2017 08:00:38 +0000 (+0200) Subject: vendor/golang_org/x/crypto/chacha20poly1305: revendor X-Git-Tag: go1.9beta1~349 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6e9b6e1d222a4f8ad3d50929ee1d6178fb3c6077;p=gostls13.git vendor/golang_org/x/crypto/chacha20poly1305: revendor Brings in chacha20poly1305 directory from golang.org/x/crypto revision 12e9ca725de4806fbda1610fd95aacad15bd6810, adding: CL 41862: chacha20poly1305: add runtime internal independent cpu feature detection CL 39952: add import comment Change-Id: Ic46ff24b081bc1c66b6317334d33180e33bfd318 Reviewed-on: https://go-review.googlesource.com/42513 Run-TryBot: Martin Möhrmann TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305.go b/src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305.go index eb6739a109..3f0dcb9d8c 100644 --- a/src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305.go +++ b/src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305.go @@ -3,7 +3,7 @@ // license that can be found in the LICENSE file. // Package chacha20poly1305 implements the ChaCha20-Poly1305 AEAD as specified in RFC 7539. -package chacha20poly1305 +package chacha20poly1305 // import "golang.org/x/crypto/chacha20poly1305" import ( "crypto/cipher" diff --git a/src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305_amd64.go b/src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305_amd64.go index 4755033212..1e523b9951 100644 --- a/src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305_amd64.go +++ b/src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305_amd64.go @@ -14,13 +14,60 @@ func chacha20Poly1305Open(dst []byte, key []uint32, src, ad []byte) bool //go:noescape func chacha20Poly1305Seal(dst []byte, key []uint32, src, ad []byte) -//go:noescape -func haveSSSE3() bool +// cpuid is implemented in chacha20poly1305_amd64.s. +func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32) + +// xgetbv with ecx = 0 is implemented in chacha20poly1305_amd64.s. +func xgetbv() (eax, edx uint32) -var canUseASM bool +var ( + useASM bool + useAVX2 bool +) func init() { - canUseASM = haveSSSE3() + detectCpuFeatures() +} + +// detectCpuFeatures is used to detect if cpu instructions +// used by the functions implemented in assembler in +// chacha20poly1305_amd64.s are supported. +func detectCpuFeatures() { + maxId, _, _, _ := cpuid(0, 0) + if maxId < 1 { + return + } + + _, _, ecx1, _ := cpuid(1, 0) + + haveSSSE3 := isSet(9, ecx1) + useASM = haveSSSE3 + + haveOSXSAVE := isSet(27, ecx1) + + osSupportsAVX := false + // For XGETBV, OSXSAVE bit is required and sufficient. + if haveOSXSAVE { + eax, _ := xgetbv() + // Check if XMM and YMM registers have OS support. + osSupportsAVX = isSet(1, eax) && isSet(2, eax) + } + haveAVX := isSet(28, ecx1) && osSupportsAVX + + if maxId < 7 { + return + } + + _, ebx7, _, _ := cpuid(7, 0) + haveAVX2 := isSet(5, ebx7) && haveAVX + haveBMI2 := isSet(8, ebx7) + + useAVX2 = haveAVX2 && haveBMI2 +} + +// isSet checks if bit at bitpos is set in value. +func isSet(bitpos uint, value uint32) bool { + return value&(1<