2020-02-15 17:21:30 +01:00
|
|
|
package stupidgcm
|
|
|
|
|
|
|
|
import (
|
2021-05-26 09:20:22 +02:00
|
|
|
"runtime"
|
|
|
|
|
2020-02-15 17:21:30 +01:00
|
|
|
"golang.org/x/sys/cpu"
|
|
|
|
)
|
|
|
|
|
2021-09-08 19:48:13 +02:00
|
|
|
// PreferOpenSSLAES256GCM tells us if OpenSSL AES-256-GCM is faster than Go stdlib
|
|
|
|
// on this machine.
|
2020-02-15 17:21:30 +01:00
|
|
|
//
|
2020-04-13 22:34:07 +02:00
|
|
|
// Go GCM is only faster if the CPU either:
|
2020-02-15 17:21:30 +01:00
|
|
|
//
|
2020-04-13 22:34:07 +02:00
|
|
|
// 1) Is X86_64 && has AES instructions && Go is v1.6 or higher
|
|
|
|
// 2) Is ARM64 && has AES instructions && Go is v1.11 or higher
|
|
|
|
// (commit https://github.com/golang/go/commit/4f1f503373cda7160392be94e3849b0c9b9ebbda)
|
2020-02-15 17:21:30 +01:00
|
|
|
//
|
2021-08-30 11:31:01 +02:00
|
|
|
// See https://github.com/rfjakob/gocryptfs/wiki/CPU-Benchmarks
|
2020-02-15 17:21:30 +01:00
|
|
|
// for benchmarks.
|
2021-09-08 19:48:13 +02:00
|
|
|
func PreferOpenSSLAES256GCM() bool {
|
2020-02-15 17:21:30 +01:00
|
|
|
if BuiltWithoutOpenssl {
|
|
|
|
return false
|
|
|
|
}
|
2021-09-14 18:47:07 +02:00
|
|
|
// If the CPU has AES acceleration, Go stdlib is faster
|
|
|
|
if CpuHasAES() {
|
2020-02-15 17:21:30 +01:00
|
|
|
return false
|
|
|
|
}
|
2021-09-14 18:47:07 +02:00
|
|
|
// Otherwise OpenSSL is probably faster
|
2020-02-15 17:21:30 +01:00
|
|
|
return true
|
|
|
|
}
|
2021-09-08 19:48:13 +02:00
|
|
|
|
|
|
|
// PreferOpenSSLXchacha20poly1305 returns true if OpenSSL Xchacha20poly1305 is
|
|
|
|
// faster than Go stdlib on this machine.
|
|
|
|
func PreferOpenSSLXchacha20poly1305() bool {
|
|
|
|
if BuiltWithoutOpenssl {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Go x/crypto has optimized assembly for amd64:
|
|
|
|
// https://github.com/golang/crypto/blob/master/chacha20poly1305/chacha20poly1305_amd64.s
|
|
|
|
if runtime.GOARCH == "amd64" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// On arm64 and arm, OpenSSL is faster. Probably everwhere else too.
|
|
|
|
return true
|
|
|
|
}
|
2021-09-14 18:47:07 +02:00
|
|
|
|
|
|
|
// CpuHasAES tells you if the CPU we are running has AES acceleration that is
|
|
|
|
// usable by the Go crypto library.
|
|
|
|
func CpuHasAES() bool {
|
|
|
|
// Safe to call on other architectures - will just read false.
|
|
|
|
if cpu.X86.HasAES || cpu.ARM64.HasAES {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// On the Apple M1, the CPU has AES acceleration, despite cpu.ARM64.HasAES
|
|
|
|
// reading false: https://github.com/rfjakob/gocryptfs/issues/556#issuecomment-848079309
|
|
|
|
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|