xref: /freebsd/sys/geom/eli/g_eli.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2019 Pawel Jakub Dawidek <pawel@dawidek.net>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef	_G_ELI_H_
32 #define	_G_ELI_H_
33 
34 #include <sys/endian.h>
35 #include <sys/errno.h>
36 #include <sys/malloc.h>
37 #include <crypto/sha2/sha256.h>
38 #include <crypto/sha2/sha512.h>
39 #include <opencrypto/cryptodev.h>
40 #ifdef _KERNEL
41 #include <sys/bio.h>
42 #include <sys/libkern.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <geom/geom.h>
46 #include <crypto/intake.h>
47 #else
48 #include <assert.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <strings.h>
52 #endif
53 #include <sys/queue.h>
54 #include <sys/tree.h>
55 #ifndef _OpenSSL_
56 #include <sys/md5.h>
57 #endif
58 
59 #define	G_ELI_CLASS_NAME	"ELI"
60 #define	G_ELI_MAGIC		"GEOM::ELI"
61 #define	G_ELI_SUFFIX		".eli"
62 
63 /*
64  * Version history:
65  * 0 - Initial version number.
66  * 1 - Added data authentication support (md_aalgo field and
67  *     G_ELI_FLAG_AUTH flag).
68  * 2 - Added G_ELI_FLAG_READONLY.
69  * 3 - Added 'configure' subcommand.
70  * 4 - IV is generated from offset converted to little-endian
71  *     (the G_ELI_FLAG_NATIVE_BYTE_ORDER flag will be set for older versions).
72  * 5 - Added multiple encrypton keys and AES-XTS support.
73  * 6 - Fixed usage of multiple keys for authenticated providers (the
74  *     G_ELI_FLAG_FIRST_KEY flag will be set for older versions).
75  * 7 - Encryption keys are now generated from the Data Key and not from the
76  *     IV Key (the G_ELI_FLAG_ENC_IVKEY flag will be set for older versions).
77  */
78 #define	G_ELI_VERSION_00	0
79 #define	G_ELI_VERSION_01	1
80 #define	G_ELI_VERSION_02	2
81 #define	G_ELI_VERSION_03	3
82 #define	G_ELI_VERSION_04	4
83 #define	G_ELI_VERSION_05	5
84 #define	G_ELI_VERSION_06	6
85 #define	G_ELI_VERSION_07	7
86 #define	G_ELI_VERSION		G_ELI_VERSION_07
87 
88 /* ON DISK FLAGS. */
89 /* Use random, onetime keys. */
90 #define	G_ELI_FLAG_ONETIME		0x00000001
91 /* Ask for the passphrase from the kernel, before mounting root. */
92 #define	G_ELI_FLAG_BOOT			0x00000002
93 /* Detach on last close, if we were open for writing. */
94 #define	G_ELI_FLAG_WO_DETACH		0x00000004
95 /* Detach on last close. */
96 #define	G_ELI_FLAG_RW_DETACH		0x00000008
97 /* Provide data authentication. */
98 #define	G_ELI_FLAG_AUTH			0x00000010
99 /* Provider is read-only, we should deny all write attempts. */
100 #define	G_ELI_FLAG_RO			0x00000020
101 /* Don't pass through BIO_DELETE requests. */
102 #define	G_ELI_FLAG_NODELETE		0x00000040
103 /* This GELI supports GELIBoot */
104 #define	G_ELI_FLAG_GELIBOOT		0x00000080
105 /* Hide passphrase length in GELIboot. */
106 #define	G_ELI_FLAG_GELIDISPLAYPASS	0x00000100
107 /* Expand provider automatically. */
108 #define	G_ELI_FLAG_AUTORESIZE		0x00000200
109 
110 /* RUNTIME FLAGS. */
111 /* Provider was open for writing. */
112 #define	G_ELI_FLAG_WOPEN		0x00010000
113 /* Destroy device. */
114 #define	G_ELI_FLAG_DESTROY		0x00020000
115 /* Provider uses native byte-order for IV generation. */
116 #define	G_ELI_FLAG_NATIVE_BYTE_ORDER	0x00040000
117 /* Provider uses single encryption key. */
118 #define	G_ELI_FLAG_SINGLE_KEY		0x00080000
119 /* Device suspended. */
120 #define	G_ELI_FLAG_SUSPEND		0x00100000
121 /* Provider uses first encryption key. */
122 #define	G_ELI_FLAG_FIRST_KEY		0x00200000
123 /* Provider uses IV-Key for encryption key generation. */
124 #define	G_ELI_FLAG_ENC_IVKEY		0x00400000
125 
126 /* BIO pflag values. */
127 #define	G_ELI_WORKER(pflags)	((pflags) & 0xff)
128 #define	G_ELI_MAX_WORKERS	255
129 #define	G_ELI_NEW_BIO		G_ELI_MAX_WORKERS
130 #define	G_ELI_SETWORKER(pflags, w)	\
131     (pflags) = ((pflags) & 0xff00) | ((w) & 0xff)
132 #define	G_ELI_SET_NEW_BIO(pflags)	G_ELI_SETWORKER((pflags), G_ELI_NEW_BIO)
133 #define	G_ELI_IS_NEW_BIO(pflags)	(G_ELI_WORKER(pflags) == G_ELI_NEW_BIO)
134 #define	G_ELI_UMA_ALLOC		0x100	/* bio_driver2 alloc came from UMA */
135 
136 #define	SHA512_MDLEN		64
137 #define	G_ELI_AUTH_SECKEYLEN	SHA256_DIGEST_LENGTH
138 
139 #define	G_ELI_MAXMKEYS		2
140 #define	G_ELI_MAXKEYLEN		64
141 #define	G_ELI_USERKEYLEN	G_ELI_MAXKEYLEN
142 #define	G_ELI_DATAKEYLEN	G_ELI_MAXKEYLEN
143 #define	G_ELI_AUTHKEYLEN	G_ELI_MAXKEYLEN
144 #define	G_ELI_IVKEYLEN		G_ELI_MAXKEYLEN
145 #define	G_ELI_SALTLEN		64
146 #define	G_ELI_DATAIVKEYLEN	(G_ELI_DATAKEYLEN + G_ELI_IVKEYLEN)
147 /* Data-Key, IV-Key, HMAC_SHA512(Derived-Key, Data-Key+IV-Key) */
148 #define	G_ELI_MKEYLEN		(G_ELI_DATAIVKEYLEN + SHA512_MDLEN)
149 #define	G_ELI_OVERWRITES	5
150 /* Switch data encryption key every 2^20 blocks. */
151 #define	G_ELI_KEY_SHIFT		20
152 
153 #define	G_ELI_CRYPTO_UNKNOWN	0
154 #define	G_ELI_CRYPTO_HW		1
155 #define	G_ELI_CRYPTO_SW		2
156 #define	G_ELI_CRYPTO_SW_ACCEL	3
157 
158 #ifdef _KERNEL
159 #if (MAX_KEY_BYTES < G_ELI_DATAIVKEYLEN)
160 #error "MAX_KEY_BYTES is less than G_ELI_DATAKEYLEN"
161 #endif
162 
163 extern int g_eli_debug;
164 extern u_int g_eli_overwrites;
165 extern u_int g_eli_batch;
166 
167 #define	G_ELI_DEBUG(lvl, ...) \
168     _GEOM_DEBUG("GEOM_ELI", g_eli_debug, (lvl), NULL, __VA_ARGS__)
169 #define	G_ELI_LOGREQ(lvl, bp, ...) \
170     _GEOM_DEBUG("GEOM_ELI", g_eli_debug, (lvl), (bp), __VA_ARGS__)
171 
172 struct g_eli_worker {
173 	struct g_eli_softc	*w_softc;
174 	struct proc		*w_proc;
175 	void			*w_first_key;
176 	u_int			 w_number;
177 	crypto_session_t	 w_sid;
178 	boolean_t		 w_active;
179 	LIST_ENTRY(g_eli_worker) w_next;
180 };
181 
182 #endif	/* _KERNEL */
183 
184 struct g_eli_softc {
185 	struct g_geom	*sc_geom;
186 	u_int		 sc_version;
187 	u_int		 sc_crypto;
188 	uint8_t		 sc_mkey[G_ELI_DATAIVKEYLEN];
189 	uint8_t		 sc_ekey[G_ELI_DATAKEYLEN];
190 	TAILQ_HEAD(, g_eli_key) sc_ekeys_queue;
191 	RB_HEAD(g_eli_key_tree, g_eli_key) sc_ekeys_tree;
192 #ifndef _STANDALONE
193 	struct mtx	 sc_ekeys_lock;
194 #endif
195 	uint64_t	 sc_ekeys_total;
196 	uint64_t	 sc_ekeys_allocated;
197 	u_int		 sc_ealgo;
198 	u_int		 sc_ekeylen;
199 	uint8_t		 sc_akey[G_ELI_AUTHKEYLEN];
200 	u_int		 sc_aalgo;
201 	u_int		 sc_akeylen;
202 	u_int		 sc_alen;
203 	SHA256_CTX	 sc_akeyctx;
204 	uint8_t		 sc_ivkey[G_ELI_IVKEYLEN];
205 	SHA256_CTX	 sc_ivctx;
206 	int		 sc_nkey;
207 	uint32_t	 sc_flags;
208 	int		 sc_inflight;
209 	off_t		 sc_mediasize;
210 	size_t		 sc_sectorsize;
211 	off_t		 sc_provsize;
212 	u_int		 sc_bytes_per_sector;
213 	u_int		 sc_data_per_sector;
214 #ifndef _KERNEL
215 	int		 sc_cpubind;
216 #else /* _KERNEL */
217 	boolean_t	 sc_cpubind;
218 
219 	/* Only for software cryptography. */
220 	struct bio_queue_head sc_queue;
221 	struct mtx	 sc_queue_mtx;
222 	LIST_HEAD(, g_eli_worker) sc_workers;
223 #endif /* _KERNEL */
224 };
225 #define	sc_name		 sc_geom->name
226 
227 #define	G_ELI_KEY_MAGIC	0xe11341c
228 
229 struct g_eli_key {
230 	/* Key value, must be first in the structure. */
231 	uint8_t		gek_key[G_ELI_DATAKEYLEN];
232 	/* Magic. */
233 	int		gek_magic;
234 	/* Key number. */
235 	uint64_t	gek_keyno;
236 	/* Reference counter. */
237 	int		gek_count;
238 	/* Keeps keys sorted by most recent use. */
239 	TAILQ_ENTRY(g_eli_key) gek_next;
240 	/* Keeps keys sorted by number. */
241 	RB_ENTRY(g_eli_key) gek_link;
242 };
243 
244 struct g_eli_metadata {
245 	char		md_magic[16];	/* Magic value. */
246 	uint32_t	md_version;	/* Version number. */
247 	uint32_t	md_flags;	/* Additional flags. */
248 	uint16_t	md_ealgo;	/* Encryption algorithm. */
249 	uint16_t	md_keylen;	/* Key length. */
250 	uint16_t	md_aalgo;	/* Authentication algorithm. */
251 	uint64_t	md_provsize;	/* Provider's size. */
252 	uint32_t	md_sectorsize;	/* Sector size. */
253 	uint8_t		md_keys;	/* Available keys. */
254 	int32_t		md_iterations;	/* Number of iterations for PKCS#5v2. */
255 	uint8_t		md_salt[G_ELI_SALTLEN]; /* Salt. */
256 			/* Encrypted master key (IV-key, Data-key, HMAC). */
257 	uint8_t		md_mkeys[G_ELI_MAXMKEYS * G_ELI_MKEYLEN];
258 	u_char		md_hash[16];	/* MD5 hash. */
259 } __packed;
260 #ifndef _OpenSSL_
261 static __inline void
262 eli_metadata_encode_v0(struct g_eli_metadata *md, u_char **datap)
263 {
264 	u_char *p;
265 
266 	p = *datap;
267 	le32enc(p, md->md_flags);	p += sizeof(md->md_flags);
268 	le16enc(p, md->md_ealgo);	p += sizeof(md->md_ealgo);
269 	le16enc(p, md->md_keylen);	p += sizeof(md->md_keylen);
270 	le64enc(p, md->md_provsize);	p += sizeof(md->md_provsize);
271 	le32enc(p, md->md_sectorsize);	p += sizeof(md->md_sectorsize);
272 	*p = md->md_keys;		p += sizeof(md->md_keys);
273 	le32enc(p, md->md_iterations);	p += sizeof(md->md_iterations);
274 	bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt);
275 	bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
276 	*datap = p;
277 }
278 static __inline void
279 eli_metadata_encode_v1v2v3v4v5v6v7(struct g_eli_metadata *md, u_char **datap)
280 {
281 	u_char *p;
282 
283 	p = *datap;
284 	le32enc(p, md->md_flags);	p += sizeof(md->md_flags);
285 	le16enc(p, md->md_ealgo);	p += sizeof(md->md_ealgo);
286 	le16enc(p, md->md_keylen);	p += sizeof(md->md_keylen);
287 	le16enc(p, md->md_aalgo);	p += sizeof(md->md_aalgo);
288 	le64enc(p, md->md_provsize);	p += sizeof(md->md_provsize);
289 	le32enc(p, md->md_sectorsize);	p += sizeof(md->md_sectorsize);
290 	*p = md->md_keys;		p += sizeof(md->md_keys);
291 	le32enc(p, md->md_iterations);	p += sizeof(md->md_iterations);
292 	bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt);
293 	bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
294 	*datap = p;
295 }
296 static __inline void
297 eli_metadata_encode(struct g_eli_metadata *md, u_char *data)
298 {
299 	uint32_t hash[4];
300 	MD5_CTX ctx;
301 	u_char *p;
302 
303 	p = data;
304 	bcopy(md->md_magic, p, sizeof(md->md_magic));
305 	p += sizeof(md->md_magic);
306 	le32enc(p, md->md_version);
307 	p += sizeof(md->md_version);
308 	switch (md->md_version) {
309 	case G_ELI_VERSION_00:
310 		eli_metadata_encode_v0(md, &p);
311 		break;
312 	case G_ELI_VERSION_01:
313 	case G_ELI_VERSION_02:
314 	case G_ELI_VERSION_03:
315 	case G_ELI_VERSION_04:
316 	case G_ELI_VERSION_05:
317 	case G_ELI_VERSION_06:
318 	case G_ELI_VERSION_07:
319 		eli_metadata_encode_v1v2v3v4v5v6v7(md, &p);
320 		break;
321 	default:
322 #ifdef _KERNEL
323 		panic("%s: Unsupported version %u.", __func__,
324 		    (u_int)md->md_version);
325 #else
326 		assert(!"Unsupported metadata version.");
327 #endif
328 	}
329 	MD5Init(&ctx);
330 	MD5Update(&ctx, data, p - data);
331 	MD5Final((void *)hash, &ctx);
332 	bcopy(hash, md->md_hash, sizeof(md->md_hash));
333 	bcopy(md->md_hash, p, sizeof(md->md_hash));
334 }
335 static __inline int
336 eli_metadata_decode_v0(const u_char *data, struct g_eli_metadata *md)
337 {
338 	uint32_t hash[4];
339 	MD5_CTX ctx;
340 	const u_char *p;
341 
342 	p = data + sizeof(md->md_magic) + sizeof(md->md_version);
343 	md->md_flags = le32dec(p);	p += sizeof(md->md_flags);
344 	md->md_ealgo = le16dec(p);	p += sizeof(md->md_ealgo);
345 	md->md_keylen = le16dec(p);	p += sizeof(md->md_keylen);
346 	md->md_provsize = le64dec(p);	p += sizeof(md->md_provsize);
347 	md->md_sectorsize = le32dec(p);	p += sizeof(md->md_sectorsize);
348 	md->md_keys = *p;		p += sizeof(md->md_keys);
349 	md->md_iterations = le32dec(p);	p += sizeof(md->md_iterations);
350 	bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
351 	bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
352 	MD5Init(&ctx);
353 	MD5Update(&ctx, data, p - data);
354 	MD5Final((void *)hash, &ctx);
355 	bcopy(hash, md->md_hash, sizeof(md->md_hash));
356 	if (bcmp(md->md_hash, p, 16) != 0)
357 		return (EINVAL);
358 	return (0);
359 }
360 
361 static __inline int
362 eli_metadata_decode_v1v2v3v4v5v6v7(const u_char *data, struct g_eli_metadata *md)
363 {
364 	uint32_t hash[4];
365 	MD5_CTX ctx;
366 	const u_char *p;
367 
368 	p = data + sizeof(md->md_magic) + sizeof(md->md_version);
369 	md->md_flags = le32dec(p);	p += sizeof(md->md_flags);
370 	md->md_ealgo = le16dec(p);	p += sizeof(md->md_ealgo);
371 	md->md_keylen = le16dec(p);	p += sizeof(md->md_keylen);
372 	md->md_aalgo = le16dec(p);	p += sizeof(md->md_aalgo);
373 	md->md_provsize = le64dec(p);	p += sizeof(md->md_provsize);
374 	md->md_sectorsize = le32dec(p);	p += sizeof(md->md_sectorsize);
375 	md->md_keys = *p;		p += sizeof(md->md_keys);
376 	md->md_iterations = le32dec(p);	p += sizeof(md->md_iterations);
377 	bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
378 	bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
379 	MD5Init(&ctx);
380 	MD5Update(&ctx, data, p - data);
381 	MD5Final((void *)hash, &ctx);
382 	bcopy(hash, md->md_hash, sizeof(md->md_hash));
383 	if (bcmp(md->md_hash, p, 16) != 0)
384 		return (EINVAL);
385 	return (0);
386 }
387 static __inline int
388 eli_metadata_decode(const u_char *data, struct g_eli_metadata *md)
389 {
390 	int error;
391 
392 	bcopy(data, md->md_magic, sizeof(md->md_magic));
393 	if (strcmp(md->md_magic, G_ELI_MAGIC) != 0)
394 		return (EINVAL);
395 	md->md_version = le32dec(data + sizeof(md->md_magic));
396 	switch (md->md_version) {
397 	case G_ELI_VERSION_00:
398 		error = eli_metadata_decode_v0(data, md);
399 		break;
400 	case G_ELI_VERSION_01:
401 	case G_ELI_VERSION_02:
402 	case G_ELI_VERSION_03:
403 	case G_ELI_VERSION_04:
404 	case G_ELI_VERSION_05:
405 	case G_ELI_VERSION_06:
406 	case G_ELI_VERSION_07:
407 		error = eli_metadata_decode_v1v2v3v4v5v6v7(data, md);
408 		break;
409 	default:
410 		error = EOPNOTSUPP;
411 		break;
412 	}
413 	return (error);
414 }
415 #endif	/* !_OpenSSL */
416 
417 static __inline u_int
418 g_eli_str2ealgo(const char *name)
419 {
420 
421 	if (strcasecmp("null", name) == 0)
422 		return (CRYPTO_NULL_CBC);
423 	else if (strcasecmp("null-cbc", name) == 0)
424 		return (CRYPTO_NULL_CBC);
425 	else if (strcasecmp("aes", name) == 0)
426 		return (CRYPTO_AES_XTS);
427 	else if (strcasecmp("aes-cbc", name) == 0)
428 		return (CRYPTO_AES_CBC);
429 	else if (strcasecmp("aes-xts", name) == 0)
430 		return (CRYPTO_AES_XTS);
431 	else if (strcasecmp("camellia", name) == 0)
432 		return (CRYPTO_CAMELLIA_CBC);
433 	else if (strcasecmp("camellia-cbc", name) == 0)
434 		return (CRYPTO_CAMELLIA_CBC);
435 	return (CRYPTO_ALGORITHM_MIN - 1);
436 }
437 
438 static __inline u_int
439 g_eli_str2aalgo(const char *name)
440 {
441 
442 	if (strcasecmp("hmac/sha1", name) == 0)
443 		return (CRYPTO_SHA1_HMAC);
444 	else if (strcasecmp("hmac/ripemd160", name) == 0)
445 		return (CRYPTO_RIPEMD160_HMAC);
446 	else if (strcasecmp("hmac/sha256", name) == 0)
447 		return (CRYPTO_SHA2_256_HMAC);
448 	else if (strcasecmp("hmac/sha384", name) == 0)
449 		return (CRYPTO_SHA2_384_HMAC);
450 	else if (strcasecmp("hmac/sha512", name) == 0)
451 		return (CRYPTO_SHA2_512_HMAC);
452 	return (CRYPTO_ALGORITHM_MIN - 1);
453 }
454 
455 static __inline const char *
456 g_eli_algo2str(u_int algo)
457 {
458 
459 	switch (algo) {
460 	case CRYPTO_NULL_CBC:
461 		return ("NULL");
462 	case CRYPTO_AES_CBC:
463 		return ("AES-CBC");
464 	case CRYPTO_AES_XTS:
465 		return ("AES-XTS");
466 	case CRYPTO_CAMELLIA_CBC:
467 		return ("CAMELLIA-CBC");
468 	case CRYPTO_SHA1_HMAC:
469 		return ("HMAC/SHA1");
470 	case CRYPTO_RIPEMD160_HMAC:
471 		return ("HMAC/RIPEMD160");
472 	case CRYPTO_SHA2_256_HMAC:
473 		return ("HMAC/SHA256");
474 	case CRYPTO_SHA2_384_HMAC:
475 		return ("HMAC/SHA384");
476 	case CRYPTO_SHA2_512_HMAC:
477 		return ("HMAC/SHA512");
478 	}
479 	return ("unknown");
480 }
481 
482 static __inline void
483 eli_metadata_dump(const struct g_eli_metadata *md)
484 {
485 	static const char hex[] = "0123456789abcdef";
486 	char str[sizeof(md->md_mkeys) * 2 + 1];
487 	u_int i;
488 
489 	printf("     magic: %s\n", md->md_magic);
490 	printf("   version: %u\n", (u_int)md->md_version);
491 	printf("     flags: 0x%x\n", (u_int)md->md_flags);
492 	printf("     ealgo: %s\n", g_eli_algo2str(md->md_ealgo));
493 	printf("    keylen: %u\n", (u_int)md->md_keylen);
494 	if (md->md_flags & G_ELI_FLAG_AUTH)
495 		printf("     aalgo: %s\n", g_eli_algo2str(md->md_aalgo));
496 	printf("  provsize: %ju\n", (uintmax_t)md->md_provsize);
497 	printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
498 	printf("      keys: 0x%02x\n", (u_int)md->md_keys);
499 	printf("iterations: %d\n", (int)md->md_iterations);
500 	bzero(str, sizeof(str));
501 	for (i = 0; i < sizeof(md->md_salt); i++) {
502 		str[i * 2] = hex[md->md_salt[i] >> 4];
503 		str[i * 2 + 1] = hex[md->md_salt[i] & 0x0f];
504 	}
505 	printf("      Salt: %s\n", str);
506 	bzero(str, sizeof(str));
507 	for (i = 0; i < sizeof(md->md_mkeys); i++) {
508 		str[i * 2] = hex[md->md_mkeys[i] >> 4];
509 		str[i * 2 + 1] = hex[md->md_mkeys[i] & 0x0f];
510 	}
511 	printf("Master Key: %s\n", str);
512 	bzero(str, sizeof(str));
513 	for (i = 0; i < 16; i++) {
514 		str[i * 2] = hex[md->md_hash[i] >> 4];
515 		str[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
516 	}
517 	printf("  MD5 hash: %s\n", str);
518 }
519 
520 #ifdef _KERNEL
521 static __inline bool
522 eli_metadata_crypto_supported(const struct g_eli_metadata *md)
523 {
524 
525 	switch (md->md_ealgo) {
526 	case CRYPTO_NULL_CBC:
527 	case CRYPTO_AES_CBC:
528 	case CRYPTO_CAMELLIA_CBC:
529 	case CRYPTO_AES_XTS:
530 		break;
531 	default:
532 		return (false);
533 	}
534 	if (md->md_flags & G_ELI_FLAG_AUTH) {
535 		switch (md->md_aalgo) {
536 		case CRYPTO_SHA1_HMAC:
537 		case CRYPTO_RIPEMD160_HMAC:
538 		case CRYPTO_SHA2_256_HMAC:
539 		case CRYPTO_SHA2_384_HMAC:
540 		case CRYPTO_SHA2_512_HMAC:
541 			break;
542 		default:
543 			return (false);
544 		}
545 	}
546 	return (true);
547 }
548 #endif
549 
550 static __inline u_int
551 g_eli_keylen(u_int algo, u_int keylen)
552 {
553 
554 	switch (algo) {
555 	case CRYPTO_NULL_CBC:
556 		if (keylen == 0)
557 			keylen = 64 * 8;
558 		else {
559 			if (keylen > 64 * 8)
560 				keylen = 0;
561 		}
562 		return (keylen);
563 	case CRYPTO_AES_CBC:
564 	case CRYPTO_CAMELLIA_CBC:
565 		switch (keylen) {
566 		case 0:
567 			return (128);
568 		case 128:
569 		case 192:
570 		case 256:
571 			return (keylen);
572 		default:
573 			return (0);
574 		}
575 	case CRYPTO_AES_XTS:
576 		switch (keylen) {
577 		case 0:
578 			return (128);
579 		case 128:
580 		case 256:
581 			return (keylen);
582 		default:
583 			return (0);
584 		}
585 	default:
586 		return (0);
587 	}
588 }
589 
590 static __inline u_int
591 g_eli_ivlen(u_int algo)
592 {
593 
594 	switch (algo) {
595 	case CRYPTO_AES_XTS:
596 		return (AES_XTS_IV_LEN);
597 	case CRYPTO_AES_CBC:
598 		return (AES_BLOCK_LEN);
599 	case CRYPTO_CAMELLIA_CBC:
600 		return (CAMELLIA_BLOCK_LEN);
601 	}
602 	return (0);
603 }
604 
605 static __inline u_int
606 g_eli_hashlen(u_int algo)
607 {
608 
609 	switch (algo) {
610 	case CRYPTO_SHA1_HMAC:
611 		return (20);
612 	case CRYPTO_RIPEMD160_HMAC:
613 		return (20);
614 	case CRYPTO_SHA2_256_HMAC:
615 		return (32);
616 	case CRYPTO_SHA2_384_HMAC:
617 		return (48);
618 	case CRYPTO_SHA2_512_HMAC:
619 		return (64);
620 	}
621 	return (0);
622 }
623 
624 static __inline off_t
625 eli_mediasize(const struct g_eli_softc *sc, off_t mediasize, u_int sectorsize)
626 {
627 
628 	if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) {
629 		mediasize -= sectorsize;
630 	}
631 	if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0) {
632 		mediasize -= (mediasize % sc->sc_sectorsize);
633 	} else {
634 		mediasize /= sc->sc_bytes_per_sector;
635 		mediasize *= sc->sc_sectorsize;
636 	}
637 
638 	return (mediasize);
639 }
640 
641 static __inline void
642 eli_metadata_softc(struct g_eli_softc *sc, const struct g_eli_metadata *md,
643     u_int sectorsize, off_t mediasize)
644 {
645 
646 	sc->sc_version = md->md_version;
647 	sc->sc_inflight = 0;
648 	sc->sc_crypto = G_ELI_CRYPTO_UNKNOWN;
649 	sc->sc_flags = md->md_flags;
650 	/* Backward compatibility. */
651 	if (md->md_version < G_ELI_VERSION_04)
652 		sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER;
653 	if (md->md_version < G_ELI_VERSION_05)
654 		sc->sc_flags |= G_ELI_FLAG_SINGLE_KEY;
655 	if (md->md_version < G_ELI_VERSION_06 &&
656 	    (sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
657 		sc->sc_flags |= G_ELI_FLAG_FIRST_KEY;
658 	}
659 	if (md->md_version < G_ELI_VERSION_07)
660 		sc->sc_flags |= G_ELI_FLAG_ENC_IVKEY;
661 	sc->sc_ealgo = md->md_ealgo;
662 
663 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
664 		sc->sc_akeylen = sizeof(sc->sc_akey) * 8;
665 		sc->sc_aalgo = md->md_aalgo;
666 		sc->sc_alen = g_eli_hashlen(sc->sc_aalgo);
667 
668 		sc->sc_data_per_sector = sectorsize - sc->sc_alen;
669 		/*
670 		 * Some hash functions (like SHA1 and RIPEMD160) generates hash
671 		 * which length is not multiple of 128 bits, but we want data
672 		 * length to be multiple of 128, so we can encrypt without
673 		 * padding. The line below rounds down data length to multiple
674 		 * of 128 bits.
675 		 */
676 		sc->sc_data_per_sector -= sc->sc_data_per_sector % 16;
677 
678 		sc->sc_bytes_per_sector =
679 		    (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1;
680 		sc->sc_bytes_per_sector *= sectorsize;
681 	}
682 	sc->sc_provsize = mediasize;
683 	sc->sc_sectorsize = md->md_sectorsize;
684 	sc->sc_mediasize = eli_mediasize(sc, mediasize, sectorsize);
685 	sc->sc_ekeylen = md->md_keylen;
686 }
687 
688 #ifdef _KERNEL
689 int g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
690     struct g_eli_metadata *md);
691 struct g_geom *g_eli_create(struct gctl_req *req, struct g_class *mp,
692     struct g_provider *bpp, const struct g_eli_metadata *md,
693     const u_char *mkey, int nkey);
694 int g_eli_destroy(struct g_eli_softc *sc, boolean_t force);
695 
696 int g_eli_access(struct g_provider *pp, int dr, int dw, int de);
697 void g_eli_config(struct gctl_req *req, struct g_class *mp, const char *verb);
698 
699 void g_eli_read_done(struct bio *bp);
700 void g_eli_write_done(struct bio *bp);
701 int g_eli_crypto_rerun(struct cryptop *crp);
702 
703 bool g_eli_alloc_data(struct bio *bp, int sz);
704 void g_eli_free_data(struct bio *bp);
705 
706 void g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker);
707 void g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp);
708 
709 void g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp);
710 void g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp);
711 #endif
712 void g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
713     size_t size);
714 
715 void g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key);
716 int g_eli_mkey_decrypt(const struct g_eli_metadata *md,
717     const unsigned char *key, unsigned char *mkey, unsigned keyp);
718 int g_eli_mkey_decrypt_any(const struct g_eli_metadata *md,
719     const unsigned char *key, unsigned char *mkey, unsigned *nkeyp);
720 int g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen,
721     unsigned char *mkey);
722 #ifdef _KERNEL
723 void g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey);
724 #endif
725 
726 int g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
727     const u_char *key, size_t keysize);
728 int g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
729     const u_char *key, size_t keysize);
730 
731 struct hmac_ctx {
732 	SHA512_CTX	innerctx;
733 	SHA512_CTX	outerctx;
734 };
735 
736 void g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const char *hkey,
737     size_t hkeylen);
738 void g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data,
739     size_t datasize);
740 void g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize);
741 void g_eli_crypto_hmac(const char *hkey, size_t hkeysize,
742     const uint8_t *data, size_t datasize, uint8_t *md, size_t mdsize);
743 
744 void g_eli_key_fill(struct g_eli_softc *sc, struct g_eli_key *key,
745     uint64_t keyno);
746 #ifdef _KERNEL
747 void g_eli_key_init(struct g_eli_softc *sc);
748 void g_eli_key_destroy(struct g_eli_softc *sc);
749 void g_eli_key_resize(struct g_eli_softc *sc);
750 uint8_t *g_eli_key_hold(struct g_eli_softc *sc, off_t offset, size_t blocksize);
751 void g_eli_key_drop(struct g_eli_softc *sc, uint8_t *rawkey);
752 #endif
753 #endif	/* !_G_ELI_H_ */
754