Documentation
¶
Index ¶
- Constants
- Variables
- func Decrypt(dst io.Writer, src io.Reader, key KeySource) error
- func Encrypt(dst io.Writer, src io.Reader, key KeySource, opts *Options) error
- func GenerateKeyPair() (pub, priv []byte, err error)
- func NewEncryptingWriter(dst io.Writer, key KeySource, opts *Options) (io.WriteCloser, error)
- func WriteHeader(w io.Writer, h Header) error
- func ZeroBytes(b []byte)
- type DecryptResult
- type Format
- type Header
- type KDFParams
- type KeySource
- type Options
Constants ¶
const ( Version = "1.0.0" DefaultArgon2Memory uint32 = 1024 * 1024 // 1 GiB in KB DefaultArgon2Time uint32 = 3 DefaultArgon2Threads uint8 = 4 DefaultKeyLen uint32 = 32 )
const ( // HybridPublicKeyLen is the length of a hybrid X25519 + ML-KEM-768 public key. HybridPublicKeyLen = x25519PubKeyLen + mlkemEncapKeyLen // 1216 // HybridPrivateKeyLen is the length of a hybrid X25519 + ML-KEM-768 private key. HybridPrivateKeyLen = x25519PrivKeyLen + mlkemSeedLen // 96 )
Variables ¶
var ( ErrInvalidHeader = errors.New("jfcrypt: invalid header") ErrUnsupportedAlgo = errors.New("jfcrypt: unsupported algorithm") ErrUnsupportedKDF = errors.New("jfcrypt: unsupported KDF") ErrUnsupportedFmt = errors.New("jfcrypt: unsupported format") ErrInvalidKey = errors.New("jfcrypt: invalid key") ErrKeyLength = errors.New("jfcrypt: key must be exactly 32 bytes") ErrEmptyPassphrase = errors.New("jfcrypt: passphrase cannot be empty") ErrDecryptFailed = errors.New("jfcrypt: decryption failed") ErrInvalidSalt = errors.New("jfcrypt: invalid salt encoding") ErrInvalidPublicKey = errors.New("jfcrypt: invalid public key") ErrInvalidPrivateKey = errors.New("jfcrypt: invalid private key") ErrKEMFailed = errors.New("jfcrypt: key encapsulation failed") )
Functions ¶
func Decrypt ¶
Decrypt reads a jfcrypt stream from src and writes plaintext to dst. This is a convenience wrapper around NewDecryptingReader.
func Encrypt ¶
Encrypt reads plaintext from src, encrypts it, and writes the jfcrypt header + ciphertext to dst. This is a convenience wrapper around NewEncryptingWriter.
func GenerateKeyPair ¶
GenerateKeyPair generates a hybrid X25519 + ML-KEM-768 keypair. Public key: X25519 pub (32) || ML-KEM encapsulation key (1184) = 1216 bytes. Private key: X25519 priv (32) || ML-KEM seed (64) = 96 bytes.
func NewEncryptingWriter ¶
NewEncryptingWriter returns a writer that encrypts data written to it. The jfcrypt header is written to dst immediately. The caller writes plaintext to the returned writer, then calls Close() to finalize. Close zeros key material.
func WriteHeader ¶
WriteHeader serializes the header as a single JSON line followed by newline.
Types ¶
type DecryptResult ¶
DecryptResult holds the plaintext reader and parsed header. Call Close() when done to zero key material.
func NewDecryptingReader ¶
func NewDecryptingReader(src io.Reader, key KeySource) (*DecryptResult, error)
NewDecryptingReader reads the jfcrypt header from src, derives the key, and returns a DecryptResult whose Reader yields plaintext. The header is consumed immediately and available via DecryptResult.Header.
func (*DecryptResult) Close ¶
func (d *DecryptResult) Close() error
Close zeros key material. Always call this when done reading.
type Header ¶
type Header struct {
Version string `json:"version"`
Algorithm string `json:"algorithm"`
Format Format `json:"format"`
KDF KDFParams `json:"kdf"`
}
Header represents the JSON metadata line at the start of a jfcrypt file.
func ParseHeader ¶
ParseHeader reads and validates a jfcrypt header from the stream. It consumes exactly the first line (through the newline). Headers larger than 64 KB are rejected.
type KDFParams ¶
type KDFParams struct {
Algorithm string `json:"algorithm"`
Salt string `json:"salt,omitempty"`
Time uint32 `json:"time,omitempty"`
Memory uint32 `json:"memory,omitempty"`
Threads uint8 `json:"threads,omitempty"`
KeyLen uint32 `json:"keylen,omitempty"`
EphemeralKey string `json:"ephemeral_key,omitempty"`
KEMCiphertext string `json:"kem_ciphertext,omitempty"`
}
KDFParams contains key derivation parameters stored in the header.
type KeySource ¶
type KeySource interface {
// Zero scrubs key material held by this KeySource.
Zero()
// contains filtered or unexported methods
}
KeySource provides the encryption key material. Implementations are constructed via KeyFromPassphrase, KeyFromRawBytes, or KeyFromKeyfile. Call Zero() when done to scrub sensitive material from memory.
func KeyFromKeyfile ¶
KeyFromKeyfile returns a KeySource that reads a 32-byte key from a file. The file may contain 32 raw bytes or base64-encoded 32 bytes.
func KeyFromPassphrase ¶
KeyFromPassphrase returns a KeySource that derives a key using Argon2id. The passphrase is copied internally.
func KeyFromPrivateKey ¶
KeyFromPrivateKey returns a KeySource for decryption using a hybrid X25519 + ML-KEM-768 private key (96 bytes).
func KeyFromPublicKey ¶
KeyFromPublicKey returns a KeySource for encryption using a hybrid X25519 + ML-KEM-768 public key (1216 bytes).
func KeyFromRawBytes ¶
KeyFromRawBytes returns a KeySource that uses the given 32-byte key directly. The value type forces compile-time length verification.
type Options ¶
type Options struct {
Format Format
Argon2Memory uint32 // in KB
Argon2Time uint32 // iterations
Argon2Threads uint8
}
Options controls encryption behavior. nil means use all defaults.
func DefaultOptions ¶
func DefaultOptions() *Options
DefaultOptions returns Options with secure defaults.