zfscrypt

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: BSD-3-Clause Imports: 12 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-encryptions/zfscrypt

go-encryptions/zfscrypt

ci

Pure-Go cryptographic primitives for OpenZFS native (dataset-level) encryption — the key hierarchy and block decryption needed to read an encrypted ZFS dataset without libzfs or cgo. Part of the go-encryptions family; AES-CCM is provided by go-encryptions/ccm and AES-GCM by the standard library.

This is the crypto layer only (KDF → wrapping key → MEK unwrap → per-block key → block decrypt). It does not parse the ZFS on-disk format; pair it with a DMU/DSL reader that supplies the salts, IVs, MACs and ciphertext.

Install

go get github.com/go-encryptions/zfscrypt

Suites

Suite is this package's own compact numbering, not the on-disk value: OpenZFS stores a zio_crypt_type_t, where 1 is on and 2 is off. Convert with SuiteFromZioCrypt / Suite.ZioCrypt.

Suite value on disk (DSL_CRYPTO_SUITE) key length
AES128CCM / AES128GCM 1 / 4 3 / 6 16 bytes
AES192CCM / AES192GCM 2 / 5 4 / 7 24 bytes
AES256CCM / AES256GCM 3 / 6 5 / 8 32 bytes

The master encryption key is KeyLen bytes long (16, 24 or 32), as OpenZFS keeps it; the wrapping key is always 32 bytes.

Key hierarchy

// 1. passphrase → wrapping key (PBKDF2-HMAC-SHA1, per the OpenZFS user-key format)
wk, _ := zfscrypt.DeriveWrappingKey(passphrase, salt, iters)

// 2. unwrap the master encryption key (MEK) + HMAC key from the DSL_CRYPTO_KEY.
//    JoinWrappedKey builds the blob from the DSL_CRYPTO_MASTER_KEY_1 (32 bytes,
//    only the first KeyLen bytes count for the 128/192-bit suites) and DSL_CRYPTO_HMAC_KEY_1 arrays;
//    ad = LE64(guid) || LE64(DSL_CRYPTO_SUITE) || LE64(version).
wrapped, _ := zfscrypt.JoinWrappedKey(suite, masterKeyArray, hmacKeyArray)
mek, hmacKey, err := zfscrypt.Unwrap(suite, wk, iv, mac, wrapped, ad)

// 3. derive the per-block key, then decrypt one block
blockKey, _ := zfscrypt.DeriveBlockKey(suite, mek, blockSalt)
plain, err := zfscrypt.DecryptBlock(suite, blockKey, blockIV, blockMAC, ciphertext, ad)

HMAC(hmacKey, data) exposes the keyed digest used for object-level integrity.

Verified against OpenZFS

testdata/openzfs.json was written by OpenZFS itself, not by this package: tools/zfsjudge (a separate module) runs as root on a Linux host, creates a file-backed pool, one passphrase-encrypted dataset for each of the six suites (zfs create -o encryption=<suite> -o keyformat=passphrase, the passphrase read from a file), writes a known two-block file into each, exports the pool, and records with zdb the wrapped keys, IV, MAC, GUID, version, PBKDF2 salt and iterations, and each data block's salt, IV and MAC, plus the raw ciphertext from the image. The tests derive the key, unwrap the master key and decrypt every block and demand the plaintext that was written, for all six suites, and check that corrupting the ciphertext, MAC, IV, AAD, wrapping AD, passphrase or suite is refused.

ZFSCRYPT_FIXTURES=<file> points the same tests at a freshly generated file (the live run).

Limitations

  • Decrypt only. There is no wrap or encrypt path.
  • Tested against one OpenZFS build: user tools zfs-2.2.2 (Ubuntu 24.04, zfs-2.2.2-0ubuntu9.5) with kernel module 2.3.4 (zfs-kmod-2.3.4-1ubuntu2, Linux 6.17, arm64), key version 1, passphrase keys (keyformat=passphrase, pbkdf2iters=100000), compression=off, 4 KiB records. Raw-key (keyformat=raw/hex) datasets use no PBKDF2 and were not exercised.
  • Only plain file data blocks were judged. DecryptBlock takes the additional data you give it; for data blocks that is empty. The AAD OpenZFS builds for dnode and objset blocks is the caller's job, and no such block was verified.
  • HMAC is not verified by OpenZFS. The unwrapped HMAC key is checked only in that it authenticates as part of the wrapped blob; the keyed digest over objset data was not compared with OpenZFS output.
  • Key version 0 datasets (AD = LE64(guid) only) were not generated; the caller builds the AD.
  • No external audit. The cryptography is the standard library's (AES, GCM, PBKDF2, HKDF, HMAC) plus go-encryptions/ccm; this package only assembles OpenZFS's key hierarchy, and nobody outside the project has reviewed that assembly.
  • PBKDF2-HMAC-SHA1 is mandated by the format (see below).

Security notes

  • SHA-1 in DeriveWrappingKey is mandated by the OpenZFS 0.8 user-key format (PBKDF2-HMAC-SHA1) — it is not a strength choice and must not be changed, or existing datasets become undecryptable. Documented inline.
  • The iteration count is untrusted. pbkdf2iters comes from the dataset header and OpenZFS enforces a minimum (100000) but no maximum, so a hostile header could ask for 2^62 iterations. DeriveWrappingKey charges the work to a kdfbudget default budget before hashing and returns ErrLimit (errors.Is) when it does not fit; every count up to 2^31 is accepted. DeriveWrappingKeyBudget(passphrase, salt, iters, b) takes your own *kdfbudget.Budget (kdfbudget.Unlimited() for a header you trust); a nil budget refuses.
  • All AEAD operations go through authenticated Open(); key material is sliced out only after the tag verifies.
  • Length parameters are validated before use; no unsafe, no cgo.

License

BSD-3-Clause © the go-encryptions/zfscrypt authors.

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

View Source
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

View Source
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

func DecryptBlock(suite Suite, key, iv, mac, ciphertext, ad []byte) ([]byte, error)

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

func DeriveBlockKey(suite Suite, mek, salt []byte) ([]byte, error)

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

func DeriveWrappingKey(passphrase, salt []byte, iters int) ([]byte, error)

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

func HMAC(hmacKey, data []byte) []byte

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

func JoinWrappedKey(suite Suite, masterKey, hmacKey []byte) ([]byte, error)

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

func Unwrap(suite Suite, wrappingKey, iv, mac, wrapped, ad []byte) (mek, hmacKey []byte, err error)

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).

const (
	SuiteInherit Suite = 0 // dataset inherits from parent (not used at the crypto layer)
	AES128CCM    Suite = 1
	AES192CCM    Suite = 2
	AES256CCM    Suite = 3
	AES128GCM    Suite = 4
	AES192GCM    Suite = 5
	AES256GCM    Suite = 6
)

func SuiteFromZioCrypt

func SuiteFromZioCrypt(v uint64) (s Suite, ok bool)

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) IsCCM

func (s Suite) IsCCM() bool

IsCCM reports whether the suite is CCM-mode (vs GCM-mode).

func (Suite) KeyLen

func (s Suite) KeyLen() int

KeyLen returns the length, in bytes, of the data-encryption key for this suite.

func (Suite) String

func (s Suite) String() string

String returns a stable label for logging.

func (Suite) WrappedKeyLen

func (s Suite) WrappedKeyLen() int

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.

func (Suite) ZioCrypt

func (s Suite) ZioCrypt() uint64

ZioCrypt returns the on-disk zio_crypt_type_t (the DSL_CRYPTO_SUITE value) for the suite, or 0 (ZIO_CRYPT_INHERIT) for a value that is not one of the six supported suites.

Directories

Path Synopsis
tools
zfsjudge module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址