xref: /freebsd/sys/contrib/openzfs/include/sys/zio_crypt.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
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_ZIO_CRYPT_H
18 #define	_SYS_ZIO_CRYPT_H
19 
20 #include <sys/dmu.h>
21 #include <sys/zfs_refcount.h>
22 #if defined(__FreeBSD__) && defined(_KERNEL)
23 #include <sys/freebsd_crypto.h>
24 #else
25 #include <sys/crypto/api.h>
26 #endif /* __FreeBSD__ */
27 #include <sys/nvpair.h>
28 #include <sys/avl.h>
29 #include <sys/zio.h>
30 
31 /* forward declarations */
32 struct zbookmark_phys;
33 
34 #define	WRAPPING_KEY_LEN	32
35 #define	WRAPPING_IV_LEN		ZIO_DATA_IV_LEN
36 #define	WRAPPING_MAC_LEN	ZIO_DATA_MAC_LEN
37 #define	MASTER_KEY_MAX_LEN	32
38 #define	SHA512_HMAC_KEYLEN	64
39 
40 #define	ZIO_CRYPT_KEY_CURRENT_VERSION	1ULL
41 
42 typedef enum zio_crypt_type {
43 	ZC_TYPE_NONE = 0,
44 	ZC_TYPE_CCM,
45 	ZC_TYPE_GCM
46 } zio_crypt_type_t;
47 
48 /* table of supported crypto algorithms, modes and keylengths. */
49 typedef struct zio_crypt_info {
50 	/* mechanism name, needed by ICP */
51 #if defined(__FreeBSD__) && defined(_KERNEL)
52 	/*
53 	 * I've deliberately used a different name here, to catch
54 	 * ICP-using code.
55 	 */
56 	const char	*ci_algname;
57 #else
58 	crypto_mech_name_t ci_mechname;
59 #endif
60 	/* cipher mode type (GCM, CCM) */
61 	zio_crypt_type_t ci_crypt_type;
62 
63 	/* length of the encryption key */
64 	size_t ci_keylen;
65 
66 	/* human-readable name of the encryption algorithm */
67 	const char *ci_name;
68 } zio_crypt_info_t;
69 
70 extern const zio_crypt_info_t zio_crypt_table[ZIO_CRYPT_FUNCTIONS];
71 
72 /* in memory representation of an unwrapped key that is loaded into memory */
73 typedef struct zio_crypt_key {
74 	/* encryption algorithm */
75 	uint64_t zk_crypt;
76 
77 	/* on-disk format version */
78 	uint64_t zk_version;
79 
80 	/* GUID for uniquely identifying this key. Not encrypted on disk. */
81 	uint64_t zk_guid;
82 
83 	/* buffer for master key */
84 	uint8_t zk_master_keydata[MASTER_KEY_MAX_LEN];
85 
86 	/* buffer for hmac key */
87 	uint8_t zk_hmac_keydata[SHA512_HMAC_KEYLEN];
88 
89 	/* buffer for current encryption key derived from master key */
90 	uint8_t zk_current_keydata[MASTER_KEY_MAX_LEN];
91 
92 	/* current 64 bit salt for deriving an encryption key */
93 	uint8_t zk_salt[ZIO_DATA_SALT_LEN];
94 
95 	/* count of how many times the current salt has been used */
96 	uint64_t zk_salt_count;
97 
98 	/* illumos crypto api current encryption key */
99 	crypto_key_t zk_current_key;
100 
101 #if defined(__FreeBSD__) && defined(_KERNEL)
102 	/* Session for current encryption key.  Must always be set */
103 	freebsd_crypt_session_t	zk_session;
104 #else
105 	/* template of current encryption key for illumos crypto api */
106 	crypto_ctx_template_t zk_current_tmpl;
107 #endif
108 
109 	/* illumos crypto api current hmac key */
110 	crypto_key_t zk_hmac_key;
111 
112 	/* template of hmac key for illumos crypto api */
113 	crypto_ctx_template_t zk_hmac_tmpl;
114 
115 	/* lock for changing the salt and dependent values */
116 	krwlock_t zk_salt_lock;
117 } zio_crypt_key_t;
118 
119 void zio_crypt_key_destroy(zio_crypt_key_t *key);
120 int zio_crypt_key_init(uint64_t crypt, zio_crypt_key_t *key);
121 int zio_crypt_key_get_salt(zio_crypt_key_t *key, uint8_t *salt_out);
122 
123 int zio_crypt_key_wrap(crypto_key_t *cwkey, zio_crypt_key_t *key, uint8_t *iv,
124     uint8_t *mac, uint8_t *keydata_out, uint8_t *hmac_keydata_out);
125 int zio_crypt_key_unwrap(crypto_key_t *cwkey, uint64_t crypt, uint64_t version,
126     uint64_t guid, uint8_t *keydata, uint8_t *hmac_keydata, uint8_t *iv,
127     uint8_t *mac, zio_crypt_key_t *key);
128 int zio_crypt_generate_iv(uint8_t *ivbuf);
129 int zio_crypt_generate_iv_salt_dedup(zio_crypt_key_t *key, uint8_t *data,
130     uint_t datalen, uint8_t *ivbuf, uint8_t *salt);
131 
132 void zio_crypt_encode_params_bp(blkptr_t *bp, uint8_t *salt, uint8_t *iv);
133 void zio_crypt_decode_params_bp(const blkptr_t *bp, uint8_t *salt, uint8_t *iv);
134 void zio_crypt_encode_mac_bp(blkptr_t *bp, uint8_t *mac);
135 void zio_crypt_decode_mac_bp(const blkptr_t *bp, uint8_t *mac);
136 void zio_crypt_encode_mac_zil(void *data, uint8_t *mac);
137 void zio_crypt_decode_mac_zil(const void *data, uint8_t *mac);
138 void zio_crypt_copy_dnode_bonus(abd_t *src_abd, uint8_t *dst, uint_t datalen);
139 
140 int zio_crypt_do_indirect_mac_checksum(boolean_t generate, void *buf,
141     uint_t datalen, boolean_t byteswap, uint8_t *cksum);
142 int zio_crypt_do_indirect_mac_checksum_abd(boolean_t generate, abd_t *abd,
143     uint_t datalen, boolean_t byteswap, uint8_t *cksum);
144 int zio_crypt_do_hmac(zio_crypt_key_t *key, uint8_t *data, uint_t datalen,
145     uint8_t *digestbuf, uint_t digestlen);
146 int zio_crypt_do_objset_hmacs(zio_crypt_key_t *key, void *data, uint_t datalen,
147     boolean_t byteswap, uint8_t *portable_mac, uint8_t *local_mac);
148 int zio_do_crypt_data(boolean_t encrypt, zio_crypt_key_t *key,
149     dmu_object_type_t ot, boolean_t byteswap, uint8_t *salt, uint8_t *iv,
150     uint8_t *mac, uint_t datalen, uint8_t *plainbuf, uint8_t *cipherbuf,
151     boolean_t *no_crypt);
152 int zio_do_crypt_abd(boolean_t encrypt, zio_crypt_key_t *key,
153     dmu_object_type_t ot, boolean_t byteswap, uint8_t *salt, uint8_t *iv,
154     uint8_t *mac, uint_t datalen, abd_t *pabd, abd_t *cabd,
155     boolean_t *no_crypt);
156 
157 #endif
158