1*eb633035STom Caputi /*
2*eb633035STom Caputi  * CDDL HEADER START
3*eb633035STom Caputi  *
4*eb633035STom Caputi  * This file and its contents are supplied under the terms of the
5*eb633035STom Caputi  * Common Development and Distribution License ("CDDL"), version 1.0.
6*eb633035STom Caputi  * You may only use this file in accordance with the terms of version
7*eb633035STom Caputi  * 1.0 of the CDDL.
8*eb633035STom Caputi  *
9*eb633035STom Caputi  * A full copy of the text of the CDDL should have accompanied this
10*eb633035STom Caputi  * source.  A copy of the CDDL is also available via the Internet at
11*eb633035STom Caputi  * http://www.illumos.org/license/CDDL.
12*eb633035STom Caputi  *
13*eb633035STom Caputi  * CDDL HEADER END
14*eb633035STom Caputi  */
15*eb633035STom Caputi 
16*eb633035STom Caputi /*
17*eb633035STom Caputi  * Copyright (c) 2017, Datto, Inc. All rights reserved.
18*eb633035STom Caputi  */
19*eb633035STom Caputi 
20*eb633035STom Caputi #ifndef	_SYS_ZIO_CRYPT_H
21*eb633035STom Caputi #define	_SYS_ZIO_CRYPT_H
22*eb633035STom Caputi 
23*eb633035STom Caputi #include <sys/dmu.h>
24*eb633035STom Caputi #include <sys/refcount.h>
25*eb633035STom Caputi #include <sys/crypto/api.h>
26*eb633035STom Caputi #include <sys/nvpair.h>
27*eb633035STom Caputi #include <sys/avl.h>
28*eb633035STom Caputi #include <sys/zio.h>
29*eb633035STom Caputi 
30*eb633035STom Caputi #ifdef	__cplusplus
31*eb633035STom Caputi extern "C" {
32*eb633035STom Caputi #endif
33*eb633035STom Caputi 
34*eb633035STom Caputi /* forward declarations */
35*eb633035STom Caputi struct zbookmark_phys;
36*eb633035STom Caputi 
37*eb633035STom Caputi #define	WRAPPING_KEY_LEN	32
38*eb633035STom Caputi #define	WRAPPING_IV_LEN		ZIO_DATA_IV_LEN
39*eb633035STom Caputi #define	WRAPPING_MAC_LEN	ZIO_DATA_MAC_LEN
40*eb633035STom Caputi #define	MASTER_KEY_MAX_LEN	32
41*eb633035STom Caputi #define	SHA512_HMAC_KEYLEN	64
42*eb633035STom Caputi 
43*eb633035STom Caputi #define	ZIO_CRYPT_KEY_CURRENT_VERSION	1ULL
44*eb633035STom Caputi 
45*eb633035STom Caputi typedef enum zio_crypt_type {
46*eb633035STom Caputi 	ZC_TYPE_NONE = 0,
47*eb633035STom Caputi 	ZC_TYPE_CCM,
48*eb633035STom Caputi 	ZC_TYPE_GCM
49*eb633035STom Caputi } zio_crypt_type_t;
50*eb633035STom Caputi 
51*eb633035STom Caputi /* table of supported crypto algorithms, modes and keylengths. */
52*eb633035STom Caputi typedef struct zio_crypt_info {
53*eb633035STom Caputi 	/* mechanism name, needed by ICP */
54*eb633035STom Caputi 	crypto_mech_name_t ci_mechname;
55*eb633035STom Caputi 
56*eb633035STom Caputi 	/* cipher mode type (GCM, CCM) */
57*eb633035STom Caputi 	zio_crypt_type_t ci_crypt_type;
58*eb633035STom Caputi 
59*eb633035STom Caputi 	/* length of the encryption key */
60*eb633035STom Caputi 	size_t ci_keylen;
61*eb633035STom Caputi 
62*eb633035STom Caputi 	/* human-readable name of the encryption alforithm */
63*eb633035STom Caputi 	char *ci_name;
64*eb633035STom Caputi } zio_crypt_info_t;
65*eb633035STom Caputi 
66*eb633035STom Caputi extern zio_crypt_info_t zio_crypt_table[ZIO_CRYPT_FUNCTIONS];
67*eb633035STom Caputi 
68*eb633035STom Caputi /* in memory representation of an unwrapped key that is loaded into memory */
69*eb633035STom Caputi typedef struct zio_crypt_key {
70*eb633035STom Caputi 	/* encryption algorithm */
71*eb633035STom Caputi 	uint64_t zk_crypt;
72*eb633035STom Caputi 
73*eb633035STom Caputi 	/* on-disk format version */
74*eb633035STom Caputi 	uint64_t zk_version;
75*eb633035STom Caputi 
76*eb633035STom Caputi 	/* GUID for uniquely identifying this key. Not encrypted on disk. */
77*eb633035STom Caputi 	uint64_t zk_guid;
78*eb633035STom Caputi 
79*eb633035STom Caputi 	/* buffer for master key */
80*eb633035STom Caputi 	uint8_t zk_master_keydata[MASTER_KEY_MAX_LEN];
81*eb633035STom Caputi 
82*eb633035STom Caputi 	/* buffer for hmac key */
83*eb633035STom Caputi 	uint8_t zk_hmac_keydata[SHA512_HMAC_KEYLEN];
84*eb633035STom Caputi 
85*eb633035STom Caputi 	/* buffer for currrent encryption key derived from master key */
86*eb633035STom Caputi 	uint8_t zk_current_keydata[MASTER_KEY_MAX_LEN];
87*eb633035STom Caputi 
88*eb633035STom Caputi 	/* current 64 bit salt for deriving an encryption key */
89*eb633035STom Caputi 	uint8_t zk_salt[ZIO_DATA_SALT_LEN];
90*eb633035STom Caputi 
91*eb633035STom Caputi 	/* count of how many times the current salt has been used */
92*eb633035STom Caputi 	uint64_t zk_salt_count;
93*eb633035STom Caputi 
94*eb633035STom Caputi 	/* illumos crypto api current encryption key */
95*eb633035STom Caputi 	crypto_key_t zk_current_key;
96*eb633035STom Caputi 
97*eb633035STom Caputi 	/* template of current encryption key for illumos crypto api */
98*eb633035STom Caputi 	crypto_ctx_template_t zk_current_tmpl;
99*eb633035STom Caputi 
100*eb633035STom Caputi 	/* illumos crypto api current hmac key */
101*eb633035STom Caputi 	crypto_key_t zk_hmac_key;
102*eb633035STom Caputi 
103*eb633035STom Caputi 	/* template of hmac key for illumos crypto api */
104*eb633035STom Caputi 	crypto_ctx_template_t zk_hmac_tmpl;
105*eb633035STom Caputi 
106*eb633035STom Caputi 	/* lock for changing the salt and dependant values */
107*eb633035STom Caputi 	krwlock_t zk_salt_lock;
108*eb633035STom Caputi } zio_crypt_key_t;
109*eb633035STom Caputi 
110*eb633035STom Caputi void zio_crypt_key_destroy(zio_crypt_key_t *key);
111*eb633035STom Caputi int zio_crypt_key_init(uint64_t crypt, zio_crypt_key_t *key);
112*eb633035STom Caputi int zio_crypt_key_get_salt(zio_crypt_key_t *key, uint8_t *salt_out);
113*eb633035STom Caputi 
114*eb633035STom Caputi int zio_crypt_key_wrap(crypto_key_t *cwkey, zio_crypt_key_t *key, uint8_t *iv,
115*eb633035STom Caputi     uint8_t *mac, uint8_t *keydata_out, uint8_t *hmac_keydata_out);
116*eb633035STom Caputi int zio_crypt_key_unwrap(crypto_key_t *cwkey, uint64_t crypt, uint64_t version,
117*eb633035STom Caputi     uint64_t guid, uint8_t *keydata, uint8_t *hmac_keydata, uint8_t *iv,
118*eb633035STom Caputi     uint8_t *mac, zio_crypt_key_t *key);
119*eb633035STom Caputi int zio_crypt_generate_iv(uint8_t *ivbuf);
120*eb633035STom Caputi int zio_crypt_generate_iv_salt_dedup(zio_crypt_key_t *key, uint8_t *data,
121*eb633035STom Caputi     uint_t datalen, uint8_t *ivbuf, uint8_t *salt);
122*eb633035STom Caputi 
123*eb633035STom Caputi void zio_crypt_encode_params_bp(blkptr_t *bp, uint8_t *salt, uint8_t *iv);
124*eb633035STom Caputi void zio_crypt_decode_params_bp(const blkptr_t *bp, uint8_t *salt, uint8_t *iv);
125*eb633035STom Caputi void zio_crypt_encode_mac_bp(blkptr_t *bp, uint8_t *mac);
126*eb633035STom Caputi void zio_crypt_decode_mac_bp(const blkptr_t *bp, uint8_t *mac);
127*eb633035STom Caputi void zio_crypt_encode_mac_zil(void *data, uint8_t *mac);
128*eb633035STom Caputi void zio_crypt_decode_mac_zil(const void *data, uint8_t *mac);
129*eb633035STom Caputi void zio_crypt_copy_dnode_bonus(abd_t *src_abd, uint8_t *dst, uint_t datalen);
130*eb633035STom Caputi 
131*eb633035STom Caputi int zio_crypt_do_indirect_mac_checksum(boolean_t generate, void *buf,
132*eb633035STom Caputi     uint_t datalen, boolean_t byteswap, uint8_t *cksum);
133*eb633035STom Caputi int zio_crypt_do_indirect_mac_checksum_abd(boolean_t generate, abd_t *abd,
134*eb633035STom Caputi     uint_t datalen, boolean_t byteswap, uint8_t *cksum);
135*eb633035STom Caputi int zio_crypt_do_hmac(zio_crypt_key_t *key, uint8_t *data, uint_t datalen,
136*eb633035STom Caputi     uint8_t *digestbuf, uint_t digestlen);
137*eb633035STom Caputi int zio_crypt_do_objset_hmacs(zio_crypt_key_t *key, void *data, uint_t datalen,
138*eb633035STom Caputi     boolean_t byteswap, uint8_t *portable_mac, uint8_t *local_mac);
139*eb633035STom Caputi int zio_do_crypt_data(boolean_t encrypt, zio_crypt_key_t *key,
140*eb633035STom Caputi     dmu_object_type_t ot, boolean_t byteswap, uint8_t *salt, uint8_t *iv,
141*eb633035STom Caputi     uint8_t *mac, uint_t datalen, uint8_t *plainbuf, uint8_t *cipherbuf,
142*eb633035STom Caputi     boolean_t *no_crypt);
143*eb633035STom Caputi int zio_do_crypt_abd(boolean_t encrypt, zio_crypt_key_t *key,
144*eb633035STom Caputi     dmu_object_type_t ot, boolean_t byteswap, uint8_t *salt, uint8_t *iv,
145*eb633035STom Caputi     uint8_t *mac, uint_t datalen, abd_t *pabd, abd_t *cabd,
146*eb633035STom Caputi     boolean_t *no_crypt);
147*eb633035STom Caputi 
148*eb633035STom Caputi #ifdef	__cplusplus
149*eb633035STom Caputi }
150*eb633035STom Caputi #endif
151*eb633035STom Caputi 
152*eb633035STom Caputi #endif /* _SYS_ZIO_CRYPT_H */
153