xref: /freebsd-src/sys/contrib/openzfs/module/os/linux/zfs/zio_crypt.c (revision 17aab35a77a1b1bf02fc85bb8ffadccb0ca5006d)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * This file and its contents are supplied under the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License ("CDDL"), version 1.0.
6eda14cbcSMatt Macy  * You may only use this file in accordance with the terms of version
7eda14cbcSMatt Macy  * 1.0 of the CDDL.
8eda14cbcSMatt Macy  *
9eda14cbcSMatt Macy  * A full copy of the text of the CDDL should have accompanied this
10eda14cbcSMatt Macy  * source.  A copy of the CDDL is also available via the Internet at
11eda14cbcSMatt Macy  * http://www.illumos.org/license/CDDL.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * CDDL HEADER END
14eda14cbcSMatt Macy  */
15eda14cbcSMatt Macy 
16eda14cbcSMatt Macy /*
17eda14cbcSMatt Macy  * Copyright (c) 2017, Datto, Inc. All rights reserved.
18eda14cbcSMatt Macy  */
19eda14cbcSMatt Macy 
20eda14cbcSMatt Macy #include <sys/zio_crypt.h>
21eda14cbcSMatt Macy #include <sys/dmu.h>
22eda14cbcSMatt Macy #include <sys/dmu_objset.h>
23eda14cbcSMatt Macy #include <sys/dnode.h>
24eda14cbcSMatt Macy #include <sys/fs/zfs.h>
25eda14cbcSMatt Macy #include <sys/zio.h>
26eda14cbcSMatt Macy #include <sys/zil.h>
27eda14cbcSMatt Macy #include <sys/sha2.h>
28eda14cbcSMatt Macy #include <sys/hkdf.h>
29eda14cbcSMatt Macy #include <sys/qat.h>
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy /*
32eda14cbcSMatt Macy  * This file is responsible for handling all of the details of generating
33eda14cbcSMatt Macy  * encryption parameters and performing encryption and authentication.
34eda14cbcSMatt Macy  *
35eda14cbcSMatt Macy  * BLOCK ENCRYPTION PARAMETERS:
36eda14cbcSMatt Macy  * Encryption /Authentication Algorithm Suite (crypt):
37eda14cbcSMatt Macy  * The encryption algorithm, mode, and key length we are going to use. We
38eda14cbcSMatt Macy  * currently support AES in either GCM or CCM modes with 128, 192, and 256 bit
39eda14cbcSMatt Macy  * keys. All authentication is currently done with SHA512-HMAC.
40eda14cbcSMatt Macy  *
41eda14cbcSMatt Macy  * Plaintext:
42eda14cbcSMatt Macy  * The unencrypted data that we want to encrypt.
43eda14cbcSMatt Macy  *
44eda14cbcSMatt Macy  * Initialization Vector (IV):
45eda14cbcSMatt Macy  * An initialization vector for the encryption algorithms. This is used to
46eda14cbcSMatt Macy  * "tweak" the encryption algorithms so that two blocks of the same data are
47eda14cbcSMatt Macy  * encrypted into different ciphertext outputs, thus obfuscating block patterns.
48eda14cbcSMatt Macy  * The supported encryption modes (AES-GCM and AES-CCM) require that an IV is
49eda14cbcSMatt Macy  * never reused with the same encryption key. This value is stored unencrypted
50eda14cbcSMatt Macy  * and must simply be provided to the decryption function. We use a 96 bit IV
51eda14cbcSMatt Macy  * (as recommended by NIST) for all block encryption. For non-dedup blocks we
52eda14cbcSMatt Macy  * derive the IV randomly. The first 64 bits of the IV are stored in the second
53eda14cbcSMatt Macy  * word of DVA[2] and the remaining 32 bits are stored in the upper 32 bits of
54eda14cbcSMatt Macy  * blk_fill. This is safe because encrypted blocks can't use the upper 32 bits
55eda14cbcSMatt Macy  * of blk_fill. We only encrypt level 0 blocks, which normally have a fill count
56eda14cbcSMatt Macy  * of 1. The only exception is for DMU_OT_DNODE objects, where the fill count of
57eda14cbcSMatt Macy  * level 0 blocks is the number of allocated dnodes in that block. The on-disk
58eda14cbcSMatt Macy  * format supports at most 2^15 slots per L0 dnode block, because the maximum
59eda14cbcSMatt Macy  * block size is 16MB (2^24). In either case, for level 0 blocks this number
60eda14cbcSMatt Macy  * will still be smaller than UINT32_MAX so it is safe to store the IV in the
61eda14cbcSMatt Macy  * top 32 bits of blk_fill, while leaving the bottom 32 bits of the fill count
62eda14cbcSMatt Macy  * for the dnode code.
63eda14cbcSMatt Macy  *
64eda14cbcSMatt Macy  * Master key:
65eda14cbcSMatt Macy  * This is the most important secret data of an encrypted dataset. It is used
66eda14cbcSMatt Macy  * along with the salt to generate that actual encryption keys via HKDF. We
67eda14cbcSMatt Macy  * do not use the master key to directly encrypt any data because there are
68eda14cbcSMatt Macy  * theoretical limits on how much data can actually be safely encrypted with
69eda14cbcSMatt Macy  * any encryption mode. The master key is stored encrypted on disk with the
70eda14cbcSMatt Macy  * user's wrapping key. Its length is determined by the encryption algorithm.
71eda14cbcSMatt Macy  * For details on how this is stored see the block comment in dsl_crypt.c
72eda14cbcSMatt Macy  *
73eda14cbcSMatt Macy  * Salt:
74eda14cbcSMatt Macy  * Used as an input to the HKDF function, along with the master key. We use a
75eda14cbcSMatt Macy  * 64 bit salt, stored unencrypted in the first word of DVA[2]. Any given salt
76eda14cbcSMatt Macy  * can be used for encrypting many blocks, so we cache the current salt and the
77eda14cbcSMatt Macy  * associated derived key in zio_crypt_t so we do not need to derive it again
78eda14cbcSMatt Macy  * needlessly.
79eda14cbcSMatt Macy  *
80eda14cbcSMatt Macy  * Encryption Key:
81eda14cbcSMatt Macy  * A secret binary key, generated from an HKDF function used to encrypt and
82eda14cbcSMatt Macy  * decrypt data.
83eda14cbcSMatt Macy  *
84eda14cbcSMatt Macy  * Message Authentication Code (MAC)
85eda14cbcSMatt Macy  * The MAC is an output of authenticated encryption modes such as AES-GCM and
86eda14cbcSMatt Macy  * AES-CCM. Its purpose is to ensure that an attacker cannot modify encrypted
87eda14cbcSMatt Macy  * data on disk and return garbage to the application. Effectively, it is a
88eda14cbcSMatt Macy  * checksum that can not be reproduced by an attacker. We store the MAC in the
89eda14cbcSMatt Macy  * second 128 bits of blk_cksum, leaving the first 128 bits for a truncated
90eda14cbcSMatt Macy  * regular checksum of the ciphertext which can be used for scrubbing.
91eda14cbcSMatt Macy  *
92eda14cbcSMatt Macy  * OBJECT AUTHENTICATION:
93eda14cbcSMatt Macy  * Some object types, such as DMU_OT_MASTER_NODE cannot be encrypted because
94eda14cbcSMatt Macy  * they contain some info that always needs to be readable. To prevent this
95eda14cbcSMatt Macy  * data from being altered, we authenticate this data using SHA512-HMAC. This
96eda14cbcSMatt Macy  * will produce a MAC (similar to the one produced via encryption) which can
97eda14cbcSMatt Macy  * be used to verify the object was not modified. HMACs do not require key
98eda14cbcSMatt Macy  * rotation or IVs, so we can keep up to the full 3 copies of authenticated
99eda14cbcSMatt Macy  * data.
100eda14cbcSMatt Macy  *
101eda14cbcSMatt Macy  * ZIL ENCRYPTION:
102eda14cbcSMatt Macy  * ZIL blocks have their bp written to disk ahead of the associated data, so we
103eda14cbcSMatt Macy  * cannot store the MAC there as we normally do. For these blocks the MAC is
104eda14cbcSMatt Macy  * stored in the embedded checksum within the zil_chain_t header. The salt and
105eda14cbcSMatt Macy  * IV are generated for the block on bp allocation instead of at encryption
106eda14cbcSMatt Macy  * time. In addition, ZIL blocks have some pieces that must be left in plaintext
107eda14cbcSMatt Macy  * for claiming even though all of the sensitive user data still needs to be
108eda14cbcSMatt Macy  * encrypted. The function zio_crypt_init_uios_zil() handles parsing which
109eda14cbcSMatt Macy  * pieces of the block need to be encrypted. All data that is not encrypted is
110eda14cbcSMatt Macy  * authenticated using the AAD mechanisms that the supported encryption modes
111eda14cbcSMatt Macy  * provide for. In order to preserve the semantics of the ZIL for encrypted
112eda14cbcSMatt Macy  * datasets, the ZIL is not protected at the objset level as described below.
113eda14cbcSMatt Macy  *
114eda14cbcSMatt Macy  * DNODE ENCRYPTION:
115eda14cbcSMatt Macy  * Similarly to ZIL blocks, the core part of each dnode_phys_t needs to be left
116eda14cbcSMatt Macy  * in plaintext for scrubbing and claiming, but the bonus buffers might contain
117eda14cbcSMatt Macy  * sensitive user data. The function zio_crypt_init_uios_dnode() handles parsing
11816038816SMartin Matuska  * which pieces of the block need to be encrypted. For more details about
119eda14cbcSMatt Macy  * dnode authentication and encryption, see zio_crypt_init_uios_dnode().
120eda14cbcSMatt Macy  *
121eda14cbcSMatt Macy  * OBJECT SET AUTHENTICATION:
122eda14cbcSMatt Macy  * Up to this point, everything we have encrypted and authenticated has been
123eda14cbcSMatt Macy  * at level 0 (or -2 for the ZIL). If we did not do any further work the
124eda14cbcSMatt Macy  * on-disk format would be susceptible to attacks that deleted or rearranged
125eda14cbcSMatt Macy  * the order of level 0 blocks. Ideally, the cleanest solution would be to
126eda14cbcSMatt Macy  * maintain a tree of authentication MACs going up the bp tree. However, this
127eda14cbcSMatt Macy  * presents a problem for raw sends. Send files do not send information about
128eda14cbcSMatt Macy  * indirect blocks so there would be no convenient way to transfer the MACs and
129eda14cbcSMatt Macy  * they cannot be recalculated on the receive side without the master key which
130eda14cbcSMatt Macy  * would defeat one of the purposes of raw sends in the first place. Instead,
131eda14cbcSMatt Macy  * for the indirect levels of the bp tree, we use a regular SHA512 of the MACs
132eda14cbcSMatt Macy  * from the level below. We also include some portable fields from blk_prop such
133eda14cbcSMatt Macy  * as the lsize and compression algorithm to prevent the data from being
134eda14cbcSMatt Macy  * misinterpreted.
135eda14cbcSMatt Macy  *
136eda14cbcSMatt Macy  * At the objset level, we maintain 2 separate 256 bit MACs in the
137eda14cbcSMatt Macy  * objset_phys_t. The first one is "portable" and is the logical root of the
138eda14cbcSMatt Macy  * MAC tree maintained in the metadnode's bps. The second, is "local" and is
139eda14cbcSMatt Macy  * used as the root MAC for the user accounting objects, which are also not
140eda14cbcSMatt Macy  * transferred via "zfs send". The portable MAC is sent in the DRR_BEGIN payload
141eda14cbcSMatt Macy  * of the send file. The useraccounting code ensures that the useraccounting
142eda14cbcSMatt Macy  * info is not present upon a receive, so the local MAC can simply be cleared
143eda14cbcSMatt Macy  * out at that time. For more info about objset_phys_t authentication, see
144eda14cbcSMatt Macy  * zio_crypt_do_objset_hmacs().
145eda14cbcSMatt Macy  *
146eda14cbcSMatt Macy  * CONSIDERATIONS FOR DEDUP:
147eda14cbcSMatt Macy  * In order for dedup to work, blocks that we want to dedup with one another
148eda14cbcSMatt Macy  * need to use the same IV and encryption key, so that they will have the same
149eda14cbcSMatt Macy  * ciphertext. Normally, one should never reuse an IV with the same encryption
150eda14cbcSMatt Macy  * key or else AES-GCM and AES-CCM can both actually leak the plaintext of both
151eda14cbcSMatt Macy  * blocks. In this case, however, since we are using the same plaintext as
152eda14cbcSMatt Macy  * well all that we end up with is a duplicate of the original ciphertext we
153eda14cbcSMatt Macy  * already had. As a result, an attacker with read access to the raw disk will
154eda14cbcSMatt Macy  * be able to tell which blocks are the same but this information is given away
155eda14cbcSMatt Macy  * by dedup anyway. In order to get the same IVs and encryption keys for
156eda14cbcSMatt Macy  * equivalent blocks of data we use an HMAC of the plaintext. We use an HMAC
157eda14cbcSMatt Macy  * here so that a reproducible checksum of the plaintext is never available to
158eda14cbcSMatt Macy  * the attacker. The HMAC key is kept alongside the master key, encrypted on
159eda14cbcSMatt Macy  * disk. The first 64 bits of the HMAC are used in place of the random salt, and
160eda14cbcSMatt Macy  * the next 96 bits are used as the IV. As a result of this mechanism, dedup
161eda14cbcSMatt Macy  * will only work within a clone family since encrypted dedup requires use of
162eda14cbcSMatt Macy  * the same master and HMAC keys.
163eda14cbcSMatt Macy  */
164eda14cbcSMatt Macy 
165eda14cbcSMatt Macy /*
166eda14cbcSMatt Macy  * After encrypting many blocks with the same key we may start to run up
167eda14cbcSMatt Macy  * against the theoretical limits of how much data can securely be encrypted
168eda14cbcSMatt Macy  * with a single key using the supported encryption modes. The most obvious
169eda14cbcSMatt Macy  * limitation is that our risk of generating 2 equivalent 96 bit IVs increases
170eda14cbcSMatt Macy  * the more IVs we generate (which both GCM and CCM modes strictly forbid).
171eda14cbcSMatt Macy  * This risk actually grows surprisingly quickly over time according to the
172eda14cbcSMatt Macy  * Birthday Problem. With a total IV space of 2^(96 bits), and assuming we have
173eda14cbcSMatt Macy  * generated n IVs with a cryptographically secure RNG, the approximate
174eda14cbcSMatt Macy  * probability p(n) of a collision is given as:
175eda14cbcSMatt Macy  *
176eda14cbcSMatt Macy  * p(n) ~= e^(-n*(n-1)/(2*(2^96)))
177eda14cbcSMatt Macy  *
178eda14cbcSMatt Macy  * [http://www.math.cornell.edu/~mec/2008-2009/TianyiZheng/Birthday.html]
179eda14cbcSMatt Macy  *
180eda14cbcSMatt Macy  * Assuming that we want to ensure that p(n) never goes over 1 / 1 trillion
181eda14cbcSMatt Macy  * we must not write more than 398,065,730 blocks with the same encryption key.
182eda14cbcSMatt Macy  * Therefore, we rotate our keys after 400,000,000 blocks have been written by
183eda14cbcSMatt Macy  * generating a new random 64 bit salt for our HKDF encryption key generation
184eda14cbcSMatt Macy  * function.
185eda14cbcSMatt Macy  */
186eda14cbcSMatt Macy #define	ZFS_KEY_MAX_SALT_USES_DEFAULT	400000000
187eda14cbcSMatt Macy #define	ZFS_CURRENT_MAX_SALT_USES	\
188eda14cbcSMatt Macy 	(MIN(zfs_key_max_salt_uses, ZFS_KEY_MAX_SALT_USES_DEFAULT))
189e92ffd9bSMartin Matuska static unsigned long zfs_key_max_salt_uses = ZFS_KEY_MAX_SALT_USES_DEFAULT;
190eda14cbcSMatt Macy 
191eda14cbcSMatt Macy typedef struct blkptr_auth_buf {
192eda14cbcSMatt Macy 	uint64_t bab_prop;			/* blk_prop - portable mask */
193eda14cbcSMatt Macy 	uint8_t bab_mac[ZIO_DATA_MAC_LEN];	/* MAC from blk_cksum */
194eda14cbcSMatt Macy 	uint64_t bab_pad;			/* reserved for future use */
195eda14cbcSMatt Macy } blkptr_auth_buf_t;
196eda14cbcSMatt Macy 
197e92ffd9bSMartin Matuska const zio_crypt_info_t zio_crypt_table[ZIO_CRYPT_FUNCTIONS] = {
198eda14cbcSMatt Macy 	{"",			ZC_TYPE_NONE,	0,	"inherit"},
199eda14cbcSMatt Macy 	{"",			ZC_TYPE_NONE,	0,	"on"},
200eda14cbcSMatt Macy 	{"",			ZC_TYPE_NONE,	0,	"off"},
201eda14cbcSMatt Macy 	{SUN_CKM_AES_CCM,	ZC_TYPE_CCM,	16,	"aes-128-ccm"},
202eda14cbcSMatt Macy 	{SUN_CKM_AES_CCM,	ZC_TYPE_CCM,	24,	"aes-192-ccm"},
203eda14cbcSMatt Macy 	{SUN_CKM_AES_CCM,	ZC_TYPE_CCM,	32,	"aes-256-ccm"},
204eda14cbcSMatt Macy 	{SUN_CKM_AES_GCM,	ZC_TYPE_GCM,	16,	"aes-128-gcm"},
205eda14cbcSMatt Macy 	{SUN_CKM_AES_GCM,	ZC_TYPE_GCM,	24,	"aes-192-gcm"},
206eda14cbcSMatt Macy 	{SUN_CKM_AES_GCM,	ZC_TYPE_GCM,	32,	"aes-256-gcm"}
207eda14cbcSMatt Macy };
208eda14cbcSMatt Macy 
209eda14cbcSMatt Macy void
210eda14cbcSMatt Macy zio_crypt_key_destroy(zio_crypt_key_t *key)
211eda14cbcSMatt Macy {
212eda14cbcSMatt Macy 	rw_destroy(&key->zk_salt_lock);
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy 	/* free crypto templates */
215eda14cbcSMatt Macy 	crypto_destroy_ctx_template(key->zk_current_tmpl);
216eda14cbcSMatt Macy 	crypto_destroy_ctx_template(key->zk_hmac_tmpl);
217eda14cbcSMatt Macy 
218eda14cbcSMatt Macy 	/* zero out sensitive data */
219da5137abSMartin Matuska 	memset(key, 0, sizeof (zio_crypt_key_t));
220eda14cbcSMatt Macy }
221eda14cbcSMatt Macy 
222eda14cbcSMatt Macy int
223eda14cbcSMatt Macy zio_crypt_key_init(uint64_t crypt, zio_crypt_key_t *key)
224eda14cbcSMatt Macy {
225eda14cbcSMatt Macy 	int ret;
2262a58b312SMartin Matuska 	crypto_mechanism_t mech = {0};
227eda14cbcSMatt Macy 	uint_t keydata_len;
228eda14cbcSMatt Macy 
229eda14cbcSMatt Macy 	ASSERT(key != NULL);
230eda14cbcSMatt Macy 	ASSERT3U(crypt, <, ZIO_CRYPT_FUNCTIONS);
231eda14cbcSMatt Macy 
232bb2d13b6SMartin Matuska /*
233bb2d13b6SMartin Matuska  * Workaround for GCC 12+ with UBSan enabled deficencies.
234bb2d13b6SMartin Matuska  *
235bb2d13b6SMartin Matuska  * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
236bb2d13b6SMartin Matuska  * below as violating -Warray-bounds
237bb2d13b6SMartin Matuska  */
238bb2d13b6SMartin Matuska #if defined(__GNUC__) && !defined(__clang__) && \
239bb2d13b6SMartin Matuska 	((!defined(_KERNEL) && defined(ZFS_UBSAN_ENABLED)) || \
240bb2d13b6SMartin Matuska 	    defined(CONFIG_UBSAN))
241bb2d13b6SMartin Matuska #pragma GCC diagnostic push
242bb2d13b6SMartin Matuska #pragma GCC diagnostic ignored "-Warray-bounds"
243bb2d13b6SMartin Matuska #endif
244eda14cbcSMatt Macy 	keydata_len = zio_crypt_table[crypt].ci_keylen;
245bb2d13b6SMartin Matuska #if defined(__GNUC__) && !defined(__clang__) && \
246bb2d13b6SMartin Matuska 	((!defined(_KERNEL) && defined(ZFS_UBSAN_ENABLED)) || \
247bb2d13b6SMartin Matuska 	    defined(CONFIG_UBSAN))
248bb2d13b6SMartin Matuska #pragma GCC diagnostic pop
249bb2d13b6SMartin Matuska #endif
250da5137abSMartin Matuska 	memset(key, 0, sizeof (zio_crypt_key_t));
251dbd5678dSMartin Matuska 	rw_init(&key->zk_salt_lock, NULL, RW_DEFAULT, NULL);
252eda14cbcSMatt Macy 
253eda14cbcSMatt Macy 	/* fill keydata buffers and salt with random data */
254eda14cbcSMatt Macy 	ret = random_get_bytes((uint8_t *)&key->zk_guid, sizeof (uint64_t));
255eda14cbcSMatt Macy 	if (ret != 0)
256eda14cbcSMatt Macy 		goto error;
257eda14cbcSMatt Macy 
258eda14cbcSMatt Macy 	ret = random_get_bytes(key->zk_master_keydata, keydata_len);
259eda14cbcSMatt Macy 	if (ret != 0)
260eda14cbcSMatt Macy 		goto error;
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy 	ret = random_get_bytes(key->zk_hmac_keydata, SHA512_HMAC_KEYLEN);
263eda14cbcSMatt Macy 	if (ret != 0)
264eda14cbcSMatt Macy 		goto error;
265eda14cbcSMatt Macy 
266eda14cbcSMatt Macy 	ret = random_get_bytes(key->zk_salt, ZIO_DATA_SALT_LEN);
267eda14cbcSMatt Macy 	if (ret != 0)
268eda14cbcSMatt Macy 		goto error;
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy 	/* derive the current key from the master key */
271eda14cbcSMatt Macy 	ret = hkdf_sha512(key->zk_master_keydata, keydata_len, NULL, 0,
272eda14cbcSMatt Macy 	    key->zk_salt, ZIO_DATA_SALT_LEN, key->zk_current_keydata,
273eda14cbcSMatt Macy 	    keydata_len);
274eda14cbcSMatt Macy 	if (ret != 0)
275eda14cbcSMatt Macy 		goto error;
276eda14cbcSMatt Macy 
277eda14cbcSMatt Macy 	/* initialize keys for the ICP */
278eda14cbcSMatt Macy 	key->zk_current_key.ck_data = key->zk_current_keydata;
279eda14cbcSMatt Macy 	key->zk_current_key.ck_length = CRYPTO_BYTES2BITS(keydata_len);
280eda14cbcSMatt Macy 
281eda14cbcSMatt Macy 	key->zk_hmac_key.ck_data = &key->zk_hmac_key;
282eda14cbcSMatt Macy 	key->zk_hmac_key.ck_length = CRYPTO_BYTES2BITS(SHA512_HMAC_KEYLEN);
283eda14cbcSMatt Macy 
284eda14cbcSMatt Macy 	/*
285eda14cbcSMatt Macy 	 * Initialize the crypto templates. It's ok if this fails because
286eda14cbcSMatt Macy 	 * this is just an optimization.
287eda14cbcSMatt Macy 	 */
288eda14cbcSMatt Macy 	mech.cm_type = crypto_mech2id(zio_crypt_table[crypt].ci_mechname);
289eda14cbcSMatt Macy 	ret = crypto_create_ctx_template(&mech, &key->zk_current_key,
290c03c5b1cSMartin Matuska 	    &key->zk_current_tmpl);
291eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS)
292eda14cbcSMatt Macy 		key->zk_current_tmpl = NULL;
293eda14cbcSMatt Macy 
294eda14cbcSMatt Macy 	mech.cm_type = crypto_mech2id(SUN_CKM_SHA512_HMAC);
295eda14cbcSMatt Macy 	ret = crypto_create_ctx_template(&mech, &key->zk_hmac_key,
296c03c5b1cSMartin Matuska 	    &key->zk_hmac_tmpl);
297eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS)
298eda14cbcSMatt Macy 		key->zk_hmac_tmpl = NULL;
299eda14cbcSMatt Macy 
300eda14cbcSMatt Macy 	key->zk_crypt = crypt;
301eda14cbcSMatt Macy 	key->zk_version = ZIO_CRYPT_KEY_CURRENT_VERSION;
302eda14cbcSMatt Macy 	key->zk_salt_count = 0;
303eda14cbcSMatt Macy 
304eda14cbcSMatt Macy 	return (0);
305eda14cbcSMatt Macy 
306eda14cbcSMatt Macy error:
307eda14cbcSMatt Macy 	zio_crypt_key_destroy(key);
308eda14cbcSMatt Macy 	return (ret);
309eda14cbcSMatt Macy }
310eda14cbcSMatt Macy 
311eda14cbcSMatt Macy static int
312eda14cbcSMatt Macy zio_crypt_key_change_salt(zio_crypt_key_t *key)
313eda14cbcSMatt Macy {
314eda14cbcSMatt Macy 	int ret = 0;
315eda14cbcSMatt Macy 	uint8_t salt[ZIO_DATA_SALT_LEN];
316eda14cbcSMatt Macy 	crypto_mechanism_t mech;
317eda14cbcSMatt Macy 	uint_t keydata_len = zio_crypt_table[key->zk_crypt].ci_keylen;
318eda14cbcSMatt Macy 
319eda14cbcSMatt Macy 	/* generate a new salt */
320eda14cbcSMatt Macy 	ret = random_get_bytes(salt, ZIO_DATA_SALT_LEN);
321eda14cbcSMatt Macy 	if (ret != 0)
322eda14cbcSMatt Macy 		goto error;
323eda14cbcSMatt Macy 
324eda14cbcSMatt Macy 	rw_enter(&key->zk_salt_lock, RW_WRITER);
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 	/* someone beat us to the salt rotation, just unlock and return */
327eda14cbcSMatt Macy 	if (key->zk_salt_count < ZFS_CURRENT_MAX_SALT_USES)
328eda14cbcSMatt Macy 		goto out_unlock;
329eda14cbcSMatt Macy 
330eda14cbcSMatt Macy 	/* derive the current key from the master key and the new salt */
331eda14cbcSMatt Macy 	ret = hkdf_sha512(key->zk_master_keydata, keydata_len, NULL, 0,
332eda14cbcSMatt Macy 	    salt, ZIO_DATA_SALT_LEN, key->zk_current_keydata, keydata_len);
333eda14cbcSMatt Macy 	if (ret != 0)
334eda14cbcSMatt Macy 		goto out_unlock;
335eda14cbcSMatt Macy 
336eda14cbcSMatt Macy 	/* assign the salt and reset the usage count */
337da5137abSMartin Matuska 	memcpy(key->zk_salt, salt, ZIO_DATA_SALT_LEN);
338eda14cbcSMatt Macy 	key->zk_salt_count = 0;
339eda14cbcSMatt Macy 
340eda14cbcSMatt Macy 	/* destroy the old context template and create the new one */
341eda14cbcSMatt Macy 	crypto_destroy_ctx_template(key->zk_current_tmpl);
342eda14cbcSMatt Macy 	ret = crypto_create_ctx_template(&mech, &key->zk_current_key,
343c03c5b1cSMartin Matuska 	    &key->zk_current_tmpl);
344eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS)
345eda14cbcSMatt Macy 		key->zk_current_tmpl = NULL;
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	rw_exit(&key->zk_salt_lock);
348eda14cbcSMatt Macy 
349eda14cbcSMatt Macy 	return (0);
350eda14cbcSMatt Macy 
351eda14cbcSMatt Macy out_unlock:
352eda14cbcSMatt Macy 	rw_exit(&key->zk_salt_lock);
353eda14cbcSMatt Macy error:
354eda14cbcSMatt Macy 	return (ret);
355eda14cbcSMatt Macy }
356eda14cbcSMatt Macy 
357eda14cbcSMatt Macy /* See comment above zfs_key_max_salt_uses definition for details */
358eda14cbcSMatt Macy int
359eda14cbcSMatt Macy zio_crypt_key_get_salt(zio_crypt_key_t *key, uint8_t *salt)
360eda14cbcSMatt Macy {
361eda14cbcSMatt Macy 	int ret;
362eda14cbcSMatt Macy 	boolean_t salt_change;
363eda14cbcSMatt Macy 
364eda14cbcSMatt Macy 	rw_enter(&key->zk_salt_lock, RW_READER);
365eda14cbcSMatt Macy 
366da5137abSMartin Matuska 	memcpy(salt, key->zk_salt, ZIO_DATA_SALT_LEN);
367eda14cbcSMatt Macy 	salt_change = (atomic_inc_64_nv(&key->zk_salt_count) >=
368eda14cbcSMatt Macy 	    ZFS_CURRENT_MAX_SALT_USES);
369eda14cbcSMatt Macy 
370eda14cbcSMatt Macy 	rw_exit(&key->zk_salt_lock);
371eda14cbcSMatt Macy 
372eda14cbcSMatt Macy 	if (salt_change) {
373eda14cbcSMatt Macy 		ret = zio_crypt_key_change_salt(key);
374eda14cbcSMatt Macy 		if (ret != 0)
375eda14cbcSMatt Macy 			goto error;
376eda14cbcSMatt Macy 	}
377eda14cbcSMatt Macy 
378eda14cbcSMatt Macy 	return (0);
379eda14cbcSMatt Macy 
380eda14cbcSMatt Macy error:
381eda14cbcSMatt Macy 	return (ret);
382eda14cbcSMatt Macy }
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy /*
385eda14cbcSMatt Macy  * This function handles all encryption and decryption in zfs. When
386eda14cbcSMatt Macy  * encrypting it expects puio to reference the plaintext and cuio to
387eda14cbcSMatt Macy  * reference the ciphertext. cuio must have enough space for the
388eda14cbcSMatt Macy  * ciphertext + room for a MAC. datalen should be the length of the
389eda14cbcSMatt Macy  * plaintext / ciphertext alone.
390eda14cbcSMatt Macy  */
391eda14cbcSMatt Macy static int
392eda14cbcSMatt Macy zio_do_crypt_uio(boolean_t encrypt, uint64_t crypt, crypto_key_t *key,
393eda14cbcSMatt Macy     crypto_ctx_template_t tmpl, uint8_t *ivbuf, uint_t datalen,
394184c1b94SMartin Matuska     zfs_uio_t *puio, zfs_uio_t *cuio, uint8_t *authbuf, uint_t auth_len)
395eda14cbcSMatt Macy {
396eda14cbcSMatt Macy 	int ret;
397eda14cbcSMatt Macy 	crypto_data_t plaindata, cipherdata;
398eda14cbcSMatt Macy 	CK_AES_CCM_PARAMS ccmp;
399eda14cbcSMatt Macy 	CK_AES_GCM_PARAMS gcmp;
400eda14cbcSMatt Macy 	crypto_mechanism_t mech;
401eda14cbcSMatt Macy 	zio_crypt_info_t crypt_info;
402eda14cbcSMatt Macy 	uint_t plain_full_len, maclen;
403eda14cbcSMatt Macy 
404eda14cbcSMatt Macy 	ASSERT3U(crypt, <, ZIO_CRYPT_FUNCTIONS);
405eda14cbcSMatt Macy 
406eda14cbcSMatt Macy 	/* lookup the encryption info */
407eda14cbcSMatt Macy 	crypt_info = zio_crypt_table[crypt];
408eda14cbcSMatt Macy 
409eda14cbcSMatt Macy 	/* the mac will always be the last iovec_t in the cipher uio */
410eda14cbcSMatt Macy 	maclen = cuio->uio_iov[cuio->uio_iovcnt - 1].iov_len;
411eda14cbcSMatt Macy 
412eda14cbcSMatt Macy 	ASSERT(maclen <= ZIO_DATA_MAC_LEN);
413eda14cbcSMatt Macy 
414eda14cbcSMatt Macy 	/* setup encryption mechanism (same as crypt) */
415eda14cbcSMatt Macy 	mech.cm_type = crypto_mech2id(crypt_info.ci_mechname);
416eda14cbcSMatt Macy 
417eda14cbcSMatt Macy 	/*
418eda14cbcSMatt Macy 	 * Strangely, the ICP requires that plain_full_len must include
419eda14cbcSMatt Macy 	 * the MAC length when decrypting, even though the UIO does not
420eda14cbcSMatt Macy 	 * need to have the extra space allocated.
421eda14cbcSMatt Macy 	 */
422eda14cbcSMatt Macy 	if (encrypt) {
423eda14cbcSMatt Macy 		plain_full_len = datalen;
424eda14cbcSMatt Macy 	} else {
425eda14cbcSMatt Macy 		plain_full_len = datalen + maclen;
426eda14cbcSMatt Macy 	}
427eda14cbcSMatt Macy 
428eda14cbcSMatt Macy 	/*
429eda14cbcSMatt Macy 	 * setup encryption params (currently only AES CCM and AES GCM
430eda14cbcSMatt Macy 	 * are supported)
431eda14cbcSMatt Macy 	 */
432eda14cbcSMatt Macy 	if (crypt_info.ci_crypt_type == ZC_TYPE_CCM) {
433eda14cbcSMatt Macy 		ccmp.ulNonceSize = ZIO_DATA_IV_LEN;
434eda14cbcSMatt Macy 		ccmp.ulAuthDataSize = auth_len;
435eda14cbcSMatt Macy 		ccmp.authData = authbuf;
436eda14cbcSMatt Macy 		ccmp.ulMACSize = maclen;
437eda14cbcSMatt Macy 		ccmp.nonce = ivbuf;
438eda14cbcSMatt Macy 		ccmp.ulDataSize = plain_full_len;
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy 		mech.cm_param = (char *)(&ccmp);
441eda14cbcSMatt Macy 		mech.cm_param_len = sizeof (CK_AES_CCM_PARAMS);
442eda14cbcSMatt Macy 	} else {
443eda14cbcSMatt Macy 		gcmp.ulIvLen = ZIO_DATA_IV_LEN;
444eda14cbcSMatt Macy 		gcmp.ulIvBits = CRYPTO_BYTES2BITS(ZIO_DATA_IV_LEN);
445eda14cbcSMatt Macy 		gcmp.ulAADLen = auth_len;
446eda14cbcSMatt Macy 		gcmp.pAAD = authbuf;
447eda14cbcSMatt Macy 		gcmp.ulTagBits = CRYPTO_BYTES2BITS(maclen);
448eda14cbcSMatt Macy 		gcmp.pIv = ivbuf;
449eda14cbcSMatt Macy 
450eda14cbcSMatt Macy 		mech.cm_param = (char *)(&gcmp);
451eda14cbcSMatt Macy 		mech.cm_param_len = sizeof (CK_AES_GCM_PARAMS);
452eda14cbcSMatt Macy 	}
453eda14cbcSMatt Macy 
454eda14cbcSMatt Macy 	/* populate the cipher and plain data structs. */
455eda14cbcSMatt Macy 	plaindata.cd_format = CRYPTO_DATA_UIO;
456eda14cbcSMatt Macy 	plaindata.cd_offset = 0;
457eda14cbcSMatt Macy 	plaindata.cd_uio = puio;
458eda14cbcSMatt Macy 	plaindata.cd_length = plain_full_len;
459eda14cbcSMatt Macy 
460eda14cbcSMatt Macy 	cipherdata.cd_format = CRYPTO_DATA_UIO;
461eda14cbcSMatt Macy 	cipherdata.cd_offset = 0;
462eda14cbcSMatt Macy 	cipherdata.cd_uio = cuio;
463eda14cbcSMatt Macy 	cipherdata.cd_length = datalen + maclen;
464eda14cbcSMatt Macy 
465eda14cbcSMatt Macy 	/* perform the actual encryption */
466eda14cbcSMatt Macy 	if (encrypt) {
467c03c5b1cSMartin Matuska 		ret = crypto_encrypt(&mech, &plaindata, key, tmpl, &cipherdata);
468eda14cbcSMatt Macy 		if (ret != CRYPTO_SUCCESS) {
469eda14cbcSMatt Macy 			ret = SET_ERROR(EIO);
470eda14cbcSMatt Macy 			goto error;
471eda14cbcSMatt Macy 		}
472eda14cbcSMatt Macy 	} else {
473c03c5b1cSMartin Matuska 		ret = crypto_decrypt(&mech, &cipherdata, key, tmpl, &plaindata);
474eda14cbcSMatt Macy 		if (ret != CRYPTO_SUCCESS) {
475eda14cbcSMatt Macy 			ASSERT3U(ret, ==, CRYPTO_INVALID_MAC);
476eda14cbcSMatt Macy 			ret = SET_ERROR(ECKSUM);
477eda14cbcSMatt Macy 			goto error;
478eda14cbcSMatt Macy 		}
479eda14cbcSMatt Macy 	}
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy 	return (0);
482eda14cbcSMatt Macy 
483eda14cbcSMatt Macy error:
484eda14cbcSMatt Macy 	return (ret);
485eda14cbcSMatt Macy }
486eda14cbcSMatt Macy 
487eda14cbcSMatt Macy int
488eda14cbcSMatt Macy zio_crypt_key_wrap(crypto_key_t *cwkey, zio_crypt_key_t *key, uint8_t *iv,
489eda14cbcSMatt Macy     uint8_t *mac, uint8_t *keydata_out, uint8_t *hmac_keydata_out)
490eda14cbcSMatt Macy {
491eda14cbcSMatt Macy 	int ret;
492184c1b94SMartin Matuska 	zfs_uio_t puio, cuio;
493eda14cbcSMatt Macy 	uint64_t aad[3];
494eda14cbcSMatt Macy 	iovec_t plain_iovecs[2], cipher_iovecs[3];
495eda14cbcSMatt Macy 	uint64_t crypt = key->zk_crypt;
496eda14cbcSMatt Macy 	uint_t enc_len, keydata_len, aad_len;
497eda14cbcSMatt Macy 
498eda14cbcSMatt Macy 	ASSERT3U(crypt, <, ZIO_CRYPT_FUNCTIONS);
499eda14cbcSMatt Macy 
500eda14cbcSMatt Macy 	keydata_len = zio_crypt_table[crypt].ci_keylen;
501eda14cbcSMatt Macy 
502eda14cbcSMatt Macy 	/* generate iv for wrapping the master and hmac key */
503eda14cbcSMatt Macy 	ret = random_get_pseudo_bytes(iv, WRAPPING_IV_LEN);
504eda14cbcSMatt Macy 	if (ret != 0)
505eda14cbcSMatt Macy 		goto error;
506eda14cbcSMatt Macy 
507184c1b94SMartin Matuska 	/* initialize zfs_uio_ts */
508eda14cbcSMatt Macy 	plain_iovecs[0].iov_base = key->zk_master_keydata;
509eda14cbcSMatt Macy 	plain_iovecs[0].iov_len = keydata_len;
510eda14cbcSMatt Macy 	plain_iovecs[1].iov_base = key->zk_hmac_keydata;
511eda14cbcSMatt Macy 	plain_iovecs[1].iov_len = SHA512_HMAC_KEYLEN;
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy 	cipher_iovecs[0].iov_base = keydata_out;
514eda14cbcSMatt Macy 	cipher_iovecs[0].iov_len = keydata_len;
515eda14cbcSMatt Macy 	cipher_iovecs[1].iov_base = hmac_keydata_out;
516eda14cbcSMatt Macy 	cipher_iovecs[1].iov_len = SHA512_HMAC_KEYLEN;
517eda14cbcSMatt Macy 	cipher_iovecs[2].iov_base = mac;
518eda14cbcSMatt Macy 	cipher_iovecs[2].iov_len = WRAPPING_MAC_LEN;
519eda14cbcSMatt Macy 
520eda14cbcSMatt Macy 	/*
521eda14cbcSMatt Macy 	 * Although we don't support writing to the old format, we do
522eda14cbcSMatt Macy 	 * support rewrapping the key so that the user can move and
523eda14cbcSMatt Macy 	 * quarantine datasets on the old format.
524eda14cbcSMatt Macy 	 */
525eda14cbcSMatt Macy 	if (key->zk_version == 0) {
526eda14cbcSMatt Macy 		aad_len = sizeof (uint64_t);
527eda14cbcSMatt Macy 		aad[0] = LE_64(key->zk_guid);
528eda14cbcSMatt Macy 	} else {
529eda14cbcSMatt Macy 		ASSERT3U(key->zk_version, ==, ZIO_CRYPT_KEY_CURRENT_VERSION);
530eda14cbcSMatt Macy 		aad_len = sizeof (uint64_t) * 3;
531eda14cbcSMatt Macy 		aad[0] = LE_64(key->zk_guid);
532eda14cbcSMatt Macy 		aad[1] = LE_64(crypt);
533eda14cbcSMatt Macy 		aad[2] = LE_64(key->zk_version);
534eda14cbcSMatt Macy 	}
535eda14cbcSMatt Macy 
536eda14cbcSMatt Macy 	enc_len = zio_crypt_table[crypt].ci_keylen + SHA512_HMAC_KEYLEN;
537eda14cbcSMatt Macy 	puio.uio_iov = plain_iovecs;
538eda14cbcSMatt Macy 	puio.uio_iovcnt = 2;
539eda14cbcSMatt Macy 	puio.uio_segflg = UIO_SYSSPACE;
540eda14cbcSMatt Macy 	cuio.uio_iov = cipher_iovecs;
541eda14cbcSMatt Macy 	cuio.uio_iovcnt = 3;
542eda14cbcSMatt Macy 	cuio.uio_segflg = UIO_SYSSPACE;
543eda14cbcSMatt Macy 
544eda14cbcSMatt Macy 	/* encrypt the keys and store the resulting ciphertext and mac */
545eda14cbcSMatt Macy 	ret = zio_do_crypt_uio(B_TRUE, crypt, cwkey, NULL, iv, enc_len,
546eda14cbcSMatt Macy 	    &puio, &cuio, (uint8_t *)aad, aad_len);
547eda14cbcSMatt Macy 	if (ret != 0)
548eda14cbcSMatt Macy 		goto error;
549eda14cbcSMatt Macy 
550eda14cbcSMatt Macy 	return (0);
551eda14cbcSMatt Macy 
552eda14cbcSMatt Macy error:
553eda14cbcSMatt Macy 	return (ret);
554eda14cbcSMatt Macy }
555eda14cbcSMatt Macy 
556eda14cbcSMatt Macy int
557eda14cbcSMatt Macy zio_crypt_key_unwrap(crypto_key_t *cwkey, uint64_t crypt, uint64_t version,
558eda14cbcSMatt Macy     uint64_t guid, uint8_t *keydata, uint8_t *hmac_keydata, uint8_t *iv,
559eda14cbcSMatt Macy     uint8_t *mac, zio_crypt_key_t *key)
560eda14cbcSMatt Macy {
561eda14cbcSMatt Macy 	crypto_mechanism_t mech;
562184c1b94SMartin Matuska 	zfs_uio_t puio, cuio;
563eda14cbcSMatt Macy 	uint64_t aad[3];
564eda14cbcSMatt Macy 	iovec_t plain_iovecs[2], cipher_iovecs[3];
565eda14cbcSMatt Macy 	uint_t enc_len, keydata_len, aad_len;
566eda14cbcSMatt Macy 	int ret;
567eda14cbcSMatt Macy 
568eda14cbcSMatt Macy 	ASSERT3U(crypt, <, ZIO_CRYPT_FUNCTIONS);
569eda14cbcSMatt Macy 
570eda14cbcSMatt Macy 	rw_init(&key->zk_salt_lock, NULL, RW_DEFAULT, NULL);
571eda14cbcSMatt Macy 
572eda14cbcSMatt Macy 	keydata_len = zio_crypt_table[crypt].ci_keylen;
573eda14cbcSMatt Macy 
574184c1b94SMartin Matuska 	/* initialize zfs_uio_ts */
575eda14cbcSMatt Macy 	plain_iovecs[0].iov_base = key->zk_master_keydata;
576eda14cbcSMatt Macy 	plain_iovecs[0].iov_len = keydata_len;
577eda14cbcSMatt Macy 	plain_iovecs[1].iov_base = key->zk_hmac_keydata;
578eda14cbcSMatt Macy 	plain_iovecs[1].iov_len = SHA512_HMAC_KEYLEN;
579eda14cbcSMatt Macy 
580eda14cbcSMatt Macy 	cipher_iovecs[0].iov_base = keydata;
581eda14cbcSMatt Macy 	cipher_iovecs[0].iov_len = keydata_len;
582eda14cbcSMatt Macy 	cipher_iovecs[1].iov_base = hmac_keydata;
583eda14cbcSMatt Macy 	cipher_iovecs[1].iov_len = SHA512_HMAC_KEYLEN;
584eda14cbcSMatt Macy 	cipher_iovecs[2].iov_base = mac;
585eda14cbcSMatt Macy 	cipher_iovecs[2].iov_len = WRAPPING_MAC_LEN;
586eda14cbcSMatt Macy 
587eda14cbcSMatt Macy 	if (version == 0) {
588eda14cbcSMatt Macy 		aad_len = sizeof (uint64_t);
589eda14cbcSMatt Macy 		aad[0] = LE_64(guid);
590eda14cbcSMatt Macy 	} else {
591eda14cbcSMatt Macy 		ASSERT3U(version, ==, ZIO_CRYPT_KEY_CURRENT_VERSION);
592eda14cbcSMatt Macy 		aad_len = sizeof (uint64_t) * 3;
593eda14cbcSMatt Macy 		aad[0] = LE_64(guid);
594eda14cbcSMatt Macy 		aad[1] = LE_64(crypt);
595eda14cbcSMatt Macy 		aad[2] = LE_64(version);
596eda14cbcSMatt Macy 	}
597eda14cbcSMatt Macy 
598eda14cbcSMatt Macy 	enc_len = keydata_len + SHA512_HMAC_KEYLEN;
599eda14cbcSMatt Macy 	puio.uio_iov = plain_iovecs;
600eda14cbcSMatt Macy 	puio.uio_segflg = UIO_SYSSPACE;
601eda14cbcSMatt Macy 	puio.uio_iovcnt = 2;
602eda14cbcSMatt Macy 	cuio.uio_iov = cipher_iovecs;
603eda14cbcSMatt Macy 	cuio.uio_iovcnt = 3;
604eda14cbcSMatt Macy 	cuio.uio_segflg = UIO_SYSSPACE;
605eda14cbcSMatt Macy 
606eda14cbcSMatt Macy 	/* decrypt the keys and store the result in the output buffers */
607eda14cbcSMatt Macy 	ret = zio_do_crypt_uio(B_FALSE, crypt, cwkey, NULL, iv, enc_len,
608eda14cbcSMatt Macy 	    &puio, &cuio, (uint8_t *)aad, aad_len);
609eda14cbcSMatt Macy 	if (ret != 0)
610eda14cbcSMatt Macy 		goto error;
611eda14cbcSMatt Macy 
612eda14cbcSMatt Macy 	/* generate a fresh salt */
613eda14cbcSMatt Macy 	ret = random_get_bytes(key->zk_salt, ZIO_DATA_SALT_LEN);
614eda14cbcSMatt Macy 	if (ret != 0)
615eda14cbcSMatt Macy 		goto error;
616eda14cbcSMatt Macy 
617eda14cbcSMatt Macy 	/* derive the current key from the master key */
618eda14cbcSMatt Macy 	ret = hkdf_sha512(key->zk_master_keydata, keydata_len, NULL, 0,
619eda14cbcSMatt Macy 	    key->zk_salt, ZIO_DATA_SALT_LEN, key->zk_current_keydata,
620eda14cbcSMatt Macy 	    keydata_len);
621eda14cbcSMatt Macy 	if (ret != 0)
622eda14cbcSMatt Macy 		goto error;
623eda14cbcSMatt Macy 
624eda14cbcSMatt Macy 	/* initialize keys for ICP */
625eda14cbcSMatt Macy 	key->zk_current_key.ck_data = key->zk_current_keydata;
626eda14cbcSMatt Macy 	key->zk_current_key.ck_length = CRYPTO_BYTES2BITS(keydata_len);
627eda14cbcSMatt Macy 
628eda14cbcSMatt Macy 	key->zk_hmac_key.ck_data = key->zk_hmac_keydata;
629eda14cbcSMatt Macy 	key->zk_hmac_key.ck_length = CRYPTO_BYTES2BITS(SHA512_HMAC_KEYLEN);
630eda14cbcSMatt Macy 
631eda14cbcSMatt Macy 	/*
632eda14cbcSMatt Macy 	 * Initialize the crypto templates. It's ok if this fails because
633eda14cbcSMatt Macy 	 * this is just an optimization.
634eda14cbcSMatt Macy 	 */
635eda14cbcSMatt Macy 	mech.cm_type = crypto_mech2id(zio_crypt_table[crypt].ci_mechname);
636eda14cbcSMatt Macy 	ret = crypto_create_ctx_template(&mech, &key->zk_current_key,
637c03c5b1cSMartin Matuska 	    &key->zk_current_tmpl);
638eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS)
639eda14cbcSMatt Macy 		key->zk_current_tmpl = NULL;
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 	mech.cm_type = crypto_mech2id(SUN_CKM_SHA512_HMAC);
642eda14cbcSMatt Macy 	ret = crypto_create_ctx_template(&mech, &key->zk_hmac_key,
643c03c5b1cSMartin Matuska 	    &key->zk_hmac_tmpl);
644eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS)
645eda14cbcSMatt Macy 		key->zk_hmac_tmpl = NULL;
646eda14cbcSMatt Macy 
647eda14cbcSMatt Macy 	key->zk_crypt = crypt;
648eda14cbcSMatt Macy 	key->zk_version = version;
649eda14cbcSMatt Macy 	key->zk_guid = guid;
650eda14cbcSMatt Macy 	key->zk_salt_count = 0;
651eda14cbcSMatt Macy 
652eda14cbcSMatt Macy 	return (0);
653eda14cbcSMatt Macy 
654eda14cbcSMatt Macy error:
655eda14cbcSMatt Macy 	zio_crypt_key_destroy(key);
656eda14cbcSMatt Macy 	return (ret);
657eda14cbcSMatt Macy }
658eda14cbcSMatt Macy 
659eda14cbcSMatt Macy int
660eda14cbcSMatt Macy zio_crypt_generate_iv(uint8_t *ivbuf)
661eda14cbcSMatt Macy {
662eda14cbcSMatt Macy 	int ret;
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy 	/* randomly generate the IV */
665eda14cbcSMatt Macy 	ret = random_get_pseudo_bytes(ivbuf, ZIO_DATA_IV_LEN);
666eda14cbcSMatt Macy 	if (ret != 0)
667eda14cbcSMatt Macy 		goto error;
668eda14cbcSMatt Macy 
669eda14cbcSMatt Macy 	return (0);
670eda14cbcSMatt Macy 
671eda14cbcSMatt Macy error:
672da5137abSMartin Matuska 	memset(ivbuf, 0, ZIO_DATA_IV_LEN);
673eda14cbcSMatt Macy 	return (ret);
674eda14cbcSMatt Macy }
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy int
677eda14cbcSMatt Macy zio_crypt_do_hmac(zio_crypt_key_t *key, uint8_t *data, uint_t datalen,
678eda14cbcSMatt Macy     uint8_t *digestbuf, uint_t digestlen)
679eda14cbcSMatt Macy {
680eda14cbcSMatt Macy 	int ret;
681eda14cbcSMatt Macy 	crypto_mechanism_t mech;
682eda14cbcSMatt Macy 	crypto_data_t in_data, digest_data;
683eda14cbcSMatt Macy 	uint8_t raw_digestbuf[SHA512_DIGEST_LENGTH];
684eda14cbcSMatt Macy 
685eda14cbcSMatt Macy 	ASSERT3U(digestlen, <=, SHA512_DIGEST_LENGTH);
686eda14cbcSMatt Macy 
687eda14cbcSMatt Macy 	/* initialize sha512-hmac mechanism and crypto data */
688eda14cbcSMatt Macy 	mech.cm_type = crypto_mech2id(SUN_CKM_SHA512_HMAC);
689eda14cbcSMatt Macy 	mech.cm_param = NULL;
690eda14cbcSMatt Macy 	mech.cm_param_len = 0;
691eda14cbcSMatt Macy 
692eda14cbcSMatt Macy 	/* initialize the crypto data */
693eda14cbcSMatt Macy 	in_data.cd_format = CRYPTO_DATA_RAW;
694eda14cbcSMatt Macy 	in_data.cd_offset = 0;
695eda14cbcSMatt Macy 	in_data.cd_length = datalen;
696eda14cbcSMatt Macy 	in_data.cd_raw.iov_base = (char *)data;
697eda14cbcSMatt Macy 	in_data.cd_raw.iov_len = in_data.cd_length;
698eda14cbcSMatt Macy 
699eda14cbcSMatt Macy 	digest_data.cd_format = CRYPTO_DATA_RAW;
700eda14cbcSMatt Macy 	digest_data.cd_offset = 0;
701eda14cbcSMatt Macy 	digest_data.cd_length = SHA512_DIGEST_LENGTH;
702eda14cbcSMatt Macy 	digest_data.cd_raw.iov_base = (char *)raw_digestbuf;
703eda14cbcSMatt Macy 	digest_data.cd_raw.iov_len = digest_data.cd_length;
704eda14cbcSMatt Macy 
705eda14cbcSMatt Macy 	/* generate the hmac */
706eda14cbcSMatt Macy 	ret = crypto_mac(&mech, &in_data, &key->zk_hmac_key, key->zk_hmac_tmpl,
707c03c5b1cSMartin Matuska 	    &digest_data);
708eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
709eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
710eda14cbcSMatt Macy 		goto error;
711eda14cbcSMatt Macy 	}
712eda14cbcSMatt Macy 
713da5137abSMartin Matuska 	memcpy(digestbuf, raw_digestbuf, digestlen);
714eda14cbcSMatt Macy 
715eda14cbcSMatt Macy 	return (0);
716eda14cbcSMatt Macy 
717eda14cbcSMatt Macy error:
718da5137abSMartin Matuska 	memset(digestbuf, 0, digestlen);
719eda14cbcSMatt Macy 	return (ret);
720eda14cbcSMatt Macy }
721eda14cbcSMatt Macy 
722eda14cbcSMatt Macy int
723eda14cbcSMatt Macy zio_crypt_generate_iv_salt_dedup(zio_crypt_key_t *key, uint8_t *data,
724eda14cbcSMatt Macy     uint_t datalen, uint8_t *ivbuf, uint8_t *salt)
725eda14cbcSMatt Macy {
726eda14cbcSMatt Macy 	int ret;
727eda14cbcSMatt Macy 	uint8_t digestbuf[SHA512_DIGEST_LENGTH];
728eda14cbcSMatt Macy 
729eda14cbcSMatt Macy 	ret = zio_crypt_do_hmac(key, data, datalen,
730eda14cbcSMatt Macy 	    digestbuf, SHA512_DIGEST_LENGTH);
731eda14cbcSMatt Macy 	if (ret != 0)
732eda14cbcSMatt Macy 		return (ret);
733eda14cbcSMatt Macy 
734da5137abSMartin Matuska 	memcpy(salt, digestbuf, ZIO_DATA_SALT_LEN);
735da5137abSMartin Matuska 	memcpy(ivbuf, digestbuf + ZIO_DATA_SALT_LEN, ZIO_DATA_IV_LEN);
736eda14cbcSMatt Macy 
737eda14cbcSMatt Macy 	return (0);
738eda14cbcSMatt Macy }
739eda14cbcSMatt Macy 
740eda14cbcSMatt Macy /*
741eda14cbcSMatt Macy  * The following functions are used to encode and decode encryption parameters
742eda14cbcSMatt Macy  * into blkptr_t and zil_header_t. The ICP wants to use these parameters as
743eda14cbcSMatt Macy  * byte strings, which normally means that these strings would not need to deal
744eda14cbcSMatt Macy  * with byteswapping at all. However, both blkptr_t and zil_header_t may be
745eda14cbcSMatt Macy  * byteswapped by lower layers and so we must "undo" that byteswap here upon
746eda14cbcSMatt Macy  * decoding and encoding in a non-native byteorder. These functions require
747eda14cbcSMatt Macy  * that the byteorder bit is correct before being called.
748eda14cbcSMatt Macy  */
749eda14cbcSMatt Macy void
750eda14cbcSMatt Macy zio_crypt_encode_params_bp(blkptr_t *bp, uint8_t *salt, uint8_t *iv)
751eda14cbcSMatt Macy {
752eda14cbcSMatt Macy 	uint64_t val64;
753eda14cbcSMatt Macy 	uint32_t val32;
754eda14cbcSMatt Macy 
755eda14cbcSMatt Macy 	ASSERT(BP_IS_ENCRYPTED(bp));
756eda14cbcSMatt Macy 
757eda14cbcSMatt Macy 	if (!BP_SHOULD_BYTESWAP(bp)) {
758da5137abSMartin Matuska 		memcpy(&bp->blk_dva[2].dva_word[0], salt, sizeof (uint64_t));
759da5137abSMartin Matuska 		memcpy(&bp->blk_dva[2].dva_word[1], iv, sizeof (uint64_t));
760da5137abSMartin Matuska 		memcpy(&val32, iv + sizeof (uint64_t), sizeof (uint32_t));
761eda14cbcSMatt Macy 		BP_SET_IV2(bp, val32);
762eda14cbcSMatt Macy 	} else {
763da5137abSMartin Matuska 		memcpy(&val64, salt, sizeof (uint64_t));
764eda14cbcSMatt Macy 		bp->blk_dva[2].dva_word[0] = BSWAP_64(val64);
765eda14cbcSMatt Macy 
766da5137abSMartin Matuska 		memcpy(&val64, iv, sizeof (uint64_t));
767eda14cbcSMatt Macy 		bp->blk_dva[2].dva_word[1] = BSWAP_64(val64);
768eda14cbcSMatt Macy 
769da5137abSMartin Matuska 		memcpy(&val32, iv + sizeof (uint64_t), sizeof (uint32_t));
770eda14cbcSMatt Macy 		BP_SET_IV2(bp, BSWAP_32(val32));
771eda14cbcSMatt Macy 	}
772eda14cbcSMatt Macy }
773eda14cbcSMatt Macy 
774eda14cbcSMatt Macy void
775eda14cbcSMatt Macy zio_crypt_decode_params_bp(const blkptr_t *bp, uint8_t *salt, uint8_t *iv)
776eda14cbcSMatt Macy {
777eda14cbcSMatt Macy 	uint64_t val64;
778eda14cbcSMatt Macy 	uint32_t val32;
779eda14cbcSMatt Macy 
780eda14cbcSMatt Macy 	ASSERT(BP_IS_PROTECTED(bp));
781eda14cbcSMatt Macy 
782eda14cbcSMatt Macy 	/* for convenience, so callers don't need to check */
783eda14cbcSMatt Macy 	if (BP_IS_AUTHENTICATED(bp)) {
784da5137abSMartin Matuska 		memset(salt, 0, ZIO_DATA_SALT_LEN);
785da5137abSMartin Matuska 		memset(iv, 0, ZIO_DATA_IV_LEN);
786eda14cbcSMatt Macy 		return;
787eda14cbcSMatt Macy 	}
788eda14cbcSMatt Macy 
789eda14cbcSMatt Macy 	if (!BP_SHOULD_BYTESWAP(bp)) {
790da5137abSMartin Matuska 		memcpy(salt, &bp->blk_dva[2].dva_word[0], sizeof (uint64_t));
791da5137abSMartin Matuska 		memcpy(iv, &bp->blk_dva[2].dva_word[1], sizeof (uint64_t));
792eda14cbcSMatt Macy 
793eda14cbcSMatt Macy 		val32 = (uint32_t)BP_GET_IV2(bp);
794da5137abSMartin Matuska 		memcpy(iv + sizeof (uint64_t), &val32, sizeof (uint32_t));
795eda14cbcSMatt Macy 	} else {
796eda14cbcSMatt Macy 		val64 = BSWAP_64(bp->blk_dva[2].dva_word[0]);
797da5137abSMartin Matuska 		memcpy(salt, &val64, sizeof (uint64_t));
798eda14cbcSMatt Macy 
799eda14cbcSMatt Macy 		val64 = BSWAP_64(bp->blk_dva[2].dva_word[1]);
800da5137abSMartin Matuska 		memcpy(iv, &val64, sizeof (uint64_t));
801eda14cbcSMatt Macy 
802eda14cbcSMatt Macy 		val32 = BSWAP_32((uint32_t)BP_GET_IV2(bp));
803da5137abSMartin Matuska 		memcpy(iv + sizeof (uint64_t), &val32, sizeof (uint32_t));
804eda14cbcSMatt Macy 	}
805eda14cbcSMatt Macy }
806eda14cbcSMatt Macy 
807eda14cbcSMatt Macy void
808eda14cbcSMatt Macy zio_crypt_encode_mac_bp(blkptr_t *bp, uint8_t *mac)
809eda14cbcSMatt Macy {
810eda14cbcSMatt Macy 	uint64_t val64;
811eda14cbcSMatt Macy 
812eda14cbcSMatt Macy 	ASSERT(BP_USES_CRYPT(bp));
813eda14cbcSMatt Macy 	ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_OBJSET);
814eda14cbcSMatt Macy 
815eda14cbcSMatt Macy 	if (!BP_SHOULD_BYTESWAP(bp)) {
816da5137abSMartin Matuska 		memcpy(&bp->blk_cksum.zc_word[2], mac, sizeof (uint64_t));
817da5137abSMartin Matuska 		memcpy(&bp->blk_cksum.zc_word[3], mac + sizeof (uint64_t),
818eda14cbcSMatt Macy 		    sizeof (uint64_t));
819eda14cbcSMatt Macy 	} else {
820da5137abSMartin Matuska 		memcpy(&val64, mac, sizeof (uint64_t));
821eda14cbcSMatt Macy 		bp->blk_cksum.zc_word[2] = BSWAP_64(val64);
822eda14cbcSMatt Macy 
823da5137abSMartin Matuska 		memcpy(&val64, mac + sizeof (uint64_t), sizeof (uint64_t));
824eda14cbcSMatt Macy 		bp->blk_cksum.zc_word[3] = BSWAP_64(val64);
825eda14cbcSMatt Macy 	}
826eda14cbcSMatt Macy }
827eda14cbcSMatt Macy 
828eda14cbcSMatt Macy void
829eda14cbcSMatt Macy zio_crypt_decode_mac_bp(const blkptr_t *bp, uint8_t *mac)
830eda14cbcSMatt Macy {
831eda14cbcSMatt Macy 	uint64_t val64;
832eda14cbcSMatt Macy 
833eda14cbcSMatt Macy 	ASSERT(BP_USES_CRYPT(bp) || BP_IS_HOLE(bp));
834eda14cbcSMatt Macy 
835eda14cbcSMatt Macy 	/* for convenience, so callers don't need to check */
836eda14cbcSMatt Macy 	if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
837da5137abSMartin Matuska 		memset(mac, 0, ZIO_DATA_MAC_LEN);
838eda14cbcSMatt Macy 		return;
839eda14cbcSMatt Macy 	}
840eda14cbcSMatt Macy 
841eda14cbcSMatt Macy 	if (!BP_SHOULD_BYTESWAP(bp)) {
842da5137abSMartin Matuska 		memcpy(mac, &bp->blk_cksum.zc_word[2], sizeof (uint64_t));
843da5137abSMartin Matuska 		memcpy(mac + sizeof (uint64_t), &bp->blk_cksum.zc_word[3],
844eda14cbcSMatt Macy 		    sizeof (uint64_t));
845eda14cbcSMatt Macy 	} else {
846eda14cbcSMatt Macy 		val64 = BSWAP_64(bp->blk_cksum.zc_word[2]);
847da5137abSMartin Matuska 		memcpy(mac, &val64, sizeof (uint64_t));
848eda14cbcSMatt Macy 
849eda14cbcSMatt Macy 		val64 = BSWAP_64(bp->blk_cksum.zc_word[3]);
850da5137abSMartin Matuska 		memcpy(mac + sizeof (uint64_t), &val64, sizeof (uint64_t));
851eda14cbcSMatt Macy 	}
852eda14cbcSMatt Macy }
853eda14cbcSMatt Macy 
854eda14cbcSMatt Macy void
855eda14cbcSMatt Macy zio_crypt_encode_mac_zil(void *data, uint8_t *mac)
856eda14cbcSMatt Macy {
857eda14cbcSMatt Macy 	zil_chain_t *zilc = data;
858eda14cbcSMatt Macy 
859da5137abSMartin Matuska 	memcpy(&zilc->zc_eck.zec_cksum.zc_word[2], mac, sizeof (uint64_t));
860da5137abSMartin Matuska 	memcpy(&zilc->zc_eck.zec_cksum.zc_word[3], mac + sizeof (uint64_t),
861eda14cbcSMatt Macy 	    sizeof (uint64_t));
862eda14cbcSMatt Macy }
863eda14cbcSMatt Macy 
864eda14cbcSMatt Macy void
865eda14cbcSMatt Macy zio_crypt_decode_mac_zil(const void *data, uint8_t *mac)
866eda14cbcSMatt Macy {
867eda14cbcSMatt Macy 	/*
868eda14cbcSMatt Macy 	 * The ZIL MAC is embedded in the block it protects, which will
869eda14cbcSMatt Macy 	 * not have been byteswapped by the time this function has been called.
870eda14cbcSMatt Macy 	 * As a result, we don't need to worry about byteswapping the MAC.
871eda14cbcSMatt Macy 	 */
872eda14cbcSMatt Macy 	const zil_chain_t *zilc = data;
873eda14cbcSMatt Macy 
874da5137abSMartin Matuska 	memcpy(mac, &zilc->zc_eck.zec_cksum.zc_word[2], sizeof (uint64_t));
875da5137abSMartin Matuska 	memcpy(mac + sizeof (uint64_t), &zilc->zc_eck.zec_cksum.zc_word[3],
876eda14cbcSMatt Macy 	    sizeof (uint64_t));
877eda14cbcSMatt Macy }
878eda14cbcSMatt Macy 
879eda14cbcSMatt Macy /*
880eda14cbcSMatt Macy  * This routine takes a block of dnodes (src_abd) and copies only the bonus
881eda14cbcSMatt Macy  * buffers to the same offsets in the dst buffer. datalen should be the size
882eda14cbcSMatt Macy  * of both the src_abd and the dst buffer (not just the length of the bonus
883eda14cbcSMatt Macy  * buffers).
884eda14cbcSMatt Macy  */
885eda14cbcSMatt Macy void
886eda14cbcSMatt Macy zio_crypt_copy_dnode_bonus(abd_t *src_abd, uint8_t *dst, uint_t datalen)
887eda14cbcSMatt Macy {
888eda14cbcSMatt Macy 	uint_t i, max_dnp = datalen >> DNODE_SHIFT;
889eda14cbcSMatt Macy 	uint8_t *src;
890eda14cbcSMatt Macy 	dnode_phys_t *dnp, *sdnp, *ddnp;
891eda14cbcSMatt Macy 
892eda14cbcSMatt Macy 	src = abd_borrow_buf_copy(src_abd, datalen);
893eda14cbcSMatt Macy 
894eda14cbcSMatt Macy 	sdnp = (dnode_phys_t *)src;
895eda14cbcSMatt Macy 	ddnp = (dnode_phys_t *)dst;
896eda14cbcSMatt Macy 
897eda14cbcSMatt Macy 	for (i = 0; i < max_dnp; i += sdnp[i].dn_extra_slots + 1) {
898eda14cbcSMatt Macy 		dnp = &sdnp[i];
899eda14cbcSMatt Macy 		if (dnp->dn_type != DMU_OT_NONE &&
900eda14cbcSMatt Macy 		    DMU_OT_IS_ENCRYPTED(dnp->dn_bonustype) &&
901eda14cbcSMatt Macy 		    dnp->dn_bonuslen != 0) {
902da5137abSMartin Matuska 			memcpy(DN_BONUS(&ddnp[i]), DN_BONUS(dnp),
903eda14cbcSMatt Macy 			    DN_MAX_BONUS_LEN(dnp));
904eda14cbcSMatt Macy 		}
905eda14cbcSMatt Macy 	}
906eda14cbcSMatt Macy 
907eda14cbcSMatt Macy 	abd_return_buf(src_abd, src, datalen);
908eda14cbcSMatt Macy }
909eda14cbcSMatt Macy 
910eda14cbcSMatt Macy /*
911eda14cbcSMatt Macy  * This function decides what fields from blk_prop are included in
912eda14cbcSMatt Macy  * the on-disk various MAC algorithms.
913eda14cbcSMatt Macy  */
914eda14cbcSMatt Macy static void
915eda14cbcSMatt Macy zio_crypt_bp_zero_nonportable_blkprop(blkptr_t *bp, uint64_t version)
916eda14cbcSMatt Macy {
917eda14cbcSMatt Macy 	/*
918eda14cbcSMatt Macy 	 * Version 0 did not properly zero out all non-portable fields
919eda14cbcSMatt Macy 	 * as it should have done. We maintain this code so that we can
920eda14cbcSMatt Macy 	 * do read-only imports of pools on this version.
921eda14cbcSMatt Macy 	 */
922eda14cbcSMatt Macy 	if (version == 0) {
923eda14cbcSMatt Macy 		BP_SET_DEDUP(bp, 0);
924eda14cbcSMatt Macy 		BP_SET_CHECKSUM(bp, 0);
925eda14cbcSMatt Macy 		BP_SET_PSIZE(bp, SPA_MINBLOCKSIZE);
926eda14cbcSMatt Macy 		return;
927eda14cbcSMatt Macy 	}
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy 	ASSERT3U(version, ==, ZIO_CRYPT_KEY_CURRENT_VERSION);
930eda14cbcSMatt Macy 
931eda14cbcSMatt Macy 	/*
932eda14cbcSMatt Macy 	 * The hole_birth feature might set these fields even if this bp
933eda14cbcSMatt Macy 	 * is a hole. We zero them out here to guarantee that raw sends
934eda14cbcSMatt Macy 	 * will function with or without the feature.
935eda14cbcSMatt Macy 	 */
936eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp)) {
937eda14cbcSMatt Macy 		bp->blk_prop = 0ULL;
938eda14cbcSMatt Macy 		return;
939eda14cbcSMatt Macy 	}
940eda14cbcSMatt Macy 
941eda14cbcSMatt Macy 	/*
942eda14cbcSMatt Macy 	 * At L0 we want to verify these fields to ensure that data blocks
943eda14cbcSMatt Macy 	 * can not be reinterpreted. For instance, we do not want an attacker
944eda14cbcSMatt Macy 	 * to trick us into returning raw lz4 compressed data to the user
945eda14cbcSMatt Macy 	 * by modifying the compression bits. At higher levels, we cannot
946eda14cbcSMatt Macy 	 * enforce this policy since raw sends do not convey any information
947eda14cbcSMatt Macy 	 * about indirect blocks, so these values might be different on the
948eda14cbcSMatt Macy 	 * receive side. Fortunately, this does not open any new attack
949eda14cbcSMatt Macy 	 * vectors, since any alterations that can be made to a higher level
950eda14cbcSMatt Macy 	 * bp must still verify the correct order of the layer below it.
951eda14cbcSMatt Macy 	 */
952eda14cbcSMatt Macy 	if (BP_GET_LEVEL(bp) != 0) {
953eda14cbcSMatt Macy 		BP_SET_BYTEORDER(bp, 0);
954eda14cbcSMatt Macy 		BP_SET_COMPRESS(bp, 0);
955eda14cbcSMatt Macy 
956eda14cbcSMatt Macy 		/*
957eda14cbcSMatt Macy 		 * psize cannot be set to zero or it will trigger
958eda14cbcSMatt Macy 		 * asserts, but the value doesn't really matter as
959eda14cbcSMatt Macy 		 * long as it is constant.
960eda14cbcSMatt Macy 		 */
961eda14cbcSMatt Macy 		BP_SET_PSIZE(bp, SPA_MINBLOCKSIZE);
962eda14cbcSMatt Macy 	}
963eda14cbcSMatt Macy 
964eda14cbcSMatt Macy 	BP_SET_DEDUP(bp, 0);
965eda14cbcSMatt Macy 	BP_SET_CHECKSUM(bp, 0);
966eda14cbcSMatt Macy }
967eda14cbcSMatt Macy 
968eda14cbcSMatt Macy static void
969eda14cbcSMatt Macy zio_crypt_bp_auth_init(uint64_t version, boolean_t should_bswap, blkptr_t *bp,
970eda14cbcSMatt Macy     blkptr_auth_buf_t *bab, uint_t *bab_len)
971eda14cbcSMatt Macy {
972eda14cbcSMatt Macy 	blkptr_t tmpbp = *bp;
973eda14cbcSMatt Macy 
974eda14cbcSMatt Macy 	if (should_bswap)
975eda14cbcSMatt Macy 		byteswap_uint64_array(&tmpbp, sizeof (blkptr_t));
976eda14cbcSMatt Macy 
977eda14cbcSMatt Macy 	ASSERT(BP_USES_CRYPT(&tmpbp) || BP_IS_HOLE(&tmpbp));
978eda14cbcSMatt Macy 	ASSERT0(BP_IS_EMBEDDED(&tmpbp));
979eda14cbcSMatt Macy 
980eda14cbcSMatt Macy 	zio_crypt_decode_mac_bp(&tmpbp, bab->bab_mac);
981eda14cbcSMatt Macy 
982eda14cbcSMatt Macy 	/*
983eda14cbcSMatt Macy 	 * We always MAC blk_prop in LE to ensure portability. This
984eda14cbcSMatt Macy 	 * must be done after decoding the mac, since the endianness
985eda14cbcSMatt Macy 	 * will get zero'd out here.
986eda14cbcSMatt Macy 	 */
987eda14cbcSMatt Macy 	zio_crypt_bp_zero_nonportable_blkprop(&tmpbp, version);
988eda14cbcSMatt Macy 	bab->bab_prop = LE_64(tmpbp.blk_prop);
989eda14cbcSMatt Macy 	bab->bab_pad = 0ULL;
990eda14cbcSMatt Macy 
991eda14cbcSMatt Macy 	/* version 0 did not include the padding */
992eda14cbcSMatt Macy 	*bab_len = sizeof (blkptr_auth_buf_t);
993eda14cbcSMatt Macy 	if (version == 0)
994eda14cbcSMatt Macy 		*bab_len -= sizeof (uint64_t);
995eda14cbcSMatt Macy }
996eda14cbcSMatt Macy 
997eda14cbcSMatt Macy static int
998eda14cbcSMatt Macy zio_crypt_bp_do_hmac_updates(crypto_context_t ctx, uint64_t version,
999eda14cbcSMatt Macy     boolean_t should_bswap, blkptr_t *bp)
1000eda14cbcSMatt Macy {
1001eda14cbcSMatt Macy 	int ret;
1002eda14cbcSMatt Macy 	uint_t bab_len;
1003eda14cbcSMatt Macy 	blkptr_auth_buf_t bab;
1004eda14cbcSMatt Macy 	crypto_data_t cd;
1005eda14cbcSMatt Macy 
1006eda14cbcSMatt Macy 	zio_crypt_bp_auth_init(version, should_bswap, bp, &bab, &bab_len);
1007eda14cbcSMatt Macy 	cd.cd_format = CRYPTO_DATA_RAW;
1008eda14cbcSMatt Macy 	cd.cd_offset = 0;
1009eda14cbcSMatt Macy 	cd.cd_length = bab_len;
1010eda14cbcSMatt Macy 	cd.cd_raw.iov_base = (char *)&bab;
1011eda14cbcSMatt Macy 	cd.cd_raw.iov_len = cd.cd_length;
1012eda14cbcSMatt Macy 
1013c03c5b1cSMartin Matuska 	ret = crypto_mac_update(ctx, &cd);
1014eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
1015eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
1016eda14cbcSMatt Macy 		goto error;
1017eda14cbcSMatt Macy 	}
1018eda14cbcSMatt Macy 
1019eda14cbcSMatt Macy 	return (0);
1020eda14cbcSMatt Macy 
1021eda14cbcSMatt Macy error:
1022eda14cbcSMatt Macy 	return (ret);
1023eda14cbcSMatt Macy }
1024eda14cbcSMatt Macy 
1025eda14cbcSMatt Macy static void
1026eda14cbcSMatt Macy zio_crypt_bp_do_indrect_checksum_updates(SHA2_CTX *ctx, uint64_t version,
1027eda14cbcSMatt Macy     boolean_t should_bswap, blkptr_t *bp)
1028eda14cbcSMatt Macy {
1029eda14cbcSMatt Macy 	uint_t bab_len;
1030eda14cbcSMatt Macy 	blkptr_auth_buf_t bab;
1031eda14cbcSMatt Macy 
1032eda14cbcSMatt Macy 	zio_crypt_bp_auth_init(version, should_bswap, bp, &bab, &bab_len);
1033eda14cbcSMatt Macy 	SHA2Update(ctx, &bab, bab_len);
1034eda14cbcSMatt Macy }
1035eda14cbcSMatt Macy 
1036eda14cbcSMatt Macy static void
1037eda14cbcSMatt Macy zio_crypt_bp_do_aad_updates(uint8_t **aadp, uint_t *aad_len, uint64_t version,
1038eda14cbcSMatt Macy     boolean_t should_bswap, blkptr_t *bp)
1039eda14cbcSMatt Macy {
1040eda14cbcSMatt Macy 	uint_t bab_len;
1041eda14cbcSMatt Macy 	blkptr_auth_buf_t bab;
1042eda14cbcSMatt Macy 
1043eda14cbcSMatt Macy 	zio_crypt_bp_auth_init(version, should_bswap, bp, &bab, &bab_len);
1044da5137abSMartin Matuska 	memcpy(*aadp, &bab, bab_len);
1045eda14cbcSMatt Macy 	*aadp += bab_len;
1046eda14cbcSMatt Macy 	*aad_len += bab_len;
1047eda14cbcSMatt Macy }
1048eda14cbcSMatt Macy 
1049eda14cbcSMatt Macy static int
1050eda14cbcSMatt Macy zio_crypt_do_dnode_hmac_updates(crypto_context_t ctx, uint64_t version,
1051eda14cbcSMatt Macy     boolean_t should_bswap, dnode_phys_t *dnp)
1052eda14cbcSMatt Macy {
1053eda14cbcSMatt Macy 	int ret, i;
105433b8c039SMartin Matuska 	dnode_phys_t *adnp, tmp_dncore;
105533b8c039SMartin Matuska 	size_t dn_core_size = offsetof(dnode_phys_t, dn_blkptr);
1056eda14cbcSMatt Macy 	boolean_t le_bswap = (should_bswap == ZFS_HOST_BYTEORDER);
1057eda14cbcSMatt Macy 	crypto_data_t cd;
1058eda14cbcSMatt Macy 
1059eda14cbcSMatt Macy 	cd.cd_format = CRYPTO_DATA_RAW;
1060eda14cbcSMatt Macy 	cd.cd_offset = 0;
1061eda14cbcSMatt Macy 
106233b8c039SMartin Matuska 	/*
106333b8c039SMartin Matuska 	 * Authenticate the core dnode (masking out non-portable bits).
106433b8c039SMartin Matuska 	 * We only copy the first 64 bytes we operate on to avoid the overhead
106533b8c039SMartin Matuska 	 * of copying 512-64 unneeded bytes. The compiler seems to be fine
106633b8c039SMartin Matuska 	 * with that.
106733b8c039SMartin Matuska 	 */
1068da5137abSMartin Matuska 	memcpy(&tmp_dncore, dnp, dn_core_size);
106933b8c039SMartin Matuska 	adnp = &tmp_dncore;
107033b8c039SMartin Matuska 
1071eda14cbcSMatt Macy 	if (le_bswap) {
1072eda14cbcSMatt Macy 		adnp->dn_datablkszsec = BSWAP_16(adnp->dn_datablkszsec);
1073eda14cbcSMatt Macy 		adnp->dn_bonuslen = BSWAP_16(adnp->dn_bonuslen);
1074eda14cbcSMatt Macy 		adnp->dn_maxblkid = BSWAP_64(adnp->dn_maxblkid);
1075eda14cbcSMatt Macy 		adnp->dn_used = BSWAP_64(adnp->dn_used);
1076eda14cbcSMatt Macy 	}
1077eda14cbcSMatt Macy 	adnp->dn_flags &= DNODE_CRYPT_PORTABLE_FLAGS_MASK;
1078eda14cbcSMatt Macy 	adnp->dn_used = 0;
1079eda14cbcSMatt Macy 
108033b8c039SMartin Matuska 	cd.cd_length = dn_core_size;
1081eda14cbcSMatt Macy 	cd.cd_raw.iov_base = (char *)adnp;
1082eda14cbcSMatt Macy 	cd.cd_raw.iov_len = cd.cd_length;
1083eda14cbcSMatt Macy 
1084c03c5b1cSMartin Matuska 	ret = crypto_mac_update(ctx, &cd);
1085eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
1086eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
1087eda14cbcSMatt Macy 		goto error;
1088eda14cbcSMatt Macy 	}
1089eda14cbcSMatt Macy 
1090eda14cbcSMatt Macy 	for (i = 0; i < dnp->dn_nblkptr; i++) {
1091eda14cbcSMatt Macy 		ret = zio_crypt_bp_do_hmac_updates(ctx, version,
1092eda14cbcSMatt Macy 		    should_bswap, &dnp->dn_blkptr[i]);
1093eda14cbcSMatt Macy 		if (ret != 0)
1094eda14cbcSMatt Macy 			goto error;
1095eda14cbcSMatt Macy 	}
1096eda14cbcSMatt Macy 
1097eda14cbcSMatt Macy 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
1098eda14cbcSMatt Macy 		ret = zio_crypt_bp_do_hmac_updates(ctx, version,
1099eda14cbcSMatt Macy 		    should_bswap, DN_SPILL_BLKPTR(dnp));
1100eda14cbcSMatt Macy 		if (ret != 0)
1101eda14cbcSMatt Macy 			goto error;
1102eda14cbcSMatt Macy 	}
1103eda14cbcSMatt Macy 
1104eda14cbcSMatt Macy 	return (0);
1105eda14cbcSMatt Macy 
1106eda14cbcSMatt Macy error:
1107eda14cbcSMatt Macy 	return (ret);
1108eda14cbcSMatt Macy }
1109eda14cbcSMatt Macy 
1110eda14cbcSMatt Macy /*
1111eda14cbcSMatt Macy  * objset_phys_t blocks introduce a number of exceptions to the normal
1112eda14cbcSMatt Macy  * authentication process. objset_phys_t's contain 2 separate HMACS for
1113eda14cbcSMatt Macy  * protecting the integrity of their data. The portable_mac protects the
1114eda14cbcSMatt Macy  * metadnode. This MAC can be sent with a raw send and protects against
1115eda14cbcSMatt Macy  * reordering of data within the metadnode. The local_mac protects the user
1116eda14cbcSMatt Macy  * accounting objects which are not sent from one system to another.
1117eda14cbcSMatt Macy  *
1118eda14cbcSMatt Macy  * In addition, objset blocks are the only blocks that can be modified and
1119eda14cbcSMatt Macy  * written to disk without the key loaded under certain circumstances. During
1120eda14cbcSMatt Macy  * zil_claim() we need to be able to update the zil_header_t to complete
1121eda14cbcSMatt Macy  * claiming log blocks and during raw receives we need to write out the
1122eda14cbcSMatt Macy  * portable_mac from the send file. Both of these actions are possible
1123eda14cbcSMatt Macy  * because these fields are not protected by either MAC so neither one will
1124eda14cbcSMatt Macy  * need to modify the MACs without the key. However, when the modified blocks
1125eda14cbcSMatt Macy  * are written out they will be byteswapped into the host machine's native
1126eda14cbcSMatt Macy  * endianness which will modify fields protected by the MAC. As a result, MAC
1127eda14cbcSMatt Macy  * calculation for objset blocks works slightly differently from other block
1128eda14cbcSMatt Macy  * types. Where other block types MAC the data in whatever endianness is
1129eda14cbcSMatt Macy  * written to disk, objset blocks always MAC little endian version of their
1130eda14cbcSMatt Macy  * values. In the code, should_bswap is the value from BP_SHOULD_BYTESWAP()
1131eda14cbcSMatt Macy  * and le_bswap indicates whether a byteswap is needed to get this block
1132eda14cbcSMatt Macy  * into little endian format.
1133eda14cbcSMatt Macy  */
1134eda14cbcSMatt Macy int
1135eda14cbcSMatt Macy zio_crypt_do_objset_hmacs(zio_crypt_key_t *key, void *data, uint_t datalen,
1136eda14cbcSMatt Macy     boolean_t should_bswap, uint8_t *portable_mac, uint8_t *local_mac)
1137eda14cbcSMatt Macy {
1138eda14cbcSMatt Macy 	int ret;
1139eda14cbcSMatt Macy 	crypto_mechanism_t mech;
1140eda14cbcSMatt Macy 	crypto_context_t ctx;
1141eda14cbcSMatt Macy 	crypto_data_t cd;
1142eda14cbcSMatt Macy 	objset_phys_t *osp = data;
1143eda14cbcSMatt Macy 	uint64_t intval;
1144eda14cbcSMatt Macy 	boolean_t le_bswap = (should_bswap == ZFS_HOST_BYTEORDER);
1145eda14cbcSMatt Macy 	uint8_t raw_portable_mac[SHA512_DIGEST_LENGTH];
1146eda14cbcSMatt Macy 	uint8_t raw_local_mac[SHA512_DIGEST_LENGTH];
1147eda14cbcSMatt Macy 
1148eda14cbcSMatt Macy 	/* initialize HMAC mechanism */
1149eda14cbcSMatt Macy 	mech.cm_type = crypto_mech2id(SUN_CKM_SHA512_HMAC);
1150eda14cbcSMatt Macy 	mech.cm_param = NULL;
1151eda14cbcSMatt Macy 	mech.cm_param_len = 0;
1152eda14cbcSMatt Macy 
1153eda14cbcSMatt Macy 	cd.cd_format = CRYPTO_DATA_RAW;
1154eda14cbcSMatt Macy 	cd.cd_offset = 0;
1155eda14cbcSMatt Macy 
1156eda14cbcSMatt Macy 	/* calculate the portable MAC from the portable fields and metadnode */
1157c03c5b1cSMartin Matuska 	ret = crypto_mac_init(&mech, &key->zk_hmac_key, NULL, &ctx);
1158eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
1159eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
1160eda14cbcSMatt Macy 		goto error;
1161eda14cbcSMatt Macy 	}
1162eda14cbcSMatt Macy 
1163eda14cbcSMatt Macy 	/* add in the os_type */
1164eda14cbcSMatt Macy 	intval = (le_bswap) ? osp->os_type : BSWAP_64(osp->os_type);
1165eda14cbcSMatt Macy 	cd.cd_length = sizeof (uint64_t);
1166eda14cbcSMatt Macy 	cd.cd_raw.iov_base = (char *)&intval;
1167eda14cbcSMatt Macy 	cd.cd_raw.iov_len = cd.cd_length;
1168eda14cbcSMatt Macy 
1169c03c5b1cSMartin Matuska 	ret = crypto_mac_update(ctx, &cd);
1170eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
1171eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
1172eda14cbcSMatt Macy 		goto error;
1173eda14cbcSMatt Macy 	}
1174eda14cbcSMatt Macy 
1175eda14cbcSMatt Macy 	/* add in the portable os_flags */
1176eda14cbcSMatt Macy 	intval = osp->os_flags;
1177eda14cbcSMatt Macy 	if (should_bswap)
1178eda14cbcSMatt Macy 		intval = BSWAP_64(intval);
1179eda14cbcSMatt Macy 	intval &= OBJSET_CRYPT_PORTABLE_FLAGS_MASK;
1180eda14cbcSMatt Macy 	if (!ZFS_HOST_BYTEORDER)
1181eda14cbcSMatt Macy 		intval = BSWAP_64(intval);
1182eda14cbcSMatt Macy 
1183eda14cbcSMatt Macy 	cd.cd_length = sizeof (uint64_t);
1184eda14cbcSMatt Macy 	cd.cd_raw.iov_base = (char *)&intval;
1185eda14cbcSMatt Macy 	cd.cd_raw.iov_len = cd.cd_length;
1186eda14cbcSMatt Macy 
1187c03c5b1cSMartin Matuska 	ret = crypto_mac_update(ctx, &cd);
1188eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
1189eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
1190eda14cbcSMatt Macy 		goto error;
1191eda14cbcSMatt Macy 	}
1192eda14cbcSMatt Macy 
1193eda14cbcSMatt Macy 	/* add in fields from the metadnode */
1194eda14cbcSMatt Macy 	ret = zio_crypt_do_dnode_hmac_updates(ctx, key->zk_version,
1195eda14cbcSMatt Macy 	    should_bswap, &osp->os_meta_dnode);
1196eda14cbcSMatt Macy 	if (ret)
1197eda14cbcSMatt Macy 		goto error;
1198eda14cbcSMatt Macy 
1199eda14cbcSMatt Macy 	/* store the final digest in a temporary buffer and copy what we need */
1200eda14cbcSMatt Macy 	cd.cd_length = SHA512_DIGEST_LENGTH;
1201eda14cbcSMatt Macy 	cd.cd_raw.iov_base = (char *)raw_portable_mac;
1202eda14cbcSMatt Macy 	cd.cd_raw.iov_len = cd.cd_length;
1203eda14cbcSMatt Macy 
1204c03c5b1cSMartin Matuska 	ret = crypto_mac_final(ctx, &cd);
1205eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
1206eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
1207eda14cbcSMatt Macy 		goto error;
1208eda14cbcSMatt Macy 	}
1209eda14cbcSMatt Macy 
1210da5137abSMartin Matuska 	memcpy(portable_mac, raw_portable_mac, ZIO_OBJSET_MAC_LEN);
1211eda14cbcSMatt Macy 
1212eda14cbcSMatt Macy 	/*
1213e92ffd9bSMartin Matuska 	 * This is necessary here as we check next whether
1214e92ffd9bSMartin Matuska 	 * OBJSET_FLAG_USERACCOUNTING_COMPLETE is set in order to
1215e92ffd9bSMartin Matuska 	 * decide if the local_mac should be zeroed out. That flag will always
1216e92ffd9bSMartin Matuska 	 * be set by dmu_objset_id_quota_upgrade_cb() and
1217e92ffd9bSMartin Matuska 	 * dmu_objset_userspace_upgrade_cb() if useraccounting has been
1218e92ffd9bSMartin Matuska 	 * completed.
1219e92ffd9bSMartin Matuska 	 */
1220e92ffd9bSMartin Matuska 	intval = osp->os_flags;
1221e92ffd9bSMartin Matuska 	if (should_bswap)
1222e92ffd9bSMartin Matuska 		intval = BSWAP_64(intval);
1223e92ffd9bSMartin Matuska 	boolean_t uacct_incomplete =
1224e92ffd9bSMartin Matuska 	    !(intval & OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1225e92ffd9bSMartin Matuska 
1226e92ffd9bSMartin Matuska 	/*
1227eda14cbcSMatt Macy 	 * The local MAC protects the user, group and project accounting.
1228eda14cbcSMatt Macy 	 * If these objects are not present, the local MAC is zeroed out.
1229eda14cbcSMatt Macy 	 */
1230e92ffd9bSMartin Matuska 	if (uacct_incomplete ||
1231e92ffd9bSMartin Matuska 	    (datalen >= OBJSET_PHYS_SIZE_V3 &&
1232eda14cbcSMatt Macy 	    osp->os_userused_dnode.dn_type == DMU_OT_NONE &&
1233eda14cbcSMatt Macy 	    osp->os_groupused_dnode.dn_type == DMU_OT_NONE &&
1234eda14cbcSMatt Macy 	    osp->os_projectused_dnode.dn_type == DMU_OT_NONE) ||
1235eda14cbcSMatt Macy 	    (datalen >= OBJSET_PHYS_SIZE_V2 &&
1236eda14cbcSMatt Macy 	    osp->os_userused_dnode.dn_type == DMU_OT_NONE &&
1237eda14cbcSMatt Macy 	    osp->os_groupused_dnode.dn_type == DMU_OT_NONE) ||
123816038816SMartin Matuska 	    (datalen <= OBJSET_PHYS_SIZE_V1)) {
1239da5137abSMartin Matuska 		memset(local_mac, 0, ZIO_OBJSET_MAC_LEN);
1240eda14cbcSMatt Macy 		return (0);
1241eda14cbcSMatt Macy 	}
1242eda14cbcSMatt Macy 
1243eda14cbcSMatt Macy 	/* calculate the local MAC from the userused and groupused dnodes */
1244c03c5b1cSMartin Matuska 	ret = crypto_mac_init(&mech, &key->zk_hmac_key, NULL, &ctx);
1245eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
1246eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
1247eda14cbcSMatt Macy 		goto error;
1248eda14cbcSMatt Macy 	}
1249eda14cbcSMatt Macy 
1250eda14cbcSMatt Macy 	/* add in the non-portable os_flags */
1251eda14cbcSMatt Macy 	intval = osp->os_flags;
1252eda14cbcSMatt Macy 	if (should_bswap)
1253eda14cbcSMatt Macy 		intval = BSWAP_64(intval);
1254eda14cbcSMatt Macy 	intval &= ~OBJSET_CRYPT_PORTABLE_FLAGS_MASK;
1255eda14cbcSMatt Macy 	if (!ZFS_HOST_BYTEORDER)
1256eda14cbcSMatt Macy 		intval = BSWAP_64(intval);
1257eda14cbcSMatt Macy 
1258eda14cbcSMatt Macy 	cd.cd_length = sizeof (uint64_t);
1259eda14cbcSMatt Macy 	cd.cd_raw.iov_base = (char *)&intval;
1260eda14cbcSMatt Macy 	cd.cd_raw.iov_len = cd.cd_length;
1261eda14cbcSMatt Macy 
1262c03c5b1cSMartin Matuska 	ret = crypto_mac_update(ctx, &cd);
1263eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
1264eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
1265eda14cbcSMatt Macy 		goto error;
1266eda14cbcSMatt Macy 	}
1267eda14cbcSMatt Macy 
1268eda14cbcSMatt Macy 	/* add in fields from the user accounting dnodes */
1269eda14cbcSMatt Macy 	if (osp->os_userused_dnode.dn_type != DMU_OT_NONE) {
1270eda14cbcSMatt Macy 		ret = zio_crypt_do_dnode_hmac_updates(ctx, key->zk_version,
1271eda14cbcSMatt Macy 		    should_bswap, &osp->os_userused_dnode);
1272eda14cbcSMatt Macy 		if (ret)
1273eda14cbcSMatt Macy 			goto error;
1274eda14cbcSMatt Macy 	}
1275eda14cbcSMatt Macy 
1276eda14cbcSMatt Macy 	if (osp->os_groupused_dnode.dn_type != DMU_OT_NONE) {
1277eda14cbcSMatt Macy 		ret = zio_crypt_do_dnode_hmac_updates(ctx, key->zk_version,
1278eda14cbcSMatt Macy 		    should_bswap, &osp->os_groupused_dnode);
1279eda14cbcSMatt Macy 		if (ret)
1280eda14cbcSMatt Macy 			goto error;
1281eda14cbcSMatt Macy 	}
1282eda14cbcSMatt Macy 
1283eda14cbcSMatt Macy 	if (osp->os_projectused_dnode.dn_type != DMU_OT_NONE &&
1284eda14cbcSMatt Macy 	    datalen >= OBJSET_PHYS_SIZE_V3) {
1285eda14cbcSMatt Macy 		ret = zio_crypt_do_dnode_hmac_updates(ctx, key->zk_version,
1286eda14cbcSMatt Macy 		    should_bswap, &osp->os_projectused_dnode);
1287eda14cbcSMatt Macy 		if (ret)
1288eda14cbcSMatt Macy 			goto error;
1289eda14cbcSMatt Macy 	}
1290eda14cbcSMatt Macy 
1291eda14cbcSMatt Macy 	/* store the final digest in a temporary buffer and copy what we need */
1292eda14cbcSMatt Macy 	cd.cd_length = SHA512_DIGEST_LENGTH;
1293eda14cbcSMatt Macy 	cd.cd_raw.iov_base = (char *)raw_local_mac;
1294eda14cbcSMatt Macy 	cd.cd_raw.iov_len = cd.cd_length;
1295eda14cbcSMatt Macy 
1296c03c5b1cSMartin Matuska 	ret = crypto_mac_final(ctx, &cd);
1297eda14cbcSMatt Macy 	if (ret != CRYPTO_SUCCESS) {
1298eda14cbcSMatt Macy 		ret = SET_ERROR(EIO);
1299eda14cbcSMatt Macy 		goto error;
1300eda14cbcSMatt Macy 	}
1301eda14cbcSMatt Macy 
1302da5137abSMartin Matuska 	memcpy(local_mac, raw_local_mac, ZIO_OBJSET_MAC_LEN);
1303eda14cbcSMatt Macy 
1304eda14cbcSMatt Macy 	return (0);
1305eda14cbcSMatt Macy 
1306eda14cbcSMatt Macy error:
1307da5137abSMartin Matuska 	memset(portable_mac, 0, ZIO_OBJSET_MAC_LEN);
1308da5137abSMartin Matuska 	memset(local_mac, 0, ZIO_OBJSET_MAC_LEN);
1309eda14cbcSMatt Macy 	return (ret);
1310eda14cbcSMatt Macy }
1311eda14cbcSMatt Macy 
1312eda14cbcSMatt Macy static void
1313184c1b94SMartin Matuska zio_crypt_destroy_uio(zfs_uio_t *uio)
1314eda14cbcSMatt Macy {
1315eda14cbcSMatt Macy 	if (uio->uio_iov)
1316eda14cbcSMatt Macy 		kmem_free(uio->uio_iov, uio->uio_iovcnt * sizeof (iovec_t));
1317eda14cbcSMatt Macy }
1318eda14cbcSMatt Macy 
1319eda14cbcSMatt Macy /*
1320eda14cbcSMatt Macy  * This function parses an uncompressed indirect block and returns a checksum
1321eda14cbcSMatt Macy  * of all the portable fields from all of the contained bps. The portable
1322eda14cbcSMatt Macy  * fields are the MAC and all of the fields from blk_prop except for the dedup,
1323eda14cbcSMatt Macy  * checksum, and psize bits. For an explanation of the purpose of this, see
1324eda14cbcSMatt Macy  * the comment block on object set authentication.
1325eda14cbcSMatt Macy  */
1326eda14cbcSMatt Macy static int
1327eda14cbcSMatt Macy zio_crypt_do_indirect_mac_checksum_impl(boolean_t generate, void *buf,
1328eda14cbcSMatt Macy     uint_t datalen, uint64_t version, boolean_t byteswap, uint8_t *cksum)
1329eda14cbcSMatt Macy {
1330eda14cbcSMatt Macy 	blkptr_t *bp;
1331eda14cbcSMatt Macy 	int i, epb = datalen >> SPA_BLKPTRSHIFT;
1332eda14cbcSMatt Macy 	SHA2_CTX ctx;
1333eda14cbcSMatt Macy 	uint8_t digestbuf[SHA512_DIGEST_LENGTH];
1334eda14cbcSMatt Macy 
1335eda14cbcSMatt Macy 	/* checksum all of the MACs from the layer below */
1336eda14cbcSMatt Macy 	SHA2Init(SHA512, &ctx);
1337eda14cbcSMatt Macy 	for (i = 0, bp = buf; i < epb; i++, bp++) {
1338eda14cbcSMatt Macy 		zio_crypt_bp_do_indrect_checksum_updates(&ctx, version,
1339eda14cbcSMatt Macy 		    byteswap, bp);
1340eda14cbcSMatt Macy 	}
1341eda14cbcSMatt Macy 	SHA2Final(digestbuf, &ctx);
1342eda14cbcSMatt Macy 
1343eda14cbcSMatt Macy 	if (generate) {
1344da5137abSMartin Matuska 		memcpy(cksum, digestbuf, ZIO_DATA_MAC_LEN);
1345eda14cbcSMatt Macy 		return (0);
1346eda14cbcSMatt Macy 	}
1347eda14cbcSMatt Macy 
1348da5137abSMartin Matuska 	if (memcmp(digestbuf, cksum, ZIO_DATA_MAC_LEN) != 0)
1349eda14cbcSMatt Macy 		return (SET_ERROR(ECKSUM));
1350eda14cbcSMatt Macy 
1351eda14cbcSMatt Macy 	return (0);
1352eda14cbcSMatt Macy }
1353eda14cbcSMatt Macy 
1354eda14cbcSMatt Macy int
1355eda14cbcSMatt Macy zio_crypt_do_indirect_mac_checksum(boolean_t generate, void *buf,
1356eda14cbcSMatt Macy     uint_t datalen, boolean_t byteswap, uint8_t *cksum)
1357eda14cbcSMatt Macy {
1358eda14cbcSMatt Macy 	int ret;
1359eda14cbcSMatt Macy 
1360eda14cbcSMatt Macy 	/*
1361eda14cbcSMatt Macy 	 * Unfortunately, callers of this function will not always have
1362eda14cbcSMatt Macy 	 * easy access to the on-disk format version. This info is
1363eda14cbcSMatt Macy 	 * normally found in the DSL Crypto Key, but the checksum-of-MACs
1364eda14cbcSMatt Macy 	 * is expected to be verifiable even when the key isn't loaded.
1365eda14cbcSMatt Macy 	 * Here, instead of doing a ZAP lookup for the version for each
1366eda14cbcSMatt Macy 	 * zio, we simply try both existing formats.
1367eda14cbcSMatt Macy 	 */
1368eda14cbcSMatt Macy 	ret = zio_crypt_do_indirect_mac_checksum_impl(generate, buf,
1369eda14cbcSMatt Macy 	    datalen, ZIO_CRYPT_KEY_CURRENT_VERSION, byteswap, cksum);
1370eda14cbcSMatt Macy 	if (ret == ECKSUM) {
1371eda14cbcSMatt Macy 		ASSERT(!generate);
1372eda14cbcSMatt Macy 		ret = zio_crypt_do_indirect_mac_checksum_impl(generate,
1373eda14cbcSMatt Macy 		    buf, datalen, 0, byteswap, cksum);
1374eda14cbcSMatt Macy 	}
1375eda14cbcSMatt Macy 
1376eda14cbcSMatt Macy 	return (ret);
1377eda14cbcSMatt Macy }
1378eda14cbcSMatt Macy 
1379eda14cbcSMatt Macy int
1380eda14cbcSMatt Macy zio_crypt_do_indirect_mac_checksum_abd(boolean_t generate, abd_t *abd,
1381eda14cbcSMatt Macy     uint_t datalen, boolean_t byteswap, uint8_t *cksum)
1382eda14cbcSMatt Macy {
1383eda14cbcSMatt Macy 	int ret;
1384eda14cbcSMatt Macy 	void *buf;
1385eda14cbcSMatt Macy 
1386eda14cbcSMatt Macy 	buf = abd_borrow_buf_copy(abd, datalen);
1387eda14cbcSMatt Macy 	ret = zio_crypt_do_indirect_mac_checksum(generate, buf, datalen,
1388eda14cbcSMatt Macy 	    byteswap, cksum);
1389eda14cbcSMatt Macy 	abd_return_buf(abd, buf, datalen);
1390eda14cbcSMatt Macy 
1391eda14cbcSMatt Macy 	return (ret);
1392eda14cbcSMatt Macy }
1393eda14cbcSMatt Macy 
1394eda14cbcSMatt Macy /*
1395eda14cbcSMatt Macy  * Special case handling routine for encrypting / decrypting ZIL blocks.
1396eda14cbcSMatt Macy  * We do not check for the older ZIL chain because the encryption feature
1397eda14cbcSMatt Macy  * was not available before the newer ZIL chain was introduced. The goal
1398eda14cbcSMatt Macy  * here is to encrypt everything except the blkptr_t of a lr_write_t and
1399eda14cbcSMatt Macy  * the zil_chain_t header. Everything that is not encrypted is authenticated.
1400eda14cbcSMatt Macy  */
1401eda14cbcSMatt Macy static int
1402eda14cbcSMatt Macy zio_crypt_init_uios_zil(boolean_t encrypt, uint8_t *plainbuf,
1403184c1b94SMartin Matuska     uint8_t *cipherbuf, uint_t datalen, boolean_t byteswap, zfs_uio_t *puio,
1404184c1b94SMartin Matuska     zfs_uio_t *cuio, uint_t *enc_len, uint8_t **authbuf, uint_t *auth_len,
1405eda14cbcSMatt Macy     boolean_t *no_crypt)
1406eda14cbcSMatt Macy {
1407eda14cbcSMatt Macy 	int ret;
1408*525fe93dSMartin Matuska 	uint64_t txtype, lr_len, nused;
1409eda14cbcSMatt Macy 	uint_t nr_src, nr_dst, crypt_len;
1410eda14cbcSMatt Macy 	uint_t aad_len = 0, nr_iovecs = 0, total_len = 0;
1411eda14cbcSMatt Macy 	iovec_t *src_iovecs = NULL, *dst_iovecs = NULL;
1412eda14cbcSMatt Macy 	uint8_t *src, *dst, *slrp, *dlrp, *blkend, *aadp;
1413eda14cbcSMatt Macy 	zil_chain_t *zilc;
1414eda14cbcSMatt Macy 	lr_t *lr;
1415eda14cbcSMatt Macy 	uint8_t *aadbuf = zio_buf_alloc(datalen);
1416eda14cbcSMatt Macy 
1417eda14cbcSMatt Macy 	/* cipherbuf always needs an extra iovec for the MAC */
1418eda14cbcSMatt Macy 	if (encrypt) {
1419eda14cbcSMatt Macy 		src = plainbuf;
1420eda14cbcSMatt Macy 		dst = cipherbuf;
1421eda14cbcSMatt Macy 		nr_src = 0;
1422eda14cbcSMatt Macy 		nr_dst = 1;
1423eda14cbcSMatt Macy 	} else {
1424eda14cbcSMatt Macy 		src = cipherbuf;
1425eda14cbcSMatt Macy 		dst = plainbuf;
1426eda14cbcSMatt Macy 		nr_src = 1;
1427eda14cbcSMatt Macy 		nr_dst = 0;
1428eda14cbcSMatt Macy 	}
1429da5137abSMartin Matuska 	memset(dst, 0, datalen);
1430eda14cbcSMatt Macy 
1431eda14cbcSMatt Macy 	/* find the start and end record of the log block */
1432eda14cbcSMatt Macy 	zilc = (zil_chain_t *)src;
1433eda14cbcSMatt Macy 	slrp = src + sizeof (zil_chain_t);
1434eda14cbcSMatt Macy 	aadp = aadbuf;
1435*525fe93dSMartin Matuska 	nused = ((byteswap) ? BSWAP_64(zilc->zc_nused) : zilc->zc_nused);
1436*525fe93dSMartin Matuska 	ASSERT3U(nused, >=, sizeof (zil_chain_t));
1437*525fe93dSMartin Matuska 	ASSERT3U(nused, <=, datalen);
1438*525fe93dSMartin Matuska 	blkend = src + nused;
1439eda14cbcSMatt Macy 
1440eda14cbcSMatt Macy 	/* calculate the number of encrypted iovecs we will need */
1441eda14cbcSMatt Macy 	for (; slrp < blkend; slrp += lr_len) {
1442eda14cbcSMatt Macy 		lr = (lr_t *)slrp;
1443eda14cbcSMatt Macy 
1444eda14cbcSMatt Macy 		if (!byteswap) {
1445eda14cbcSMatt Macy 			txtype = lr->lrc_txtype;
1446eda14cbcSMatt Macy 			lr_len = lr->lrc_reclen;
1447eda14cbcSMatt Macy 		} else {
1448eda14cbcSMatt Macy 			txtype = BSWAP_64(lr->lrc_txtype);
1449eda14cbcSMatt Macy 			lr_len = BSWAP_64(lr->lrc_reclen);
1450eda14cbcSMatt Macy 		}
1451*525fe93dSMartin Matuska 		ASSERT3U(lr_len, >=, sizeof (lr_t));
1452*525fe93dSMartin Matuska 		ASSERT3U(lr_len, <=, blkend - slrp);
1453eda14cbcSMatt Macy 
1454eda14cbcSMatt Macy 		nr_iovecs++;
1455eda14cbcSMatt Macy 		if (txtype == TX_WRITE && lr_len != sizeof (lr_write_t))
1456eda14cbcSMatt Macy 			nr_iovecs++;
1457eda14cbcSMatt Macy 	}
1458eda14cbcSMatt Macy 
1459eda14cbcSMatt Macy 	nr_src += nr_iovecs;
1460eda14cbcSMatt Macy 	nr_dst += nr_iovecs;
1461eda14cbcSMatt Macy 
1462eda14cbcSMatt Macy 	/* allocate the iovec arrays */
1463eda14cbcSMatt Macy 	if (nr_src != 0) {
1464eda14cbcSMatt Macy 		src_iovecs = kmem_alloc(nr_src * sizeof (iovec_t), KM_SLEEP);
1465eda14cbcSMatt Macy 		if (src_iovecs == NULL) {
1466eda14cbcSMatt Macy 			ret = SET_ERROR(ENOMEM);
1467eda14cbcSMatt Macy 			goto error;
1468eda14cbcSMatt Macy 		}
1469eda14cbcSMatt Macy 	}
1470eda14cbcSMatt Macy 
1471eda14cbcSMatt Macy 	if (nr_dst != 0) {
1472eda14cbcSMatt Macy 		dst_iovecs = kmem_alloc(nr_dst * sizeof (iovec_t), KM_SLEEP);
1473eda14cbcSMatt Macy 		if (dst_iovecs == NULL) {
1474eda14cbcSMatt Macy 			ret = SET_ERROR(ENOMEM);
1475eda14cbcSMatt Macy 			goto error;
1476eda14cbcSMatt Macy 		}
1477eda14cbcSMatt Macy 	}
1478eda14cbcSMatt Macy 
1479eda14cbcSMatt Macy 	/*
1480eda14cbcSMatt Macy 	 * Copy the plain zil header over and authenticate everything except
1481eda14cbcSMatt Macy 	 * the checksum that will store our MAC. If we are writing the data
1482eda14cbcSMatt Macy 	 * the embedded checksum will not have been calculated yet, so we don't
1483eda14cbcSMatt Macy 	 * authenticate that.
1484eda14cbcSMatt Macy 	 */
1485da5137abSMartin Matuska 	memcpy(dst, src, sizeof (zil_chain_t));
1486da5137abSMartin Matuska 	memcpy(aadp, src, sizeof (zil_chain_t) - sizeof (zio_eck_t));
1487eda14cbcSMatt Macy 	aadp += sizeof (zil_chain_t) - sizeof (zio_eck_t);
1488eda14cbcSMatt Macy 	aad_len += sizeof (zil_chain_t) - sizeof (zio_eck_t);
1489eda14cbcSMatt Macy 
1490eda14cbcSMatt Macy 	/* loop over records again, filling in iovecs */
1491eda14cbcSMatt Macy 	nr_iovecs = 0;
1492eda14cbcSMatt Macy 	slrp = src + sizeof (zil_chain_t);
1493eda14cbcSMatt Macy 	dlrp = dst + sizeof (zil_chain_t);
1494eda14cbcSMatt Macy 
1495eda14cbcSMatt Macy 	for (; slrp < blkend; slrp += lr_len, dlrp += lr_len) {
1496eda14cbcSMatt Macy 		lr = (lr_t *)slrp;
1497eda14cbcSMatt Macy 
1498eda14cbcSMatt Macy 		if (!byteswap) {
1499eda14cbcSMatt Macy 			txtype = lr->lrc_txtype;
1500eda14cbcSMatt Macy 			lr_len = lr->lrc_reclen;
1501eda14cbcSMatt Macy 		} else {
1502eda14cbcSMatt Macy 			txtype = BSWAP_64(lr->lrc_txtype);
1503eda14cbcSMatt Macy 			lr_len = BSWAP_64(lr->lrc_reclen);
1504eda14cbcSMatt Macy 		}
1505eda14cbcSMatt Macy 
1506eda14cbcSMatt Macy 		/* copy the common lr_t */
1507da5137abSMartin Matuska 		memcpy(dlrp, slrp, sizeof (lr_t));
1508da5137abSMartin Matuska 		memcpy(aadp, slrp, sizeof (lr_t));
1509eda14cbcSMatt Macy 		aadp += sizeof (lr_t);
1510eda14cbcSMatt Macy 		aad_len += sizeof (lr_t);
1511eda14cbcSMatt Macy 
1512eda14cbcSMatt Macy 		ASSERT3P(src_iovecs, !=, NULL);
1513eda14cbcSMatt Macy 		ASSERT3P(dst_iovecs, !=, NULL);
1514eda14cbcSMatt Macy 
1515eda14cbcSMatt Macy 		/*
1516eda14cbcSMatt Macy 		 * If this is a TX_WRITE record we want to encrypt everything
1517eda14cbcSMatt Macy 		 * except the bp if exists. If the bp does exist we want to
1518eda14cbcSMatt Macy 		 * authenticate it.
1519eda14cbcSMatt Macy 		 */
1520eda14cbcSMatt Macy 		if (txtype == TX_WRITE) {
15212276e539SMartin Matuska 			const size_t o = offsetof(lr_write_t, lr_blkptr);
15222276e539SMartin Matuska 			crypt_len = o - sizeof (lr_t);
1523eda14cbcSMatt Macy 			src_iovecs[nr_iovecs].iov_base = slrp + sizeof (lr_t);
1524eda14cbcSMatt Macy 			src_iovecs[nr_iovecs].iov_len = crypt_len;
1525eda14cbcSMatt Macy 			dst_iovecs[nr_iovecs].iov_base = dlrp + sizeof (lr_t);
1526eda14cbcSMatt Macy 			dst_iovecs[nr_iovecs].iov_len = crypt_len;
1527eda14cbcSMatt Macy 
1528eda14cbcSMatt Macy 			/* copy the bp now since it will not be encrypted */
15292276e539SMartin Matuska 			memcpy(dlrp + o, slrp + o, sizeof (blkptr_t));
15302276e539SMartin Matuska 			memcpy(aadp, slrp + o, sizeof (blkptr_t));
1531eda14cbcSMatt Macy 			aadp += sizeof (blkptr_t);
1532eda14cbcSMatt Macy 			aad_len += sizeof (blkptr_t);
1533eda14cbcSMatt Macy 			nr_iovecs++;
1534eda14cbcSMatt Macy 			total_len += crypt_len;
1535eda14cbcSMatt Macy 
1536eda14cbcSMatt Macy 			if (lr_len != sizeof (lr_write_t)) {
1537eda14cbcSMatt Macy 				crypt_len = lr_len - sizeof (lr_write_t);
1538eda14cbcSMatt Macy 				src_iovecs[nr_iovecs].iov_base =
1539eda14cbcSMatt Macy 				    slrp + sizeof (lr_write_t);
1540eda14cbcSMatt Macy 				src_iovecs[nr_iovecs].iov_len = crypt_len;
1541eda14cbcSMatt Macy 				dst_iovecs[nr_iovecs].iov_base =
1542eda14cbcSMatt Macy 				    dlrp + sizeof (lr_write_t);
1543eda14cbcSMatt Macy 				dst_iovecs[nr_iovecs].iov_len = crypt_len;
1544eda14cbcSMatt Macy 				nr_iovecs++;
1545eda14cbcSMatt Macy 				total_len += crypt_len;
1546eda14cbcSMatt Macy 			}
15472276e539SMartin Matuska 		} else if (txtype == TX_CLONE_RANGE) {
15482276e539SMartin Matuska 			const size_t o = offsetof(lr_clone_range_t, lr_nbps);
15492276e539SMartin Matuska 			crypt_len = o - sizeof (lr_t);
15502276e539SMartin Matuska 			src_iovecs[nr_iovecs].iov_base = slrp + sizeof (lr_t);
15512276e539SMartin Matuska 			src_iovecs[nr_iovecs].iov_len = crypt_len;
15522276e539SMartin Matuska 			dst_iovecs[nr_iovecs].iov_base = dlrp + sizeof (lr_t);
15532276e539SMartin Matuska 			dst_iovecs[nr_iovecs].iov_len = crypt_len;
15542276e539SMartin Matuska 
15552276e539SMartin Matuska 			/* copy the bps now since they will not be encrypted */
15562276e539SMartin Matuska 			memcpy(dlrp + o, slrp + o, lr_len - o);
15572276e539SMartin Matuska 			memcpy(aadp, slrp + o, lr_len - o);
15582276e539SMartin Matuska 			aadp += lr_len - o;
15592276e539SMartin Matuska 			aad_len += lr_len - o;
15602276e539SMartin Matuska 			nr_iovecs++;
15612276e539SMartin Matuska 			total_len += crypt_len;
1562eda14cbcSMatt Macy 		} else {
1563eda14cbcSMatt Macy 			crypt_len = lr_len - sizeof (lr_t);
1564eda14cbcSMatt Macy 			src_iovecs[nr_iovecs].iov_base = slrp + sizeof (lr_t);
1565eda14cbcSMatt Macy 			src_iovecs[nr_iovecs].iov_len = crypt_len;
1566eda14cbcSMatt Macy 			dst_iovecs[nr_iovecs].iov_base = dlrp + sizeof (lr_t);
1567eda14cbcSMatt Macy 			dst_iovecs[nr_iovecs].iov_len = crypt_len;
1568eda14cbcSMatt Macy 			nr_iovecs++;
1569eda14cbcSMatt Macy 			total_len += crypt_len;
1570eda14cbcSMatt Macy 		}
1571eda14cbcSMatt Macy 	}
1572eda14cbcSMatt Macy 
1573eda14cbcSMatt Macy 	*no_crypt = (nr_iovecs == 0);
1574eda14cbcSMatt Macy 	*enc_len = total_len;
1575eda14cbcSMatt Macy 	*authbuf = aadbuf;
1576eda14cbcSMatt Macy 	*auth_len = aad_len;
1577eda14cbcSMatt Macy 
1578eda14cbcSMatt Macy 	if (encrypt) {
1579eda14cbcSMatt Macy 		puio->uio_iov = src_iovecs;
1580eda14cbcSMatt Macy 		puio->uio_iovcnt = nr_src;
1581eda14cbcSMatt Macy 		cuio->uio_iov = dst_iovecs;
1582eda14cbcSMatt Macy 		cuio->uio_iovcnt = nr_dst;
1583eda14cbcSMatt Macy 	} else {
1584eda14cbcSMatt Macy 		puio->uio_iov = dst_iovecs;
1585eda14cbcSMatt Macy 		puio->uio_iovcnt = nr_dst;
1586eda14cbcSMatt Macy 		cuio->uio_iov = src_iovecs;
1587eda14cbcSMatt Macy 		cuio->uio_iovcnt = nr_src;
1588eda14cbcSMatt Macy 	}
1589eda14cbcSMatt Macy 
1590eda14cbcSMatt Macy 	return (0);
1591eda14cbcSMatt Macy 
1592eda14cbcSMatt Macy error:
1593eda14cbcSMatt Macy 	zio_buf_free(aadbuf, datalen);
1594eda14cbcSMatt Macy 	if (src_iovecs != NULL)
1595eda14cbcSMatt Macy 		kmem_free(src_iovecs, nr_src * sizeof (iovec_t));
1596eda14cbcSMatt Macy 	if (dst_iovecs != NULL)
1597eda14cbcSMatt Macy 		kmem_free(dst_iovecs, nr_dst * sizeof (iovec_t));
1598eda14cbcSMatt Macy 
1599eda14cbcSMatt Macy 	*enc_len = 0;
1600eda14cbcSMatt Macy 	*authbuf = NULL;
1601eda14cbcSMatt Macy 	*auth_len = 0;
1602eda14cbcSMatt Macy 	*no_crypt = B_FALSE;
1603eda14cbcSMatt Macy 	puio->uio_iov = NULL;
1604eda14cbcSMatt Macy 	puio->uio_iovcnt = 0;
1605eda14cbcSMatt Macy 	cuio->uio_iov = NULL;
1606eda14cbcSMatt Macy 	cuio->uio_iovcnt = 0;
1607eda14cbcSMatt Macy 	return (ret);
1608eda14cbcSMatt Macy }
1609eda14cbcSMatt Macy 
1610eda14cbcSMatt Macy /*
1611eda14cbcSMatt Macy  * Special case handling routine for encrypting / decrypting dnode blocks.
1612eda14cbcSMatt Macy  */
1613eda14cbcSMatt Macy static int
1614eda14cbcSMatt Macy zio_crypt_init_uios_dnode(boolean_t encrypt, uint64_t version,
1615eda14cbcSMatt Macy     uint8_t *plainbuf, uint8_t *cipherbuf, uint_t datalen, boolean_t byteswap,
1616184c1b94SMartin Matuska     zfs_uio_t *puio, zfs_uio_t *cuio, uint_t *enc_len, uint8_t **authbuf,
1617eda14cbcSMatt Macy     uint_t *auth_len, boolean_t *no_crypt)
1618eda14cbcSMatt Macy {
1619eda14cbcSMatt Macy 	int ret;
1620eda14cbcSMatt Macy 	uint_t nr_src, nr_dst, crypt_len;
1621eda14cbcSMatt Macy 	uint_t aad_len = 0, nr_iovecs = 0, total_len = 0;
1622eda14cbcSMatt Macy 	uint_t i, j, max_dnp = datalen >> DNODE_SHIFT;
1623eda14cbcSMatt Macy 	iovec_t *src_iovecs = NULL, *dst_iovecs = NULL;
1624eda14cbcSMatt Macy 	uint8_t *src, *dst, *aadp;
1625eda14cbcSMatt Macy 	dnode_phys_t *dnp, *adnp, *sdnp, *ddnp;
1626eda14cbcSMatt Macy 	uint8_t *aadbuf = zio_buf_alloc(datalen);
1627eda14cbcSMatt Macy 
1628eda14cbcSMatt Macy 	if (encrypt) {
1629eda14cbcSMatt Macy 		src = plainbuf;
1630eda14cbcSMatt Macy 		dst = cipherbuf;
1631eda14cbcSMatt Macy 		nr_src = 0;
1632eda14cbcSMatt Macy 		nr_dst = 1;
1633eda14cbcSMatt Macy 	} else {
1634eda14cbcSMatt Macy 		src = cipherbuf;
1635eda14cbcSMatt Macy 		dst = plainbuf;
1636eda14cbcSMatt Macy 		nr_src = 1;
1637eda14cbcSMatt Macy 		nr_dst = 0;
1638eda14cbcSMatt Macy 	}
1639eda14cbcSMatt Macy 
1640eda14cbcSMatt Macy 	sdnp = (dnode_phys_t *)src;
1641eda14cbcSMatt Macy 	ddnp = (dnode_phys_t *)dst;
1642eda14cbcSMatt Macy 	aadp = aadbuf;
1643eda14cbcSMatt Macy 
1644eda14cbcSMatt Macy 	/*
1645eda14cbcSMatt Macy 	 * Count the number of iovecs we will need to do the encryption by
1646eda14cbcSMatt Macy 	 * counting the number of bonus buffers that need to be encrypted.
1647eda14cbcSMatt Macy 	 */
1648eda14cbcSMatt Macy 	for (i = 0; i < max_dnp; i += sdnp[i].dn_extra_slots + 1) {
1649eda14cbcSMatt Macy 		/*
1650eda14cbcSMatt Macy 		 * This block may still be byteswapped. However, all of the
1651eda14cbcSMatt Macy 		 * values we use are either uint8_t's (for which byteswapping
1652eda14cbcSMatt Macy 		 * is a noop) or a * != 0 check, which will work regardless
1653eda14cbcSMatt Macy 		 * of whether or not we byteswap.
1654eda14cbcSMatt Macy 		 */
1655eda14cbcSMatt Macy 		if (sdnp[i].dn_type != DMU_OT_NONE &&
1656eda14cbcSMatt Macy 		    DMU_OT_IS_ENCRYPTED(sdnp[i].dn_bonustype) &&
1657eda14cbcSMatt Macy 		    sdnp[i].dn_bonuslen != 0) {
1658eda14cbcSMatt Macy 			nr_iovecs++;
1659eda14cbcSMatt Macy 		}
1660eda14cbcSMatt Macy 	}
1661eda14cbcSMatt Macy 
1662eda14cbcSMatt Macy 	nr_src += nr_iovecs;
1663eda14cbcSMatt Macy 	nr_dst += nr_iovecs;
1664eda14cbcSMatt Macy 
1665eda14cbcSMatt Macy 	if (nr_src != 0) {
1666eda14cbcSMatt Macy 		src_iovecs = kmem_alloc(nr_src * sizeof (iovec_t), KM_SLEEP);
1667eda14cbcSMatt Macy 		if (src_iovecs == NULL) {
1668eda14cbcSMatt Macy 			ret = SET_ERROR(ENOMEM);
1669eda14cbcSMatt Macy 			goto error;
1670eda14cbcSMatt Macy 		}
1671eda14cbcSMatt Macy 	}
1672eda14cbcSMatt Macy 
1673eda14cbcSMatt Macy 	if (nr_dst != 0) {
1674eda14cbcSMatt Macy 		dst_iovecs = kmem_alloc(nr_dst * sizeof (iovec_t), KM_SLEEP);
1675eda14cbcSMatt Macy 		if (dst_iovecs == NULL) {
1676eda14cbcSMatt Macy 			ret = SET_ERROR(ENOMEM);
1677eda14cbcSMatt Macy 			goto error;
1678eda14cbcSMatt Macy 		}
1679eda14cbcSMatt Macy 	}
1680eda14cbcSMatt Macy 
1681eda14cbcSMatt Macy 	nr_iovecs = 0;
1682eda14cbcSMatt Macy 
1683eda14cbcSMatt Macy 	/*
1684eda14cbcSMatt Macy 	 * Iterate through the dnodes again, this time filling in the uios
1685eda14cbcSMatt Macy 	 * we allocated earlier. We also concatenate any data we want to
1686eda14cbcSMatt Macy 	 * authenticate onto aadbuf.
1687eda14cbcSMatt Macy 	 */
1688eda14cbcSMatt Macy 	for (i = 0; i < max_dnp; i += sdnp[i].dn_extra_slots + 1) {
1689eda14cbcSMatt Macy 		dnp = &sdnp[i];
1690eda14cbcSMatt Macy 
1691eda14cbcSMatt Macy 		/* copy over the core fields and blkptrs (kept as plaintext) */
1692da5137abSMartin Matuska 		memcpy(&ddnp[i], dnp,
1693da5137abSMartin Matuska 		    (uint8_t *)DN_BONUS(dnp) - (uint8_t *)dnp);
1694eda14cbcSMatt Macy 
1695eda14cbcSMatt Macy 		if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
1696da5137abSMartin Matuska 			memcpy(DN_SPILL_BLKPTR(&ddnp[i]), DN_SPILL_BLKPTR(dnp),
1697eda14cbcSMatt Macy 			    sizeof (blkptr_t));
1698eda14cbcSMatt Macy 		}
1699eda14cbcSMatt Macy 
1700eda14cbcSMatt Macy 		/*
1701eda14cbcSMatt Macy 		 * Handle authenticated data. We authenticate everything in
1702eda14cbcSMatt Macy 		 * the dnode that can be brought over when we do a raw send.
1703eda14cbcSMatt Macy 		 * This includes all of the core fields as well as the MACs
1704eda14cbcSMatt Macy 		 * stored in the bp checksums and all of the portable bits
1705eda14cbcSMatt Macy 		 * from blk_prop. We include the dnode padding here in case it
1706eda14cbcSMatt Macy 		 * ever gets used in the future. Some dn_flags and dn_used are
1707eda14cbcSMatt Macy 		 * not portable so we mask those out values out of the
1708eda14cbcSMatt Macy 		 * authenticated data.
1709eda14cbcSMatt Macy 		 */
1710eda14cbcSMatt Macy 		crypt_len = offsetof(dnode_phys_t, dn_blkptr);
1711da5137abSMartin Matuska 		memcpy(aadp, dnp, crypt_len);
1712eda14cbcSMatt Macy 		adnp = (dnode_phys_t *)aadp;
1713eda14cbcSMatt Macy 		adnp->dn_flags &= DNODE_CRYPT_PORTABLE_FLAGS_MASK;
1714eda14cbcSMatt Macy 		adnp->dn_used = 0;
1715eda14cbcSMatt Macy 		aadp += crypt_len;
1716eda14cbcSMatt Macy 		aad_len += crypt_len;
1717eda14cbcSMatt Macy 
1718eda14cbcSMatt Macy 		for (j = 0; j < dnp->dn_nblkptr; j++) {
1719eda14cbcSMatt Macy 			zio_crypt_bp_do_aad_updates(&aadp, &aad_len,
1720eda14cbcSMatt Macy 			    version, byteswap, &dnp->dn_blkptr[j]);
1721eda14cbcSMatt Macy 		}
1722eda14cbcSMatt Macy 
1723eda14cbcSMatt Macy 		if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
1724eda14cbcSMatt Macy 			zio_crypt_bp_do_aad_updates(&aadp, &aad_len,
1725eda14cbcSMatt Macy 			    version, byteswap, DN_SPILL_BLKPTR(dnp));
1726eda14cbcSMatt Macy 		}
1727eda14cbcSMatt Macy 
1728eda14cbcSMatt Macy 		/*
1729eda14cbcSMatt Macy 		 * If this bonus buffer needs to be encrypted, we prepare an
1730eda14cbcSMatt Macy 		 * iovec_t. The encryption / decryption functions will fill
1731eda14cbcSMatt Macy 		 * this in for us with the encrypted or decrypted data.
1732eda14cbcSMatt Macy 		 * Otherwise we add the bonus buffer to the authenticated
1733eda14cbcSMatt Macy 		 * data buffer and copy it over to the destination. The
1734eda14cbcSMatt Macy 		 * encrypted iovec extends to DN_MAX_BONUS_LEN(dnp) so that
1735eda14cbcSMatt Macy 		 * we can guarantee alignment with the AES block size
1736eda14cbcSMatt Macy 		 * (128 bits).
1737eda14cbcSMatt Macy 		 */
1738eda14cbcSMatt Macy 		crypt_len = DN_MAX_BONUS_LEN(dnp);
1739eda14cbcSMatt Macy 		if (dnp->dn_type != DMU_OT_NONE &&
1740eda14cbcSMatt Macy 		    DMU_OT_IS_ENCRYPTED(dnp->dn_bonustype) &&
1741eda14cbcSMatt Macy 		    dnp->dn_bonuslen != 0) {
1742eda14cbcSMatt Macy 			ASSERT3U(nr_iovecs, <, nr_src);
1743eda14cbcSMatt Macy 			ASSERT3U(nr_iovecs, <, nr_dst);
1744eda14cbcSMatt Macy 			ASSERT3P(src_iovecs, !=, NULL);
1745eda14cbcSMatt Macy 			ASSERT3P(dst_iovecs, !=, NULL);
1746eda14cbcSMatt Macy 			src_iovecs[nr_iovecs].iov_base = DN_BONUS(dnp);
1747eda14cbcSMatt Macy 			src_iovecs[nr_iovecs].iov_len = crypt_len;
1748eda14cbcSMatt Macy 			dst_iovecs[nr_iovecs].iov_base = DN_BONUS(&ddnp[i]);
1749eda14cbcSMatt Macy 			dst_iovecs[nr_iovecs].iov_len = crypt_len;
1750eda14cbcSMatt Macy 
1751eda14cbcSMatt Macy 			nr_iovecs++;
1752eda14cbcSMatt Macy 			total_len += crypt_len;
1753eda14cbcSMatt Macy 		} else {
1754da5137abSMartin Matuska 			memcpy(DN_BONUS(&ddnp[i]), DN_BONUS(dnp), crypt_len);
1755da5137abSMartin Matuska 			memcpy(aadp, DN_BONUS(dnp), crypt_len);
1756eda14cbcSMatt Macy 			aadp += crypt_len;
1757eda14cbcSMatt Macy 			aad_len += crypt_len;
1758eda14cbcSMatt Macy 		}
1759eda14cbcSMatt Macy 	}
1760eda14cbcSMatt Macy 
1761eda14cbcSMatt Macy 	*no_crypt = (nr_iovecs == 0);
1762eda14cbcSMatt Macy 	*enc_len = total_len;
1763eda14cbcSMatt Macy 	*authbuf = aadbuf;
1764eda14cbcSMatt Macy 	*auth_len = aad_len;
1765eda14cbcSMatt Macy 
1766eda14cbcSMatt Macy 	if (encrypt) {
1767eda14cbcSMatt Macy 		puio->uio_iov = src_iovecs;
1768eda14cbcSMatt Macy 		puio->uio_iovcnt = nr_src;
1769eda14cbcSMatt Macy 		cuio->uio_iov = dst_iovecs;
1770eda14cbcSMatt Macy 		cuio->uio_iovcnt = nr_dst;
1771eda14cbcSMatt Macy 	} else {
1772eda14cbcSMatt Macy 		puio->uio_iov = dst_iovecs;
1773eda14cbcSMatt Macy 		puio->uio_iovcnt = nr_dst;
1774eda14cbcSMatt Macy 		cuio->uio_iov = src_iovecs;
1775eda14cbcSMatt Macy 		cuio->uio_iovcnt = nr_src;
1776eda14cbcSMatt Macy 	}
1777eda14cbcSMatt Macy 
1778eda14cbcSMatt Macy 	return (0);
1779eda14cbcSMatt Macy 
1780eda14cbcSMatt Macy error:
1781eda14cbcSMatt Macy 	zio_buf_free(aadbuf, datalen);
1782eda14cbcSMatt Macy 	if (src_iovecs != NULL)
1783eda14cbcSMatt Macy 		kmem_free(src_iovecs, nr_src * sizeof (iovec_t));
1784eda14cbcSMatt Macy 	if (dst_iovecs != NULL)
1785eda14cbcSMatt Macy 		kmem_free(dst_iovecs, nr_dst * sizeof (iovec_t));
1786eda14cbcSMatt Macy 
1787eda14cbcSMatt Macy 	*enc_len = 0;
1788eda14cbcSMatt Macy 	*authbuf = NULL;
1789eda14cbcSMatt Macy 	*auth_len = 0;
1790eda14cbcSMatt Macy 	*no_crypt = B_FALSE;
1791eda14cbcSMatt Macy 	puio->uio_iov = NULL;
1792eda14cbcSMatt Macy 	puio->uio_iovcnt = 0;
1793eda14cbcSMatt Macy 	cuio->uio_iov = NULL;
1794eda14cbcSMatt Macy 	cuio->uio_iovcnt = 0;
1795eda14cbcSMatt Macy 	return (ret);
1796eda14cbcSMatt Macy }
1797eda14cbcSMatt Macy 
1798eda14cbcSMatt Macy static int
1799eda14cbcSMatt Macy zio_crypt_init_uios_normal(boolean_t encrypt, uint8_t *plainbuf,
1800184c1b94SMartin Matuska     uint8_t *cipherbuf, uint_t datalen, zfs_uio_t *puio, zfs_uio_t *cuio,
1801eda14cbcSMatt Macy     uint_t *enc_len)
1802eda14cbcSMatt Macy {
1803e92ffd9bSMartin Matuska 	(void) encrypt;
1804eda14cbcSMatt Macy 	int ret;
1805eda14cbcSMatt Macy 	uint_t nr_plain = 1, nr_cipher = 2;
1806eda14cbcSMatt Macy 	iovec_t *plain_iovecs = NULL, *cipher_iovecs = NULL;
1807eda14cbcSMatt Macy 
1808eda14cbcSMatt Macy 	/* allocate the iovecs for the plain and cipher data */
1809eda14cbcSMatt Macy 	plain_iovecs = kmem_alloc(nr_plain * sizeof (iovec_t),
1810eda14cbcSMatt Macy 	    KM_SLEEP);
1811eda14cbcSMatt Macy 	if (!plain_iovecs) {
1812eda14cbcSMatt Macy 		ret = SET_ERROR(ENOMEM);
1813eda14cbcSMatt Macy 		goto error;
1814eda14cbcSMatt Macy 	}
1815eda14cbcSMatt Macy 
1816eda14cbcSMatt Macy 	cipher_iovecs = kmem_alloc(nr_cipher * sizeof (iovec_t),
1817eda14cbcSMatt Macy 	    KM_SLEEP);
1818eda14cbcSMatt Macy 	if (!cipher_iovecs) {
1819eda14cbcSMatt Macy 		ret = SET_ERROR(ENOMEM);
1820eda14cbcSMatt Macy 		goto error;
1821eda14cbcSMatt Macy 	}
1822eda14cbcSMatt Macy 
1823eda14cbcSMatt Macy 	plain_iovecs[0].iov_base = plainbuf;
1824eda14cbcSMatt Macy 	plain_iovecs[0].iov_len = datalen;
1825eda14cbcSMatt Macy 	cipher_iovecs[0].iov_base = cipherbuf;
1826eda14cbcSMatt Macy 	cipher_iovecs[0].iov_len = datalen;
1827eda14cbcSMatt Macy 
1828eda14cbcSMatt Macy 	*enc_len = datalen;
1829eda14cbcSMatt Macy 	puio->uio_iov = plain_iovecs;
1830eda14cbcSMatt Macy 	puio->uio_iovcnt = nr_plain;
1831eda14cbcSMatt Macy 	cuio->uio_iov = cipher_iovecs;
1832eda14cbcSMatt Macy 	cuio->uio_iovcnt = nr_cipher;
1833eda14cbcSMatt Macy 
1834eda14cbcSMatt Macy 	return (0);
1835eda14cbcSMatt Macy 
1836eda14cbcSMatt Macy error:
1837eda14cbcSMatt Macy 	if (plain_iovecs != NULL)
1838eda14cbcSMatt Macy 		kmem_free(plain_iovecs, nr_plain * sizeof (iovec_t));
1839eda14cbcSMatt Macy 	if (cipher_iovecs != NULL)
1840eda14cbcSMatt Macy 		kmem_free(cipher_iovecs, nr_cipher * sizeof (iovec_t));
1841eda14cbcSMatt Macy 
1842eda14cbcSMatt Macy 	*enc_len = 0;
1843eda14cbcSMatt Macy 	puio->uio_iov = NULL;
1844eda14cbcSMatt Macy 	puio->uio_iovcnt = 0;
1845eda14cbcSMatt Macy 	cuio->uio_iov = NULL;
1846eda14cbcSMatt Macy 	cuio->uio_iovcnt = 0;
1847eda14cbcSMatt Macy 	return (ret);
1848eda14cbcSMatt Macy }
1849eda14cbcSMatt Macy 
1850eda14cbcSMatt Macy /*
1851eda14cbcSMatt Macy  * This function builds up the plaintext (puio) and ciphertext (cuio) uios so
1852eda14cbcSMatt Macy  * that they can be used for encryption and decryption by zio_do_crypt_uio().
1853eda14cbcSMatt Macy  * Most blocks will use zio_crypt_init_uios_normal(), with ZIL and dnode blocks
1854eda14cbcSMatt Macy  * requiring special handling to parse out pieces that are to be encrypted. The
1855eda14cbcSMatt Macy  * authbuf is used by these special cases to store additional authenticated
1856eda14cbcSMatt Macy  * data (AAD) for the encryption modes.
1857eda14cbcSMatt Macy  */
1858eda14cbcSMatt Macy static int
1859eda14cbcSMatt Macy zio_crypt_init_uios(boolean_t encrypt, uint64_t version, dmu_object_type_t ot,
1860eda14cbcSMatt Macy     uint8_t *plainbuf, uint8_t *cipherbuf, uint_t datalen, boolean_t byteswap,
1861184c1b94SMartin Matuska     uint8_t *mac, zfs_uio_t *puio, zfs_uio_t *cuio, uint_t *enc_len,
1862184c1b94SMartin Matuska     uint8_t **authbuf, uint_t *auth_len, boolean_t *no_crypt)
1863eda14cbcSMatt Macy {
1864eda14cbcSMatt Macy 	int ret;
1865eda14cbcSMatt Macy 	iovec_t *mac_iov;
1866eda14cbcSMatt Macy 
1867eda14cbcSMatt Macy 	ASSERT(DMU_OT_IS_ENCRYPTED(ot) || ot == DMU_OT_NONE);
1868eda14cbcSMatt Macy 
1869eda14cbcSMatt Macy 	/* route to handler */
1870eda14cbcSMatt Macy 	switch (ot) {
1871eda14cbcSMatt Macy 	case DMU_OT_INTENT_LOG:
1872eda14cbcSMatt Macy 		ret = zio_crypt_init_uios_zil(encrypt, plainbuf, cipherbuf,
1873eda14cbcSMatt Macy 		    datalen, byteswap, puio, cuio, enc_len, authbuf, auth_len,
1874eda14cbcSMatt Macy 		    no_crypt);
1875eda14cbcSMatt Macy 		break;
1876eda14cbcSMatt Macy 	case DMU_OT_DNODE:
1877eda14cbcSMatt Macy 		ret = zio_crypt_init_uios_dnode(encrypt, version, plainbuf,
1878eda14cbcSMatt Macy 		    cipherbuf, datalen, byteswap, puio, cuio, enc_len, authbuf,
1879eda14cbcSMatt Macy 		    auth_len, no_crypt);
1880eda14cbcSMatt Macy 		break;
1881eda14cbcSMatt Macy 	default:
1882eda14cbcSMatt Macy 		ret = zio_crypt_init_uios_normal(encrypt, plainbuf, cipherbuf,
1883eda14cbcSMatt Macy 		    datalen, puio, cuio, enc_len);
1884eda14cbcSMatt Macy 		*authbuf = NULL;
1885eda14cbcSMatt Macy 		*auth_len = 0;
1886eda14cbcSMatt Macy 		*no_crypt = B_FALSE;
1887eda14cbcSMatt Macy 		break;
1888eda14cbcSMatt Macy 	}
1889eda14cbcSMatt Macy 
1890eda14cbcSMatt Macy 	if (ret != 0)
1891eda14cbcSMatt Macy 		goto error;
1892eda14cbcSMatt Macy 
1893eda14cbcSMatt Macy 	/* populate the uios */
1894eda14cbcSMatt Macy 	puio->uio_segflg = UIO_SYSSPACE;
1895eda14cbcSMatt Macy 	cuio->uio_segflg = UIO_SYSSPACE;
1896eda14cbcSMatt Macy 
1897eda14cbcSMatt Macy 	mac_iov = ((iovec_t *)&cuio->uio_iov[cuio->uio_iovcnt - 1]);
1898eda14cbcSMatt Macy 	mac_iov->iov_base = mac;
1899eda14cbcSMatt Macy 	mac_iov->iov_len = ZIO_DATA_MAC_LEN;
1900eda14cbcSMatt Macy 
1901eda14cbcSMatt Macy 	return (0);
1902eda14cbcSMatt Macy 
1903eda14cbcSMatt Macy error:
1904eda14cbcSMatt Macy 	return (ret);
1905eda14cbcSMatt Macy }
1906eda14cbcSMatt Macy 
1907eda14cbcSMatt Macy /*
1908eda14cbcSMatt Macy  * Primary encryption / decryption entrypoint for zio data.
1909eda14cbcSMatt Macy  */
1910eda14cbcSMatt Macy int
1911eda14cbcSMatt Macy zio_do_crypt_data(boolean_t encrypt, zio_crypt_key_t *key,
1912eda14cbcSMatt Macy     dmu_object_type_t ot, boolean_t byteswap, uint8_t *salt, uint8_t *iv,
1913eda14cbcSMatt Macy     uint8_t *mac, uint_t datalen, uint8_t *plainbuf, uint8_t *cipherbuf,
1914eda14cbcSMatt Macy     boolean_t *no_crypt)
1915eda14cbcSMatt Macy {
1916eda14cbcSMatt Macy 	int ret;
1917eda14cbcSMatt Macy 	boolean_t locked = B_FALSE;
1918eda14cbcSMatt Macy 	uint64_t crypt = key->zk_crypt;
1919eda14cbcSMatt Macy 	uint_t keydata_len = zio_crypt_table[crypt].ci_keylen;
1920eda14cbcSMatt Macy 	uint_t enc_len, auth_len;
1921184c1b94SMartin Matuska 	zfs_uio_t puio, cuio;
1922eda14cbcSMatt Macy 	uint8_t enc_keydata[MASTER_KEY_MAX_LEN];
1923eda14cbcSMatt Macy 	crypto_key_t tmp_ckey, *ckey = NULL;
1924eda14cbcSMatt Macy 	crypto_ctx_template_t tmpl;
1925eda14cbcSMatt Macy 	uint8_t *authbuf = NULL;
1926eda14cbcSMatt Macy 
1927be181ee2SMartin Matuska 	memset(&puio, 0, sizeof (puio));
1928be181ee2SMartin Matuska 	memset(&cuio, 0, sizeof (cuio));
1929be181ee2SMartin Matuska 
1930eda14cbcSMatt Macy 	/*
1931eda14cbcSMatt Macy 	 * If the needed key is the current one, just use it. Otherwise we
1932eda14cbcSMatt Macy 	 * need to generate a temporary one from the given salt + master key.
1933eda14cbcSMatt Macy 	 * If we are encrypting, we must return a copy of the current salt
1934eda14cbcSMatt Macy 	 * so that it can be stored in the blkptr_t.
1935eda14cbcSMatt Macy 	 */
1936eda14cbcSMatt Macy 	rw_enter(&key->zk_salt_lock, RW_READER);
1937eda14cbcSMatt Macy 	locked = B_TRUE;
1938eda14cbcSMatt Macy 
1939da5137abSMartin Matuska 	if (memcmp(salt, key->zk_salt, ZIO_DATA_SALT_LEN) == 0) {
1940eda14cbcSMatt Macy 		ckey = &key->zk_current_key;
1941eda14cbcSMatt Macy 		tmpl = key->zk_current_tmpl;
1942eda14cbcSMatt Macy 	} else {
1943eda14cbcSMatt Macy 		rw_exit(&key->zk_salt_lock);
1944eda14cbcSMatt Macy 		locked = B_FALSE;
1945eda14cbcSMatt Macy 
1946eda14cbcSMatt Macy 		ret = hkdf_sha512(key->zk_master_keydata, keydata_len, NULL, 0,
1947eda14cbcSMatt Macy 		    salt, ZIO_DATA_SALT_LEN, enc_keydata, keydata_len);
1948eda14cbcSMatt Macy 		if (ret != 0)
1949eda14cbcSMatt Macy 			goto error;
1950eda14cbcSMatt Macy 
1951eda14cbcSMatt Macy 		tmp_ckey.ck_data = enc_keydata;
1952eda14cbcSMatt Macy 		tmp_ckey.ck_length = CRYPTO_BYTES2BITS(keydata_len);
1953eda14cbcSMatt Macy 
1954eda14cbcSMatt Macy 		ckey = &tmp_ckey;
1955eda14cbcSMatt Macy 		tmpl = NULL;
1956eda14cbcSMatt Macy 	}
1957eda14cbcSMatt Macy 
1958eda14cbcSMatt Macy 	/*
1959eda14cbcSMatt Macy 	 * Attempt to use QAT acceleration if we can. We currently don't
1960eda14cbcSMatt Macy 	 * do this for metadnode and ZIL blocks, since they have a much
1961eda14cbcSMatt Macy 	 * more involved buffer layout and the qat_crypt() function only
1962eda14cbcSMatt Macy 	 * works in-place.
1963eda14cbcSMatt Macy 	 */
1964eda14cbcSMatt Macy 	if (qat_crypt_use_accel(datalen) &&
1965eda14cbcSMatt Macy 	    ot != DMU_OT_INTENT_LOG && ot != DMU_OT_DNODE) {
1966eda14cbcSMatt Macy 		uint8_t *srcbuf, *dstbuf;
1967eda14cbcSMatt Macy 
1968eda14cbcSMatt Macy 		if (encrypt) {
1969eda14cbcSMatt Macy 			srcbuf = plainbuf;
1970eda14cbcSMatt Macy 			dstbuf = cipherbuf;
1971eda14cbcSMatt Macy 		} else {
1972eda14cbcSMatt Macy 			srcbuf = cipherbuf;
1973eda14cbcSMatt Macy 			dstbuf = plainbuf;
1974eda14cbcSMatt Macy 		}
1975eda14cbcSMatt Macy 
1976eda14cbcSMatt Macy 		ret = qat_crypt((encrypt) ? QAT_ENCRYPT : QAT_DECRYPT, srcbuf,
1977eda14cbcSMatt Macy 		    dstbuf, NULL, 0, iv, mac, ckey, key->zk_crypt, datalen);
1978eda14cbcSMatt Macy 		if (ret == CPA_STATUS_SUCCESS) {
1979eda14cbcSMatt Macy 			if (locked) {
1980eda14cbcSMatt Macy 				rw_exit(&key->zk_salt_lock);
1981eda14cbcSMatt Macy 				locked = B_FALSE;
1982eda14cbcSMatt Macy 			}
1983eda14cbcSMatt Macy 
1984eda14cbcSMatt Macy 			return (0);
1985eda14cbcSMatt Macy 		}
1986eda14cbcSMatt Macy 		/* If the hardware implementation fails fall back to software */
1987eda14cbcSMatt Macy 	}
1988eda14cbcSMatt Macy 
1989eda14cbcSMatt Macy 	/* create uios for encryption */
1990eda14cbcSMatt Macy 	ret = zio_crypt_init_uios(encrypt, key->zk_version, ot, plainbuf,
1991eda14cbcSMatt Macy 	    cipherbuf, datalen, byteswap, mac, &puio, &cuio, &enc_len,
1992eda14cbcSMatt Macy 	    &authbuf, &auth_len, no_crypt);
1993eda14cbcSMatt Macy 	if (ret != 0)
1994eda14cbcSMatt Macy 		goto error;
1995eda14cbcSMatt Macy 
1996eda14cbcSMatt Macy 	/* perform the encryption / decryption in software */
1997eda14cbcSMatt Macy 	ret = zio_do_crypt_uio(encrypt, key->zk_crypt, ckey, tmpl, iv, enc_len,
1998eda14cbcSMatt Macy 	    &puio, &cuio, authbuf, auth_len);
1999eda14cbcSMatt Macy 	if (ret != 0)
2000eda14cbcSMatt Macy 		goto error;
2001eda14cbcSMatt Macy 
2002eda14cbcSMatt Macy 	if (locked) {
2003eda14cbcSMatt Macy 		rw_exit(&key->zk_salt_lock);
2004eda14cbcSMatt Macy 	}
2005eda14cbcSMatt Macy 
2006eda14cbcSMatt Macy 	if (authbuf != NULL)
2007eda14cbcSMatt Macy 		zio_buf_free(authbuf, datalen);
2008eda14cbcSMatt Macy 	if (ckey == &tmp_ckey)
2009da5137abSMartin Matuska 		memset(enc_keydata, 0, keydata_len);
2010eda14cbcSMatt Macy 	zio_crypt_destroy_uio(&puio);
2011eda14cbcSMatt Macy 	zio_crypt_destroy_uio(&cuio);
2012eda14cbcSMatt Macy 
2013eda14cbcSMatt Macy 	return (0);
2014eda14cbcSMatt Macy 
2015eda14cbcSMatt Macy error:
2016eda14cbcSMatt Macy 	if (locked)
2017eda14cbcSMatt Macy 		rw_exit(&key->zk_salt_lock);
2018eda14cbcSMatt Macy 	if (authbuf != NULL)
2019eda14cbcSMatt Macy 		zio_buf_free(authbuf, datalen);
2020eda14cbcSMatt Macy 	if (ckey == &tmp_ckey)
2021da5137abSMartin Matuska 		memset(enc_keydata, 0, keydata_len);
2022eda14cbcSMatt Macy 	zio_crypt_destroy_uio(&puio);
2023eda14cbcSMatt Macy 	zio_crypt_destroy_uio(&cuio);
2024eda14cbcSMatt Macy 
2025eda14cbcSMatt Macy 	return (ret);
2026eda14cbcSMatt Macy }
2027eda14cbcSMatt Macy 
2028eda14cbcSMatt Macy /*
2029eda14cbcSMatt Macy  * Simple wrapper around zio_do_crypt_data() to work with abd's instead of
2030eda14cbcSMatt Macy  * linear buffers.
2031eda14cbcSMatt Macy  */
2032eda14cbcSMatt Macy int
2033eda14cbcSMatt Macy zio_do_crypt_abd(boolean_t encrypt, zio_crypt_key_t *key, dmu_object_type_t ot,
2034eda14cbcSMatt Macy     boolean_t byteswap, uint8_t *salt, uint8_t *iv, uint8_t *mac,
2035eda14cbcSMatt Macy     uint_t datalen, abd_t *pabd, abd_t *cabd, boolean_t *no_crypt)
2036eda14cbcSMatt Macy {
2037eda14cbcSMatt Macy 	int ret;
2038eda14cbcSMatt Macy 	void *ptmp, *ctmp;
2039eda14cbcSMatt Macy 
2040eda14cbcSMatt Macy 	if (encrypt) {
2041eda14cbcSMatt Macy 		ptmp = abd_borrow_buf_copy(pabd, datalen);
2042eda14cbcSMatt Macy 		ctmp = abd_borrow_buf(cabd, datalen);
2043eda14cbcSMatt Macy 	} else {
2044eda14cbcSMatt Macy 		ptmp = abd_borrow_buf(pabd, datalen);
2045eda14cbcSMatt Macy 		ctmp = abd_borrow_buf_copy(cabd, datalen);
2046eda14cbcSMatt Macy 	}
2047eda14cbcSMatt Macy 
2048eda14cbcSMatt Macy 	ret = zio_do_crypt_data(encrypt, key, ot, byteswap, salt, iv, mac,
2049eda14cbcSMatt Macy 	    datalen, ptmp, ctmp, no_crypt);
2050eda14cbcSMatt Macy 	if (ret != 0)
2051eda14cbcSMatt Macy 		goto error;
2052eda14cbcSMatt Macy 
2053eda14cbcSMatt Macy 	if (encrypt) {
2054eda14cbcSMatt Macy 		abd_return_buf(pabd, ptmp, datalen);
2055eda14cbcSMatt Macy 		abd_return_buf_copy(cabd, ctmp, datalen);
2056eda14cbcSMatt Macy 	} else {
2057eda14cbcSMatt Macy 		abd_return_buf_copy(pabd, ptmp, datalen);
2058eda14cbcSMatt Macy 		abd_return_buf(cabd, ctmp, datalen);
2059eda14cbcSMatt Macy 	}
2060eda14cbcSMatt Macy 
2061eda14cbcSMatt Macy 	return (0);
2062eda14cbcSMatt Macy 
2063eda14cbcSMatt Macy error:
2064eda14cbcSMatt Macy 	if (encrypt) {
2065eda14cbcSMatt Macy 		abd_return_buf(pabd, ptmp, datalen);
2066eda14cbcSMatt Macy 		abd_return_buf_copy(cabd, ctmp, datalen);
2067eda14cbcSMatt Macy 	} else {
2068eda14cbcSMatt Macy 		abd_return_buf_copy(pabd, ptmp, datalen);
2069eda14cbcSMatt Macy 		abd_return_buf(cabd, ctmp, datalen);
2070eda14cbcSMatt Macy 	}
2071eda14cbcSMatt Macy 
2072eda14cbcSMatt Macy 	return (ret);
2073eda14cbcSMatt Macy }
2074eda14cbcSMatt Macy 
2075eda14cbcSMatt Macy #if defined(_KERNEL)
2076eda14cbcSMatt Macy module_param(zfs_key_max_salt_uses, ulong, 0644);
2077eda14cbcSMatt Macy MODULE_PARM_DESC(zfs_key_max_salt_uses, "Max number of times a salt value "
2078eda14cbcSMatt Macy 	"can be used for generating encryption keys before it is rotated");
2079eda14cbcSMatt Macy #endif
2080