4 * This file and its contents are supplied under the terms of the
5 * Common Development and Distribution License ("CDDL"), version 1.0.
6 * You may only use this file in accordance with the terms of version
9 * A full copy of the text of the CDDL should have accompanied this
10 * source. A copy of the CDDL is also available via the Internet at
11 * http://www.illumos.org/license/CDDL.
17 * Copyright (c) 2017, Datto, Inc. All rights reserved.
20 #include <sys/dsl_crypt.h>
21 #include <sys/dsl_pool.h>
24 #include <sys/dsl_dir.h>
25 #include <sys/dsl_prop.h>
26 #include <sys/spa_impl.h>
27 #include <sys/dmu_objset.h>
31 * This file's primary purpose is for managing master encryption keys in
32 * memory and on disk. For more info on how these keys are used, see the
33 * block comment in zio_crypt.c.
35 * All master keys are stored encrypted on disk in the form of the DSL
36 * Crypto Key ZAP object. The binary key data in this object is always
37 * randomly generated and is encrypted with the user's wrapping key. This
38 * layer of indirection allows the user to change their key without
39 * needing to re-encrypt the entire dataset. The ZAP also holds on to the
40 * (non-encrypted) encryption algorithm identifier, IV, and MAC needed to
41 * safely decrypt the master key. For more info on the user's key see the
42 * block comment in libzfs_crypto.c
44 * In-memory encryption keys are managed through the spa_keystore. The
45 * keystore consists of 3 AVL trees, which are as follows:
47 * The Wrapping Key Tree:
48 * The wrapping key (wkey) tree stores the user's keys that are fed into the
49 * kernel through 'zfs load-key' and related commands. Datasets inherit their
50 * parent's wkey by default, so these structures are refcounted. The wrapping
51 * keys remain in memory until they are explicitly unloaded (with
52 * "zfs unload-key"). Unloading is only possible when no datasets are using
55 * The DSL Crypto Key Tree:
56 * The DSL Crypto Keys (DCK) are the in-memory representation of decrypted
57 * master keys. They are used by the functions in zio_crypt.c to perform
58 * encryption, decryption, and authentication. Snapshots and clones of a given
59 * dataset will share a DSL Crypto Key, so they are also refcounted. Once the
60 * refcount on a key hits zero, it is immediately zeroed out and freed.
62 * The Crypto Key Mapping Tree:
63 * The zio layer needs to lookup master keys by their dataset object id. Since
64 * the DSL Crypto Keys can belong to multiple datasets, we maintain a tree of
65 * dsl_key_mapping_t's which essentially just map the dataset object id to its
66 * appropriate DSL Crypto Key. The management for creating and destroying these
67 * mappings hooks into the code for owning and disowning datasets. Usually,
68 * there will only be one active dataset owner, but there are times
69 * (particularly during dataset creation and destruction) when this may not be
70 * true or the dataset may not be initialized enough to own. As a result, this
71 * object is also refcounted.
75 dsl_wrapping_key_hold(dsl_wrapping_key_t
*wkey
, void *tag
)
77 (void) refcount_add(&wkey
->wk_refcnt
, tag
);
81 dsl_wrapping_key_rele(dsl_wrapping_key_t
*wkey
, void *tag
)
83 (void) refcount_remove(&wkey
->wk_refcnt
, tag
);
87 dsl_wrapping_key_free(dsl_wrapping_key_t
*wkey
)
89 ASSERT0(refcount_count(&wkey
->wk_refcnt
));
91 if (wkey
->wk_key
.ck_data
) {
92 bzero(wkey
->wk_key
.ck_data
,
93 CRYPTO_BITS2BYTES(wkey
->wk_key
.ck_length
));
94 kmem_free(wkey
->wk_key
.ck_data
,
95 CRYPTO_BITS2BYTES(wkey
->wk_key
.ck_length
));
98 refcount_destroy(&wkey
->wk_refcnt
);
99 kmem_free(wkey
, sizeof (dsl_wrapping_key_t
));
103 dsl_wrapping_key_create(uint8_t *wkeydata
, zfs_keyformat_t keyformat
,
104 uint64_t salt
, uint64_t iters
, dsl_wrapping_key_t
**wkey_out
)
107 dsl_wrapping_key_t
*wkey
;
109 /* allocate the wrapping key */
110 wkey
= kmem_alloc(sizeof (dsl_wrapping_key_t
), KM_SLEEP
);
112 return (SET_ERROR(ENOMEM
));
114 /* allocate and initialize the underlying crypto key */
115 wkey
->wk_key
.ck_data
= kmem_alloc(WRAPPING_KEY_LEN
, KM_SLEEP
);
116 if (!wkey
->wk_key
.ck_data
) {
121 wkey
->wk_key
.ck_format
= CRYPTO_KEY_RAW
;
122 wkey
->wk_key
.ck_length
= CRYPTO_BYTES2BITS(WRAPPING_KEY_LEN
);
123 bcopy(wkeydata
, wkey
->wk_key
.ck_data
, WRAPPING_KEY_LEN
);
125 /* initialize the rest of the struct */
126 refcount_create(&wkey
->wk_refcnt
);
127 wkey
->wk_keyformat
= keyformat
;
128 wkey
->wk_salt
= salt
;
129 wkey
->wk_iters
= iters
;
135 dsl_wrapping_key_free(wkey
);
142 dsl_crypto_params_create_nvlist(dcp_cmd_t cmd
, nvlist_t
*props
,
143 nvlist_t
*crypto_args
, dsl_crypto_params_t
**dcp_out
)
146 uint64_t crypt
= ZIO_CRYPT_INHERIT
;
147 uint64_t keyformat
= ZFS_KEYFORMAT_NONE
;
148 uint64_t salt
= 0, iters
= 0;
149 dsl_crypto_params_t
*dcp
= NULL
;
150 dsl_wrapping_key_t
*wkey
= NULL
;
151 uint8_t *wkeydata
= NULL
;
152 uint_t wkeydata_len
= 0;
153 char *keylocation
= NULL
;
155 dcp
= kmem_zalloc(sizeof (dsl_crypto_params_t
), KM_SLEEP
);
157 ret
= SET_ERROR(ENOMEM
);
163 /* get relevant arguments from the nvlists */
165 (void) nvlist_lookup_uint64(props
,
166 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), &crypt
);
167 (void) nvlist_lookup_uint64(props
,
168 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), &keyformat
);
169 (void) nvlist_lookup_string(props
,
170 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), &keylocation
);
171 (void) nvlist_lookup_uint64(props
,
172 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), &salt
);
173 (void) nvlist_lookup_uint64(props
,
174 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), &iters
);
176 dcp
->cp_crypt
= crypt
;
179 if (crypto_args
!= NULL
) {
180 (void) nvlist_lookup_uint8_array(crypto_args
, "wkeydata",
181 &wkeydata
, &wkeydata_len
);
184 /* check for valid command */
185 if (dcp
->cp_cmd
>= DCP_CMD_MAX
) {
186 ret
= SET_ERROR(EINVAL
);
192 /* check for valid crypt */
193 if (dcp
->cp_crypt
>= ZIO_CRYPT_FUNCTIONS
) {
194 ret
= SET_ERROR(EINVAL
);
197 dcp
->cp_crypt
= crypt
;
200 /* check for valid keyformat */
201 if (keyformat
>= ZFS_KEYFORMAT_FORMATS
) {
202 ret
= SET_ERROR(EINVAL
);
206 /* check for a valid keylocation (of any kind) and copy it in */
207 if (keylocation
!= NULL
) {
208 if (!zfs_prop_valid_keylocation(keylocation
, B_FALSE
)) {
209 ret
= SET_ERROR(EINVAL
);
213 dcp
->cp_keylocation
= spa_strdup(keylocation
);
216 /* check wrapping key length, if given */
217 if (wkeydata
!= NULL
&& wkeydata_len
!= WRAPPING_KEY_LEN
) {
218 ret
= SET_ERROR(EINVAL
);
222 /* if the user asked for the deault crypt, determine that now */
223 if (dcp
->cp_crypt
== ZIO_CRYPT_ON
)
224 dcp
->cp_crypt
= ZIO_CRYPT_ON_VALUE
;
226 /* create the wrapping key from the raw data */
227 if (wkeydata
!= NULL
) {
228 /* create the wrapping key with the verified parameters */
229 ret
= dsl_wrapping_key_create(wkeydata
, keyformat
, salt
,
238 * Remove the encryption properties from the nvlist since they are not
239 * maintained through the DSL.
241 (void) nvlist_remove_all(props
, zfs_prop_to_name(ZFS_PROP_ENCRYPTION
));
242 (void) nvlist_remove_all(props
, zfs_prop_to_name(ZFS_PROP_KEYFORMAT
));
243 (void) nvlist_remove_all(props
, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
));
244 (void) nvlist_remove_all(props
,
245 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
));
253 dsl_wrapping_key_free(wkey
);
255 kmem_free(dcp
, sizeof (dsl_crypto_params_t
));
262 dsl_crypto_params_free(dsl_crypto_params_t
*dcp
, boolean_t unload
)
267 if (dcp
->cp_keylocation
!= NULL
)
268 spa_strfree(dcp
->cp_keylocation
);
269 if (unload
&& dcp
->cp_wkey
!= NULL
)
270 dsl_wrapping_key_free(dcp
->cp_wkey
);
272 kmem_free(dcp
, sizeof (dsl_crypto_params_t
));
276 spa_crypto_key_compare(const void *a
, const void *b
)
278 const dsl_crypto_key_t
*dcka
= a
;
279 const dsl_crypto_key_t
*dckb
= b
;
281 if (dcka
->dck_obj
< dckb
->dck_obj
)
283 if (dcka
->dck_obj
> dckb
->dck_obj
)
289 spa_key_mapping_compare(const void *a
, const void *b
)
291 const dsl_key_mapping_t
*kma
= a
;
292 const dsl_key_mapping_t
*kmb
= b
;
294 if (kma
->km_dsobj
< kmb
->km_dsobj
)
296 if (kma
->km_dsobj
> kmb
->km_dsobj
)
302 spa_wkey_compare(const void *a
, const void *b
)
304 const dsl_wrapping_key_t
*wka
= a
;
305 const dsl_wrapping_key_t
*wkb
= b
;
307 if (wka
->wk_ddobj
< wkb
->wk_ddobj
)
309 if (wka
->wk_ddobj
> wkb
->wk_ddobj
)
315 spa_keystore_init(spa_keystore_t
*sk
)
317 rw_init(&sk
->sk_dk_lock
, NULL
, RW_DEFAULT
, NULL
);
318 rw_init(&sk
->sk_km_lock
, NULL
, RW_DEFAULT
, NULL
);
319 rw_init(&sk
->sk_wkeys_lock
, NULL
, RW_DEFAULT
, NULL
);
320 avl_create(&sk
->sk_dsl_keys
, spa_crypto_key_compare
,
321 sizeof (dsl_crypto_key_t
),
322 offsetof(dsl_crypto_key_t
, dck_avl_link
));
323 avl_create(&sk
->sk_key_mappings
, spa_key_mapping_compare
,
324 sizeof (dsl_key_mapping_t
),
325 offsetof(dsl_key_mapping_t
, km_avl_link
));
326 avl_create(&sk
->sk_wkeys
, spa_wkey_compare
, sizeof (dsl_wrapping_key_t
),
327 offsetof(dsl_wrapping_key_t
, wk_avl_link
));
331 spa_keystore_fini(spa_keystore_t
*sk
)
333 dsl_wrapping_key_t
*wkey
;
336 ASSERT(avl_is_empty(&sk
->sk_dsl_keys
));
337 ASSERT(avl_is_empty(&sk
->sk_key_mappings
));
339 while ((wkey
= avl_destroy_nodes(&sk
->sk_wkeys
, &cookie
)) != NULL
)
340 dsl_wrapping_key_free(wkey
);
342 avl_destroy(&sk
->sk_wkeys
);
343 avl_destroy(&sk
->sk_key_mappings
);
344 avl_destroy(&sk
->sk_dsl_keys
);
345 rw_destroy(&sk
->sk_wkeys_lock
);
346 rw_destroy(&sk
->sk_km_lock
);
347 rw_destroy(&sk
->sk_dk_lock
);
351 dsl_dir_get_encryption_root_ddobj(dsl_dir_t
*dd
, uint64_t *rddobj
)
353 if (dd
->dd_crypto_obj
== 0)
354 return (SET_ERROR(ENOENT
));
356 return (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
357 DSL_CRYPTO_KEY_ROOT_DDOBJ
, 8, 1, rddobj
));
361 dsl_dir_get_encryption_version(dsl_dir_t
*dd
, uint64_t *version
)
365 if (dd
->dd_crypto_obj
== 0)
366 return (SET_ERROR(ENOENT
));
368 /* version 0 is implied by ENOENT */
369 (void) zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
370 DSL_CRYPTO_KEY_VERSION
, 8, 1, version
);
376 dsl_dir_incompatible_encryption_version(dsl_dir_t
*dd
)
379 uint64_t version
= 0;
381 ret
= dsl_dir_get_encryption_version(dd
, &version
);
385 return (version
!= ZIO_CRYPT_KEY_CURRENT_VERSION
);
389 spa_keystore_wkey_hold_ddobj_impl(spa_t
*spa
, uint64_t ddobj
,
390 void *tag
, dsl_wrapping_key_t
**wkey_out
)
393 dsl_wrapping_key_t search_wkey
;
394 dsl_wrapping_key_t
*found_wkey
;
396 ASSERT(RW_LOCK_HELD(&spa
->spa_keystore
.sk_wkeys_lock
));
398 /* init the search wrapping key */
399 search_wkey
.wk_ddobj
= ddobj
;
401 /* lookup the wrapping key */
402 found_wkey
= avl_find(&spa
->spa_keystore
.sk_wkeys
, &search_wkey
, NULL
);
404 ret
= SET_ERROR(ENOENT
);
408 /* increment the refcount */
409 dsl_wrapping_key_hold(found_wkey
, tag
);
411 *wkey_out
= found_wkey
;
420 spa_keystore_wkey_hold_dd(spa_t
*spa
, dsl_dir_t
*dd
, void *tag
,
421 dsl_wrapping_key_t
**wkey_out
)
424 dsl_wrapping_key_t
*wkey
;
426 boolean_t locked
= B_FALSE
;
428 if (!RW_WRITE_HELD(&spa
->spa_keystore
.sk_wkeys_lock
)) {
429 rw_enter(&spa
->spa_keystore
.sk_wkeys_lock
, RW_READER
);
433 /* get the ddobj that the keylocation property was inherited from */
434 ret
= dsl_dir_get_encryption_root_ddobj(dd
, &rddobj
);
438 /* lookup the wkey in the avl tree */
439 ret
= spa_keystore_wkey_hold_ddobj_impl(spa
, rddobj
, tag
, &wkey
);
443 /* unlock the wkey tree if we locked it */
445 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
452 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
459 dsl_crypto_can_set_keylocation(const char *dsname
, const char *keylocation
)
462 dsl_dir_t
*dd
= NULL
;
463 dsl_pool_t
*dp
= NULL
;
466 /* hold the dsl dir */
467 ret
= dsl_pool_hold(dsname
, FTAG
, &dp
);
471 ret
= dsl_dir_hold(dp
, dsname
, FTAG
, &dd
, NULL
);
475 /* if dd is not encrypted, the value may only be "none" */
476 if (dd
->dd_crypto_obj
== 0) {
477 if (strcmp(keylocation
, "none") != 0) {
478 ret
= SET_ERROR(EACCES
);
486 /* check for a valid keylocation for encrypted datasets */
487 if (!zfs_prop_valid_keylocation(keylocation
, B_TRUE
)) {
488 ret
= SET_ERROR(EINVAL
);
492 /* check that this is an encryption root */
493 ret
= dsl_dir_get_encryption_root_ddobj(dd
, &rddobj
);
497 if (rddobj
!= dd
->dd_object
) {
498 ret
= SET_ERROR(EACCES
);
502 dsl_dir_rele(dd
, FTAG
);
503 dsl_pool_rele(dp
, FTAG
);
509 dsl_dir_rele(dd
, FTAG
);
511 dsl_pool_rele(dp
, FTAG
);
517 dsl_crypto_key_free(dsl_crypto_key_t
*dck
)
519 ASSERT(refcount_count(&dck
->dck_holds
) == 0);
521 /* destroy the zio_crypt_key_t */
522 zio_crypt_key_destroy(&dck
->dck_key
);
524 /* free the refcount, wrapping key, and lock */
525 refcount_destroy(&dck
->dck_holds
);
527 dsl_wrapping_key_rele(dck
->dck_wkey
, dck
);
530 kmem_free(dck
, sizeof (dsl_crypto_key_t
));
534 dsl_crypto_key_rele(dsl_crypto_key_t
*dck
, void *tag
)
536 if (refcount_remove(&dck
->dck_holds
, tag
) == 0)
537 dsl_crypto_key_free(dck
);
541 dsl_crypto_key_open(objset_t
*mos
, dsl_wrapping_key_t
*wkey
,
542 uint64_t dckobj
, void *tag
, dsl_crypto_key_t
**dck_out
)
545 uint64_t crypt
= 0, guid
= 0, version
= 0;
546 uint8_t raw_keydata
[MASTER_KEY_MAX_LEN
];
547 uint8_t raw_hmac_keydata
[SHA512_HMAC_KEYLEN
];
548 uint8_t iv
[WRAPPING_IV_LEN
];
549 uint8_t mac
[WRAPPING_MAC_LEN
];
550 dsl_crypto_key_t
*dck
;
552 /* allocate and initialize the key */
553 dck
= kmem_zalloc(sizeof (dsl_crypto_key_t
), KM_SLEEP
);
555 return (SET_ERROR(ENOMEM
));
557 /* fetch all of the values we need from the ZAP */
558 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_CRYPTO_SUITE
, 8, 1,
563 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_GUID
, 8, 1, &guid
);
567 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_MASTER_KEY
, 1,
568 MASTER_KEY_MAX_LEN
, raw_keydata
);
572 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_HMAC_KEY
, 1,
573 SHA512_HMAC_KEYLEN
, raw_hmac_keydata
);
577 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_IV
, 1, WRAPPING_IV_LEN
,
582 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_MAC
, 1, WRAPPING_MAC_LEN
,
587 /* the initial on-disk format for encryption did not have a version */
588 (void) zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_VERSION
, 8, 1, &version
);
591 * Unwrap the keys. If there is an error return EACCES to indicate
592 * an authentication failure.
594 ret
= zio_crypt_key_unwrap(&wkey
->wk_key
, crypt
, version
, guid
,
595 raw_keydata
, raw_hmac_keydata
, iv
, mac
, &dck
->dck_key
);
597 ret
= SET_ERROR(EACCES
);
601 /* finish initializing the dsl_crypto_key_t */
602 refcount_create(&dck
->dck_holds
);
603 dsl_wrapping_key_hold(wkey
, dck
);
604 dck
->dck_wkey
= wkey
;
605 dck
->dck_obj
= dckobj
;
606 refcount_add(&dck
->dck_holds
, tag
);
613 bzero(dck
, sizeof (dsl_crypto_key_t
));
614 kmem_free(dck
, sizeof (dsl_crypto_key_t
));
622 spa_keystore_dsl_key_hold_impl(spa_t
*spa
, uint64_t dckobj
, void *tag
,
623 dsl_crypto_key_t
**dck_out
)
626 dsl_crypto_key_t search_dck
;
627 dsl_crypto_key_t
*found_dck
;
629 ASSERT(RW_LOCK_HELD(&spa
->spa_keystore
.sk_dk_lock
));
631 /* init the search key */
632 search_dck
.dck_obj
= dckobj
;
634 /* find the matching key in the keystore */
635 found_dck
= avl_find(&spa
->spa_keystore
.sk_dsl_keys
, &search_dck
, NULL
);
637 ret
= SET_ERROR(ENOENT
);
641 /* increment the refcount */
642 refcount_add(&found_dck
->dck_holds
, tag
);
644 *dck_out
= found_dck
;
653 spa_keystore_dsl_key_hold_dd(spa_t
*spa
, dsl_dir_t
*dd
, void *tag
,
654 dsl_crypto_key_t
**dck_out
)
658 dsl_crypto_key_t
*dck
= NULL
;
659 dsl_wrapping_key_t
*wkey
= NULL
;
660 uint64_t dckobj
= dd
->dd_crypto_obj
;
662 rw_enter(&spa
->spa_keystore
.sk_dk_lock
, RW_WRITER
);
664 /* lookup the key in the tree of currently loaded keys */
665 ret
= spa_keystore_dsl_key_hold_impl(spa
, dckobj
, tag
, &dck
);
667 rw_exit(&spa
->spa_keystore
.sk_dk_lock
);
672 /* lookup the wrapping key from the keystore */
673 ret
= spa_keystore_wkey_hold_dd(spa
, dd
, FTAG
, &wkey
);
675 ret
= SET_ERROR(EACCES
);
679 /* read the key from disk */
680 ret
= dsl_crypto_key_open(spa
->spa_meta_objset
, wkey
, dckobj
,
686 * add the key to the keystore (this should always succeed
687 * since we made sure it didn't exist before)
689 avl_find(&spa
->spa_keystore
.sk_dsl_keys
, dck
, &where
);
690 avl_insert(&spa
->spa_keystore
.sk_dsl_keys
, dck
, where
);
692 /* release the wrapping key (the dsl key now has a reference to it) */
693 dsl_wrapping_key_rele(wkey
, FTAG
);
695 rw_exit(&spa
->spa_keystore
.sk_dk_lock
);
701 rw_exit(&spa
->spa_keystore
.sk_dk_lock
);
703 dsl_wrapping_key_rele(wkey
, FTAG
);
710 spa_keystore_dsl_key_rele(spa_t
*spa
, dsl_crypto_key_t
*dck
, void *tag
)
712 rw_enter(&spa
->spa_keystore
.sk_dk_lock
, RW_WRITER
);
714 if (refcount_remove(&dck
->dck_holds
, tag
) == 0) {
715 avl_remove(&spa
->spa_keystore
.sk_dsl_keys
, dck
);
716 dsl_crypto_key_free(dck
);
719 rw_exit(&spa
->spa_keystore
.sk_dk_lock
);
723 spa_keystore_load_wkey_impl(spa_t
*spa
, dsl_wrapping_key_t
*wkey
)
727 dsl_wrapping_key_t
*found_wkey
;
729 rw_enter(&spa
->spa_keystore
.sk_wkeys_lock
, RW_WRITER
);
731 /* insert the wrapping key into the keystore */
732 found_wkey
= avl_find(&spa
->spa_keystore
.sk_wkeys
, wkey
, &where
);
733 if (found_wkey
!= NULL
) {
734 ret
= SET_ERROR(EEXIST
);
737 avl_insert(&spa
->spa_keystore
.sk_wkeys
, wkey
, where
);
739 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
744 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
749 spa_keystore_load_wkey(const char *dsname
, dsl_crypto_params_t
*dcp
,
753 dsl_dir_t
*dd
= NULL
;
754 dsl_crypto_key_t
*dck
= NULL
;
755 dsl_wrapping_key_t
*wkey
= dcp
->cp_wkey
;
756 dsl_pool_t
*dp
= NULL
;
757 uint64_t keyformat
, salt
, iters
;
760 * We don't validate the wrapping key's keyformat, salt, or iters
761 * since they will never be needed after the DCK has been wrapped.
763 if (dcp
->cp_wkey
== NULL
||
764 dcp
->cp_cmd
!= DCP_CMD_NONE
||
765 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
||
766 dcp
->cp_keylocation
!= NULL
)
767 return (SET_ERROR(EINVAL
));
769 ret
= dsl_pool_hold(dsname
, FTAG
, &dp
);
773 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ENCRYPTION
)) {
774 ret
= (SET_ERROR(ENOTSUP
));
778 /* hold the dsl dir */
779 ret
= dsl_dir_hold(dp
, dsname
, FTAG
, &dd
, NULL
);
783 /* initialize the wkey's ddobj */
784 wkey
->wk_ddobj
= dd
->dd_object
;
786 /* verify that the wkey is correct by opening its dsl key */
787 ret
= dsl_crypto_key_open(dp
->dp_meta_objset
, wkey
,
788 dd
->dd_crypto_obj
, FTAG
, &dck
);
792 /* initialize the wkey encryption parameters from the DSL Crypto Key */
793 ret
= zap_lookup(dp
->dp_meta_objset
, dd
->dd_crypto_obj
,
794 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), 8, 1, &keyformat
);
798 ret
= zap_lookup(dp
->dp_meta_objset
, dd
->dd_crypto_obj
,
799 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 8, 1, &salt
);
803 ret
= zap_lookup(dp
->dp_meta_objset
, dd
->dd_crypto_obj
,
804 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 8, 1, &iters
);
808 ASSERT3U(keyformat
, <, ZFS_KEYFORMAT_FORMATS
);
809 ASSERT3U(keyformat
, !=, ZFS_KEYFORMAT_NONE
);
810 IMPLY(keyformat
== ZFS_KEYFORMAT_PASSPHRASE
, iters
!= 0);
811 IMPLY(keyformat
== ZFS_KEYFORMAT_PASSPHRASE
, salt
!= 0);
812 IMPLY(keyformat
!= ZFS_KEYFORMAT_PASSPHRASE
, iters
== 0);
813 IMPLY(keyformat
!= ZFS_KEYFORMAT_PASSPHRASE
, salt
== 0);
815 wkey
->wk_keyformat
= keyformat
;
816 wkey
->wk_salt
= salt
;
817 wkey
->wk_iters
= iters
;
820 * At this point we have verified the wkey and confirmed that it can
821 * be used to decrypt a DSL Crypto Key. We can simply cleanup and
822 * return if this is all the user wanted to do.
827 /* insert the wrapping key into the keystore */
828 ret
= spa_keystore_load_wkey_impl(dp
->dp_spa
, wkey
);
832 dsl_crypto_key_rele(dck
, FTAG
);
833 dsl_dir_rele(dd
, FTAG
);
834 dsl_pool_rele(dp
, FTAG
);
836 /* create any zvols under this ds */
837 zvol_create_minors(dp
->dp_spa
, dsname
, B_TRUE
);
843 dsl_crypto_key_rele(dck
, FTAG
);
845 dsl_dir_rele(dd
, FTAG
);
847 dsl_pool_rele(dp
, FTAG
);
853 spa_keystore_unload_wkey_impl(spa_t
*spa
, uint64_t ddobj
)
856 dsl_wrapping_key_t search_wkey
;
857 dsl_wrapping_key_t
*found_wkey
;
859 /* init the search wrapping key */
860 search_wkey
.wk_ddobj
= ddobj
;
862 rw_enter(&spa
->spa_keystore
.sk_wkeys_lock
, RW_WRITER
);
864 /* remove the wrapping key from the keystore */
865 found_wkey
= avl_find(&spa
->spa_keystore
.sk_wkeys
,
868 ret
= SET_ERROR(ENOENT
);
870 } else if (refcount_count(&found_wkey
->wk_refcnt
) != 0) {
871 ret
= SET_ERROR(EBUSY
);
874 avl_remove(&spa
->spa_keystore
.sk_wkeys
, found_wkey
);
876 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
878 /* free the wrapping key */
879 dsl_wrapping_key_free(found_wkey
);
884 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
889 spa_keystore_unload_wkey(const char *dsname
)
892 dsl_dir_t
*dd
= NULL
;
893 dsl_pool_t
*dp
= NULL
;
895 /* hold the dsl dir */
896 ret
= dsl_pool_hold(dsname
, FTAG
, &dp
);
900 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ENCRYPTION
)) {
901 ret
= (SET_ERROR(ENOTSUP
));
905 ret
= dsl_dir_hold(dp
, dsname
, FTAG
, &dd
, NULL
);
909 /* unload the wkey */
910 ret
= spa_keystore_unload_wkey_impl(dp
->dp_spa
, dd
->dd_object
);
914 dsl_dir_rele(dd
, FTAG
);
915 dsl_pool_rele(dp
, FTAG
);
917 /* remove any zvols under this ds */
918 zvol_remove_minors(dp
->dp_spa
, dsname
, B_TRUE
);
924 dsl_dir_rele(dd
, FTAG
);
926 dsl_pool_rele(dp
, FTAG
);
932 spa_keystore_create_mapping_impl(spa_t
*spa
, uint64_t dsobj
,
933 dsl_dir_t
*dd
, void *tag
)
937 dsl_key_mapping_t
*km
= NULL
, *found_km
;
938 boolean_t should_free
= B_FALSE
;
940 /* allocate the mapping */
941 km
= kmem_alloc(sizeof (dsl_key_mapping_t
), KM_SLEEP
);
943 return (SET_ERROR(ENOMEM
));
945 /* initialize the mapping */
946 refcount_create(&km
->km_refcnt
);
948 ret
= spa_keystore_dsl_key_hold_dd(spa
, dd
, km
, &km
->km_key
);
952 km
->km_dsobj
= dsobj
;
954 rw_enter(&spa
->spa_keystore
.sk_km_lock
, RW_WRITER
);
957 * If a mapping already exists, simply increment its refcount and
958 * cleanup the one we made. We want to allocate / free outside of
959 * the lock because this lock is also used by the zio layer to lookup
960 * key mappings. Otherwise, use the one we created. Normally, there will
961 * only be one active reference at a time (the objset owner), but there
962 * are times when there could be multiple async users.
964 found_km
= avl_find(&spa
->spa_keystore
.sk_key_mappings
, km
, &where
);
965 if (found_km
!= NULL
) {
966 should_free
= B_TRUE
;
967 refcount_add(&found_km
->km_refcnt
, tag
);
969 refcount_add(&km
->km_refcnt
, tag
);
970 avl_insert(&spa
->spa_keystore
.sk_key_mappings
, km
, where
);
973 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
976 spa_keystore_dsl_key_rele(spa
, km
->km_key
, km
);
977 refcount_destroy(&km
->km_refcnt
);
978 kmem_free(km
, sizeof (dsl_key_mapping_t
));
985 spa_keystore_dsl_key_rele(spa
, km
->km_key
, km
);
987 refcount_destroy(&km
->km_refcnt
);
988 kmem_free(km
, sizeof (dsl_key_mapping_t
));
994 spa_keystore_create_mapping(spa_t
*spa
, dsl_dataset_t
*ds
, void *tag
)
996 return (spa_keystore_create_mapping_impl(spa
, ds
->ds_object
,
1001 spa_keystore_remove_mapping(spa_t
*spa
, uint64_t dsobj
, void *tag
)
1004 dsl_key_mapping_t search_km
;
1005 dsl_key_mapping_t
*found_km
;
1006 boolean_t should_free
= B_FALSE
;
1008 /* init the search key mapping */
1009 search_km
.km_dsobj
= dsobj
;
1011 rw_enter(&spa
->spa_keystore
.sk_km_lock
, RW_WRITER
);
1013 /* find the matching mapping */
1014 found_km
= avl_find(&spa
->spa_keystore
.sk_key_mappings
,
1016 if (found_km
== NULL
) {
1017 ret
= SET_ERROR(ENOENT
);
1022 * Decrement the refcount on the mapping and remove it from the tree if
1023 * it is zero. Try to minimize time spent in this lock by deferring
1026 if (refcount_remove(&found_km
->km_refcnt
, tag
) == 0) {
1027 should_free
= B_TRUE
;
1028 avl_remove(&spa
->spa_keystore
.sk_key_mappings
, found_km
);
1031 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
1033 /* destroy the key mapping */
1035 spa_keystore_dsl_key_rele(spa
, found_km
->km_key
, found_km
);
1036 kmem_free(found_km
, sizeof (dsl_key_mapping_t
));
1042 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
1047 * This function is primarily used by the zio and arc layer to lookup
1048 * DSL Crypto Keys for encryption. Callers must release the key with
1049 * spa_keystore_dsl_key_rele(). The function may also be called with
1050 * dck_out == NULL and tag == NULL to simply check that a key exists
1051 * without getting a reference to it.
1054 spa_keystore_lookup_key(spa_t
*spa
, uint64_t dsobj
, void *tag
,
1055 dsl_crypto_key_t
**dck_out
)
1058 dsl_key_mapping_t search_km
;
1059 dsl_key_mapping_t
*found_km
;
1061 ASSERT((tag
!= NULL
&& dck_out
!= NULL
) ||
1062 (tag
== NULL
&& dck_out
== NULL
));
1064 /* init the search key mapping */
1065 search_km
.km_dsobj
= dsobj
;
1067 rw_enter(&spa
->spa_keystore
.sk_km_lock
, RW_READER
);
1069 /* remove the mapping from the tree */
1070 found_km
= avl_find(&spa
->spa_keystore
.sk_key_mappings
, &search_km
,
1072 if (found_km
== NULL
) {
1073 ret
= SET_ERROR(ENOENT
);
1077 if (found_km
&& tag
)
1078 refcount_add(&found_km
->km_key
->dck_holds
, tag
);
1080 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
1082 if (dck_out
!= NULL
)
1083 *dck_out
= found_km
->km_key
;
1087 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
1089 if (dck_out
!= NULL
)
1095 dmu_objset_check_wkey_loaded(dsl_dir_t
*dd
)
1098 dsl_wrapping_key_t
*wkey
= NULL
;
1100 ret
= spa_keystore_wkey_hold_dd(dd
->dd_pool
->dp_spa
, dd
, FTAG
,
1103 return (SET_ERROR(EACCES
));
1105 dsl_wrapping_key_rele(wkey
, FTAG
);
1110 static zfs_keystatus_t
1111 dsl_dataset_get_keystatus(dsl_dir_t
*dd
)
1113 /* check if this dd has a has a dsl key */
1114 if (dd
->dd_crypto_obj
== 0)
1115 return (ZFS_KEYSTATUS_NONE
);
1117 return (dmu_objset_check_wkey_loaded(dd
) == 0 ?
1118 ZFS_KEYSTATUS_AVAILABLE
: ZFS_KEYSTATUS_UNAVAILABLE
);
1122 dsl_dir_get_crypt(dsl_dir_t
*dd
, uint64_t *crypt
)
1124 if (dd
->dd_crypto_obj
== 0) {
1125 *crypt
= ZIO_CRYPT_OFF
;
1129 return (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
1130 DSL_CRYPTO_KEY_CRYPTO_SUITE
, 8, 1, crypt
));
1134 dsl_crypto_key_sync_impl(objset_t
*mos
, uint64_t dckobj
, uint64_t crypt
,
1135 uint64_t root_ddobj
, uint64_t guid
, uint8_t *iv
, uint8_t *mac
,
1136 uint8_t *keydata
, uint8_t *hmac_keydata
, uint64_t keyformat
,
1137 uint64_t salt
, uint64_t iters
, dmu_tx_t
*tx
)
1139 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_CRYPTO_SUITE
, 8, 1,
1141 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_ROOT_DDOBJ
, 8, 1,
1143 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_GUID
, 8, 1,
1145 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_IV
, 1, WRAPPING_IV_LEN
,
1147 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_MAC
, 1, WRAPPING_MAC_LEN
,
1149 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_MASTER_KEY
, 1,
1150 MASTER_KEY_MAX_LEN
, keydata
, tx
));
1151 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_HMAC_KEY
, 1,
1152 SHA512_HMAC_KEYLEN
, hmac_keydata
, tx
));
1153 VERIFY0(zap_update(mos
, dckobj
, zfs_prop_to_name(ZFS_PROP_KEYFORMAT
),
1154 8, 1, &keyformat
, tx
));
1155 VERIFY0(zap_update(mos
, dckobj
, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
),
1157 VERIFY0(zap_update(mos
, dckobj
, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
),
1162 dsl_crypto_key_sync(dsl_crypto_key_t
*dck
, dmu_tx_t
*tx
)
1164 zio_crypt_key_t
*key
= &dck
->dck_key
;
1165 dsl_wrapping_key_t
*wkey
= dck
->dck_wkey
;
1166 uint8_t keydata
[MASTER_KEY_MAX_LEN
];
1167 uint8_t hmac_keydata
[SHA512_HMAC_KEYLEN
];
1168 uint8_t iv
[WRAPPING_IV_LEN
];
1169 uint8_t mac
[WRAPPING_MAC_LEN
];
1171 ASSERT(dmu_tx_is_syncing(tx
));
1172 ASSERT3U(key
->zk_crypt
, <, ZIO_CRYPT_FUNCTIONS
);
1174 /* encrypt and store the keys along with the IV and MAC */
1175 VERIFY0(zio_crypt_key_wrap(&dck
->dck_wkey
->wk_key
, key
, iv
, mac
,
1176 keydata
, hmac_keydata
));
1178 /* update the ZAP with the obtained values */
1179 dsl_crypto_key_sync_impl(tx
->tx_pool
->dp_meta_objset
, dck
->dck_obj
,
1180 key
->zk_crypt
, wkey
->wk_ddobj
, key
->zk_guid
, iv
, mac
, keydata
,
1181 hmac_keydata
, wkey
->wk_keyformat
, wkey
->wk_salt
, wkey
->wk_iters
,
1185 typedef struct spa_keystore_change_key_args
{
1186 const char *skcka_dsname
;
1187 dsl_crypto_params_t
*skcka_cp
;
1188 } spa_keystore_change_key_args_t
;
1191 spa_keystore_change_key_check(void *arg
, dmu_tx_t
*tx
)
1194 dsl_dir_t
*dd
= NULL
;
1195 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1196 spa_keystore_change_key_args_t
*skcka
= arg
;
1197 dsl_crypto_params_t
*dcp
= skcka
->skcka_cp
;
1200 /* check for the encryption feature */
1201 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ENCRYPTION
)) {
1202 ret
= SET_ERROR(ENOTSUP
);
1206 /* check for valid key change command */
1207 if (dcp
->cp_cmd
!= DCP_CMD_NEW_KEY
&&
1208 dcp
->cp_cmd
!= DCP_CMD_INHERIT
&&
1209 dcp
->cp_cmd
!= DCP_CMD_FORCE_NEW_KEY
&&
1210 dcp
->cp_cmd
!= DCP_CMD_FORCE_INHERIT
) {
1211 ret
= SET_ERROR(EINVAL
);
1216 ret
= dsl_dir_hold(dp
, skcka
->skcka_dsname
, FTAG
, &dd
, NULL
);
1220 /* verify that the dataset is encrypted */
1221 if (dd
->dd_crypto_obj
== 0) {
1222 ret
= SET_ERROR(EINVAL
);
1226 /* clones must always use their origin's key */
1227 if (dsl_dir_is_clone(dd
)) {
1228 ret
= SET_ERROR(EINVAL
);
1232 /* lookup the ddobj we are inheriting the keylocation from */
1233 ret
= dsl_dir_get_encryption_root_ddobj(dd
, &rddobj
);
1237 /* Handle inheritence */
1238 if (dcp
->cp_cmd
== DCP_CMD_INHERIT
||
1239 dcp
->cp_cmd
== DCP_CMD_FORCE_INHERIT
) {
1240 /* no other encryption params should be given */
1241 if (dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
||
1242 dcp
->cp_keylocation
!= NULL
||
1243 dcp
->cp_wkey
!= NULL
) {
1244 ret
= SET_ERROR(EINVAL
);
1248 /* check that this is an encryption root */
1249 if (dd
->dd_object
!= rddobj
) {
1250 ret
= SET_ERROR(EINVAL
);
1254 /* check that the parent is encrypted */
1255 if (dd
->dd_parent
->dd_crypto_obj
== 0) {
1256 ret
= SET_ERROR(EINVAL
);
1260 /* if we are rewrapping check that both keys are loaded */
1261 if (dcp
->cp_cmd
== DCP_CMD_INHERIT
) {
1262 ret
= dmu_objset_check_wkey_loaded(dd
);
1266 ret
= dmu_objset_check_wkey_loaded(dd
->dd_parent
);
1271 dsl_dir_rele(dd
, FTAG
);
1275 /* handle forcing an encryption root without rewrapping */
1276 if (dcp
->cp_cmd
== DCP_CMD_FORCE_NEW_KEY
) {
1277 /* no other encryption params should be given */
1278 if (dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
||
1279 dcp
->cp_keylocation
!= NULL
||
1280 dcp
->cp_wkey
!= NULL
) {
1281 ret
= SET_ERROR(EINVAL
);
1285 /* check that this is not an encryption root */
1286 if (dd
->dd_object
== rddobj
) {
1287 ret
= SET_ERROR(EINVAL
);
1291 dsl_dir_rele(dd
, FTAG
);
1295 /* crypt cannot be changed after creation */
1296 if (dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
) {
1297 ret
= SET_ERROR(EINVAL
);
1301 /* we are not inheritting our parent's wkey so we need one ourselves */
1302 if (dcp
->cp_wkey
== NULL
) {
1303 ret
= SET_ERROR(EINVAL
);
1307 /* check for a valid keyformat for the new wrapping key */
1308 if (dcp
->cp_wkey
->wk_keyformat
>= ZFS_KEYFORMAT_FORMATS
||
1309 dcp
->cp_wkey
->wk_keyformat
== ZFS_KEYFORMAT_NONE
) {
1310 ret
= SET_ERROR(EINVAL
);
1315 * If this dataset is not currently an encryption root we need a new
1316 * keylocation for this dataset's new wrapping key. Otherwise we can
1317 * just keep the one we already had.
1319 if (dd
->dd_object
!= rddobj
&& dcp
->cp_keylocation
== NULL
) {
1320 ret
= SET_ERROR(EINVAL
);
1324 /* check that the keylocation is valid if it is not NULL */
1325 if (dcp
->cp_keylocation
!= NULL
&&
1326 !zfs_prop_valid_keylocation(dcp
->cp_keylocation
, B_TRUE
)) {
1327 ret
= SET_ERROR(EINVAL
);
1331 /* passphrases require pbkdf2 salt and iters */
1332 if (dcp
->cp_wkey
->wk_keyformat
== ZFS_KEYFORMAT_PASSPHRASE
) {
1333 if (dcp
->cp_wkey
->wk_salt
== 0 ||
1334 dcp
->cp_wkey
->wk_iters
< MIN_PBKDF2_ITERATIONS
) {
1335 ret
= SET_ERROR(EINVAL
);
1339 if (dcp
->cp_wkey
->wk_salt
!= 0 || dcp
->cp_wkey
->wk_iters
!= 0) {
1340 ret
= SET_ERROR(EINVAL
);
1345 /* make sure the dd's wkey is loaded */
1346 ret
= dmu_objset_check_wkey_loaded(dd
);
1350 dsl_dir_rele(dd
, FTAG
);
1356 dsl_dir_rele(dd
, FTAG
);
1363 spa_keystore_change_key_sync_impl(uint64_t rddobj
, uint64_t ddobj
,
1364 uint64_t new_rddobj
, dsl_wrapping_key_t
*wkey
, dmu_tx_t
*tx
)
1367 zap_attribute_t
*za
;
1368 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1369 dsl_dir_t
*dd
= NULL
;
1370 dsl_crypto_key_t
*dck
= NULL
;
1371 uint64_t curr_rddobj
;
1373 ASSERT(RW_WRITE_HELD(&dp
->dp_spa
->spa_keystore
.sk_wkeys_lock
));
1376 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
));
1378 /* ignore hidden dsl dirs */
1379 if (dd
->dd_myname
[0] == '$' || dd
->dd_myname
[0] == '%') {
1380 dsl_dir_rele(dd
, FTAG
);
1385 * Stop recursing if this dsl dir didn't inherit from the root
1386 * or if this dd is a clone.
1388 VERIFY0(dsl_dir_get_encryption_root_ddobj(dd
, &curr_rddobj
));
1389 if (curr_rddobj
!= rddobj
|| dsl_dir_is_clone(dd
)) {
1390 dsl_dir_rele(dd
, FTAG
);
1395 * If we don't have a wrapping key just update the dck to reflect the
1396 * new encryption root. Otherwise rewrap the entire dck and re-sync it
1400 VERIFY0(zap_update(dp
->dp_meta_objset
, dd
->dd_crypto_obj
,
1401 DSL_CRYPTO_KEY_ROOT_DDOBJ
, 8, 1, &new_rddobj
, tx
));
1403 VERIFY0(spa_keystore_dsl_key_hold_dd(dp
->dp_spa
, dd
,
1405 dsl_wrapping_key_hold(wkey
, dck
);
1406 dsl_wrapping_key_rele(dck
->dck_wkey
, dck
);
1407 dck
->dck_wkey
= wkey
;
1408 dsl_crypto_key_sync(dck
, tx
);
1409 spa_keystore_dsl_key_rele(dp
->dp_spa
, dck
, FTAG
);
1412 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
1413 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1415 /* Recurse into all child dsl dirs. */
1416 for (zap_cursor_init(zc
, dp
->dp_meta_objset
,
1417 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
1418 zap_cursor_retrieve(zc
, za
) == 0;
1419 zap_cursor_advance(zc
)) {
1420 spa_keystore_change_key_sync_impl(rddobj
,
1421 za
->za_first_integer
, new_rddobj
, wkey
, tx
);
1423 zap_cursor_fini(zc
);
1425 kmem_free(za
, sizeof (zap_attribute_t
));
1426 kmem_free(zc
, sizeof (zap_cursor_t
));
1428 dsl_dir_rele(dd
, FTAG
);
1432 spa_keystore_change_key_sync(void *arg
, dmu_tx_t
*tx
)
1436 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1437 spa_t
*spa
= dp
->dp_spa
;
1438 spa_keystore_change_key_args_t
*skcka
= arg
;
1439 dsl_crypto_params_t
*dcp
= skcka
->skcka_cp
;
1440 dsl_wrapping_key_t
*wkey
= NULL
, *found_wkey
;
1441 dsl_wrapping_key_t wkey_search
;
1442 char *keylocation
= dcp
->cp_keylocation
;
1443 uint64_t rddobj
, new_rddobj
;
1445 /* create and initialize the wrapping key */
1446 VERIFY0(dsl_dataset_hold(dp
, skcka
->skcka_dsname
, FTAG
, &ds
));
1447 ASSERT(!ds
->ds_is_snapshot
);
1449 if (dcp
->cp_cmd
== DCP_CMD_NEW_KEY
||
1450 dcp
->cp_cmd
== DCP_CMD_FORCE_NEW_KEY
) {
1452 * We are changing to a new wkey. Set additional properties
1453 * which can be sent along with this ioctl. Note that this
1454 * command can set keylocation even if it can't normally be
1455 * set via 'zfs set' due to a non-local keylocation.
1457 if (dcp
->cp_cmd
== DCP_CMD_NEW_KEY
) {
1458 wkey
= dcp
->cp_wkey
;
1459 wkey
->wk_ddobj
= ds
->ds_dir
->dd_object
;
1461 keylocation
= "prompt";
1464 if (keylocation
!= NULL
) {
1465 dsl_prop_set_sync_impl(ds
,
1466 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1467 ZPROP_SRC_LOCAL
, 1, strlen(keylocation
) + 1,
1471 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds
->ds_dir
, &rddobj
));
1472 new_rddobj
= ds
->ds_dir
->dd_object
;
1475 * We are inheritting the parent's wkey. Unset any local
1476 * keylocation and grab a reference to the wkey.
1478 if (dcp
->cp_cmd
== DCP_CMD_INHERIT
) {
1479 VERIFY0(spa_keystore_wkey_hold_dd(spa
,
1480 ds
->ds_dir
->dd_parent
, FTAG
, &wkey
));
1483 dsl_prop_set_sync_impl(ds
,
1484 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), ZPROP_SRC_NONE
,
1487 rddobj
= ds
->ds_dir
->dd_object
;
1488 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds
->ds_dir
->dd_parent
,
1493 ASSERT(dcp
->cp_cmd
== DCP_CMD_FORCE_INHERIT
||
1494 dcp
->cp_cmd
== DCP_CMD_FORCE_NEW_KEY
);
1497 rw_enter(&spa
->spa_keystore
.sk_wkeys_lock
, RW_WRITER
);
1499 /* recurse through all children and rewrap their keys */
1500 spa_keystore_change_key_sync_impl(rddobj
, ds
->ds_dir
->dd_object
,
1501 new_rddobj
, wkey
, tx
);
1504 * All references to the old wkey should be released now (if it
1505 * existed). Replace the wrapping key.
1507 wkey_search
.wk_ddobj
= ds
->ds_dir
->dd_object
;
1508 found_wkey
= avl_find(&spa
->spa_keystore
.sk_wkeys
, &wkey_search
, NULL
);
1509 if (found_wkey
!= NULL
) {
1510 ASSERT0(refcount_count(&found_wkey
->wk_refcnt
));
1511 avl_remove(&spa
->spa_keystore
.sk_wkeys
, found_wkey
);
1512 dsl_wrapping_key_free(found_wkey
);
1515 if (dcp
->cp_cmd
== DCP_CMD_NEW_KEY
) {
1516 avl_find(&spa
->spa_keystore
.sk_wkeys
, wkey
, &where
);
1517 avl_insert(&spa
->spa_keystore
.sk_wkeys
, wkey
, where
);
1518 } else if (wkey
!= NULL
) {
1519 dsl_wrapping_key_rele(wkey
, FTAG
);
1522 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
1524 dsl_dataset_rele(ds
, FTAG
);
1528 spa_keystore_change_key(const char *dsname
, dsl_crypto_params_t
*dcp
)
1530 spa_keystore_change_key_args_t skcka
;
1532 /* initialize the args struct */
1533 skcka
.skcka_dsname
= dsname
;
1534 skcka
.skcka_cp
= dcp
;
1537 * Perform the actual work in syncing context. The blocks modified
1538 * here could be calculated but it would require holding the pool
1539 * lock and tarversing all of the datasets that will have their keys
1542 return (dsl_sync_task(dsname
, spa_keystore_change_key_check
,
1543 spa_keystore_change_key_sync
, &skcka
, 15,
1544 ZFS_SPACE_CHECK_RESERVED
));
1548 dsl_dir_rename_crypt_check(dsl_dir_t
*dd
, dsl_dir_t
*newparent
)
1551 uint64_t curr_rddobj
, parent_rddobj
;
1553 if (dd
->dd_crypto_obj
== 0) {
1554 /* children of encrypted parents must be encrypted */
1555 if (newparent
->dd_crypto_obj
!= 0) {
1556 ret
= SET_ERROR(EACCES
);
1563 ret
= dsl_dir_get_encryption_root_ddobj(dd
, &curr_rddobj
);
1568 * if this is not an encryption root, we must make sure we are not
1569 * moving dd to a new encryption root
1571 if (dd
->dd_object
!= curr_rddobj
) {
1572 ret
= dsl_dir_get_encryption_root_ddobj(newparent
,
1577 if (parent_rddobj
!= curr_rddobj
) {
1578 ret
= SET_ERROR(EACCES
);
1590 * Check to make sure that a promote from targetdd to origindd will not require
1594 dsl_dataset_promote_crypt_check(dsl_dir_t
*target
, dsl_dir_t
*origin
)
1597 uint64_t rddobj
, op_rddobj
, tp_rddobj
;
1599 /* If the dataset is not encrypted we don't need to check anything */
1600 if (origin
->dd_crypto_obj
== 0)
1604 * If we are not changing the first origin snapshot in a chain
1605 * the encryption root won't change either.
1607 if (dsl_dir_is_clone(origin
))
1611 * If the origin is the encryption root we will update
1612 * the DSL Crypto Key to point to the target instead.
1614 ret
= dsl_dir_get_encryption_root_ddobj(origin
, &rddobj
);
1618 if (rddobj
== origin
->dd_object
)
1622 * The origin is inheriting its encryption root from its parent.
1623 * Check that the parent of the target has the same encryption root.
1625 ret
= dsl_dir_get_encryption_root_ddobj(origin
->dd_parent
, &op_rddobj
);
1629 ret
= dsl_dir_get_encryption_root_ddobj(target
->dd_parent
, &tp_rddobj
);
1633 if (op_rddobj
!= tp_rddobj
)
1634 return (SET_ERROR(EACCES
));
1640 dsl_dataset_promote_crypt_sync(dsl_dir_t
*target
, dsl_dir_t
*origin
,
1644 dsl_pool_t
*dp
= target
->dd_pool
;
1645 dsl_dataset_t
*targetds
;
1646 dsl_dataset_t
*originds
;
1649 if (origin
->dd_crypto_obj
== 0)
1651 if (dsl_dir_is_clone(origin
))
1654 VERIFY0(dsl_dir_get_encryption_root_ddobj(origin
, &rddobj
));
1656 if (rddobj
!= origin
->dd_object
)
1660 * If the target is being promoted to the encyrption root update the
1661 * DSL Crypto Key and keylocation to reflect that. We also need to
1662 * update the DSL Crypto Keys of all children inheritting their
1663 * encryption root to point to the new target. Otherwise, the check
1664 * function ensured that the encryption root will not change.
1666 keylocation
= kmem_alloc(ZAP_MAXVALUELEN
, KM_SLEEP
);
1668 VERIFY0(dsl_dataset_hold_obj(dp
,
1669 dsl_dir_phys(target
)->dd_head_dataset_obj
, FTAG
, &targetds
));
1670 VERIFY0(dsl_dataset_hold_obj(dp
,
1671 dsl_dir_phys(origin
)->dd_head_dataset_obj
, FTAG
, &originds
));
1673 VERIFY0(dsl_prop_get_dd(origin
, zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1674 1, ZAP_MAXVALUELEN
, keylocation
, NULL
, B_FALSE
));
1675 dsl_prop_set_sync_impl(targetds
, zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1676 ZPROP_SRC_LOCAL
, 1, strlen(keylocation
) + 1, keylocation
, tx
);
1677 dsl_prop_set_sync_impl(originds
, zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1678 ZPROP_SRC_NONE
, 0, 0, NULL
, tx
);
1680 rw_enter(&dp
->dp_spa
->spa_keystore
.sk_wkeys_lock
, RW_WRITER
);
1681 spa_keystore_change_key_sync_impl(rddobj
, origin
->dd_object
,
1682 target
->dd_object
, NULL
, tx
);
1683 rw_exit(&dp
->dp_spa
->spa_keystore
.sk_wkeys_lock
);
1685 dsl_dataset_rele(targetds
, FTAG
);
1686 dsl_dataset_rele(originds
, FTAG
);
1687 kmem_free(keylocation
, ZAP_MAXVALUELEN
);
1691 dmu_objset_clone_crypt_check(dsl_dir_t
*parentdd
, dsl_dir_t
*origindd
)
1694 uint64_t pcrypt
, crypt
;
1697 * Check that we are not making an unencrypted child of an
1700 ret
= dsl_dir_get_crypt(parentdd
, &pcrypt
);
1704 ret
= dsl_dir_get_crypt(origindd
, &crypt
);
1708 ASSERT3U(pcrypt
, !=, ZIO_CRYPT_INHERIT
);
1709 ASSERT3U(crypt
, !=, ZIO_CRYPT_INHERIT
);
1711 if (crypt
== ZIO_CRYPT_OFF
&& pcrypt
!= ZIO_CRYPT_OFF
)
1712 return (SET_ERROR(EINVAL
));
1719 dmu_objset_create_crypt_check(dsl_dir_t
*parentdd
, dsl_crypto_params_t
*dcp
)
1722 uint64_t pcrypt
, crypt
;
1724 if (dcp
->cp_cmd
!= DCP_CMD_NONE
)
1725 return (SET_ERROR(EINVAL
));
1727 if (parentdd
!= NULL
) {
1728 ret
= dsl_dir_get_crypt(parentdd
, &pcrypt
);
1732 pcrypt
= ZIO_CRYPT_OFF
;
1735 crypt
= (dcp
->cp_crypt
== ZIO_CRYPT_INHERIT
) ? pcrypt
: dcp
->cp_crypt
;
1737 ASSERT3U(pcrypt
, !=, ZIO_CRYPT_INHERIT
);
1738 ASSERT3U(crypt
, !=, ZIO_CRYPT_INHERIT
);
1741 * We can't create an unencrypted child of an encrypted parent
1742 * under any circumstances.
1744 if (crypt
== ZIO_CRYPT_OFF
&& pcrypt
!= ZIO_CRYPT_OFF
)
1745 return (SET_ERROR(EINVAL
));
1747 /* check for valid dcp with no encryption (inherited or local) */
1748 if (crypt
== ZIO_CRYPT_OFF
) {
1749 /* Must not specify encryption params */
1750 if (dcp
->cp_wkey
!= NULL
||
1751 (dcp
->cp_keylocation
!= NULL
&&
1752 strcmp(dcp
->cp_keylocation
, "none") != 0))
1753 return (SET_ERROR(EINVAL
));
1759 * We will now definitely be encrypting. Check the feature flag. When
1760 * creating the pool the caller will check this for us since we won't
1761 * technically have the fetaure activated yet.
1763 if (parentdd
!= NULL
&&
1764 !spa_feature_is_enabled(parentdd
->dd_pool
->dp_spa
,
1765 SPA_FEATURE_ENCRYPTION
)) {
1766 return (SET_ERROR(EOPNOTSUPP
));
1769 /* handle inheritence */
1770 if (dcp
->cp_wkey
== NULL
) {
1771 ASSERT3P(parentdd
, !=, NULL
);
1773 /* key must be fully unspecified */
1774 if (dcp
->cp_keylocation
!= NULL
)
1775 return (SET_ERROR(EINVAL
));
1777 /* parent must have a key to inherit */
1778 if (pcrypt
== ZIO_CRYPT_OFF
)
1779 return (SET_ERROR(EINVAL
));
1781 /* check for parent key */
1782 ret
= dmu_objset_check_wkey_loaded(parentdd
);
1789 /* At this point we should have a fully specified key. Check location */
1790 if (dcp
->cp_keylocation
== NULL
||
1791 !zfs_prop_valid_keylocation(dcp
->cp_keylocation
, B_TRUE
))
1792 return (SET_ERROR(EINVAL
));
1794 /* Must have fully specified keyformat */
1795 switch (dcp
->cp_wkey
->wk_keyformat
) {
1796 case ZFS_KEYFORMAT_HEX
:
1797 case ZFS_KEYFORMAT_RAW
:
1798 /* requires no pbkdf2 iters and salt */
1799 if (dcp
->cp_wkey
->wk_salt
!= 0 || dcp
->cp_wkey
->wk_iters
!= 0)
1800 return (SET_ERROR(EINVAL
));
1802 case ZFS_KEYFORMAT_PASSPHRASE
:
1803 /* requires pbkdf2 iters and salt */
1804 if (dcp
->cp_wkey
->wk_salt
== 0 ||
1805 dcp
->cp_wkey
->wk_iters
< MIN_PBKDF2_ITERATIONS
)
1806 return (SET_ERROR(EINVAL
));
1808 case ZFS_KEYFORMAT_NONE
:
1810 /* keyformat must be specified and valid */
1811 return (SET_ERROR(EINVAL
));
1818 dsl_dataset_create_crypt_sync(uint64_t dsobj
, dsl_dir_t
*dd
,
1819 dsl_dataset_t
*origin
, dsl_crypto_params_t
*dcp
, dmu_tx_t
*tx
)
1821 dsl_pool_t
*dp
= dd
->dd_pool
;
1823 dsl_wrapping_key_t
*wkey
;
1825 /* clones always use their origin's wrapping key */
1826 if (dsl_dir_is_clone(dd
)) {
1827 ASSERT3P(dcp
, ==, NULL
);
1830 * If this is an encrypted clone we just need to clone the
1831 * dck into dd. Zapify the dd so we can do that.
1833 if (origin
->ds_dir
->dd_crypto_obj
!= 0) {
1834 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1835 dsl_dir_zapify(dd
, tx
);
1838 dsl_crypto_key_clone_sync(origin
->ds_dir
, tx
);
1839 VERIFY0(zap_add(dp
->dp_meta_objset
, dd
->dd_object
,
1840 DD_FIELD_CRYPTO_KEY_OBJ
, sizeof (uint64_t), 1,
1841 &dd
->dd_crypto_obj
, tx
));
1848 * A NULL dcp at this point indicates this is the origin dataset
1849 * which does not have an objset to encrypt. Raw receives will handle
1850 * encryption seperately later. In both cases we can simply return.
1852 if (dcp
== NULL
|| dcp
->cp_cmd
== DCP_CMD_RAW_RECV
)
1855 crypt
= dcp
->cp_crypt
;
1856 wkey
= dcp
->cp_wkey
;
1858 /* figure out the effective crypt */
1859 if (crypt
== ZIO_CRYPT_INHERIT
&& dd
->dd_parent
!= NULL
)
1860 VERIFY0(dsl_dir_get_crypt(dd
->dd_parent
, &crypt
));
1862 /* if we aren't doing encryption just return */
1863 if (crypt
== ZIO_CRYPT_OFF
|| crypt
== ZIO_CRYPT_INHERIT
)
1866 /* zapify the dd so that we can add the crypto key obj to it */
1867 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1868 dsl_dir_zapify(dd
, tx
);
1870 /* use the new key if given or inherit from the parent */
1872 VERIFY0(spa_keystore_wkey_hold_dd(dp
->dp_spa
,
1873 dd
->dd_parent
, FTAG
, &wkey
));
1875 wkey
->wk_ddobj
= dd
->dd_object
;
1878 ASSERT3P(wkey
, !=, NULL
);
1880 /* Create or clone the DSL crypto key and activate the feature */
1881 dd
->dd_crypto_obj
= dsl_crypto_key_create_sync(crypt
, wkey
, tx
);
1882 VERIFY0(zap_add(dp
->dp_meta_objset
, dd
->dd_object
,
1883 DD_FIELD_CRYPTO_KEY_OBJ
, sizeof (uint64_t), 1, &dd
->dd_crypto_obj
,
1885 dsl_dataset_activate_feature(dsobj
, SPA_FEATURE_ENCRYPTION
, tx
);
1888 * If we inherited the wrapping key we release our reference now.
1889 * Otherwise, this is a new key and we need to load it into the
1892 if (dcp
->cp_wkey
== NULL
) {
1893 dsl_wrapping_key_rele(wkey
, FTAG
);
1895 VERIFY0(spa_keystore_load_wkey_impl(dp
->dp_spa
, wkey
));
1899 typedef struct dsl_crypto_recv_key_arg
{
1900 uint64_t dcrka_dsobj
;
1901 nvlist_t
*dcrka_nvl
;
1902 dmu_objset_type_t dcrka_ostype
;
1903 } dsl_crypto_recv_key_arg_t
;
1906 dsl_crypto_recv_key_check(void *arg
, dmu_tx_t
*tx
)
1909 objset_t
*mos
= tx
->tx_pool
->dp_meta_objset
;
1912 dsl_crypto_recv_key_arg_t
*dcrka
= arg
;
1913 nvlist_t
*nvl
= dcrka
->dcrka_nvl
;
1914 dsl_dataset_t
*ds
= NULL
;
1915 uint8_t *buf
= NULL
;
1917 uint64_t intval
, guid
, nlevels
, blksz
, ibs
, nblkptr
, maxblkid
, version
;
1918 boolean_t is_passphrase
= B_FALSE
;
1920 ret
= dsl_dataset_hold_obj(tx
->tx_pool
, dcrka
->dcrka_dsobj
, FTAG
, &ds
);
1924 ASSERT(dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
1927 * Read and check all the encryption values from the nvlist. We need
1928 * all of the fields of a DSL Crypto Key, as well as a fully specified
1931 ret
= nvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_CRYPTO_SUITE
, &intval
);
1932 if (ret
!= 0 || intval
>= ZIO_CRYPT_FUNCTIONS
||
1933 intval
<= ZIO_CRYPT_OFF
) {
1934 ret
= SET_ERROR(EINVAL
);
1938 ret
= nvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_GUID
, &intval
);
1940 ret
= SET_ERROR(EINVAL
);
1945 * If this is an incremental receive make sure the given key guid
1946 * matches the one we already have.
1948 if (ds
->ds_dir
->dd_crypto_obj
!= 0) {
1949 ret
= zap_lookup(mos
, ds
->ds_dir
->dd_crypto_obj
,
1950 DSL_CRYPTO_KEY_GUID
, 8, 1, &guid
);
1954 if (intval
!= guid
) {
1955 ret
= SET_ERROR(EACCES
);
1960 ret
= nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_MASTER_KEY
,
1962 if (ret
!= 0 || len
!= MASTER_KEY_MAX_LEN
) {
1963 ret
= SET_ERROR(EINVAL
);
1967 ret
= nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_HMAC_KEY
,
1969 if (ret
!= 0 || len
!= SHA512_HMAC_KEYLEN
) {
1970 ret
= SET_ERROR(EINVAL
);
1974 ret
= nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_IV
, &buf
, &len
);
1975 if (ret
!= 0 || len
!= WRAPPING_IV_LEN
) {
1976 ret
= SET_ERROR(EINVAL
);
1980 ret
= nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_MAC
, &buf
, &len
);
1981 if (ret
!= 0 || len
!= WRAPPING_MAC_LEN
) {
1982 ret
= SET_ERROR(EINVAL
);
1987 * We don't support receiving old on-disk formats. The version 0
1988 * implementation protected several fields in an objset that were
1989 * not always portable during a raw receive. As a result, we call
1990 * the old version an on-disk errata #3.
1992 ret
= nvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_VERSION
, &version
);
1993 if (ret
!= 0 || version
!= ZIO_CRYPT_KEY_CURRENT_VERSION
) {
1994 ret
= SET_ERROR(ENOTSUP
);
1998 ret
= nvlist_lookup_uint8_array(nvl
, "portable_mac", &buf
, &len
);
1999 if (ret
!= 0 || len
!= ZIO_OBJSET_MAC_LEN
) {
2000 ret
= SET_ERROR(EINVAL
);
2004 ret
= nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_KEYFORMAT
),
2006 if (ret
!= 0 || intval
>= ZFS_KEYFORMAT_FORMATS
||
2007 intval
== ZFS_KEYFORMAT_NONE
) {
2008 ret
= SET_ERROR(EINVAL
);
2012 is_passphrase
= (intval
== ZFS_KEYFORMAT_PASSPHRASE
);
2015 * for raw receives we allow any number of pbkdf2iters since there
2016 * won't be a chance for the user to change it.
2018 ret
= nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
),
2020 if (ret
!= 0 || (is_passphrase
== (intval
== 0))) {
2021 ret
= SET_ERROR(EINVAL
);
2025 ret
= nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
),
2027 if (ret
!= 0 || (is_passphrase
== (intval
== 0))) {
2028 ret
= SET_ERROR(EINVAL
);
2032 /* raw receives also need info about the structure of the metadnode */
2033 ret
= nvlist_lookup_uint64(nvl
, "mdn_checksum", &intval
);
2034 if (ret
!= 0 || intval
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
) {
2035 ret
= SET_ERROR(EINVAL
);
2039 ret
= nvlist_lookup_uint64(nvl
, "mdn_compress", &intval
);
2040 if (ret
!= 0 || intval
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
) {
2041 ret
= SET_ERROR(EINVAL
);
2045 ret
= nvlist_lookup_uint64(nvl
, "mdn_nlevels", &nlevels
);
2046 if (ret
!= 0 || nlevels
> DN_MAX_LEVELS
) {
2047 ret
= SET_ERROR(EINVAL
);
2051 ret
= nvlist_lookup_uint64(nvl
, "mdn_blksz", &blksz
);
2052 if (ret
!= 0 || blksz
< SPA_MINBLOCKSIZE
) {
2053 ret
= SET_ERROR(EINVAL
);
2055 } else if (blksz
> spa_maxblocksize(tx
->tx_pool
->dp_spa
)) {
2056 ret
= SET_ERROR(ENOTSUP
);
2060 ret
= nvlist_lookup_uint64(nvl
, "mdn_indblkshift", &ibs
);
2061 if (ret
!= 0 || ibs
< DN_MIN_INDBLKSHIFT
||
2062 ibs
> DN_MAX_INDBLKSHIFT
) {
2063 ret
= SET_ERROR(ENOTSUP
);
2067 ret
= nvlist_lookup_uint64(nvl
, "mdn_nblkptr", &nblkptr
);
2068 if (ret
!= 0 || nblkptr
!= DN_MAX_NBLKPTR
) {
2069 ret
= SET_ERROR(ENOTSUP
);
2073 ret
= nvlist_lookup_uint64(nvl
, "mdn_maxblkid", &maxblkid
);
2075 ret
= SET_ERROR(EINVAL
);
2079 ret
= dmu_objset_from_ds(ds
, &os
);
2084 * Useraccounting is not portable and must be done with the keys loaded.
2085 * Therefore, whenever we do any kind of receive the useraccounting
2086 * must not be present.
2088 ASSERT0(os
->os_flags
& OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
2089 ASSERT0(os
->os_flags
& OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
2091 mdn
= DMU_META_DNODE(os
);
2094 * If we already created the objset, make sure its unchangable
2095 * properties match the ones received in the nvlist.
2097 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
2098 if (!BP_IS_HOLE(dsl_dataset_get_blkptr(ds
)) &&
2099 (mdn
->dn_nlevels
!= nlevels
|| mdn
->dn_datablksz
!= blksz
||
2100 mdn
->dn_indblkshift
!= ibs
|| mdn
->dn_nblkptr
!= nblkptr
)) {
2101 ret
= SET_ERROR(EINVAL
);
2104 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
2106 dsl_dataset_rele(ds
, FTAG
);
2111 dsl_dataset_rele(ds
, FTAG
);
2116 dsl_crypto_recv_key_sync(void *arg
, dmu_tx_t
*tx
)
2118 dsl_crypto_recv_key_arg_t
*dcrka
= arg
;
2119 uint64_t dsobj
= dcrka
->dcrka_dsobj
;
2120 nvlist_t
*nvl
= dcrka
->dcrka_nvl
;
2121 dsl_pool_t
*dp
= tx
->tx_pool
;
2122 objset_t
*mos
= dp
->dp_meta_objset
;
2126 uint8_t *keydata
, *hmac_keydata
, *iv
, *mac
, *portable_mac
;
2128 uint64_t rddobj
, one
= 1;
2129 uint64_t version
= ZIO_CRYPT_KEY_CURRENT_VERSION
;
2130 uint64_t crypt
, guid
, keyformat
, iters
, salt
;
2131 uint64_t compress
, checksum
, nlevels
, blksz
, ibs
, maxblkid
;
2132 char *keylocation
= "prompt";
2134 VERIFY0(dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
2135 VERIFY0(dmu_objset_from_ds(ds
, &os
));
2136 mdn
= DMU_META_DNODE(os
);
2138 /* lookup the values we need to create the DSL Crypto Key and objset */
2139 crypt
= fnvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_CRYPTO_SUITE
);
2140 guid
= fnvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_GUID
);
2141 keyformat
= fnvlist_lookup_uint64(nvl
,
2142 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
));
2143 iters
= fnvlist_lookup_uint64(nvl
,
2144 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
));
2145 salt
= fnvlist_lookup_uint64(nvl
,
2146 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
));
2147 VERIFY0(nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_MASTER_KEY
,
2149 VERIFY0(nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_HMAC_KEY
,
2150 &hmac_keydata
, &len
));
2151 VERIFY0(nvlist_lookup_uint8_array(nvl
, "portable_mac", &portable_mac
,
2153 VERIFY0(nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_IV
, &iv
, &len
));
2154 VERIFY0(nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_MAC
, &mac
, &len
));
2155 compress
= fnvlist_lookup_uint64(nvl
, "mdn_compress");
2156 checksum
= fnvlist_lookup_uint64(nvl
, "mdn_checksum");
2157 nlevels
= fnvlist_lookup_uint64(nvl
, "mdn_nlevels");
2158 blksz
= fnvlist_lookup_uint64(nvl
, "mdn_blksz");
2159 ibs
= fnvlist_lookup_uint64(nvl
, "mdn_indblkshift");
2160 maxblkid
= fnvlist_lookup_uint64(nvl
, "mdn_maxblkid");
2162 /* if we haven't created an objset for the ds yet, do that now */
2163 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
2164 if (BP_IS_HOLE(dsl_dataset_get_blkptr(ds
))) {
2165 (void) dmu_objset_create_impl_dnstats(dp
->dp_spa
, ds
,
2166 dsl_dataset_get_blkptr(ds
), dcrka
->dcrka_ostype
, nlevels
,
2169 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
2172 * Set the portable MAC. The local MAC will always be zero since the
2173 * incoming data will all be portable and user accounting will be
2174 * deferred until the next mount. Afterwards, flag the os to be
2175 * written out raw next time.
2177 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
2178 bcopy(portable_mac
, os
->os_phys
->os_portable_mac
, ZIO_OBJSET_MAC_LEN
);
2179 bzero(os
->os_phys
->os_local_mac
, ZIO_OBJSET_MAC_LEN
);
2180 os
->os_next_write_raw
= B_TRUE
;
2182 /* set metadnode compression and checksum */
2183 mdn
->dn_compress
= compress
;
2184 mdn
->dn_checksum
= checksum
;
2186 rw_enter(&mdn
->dn_struct_rwlock
, RW_WRITER
);
2187 dnode_new_blkid(mdn
, maxblkid
, tx
, B_FALSE
);
2188 rw_exit(&mdn
->dn_struct_rwlock
);
2190 dsl_dataset_dirty(ds
, tx
);
2192 /* if this is a new dataset setup the DSL Crypto Key. */
2193 if (ds
->ds_dir
->dd_crypto_obj
== 0) {
2194 /* zapify the dsl dir so we can add the key object to it */
2195 dmu_buf_will_dirty(ds
->ds_dir
->dd_dbuf
, tx
);
2196 dsl_dir_zapify(ds
->ds_dir
, tx
);
2198 /* create the DSL Crypto Key on disk and activate the feature */
2199 ds
->ds_dir
->dd_crypto_obj
= zap_create(mos
,
2200 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
2201 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
,
2202 ds
->ds_dir
->dd_crypto_obj
, DSL_CRYPTO_KEY_REFCOUNT
,
2203 sizeof (uint64_t), 1, &one
, tx
));
2204 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
,
2205 ds
->ds_dir
->dd_crypto_obj
, DSL_CRYPTO_KEY_VERSION
,
2206 sizeof (uint64_t), 1, &version
, tx
));
2208 dsl_dataset_activate_feature(dsobj
, SPA_FEATURE_ENCRYPTION
, tx
);
2209 ds
->ds_feature_inuse
[SPA_FEATURE_ENCRYPTION
] = B_TRUE
;
2211 /* save the dd_crypto_obj on disk */
2212 VERIFY0(zap_add(mos
, ds
->ds_dir
->dd_object
,
2213 DD_FIELD_CRYPTO_KEY_OBJ
, sizeof (uint64_t), 1,
2214 &ds
->ds_dir
->dd_crypto_obj
, tx
));
2217 * Set the keylocation to prompt by default. If keylocation
2218 * has been provided via the properties, this will be overriden
2221 dsl_prop_set_sync_impl(ds
,
2222 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
2223 ZPROP_SRC_LOCAL
, 1, strlen(keylocation
) + 1,
2226 rddobj
= ds
->ds_dir
->dd_object
;
2228 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds
->ds_dir
, &rddobj
));
2231 /* sync the key data to the ZAP object on disk */
2232 dsl_crypto_key_sync_impl(mos
, ds
->ds_dir
->dd_crypto_obj
, crypt
,
2233 rddobj
, guid
, iv
, mac
, keydata
, hmac_keydata
, keyformat
, salt
,
2236 dsl_dataset_rele(ds
, FTAG
);
2240 * This function is used to sync an nvlist representing a DSL Crypto Key and
2241 * the associated encryption parameters. The key will be written exactly as is
2242 * without wrapping it.
2245 dsl_crypto_recv_key(const char *poolname
, uint64_t dsobj
,
2246 dmu_objset_type_t ostype
, nvlist_t
*nvl
)
2248 dsl_crypto_recv_key_arg_t dcrka
;
2250 dcrka
.dcrka_dsobj
= dsobj
;
2251 dcrka
.dcrka_nvl
= nvl
;
2252 dcrka
.dcrka_ostype
= ostype
;
2254 return (dsl_sync_task(poolname
, dsl_crypto_recv_key_check
,
2255 dsl_crypto_recv_key_sync
, &dcrka
, 1, ZFS_SPACE_CHECK_NORMAL
));
2259 dsl_crypto_populate_key_nvlist(dsl_dataset_t
*ds
, nvlist_t
**nvl_out
)
2265 nvlist_t
*nvl
= NULL
;
2266 uint64_t dckobj
= ds
->ds_dir
->dd_crypto_obj
;
2267 dsl_dir_t
*rdd
= NULL
;
2268 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
2269 objset_t
*mos
= dp
->dp_meta_objset
;
2270 uint64_t crypt
= 0, guid
= 0, format
= 0;
2271 uint64_t iters
= 0, salt
= 0, version
= 0;
2272 uint8_t raw_keydata
[MASTER_KEY_MAX_LEN
];
2273 uint8_t raw_hmac_keydata
[SHA512_HMAC_KEYLEN
];
2274 uint8_t iv
[WRAPPING_IV_LEN
];
2275 uint8_t mac
[WRAPPING_MAC_LEN
];
2277 ASSERT(dckobj
!= 0);
2279 VERIFY0(dmu_objset_from_ds(ds
, &os
));
2280 mdn
= DMU_META_DNODE(os
);
2282 ret
= nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
);
2286 /* lookup values from the DSL Crypto Key */
2287 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_CRYPTO_SUITE
, 8, 1,
2292 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_GUID
, 8, 1, &guid
);
2296 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_MASTER_KEY
, 1,
2297 MASTER_KEY_MAX_LEN
, raw_keydata
);
2301 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_HMAC_KEY
, 1,
2302 SHA512_HMAC_KEYLEN
, raw_hmac_keydata
);
2306 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_IV
, 1, WRAPPING_IV_LEN
,
2311 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_MAC
, 1, WRAPPING_MAC_LEN
,
2317 * We don't support raw sends of legacy on-disk formats. See the
2318 * comment in dsl_crypto_recv_key_check() for details.
2320 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_VERSION
, 8, 1, &version
);
2321 if (ret
!= 0 || version
!= ZIO_CRYPT_KEY_CURRENT_VERSION
) {
2322 dp
->dp_spa
->spa_errata
= ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
;
2323 ret
= SET_ERROR(ENOTSUP
);
2328 * Lookup wrapping key properties. An early version of the code did
2329 * not correctly add these values to the wrapping key or the DSL
2330 * Crypto Key on disk for non encryption roots, so to be safe we
2331 * always take the slightly circuitous route of looking it up from
2332 * the encryption root's key.
2334 ret
= dsl_dir_get_encryption_root_ddobj(ds
->ds_dir
, &rddobj
);
2338 dsl_pool_config_enter(dp
, FTAG
);
2340 ret
= dsl_dir_hold_obj(dp
, rddobj
, NULL
, FTAG
, &rdd
);
2344 ret
= zap_lookup(dp
->dp_meta_objset
, rdd
->dd_crypto_obj
,
2345 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), 8, 1, &format
);
2349 if (format
== ZFS_KEYFORMAT_PASSPHRASE
) {
2350 ret
= zap_lookup(dp
->dp_meta_objset
, rdd
->dd_crypto_obj
,
2351 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 8, 1, &iters
);
2355 ret
= zap_lookup(dp
->dp_meta_objset
, rdd
->dd_crypto_obj
,
2356 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 8, 1, &salt
);
2361 dsl_dir_rele(rdd
, FTAG
);
2362 dsl_pool_config_exit(dp
, FTAG
);
2364 fnvlist_add_uint64(nvl
, DSL_CRYPTO_KEY_CRYPTO_SUITE
, crypt
);
2365 fnvlist_add_uint64(nvl
, DSL_CRYPTO_KEY_GUID
, guid
);
2366 fnvlist_add_uint64(nvl
, DSL_CRYPTO_KEY_VERSION
, version
);
2367 VERIFY0(nvlist_add_uint8_array(nvl
, DSL_CRYPTO_KEY_MASTER_KEY
,
2368 raw_keydata
, MASTER_KEY_MAX_LEN
));
2369 VERIFY0(nvlist_add_uint8_array(nvl
, DSL_CRYPTO_KEY_HMAC_KEY
,
2370 raw_hmac_keydata
, SHA512_HMAC_KEYLEN
));
2371 VERIFY0(nvlist_add_uint8_array(nvl
, DSL_CRYPTO_KEY_IV
, iv
,
2373 VERIFY0(nvlist_add_uint8_array(nvl
, DSL_CRYPTO_KEY_MAC
, mac
,
2375 VERIFY0(nvlist_add_uint8_array(nvl
, "portable_mac",
2376 os
->os_phys
->os_portable_mac
, ZIO_OBJSET_MAC_LEN
));
2377 fnvlist_add_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), format
);
2378 fnvlist_add_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), iters
);
2379 fnvlist_add_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), salt
);
2380 fnvlist_add_uint64(nvl
, "mdn_checksum", mdn
->dn_checksum
);
2381 fnvlist_add_uint64(nvl
, "mdn_compress", mdn
->dn_compress
);
2382 fnvlist_add_uint64(nvl
, "mdn_nlevels", mdn
->dn_nlevels
);
2383 fnvlist_add_uint64(nvl
, "mdn_blksz", mdn
->dn_datablksz
);
2384 fnvlist_add_uint64(nvl
, "mdn_indblkshift", mdn
->dn_indblkshift
);
2385 fnvlist_add_uint64(nvl
, "mdn_nblkptr", mdn
->dn_nblkptr
);
2386 fnvlist_add_uint64(nvl
, "mdn_maxblkid", mdn
->dn_maxblkid
);
2392 dsl_pool_config_exit(dp
, FTAG
);
2395 dsl_dir_rele(rdd
, FTAG
);
2403 dsl_crypto_key_create_sync(uint64_t crypt
, dsl_wrapping_key_t
*wkey
,
2406 dsl_crypto_key_t dck
;
2407 uint64_t version
= ZIO_CRYPT_KEY_CURRENT_VERSION
;
2408 uint64_t one
= 1ULL;
2410 ASSERT(dmu_tx_is_syncing(tx
));
2411 ASSERT3U(crypt
, <, ZIO_CRYPT_FUNCTIONS
);
2412 ASSERT3U(crypt
, >, ZIO_CRYPT_OFF
);
2414 /* create the DSL Crypto Key ZAP object */
2415 dck
.dck_obj
= zap_create(tx
->tx_pool
->dp_meta_objset
,
2416 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
2418 /* fill in the key (on the stack) and sync it to disk */
2419 dck
.dck_wkey
= wkey
;
2420 VERIFY0(zio_crypt_key_init(crypt
, &dck
.dck_key
));
2422 dsl_crypto_key_sync(&dck
, tx
);
2423 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
, dck
.dck_obj
,
2424 DSL_CRYPTO_KEY_REFCOUNT
, sizeof (uint64_t), 1, &one
, tx
));
2425 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
, dck
.dck_obj
,
2426 DSL_CRYPTO_KEY_VERSION
, sizeof (uint64_t), 1, &version
, tx
));
2428 zio_crypt_key_destroy(&dck
.dck_key
);
2429 bzero(&dck
.dck_key
, sizeof (zio_crypt_key_t
));
2431 return (dck
.dck_obj
);
2435 dsl_crypto_key_clone_sync(dsl_dir_t
*origindd
, dmu_tx_t
*tx
)
2437 objset_t
*mos
= tx
->tx_pool
->dp_meta_objset
;
2439 ASSERT(dmu_tx_is_syncing(tx
));
2441 VERIFY0(zap_increment(mos
, origindd
->dd_crypto_obj
,
2442 DSL_CRYPTO_KEY_REFCOUNT
, 1, tx
));
2444 return (origindd
->dd_crypto_obj
);
2448 dsl_crypto_key_destroy_sync(uint64_t dckobj
, dmu_tx_t
*tx
)
2450 objset_t
*mos
= tx
->tx_pool
->dp_meta_objset
;
2453 /* Decrement the refcount, destroy if this is the last reference */
2454 VERIFY0(zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_REFCOUNT
,
2455 sizeof (uint64_t), 1, &refcnt
));
2458 VERIFY0(zap_increment(mos
, dckobj
, DSL_CRYPTO_KEY_REFCOUNT
,
2461 VERIFY0(zap_destroy(mos
, dckobj
, tx
));
2466 dsl_dataset_crypt_stats(dsl_dataset_t
*ds
, nvlist_t
*nv
)
2469 dsl_dir_t
*dd
= ds
->ds_dir
;
2470 dsl_dir_t
*enc_root
;
2471 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2473 if (dd
->dd_crypto_obj
== 0)
2476 intval
= dsl_dataset_get_keystatus(dd
);
2477 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_KEYSTATUS
, intval
);
2479 if (dsl_dir_get_crypt(dd
, &intval
) == 0)
2480 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_ENCRYPTION
, intval
);
2481 if (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
2482 DSL_CRYPTO_KEY_GUID
, 8, 1, &intval
) == 0) {
2483 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_KEY_GUID
, intval
);
2485 if (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
2486 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), 8, 1, &intval
) == 0) {
2487 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_KEYFORMAT
, intval
);
2489 if (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
2490 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 8, 1, &intval
) == 0) {
2491 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_PBKDF2_SALT
, intval
);
2493 if (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
2494 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 8, 1, &intval
) == 0) {
2495 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_PBKDF2_ITERS
, intval
);
2498 if (dsl_dir_get_encryption_root_ddobj(dd
, &intval
) == 0) {
2499 VERIFY0(dsl_dir_hold_obj(dd
->dd_pool
, intval
, NULL
, FTAG
,
2501 dsl_dir_name(enc_root
, buf
);
2502 dsl_dir_rele(enc_root
, FTAG
);
2503 dsl_prop_nvlist_add_string(nv
, ZFS_PROP_ENCRYPTION_ROOT
, buf
);
2508 spa_crypt_get_salt(spa_t
*spa
, uint64_t dsobj
, uint8_t *salt
)
2511 dsl_crypto_key_t
*dck
= NULL
;
2513 /* look up the key from the spa's keystore */
2514 ret
= spa_keystore_lookup_key(spa
, dsobj
, FTAG
, &dck
);
2518 ret
= zio_crypt_key_get_salt(&dck
->dck_key
, salt
);
2522 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2527 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2532 * Objset blocks are a special case for MAC generation. These blocks have 2
2533 * 256-bit MACs which are embedded within the block itself, rather than a
2534 * single 128 bit MAC. As a result, this function handles encoding and decoding
2535 * the MACs on its own, unlike other functions in this file.
2538 spa_do_crypt_objset_mac_abd(boolean_t generate
, spa_t
*spa
, uint64_t dsobj
,
2539 abd_t
*abd
, uint_t datalen
, boolean_t byteswap
)
2542 dsl_crypto_key_t
*dck
= NULL
;
2543 void *buf
= abd_borrow_buf_copy(abd
, datalen
);
2544 objset_phys_t
*osp
= buf
;
2545 uint8_t portable_mac
[ZIO_OBJSET_MAC_LEN
];
2546 uint8_t local_mac
[ZIO_OBJSET_MAC_LEN
];
2548 /* look up the key from the spa's keystore */
2549 ret
= spa_keystore_lookup_key(spa
, dsobj
, FTAG
, &dck
);
2553 /* calculate both HMACs */
2554 ret
= zio_crypt_do_objset_hmacs(&dck
->dck_key
, buf
, datalen
,
2555 byteswap
, portable_mac
, local_mac
);
2559 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2561 /* if we are generating encode the HMACs in the objset_phys_t */
2563 bcopy(portable_mac
, osp
->os_portable_mac
, ZIO_OBJSET_MAC_LEN
);
2564 bcopy(local_mac
, osp
->os_local_mac
, ZIO_OBJSET_MAC_LEN
);
2565 abd_return_buf_copy(abd
, buf
, datalen
);
2569 if (bcmp(portable_mac
, osp
->os_portable_mac
, ZIO_OBJSET_MAC_LEN
) != 0 ||
2570 bcmp(local_mac
, osp
->os_local_mac
, ZIO_OBJSET_MAC_LEN
) != 0) {
2571 abd_return_buf(abd
, buf
, datalen
);
2572 return (SET_ERROR(ECKSUM
));
2575 abd_return_buf(abd
, buf
, datalen
);
2581 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2582 abd_return_buf(abd
, buf
, datalen
);
2587 spa_do_crypt_mac_abd(boolean_t generate
, spa_t
*spa
, uint64_t dsobj
, abd_t
*abd
,
2588 uint_t datalen
, uint8_t *mac
)
2591 dsl_crypto_key_t
*dck
= NULL
;
2592 uint8_t *buf
= abd_borrow_buf_copy(abd
, datalen
);
2593 uint8_t digestbuf
[ZIO_DATA_MAC_LEN
];
2595 /* look up the key from the spa's keystore */
2596 ret
= spa_keystore_lookup_key(spa
, dsobj
, FTAG
, &dck
);
2600 /* perform the hmac */
2601 ret
= zio_crypt_do_hmac(&dck
->dck_key
, buf
, datalen
,
2602 digestbuf
, ZIO_DATA_MAC_LEN
);
2606 abd_return_buf(abd
, buf
, datalen
);
2607 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2610 * Truncate and fill in mac buffer if we were asked to generate a MAC.
2611 * Otherwise verify that the MAC matched what we expected.
2614 bcopy(digestbuf
, mac
, ZIO_DATA_MAC_LEN
);
2618 if (bcmp(digestbuf
, mac
, ZIO_DATA_MAC_LEN
) != 0)
2619 return (SET_ERROR(ECKSUM
));
2625 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2626 abd_return_buf(abd
, buf
, datalen
);
2631 * This function serves as a multiplexer for encryption and decryption of
2632 * all blocks (except the L2ARC). For encryption, it will populate the IV,
2633 * salt, MAC, and cabd (the ciphertext). On decryption it will simply use
2634 * these fields to populate pabd (the plaintext).
2637 spa_do_crypt_abd(boolean_t encrypt
, spa_t
*spa
, uint64_t dsobj
,
2638 const blkptr_t
*bp
, uint64_t txgid
, uint_t datalen
, abd_t
*pabd
,
2639 abd_t
*cabd
, uint8_t *iv
, uint8_t *mac
, uint8_t *salt
, boolean_t
*no_crypt
)
2642 dmu_object_type_t ot
= BP_GET_TYPE(bp
);
2643 dsl_crypto_key_t
*dck
= NULL
;
2644 uint8_t *plainbuf
= NULL
, *cipherbuf
= NULL
;
2646 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_ENCRYPTION
));
2647 ASSERT(!BP_IS_EMBEDDED(bp
));
2648 ASSERT(BP_IS_ENCRYPTED(bp
));
2650 /* look up the key from the spa's keystore */
2651 ret
= spa_keystore_lookup_key(spa
, dsobj
, FTAG
, &dck
);
2656 plainbuf
= abd_borrow_buf_copy(pabd
, datalen
);
2657 cipherbuf
= abd_borrow_buf(cabd
, datalen
);
2659 plainbuf
= abd_borrow_buf(pabd
, datalen
);
2660 cipherbuf
= abd_borrow_buf_copy(cabd
, datalen
);
2664 * Both encryption and decryption functions need a salt for key
2665 * generation and an IV. When encrypting a non-dedup block, we
2666 * generate the salt and IV randomly to be stored by the caller. Dedup
2667 * blocks perform a (more expensive) HMAC of the plaintext to obtain
2668 * the salt and the IV. ZIL blocks have their salt and IV generated
2669 * at allocation time in zio_alloc_zil(). On decryption, we simply use
2670 * the provided values.
2672 if (encrypt
&& ot
!= DMU_OT_INTENT_LOG
&& !BP_GET_DEDUP(bp
)) {
2673 ret
= zio_crypt_key_get_salt(&dck
->dck_key
, salt
);
2677 ret
= zio_crypt_generate_iv(iv
);
2680 } else if (encrypt
&& BP_GET_DEDUP(bp
)) {
2681 ret
= zio_crypt_generate_iv_salt_dedup(&dck
->dck_key
,
2682 plainbuf
, datalen
, iv
, salt
);
2687 /* call lower level function to perform encryption / decryption */
2688 ret
= zio_do_crypt_data(encrypt
, &dck
->dck_key
, salt
, ot
, iv
, mac
,
2689 datalen
, BP_SHOULD_BYTESWAP(bp
), plainbuf
, cipherbuf
, no_crypt
);
2694 abd_return_buf(pabd
, plainbuf
, datalen
);
2695 abd_return_buf_copy(cabd
, cipherbuf
, datalen
);
2697 abd_return_buf_copy(pabd
, plainbuf
, datalen
);
2698 abd_return_buf(cabd
, cipherbuf
, datalen
);
2701 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2707 /* zero out any state we might have changed while encrypting */
2708 bzero(salt
, ZIO_DATA_SALT_LEN
);
2709 bzero(iv
, ZIO_DATA_IV_LEN
);
2710 bzero(mac
, ZIO_DATA_MAC_LEN
);
2711 abd_return_buf(pabd
, plainbuf
, datalen
);
2712 abd_return_buf_copy(cabd
, cipherbuf
, datalen
);
2714 abd_return_buf_copy(pabd
, plainbuf
, datalen
);
2715 abd_return_buf(cabd
, cipherbuf
, datalen
);
2718 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);