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