mirror of
https://github.com/rwinkhart/rcw.git
synced 2026-08-27 20:36:30 -04:00
Use multiple KDFs (add Scrypt for AES256)
This commit is contained in:
+2
-1
@@ -27,7 +27,8 @@ import (
|
|||||||
|
|
||||||
// TODO Enhancements:
|
// TODO Enhancements:
|
||||||
// Security:
|
// Security:
|
||||||
// Play with nonce sizes and Argon2 parameters to find the best speed-security balance
|
// Handle decryption in the daemon and return decrypted data, rather than returning the passphrase
|
||||||
|
// Play with nonce sizes and KDF parameters to find the best speed-security balance
|
||||||
// Standalone cmd:
|
// Standalone cmd:
|
||||||
// Usable as symmetric-only GPG replacement
|
// Usable as symmetric-only GPG replacement
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -19,7 +19,7 @@ func encryptAES(decBytes []byte, passphrase []byte) []byte {
|
|||||||
io.ReadFull(rand.Reader, salt)
|
io.ReadFull(rand.Reader, salt)
|
||||||
|
|
||||||
// derive key from passphrase using the salt
|
// derive key from passphrase using the salt
|
||||||
key := deriveKey(passphrase, salt)
|
key := deriveKeyScrypt(passphrase, salt)
|
||||||
|
|
||||||
// create AES-256 cipher
|
// create AES-256 cipher
|
||||||
block, _ := aes.NewCipher(key)
|
block, _ := aes.NewCipher(key)
|
||||||
@@ -55,7 +55,7 @@ func decryptAES(encBytes []byte, passphrase []byte) ([]byte, error) {
|
|||||||
ciphertext := encBytes[saltSize+nonceSizeAES:]
|
ciphertext := encBytes[saltSize+nonceSizeAES:]
|
||||||
|
|
||||||
// derive key from passphrase using the salt
|
// derive key from passphrase using the salt
|
||||||
key := deriveKey(passphrase, salt)
|
key := deriveKeyScrypt(passphrase, salt)
|
||||||
|
|
||||||
// create AES-256 cipher
|
// create AES-256 cipher
|
||||||
block, _ := aes.NewCipher(key)
|
block, _ := aes.NewCipher(key)
|
||||||
|
|||||||
+2
-2
@@ -19,7 +19,7 @@ func encryptCha(data []byte, passphrase []byte) []byte {
|
|||||||
io.ReadFull(rand.Reader, salt)
|
io.ReadFull(rand.Reader, salt)
|
||||||
|
|
||||||
// derive key from passphrase using the salt
|
// derive key from passphrase using the salt
|
||||||
key := deriveKey(passphrase, salt)
|
key := deriveKeyArgon2(passphrase, salt)
|
||||||
|
|
||||||
// create ChaCha20-Poly1305 cipher
|
// create ChaCha20-Poly1305 cipher
|
||||||
stream, _ := chacha20poly1305.NewX(key)
|
stream, _ := chacha20poly1305.NewX(key)
|
||||||
@@ -52,7 +52,7 @@ func decryptCha(encryptedData []byte, passphrase []byte) ([]byte, error) {
|
|||||||
ciphertext := encryptedData[saltSize+nonceSizeCha:]
|
ciphertext := encryptedData[saltSize+nonceSizeCha:]
|
||||||
|
|
||||||
// derive key from passphrase using the salt
|
// derive key from passphrase using the salt
|
||||||
key := deriveKey(passphrase, salt)
|
key := deriveKeyArgon2(passphrase, salt)
|
||||||
|
|
||||||
// create ChaCha20-Poly1305 cipher
|
// create ChaCha20-Poly1305 cipher
|
||||||
stream, _ := chacha20poly1305.NewX(key)
|
stream, _ := chacha20poly1305.NewX(key)
|
||||||
|
|||||||
+18
-6
@@ -4,21 +4,33 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"golang.org/x/crypto/argon2"
|
"golang.org/x/crypto/argon2"
|
||||||
|
"golang.org/x/crypto/scrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// parameters for Argon2
|
// parameters for Argon2
|
||||||
argonTime = 8 // set to pass 1-second test in dev environment
|
argonTime = 8 // set to pass 1-second test in dev environment
|
||||||
argonMemory = 384 * 1024 // 384 MB
|
argonMemory = 384 * 1024 // 384 MB
|
||||||
argonKeyLen = 32 // 256 bits, key length for both ChaCha20 and AES256
|
|
||||||
|
// parameters for Scrypt
|
||||||
|
scryptN = 262144
|
||||||
|
scryptR = 8 // block size, recommended to be 8
|
||||||
|
scryptP = 1 // parallelization factor, recommended to be 1
|
||||||
|
|
||||||
// general constants
|
// general constants
|
||||||
saltSize = 16
|
keyLen = 32 // 256 bits, key length for both algorithms
|
||||||
|
saltSize = 16 // 128 bits, recommended salt size for both algorithms
|
||||||
)
|
)
|
||||||
|
|
||||||
var argonThreads = uint8(runtime.NumCPU())
|
var threads = uint8(runtime.NumCPU())
|
||||||
|
|
||||||
// DeriveKey derives an encryption key from a passphrase using Argon2.
|
// DeriveKeyArgon2 derives an encryption key from a passphrase using Argon2.
|
||||||
func deriveKey(passphrase []byte, salt []byte) []byte {
|
func deriveKeyArgon2(passphrase []byte, salt []byte) []byte {
|
||||||
return argon2.IDKey(passphrase, salt, argonTime, argonMemory, argonThreads, argonKeyLen)
|
return argon2.IDKey(passphrase, salt, argonTime, argonMemory, threads, keyLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeriveKeyScrypt derives an encryption key from a passphrase using Scrypt.
|
||||||
|
func deriveKeyScrypt(passphrase []byte, salt []byte) []byte {
|
||||||
|
key, _ := scrypt.Key(passphrase, salt, scryptN, scryptR, scryptP, keyLen)
|
||||||
|
return key
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user