Documentation
¶
Overview ¶
Package zfscrypt implements the cryptographic primitives used by OpenZFS native (dataset-level) encryption, as added in OpenZFS 0.8 (2019) and stable since.
This package is the "math" half of ZFS encryption: KDF, MEK unwrap, per-block key derivation, AES-CCM/GCM block decryption. The "format" half — parsing the DSL_CRYPTO_KEY bonus area and extracting per-block IV/MAC from blkptr_t — lives in the pkg/go-filesystems/zfs driver, which already understands those on-disk structures.
References (OpenZFS source tree):
module/zfs/zio_crypt.c module/zfs/dsl_crypt.c include/sys/zio_crypt.h include/sys/dsl_crypt.h
Only the read path is currently needed (cloud-boot just needs to pull /boot/vmlinuz + /boot/initrd out of an encrypted root), so Wrap and EncryptBlock are not exposed. Adding them later is straightforward — Seal is already there in the underlying AEADs.
Algorithms covered:
AES-128/192/256-CCM (RFC 3610, via github.com/go-encryptions/ccm) AES-128/192/256-GCM (stdlib crypto/cipher.NewGCM)
Key derivation:
wrapping key = PBKDF2-HMAC-SHA1(passphrase, salt, iters, 32) per-block key = HKDF-SHA512(mek, salt=empty, info=blockSalt, KeyLen)
Index ¶
- Constants
- Variables
- func DecryptBlock(suite Suite, key, iv, mac, ciphertext, ad []byte) ([]byte, error)
- func DeriveBlockKey(suite Suite, mek, salt []byte) ([]byte, error)
- func DeriveWrappingKey(passphrase, salt []byte, iters int) ([]byte, error)
- func DeriveWrappingKeyBudget(passphrase, salt []byte, iters int, b *kdfbudget.Budget) ([]byte, error)
- func HMAC(hmacKey, data []byte) []byte
- func JoinWrappedKey(suite Suite, masterKey, hmacKey []byte) ([]byte, error)
- func Unwrap(suite Suite, wrappingKey, iv, mac, wrapped, ad []byte) (mek, hmacKey []byte, err error)
- type Suite
Constants ¶
const ( IVSize = 12 MACSize = 16 // WrappingKeyLen is the length of the user-derived wrapping key // regardless of the dataset's data-encryption suite. OpenZFS // derives 32 bytes (WRAPPING_KEY_LEN); the AEAD that wraps the MEK // then wraps the MEK with all of it, for every suite (see Unwrap). WrappingKeyLen = 32 // MasterKeyLen is MASTER_KEY_MAX_LEN in OpenZFS. MasterKeyLen = 32 // HMACKeyLen is SHA512_HMAC_KEYLEN in OpenZFS — the wrapped HMAC // key is a full 64-byte HMAC-SHA512 key, NOT 32 bytes. HMACKeyLen = 64 // WrappedKeySize is the size of the unwrap ciphertext input for the // 256-bit suites: master key (32) || HMAC key (64), decrypted as a // single AEAD ciphertext. The 128- and 192-bit suites carry a shorter // master key; see Suite.WrappedKeyLen. WrappedKeySize = MasterKeyLen + HMACKeyLen )
ZFS encryption uses 12-byte IVs and 16-byte MACs uniformly.
Variables ¶
var ErrLimit = kdfbudget.ErrLimit
ErrLimit is returned when the key derivation asks for more work than the budget allows. It is kdfbudget.ErrLimit.
Functions ¶
func DecryptBlock ¶
DecryptBlock authenticates and decrypts one encrypted ZFS block using a previously-derived per-block key. iv is the block's IV (12 bytes), mac is the block's authentication tag (16 bytes), ciphertext is the raw block payload (without IV/MAC appended), and ad is the additional authenticated data the pool computed over the blkptr's "sensitive" fields (blockid, logical birth txg, …).
Returns the plaintext or an error on tag-verification failure.
func DeriveBlockKey ¶
DeriveBlockKey derives the per-block data-encryption key from the dataset's master encryption key via HKDF-SHA512.
CONFIRMED against OpenZFS (zfs-2.2.x sources; judged on kmod 2.3.4): module/os/*/zfs/zio_crypt.c zio_do_crypt_data() derives the per-block key with
hkdf_sha512(master, keylen, /*salt*/ NULL, 0,
/*info*/ blk_salt, ZIO_DATA_SALT_LEN, out, keylen)
where hkdf_sha512's prototype is
hkdf_sha512(key_material, km_len, salt, salt_len, info, info_len, out, out_len)
— i.e. the HKDF *salt* is EMPTY and the per-block 8-byte salt is the HKDF *info*. (This is the opposite of the intuitive "salt is the salt" mapping; an earlier version of this function used the block salt as the HKDF salt and the suite name as info, which produced a key that did NOT decrypt real on-disk blocks.) Validated end-to-end against a real aes-256-gcm pool: the key produced here, with the block IV/MAC from the blkptr, decrypts on-disk ciphertext and the GCM tag verifies.
The `salt` argument is the encrypted block's BP_CRYPT salt (ZIO_DATA_SALT_LEN = 8 bytes). Output length matches the suite's data-key length (16/24/32).
func DeriveWrappingKey ¶
DeriveWrappingKey derives the dataset's wrapping key from the user passphrase via PBKDF2-HMAC-SHA1. The choice of SHA1 (rather than SHA512) matches what OpenZFS shipped in 0.8; do not "upgrade" without a corresponding on-disk format change.
iters must match the value stored in the DSL_CRYPTO_KEY's `iters` field. Salt is the 8-byte field stored next to it.
iters comes from an untrusted dataset header: the work is charged to a kdfbudget.Default budget first, so an absurd count is refused with ErrLimit before any hashing. OpenZFS has no upper bound; the default budget accepts every count up to 2^31. Use DeriveWrappingKeyBudget to choose the budget (kdfbudget.Unlimited for a trusted header).
Output: WrappingKeyLen (32) bytes.
func DeriveWrappingKeyBudget ¶ added in v0.2.0
func DeriveWrappingKeyBudget(passphrase, salt []byte, iters int, b *kdfbudget.Budget) ([]byte, error)
DeriveWrappingKeyBudget is DeriveWrappingKey with the caller's budget. The PBKDF2 run (one SHA-1 output block) is charged to b before it starts; a nil or zero Budget refuses.
func HMAC ¶
HMAC returns HMAC-SHA512-256 over data using the dataset's HMAC key (the 64-byte sibling of the MEK obtained from Unwrap). OpenZFS uses this to authenticate metadata that does not pass through the AEAD layer — most notably the per-block "salt" field. The truncation to 32 bytes matches OpenZFS's choice.
func JoinWrappedKey ¶
JoinWrappedKey builds the Unwrap input from the two byte arrays OpenZFS keeps in the DSL crypto key ZAP (DSL_CRYPTO_MASTER_KEY_1, always 32 bytes, and DSL_CRYPTO_HMAC_KEY_1, 64 bytes). For the 128- and 192-bit suites only the first KeyLen bytes of the master array are the wrapped key. The rest is padding whose content OpenZFS does not define (dsl_crypt.c leaves the buffer uninitialised, so it may be leftover stack), and is ignored.
func Unwrap ¶
Unwrap decrypts a wrapped (MEK || HMAC key) blob using the dataset's wrapping key.
suite chooses the AEAD: CCM or GCM. iv is the 12-byte field stored in the DSL_CRYPTO_KEY object; mac is the matching 16-byte authentication tag; wrapped is the suite.WrappedKeyLen() byte ciphertext (master key of suite.KeyLen() bytes || 64-byte HMAC key); JoinWrappedKey builds it from the on-disk ZAP values.
ad is the additional authenticated data the pool computed at wrap time. OpenZFS zio_crypt_key_unwrap() uses LE64(guid) for version-0 keys, and LE64(guid)||LE64(crypt)||LE64(version) for the current version. The caller must pass the same bytes the pool used; passing the wrong AD shows up as a tag-verification failure rather than as decryption garbage.
Returns the MEK (suite.KeyLen() bytes: 16, 24 or 32, as OpenZFS keeps it) and the 64-byte HMAC key on success.
Types ¶
type Suite ¶
type Suite uint8
Suite enumerates the encryption algorithms OpenZFS supports for dataset-level encryption.
The numbers are this package's own compact numbering, NOT the on-disk value: OpenZFS stores a zio_crypt_type_t (include/sys/fs/zfs.h), where 1 is "on" and 2 is "off", so aes-128-ccm is 3 and aes-256-gcm is 8. Convert with SuiteFromZioCrypt and ZioCrypt; both are checked against pools written by OpenZFS (see testdata/openzfs.json).
func SuiteFromZioCrypt ¶
SuiteFromZioCrypt maps an on-disk zio_crypt_type_t to a Suite. ok is false for inherit, on, off (which name no algorithm by themselves) and anything out of range.
func (Suite) KeyLen ¶
KeyLen returns the length, in bytes, of the data-encryption key for this suite.
func (Suite) WrappedKeyLen ¶
WrappedKeyLen is the length of the wrapped (MEK || HMAC key) ciphertext for the suite: KeyLen bytes of master key plus HMACKeyLen of HMAC key (96 for the 256-bit suites, 88 for 192, 80 for 128), or 0 for an unsupported suite.
