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