Documentation
¶
Overview ¶
Package yEncBodyEnc provides the reference Go implementation of the yEnc Body Encryption Standard.
This package implements XChaCha20-Poly1305 authenticated encryption for binary data before yEnc encoding, maintaining full compatibility with existing yEnc parsers and protocols. It uses Argon2id key derivation with cryptographically secure random salt generation to provide both confidentiality and integrity protection.
The implementation follows the complete yEnc Body Encryption Standard specification available at: https://github.com/Tensai75/yenc-encryption-standards
Basic usage:
// Create cipher with random salt
cipher, err := yEncBodyEnc.NewCipher("password", "")
if err != nil {
log.Fatal(err)
}
// Encrypt data
encrypted, salt, authTag, err := cipher.Encrypt(data, 1)
if err != nil {
log.Fatal(err)
}
// Decrypt with new cipher using same salt
decryptCipher, err := yEncBodyEnc.NewCipher("password", salt)
if err != nil {
log.Fatal(err)
}
decrypted, err := decryptCipher.Decrypt(encrypted, authTag, 1)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeriveKey ¶
DeriveKey derives a 32-byte master key using Argon2id key derivation function.
Parameters:
- password: Password string to derive key from
- salt: 16-byte salt for domain separation
Returns:
- []byte: 32-byte derived key suitable for XChaCha20-Poly1305
Note: This function is deterministic - same password and salt always produce the same key.
func DeriveNonce ¶
DeriveNonce derives a 24-byte nonce for XChaCha20-Poly1305 using HMAC-SHA256.
Parameters:
- key: 32-byte derived key from DeriveKey function
- segmentIndex: Segment number (must be >= 1, affects nonce derivation)
Returns:
- []byte: 24-byte nonce suitable for XChaCha20-Poly1305
Note: This function is deterministic and does not use random number generation.
func GenerateSalt ¶
GenerateSalt generates a cryptographically secure random 16-byte salt using crypto/rand.
Returns:
- []byte: 16-byte cryptographically secure random salt
- error: Error if the system's random number generator fails
Note: This function is called automatically by NewCipher when an empty salt is provided.
Types ¶
type Cipher ¶
type Cipher struct {
// contains filtered or unexported fields
}
Cipher holds the pre-derived key and salt for encrypting/decrypting multiple segments.
The key is derived once during NewCipher creation using Argon2id and reused for all encrypt/decrypt operations with the same cipher instance. Different segment indices produce unique nonces to ensure semantic security across segments.
func NewCipher ¶
NewCipher creates a new Cipher instance with a pre-derived key from the password and salt.
Parameters:
- password: Password for Argon2id key derivation (cannot be empty)
- salt: 32-character hex string (16 bytes) or empty string for random salt generation
Returns:
- *Cipher: Cipher instance with accessible Salt field for multiple operations
- error: Error if password is empty, salt format is invalid, or random generation fails
Example:
// Random salt generation
cipher, err := NewCipher("password", "")
if err != nil {
log.Fatal(err)
}
// Salt is now accessible: cipher.Salt()
// Use existing salt for decryption
decryptCipher, err := NewCipher("password", cipher.Salt())
func (*Cipher) Decrypt ¶
Decrypt decrypts binary data using XChaCha20-Poly1305 authenticated decryption.
Parameters:
- input: Encrypted binary data (cannot be empty)
- authtag: Authentication tag as 32-character hex string (16 bytes)
- segmentIndex: Segment number used during encryption (must be >= 1 and match)
Returns:
- []byte: Original plaintext binary data
- error: Error if validation fails, authentication fails, or decryption fails
Example:
// Create decrypt cipher with same password and salt from encryption
decryptCipher, err := NewCipher("password", salt)
if err != nil {
log.Fatal(err)
}
decrypted, err := decryptCipher.Decrypt(encrypted, authTag, 1)
if err != nil {
log.Fatal("Authentication failed or data corrupted:", err)
}
func (*Cipher) Encrypt ¶
Encrypt encrypts binary data using XChaCha20-Poly1305 authenticated encryption.
Parameters:
- input: Binary data to encrypt (cannot be empty)
- segmentIndex: Segment number for multi-part files (must be >= 1, affects nonce derivation)
Returns:
- []byte: Encrypted binary data (same length as input)
- string: Authentication tag as 32-character hex string (16 bytes)
- error: Error if input is empty, segmentIndex is 0, or encryption fails
Example:
encrypted, authTag, err := cipher.Encrypt(fileData, 1)
if err != nil {
log.Fatal(err)
}
// encrypted data is ready for yEnc encoding
// cipher.Salt and authTag must be stored/transmitted for decryption
func (*Cipher) Salt ¶
Salt returns the salt used by this cipher instance as a 32-character hex string.
Returns:
- string: Salt as 32-character hex string (16 bytes represented as lowercase hex)
Example:
cipher, _ := NewCipher("password", "") // Random salt
salt := cipher.Salt() // Get the salt for later use
// For decryption, use the same salt
decryptCipher, _ := NewCipher("password", salt)