From 57612a278bdd26de611b543be311748ca4dc42ca Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sat, 25 Feb 2017 18:51:17 +0100 Subject: configfile: rename "kdf.go" -> "scrypt.go" This really only handles scrypt and no other key-derivation functions. Renaming the files prevents confusion once we introduce HKDF. renamed: internal/configfile/kdf.go -> internal/configfile/scrypt.go renamed: internal/configfile/kdf_test.go -> internal/configfile/scrypt_test.go --- internal/configfile/kdf.go | 62 -------------------------------------- internal/configfile/kdf_test.go | 60 ------------------------------------ internal/configfile/scrypt.go | 62 ++++++++++++++++++++++++++++++++++++++ internal/configfile/scrypt_test.go | 60 ++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 122 deletions(-) delete mode 100644 internal/configfile/kdf.go delete mode 100644 internal/configfile/kdf_test.go create mode 100644 internal/configfile/scrypt.go create mode 100644 internal/configfile/scrypt_test.go diff --git a/internal/configfile/kdf.go b/internal/configfile/kdf.go deleted file mode 100644 index 31bcbe4..0000000 --- a/internal/configfile/kdf.go +++ /dev/null @@ -1,62 +0,0 @@ -package configfile - -import ( - "log" - "math" - "os" - - "golang.org/x/crypto/scrypt" - - "github.com/rfjakob/gocryptfs/internal/cryptocore" - "github.com/rfjakob/gocryptfs/internal/tlog" -) - -const ( - // ScryptDefaultLogN is the default scrypt logN configuration parameter. - // 1 << 16 uses 64MB of memory, - // takes 4 seconds on my Atom Z3735F netbook - ScryptDefaultLogN = 16 -) - -// ScryptKDF is an instance of the scrypt key deriviation function. -type ScryptKDF struct { - Salt []byte - N int - R int - P int - KeyLen int -} - -// NewScryptKDF returns a new instance of ScryptKDF. -func NewScryptKDF(logN int) ScryptKDF { - var s ScryptKDF - s.Salt = cryptocore.RandBytes(cryptocore.KeyLen) - if logN <= 0 { - s.N = 1 << ScryptDefaultLogN - } else { - if logN < 10 { - tlog.Fatal.Println("Error: scryptn below 10 is too low to make sense. Aborting.") - os.Exit(1) - } - s.N = 1 << uint32(logN) - } - s.R = 8 // Always 8 - s.P = 1 // Always 1 - s.KeyLen = cryptocore.KeyLen - return s -} - -// DeriveKey returns a new key from a supplied password. -func (s *ScryptKDF) DeriveKey(pw string) []byte { - k, err := scrypt.Key([]byte(pw), s.Salt, s.N, s.R, s.P, s.KeyLen) - if err != nil { - log.Panicf("DeriveKey failed: %v", err) - } - return k -} - -// LogN - N is saved as 2^LogN, but LogN is much easier to work with. -// This function gives you LogN = Log2(N). -func (s *ScryptKDF) LogN() int { - return int(math.Log2(float64(s.N)) + 0.5) -} diff --git a/internal/configfile/kdf_test.go b/internal/configfile/kdf_test.go deleted file mode 100644 index c1a656a..0000000 --- a/internal/configfile/kdf_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package configfile - -import ( - "testing" -) - -/* -Results on a 2.7GHz Pentium G630: - -gocryptfs/cryptfs$ go test -bench=. -PASS -BenchmarkScrypt10-2 300 6021435 ns/op ... 6ms -BenchmarkScrypt11-2 100 11861460 ns/op -BenchmarkScrypt12-2 100 23420822 ns/op -BenchmarkScrypt13-2 30 47666518 ns/op -BenchmarkScrypt14-2 20 92561590 ns/op ... 92ms -BenchmarkScrypt15-2 10 183971593 ns/op -BenchmarkScrypt16-2 3 368506365 ns/op -BenchmarkScrypt17-2 2 755502608 ns/op ... 755ms -ok github.com/rfjakob/gocryptfs/cryptfs 18.772s -*/ - -func benchmarkScryptN(n int, b *testing.B) { - kdf := NewScryptKDF(n) - for i := 0; i < b.N; i++ { - kdf.DeriveKey("test") - } -} - -func BenchmarkScrypt10(b *testing.B) { - benchmarkScryptN(10, b) -} - -func BenchmarkScrypt11(b *testing.B) { - benchmarkScryptN(11, b) -} - -func BenchmarkScrypt12(b *testing.B) { - benchmarkScryptN(12, b) -} - -func BenchmarkScrypt13(b *testing.B) { - benchmarkScryptN(13, b) -} - -func BenchmarkScrypt14(b *testing.B) { - benchmarkScryptN(14, b) -} - -func BenchmarkScrypt15(b *testing.B) { - benchmarkScryptN(15, b) -} - -func BenchmarkScrypt16(b *testing.B) { - benchmarkScryptN(16, b) -} - -func BenchmarkScrypt17(b *testing.B) { - benchmarkScryptN(17, b) -} diff --git a/internal/configfile/scrypt.go b/internal/configfile/scrypt.go new file mode 100644 index 0000000..31bcbe4 --- /dev/null +++ b/internal/configfile/scrypt.go @@ -0,0 +1,62 @@ +package configfile + +import ( + "log" + "math" + "os" + + "golang.org/x/crypto/scrypt" + + "github.com/rfjakob/gocryptfs/internal/cryptocore" + "github.com/rfjakob/gocryptfs/internal/tlog" +) + +const ( + // ScryptDefaultLogN is the default scrypt logN configuration parameter. + // 1 << 16 uses 64MB of memory, + // takes 4 seconds on my Atom Z3735F netbook + ScryptDefaultLogN = 16 +) + +// ScryptKDF is an instance of the scrypt key deriviation function. +type ScryptKDF struct { + Salt []byte + N int + R int + P int + KeyLen int +} + +// NewScryptKDF returns a new instance of ScryptKDF. +func NewScryptKDF(logN int) ScryptKDF { + var s ScryptKDF + s.Salt = cryptocore.RandBytes(cryptocore.KeyLen) + if logN <= 0 { + s.N = 1 << ScryptDefaultLogN + } else { + if logN < 10 { + tlog.Fatal.Println("Error: scryptn below 10 is too low to make sense. Aborting.") + os.Exit(1) + } + s.N = 1 << uint32(logN) + } + s.R = 8 // Always 8 + s.P = 1 // Always 1 + s.KeyLen = cryptocore.KeyLen + return s +} + +// DeriveKey returns a new key from a supplied password. +func (s *ScryptKDF) DeriveKey(pw string) []byte { + k, err := scrypt.Key([]byte(pw), s.Salt, s.N, s.R, s.P, s.KeyLen) + if err != nil { + log.Panicf("DeriveKey failed: %v", err) + } + return k +} + +// LogN - N is saved as 2^LogN, but LogN is much easier to work with. +// This function gives you LogN = Log2(N). +func (s *ScryptKDF) LogN() int { + return int(math.Log2(float64(s.N)) + 0.5) +} diff --git a/internal/configfile/scrypt_test.go b/internal/configfile/scrypt_test.go new file mode 100644 index 0000000..c1a656a --- /dev/null +++ b/internal/configfile/scrypt_test.go @@ -0,0 +1,60 @@ +package configfile + +import ( + "testing" +) + +/* +Results on a 2.7GHz Pentium G630: + +gocryptfs/cryptfs$ go test -bench=. +PASS +BenchmarkScrypt10-2 300 6021435 ns/op ... 6ms +BenchmarkScrypt11-2 100 11861460 ns/op +BenchmarkScrypt12-2 100 23420822 ns/op +BenchmarkScrypt13-2 30 47666518 ns/op +BenchmarkScrypt14-2 20 92561590 ns/op ... 92ms +BenchmarkScrypt15-2 10 183971593 ns/op +BenchmarkScrypt16-2 3 368506365 ns/op +BenchmarkScrypt17-2 2 755502608 ns/op ... 755ms +ok github.com/rfjakob/gocryptfs/cryptfs 18.772s +*/ + +func benchmarkScryptN(n int, b *testing.B) { + kdf := NewScryptKDF(n) + for i := 0; i < b.N; i++ { + kdf.DeriveKey("test") + } +} + +func BenchmarkScrypt10(b *testing.B) { + benchmarkScryptN(10, b) +} + +func BenchmarkScrypt11(b *testing.B) { + benchmarkScryptN(11, b) +} + +func BenchmarkScrypt12(b *testing.B) { + benchmarkScryptN(12, b) +} + +func BenchmarkScrypt13(b *testing.B) { + benchmarkScryptN(13, b) +} + +func BenchmarkScrypt14(b *testing.B) { + benchmarkScryptN(14, b) +} + +func BenchmarkScrypt15(b *testing.B) { + benchmarkScryptN(15, b) +} + +func BenchmarkScrypt16(b *testing.B) { + benchmarkScryptN(16, b) +} + +func BenchmarkScrypt17(b *testing.B) { + benchmarkScryptN(17, b) +} -- cgit v1.2.3