1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * This file and its contents are supplied under the terms of the 4 * Common Development and Distribution License ("CDDL"), version 1.0. 5 * You may only use this file in accordance with the terms of version 6 * 1.0 of the CDDL. 7 * 8 * A full copy of the text of the CDDL should have accompanied this 9 * source. A copy of the CDDL is also available via the Internet at 10 * https://opensource.org/license/CDDL-1.0. 11 */ 12 13 /* 14 * Copyright (c) 2017, Datto, Inc. All rights reserved. 15 */ 16 17 #ifndef _SYS_DSL_CRYPT_H 18 #define _SYS_DSL_CRYPT_H 19 20 #include <sys/dmu_tx.h> 21 #include <sys/dmu.h> 22 #include <sys/zio_crypt.h> 23 #include <sys/spa.h> 24 #include <sys/dsl_dataset.h> 25 26 /* 27 * ZAP entry keys for DSL Crypto Keys stored on disk. In addition, 28 * ZFS_PROP_KEYFORMAT, ZFS_PROP_PBKDF2_SALT, and ZFS_PROP_PBKDF2_ITERS are 29 * also maintained here using their respective property names. 30 */ 31 #define DSL_CRYPTO_KEY_CRYPTO_SUITE "DSL_CRYPTO_SUITE" 32 #define DSL_CRYPTO_KEY_GUID "DSL_CRYPTO_GUID" 33 #define DSL_CRYPTO_KEY_IV "DSL_CRYPTO_IV" 34 #define DSL_CRYPTO_KEY_MAC "DSL_CRYPTO_MAC" 35 #define DSL_CRYPTO_KEY_MASTER_KEY "DSL_CRYPTO_MASTER_KEY_1" 36 #define DSL_CRYPTO_KEY_HMAC_KEY "DSL_CRYPTO_HMAC_KEY_1" 37 #define DSL_CRYPTO_KEY_ROOT_DDOBJ "DSL_CRYPTO_ROOT_DDOBJ" 38 #define DSL_CRYPTO_KEY_REFCOUNT "DSL_CRYPTO_REFCOUNT" 39 #define DSL_CRYPTO_KEY_VERSION "DSL_CRYPTO_VERSION" 40 41 /* 42 * In-memory representation of a wrapping key. One of these structs will exist 43 * for each encryption root with its key loaded. 44 */ 45 typedef struct dsl_wrapping_key { 46 /* link on spa_keystore_t:sk_wkeys */ 47 avl_node_t wk_avl_link; 48 49 /* keyformat property enum */ 50 zfs_keyformat_t wk_keyformat; 51 52 /* the pbkdf2 salt, if the keyformat is of type passphrase */ 53 uint64_t wk_salt; 54 55 /* the pbkdf2 iterations, if the keyformat is of type passphrase */ 56 uint64_t wk_iters; 57 58 /* actual wrapping key */ 59 crypto_key_t wk_key; 60 61 /* refcount of number of dsl_crypto_key_t's holding this struct */ 62 zfs_refcount_t wk_refcnt; 63 64 /* dsl directory object that owns this wrapping key */ 65 uint64_t wk_ddobj; 66 } dsl_wrapping_key_t; 67 68 /* enum of commands indicating special actions that should be run */ 69 typedef enum dcp_cmd { 70 /* key creation commands */ 71 DCP_CMD_NONE = 0, /* no specific command */ 72 DCP_CMD_RAW_RECV, /* raw receive */ 73 74 /* key changing commands */ 75 DCP_CMD_NEW_KEY, /* rewrap key as an encryption root */ 76 DCP_CMD_INHERIT, /* rewrap key with parent's wrapping key */ 77 DCP_CMD_FORCE_NEW_KEY, /* change to encryption root without rewrap */ 78 DCP_CMD_FORCE_INHERIT, /* inherit parent's key without rewrap */ 79 80 DCP_CMD_MAX 81 } dcp_cmd_t; 82 83 /* 84 * This struct is a simple wrapper around all the parameters that are usually 85 * required to setup encryption. It exists so that all of the params can be 86 * passed around the kernel together for convenience. 87 */ 88 typedef struct dsl_crypto_params { 89 /* command indicating intended action */ 90 dcp_cmd_t cp_cmd; 91 92 /* the encryption algorithm */ 93 enum zio_encrypt cp_crypt; 94 95 /* keylocation property string */ 96 char *cp_keylocation; 97 98 /* the wrapping key */ 99 dsl_wrapping_key_t *cp_wkey; 100 } dsl_crypto_params_t; 101 102 /* 103 * In-memory representation of a DSL Crypto Key object. One of these structs 104 * (and corresponding on-disk ZAP object) will exist for each encrypted 105 * clone family that is mounted or otherwise reading protected data. 106 */ 107 typedef struct dsl_crypto_key { 108 /* link on spa_keystore_t:sk_dsl_keys */ 109 avl_node_t dck_avl_link; 110 111 /* refcount of holders of this key */ 112 zfs_refcount_t dck_holds; 113 114 /* master key used to derive encryption keys */ 115 zio_crypt_key_t dck_key; 116 117 /* wrapping key for syncing this structure to disk */ 118 dsl_wrapping_key_t *dck_wkey; 119 120 /* on-disk object id */ 121 uint64_t dck_obj; 122 } dsl_crypto_key_t; 123 124 /* 125 * In-memory mapping of a dataset object id to a DSL Crypto Key. This is used 126 * to look up the corresponding dsl_crypto_key_t from the zio layer for 127 * performing data encryption and decryption. 128 */ 129 typedef struct dsl_key_mapping { 130 /* link on spa_keystore_t:sk_key_mappings */ 131 avl_node_t km_avl_link; 132 133 /* refcount of how many users are depending on this mapping */ 134 zfs_refcount_t km_refcnt; 135 136 /* dataset this crypto key belongs to (index) */ 137 uint64_t km_dsobj; 138 139 /* crypto key (value) of this record */ 140 dsl_crypto_key_t *km_key; 141 } dsl_key_mapping_t; 142 143 /* in memory structure for holding all wrapping and dsl keys */ 144 typedef struct spa_keystore { 145 /* lock for protecting sk_dsl_keys */ 146 krwlock_t sk_dk_lock; 147 148 /* tree of all dsl_crypto_key_t's */ 149 avl_tree_t sk_dsl_keys; 150 151 /* lock for protecting sk_key_mappings */ 152 krwlock_t sk_km_lock; 153 154 /* tree of all dsl_key_mapping_t's, indexed by dsobj */ 155 avl_tree_t sk_key_mappings; 156 157 /* lock for protecting the wrapping keys tree */ 158 krwlock_t sk_wkeys_lock; 159 160 /* tree of all dsl_wrapping_key_t's, indexed by ddobj */ 161 avl_tree_t sk_wkeys; 162 } spa_keystore_t; 163 164 int dsl_crypto_params_create_nvlist(dcp_cmd_t cmd, nvlist_t *props, 165 nvlist_t *crypto_args, dsl_crypto_params_t **dcp_out); 166 void dsl_crypto_params_free(dsl_crypto_params_t *dcp, boolean_t unload); 167 void dsl_dataset_crypt_stats(struct dsl_dataset *ds, nvlist_t *nv); 168 int dsl_crypto_can_set_keylocation(const char *dsname, const char *keylocation); 169 boolean_t dsl_dir_incompatible_encryption_version(dsl_dir_t *dd); 170 171 void spa_keystore_init(spa_keystore_t *sk); 172 void spa_keystore_fini(spa_keystore_t *sk); 173 174 void spa_keystore_dsl_key_rele(spa_t *spa, dsl_crypto_key_t *dck, 175 const void *tag); 176 int spa_keystore_load_wkey_impl(spa_t *spa, dsl_wrapping_key_t *wkey); 177 int spa_keystore_load_wkey(const char *dsname, dsl_crypto_params_t *dcp, 178 boolean_t noop); 179 int spa_keystore_unload_wkey_impl(spa_t *spa, uint64_t ddobj); 180 int spa_keystore_unload_wkey(const char *dsname); 181 182 int spa_keystore_create_mapping(spa_t *spa, struct dsl_dataset *ds, 183 const void *tag, dsl_key_mapping_t **km_out); 184 int spa_keystore_remove_mapping(spa_t *spa, uint64_t dsobj, const void *tag); 185 void key_mapping_add_ref(dsl_key_mapping_t *km, const void *tag); 186 void key_mapping_rele(spa_t *spa, dsl_key_mapping_t *km, const void *tag); 187 int spa_keystore_lookup_key(spa_t *spa, uint64_t dsobj, const void *tag, 188 dsl_crypto_key_t **dck_out); 189 190 int dsl_crypto_populate_key_nvlist(struct objset *os, 191 uint64_t from_ivset_guid, nvlist_t **nvl_out); 192 int dsl_crypto_recv_raw_key_check(struct dsl_dataset *ds, 193 nvlist_t *nvl, dmu_tx_t *tx); 194 void dsl_crypto_recv_raw_key_sync(struct dsl_dataset *ds, 195 nvlist_t *nvl, dmu_tx_t *tx); 196 int dsl_crypto_recv_raw(const char *poolname, uint64_t dsobj, uint64_t fromobj, 197 dmu_objset_type_t ostype, nvlist_t *nvl, boolean_t do_key); 198 199 int spa_keystore_change_key(const char *dsname, dsl_crypto_params_t *dcp, 200 nvlist_t *userprops); 201 int dsl_dir_rename_crypt_check(dsl_dir_t *dd, dsl_dir_t *newparent); 202 int dsl_dataset_promote_crypt_check(dsl_dir_t *target, dsl_dir_t *origin); 203 void dsl_dataset_promote_crypt_sync(dsl_dir_t *target, dsl_dir_t *origin, 204 dmu_tx_t *tx); 205 int dmu_objset_create_crypt_check(dsl_dir_t *parentdd, 206 dsl_crypto_params_t *dcp, boolean_t *will_encrypt); 207 boolean_t dmu_objset_crypto_key_equal(objset_t *osa, objset_t *osb); 208 void dsl_dataset_create_crypt_sync(uint64_t dsobj, dsl_dir_t *dd, 209 struct dsl_dataset *origin, dsl_crypto_params_t *dcp, dmu_tx_t *tx); 210 uint64_t dsl_crypto_key_create_sync(uint64_t crypt, dsl_wrapping_key_t *wkey, 211 dmu_tx_t *tx); 212 uint64_t dsl_crypto_key_clone_sync(dsl_dir_t *origindd, dmu_tx_t *tx); 213 void dsl_crypto_key_destroy_sync(uint64_t dckobj, dmu_tx_t *tx); 214 215 int spa_crypt_get_salt(spa_t *spa, uint64_t dsobj, uint8_t *salt); 216 int spa_do_crypt_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj, 217 abd_t *abd, uint_t datalen, uint8_t *mac); 218 int spa_do_crypt_objset_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj, 219 abd_t *abd, uint_t datalen, boolean_t byteswap); 220 int spa_do_crypt_abd(boolean_t encrypt, spa_t *spa, const zbookmark_phys_t *zb, 221 dmu_object_type_t ot, boolean_t dedup, boolean_t bswap, uint8_t *salt, 222 uint8_t *iv, uint8_t *mac, uint_t datalen, abd_t *pabd, abd_t *cabd, 223 boolean_t *no_crypt); 224 zfs_keystatus_t dsl_dataset_get_keystatus(dsl_dir_t *dd); 225 226 #endif 227