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