Implement AES256+GCM encryption/decryption (cascading)

This commit is contained in:
2025-05-03 18:13:40 -04:00
parent b93acdcaef
commit 146901a479
4 changed files with 102 additions and 12 deletions
+16 -1
View File
@@ -16,6 +16,19 @@ import (
// rcw enc <text> <passwd> : Encrypts the provided text and outputs the ciphertext to encrypted-example.txt // rcw enc <text> <passwd> : Encrypts the provided text and outputs the ciphertext to encrypted-example.txt
// rcw dec <passwd> : Decrypts the provided file and outputs the plaintext to stdout // rcw dec <passwd> : Decrypts the provided file and outputs the plaintext to stdout
// TODO Tests:
// Salt (aes+chacha)
// Nonce (aes+chacha)
// Encryption (individual+combined)
// Decryption (individual+combined)
// RPC password sharing
// 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)
func main() { func main() {
switch len(os.Args) { switch len(os.Args) {
case 2: case 2:
@@ -25,10 +38,12 @@ func main() {
// decrypt file // decrypt file
encBytes, _ := os.ReadFile("encrypted-example.txt") encBytes, _ := os.ReadFile("encrypted-example.txt")
decBytes := wrappers.DecryptCha(encBytes, []byte(os.Args[2])) decBytes := wrappers.DecryptCha(encBytes, []byte(os.Args[2]))
decBytes = wrappers.DecryptAES(decBytes, []byte(os.Args[2]))
fmt.Println(string(decBytes)) fmt.Println(string(decBytes))
case 4: case 4:
// encrypt data (from cli args) // encrypt data (from cli args)
encBytes := wrappers.EncryptCha([]byte(os.Args[2]), []byte(os.Args[3])) encBytes := wrappers.EncryptAES([]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
+75
View File
@@ -0,0 +1,75 @@
package wrappers
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
)
const (
nonceSizeAES = 12 // GCM standard nonce size is 12 bytes
)
// EncryptAES encrypts data using AES-256-GCM
func EncryptAES(data []byte, passphrase []byte) []byte {
// generate a random salt
salt := make([]byte, saltSize)
io.ReadFull(rand.Reader, salt)
// derive key from passphrase using the salt
key := deriveKey(passphrase, salt)
// create AES-256 cipher
block, _ := aes.NewCipher(key)
// create GCM mode
aesGCM, _ := cipher.NewGCM(block)
// generate a random nonce
nonce := make([]byte, nonceSizeAES)
io.ReadFull(rand.Reader, nonce)
// encrypt the data
ciphertext := aesGCM.Seal(nil, nonce, data, nil)
// format: salt + nonce + ciphertext
result := make([]byte, 0, saltSize+nonceSizeAES+len(ciphertext))
result = append(result, salt...)
result = append(result, nonce...)
result = append(result, ciphertext...)
return result
}
// DecryptAES decrypts data using AES-256-GCM
func DecryptAES(encryptedData []byte, passphrase []byte) []byte {
if len(encryptedData) < saltSize+nonceSizeAES {
fmt.Println("Encrypted data is too short")
return nil
}
// extract salt, nonce, and ciphertext
salt := encryptedData[:saltSize]
nonce := encryptedData[saltSize : saltSize+nonceSizeAES]
ciphertext := encryptedData[saltSize+nonceSizeAES:]
// derive key from passphrase using the salt
key := deriveKey(passphrase, salt)
// create AES-256 cipher
block, _ := aes.NewCipher(key)
// create GCM mode
aesGCM, _ := cipher.NewGCM(block)
// decrypt the data
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
fmt.Printf("Decryption failed (possibly wrong passphrase): %s", err.Error())
return nil
}
return plaintext
}
+10 -11
View File
@@ -10,13 +10,12 @@ import (
const ( const (
nonceSizeCha = chacha20poly1305.NonceSizeX nonceSizeCha = chacha20poly1305.NonceSizeX
saltSizeCha = 16
) )
// 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, saltSizeCha) 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
@@ -24,17 +23,17 @@ func EncryptCha(data []byte, passphrase []byte) []byte {
key := deriveKey(passphrase, salt) key := deriveKey(passphrase, salt)
// create ChaCha20-Poly1305 cipher // create ChaCha20-Poly1305 cipher
aead, _ := chacha20poly1305.NewX(key) stream, _ := chacha20poly1305.NewX(key)
// generate a random nonce // generate a random nonce
nonce := make([]byte, nonceSizeCha) nonce := make([]byte, nonceSizeCha)
io.ReadFull(rand.Reader, nonce) io.ReadFull(rand.Reader, nonce)
// encrypt the data // encrypt the data
ciphertext := aead.Seal(nil, nonce, data, nil) ciphertext := stream.Seal(nil, nonce, data, nil)
// format: salt + nonce + ciphertext // format: salt + nonce + ciphertext
result := make([]byte, 0, saltSizeCha+nonceSizeCha+len(ciphertext)) result := make([]byte, 0, saltSize+nonceSizeCha+len(ciphertext))
result = append(result, salt...) result = append(result, salt...)
result = append(result, nonce...) result = append(result, nonce...)
result = append(result, ciphertext...) result = append(result, ciphertext...)
@@ -44,24 +43,24 @@ 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 { func DecryptCha(encryptedData []byte, passphrase []byte) []byte {
if len(encryptedData) < saltSizeCha+nonceSizeCha { if len(encryptedData) < saltSize+nonceSizeCha {
fmt.Println("Encrypted data is too short") fmt.Println("Encrypted data is too short")
return nil return nil
} }
// extract salt, nonce, and ciphertext // extract salt, nonce, and ciphertext
salt := encryptedData[:saltSizeCha] salt := encryptedData[:saltSize]
nonce := encryptedData[saltSizeCha : saltSizeCha+nonceSizeCha] nonce := encryptedData[saltSize : saltSize+nonceSizeCha]
ciphertext := encryptedData[saltSizeCha+nonceSizeCha:] ciphertext := encryptedData[saltSize+nonceSizeCha:]
// derive key from passphrase using the salt // derive key from passphrase using the salt
key := deriveKey(passphrase, salt) key := deriveKey(passphrase, salt)
// create ChaCha20-Poly1305 cipher // create ChaCha20-Poly1305 cipher
aead, _ := chacha20poly1305.NewX(key) stream, _ := chacha20poly1305.NewX(key)
// decrypt the data // decrypt the data
plaintext, err := aead.Open(nil, nonce, ciphertext, nil) plaintext, err := stream.Open(nil, nonce, ciphertext, nil)
if err != nil { if err != nil {
fmt.Printf("Decryption failed (possibly wrong passphrase): %s", err.Error()) fmt.Printf("Decryption failed (possibly wrong passphrase): %s", err.Error())
return nil return nil
+1
View File
@@ -11,6 +11,7 @@ const (
argonMemory = 64 * 1024 argonMemory = 64 * 1024
argonThreads = 4 argonThreads = 4
argonKeyLen = chacha20poly1305.KeySize argonKeyLen = chacha20poly1305.KeySize
saltSize = 16
) )
// DeriveKey derives an encryption key from a passphrase using Argon2. // DeriveKey derives an encryption key from a passphrase using Argon2.