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