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