2016-02-06 19:20:54 +01:00
|
|
|
package configfile
|
2015-09-13 21:47:18 +02:00
|
|
|
|
|
|
|
import (
|
2016-06-05 14:26:16 +02:00
|
|
|
"log"
|
2015-11-29 21:41:38 +01:00
|
|
|
"math"
|
|
|
|
"os"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
|
|
|
"golang.org/x/crypto/scrypt"
|
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
2017-05-07 22:15:01 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/exitcodes"
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2015-09-13 21:47:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-10-02 06:14:18 +02:00
|
|
|
// ScryptDefaultLogN is the default scrypt logN configuration parameter.
|
2017-03-25 19:22:43 +01:00
|
|
|
// logN=16 (N=2^16) uses 64MB of memory and takes 4 seconds on my Atom Z3735F
|
|
|
|
// netbook.
|
2016-02-06 19:20:54 +01:00
|
|
|
ScryptDefaultLogN = 16
|
2017-03-25 19:22:43 +01:00
|
|
|
// From RFC7914, section 2:
|
|
|
|
// At the current time, r=8 and p=1 appears to yield good
|
|
|
|
// results, but as memory latency and CPU parallelism increase, it is
|
|
|
|
// likely that the optimum values for both r and p will increase.
|
|
|
|
// We reject all lower values that we might get through modified config files.
|
|
|
|
scryptMinR = 8
|
|
|
|
scryptMinP = 1
|
|
|
|
// logN=10 takes 6ms on a Pentium G630. This should be fast enough for all
|
|
|
|
// purposes. We reject lower values.
|
|
|
|
scryptMinLogN = 10
|
|
|
|
// We always generate 32-byte salts. Anything smaller than that is rejected.
|
|
|
|
scryptMinSaltLen = 32
|
2015-09-13 21:47:18 +02:00
|
|
|
)
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// ScryptKDF is an instance of the scrypt key deriviation function.
|
|
|
|
type ScryptKDF struct {
|
2017-03-25 19:22:43 +01:00
|
|
|
// Salt is the random salt that is passed to scrypt
|
|
|
|
Salt []byte
|
|
|
|
// N: scrypt CPU/Memory cost parameter
|
|
|
|
N int
|
|
|
|
// R: scrypt block size parameter
|
|
|
|
R int
|
2017-05-07 12:22:15 +02:00
|
|
|
// P: scrypt parallelization parameter
|
2017-03-25 19:22:43 +01:00
|
|
|
P int
|
|
|
|
// KeyLen is the output data length
|
2015-09-13 21:47:18 +02:00
|
|
|
KeyLen int
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// NewScryptKDF returns a new instance of ScryptKDF.
|
|
|
|
func NewScryptKDF(logN int) ScryptKDF {
|
|
|
|
var s ScryptKDF
|
2016-02-06 19:20:54 +01:00
|
|
|
s.Salt = cryptocore.RandBytes(cryptocore.KeyLen)
|
2015-11-29 18:52:58 +01:00
|
|
|
if logN <= 0 {
|
2016-02-06 19:20:54 +01:00
|
|
|
s.N = 1 << ScryptDefaultLogN
|
2015-11-29 18:52:58 +01:00
|
|
|
} else {
|
|
|
|
s.N = 1 << uint32(logN)
|
|
|
|
}
|
2015-09-13 21:47:18 +02:00
|
|
|
s.R = 8 // Always 8
|
|
|
|
s.P = 1 // Always 1
|
2016-02-06 19:20:54 +01:00
|
|
|
s.KeyLen = cryptocore.KeyLen
|
2015-09-13 21:47:18 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// DeriveKey returns a new key from a supplied password.
|
2018-02-18 14:26:54 +01:00
|
|
|
func (s *ScryptKDF) DeriveKey(pw []byte) []byte {
|
2017-03-25 19:22:43 +01:00
|
|
|
s.validateParams()
|
|
|
|
|
2018-02-18 14:26:54 +01:00
|
|
|
k, err := scrypt.Key(pw, s.Salt, s.N, s.R, s.P, s.KeyLen)
|
2015-09-13 21:47:18 +02:00
|
|
|
if err != nil {
|
2016-06-05 14:26:16 +02:00
|
|
|
log.Panicf("DeriveKey failed: %v", err)
|
2015-09-13 21:47:18 +02:00
|
|
|
}
|
|
|
|
return k
|
|
|
|
}
|
2015-11-29 18:52:58 +01:00
|
|
|
|
|
|
|
// LogN - N is saved as 2^LogN, but LogN is much easier to work with.
|
|
|
|
// This function gives you LogN = Log2(N).
|
2016-10-02 06:14:18 +02:00
|
|
|
func (s *ScryptKDF) LogN() int {
|
2015-11-29 21:41:38 +01:00
|
|
|
return int(math.Log2(float64(s.N)) + 0.5)
|
2015-11-29 18:52:58 +01:00
|
|
|
}
|
2017-03-25 19:22:43 +01:00
|
|
|
|
|
|
|
// validateParams checks that all parameters are at or above hardcoded limits.
|
|
|
|
// If not, it exists with an error message.
|
|
|
|
// This makes sure we do not get weak parameters passed through a
|
|
|
|
// rougue gocryptfs.conf.
|
|
|
|
func (s *ScryptKDF) validateParams() {
|
|
|
|
minN := 1 << scryptMinLogN
|
|
|
|
if s.N < minN {
|
|
|
|
tlog.Fatal.Println("Fatal: scryptn below 10 is too low to make sense")
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ScryptParams)
|
2017-03-25 19:22:43 +01:00
|
|
|
}
|
|
|
|
if s.R < scryptMinR {
|
|
|
|
tlog.Fatal.Printf("Fatal: scrypt parameter R below minimum: value=%d, min=%d", s.R, scryptMinR)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ScryptParams)
|
2017-03-25 19:22:43 +01:00
|
|
|
}
|
|
|
|
if s.P < scryptMinP {
|
|
|
|
tlog.Fatal.Printf("Fatal: scrypt parameter P below minimum: value=%d, min=%d", s.P, scryptMinP)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ScryptParams)
|
2017-03-25 19:22:43 +01:00
|
|
|
}
|
|
|
|
if len(s.Salt) < scryptMinSaltLen {
|
|
|
|
tlog.Fatal.Printf("Fatal: scrypt salt length below minimum: value=%d, min=%d", len(s.Salt), scryptMinSaltLen)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ScryptParams)
|
2017-03-25 19:22:43 +01:00
|
|
|
}
|
|
|
|
if s.KeyLen < cryptocore.KeyLen {
|
2018-12-28 02:43:40 +01:00
|
|
|
tlog.Fatal.Printf("Fatal: scrypt parameter KeyLen below minimum: value=%d, min=%d", s.KeyLen, cryptocore.KeyLen)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.ScryptParams)
|
2017-03-25 19:22:43 +01:00
|
|
|
}
|
|
|
|
}
|