mirror of
https://github.com/rwinkhart/rcw.git
synced 2026-09-05 16:47:24 -04:00
Add high-level Decrypt() and Encrypt() functions for use by importing applications
This commit is contained in:
+2
-12
@@ -24,10 +24,6 @@ import (
|
|||||||
// RPC password sharing
|
// RPC password sharing
|
||||||
|
|
||||||
// TODO Enhancements:
|
// TODO Enhancements:
|
||||||
// Keyfile:
|
|
||||||
// Store:
|
|
||||||
// Hash of passphrase (prevent user from losing data by accidentally providing incorrect passphrase during encryption)
|
|
||||||
// Order of algorithms (determined randomly at keyfile generation)
|
|
||||||
// Security:
|
// Security:
|
||||||
// Play with nonce sizes and Argon2 parameters to find the best speed-security balance
|
// Play with nonce sizes and Argon2 parameters to find the best speed-security balance
|
||||||
// Standalone cmd:
|
// Standalone cmd:
|
||||||
@@ -41,12 +37,7 @@ func main() {
|
|||||||
case 3:
|
case 3:
|
||||||
// decrypt file
|
// decrypt file
|
||||||
encBytes, _ := os.ReadFile("encrypted-example.txt")
|
encBytes, _ := os.ReadFile("encrypted-example.txt")
|
||||||
decBytes, err := wrappers.DecryptCha(encBytes, []byte(os.Args[2]))
|
decBytes, err := wrappers.Decrypt(encBytes, []byte(os.Args[2]))
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
decBytes, err = wrappers.DecryptAES(decBytes, []byte(os.Args[2]))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
@@ -54,8 +45,7 @@ func main() {
|
|||||||
fmt.Println(string(decBytes))
|
fmt.Println(string(decBytes))
|
||||||
case 4:
|
case 4:
|
||||||
// encrypt data (from cli args)
|
// encrypt data (from cli args)
|
||||||
encBytes := wrappers.EncryptAES([]byte(os.Args[2]), []byte(os.Args[3]))
|
encBytes := wrappers.Encrypt([]byte(os.Args[2]), []byte(os.Args[3]))
|
||||||
encBytes = wrappers.EncryptCha(encBytes, []byte(os.Args[3]))
|
|
||||||
os.WriteFile("encrypted-example.txt", encBytes, 0644)
|
os.WriteFile("encrypted-example.txt", encBytes, 0644)
|
||||||
default:
|
default:
|
||||||
// request served data
|
// request served data
|
||||||
|
|||||||
+7
-7
@@ -13,7 +13,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// EncryptAES encrypts data using AES-256-GCM.
|
// EncryptAES encrypts data using AES-256-GCM.
|
||||||
func EncryptAES(data []byte, passphrase []byte) []byte {
|
func encryptAES(decBytes []byte, passphrase []byte) []byte {
|
||||||
// generate a random salt
|
// generate a random salt
|
||||||
salt := make([]byte, saltSize)
|
salt := make([]byte, saltSize)
|
||||||
io.ReadFull(rand.Reader, salt)
|
io.ReadFull(rand.Reader, salt)
|
||||||
@@ -32,7 +32,7 @@ func EncryptAES(data []byte, passphrase []byte) []byte {
|
|||||||
io.ReadFull(rand.Reader, nonce)
|
io.ReadFull(rand.Reader, nonce)
|
||||||
|
|
||||||
// encrypt the data
|
// encrypt the data
|
||||||
ciphertext := aesGCM.Seal(nil, nonce, data, nil)
|
ciphertext := aesGCM.Seal(nil, nonce, decBytes, nil)
|
||||||
|
|
||||||
// format: salt + nonce + ciphertext
|
// format: salt + nonce + ciphertext
|
||||||
result := make([]byte, 0, saltSize+nonceSizeAES+len(ciphertext))
|
result := make([]byte, 0, saltSize+nonceSizeAES+len(ciphertext))
|
||||||
@@ -44,15 +44,15 @@ func EncryptAES(data []byte, passphrase []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DecryptAES decrypts data using AES256-GCM.
|
// DecryptAES decrypts data using AES256-GCM.
|
||||||
func DecryptAES(encryptedData []byte, passphrase []byte) ([]byte, error) {
|
func decryptAES(encBytes []byte, passphrase []byte) ([]byte, error) {
|
||||||
if len(encryptedData) < saltSize+nonceSizeAES {
|
if len(encBytes) < saltSize+nonceSizeAES {
|
||||||
return nil, errors.New("AES256-GCM: Encrypted data is too short")
|
return nil, errors.New("AES256-GCM: Encrypted data is too short")
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract salt, nonce, and ciphertext
|
// extract salt, nonce, and ciphertext
|
||||||
salt := encryptedData[:saltSize]
|
salt := encBytes[:saltSize]
|
||||||
nonce := encryptedData[saltSize : saltSize+nonceSizeAES]
|
nonce := encBytes[saltSize : saltSize+nonceSizeAES]
|
||||||
ciphertext := encryptedData[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 := deriveKey(passphrase, salt)
|
||||||
|
|||||||
+2
-3
@@ -13,13 +13,12 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// EncryptCha encrypts data using ChaCha20-Poly1305.
|
// EncryptCha encrypts data using ChaCha20-Poly1305.
|
||||||
func EncryptCha(data []byte, passphrase []byte) []byte {
|
func encryptCha(data []byte, passphrase []byte) []byte {
|
||||||
// generate a random salt
|
// generate a random salt
|
||||||
salt := make([]byte, saltSize)
|
salt := make([]byte, saltSize)
|
||||||
io.ReadFull(rand.Reader, salt)
|
io.ReadFull(rand.Reader, salt)
|
||||||
|
|
||||||
// derive key from passphrase using the salt
|
// derive key from passphrase using the salt
|
||||||
// TODO ensure the passphrase is consistent (store a hashed version to compare against)
|
|
||||||
key := deriveKey(passphrase, salt)
|
key := deriveKey(passphrase, salt)
|
||||||
|
|
||||||
// create ChaCha20-Poly1305 cipher
|
// create ChaCha20-Poly1305 cipher
|
||||||
@@ -42,7 +41,7 @@ func EncryptCha(data []byte, passphrase []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DecryptCha decrypts data using ChaCha20-Poly1305.
|
// DecryptCha decrypts data using ChaCha20-Poly1305.
|
||||||
func DecryptCha(encryptedData []byte, passphrase []byte) ([]byte, error) {
|
func decryptCha(encryptedData []byte, passphrase []byte) ([]byte, error) {
|
||||||
if len(encryptedData) < saltSize+nonceSizeCha {
|
if len(encryptedData) < saltSize+nonceSizeCha {
|
||||||
return nil, errors.New("ChaCha20-Poly1305: Encrypted data is too short")
|
return nil, errors.New("ChaCha20-Poly1305: Encrypted data is too short")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package wrappers
|
||||||
|
|
||||||
|
func Decrypt(encBytes []byte, passphrase []byte) ([]byte, error) {
|
||||||
|
var err error = nil
|
||||||
|
encBytes, err = decryptCha(encBytes, passphrase)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
encBytes, err = decryptAES(encBytes, passphrase)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return encBytes, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Encrypt(decBytes []byte, passphrase []byte) []byte {
|
||||||
|
decBytes = encryptAES(decBytes, passphrase)
|
||||||
|
decBytes = encryptCha(decBytes, passphrase)
|
||||||
|
return decBytes
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user