xref: /freebsd/sys/geom/eli/g_eli.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1c58794deSPawel Jakub Dawidek /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
33728855aSPedro F. Giffuni  *
42f07cdf8SPawel Jakub Dawidek  * Copyright (c) 2005-2019 Pawel Jakub Dawidek <pawel@dawidek.net>
5c58794deSPawel Jakub Dawidek  * All rights reserved.
6c58794deSPawel Jakub Dawidek  *
7c58794deSPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
8c58794deSPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
9c58794deSPawel Jakub Dawidek  * are met:
10c58794deSPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
11c58794deSPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
12c58794deSPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
13c58794deSPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
14c58794deSPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
15c58794deSPawel Jakub Dawidek  *
16c58794deSPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17c58794deSPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c58794deSPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c58794deSPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20c58794deSPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c58794deSPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c58794deSPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c58794deSPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c58794deSPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c58794deSPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c58794deSPawel Jakub Dawidek  * SUCH DAMAGE.
27c58794deSPawel Jakub Dawidek  */
28c58794deSPawel Jakub Dawidek 
29c58794deSPawel Jakub Dawidek #ifndef	_G_ELI_H_
30c58794deSPawel Jakub Dawidek #define	_G_ELI_H_
31c58794deSPawel Jakub Dawidek 
32c58794deSPawel Jakub Dawidek #include <sys/endian.h>
33c58794deSPawel Jakub Dawidek #include <sys/errno.h>
34c58794deSPawel Jakub Dawidek #include <sys/malloc.h>
357a3f5d11SAllan Jude #include <crypto/sha2/sha256.h>
367a3f5d11SAllan Jude #include <crypto/sha2/sha512.h>
37c58794deSPawel Jakub Dawidek #include <opencrypto/cryptodev.h>
38c58794deSPawel Jakub Dawidek #ifdef _KERNEL
39c58794deSPawel Jakub Dawidek #include <sys/bio.h>
40c58794deSPawel Jakub Dawidek #include <sys/libkern.h>
411e09ff3dSPawel Jakub Dawidek #include <sys/lock.h>
421e09ff3dSPawel Jakub Dawidek #include <sys/mutex.h>
43c58794deSPawel Jakub Dawidek #include <geom/geom.h>
44ec5c0e5bSAllan Jude #include <crypto/intake.h>
45c58794deSPawel Jakub Dawidek #else
461f8c92e6SPawel Jakub Dawidek #include <assert.h>
47c58794deSPawel Jakub Dawidek #include <stdio.h>
48c58794deSPawel Jakub Dawidek #include <string.h>
496eb1d21fSPawel Jakub Dawidek #include <strings.h>
50c58794deSPawel Jakub Dawidek #endif
514332fecaSAllan Jude #include <sys/queue.h>
524332fecaSAllan Jude #include <sys/tree.h>
53c58794deSPawel Jakub Dawidek #ifndef _OpenSSL_
54c58794deSPawel Jakub Dawidek #include <sys/md5.h>
55c58794deSPawel Jakub Dawidek #endif
56c58794deSPawel Jakub Dawidek 
57c58794deSPawel Jakub Dawidek #define	G_ELI_CLASS_NAME	"ELI"
58c58794deSPawel Jakub Dawidek #define	G_ELI_MAGIC		"GEOM::ELI"
59c58794deSPawel Jakub Dawidek #define	G_ELI_SUFFIX		".eli"
60c58794deSPawel Jakub Dawidek 
61c58794deSPawel Jakub Dawidek /*
62c58794deSPawel Jakub Dawidek  * Version history:
63c58794deSPawel Jakub Dawidek  * 0 - Initial version number.
64eaa3b919SPawel Jakub Dawidek  * 1 - Added data authentication support (md_aalgo field and
65eaa3b919SPawel Jakub Dawidek  *     G_ELI_FLAG_AUTH flag).
66d04c304dSPawel Jakub Dawidek  * 2 - Added G_ELI_FLAG_READONLY.
678abd1ad1SPawel Jakub Dawidek  * 3 - Added 'configure' subcommand.
68efb46508SPawel Jakub Dawidek  * 4 - IV is generated from offset converted to little-endian
69ad0a5236SPawel Jakub Dawidek  *     (the G_ELI_FLAG_NATIVE_BYTE_ORDER flag will be set for older versions).
709a5a1d1eSPawel Jakub Dawidek  * 5 - Added multiple encrypton keys and AES-XTS support.
71ad0a5236SPawel Jakub Dawidek  * 6 - Fixed usage of multiple keys for authenticated providers (the
72ad0a5236SPawel Jakub Dawidek  *     G_ELI_FLAG_FIRST_KEY flag will be set for older versions).
73457bbc4fSPawel Jakub Dawidek  * 7 - Encryption keys are now generated from the Data Key and not from the
74457bbc4fSPawel Jakub Dawidek  *     IV Key (the G_ELI_FLAG_ENC_IVKEY flag will be set for older versions).
75c58794deSPawel Jakub Dawidek  */
76ad0a5236SPawel Jakub Dawidek #define	G_ELI_VERSION_00	0
77ad0a5236SPawel Jakub Dawidek #define	G_ELI_VERSION_01	1
78ad0a5236SPawel Jakub Dawidek #define	G_ELI_VERSION_02	2
79ad0a5236SPawel Jakub Dawidek #define	G_ELI_VERSION_03	3
80ad0a5236SPawel Jakub Dawidek #define	G_ELI_VERSION_04	4
81ad0a5236SPawel Jakub Dawidek #define	G_ELI_VERSION_05	5
82ad0a5236SPawel Jakub Dawidek #define	G_ELI_VERSION_06	6
83457bbc4fSPawel Jakub Dawidek #define	G_ELI_VERSION_07	7
84457bbc4fSPawel Jakub Dawidek #define	G_ELI_VERSION		G_ELI_VERSION_07
85c58794deSPawel Jakub Dawidek 
8685059016SPawel Jakub Dawidek /* ON DISK FLAGS. */
87c58794deSPawel Jakub Dawidek /* Use random, onetime keys. */
88c58794deSPawel Jakub Dawidek #define	G_ELI_FLAG_ONETIME		0x00000001
89c58794deSPawel Jakub Dawidek /* Ask for the passphrase from the kernel, before mounting root. */
90c58794deSPawel Jakub Dawidek #define	G_ELI_FLAG_BOOT			0x00000002
91c58794deSPawel Jakub Dawidek /* Detach on last close, if we were open for writing. */
92c58794deSPawel Jakub Dawidek #define	G_ELI_FLAG_WO_DETACH		0x00000004
93c58794deSPawel Jakub Dawidek /* Detach on last close. */
94c58794deSPawel Jakub Dawidek #define	G_ELI_FLAG_RW_DETACH		0x00000008
95eaa3b919SPawel Jakub Dawidek /* Provide data authentication. */
96eaa3b919SPawel Jakub Dawidek #define	G_ELI_FLAG_AUTH			0x00000010
9785059016SPawel Jakub Dawidek /* Provider is read-only, we should deny all write attempts. */
9885059016SPawel Jakub Dawidek #define	G_ELI_FLAG_RO			0x00000020
9946e34470SPawel Jakub Dawidek /* Don't pass through BIO_DELETE requests. */
10046e34470SPawel Jakub Dawidek #define	G_ELI_FLAG_NODELETE		0x00000040
101d8736625SAllan Jude /* This GELI supports GELIBoot */
102d8736625SAllan Jude #define	G_ELI_FLAG_GELIBOOT		0x00000080
1033453dc72SMariusz Zaborski /* Hide passphrase length in GELIboot. */
1043453dc72SMariusz Zaborski #define	G_ELI_FLAG_GELIDISPLAYPASS	0x00000100
1052f07cdf8SPawel Jakub Dawidek /* Expand provider automatically. */
1062f07cdf8SPawel Jakub Dawidek #define	G_ELI_FLAG_AUTORESIZE		0x00000200
1072f07cdf8SPawel Jakub Dawidek 
10885059016SPawel Jakub Dawidek /* RUNTIME FLAGS. */
109c58794deSPawel Jakub Dawidek /* Provider was open for writing. */
110c58794deSPawel Jakub Dawidek #define	G_ELI_FLAG_WOPEN		0x00010000
111c58794deSPawel Jakub Dawidek /* Destroy device. */
112c58794deSPawel Jakub Dawidek #define	G_ELI_FLAG_DESTROY		0x00020000
1132bd4ade6SPawel Jakub Dawidek /* Provider uses native byte-order for IV generation. */
1142bd4ade6SPawel Jakub Dawidek #define	G_ELI_FLAG_NATIVE_BYTE_ORDER	0x00040000
115c6a26d4cSPawel Jakub Dawidek /* Provider uses single encryption key. */
116c6a26d4cSPawel Jakub Dawidek #define	G_ELI_FLAG_SINGLE_KEY		0x00080000
1175ad4a7c7SPawel Jakub Dawidek /* Device suspended. */
1185ad4a7c7SPawel Jakub Dawidek #define	G_ELI_FLAG_SUSPEND		0x00100000
119ad0a5236SPawel Jakub Dawidek /* Provider uses first encryption key. */
120ad0a5236SPawel Jakub Dawidek #define	G_ELI_FLAG_FIRST_KEY		0x00200000
121457bbc4fSPawel Jakub Dawidek /* Provider uses IV-Key for encryption key generation. */
122457bbc4fSPawel Jakub Dawidek #define	G_ELI_FLAG_ENC_IVKEY		0x00400000
1235ad4a7c7SPawel Jakub Dawidek 
1242dbc9a38SGleb Smirnoff /* BIO pflag values. */
1252dbc9a38SGleb Smirnoff #define	G_ELI_WORKER(pflags)	((pflags) & 0xff)
1262dbc9a38SGleb Smirnoff #define	G_ELI_MAX_WORKERS	255
1272dbc9a38SGleb Smirnoff #define	G_ELI_NEW_BIO		G_ELI_MAX_WORKERS
1282dbc9a38SGleb Smirnoff #define	G_ELI_SETWORKER(pflags, w)	\
1292dbc9a38SGleb Smirnoff     (pflags) = ((pflags) & 0xff00) | ((w) & 0xff)
1302dbc9a38SGleb Smirnoff #define	G_ELI_SET_NEW_BIO(pflags)	G_ELI_SETWORKER((pflags), G_ELI_NEW_BIO)
1312dbc9a38SGleb Smirnoff #define	G_ELI_IS_NEW_BIO(pflags)	(G_ELI_WORKER(pflags) == G_ELI_NEW_BIO)
1322dbc9a38SGleb Smirnoff #define	G_ELI_UMA_ALLOC		0x100	/* bio_driver2 alloc came from UMA */
133c58794deSPawel Jakub Dawidek 
134c58794deSPawel Jakub Dawidek #define	SHA512_MDLEN		64
135eaa3b919SPawel Jakub Dawidek #define	G_ELI_AUTH_SECKEYLEN	SHA256_DIGEST_LENGTH
136c58794deSPawel Jakub Dawidek 
137c58794deSPawel Jakub Dawidek #define	G_ELI_MAXMKEYS		2
138c58794deSPawel Jakub Dawidek #define	G_ELI_MAXKEYLEN		64
139c58794deSPawel Jakub Dawidek #define	G_ELI_USERKEYLEN	G_ELI_MAXKEYLEN
140c58794deSPawel Jakub Dawidek #define	G_ELI_DATAKEYLEN	G_ELI_MAXKEYLEN
141eaa3b919SPawel Jakub Dawidek #define	G_ELI_AUTHKEYLEN	G_ELI_MAXKEYLEN
142c58794deSPawel Jakub Dawidek #define	G_ELI_IVKEYLEN		G_ELI_MAXKEYLEN
143c58794deSPawel Jakub Dawidek #define	G_ELI_SALTLEN		64
144c58794deSPawel Jakub Dawidek #define	G_ELI_DATAIVKEYLEN	(G_ELI_DATAKEYLEN + G_ELI_IVKEYLEN)
145c58794deSPawel Jakub Dawidek /* Data-Key, IV-Key, HMAC_SHA512(Derived-Key, Data-Key+IV-Key) */
146c58794deSPawel Jakub Dawidek #define	G_ELI_MKEYLEN		(G_ELI_DATAIVKEYLEN + SHA512_MDLEN)
147b35bfe7eSPawel Jakub Dawidek #define	G_ELI_OVERWRITES	5
148c6a26d4cSPawel Jakub Dawidek /* Switch data encryption key every 2^20 blocks. */
149c6a26d4cSPawel Jakub Dawidek #define	G_ELI_KEY_SHIFT		20
150c58794deSPawel Jakub Dawidek 
1514332fecaSAllan Jude #define	G_ELI_CRYPTO_UNKNOWN	0
1524332fecaSAllan Jude #define	G_ELI_CRYPTO_HW		1
1534332fecaSAllan Jude #define	G_ELI_CRYPTO_SW		2
154a3d565a1SJohn Baldwin #define	G_ELI_CRYPTO_SW_ACCEL	3
1554332fecaSAllan Jude 
156c58794deSPawel Jakub Dawidek #ifdef _KERNEL
157ec5c0e5bSAllan Jude #if (MAX_KEY_BYTES < G_ELI_DATAIVKEYLEN)
158ec5c0e5bSAllan Jude #error "MAX_KEY_BYTES is less than G_ELI_DATAKEYLEN"
159ec5c0e5bSAllan Jude #endif
160ec5c0e5bSAllan Jude 
161f95168e0SPawel Jakub Dawidek extern int g_eli_debug;
162c58794deSPawel Jakub Dawidek extern u_int g_eli_overwrites;
163eaa3b919SPawel Jakub Dawidek extern u_int g_eli_batch;
164c58794deSPawel Jakub Dawidek 
165ac03832eSConrad Meyer #define	G_ELI_DEBUG(lvl, ...) \
166ac03832eSConrad Meyer     _GEOM_DEBUG("GEOM_ELI", g_eli_debug, (lvl), NULL, __VA_ARGS__)
167ac03832eSConrad Meyer #define	G_ELI_LOGREQ(lvl, bp, ...) \
168ac03832eSConrad Meyer     _GEOM_DEBUG("GEOM_ELI", g_eli_debug, (lvl), (bp), __VA_ARGS__)
169c58794deSPawel Jakub Dawidek 
170c58794deSPawel Jakub Dawidek struct g_eli_worker {
171c58794deSPawel Jakub Dawidek 	struct g_eli_softc	*w_softc;
172c58794deSPawel Jakub Dawidek 	struct proc		*w_proc;
173c0341432SJohn Baldwin 	void			*w_first_key;
174c58794deSPawel Jakub Dawidek 	u_int			 w_number;
1751df7f415SConrad Meyer 	crypto_session_t	 w_sid;
1765ad4a7c7SPawel Jakub Dawidek 	boolean_t		 w_active;
177c58794deSPawel Jakub Dawidek 	LIST_ENTRY(g_eli_worker) w_next;
178c58794deSPawel Jakub Dawidek };
179c58794deSPawel Jakub Dawidek 
1804332fecaSAllan Jude #endif	/* _KERNEL */
1814332fecaSAllan Jude 
182c58794deSPawel Jakub Dawidek struct g_eli_softc {
183c58794deSPawel Jakub Dawidek 	struct g_geom	*sc_geom;
1841f8c92e6SPawel Jakub Dawidek 	u_int		 sc_version;
185c58794deSPawel Jakub Dawidek 	u_int		 sc_crypto;
186eaa3b919SPawel Jakub Dawidek 	uint8_t		 sc_mkey[G_ELI_DATAIVKEYLEN];
1871e09ff3dSPawel Jakub Dawidek 	uint8_t		 sc_ekey[G_ELI_DATAKEYLEN];
1881e09ff3dSPawel Jakub Dawidek 	TAILQ_HEAD(, g_eli_key) sc_ekeys_queue;
1891e09ff3dSPawel Jakub Dawidek 	RB_HEAD(g_eli_key_tree, g_eli_key) sc_ekeys_tree;
1900c97af56SWarner Losh #ifndef _STANDALONE
1911e09ff3dSPawel Jakub Dawidek 	struct mtx	 sc_ekeys_lock;
1920c97af56SWarner Losh #endif
1931e09ff3dSPawel Jakub Dawidek 	uint64_t	 sc_ekeys_total;
1941e09ff3dSPawel Jakub Dawidek 	uint64_t	 sc_ekeys_allocated;
195eaa3b919SPawel Jakub Dawidek 	u_int		 sc_ealgo;
196eaa3b919SPawel Jakub Dawidek 	u_int		 sc_ekeylen;
197eaa3b919SPawel Jakub Dawidek 	uint8_t		 sc_akey[G_ELI_AUTHKEYLEN];
198eaa3b919SPawel Jakub Dawidek 	u_int		 sc_aalgo;
199eaa3b919SPawel Jakub Dawidek 	u_int		 sc_akeylen;
200eaa3b919SPawel Jakub Dawidek 	u_int		 sc_alen;
201eaa3b919SPawel Jakub Dawidek 	SHA256_CTX	 sc_akeyctx;
202c58794deSPawel Jakub Dawidek 	uint8_t		 sc_ivkey[G_ELI_IVKEYLEN];
203c58794deSPawel Jakub Dawidek 	SHA256_CTX	 sc_ivctx;
204c58794deSPawel Jakub Dawidek 	int		 sc_nkey;
205c58794deSPawel Jakub Dawidek 	uint32_t	 sc_flags;
2065ad4a7c7SPawel Jakub Dawidek 	int		 sc_inflight;
207c6a26d4cSPawel Jakub Dawidek 	off_t		 sc_mediasize;
208c6a26d4cSPawel Jakub Dawidek 	size_t		 sc_sectorsize;
2092f07cdf8SPawel Jakub Dawidek 	off_t		 sc_provsize;
210eaa3b919SPawel Jakub Dawidek 	u_int		 sc_bytes_per_sector;
211eaa3b919SPawel Jakub Dawidek 	u_int		 sc_data_per_sector;
2124332fecaSAllan Jude #ifndef _KERNEL
2134332fecaSAllan Jude 	int		 sc_cpubind;
2144332fecaSAllan Jude #else /* _KERNEL */
2150c879bd9SPawel Jakub Dawidek 	boolean_t	 sc_cpubind;
216c58794deSPawel Jakub Dawidek 
217c58794deSPawel Jakub Dawidek 	/* Only for software cryptography. */
218c58794deSPawel Jakub Dawidek 	struct bio_queue_head sc_queue;
219c58794deSPawel Jakub Dawidek 	struct mtx	 sc_queue_mtx;
220c58794deSPawel Jakub Dawidek 	LIST_HEAD(, g_eli_worker) sc_workers;
2214332fecaSAllan Jude #endif /* _KERNEL */
222c58794deSPawel Jakub Dawidek };
223c58794deSPawel Jakub Dawidek #define	sc_name		 sc_geom->name
2244332fecaSAllan Jude 
2254332fecaSAllan Jude #define	G_ELI_KEY_MAGIC	0xe11341c
2264332fecaSAllan Jude 
2274332fecaSAllan Jude struct g_eli_key {
2284332fecaSAllan Jude 	/* Key value, must be first in the structure. */
2294332fecaSAllan Jude 	uint8_t		gek_key[G_ELI_DATAKEYLEN];
2304332fecaSAllan Jude 	/* Magic. */
2314332fecaSAllan Jude 	int		gek_magic;
2324332fecaSAllan Jude 	/* Key number. */
2334332fecaSAllan Jude 	uint64_t	gek_keyno;
2344332fecaSAllan Jude 	/* Reference counter. */
2354332fecaSAllan Jude 	int		gek_count;
2364332fecaSAllan Jude 	/* Keeps keys sorted by most recent use. */
2374332fecaSAllan Jude 	TAILQ_ENTRY(g_eli_key) gek_next;
2384332fecaSAllan Jude 	/* Keeps keys sorted by number. */
2394332fecaSAllan Jude 	RB_ENTRY(g_eli_key) gek_link;
2404332fecaSAllan Jude };
241c58794deSPawel Jakub Dawidek 
242c58794deSPawel Jakub Dawidek struct g_eli_metadata {
243c58794deSPawel Jakub Dawidek 	char		md_magic[16];	/* Magic value. */
244c58794deSPawel Jakub Dawidek 	uint32_t	md_version;	/* Version number. */
245c58794deSPawel Jakub Dawidek 	uint32_t	md_flags;	/* Additional flags. */
246eaa3b919SPawel Jakub Dawidek 	uint16_t	md_ealgo;	/* Encryption algorithm. */
247c58794deSPawel Jakub Dawidek 	uint16_t	md_keylen;	/* Key length. */
248eaa3b919SPawel Jakub Dawidek 	uint16_t	md_aalgo;	/* Authentication algorithm. */
249c58794deSPawel Jakub Dawidek 	uint64_t	md_provsize;	/* Provider's size. */
250c58794deSPawel Jakub Dawidek 	uint32_t	md_sectorsize;	/* Sector size. */
251c58794deSPawel Jakub Dawidek 	uint8_t		md_keys;	/* Available keys. */
252fcd46203SPawel Jakub Dawidek 	int32_t		md_iterations;	/* Number of iterations for PKCS#5v2. */
253c58794deSPawel Jakub Dawidek 	uint8_t		md_salt[G_ELI_SALTLEN]; /* Salt. */
254c58794deSPawel Jakub Dawidek 			/* Encrypted master key (IV-key, Data-key, HMAC). */
255c58794deSPawel Jakub Dawidek 	uint8_t		md_mkeys[G_ELI_MAXMKEYS * G_ELI_MKEYLEN];
256c58794deSPawel Jakub Dawidek 	u_char		md_hash[16];	/* MD5 hash. */
257efd9ac0dSPawel Jakub Dawidek } __packed;
258c58794deSPawel Jakub Dawidek #ifndef _OpenSSL_
259c58794deSPawel Jakub Dawidek static __inline void
eli_metadata_encode_v0(struct g_eli_metadata * md,u_char ** datap)2601f8c92e6SPawel Jakub Dawidek eli_metadata_encode_v0(struct g_eli_metadata *md, u_char **datap)
261c58794deSPawel Jakub Dawidek {
262c58794deSPawel Jakub Dawidek 	u_char *p;
263c58794deSPawel Jakub Dawidek 
2641f8c92e6SPawel Jakub Dawidek 	p = *datap;
2651f8c92e6SPawel Jakub Dawidek 	le32enc(p, md->md_flags);	p += sizeof(md->md_flags);
2661f8c92e6SPawel Jakub Dawidek 	le16enc(p, md->md_ealgo);	p += sizeof(md->md_ealgo);
2671f8c92e6SPawel Jakub Dawidek 	le16enc(p, md->md_keylen);	p += sizeof(md->md_keylen);
2681f8c92e6SPawel Jakub Dawidek 	le64enc(p, md->md_provsize);	p += sizeof(md->md_provsize);
2691f8c92e6SPawel Jakub Dawidek 	le32enc(p, md->md_sectorsize);	p += sizeof(md->md_sectorsize);
2701f8c92e6SPawel Jakub Dawidek 	*p = md->md_keys;		p += sizeof(md->md_keys);
2711f8c92e6SPawel Jakub Dawidek 	le32enc(p, md->md_iterations);	p += sizeof(md->md_iterations);
2721f8c92e6SPawel Jakub Dawidek 	bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt);
2731f8c92e6SPawel Jakub Dawidek 	bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
2741f8c92e6SPawel Jakub Dawidek 	*datap = p;
2751f8c92e6SPawel Jakub Dawidek }
2761f8c92e6SPawel Jakub Dawidek static __inline void
eli_metadata_encode_v1v2v3v4v5v6v7(struct g_eli_metadata * md,u_char ** datap)277457bbc4fSPawel Jakub Dawidek eli_metadata_encode_v1v2v3v4v5v6v7(struct g_eli_metadata *md, u_char **datap)
2781f8c92e6SPawel Jakub Dawidek {
2791f8c92e6SPawel Jakub Dawidek 	u_char *p;
2801f8c92e6SPawel Jakub Dawidek 
2811f8c92e6SPawel Jakub Dawidek 	p = *datap;
282c58794deSPawel Jakub Dawidek 	le32enc(p, md->md_flags);	p += sizeof(md->md_flags);
283eaa3b919SPawel Jakub Dawidek 	le16enc(p, md->md_ealgo);	p += sizeof(md->md_ealgo);
284c58794deSPawel Jakub Dawidek 	le16enc(p, md->md_keylen);	p += sizeof(md->md_keylen);
285eaa3b919SPawel Jakub Dawidek 	le16enc(p, md->md_aalgo);	p += sizeof(md->md_aalgo);
286c58794deSPawel Jakub Dawidek 	le64enc(p, md->md_provsize);	p += sizeof(md->md_provsize);
287c58794deSPawel Jakub Dawidek 	le32enc(p, md->md_sectorsize);	p += sizeof(md->md_sectorsize);
288c58794deSPawel Jakub Dawidek 	*p = md->md_keys;		p += sizeof(md->md_keys);
289c58794deSPawel Jakub Dawidek 	le32enc(p, md->md_iterations);	p += sizeof(md->md_iterations);
290c58794deSPawel Jakub Dawidek 	bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt);
291c58794deSPawel Jakub Dawidek 	bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
2921f8c92e6SPawel Jakub Dawidek 	*datap = p;
2931f8c92e6SPawel Jakub Dawidek }
2941f8c92e6SPawel Jakub Dawidek static __inline void
eli_metadata_encode(struct g_eli_metadata * md,u_char * data)2951f8c92e6SPawel Jakub Dawidek eli_metadata_encode(struct g_eli_metadata *md, u_char *data)
2961f8c92e6SPawel Jakub Dawidek {
297ae8b1f90SRuslan Bukin 	uint32_t hash[4];
2981f8c92e6SPawel Jakub Dawidek 	MD5_CTX ctx;
2991f8c92e6SPawel Jakub Dawidek 	u_char *p;
3001f8c92e6SPawel Jakub Dawidek 
3011f8c92e6SPawel Jakub Dawidek 	p = data;
3021f8c92e6SPawel Jakub Dawidek 	bcopy(md->md_magic, p, sizeof(md->md_magic));
3031f8c92e6SPawel Jakub Dawidek 	p += sizeof(md->md_magic);
3041f8c92e6SPawel Jakub Dawidek 	le32enc(p, md->md_version);
3051f8c92e6SPawel Jakub Dawidek 	p += sizeof(md->md_version);
3061f8c92e6SPawel Jakub Dawidek 	switch (md->md_version) {
3071f8c92e6SPawel Jakub Dawidek 	case G_ELI_VERSION_00:
3081f8c92e6SPawel Jakub Dawidek 		eli_metadata_encode_v0(md, &p);
3091f8c92e6SPawel Jakub Dawidek 		break;
3101f8c92e6SPawel Jakub Dawidek 	case G_ELI_VERSION_01:
3111f8c92e6SPawel Jakub Dawidek 	case G_ELI_VERSION_02:
3121f8c92e6SPawel Jakub Dawidek 	case G_ELI_VERSION_03:
3131f8c92e6SPawel Jakub Dawidek 	case G_ELI_VERSION_04:
3141f8c92e6SPawel Jakub Dawidek 	case G_ELI_VERSION_05:
3151f8c92e6SPawel Jakub Dawidek 	case G_ELI_VERSION_06:
316457bbc4fSPawel Jakub Dawidek 	case G_ELI_VERSION_07:
317457bbc4fSPawel Jakub Dawidek 		eli_metadata_encode_v1v2v3v4v5v6v7(md, &p);
3181f8c92e6SPawel Jakub Dawidek 		break;
3191f8c92e6SPawel Jakub Dawidek 	default:
3201f8c92e6SPawel Jakub Dawidek #ifdef _KERNEL
3211f8c92e6SPawel Jakub Dawidek 		panic("%s: Unsupported version %u.", __func__,
3221f8c92e6SPawel Jakub Dawidek 		    (u_int)md->md_version);
3231f8c92e6SPawel Jakub Dawidek #else
3241f8c92e6SPawel Jakub Dawidek 		assert(!"Unsupported metadata version.");
3251f8c92e6SPawel Jakub Dawidek #endif
3261f8c92e6SPawel Jakub Dawidek 	}
327c58794deSPawel Jakub Dawidek 	MD5Init(&ctx);
328c58794deSPawel Jakub Dawidek 	MD5Update(&ctx, data, p - data);
329ae8b1f90SRuslan Bukin 	MD5Final((void *)hash, &ctx);
330ae8b1f90SRuslan Bukin 	bcopy(hash, md->md_hash, sizeof(md->md_hash));
331c58794deSPawel Jakub Dawidek 	bcopy(md->md_hash, p, sizeof(md->md_hash));
332c58794deSPawel Jakub Dawidek }
333c58794deSPawel Jakub Dawidek static __inline int
eli_metadata_decode_v0(const u_char * data,struct g_eli_metadata * md)334c58794deSPawel Jakub Dawidek eli_metadata_decode_v0(const u_char *data, struct g_eli_metadata *md)
335c58794deSPawel Jakub Dawidek {
336ae8b1f90SRuslan Bukin 	uint32_t hash[4];
337c58794deSPawel Jakub Dawidek 	MD5_CTX ctx;
338c58794deSPawel Jakub Dawidek 	const u_char *p;
339c58794deSPawel Jakub Dawidek 
340c58794deSPawel Jakub Dawidek 	p = data + sizeof(md->md_magic) + sizeof(md->md_version);
341c58794deSPawel Jakub Dawidek 	md->md_flags = le32dec(p);	p += sizeof(md->md_flags);
342eaa3b919SPawel Jakub Dawidek 	md->md_ealgo = le16dec(p);	p += sizeof(md->md_ealgo);
343c58794deSPawel Jakub Dawidek 	md->md_keylen = le16dec(p);	p += sizeof(md->md_keylen);
344c58794deSPawel Jakub Dawidek 	md->md_provsize = le64dec(p);	p += sizeof(md->md_provsize);
345c58794deSPawel Jakub Dawidek 	md->md_sectorsize = le32dec(p);	p += sizeof(md->md_sectorsize);
346c58794deSPawel Jakub Dawidek 	md->md_keys = *p;		p += sizeof(md->md_keys);
347c58794deSPawel Jakub Dawidek 	md->md_iterations = le32dec(p);	p += sizeof(md->md_iterations);
348c58794deSPawel Jakub Dawidek 	bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
349c58794deSPawel Jakub Dawidek 	bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
350c58794deSPawel Jakub Dawidek 	MD5Init(&ctx);
351c58794deSPawel Jakub Dawidek 	MD5Update(&ctx, data, p - data);
352ae8b1f90SRuslan Bukin 	MD5Final((void *)hash, &ctx);
353ae8b1f90SRuslan Bukin 	bcopy(hash, md->md_hash, sizeof(md->md_hash));
354c58794deSPawel Jakub Dawidek 	if (bcmp(md->md_hash, p, 16) != 0)
355c58794deSPawel Jakub Dawidek 		return (EINVAL);
356c58794deSPawel Jakub Dawidek 	return (0);
357c58794deSPawel Jakub Dawidek }
3580f81f304SBrian Somers 
359c58794deSPawel Jakub Dawidek static __inline int
eli_metadata_decode_v1v2v3v4v5v6v7(const u_char * data,struct g_eli_metadata * md)360457bbc4fSPawel Jakub Dawidek eli_metadata_decode_v1v2v3v4v5v6v7(const u_char *data, struct g_eli_metadata *md)
361eaa3b919SPawel Jakub Dawidek {
362ae8b1f90SRuslan Bukin 	uint32_t hash[4];
363eaa3b919SPawel Jakub Dawidek 	MD5_CTX ctx;
364eaa3b919SPawel Jakub Dawidek 	const u_char *p;
365eaa3b919SPawel Jakub Dawidek 
366eaa3b919SPawel Jakub Dawidek 	p = data + sizeof(md->md_magic) + sizeof(md->md_version);
367eaa3b919SPawel Jakub Dawidek 	md->md_flags = le32dec(p);	p += sizeof(md->md_flags);
368eaa3b919SPawel Jakub Dawidek 	md->md_ealgo = le16dec(p);	p += sizeof(md->md_ealgo);
369eaa3b919SPawel Jakub Dawidek 	md->md_keylen = le16dec(p);	p += sizeof(md->md_keylen);
370eaa3b919SPawel Jakub Dawidek 	md->md_aalgo = le16dec(p);	p += sizeof(md->md_aalgo);
371eaa3b919SPawel Jakub Dawidek 	md->md_provsize = le64dec(p);	p += sizeof(md->md_provsize);
372eaa3b919SPawel Jakub Dawidek 	md->md_sectorsize = le32dec(p);	p += sizeof(md->md_sectorsize);
373eaa3b919SPawel Jakub Dawidek 	md->md_keys = *p;		p += sizeof(md->md_keys);
374eaa3b919SPawel Jakub Dawidek 	md->md_iterations = le32dec(p);	p += sizeof(md->md_iterations);
375eaa3b919SPawel Jakub Dawidek 	bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
376eaa3b919SPawel Jakub Dawidek 	bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
377eaa3b919SPawel Jakub Dawidek 	MD5Init(&ctx);
378eaa3b919SPawel Jakub Dawidek 	MD5Update(&ctx, data, p - data);
379ae8b1f90SRuslan Bukin 	MD5Final((void *)hash, &ctx);
380ae8b1f90SRuslan Bukin 	bcopy(hash, md->md_hash, sizeof(md->md_hash));
381eaa3b919SPawel Jakub Dawidek 	if (bcmp(md->md_hash, p, 16) != 0)
382eaa3b919SPawel Jakub Dawidek 		return (EINVAL);
383eaa3b919SPawel Jakub Dawidek 	return (0);
384eaa3b919SPawel Jakub Dawidek }
385eaa3b919SPawel Jakub Dawidek static __inline int
eli_metadata_decode(const u_char * data,struct g_eli_metadata * md)386c58794deSPawel Jakub Dawidek eli_metadata_decode(const u_char *data, struct g_eli_metadata *md)
387c58794deSPawel Jakub Dawidek {
388c58794deSPawel Jakub Dawidek 	int error;
389c58794deSPawel Jakub Dawidek 
390c58794deSPawel Jakub Dawidek 	bcopy(data, md->md_magic, sizeof(md->md_magic));
391effb9912SPawel Jakub Dawidek 	if (strcmp(md->md_magic, G_ELI_MAGIC) != 0)
392effb9912SPawel Jakub Dawidek 		return (EINVAL);
393c58794deSPawel Jakub Dawidek 	md->md_version = le32dec(data + sizeof(md->md_magic));
394c58794deSPawel Jakub Dawidek 	switch (md->md_version) {
395964d172cSPawel Jakub Dawidek 	case G_ELI_VERSION_00:
396c58794deSPawel Jakub Dawidek 		error = eli_metadata_decode_v0(data, md);
397c58794deSPawel Jakub Dawidek 		break;
398964d172cSPawel Jakub Dawidek 	case G_ELI_VERSION_01:
399964d172cSPawel Jakub Dawidek 	case G_ELI_VERSION_02:
400964d172cSPawel Jakub Dawidek 	case G_ELI_VERSION_03:
401964d172cSPawel Jakub Dawidek 	case G_ELI_VERSION_04:
402964d172cSPawel Jakub Dawidek 	case G_ELI_VERSION_05:
403964d172cSPawel Jakub Dawidek 	case G_ELI_VERSION_06:
404457bbc4fSPawel Jakub Dawidek 	case G_ELI_VERSION_07:
405457bbc4fSPawel Jakub Dawidek 		error = eli_metadata_decode_v1v2v3v4v5v6v7(data, md);
406eaa3b919SPawel Jakub Dawidek 		break;
407c58794deSPawel Jakub Dawidek 	default:
408e880ff00SPawel Jakub Dawidek 		error = EOPNOTSUPP;
409c58794deSPawel Jakub Dawidek 		break;
410c58794deSPawel Jakub Dawidek 	}
411c58794deSPawel Jakub Dawidek 	return (error);
412c58794deSPawel Jakub Dawidek }
413c58794deSPawel Jakub Dawidek #endif	/* !_OpenSSL */
414c58794deSPawel Jakub Dawidek 
415c58794deSPawel Jakub Dawidek static __inline u_int
g_eli_str2ealgo(const char * name)416eaa3b919SPawel Jakub Dawidek g_eli_str2ealgo(const char *name)
417c58794deSPawel Jakub Dawidek {
418c58794deSPawel Jakub Dawidek 
4196eb1d21fSPawel Jakub Dawidek 	if (strcasecmp("null", name) == 0)
420c58794deSPawel Jakub Dawidek 		return (CRYPTO_NULL_CBC);
4219a5a1d1eSPawel Jakub Dawidek 	else if (strcasecmp("null-cbc", name) == 0)
4229a5a1d1eSPawel Jakub Dawidek 		return (CRYPTO_NULL_CBC);
4236eb1d21fSPawel Jakub Dawidek 	else if (strcasecmp("aes", name) == 0)
4249a5a1d1eSPawel Jakub Dawidek 		return (CRYPTO_AES_XTS);
4259a5a1d1eSPawel Jakub Dawidek 	else if (strcasecmp("aes-cbc", name) == 0)
426c58794deSPawel Jakub Dawidek 		return (CRYPTO_AES_CBC);
4279a5a1d1eSPawel Jakub Dawidek 	else if (strcasecmp("aes-xts", name) == 0)
4289a5a1d1eSPawel Jakub Dawidek 		return (CRYPTO_AES_XTS);
429864cba96SPawel Jakub Dawidek 	else if (strcasecmp("camellia", name) == 0)
430864cba96SPawel Jakub Dawidek 		return (CRYPTO_CAMELLIA_CBC);
4319a5a1d1eSPawel Jakub Dawidek 	else if (strcasecmp("camellia-cbc", name) == 0)
4329a5a1d1eSPawel Jakub Dawidek 		return (CRYPTO_CAMELLIA_CBC);
433c58794deSPawel Jakub Dawidek 	return (CRYPTO_ALGORITHM_MIN - 1);
434c58794deSPawel Jakub Dawidek }
435c58794deSPawel Jakub Dawidek 
436eaa3b919SPawel Jakub Dawidek static __inline u_int
g_eli_str2aalgo(const char * name)437eaa3b919SPawel Jakub Dawidek g_eli_str2aalgo(const char *name)
438eaa3b919SPawel Jakub Dawidek {
439eaa3b919SPawel Jakub Dawidek 
440e2b99193SJohn Baldwin 	if (strcasecmp("hmac/sha1", name) == 0)
441eaa3b919SPawel Jakub Dawidek 		return (CRYPTO_SHA1_HMAC);
442eaa3b919SPawel Jakub Dawidek 	else if (strcasecmp("hmac/ripemd160", name) == 0)
443eaa3b919SPawel Jakub Dawidek 		return (CRYPTO_RIPEMD160_HMAC);
444eaa3b919SPawel Jakub Dawidek 	else if (strcasecmp("hmac/sha256", name) == 0)
445eaa3b919SPawel Jakub Dawidek 		return (CRYPTO_SHA2_256_HMAC);
446eaa3b919SPawel Jakub Dawidek 	else if (strcasecmp("hmac/sha384", name) == 0)
447eaa3b919SPawel Jakub Dawidek 		return (CRYPTO_SHA2_384_HMAC);
448eaa3b919SPawel Jakub Dawidek 	else if (strcasecmp("hmac/sha512", name) == 0)
449eaa3b919SPawel Jakub Dawidek 		return (CRYPTO_SHA2_512_HMAC);
450eaa3b919SPawel Jakub Dawidek 	return (CRYPTO_ALGORITHM_MIN - 1);
451eaa3b919SPawel Jakub Dawidek }
452eaa3b919SPawel Jakub Dawidek 
453c58794deSPawel Jakub Dawidek static __inline const char *
g_eli_algo2str(u_int algo)454c58794deSPawel Jakub Dawidek g_eli_algo2str(u_int algo)
455c58794deSPawel Jakub Dawidek {
456c58794deSPawel Jakub Dawidek 
457c58794deSPawel Jakub Dawidek 	switch (algo) {
458c58794deSPawel Jakub Dawidek 	case CRYPTO_NULL_CBC:
459c58794deSPawel Jakub Dawidek 		return ("NULL");
460c58794deSPawel Jakub Dawidek 	case CRYPTO_AES_CBC:
461eaa3b919SPawel Jakub Dawidek 		return ("AES-CBC");
4629a5a1d1eSPawel Jakub Dawidek 	case CRYPTO_AES_XTS:
4639a5a1d1eSPawel Jakub Dawidek 		return ("AES-XTS");
464864cba96SPawel Jakub Dawidek 	case CRYPTO_CAMELLIA_CBC:
465864cba96SPawel Jakub Dawidek 		return ("CAMELLIA-CBC");
466eaa3b919SPawel Jakub Dawidek 	case CRYPTO_SHA1_HMAC:
467eaa3b919SPawel Jakub Dawidek 		return ("HMAC/SHA1");
468eaa3b919SPawel Jakub Dawidek 	case CRYPTO_RIPEMD160_HMAC:
469eaa3b919SPawel Jakub Dawidek 		return ("HMAC/RIPEMD160");
470eaa3b919SPawel Jakub Dawidek 	case CRYPTO_SHA2_256_HMAC:
471eaa3b919SPawel Jakub Dawidek 		return ("HMAC/SHA256");
472eaa3b919SPawel Jakub Dawidek 	case CRYPTO_SHA2_384_HMAC:
473eaa3b919SPawel Jakub Dawidek 		return ("HMAC/SHA384");
474eaa3b919SPawel Jakub Dawidek 	case CRYPTO_SHA2_512_HMAC:
475eaa3b919SPawel Jakub Dawidek 		return ("HMAC/SHA512");
476c58794deSPawel Jakub Dawidek 	}
477c58794deSPawel Jakub Dawidek 	return ("unknown");
478c58794deSPawel Jakub Dawidek }
479c58794deSPawel Jakub Dawidek 
480c58794deSPawel Jakub Dawidek static __inline void
eli_metadata_dump(const struct g_eli_metadata * md)481c58794deSPawel Jakub Dawidek eli_metadata_dump(const struct g_eli_metadata *md)
482c58794deSPawel Jakub Dawidek {
483c58794deSPawel Jakub Dawidek 	static const char hex[] = "0123456789abcdef";
484c58794deSPawel Jakub Dawidek 	char str[sizeof(md->md_mkeys) * 2 + 1];
485c58794deSPawel Jakub Dawidek 	u_int i;
486c58794deSPawel Jakub Dawidek 
487c58794deSPawel Jakub Dawidek 	printf("     magic: %s\n", md->md_magic);
488c58794deSPawel Jakub Dawidek 	printf("   version: %u\n", (u_int)md->md_version);
489c58794deSPawel Jakub Dawidek 	printf("     flags: 0x%x\n", (u_int)md->md_flags);
490eaa3b919SPawel Jakub Dawidek 	printf("     ealgo: %s\n", g_eli_algo2str(md->md_ealgo));
491c58794deSPawel Jakub Dawidek 	printf("    keylen: %u\n", (u_int)md->md_keylen);
492eaa3b919SPawel Jakub Dawidek 	if (md->md_flags & G_ELI_FLAG_AUTH)
493eaa3b919SPawel Jakub Dawidek 		printf("     aalgo: %s\n", g_eli_algo2str(md->md_aalgo));
494c58794deSPawel Jakub Dawidek 	printf("  provsize: %ju\n", (uintmax_t)md->md_provsize);
495c58794deSPawel Jakub Dawidek 	printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
496c58794deSPawel Jakub Dawidek 	printf("      keys: 0x%02x\n", (u_int)md->md_keys);
497d109d8adSAlexander Motin 	printf("iterations: %d\n", (int)md->md_iterations);
498c58794deSPawel Jakub Dawidek 	bzero(str, sizeof(str));
499c58794deSPawel Jakub Dawidek 	for (i = 0; i < sizeof(md->md_salt); i++) {
500c58794deSPawel Jakub Dawidek 		str[i * 2] = hex[md->md_salt[i] >> 4];
501c58794deSPawel Jakub Dawidek 		str[i * 2 + 1] = hex[md->md_salt[i] & 0x0f];
502c58794deSPawel Jakub Dawidek 	}
503c58794deSPawel Jakub Dawidek 	printf("      Salt: %s\n", str);
504c58794deSPawel Jakub Dawidek 	bzero(str, sizeof(str));
505c58794deSPawel Jakub Dawidek 	for (i = 0; i < sizeof(md->md_mkeys); i++) {
506c58794deSPawel Jakub Dawidek 		str[i * 2] = hex[md->md_mkeys[i] >> 4];
507c58794deSPawel Jakub Dawidek 		str[i * 2 + 1] = hex[md->md_mkeys[i] & 0x0f];
508c58794deSPawel Jakub Dawidek 	}
509c58794deSPawel Jakub Dawidek 	printf("Master Key: %s\n", str);
510c58794deSPawel Jakub Dawidek 	bzero(str, sizeof(str));
511c58794deSPawel Jakub Dawidek 	for (i = 0; i < 16; i++) {
512c58794deSPawel Jakub Dawidek 		str[i * 2] = hex[md->md_hash[i] >> 4];
513c58794deSPawel Jakub Dawidek 		str[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
514c58794deSPawel Jakub Dawidek 	}
515c58794deSPawel Jakub Dawidek 	printf("  MD5 hash: %s\n", str);
516c58794deSPawel Jakub Dawidek }
517c58794deSPawel Jakub Dawidek 
518e2b99193SJohn Baldwin #ifdef _KERNEL
519bfe26b97SJohn Baldwin static __inline bool
eli_metadata_crypto_supported(const struct g_eli_metadata * md)520e2b99193SJohn Baldwin eli_metadata_crypto_supported(const struct g_eli_metadata *md)
521e2b99193SJohn Baldwin {
522e2b99193SJohn Baldwin 
523e2b99193SJohn Baldwin 	switch (md->md_ealgo) {
524e2b99193SJohn Baldwin 	case CRYPTO_NULL_CBC:
525e2b99193SJohn Baldwin 	case CRYPTO_AES_CBC:
526e2b99193SJohn Baldwin 	case CRYPTO_CAMELLIA_CBC:
527e2b99193SJohn Baldwin 	case CRYPTO_AES_XTS:
528e2b99193SJohn Baldwin 		break;
529e2b99193SJohn Baldwin 	default:
530e2b99193SJohn Baldwin 		return (false);
531e2b99193SJohn Baldwin 	}
532e2b99193SJohn Baldwin 	if (md->md_flags & G_ELI_FLAG_AUTH) {
533e2b99193SJohn Baldwin 		switch (md->md_aalgo) {
534e2b99193SJohn Baldwin 		case CRYPTO_SHA1_HMAC:
535e2b99193SJohn Baldwin 		case CRYPTO_RIPEMD160_HMAC:
536e2b99193SJohn Baldwin 		case CRYPTO_SHA2_256_HMAC:
537e2b99193SJohn Baldwin 		case CRYPTO_SHA2_384_HMAC:
538e2b99193SJohn Baldwin 		case CRYPTO_SHA2_512_HMAC:
539e2b99193SJohn Baldwin 			break;
540e2b99193SJohn Baldwin 		default:
541e2b99193SJohn Baldwin 			return (false);
542e2b99193SJohn Baldwin 		}
543e2b99193SJohn Baldwin 	}
544e2b99193SJohn Baldwin 	return (true);
545e2b99193SJohn Baldwin }
546e2b99193SJohn Baldwin #endif
547e2b99193SJohn Baldwin 
548c58794deSPawel Jakub Dawidek static __inline u_int
g_eli_keylen(u_int algo,u_int keylen)549c58794deSPawel Jakub Dawidek g_eli_keylen(u_int algo, u_int keylen)
550c58794deSPawel Jakub Dawidek {
551c58794deSPawel Jakub Dawidek 
552c58794deSPawel Jakub Dawidek 	switch (algo) {
553c58794deSPawel Jakub Dawidek 	case CRYPTO_NULL_CBC:
554c58794deSPawel Jakub Dawidek 		if (keylen == 0)
555c58794deSPawel Jakub Dawidek 			keylen = 64 * 8;
556c58794deSPawel Jakub Dawidek 		else {
557c58794deSPawel Jakub Dawidek 			if (keylen > 64 * 8)
558c58794deSPawel Jakub Dawidek 				keylen = 0;
559c58794deSPawel Jakub Dawidek 		}
560c58794deSPawel Jakub Dawidek 		return (keylen);
561efb46508SPawel Jakub Dawidek 	case CRYPTO_AES_CBC:
562864cba96SPawel Jakub Dawidek 	case CRYPTO_CAMELLIA_CBC:
563c58794deSPawel Jakub Dawidek 		switch (keylen) {
564c58794deSPawel Jakub Dawidek 		case 0:
565c58794deSPawel Jakub Dawidek 			return (128);
566c58794deSPawel Jakub Dawidek 		case 128:
567c58794deSPawel Jakub Dawidek 		case 192:
568c58794deSPawel Jakub Dawidek 		case 256:
569c58794deSPawel Jakub Dawidek 			return (keylen);
570c58794deSPawel Jakub Dawidek 		default:
571c58794deSPawel Jakub Dawidek 			return (0);
572c58794deSPawel Jakub Dawidek 		}
5739a5a1d1eSPawel Jakub Dawidek 	case CRYPTO_AES_XTS:
5749a5a1d1eSPawel Jakub Dawidek 		switch (keylen) {
5759a5a1d1eSPawel Jakub Dawidek 		case 0:
5769a5a1d1eSPawel Jakub Dawidek 			return (128);
5779a5a1d1eSPawel Jakub Dawidek 		case 128:
5789a5a1d1eSPawel Jakub Dawidek 		case 256:
5799a5a1d1eSPawel Jakub Dawidek 			return (keylen);
5809a5a1d1eSPawel Jakub Dawidek 		default:
5819a5a1d1eSPawel Jakub Dawidek 			return (0);
5829a5a1d1eSPawel Jakub Dawidek 		}
583c58794deSPawel Jakub Dawidek 	default:
584c58794deSPawel Jakub Dawidek 		return (0);
585c58794deSPawel Jakub Dawidek 	}
586c58794deSPawel Jakub Dawidek }
587c58794deSPawel Jakub Dawidek 
588eaa3b919SPawel Jakub Dawidek static __inline u_int
g_eli_ivlen(u_int algo)589c0341432SJohn Baldwin g_eli_ivlen(u_int algo)
590c0341432SJohn Baldwin {
591c0341432SJohn Baldwin 
592c0341432SJohn Baldwin 	switch (algo) {
593c0341432SJohn Baldwin 	case CRYPTO_AES_XTS:
594c0341432SJohn Baldwin 		return (AES_XTS_IV_LEN);
595c0341432SJohn Baldwin 	case CRYPTO_AES_CBC:
596c0341432SJohn Baldwin 		return (AES_BLOCK_LEN);
597c0341432SJohn Baldwin 	case CRYPTO_CAMELLIA_CBC:
598c0341432SJohn Baldwin 		return (CAMELLIA_BLOCK_LEN);
599c0341432SJohn Baldwin 	}
600c0341432SJohn Baldwin 	return (0);
601c0341432SJohn Baldwin }
602c0341432SJohn Baldwin 
603c0341432SJohn Baldwin static __inline u_int
g_eli_hashlen(u_int algo)604eaa3b919SPawel Jakub Dawidek g_eli_hashlen(u_int algo)
605eaa3b919SPawel Jakub Dawidek {
606eaa3b919SPawel Jakub Dawidek 
607eaa3b919SPawel Jakub Dawidek 	switch (algo) {
608eaa3b919SPawel Jakub Dawidek 	case CRYPTO_SHA1_HMAC:
609eaa3b919SPawel Jakub Dawidek 		return (20);
610eaa3b919SPawel Jakub Dawidek 	case CRYPTO_RIPEMD160_HMAC:
611eaa3b919SPawel Jakub Dawidek 		return (20);
612eaa3b919SPawel Jakub Dawidek 	case CRYPTO_SHA2_256_HMAC:
613eaa3b919SPawel Jakub Dawidek 		return (32);
614eaa3b919SPawel Jakub Dawidek 	case CRYPTO_SHA2_384_HMAC:
615eaa3b919SPawel Jakub Dawidek 		return (48);
616eaa3b919SPawel Jakub Dawidek 	case CRYPTO_SHA2_512_HMAC:
617eaa3b919SPawel Jakub Dawidek 		return (64);
618eaa3b919SPawel Jakub Dawidek 	}
619eaa3b919SPawel Jakub Dawidek 	return (0);
620eaa3b919SPawel Jakub Dawidek }
621eaa3b919SPawel Jakub Dawidek 
6222f07cdf8SPawel Jakub Dawidek static __inline off_t
eli_mediasize(const struct g_eli_softc * sc,off_t mediasize,u_int sectorsize)6232f07cdf8SPawel Jakub Dawidek eli_mediasize(const struct g_eli_softc *sc, off_t mediasize, u_int sectorsize)
6242f07cdf8SPawel Jakub Dawidek {
6252f07cdf8SPawel Jakub Dawidek 
6262f07cdf8SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) {
6272f07cdf8SPawel Jakub Dawidek 		mediasize -= sectorsize;
6282f07cdf8SPawel Jakub Dawidek 	}
6292f07cdf8SPawel Jakub Dawidek 	if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0) {
6302f07cdf8SPawel Jakub Dawidek 		mediasize -= (mediasize % sc->sc_sectorsize);
6312f07cdf8SPawel Jakub Dawidek 	} else {
6322f07cdf8SPawel Jakub Dawidek 		mediasize /= sc->sc_bytes_per_sector;
6332f07cdf8SPawel Jakub Dawidek 		mediasize *= sc->sc_sectorsize;
6342f07cdf8SPawel Jakub Dawidek 	}
6352f07cdf8SPawel Jakub Dawidek 
6362f07cdf8SPawel Jakub Dawidek 	return (mediasize);
6372f07cdf8SPawel Jakub Dawidek }
6382f07cdf8SPawel Jakub Dawidek 
6394332fecaSAllan Jude static __inline void
eli_metadata_softc(struct g_eli_softc * sc,const struct g_eli_metadata * md,u_int sectorsize,off_t mediasize)6404332fecaSAllan Jude eli_metadata_softc(struct g_eli_softc *sc, const struct g_eli_metadata *md,
6414332fecaSAllan Jude     u_int sectorsize, off_t mediasize)
6424332fecaSAllan Jude {
6434332fecaSAllan Jude 
6444332fecaSAllan Jude 	sc->sc_version = md->md_version;
6454332fecaSAllan Jude 	sc->sc_inflight = 0;
6464332fecaSAllan Jude 	sc->sc_crypto = G_ELI_CRYPTO_UNKNOWN;
6474332fecaSAllan Jude 	sc->sc_flags = md->md_flags;
6484332fecaSAllan Jude 	/* Backward compatibility. */
6494332fecaSAllan Jude 	if (md->md_version < G_ELI_VERSION_04)
6504332fecaSAllan Jude 		sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER;
6514332fecaSAllan Jude 	if (md->md_version < G_ELI_VERSION_05)
6524332fecaSAllan Jude 		sc->sc_flags |= G_ELI_FLAG_SINGLE_KEY;
6534332fecaSAllan Jude 	if (md->md_version < G_ELI_VERSION_06 &&
6544332fecaSAllan Jude 	    (sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
6554332fecaSAllan Jude 		sc->sc_flags |= G_ELI_FLAG_FIRST_KEY;
6564332fecaSAllan Jude 	}
6574332fecaSAllan Jude 	if (md->md_version < G_ELI_VERSION_07)
6584332fecaSAllan Jude 		sc->sc_flags |= G_ELI_FLAG_ENC_IVKEY;
6594332fecaSAllan Jude 	sc->sc_ealgo = md->md_ealgo;
6604332fecaSAllan Jude 
6614332fecaSAllan Jude 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
6624332fecaSAllan Jude 		sc->sc_akeylen = sizeof(sc->sc_akey) * 8;
6634332fecaSAllan Jude 		sc->sc_aalgo = md->md_aalgo;
6644332fecaSAllan Jude 		sc->sc_alen = g_eli_hashlen(sc->sc_aalgo);
6654332fecaSAllan Jude 
6664332fecaSAllan Jude 		sc->sc_data_per_sector = sectorsize - sc->sc_alen;
6674332fecaSAllan Jude 		/*
6684332fecaSAllan Jude 		 * Some hash functions (like SHA1 and RIPEMD160) generates hash
6694332fecaSAllan Jude 		 * which length is not multiple of 128 bits, but we want data
6704332fecaSAllan Jude 		 * length to be multiple of 128, so we can encrypt without
6714332fecaSAllan Jude 		 * padding. The line below rounds down data length to multiple
6724332fecaSAllan Jude 		 * of 128 bits.
6734332fecaSAllan Jude 		 */
6744332fecaSAllan Jude 		sc->sc_data_per_sector -= sc->sc_data_per_sector % 16;
6754332fecaSAllan Jude 
6764332fecaSAllan Jude 		sc->sc_bytes_per_sector =
6774332fecaSAllan Jude 		    (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1;
6784332fecaSAllan Jude 		sc->sc_bytes_per_sector *= sectorsize;
6794332fecaSAllan Jude 	}
6802f07cdf8SPawel Jakub Dawidek 	sc->sc_provsize = mediasize;
6814332fecaSAllan Jude 	sc->sc_sectorsize = md->md_sectorsize;
6822f07cdf8SPawel Jakub Dawidek 	sc->sc_mediasize = eli_mediasize(sc, mediasize, sectorsize);
6834332fecaSAllan Jude 	sc->sc_ekeylen = md->md_keylen;
6844332fecaSAllan Jude }
6854332fecaSAllan Jude 
686c58794deSPawel Jakub Dawidek #ifdef _KERNEL
687c58794deSPawel Jakub Dawidek int g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
688c58794deSPawel Jakub Dawidek     struct g_eli_metadata *md);
689c58794deSPawel Jakub Dawidek struct g_geom *g_eli_create(struct gctl_req *req, struct g_class *mp,
690c58794deSPawel Jakub Dawidek     struct g_provider *bpp, const struct g_eli_metadata *md,
691c58794deSPawel Jakub Dawidek     const u_char *mkey, int nkey);
692c58794deSPawel Jakub Dawidek int g_eli_destroy(struct g_eli_softc *sc, boolean_t force);
693c58794deSPawel Jakub Dawidek 
694c58794deSPawel Jakub Dawidek int g_eli_access(struct g_provider *pp, int dr, int dw, int de);
695c58794deSPawel Jakub Dawidek void g_eli_config(struct gctl_req *req, struct g_class *mp, const char *verb);
696eaa3b919SPawel Jakub Dawidek 
697eaa3b919SPawel Jakub Dawidek void g_eli_read_done(struct bio *bp);
698eaa3b919SPawel Jakub Dawidek void g_eli_write_done(struct bio *bp);
699eaa3b919SPawel Jakub Dawidek int g_eli_crypto_rerun(struct cryptop *crp);
700eaa3b919SPawel Jakub Dawidek 
7012dbc9a38SGleb Smirnoff bool g_eli_alloc_data(struct bio *bp, int sz);
7022dbc9a38SGleb Smirnoff void g_eli_free_data(struct bio *bp);
7032dbc9a38SGleb Smirnoff 
7045ad4a7c7SPawel Jakub Dawidek void g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker);
705eaa3b919SPawel Jakub Dawidek void g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp);
706eaa3b919SPawel Jakub Dawidek 
707eaa3b919SPawel Jakub Dawidek void g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp);
708eaa3b919SPawel Jakub Dawidek void g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp);
709c58794deSPawel Jakub Dawidek #endif
7104332fecaSAllan Jude void g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
7114332fecaSAllan Jude     size_t size);
712c58794deSPawel Jakub Dawidek 
713c58794deSPawel Jakub Dawidek void g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key);
714c58794deSPawel Jakub Dawidek int g_eli_mkey_decrypt(const struct g_eli_metadata *md,
71531f7586dSMariusz Zaborski     const unsigned char *key, unsigned char *mkey, unsigned keyp);
71631f7586dSMariusz Zaborski int g_eli_mkey_decrypt_any(const struct g_eli_metadata *md,
717c58794deSPawel Jakub Dawidek     const unsigned char *key, unsigned char *mkey, unsigned *nkeyp);
718c58794deSPawel Jakub Dawidek int g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen,
719c58794deSPawel Jakub Dawidek     unsigned char *mkey);
720eaa3b919SPawel Jakub Dawidek #ifdef _KERNEL
721eaa3b919SPawel Jakub Dawidek void g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey);
722eaa3b919SPawel Jakub Dawidek #endif
723c58794deSPawel Jakub Dawidek 
724c58794deSPawel Jakub Dawidek int g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
725c58794deSPawel Jakub Dawidek     const u_char *key, size_t keysize);
726c58794deSPawel Jakub Dawidek int g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
727c58794deSPawel Jakub Dawidek     const u_char *key, size_t keysize);
728c58794deSPawel Jakub Dawidek 
729c58794deSPawel Jakub Dawidek struct hmac_ctx {
73085c15ab8SAllan Jude 	SHA512_CTX	innerctx;
73185c15ab8SAllan Jude 	SHA512_CTX	outerctx;
732c58794deSPawel Jakub Dawidek };
733c58794deSPawel Jakub Dawidek 
734e7630efbSMariusz Zaborski void g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const char *hkey,
735c58794deSPawel Jakub Dawidek     size_t hkeylen);
736c58794deSPawel Jakub Dawidek void g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data,
737c58794deSPawel Jakub Dawidek     size_t datasize);
738c58794deSPawel Jakub Dawidek void g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize);
739e7630efbSMariusz Zaborski void g_eli_crypto_hmac(const char *hkey, size_t hkeysize,
740c58794deSPawel Jakub Dawidek     const uint8_t *data, size_t datasize, uint8_t *md, size_t mdsize);
7411e09ff3dSPawel Jakub Dawidek 
7424332fecaSAllan Jude void g_eli_key_fill(struct g_eli_softc *sc, struct g_eli_key *key,
7434332fecaSAllan Jude     uint64_t keyno);
7441e09ff3dSPawel Jakub Dawidek #ifdef _KERNEL
7451e09ff3dSPawel Jakub Dawidek void g_eli_key_init(struct g_eli_softc *sc);
7461e09ff3dSPawel Jakub Dawidek void g_eli_key_destroy(struct g_eli_softc *sc);
7472f07cdf8SPawel Jakub Dawidek void g_eli_key_resize(struct g_eli_softc *sc);
7481e09ff3dSPawel Jakub Dawidek uint8_t *g_eli_key_hold(struct g_eli_softc *sc, off_t offset, size_t blocksize);
7491e09ff3dSPawel Jakub Dawidek void g_eli_key_drop(struct g_eli_softc *sc, uint8_t *rawkey);
7501e09ff3dSPawel Jakub Dawidek #endif
751c58794deSPawel Jakub Dawidek #endif	/* !_G_ELI_H_ */
752