Skip to content

Commit 0efd220

Browse files
committed
configfile: overwrite and let keys run out of scope
As soon as we don't need them anymore, overwrite keys with zeros and make sure they run out of scope so we don't create a risk of inadvertedly using all-zero keys for encryption. #211
1 parent 72ddbae commit 0efd220

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

internal/configfile/config_file.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,23 @@ func CreateConfFile(filename string, password string, plaintextNames bool, logN
8787
if aessiv {
8888
cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagAESSIV])
8989
}
90-
91-
// Generate new random master key
92-
var key []byte
93-
if devrandom {
94-
key = randBytesDevRandom(cryptocore.KeyLen)
95-
} else {
96-
key = cryptocore.RandBytes(cryptocore.KeyLen)
90+
{
91+
// Generate new random master key
92+
var key []byte
93+
if devrandom {
94+
key = randBytesDevRandom(cryptocore.KeyLen)
95+
} else {
96+
key = cryptocore.RandBytes(cryptocore.KeyLen)
97+
}
98+
// Encrypt it using the password
99+
// This sets ScryptObject and EncryptedKey
100+
// Note: this looks at the FeatureFlags, so call it AFTER setting them.
101+
cf.EncryptKey(key, password, logN)
102+
for i := range key {
103+
key[i] = 0
104+
}
105+
// key runs out of scope here
97106
}
98-
99-
// Encrypt it using the password
100-
// This sets ScryptObject and EncryptedKey
101-
// Note: this looks at the FeatureFlags, so call it AFTER setting them.
102-
cf.EncryptKey(key, password, logN)
103-
104107
// Write file to disk
105108
return cf.WriteFile()
106109
}
@@ -197,14 +200,17 @@ func LoadConfFile(filename string, password string) ([]byte, *ConfFile, error) {
197200
// Uses scrypt with cost parameter logN and stores the scrypt parameters in
198201
// cf.ScryptObject.
199202
func (cf *ConfFile) EncryptKey(key []byte, password string, logN int) {
200-
// Generate derived key from password
203+
// Generate scrypt-derived key from password
201204
cf.ScryptObject = NewScryptKDF(logN)
202205
scryptHash := cf.ScryptObject.DeriveKey(password)
203-
204206
// Lock master key using password-based key
205207
useHKDF := cf.IsFeatureFlagSet(FlagHKDF)
206208
ce := getKeyEncrypter(scryptHash, useHKDF)
207209
cf.EncryptedKey = ce.EncryptBlock(key, 0, nil)
210+
// Purge scrypt-derived key
211+
for i := range scryptHash {
212+
scryptHash[i] = 0
213+
}
208214
}
209215

210216
// WriteFile - write out config in JSON format to file "filename.tmp"

0 commit comments

Comments
 (0)