Derive HKDF keys (per algo) from primary key: roughly doubles time/security efficiency

This commit is contained in:
2025-05-19 19:59:57 -04:00
parent 13ae080af2
commit 0e7a796bb1
4 changed files with 79 additions and 61 deletions
+18 -5
View File
@@ -3,11 +3,14 @@ package wrappers
// Decrypt decrypts the provided byte slice using the provided passphrase.
func Decrypt(encBytes []byte, passphrase []byte) ([]byte, error) {
var err error = nil
encBytes, err = decryptCha(encBytes, passphrase)
salt1 := encBytes[:saltSize1]
encBytes = encBytes[saltSize1:]
key1 := derivePrimaryKey(passphrase, salt1)
encBytes, err = decryptCha(encBytes, key1)
if err != nil {
return nil, err
}
encBytes, err = decryptAES(encBytes, passphrase)
encBytes, err = decryptAES(encBytes, key1)
if err != nil {
return nil, err
}
@@ -16,7 +19,17 @@ func Decrypt(encBytes []byte, passphrase []byte) ([]byte, error) {
// Encrypt encrypts the provided byte slice using the provided passphrase.
func Encrypt(decBytes []byte, passphrase []byte) []byte {
decBytes = encryptAES(decBytes, passphrase)
decBytes = encryptCha(decBytes, passphrase)
return decBytes
salt1 := getRandomBytes(saltSize1)
salt2AES := getRandomBytes(saltSize2)
salt2Cha := getRandomBytes(saltSize2)
key1 := derivePrimaryKey(passphrase, salt1)
key2AES := deriveSecondaryKey(key1, salt2AES, []byte(hkdfInfoAES))
key2Cha := deriveSecondaryKey(key1, salt2Cha, []byte(hkdfInfoCha))
decBytes = encryptAES(decBytes, key2AES, salt2AES)
decBytes = encryptCha(decBytes, key2Cha, salt2Cha)
// format: salt1 + decBytes per algorithm (salt2* + nonce + ciphertext)
encBytes := make([]byte, 0, saltSize1+len(decBytes))
encBytes = append(encBytes, salt1...)
encBytes = append(encBytes, decBytes...)
return encBytes
}