Drop Go 1.4 compatability code everywhere

Yields a nice reduction in code size.
This commit is contained in:
Jakob Unterwurzacher 2017-03-05 17:44:14 +01:00
parent e646163442
commit 966308eeb7
19 changed files with 39 additions and 213 deletions

View File

@ -38,16 +38,17 @@ BUILDTIME=$(date +%s)
# Make sure we have the go binary
go version > /dev/null
# Go 1.5 changed the CLI syntax. Let's also support 1.3 and 1.4.
# "go version go1.6.2 linux/amd64" -> "1.6"
# Parse "go version go1.6.2 linux/amd64" to "1.6"
V=$(go version | cut -d" " -f3 | cut -c3-5)
# Reject old Go versions already here. It would fail with compile
# errors anyway.
if [[ $V == "1.3" || $V == "1.4" ]] ; then
LDFLAGS="-X main.GitVersion $GITVERSION -X main.GitVersionFuse $GITVERSIONFUSE -X main.BuildTime $BUILDTIME"
else
# Go 1.5+ wants an "=" here
LDFLAGS="-X main.GitVersion=$GITVERSION -X main.GitVersionFuse=$GITVERSIONFUSE -X main.BuildTime=$BUILDTIME"
echo "Error: you need Go 1.5 or higher to compile gocryptfs"
echo -n "You have: "
go version
fi
LDFLAGS="-X main.GitVersion=$GITVERSION -X main.GitVersionFuse=$GITVERSIONFUSE -X main.BuildTime=$BUILDTIME"
go build "-ldflags=$LDFLAGS" $@
(cd gocryptfs-xray; go build $@)

View File

@ -74,7 +74,7 @@ func New(key []byte, aeadType AEADTypeEnum, IVBitLen int) *CryptoCore {
}
aeadCipher = stupidgcm.New(key)
case BackendGoGCM:
aeadCipher, err = goGCMWrapper(blockCipher, IVLen)
aeadCipher, err = cipher.NewGCMWithNonceSize(blockCipher, IVLen)
case BackendAESSIV:
if IVLen != 16 {
// SIV supports any nonce size, but we only use 16.

View File

@ -1,20 +0,0 @@
// +build !go1.5
// = go 1.4 or lower
package cryptocore
import (
"testing"
)
// Native Go crypto with 128-bit IVs is only supported on Go 1.5 and up,
// this should panic.
func TestCryptoCoreNewGo14(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
key := make([]byte, 32)
New(key, BackendGoGCM, 128)
}

View File

@ -1,16 +0,0 @@
// +build go1.5
// = go 1.5 or higher
package cryptocore
import (
"testing"
)
func TestCryptoCoreNewGo15(t *testing.T) {
key := make([]byte, 32)
c := New(key, BackendGoGCM, 128)
if c.IVLen != 16 {
t.Fail()
}
}

View File

@ -16,7 +16,10 @@ func TestCryptoCoreNew(t *testing.T) {
if c.IVLen != 12 {
t.Fail()
}
// "New(key, BackendGoGCM, 128)" is tested for Go 1.4 and 1.5+ separately
c = New(key, BackendGoGCM, 128)
if c.IVLen != 16 {
t.Fail()
}
}
// "New" should panic on any key not 32 bytes long

View File

@ -1,29 +0,0 @@
// +build !go1.5
// = go 1.4 or lower
package cryptocore
import (
"crypto/cipher"
"fmt"
"github.com/rfjakob/gocryptfs/internal/tlog"
)
const (
// HaveModernGoGCM indicates if Go GCM supports 128-bit nonces
HaveModernGoGCM = false
)
// goGCMWrapper - This wrapper makes sure gocryptfs can be compiled on Go
// versions 1.4 and lower that lack NewGCMWithNonceSize().
// 128 bit GCM IVs will not work when using built-in Go crypto, obviously, when
// compiled on 1.4.
func goGCMWrapper(bc cipher.Block, nonceSize int) (cipher.AEAD, error) {
if nonceSize != 12 {
tlog.Warn.Printf("128 bit GCM IVs are not supported by Go 1.4 and lower.")
tlog.Warn.Printf("Please use openssl crypto or recompile using a newer Go runtime.")
return nil, fmt.Errorf("128 bit GCM IVs are not supported by Go 1.4 and lower")
}
return cipher.NewGCM(bc)
}

View File

@ -1,21 +0,0 @@
// +build go1.5
// = go 1.5 or higher
package cryptocore
import (
"crypto/cipher"
)
const (
// HaveModernGoGCM indicates if Go GCM supports 128-bit nonces
HaveModernGoGCM = true
)
// goGCMWrapper - This wrapper makes sure gocryptfs can be compiled on Go
// versions 1.4 and lower that lack NewGCMWithNonceSize().
// 128 bit GCM IVs will not work when using built-in Go crypto, obviously, when
// compiled on 1.4.
func goGCMWrapper(bc cipher.Block, nonceSize int) (cipher.AEAD, error) {
return cipher.NewGCMWithNonceSize(bc, nonceSize)
}

View File

@ -24,7 +24,7 @@ type NameTransform struct {
func New(e *eme.EMECipher, longNames bool, raw64 bool) *NameTransform {
b64 := base64.URLEncoding
if raw64 {
b64 = getRaw64Encoding()
b64 = base64.RawURLEncoding
}
return &NameTransform{
emeCipher: e,

View File

@ -1,18 +0,0 @@
//+build !go1.5
package nametransform
import (
"encoding/base64"
"log"
)
const (
HaveRaw64 = false
)
func getRaw64Encoding() *base64.Encoding {
log.Panicf("Tried to use base64.RawURLEncoding but your Go version does not provide it.\n" +
"You need Go 1.5 or higher.")
return nil
}

View File

@ -1,16 +0,0 @@
//+build go1.5
package nametransform
import (
"encoding/base64"
)
const (
// HaveRaw64 is true when Go is new enough to have base64.RawURLEncoding
HaveRaw64 = true
)
func getRaw64Encoding() *base64.Encoding {
return base64.RawURLEncoding
}

View File

@ -1,5 +1,3 @@
// +build go1.5
// Package speed implements the "-speed" command-line option,
// similar to "openssl speed".
// It benchmarks the crypto algorithms and libraries used by

View File

@ -1,11 +0,0 @@
// +build !go1.5
package speed
import (
"fmt"
)
func Run() {
fmt.Printf("Compile with Go 1.5 or higher to run the benchmarks\n")
}

View File

@ -1,5 +1,3 @@
// +build go1.5
package speed
/*

View File

@ -1,10 +1,6 @@
// +build go1.5
// = go 1.5 or higher
//
// We compare against Go's built-in GCM implementation. Since stupidgcm only
// supports 128-bit IVs and Go only supports that from 1.5 onward, we cannot
// run these tests on older Go versions.
package stupidgcm
import (

View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"log"
"log/syslog"
"os"
"golang.org/x/crypto/ssh/terminal"
@ -117,3 +118,27 @@ func init() {
postfix: ColorReset,
}
}
// SwitchToSyslog redirects the output of this logger to syslog.
func (l *toggledLogger) SwitchToSyslog(p syslog.Priority) {
w, err := syslog.New(p, ProgramName)
if err != nil {
Warn.Printf("SwitchToSyslog: %v", err)
} else {
l.SetOutput(w)
}
}
// SwitchLoggerToSyslog redirects the default log.Logger that the go-fuse lib uses
// to syslog.
func SwitchLoggerToSyslog(p syslog.Priority) {
w, err := syslog.New(p, ProgramName)
if err != nil {
Warn.Printf("SwitchLoggerToSyslog: %v", err)
} else {
log.SetPrefix("go-fuse: ")
// Disable printing the timestamp, syslog already provides that
log.SetFlags(0)
log.SetOutput(w)
}
}

View File

@ -1,16 +0,0 @@
// +build !go1.5
// = go 1.4 or lower
package tlog
import (
"log/syslog"
)
func (l *toggledLogger) SwitchToSyslog(p syslog.Priority) {
Info.Printf("Cannot switch to syslog - need Go 1.5 or higher")
}
func SwitchLoggerToSyslog(p syslog.Priority) {
Info.Printf("Cannot switch to syslog - need Go 1.5 or higher")
}

View File

@ -1,32 +0,0 @@
// +build go1.5
// = go 1.5 or higher
package tlog
import (
"log"
"log/syslog"
)
func (l *toggledLogger) SwitchToSyslog(p syslog.Priority) {
w, err := syslog.New(p, ProgramName)
if err != nil {
Warn.Printf("SwitchToSyslog: %v", err)
} else {
l.SetOutput(w)
}
}
// SwitchLoggerToSyslog redirects the default log.Logger that the go-fuse lib uses
// to syslog.
func SwitchLoggerToSyslog(p syslog.Priority) {
w, err := syslog.New(p, ProgramName)
if err != nil {
Warn.Printf("SwitchLoggerToSyslog: %v", err)
} else {
log.SetPrefix("go-fuse: ")
// Disable printing the timestamp, syslog already provides that
log.SetFlags(0)
log.SetOutput(w)
}
}

View File

@ -12,7 +12,6 @@ import (
"os"
"testing"
"github.com/rfjakob/gocryptfs/internal/cryptocore"
"github.com/rfjakob/gocryptfs/internal/stupidgcm"
"github.com/rfjakob/gocryptfs/tests/test_helpers"
)
@ -24,12 +23,7 @@ var opensslOpt string
func TestMain(m *testing.M) {
// Make "testing.Verbose()" return the correct value
flag.Parse()
var variants []string
if cryptocore.HaveModernGoGCM {
variants = append(variants, "-openssl=false")
} else {
fmt.Println("Skipping Go GCM tests, Go installation is too old")
}
variants := []string{"-openssl=false"}
if !stupidgcm.BuiltWithoutOpenssl {
variants = append(variants, "-openssl=true")
} else {

View File

@ -23,8 +23,6 @@ import (
"syscall"
"testing"
"github.com/rfjakob/gocryptfs/internal/cryptocore"
"github.com/rfjakob/gocryptfs/internal/nametransform"
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
"github.com/rfjakob/gocryptfs/tests/test_helpers"
)
@ -60,14 +58,6 @@ func TestMain(m *testing.M) {
// Make "testing.Verbose()" return the correct value
flag.Parse()
for _, testcase = range matrix {
if !cryptocore.HaveModernGoGCM && testcase.openssl != "true" {
fmt.Printf("Skipping Go GCM variant, Go installation is too old")
continue
}
if testcase.raw64 && !nametransform.HaveRaw64 {
fmt.Printf("Skipping raw64 test, Go installation is too old")
continue
}
if testing.Verbose() {
fmt.Printf("matrix: testcase = %#v\n", testcase)
}