xref: /freebsd/sys/geom/eli/g_eli.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
1 /*-
2  * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/bio.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <sys/kthread.h>
41 #include <sys/proc.h>
42 #include <sys/sched.h>
43 #include <sys/smp.h>
44 #include <sys/uio.h>
45 #include <sys/vnode.h>
46 
47 #include <vm/uma.h>
48 
49 #include <geom/geom.h>
50 #include <geom/eli/g_eli.h>
51 #include <geom/eli/pkcs5v2.h>
52 
53 
54 MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data");
55 
56 SYSCTL_DECL(_kern_geom);
57 SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW, 0, "GEOM_ELI stuff");
58 u_int g_eli_debug = 0;
59 TUNABLE_INT("kern.geom.eli.debug", &g_eli_debug);
60 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RW, &g_eli_debug, 0,
61     "Debug level");
62 static u_int g_eli_tries = 3;
63 TUNABLE_INT("kern.geom.eli.tries", &g_eli_tries);
64 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RW, &g_eli_tries, 0,
65     "Number of tries for entering the passphrase");
66 static u_int g_eli_visible_passphrase = 0;
67 TUNABLE_INT("kern.geom.eli.visible_passphrase", &g_eli_visible_passphrase);
68 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RW,
69     &g_eli_visible_passphrase, 0,
70     "Turn on echo when entering the passphrase (for debug purposes only!!)");
71 u_int g_eli_overwrites = 5;
72 TUNABLE_INT("kern.geom.eli.overwrites", &g_eli_overwrites);
73 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RW, &g_eli_overwrites,
74     0, "Number of times on-disk keys should be overwritten when destroying them");
75 static u_int g_eli_threads = 0;
76 TUNABLE_INT("kern.geom.eli.threads", &g_eli_threads);
77 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RW, &g_eli_threads, 0,
78     "Number of threads doing crypto work");
79 u_int g_eli_batch = 0;
80 TUNABLE_INT("kern.geom.eli.batch", &g_eli_batch);
81 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RW, &g_eli_batch, 0,
82     "Use crypto operations batching");
83 
84 static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp,
85     struct g_geom *gp);
86 
87 static g_taste_t g_eli_taste;
88 static g_dumpconf_t g_eli_dumpconf;
89 
90 struct g_class g_eli_class = {
91 	.name = G_ELI_CLASS_NAME,
92 	.version = G_VERSION,
93 	.ctlreq = g_eli_config,
94 	.taste = g_eli_taste,
95 	.destroy_geom = g_eli_destroy_geom
96 };
97 
98 
99 /*
100  * Code paths:
101  * BIO_READ:
102  *	g_eli_start -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
103  * BIO_WRITE:
104  *	g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
105  */
106 
107 
108 /*
109  * EAGAIN from crypto(9) means, that we were probably balanced to another crypto
110  * accelerator or something like this.
111  * The function updates the SID and rerun the operation.
112  */
113 int
114 g_eli_crypto_rerun(struct cryptop *crp)
115 {
116 	struct g_eli_softc *sc;
117 	struct g_eli_worker *wr;
118 	struct bio *bp;
119 	int error;
120 
121 	bp = (struct bio *)crp->crp_opaque;
122 	sc = bp->bio_to->geom->softc;
123 	LIST_FOREACH(wr, &sc->sc_workers, w_next) {
124 		if (wr->w_number == bp->bio_pflags)
125 			break;
126 	}
127 	KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags));
128 	G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %ju -> %ju).",
129 	    bp->bio_cmd == BIO_READ ? "READ" : "WRITE", (uintmax_t)wr->w_sid,
130 	    (uintmax_t)crp->crp_sid);
131 	wr->w_sid = crp->crp_sid;
132 	crp->crp_etype = 0;
133 	error = crypto_dispatch(crp);
134 	if (error == 0)
135 		return (0);
136 	G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error);
137 	crp->crp_etype = error;
138 	return (error);
139 }
140 
141 /*
142  * The function is called afer reading encrypted data from the provider.
143  *
144  * g_eli_start -> g_io_request -> G_ELI_READ_DONE -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
145  */
146 void
147 g_eli_read_done(struct bio *bp)
148 {
149 	struct g_eli_softc *sc;
150 	struct bio *pbp;
151 
152 	G_ELI_LOGREQ(2, bp, "Request done.");
153 	pbp = bp->bio_parent;
154 	if (pbp->bio_error == 0)
155 		pbp->bio_error = bp->bio_error;
156 	/*
157 	 * Do we have all sectors already?
158 	 */
159 	pbp->bio_inbed++;
160 	if (pbp->bio_inbed < pbp->bio_children)
161 		return;
162 	g_destroy_bio(bp);
163 	if (pbp->bio_error != 0) {
164 		G_ELI_LOGREQ(0, pbp, "%s() failed", __func__);
165 		pbp->bio_completed = 0;
166 		if (pbp->bio_driver2 != NULL) {
167 			free(pbp->bio_driver2, M_ELI);
168 			pbp->bio_driver2 = NULL;
169 		}
170 		g_io_deliver(pbp, pbp->bio_error);
171 		return;
172 	}
173 	sc = pbp->bio_to->geom->softc;
174 	mtx_lock(&sc->sc_queue_mtx);
175 	bioq_insert_tail(&sc->sc_queue, pbp);
176 	mtx_unlock(&sc->sc_queue_mtx);
177 	wakeup(sc);
178 }
179 
180 /*
181  * The function is called after we encrypt and write data.
182  *
183  * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver
184  */
185 void
186 g_eli_write_done(struct bio *bp)
187 {
188 	struct bio *pbp;
189 
190 	G_ELI_LOGREQ(2, bp, "Request done.");
191 	pbp = bp->bio_parent;
192 	if (pbp->bio_error == 0) {
193 		if (bp->bio_error != 0)
194 			pbp->bio_error = bp->bio_error;
195 	}
196 	/*
197 	 * Do we have all sectors already?
198 	 */
199 	pbp->bio_inbed++;
200 	if (pbp->bio_inbed < pbp->bio_children)
201 		return;
202 	free(pbp->bio_driver2, M_ELI);
203 	pbp->bio_driver2 = NULL;
204 	if (pbp->bio_error != 0) {
205 		G_ELI_LOGREQ(0, pbp, "Crypto WRITE request failed (error=%d).",
206 		    pbp->bio_error);
207 		pbp->bio_completed = 0;
208 	}
209 	g_destroy_bio(bp);
210 	/*
211 	 * Write is finished, send it up.
212 	 */
213 	pbp->bio_completed = pbp->bio_length;
214 	g_io_deliver(pbp, pbp->bio_error);
215 }
216 
217 /*
218  * This function should never be called, but GEOM made as it set ->orphan()
219  * method for every geom.
220  */
221 static void
222 g_eli_orphan_spoil_assert(struct g_consumer *cp)
223 {
224 
225 	panic("Function %s() called for %s.", __func__, cp->geom->name);
226 }
227 
228 static void
229 g_eli_orphan(struct g_consumer *cp)
230 {
231 	struct g_eli_softc *sc;
232 
233 	g_topology_assert();
234 	sc = cp->geom->softc;
235 	if (sc == NULL)
236 		return;
237 	g_eli_destroy(sc, 1);
238 }
239 
240 /*
241  * BIO_READ : G_ELI_START -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
242  * BIO_WRITE: G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
243  */
244 static void
245 g_eli_start(struct bio *bp)
246 {
247 	struct g_eli_softc *sc;
248 	struct g_consumer *cp;
249 	struct bio *cbp;
250 
251 	sc = bp->bio_to->geom->softc;
252 	KASSERT(sc != NULL,
253 	    ("Provider's error should be set (error=%d)(device=%s).",
254 	    bp->bio_to->error, bp->bio_to->name));
255 	G_ELI_LOGREQ(2, bp, "Request received.");
256 
257 	switch (bp->bio_cmd) {
258 	case BIO_READ:
259 	case BIO_WRITE:
260 	case BIO_GETATTR:
261 	case BIO_FLUSH:
262 		break;
263 	case BIO_DELETE:
264 		/*
265 		 * We could eventually support BIO_DELETE request.
266 		 * It could be done by overwritting requested sector with
267 		 * random data g_eli_overwrites number of times.
268 		 */
269 	default:
270 		g_io_deliver(bp, EOPNOTSUPP);
271 		return;
272 	}
273 	cbp = g_clone_bio(bp);
274 	if (cbp == NULL) {
275 		g_io_deliver(bp, ENOMEM);
276 		return;
277 	}
278 	switch (bp->bio_cmd) {
279 	case BIO_READ:
280 		if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) {
281 			bp->bio_driver2 = NULL;
282 			cbp->bio_done = g_eli_read_done;
283 			cp = LIST_FIRST(&sc->sc_geom->consumer);
284 			cbp->bio_to = cp->provider;
285 			G_ELI_LOGREQ(2, cbp, "Sending request.");
286 			/*
287 			 * Read encrypted data from provider.
288 			 */
289 			g_io_request(cbp, cp);
290 			break;
291 		}
292 		bp->bio_pflags = 255;
293 		/* FALLTHROUGH */
294 	case BIO_WRITE:
295 		bp->bio_driver1 = cbp;
296 		mtx_lock(&sc->sc_queue_mtx);
297 		bioq_insert_tail(&sc->sc_queue, bp);
298 		mtx_unlock(&sc->sc_queue_mtx);
299 		wakeup(sc);
300 		break;
301 	case BIO_GETATTR:
302 	case BIO_FLUSH:
303 		cbp->bio_done = g_std_done;
304 		cp = LIST_FIRST(&sc->sc_geom->consumer);
305 		cbp->bio_to = cp->provider;
306 		G_ELI_LOGREQ(2, cbp, "Sending request.");
307 		g_io_request(cbp, cp);
308 		break;
309 	}
310 }
311 
312 /*
313  * This is the main function for kernel worker thread when we don't have
314  * hardware acceleration and we have to do cryptography in software.
315  * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM
316  * threads with crypto work.
317  */
318 static void
319 g_eli_worker(void *arg)
320 {
321 	struct g_eli_softc *sc;
322 	struct g_eli_worker *wr;
323 	struct bio *bp;
324 
325 	wr = arg;
326 	sc = wr->w_softc;
327 	mtx_lock_spin(&sched_lock);
328 	sched_prio(curthread, PRIBIO);
329 	if (sc->sc_crypto == G_ELI_CRYPTO_SW && g_eli_threads == 0)
330 		sched_bind(curthread, wr->w_number);
331 	mtx_unlock_spin(&sched_lock);
332 
333 	G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm);
334 
335 	for (;;) {
336 		mtx_lock(&sc->sc_queue_mtx);
337 		bp = bioq_takefirst(&sc->sc_queue);
338 		if (bp == NULL) {
339 			if (sc->sc_flags & G_ELI_FLAG_DESTROY) {
340 				LIST_REMOVE(wr, w_next);
341 				crypto_freesession(wr->w_sid);
342 				free(wr, M_ELI);
343 				G_ELI_DEBUG(1, "Thread %s exiting.",
344 				    curthread->td_proc->p_comm);
345 				wakeup(&sc->sc_workers);
346 				mtx_unlock(&sc->sc_queue_mtx);
347 				kthread_exit(0);
348 			}
349 			msleep(sc, &sc->sc_queue_mtx, PRIBIO | PDROP,
350 			    "geli:w", 0);
351 			continue;
352 		}
353 		mtx_unlock(&sc->sc_queue_mtx);
354 		if (bp->bio_cmd == BIO_READ && bp->bio_pflags == 255)
355 			g_eli_auth_read(sc, bp);
356 		else if (sc->sc_flags & G_ELI_FLAG_AUTH)
357 			g_eli_auth_run(wr, bp);
358 		else
359 			g_eli_crypto_run(wr, bp);
360 	}
361 }
362 
363 /*
364  * Here we generate IV. It is unique for every sector.
365  */
366 void
367 g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
368     size_t size)
369 {
370 	u_char off[8], hash[SHA256_DIGEST_LENGTH];
371 	SHA256_CTX ctx;
372 
373 	if (!(sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER))
374 		le64enc(off, (uint64_t)offset);
375 	/* Copy precalculated SHA256 context for IV-Key. */
376 	bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx));
377 	SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset));
378 	SHA256_Final(hash, &ctx);
379 	bcopy(hash, iv, size);
380 }
381 
382 int
383 g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
384     struct g_eli_metadata *md)
385 {
386 	struct g_geom *gp;
387 	struct g_consumer *cp;
388 	u_char *buf = NULL;
389 	int error;
390 
391 	g_topology_assert();
392 
393 	gp = g_new_geomf(mp, "eli:taste");
394 	gp->start = g_eli_start;
395 	gp->access = g_std_access;
396 	/*
397 	 * g_eli_read_metadata() is always called from the event thread.
398 	 * Our geom is created and destroyed in the same event, so there
399 	 * could be no orphan nor spoil event in the meantime.
400 	 */
401 	gp->orphan = g_eli_orphan_spoil_assert;
402 	gp->spoiled = g_eli_orphan_spoil_assert;
403 	cp = g_new_consumer(gp);
404 	error = g_attach(cp, pp);
405 	if (error != 0)
406 		goto end;
407 	error = g_access(cp, 1, 0, 0);
408 	if (error != 0)
409 		goto end;
410 	g_topology_unlock();
411 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
412 	    &error);
413 	g_topology_lock();
414 	if (buf == NULL)
415 		goto end;
416 	eli_metadata_decode(buf, md);
417 end:
418 	if (buf != NULL)
419 		g_free(buf);
420 	if (cp->provider != NULL) {
421 		if (cp->acr == 1)
422 			g_access(cp, -1, 0, 0);
423 		g_detach(cp);
424 	}
425 	g_destroy_consumer(cp);
426 	g_destroy_geom(gp);
427 	return (error);
428 }
429 
430 /*
431  * The function is called when we had last close on provider and user requested
432  * to close it when this situation occur.
433  */
434 static void
435 g_eli_last_close(struct g_eli_softc *sc)
436 {
437 	struct g_geom *gp;
438 	struct g_provider *pp;
439 	char ppname[64];
440 	int error;
441 
442 	g_topology_assert();
443 	gp = sc->sc_geom;
444 	pp = LIST_FIRST(&gp->provider);
445 	strlcpy(ppname, pp->name, sizeof(ppname));
446 	error = g_eli_destroy(sc, 1);
447 	KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).",
448 	    ppname, error));
449 	G_ELI_DEBUG(0, "Detached %s on last close.", ppname);
450 }
451 
452 int
453 g_eli_access(struct g_provider *pp, int dr, int dw, int de)
454 {
455 	struct g_eli_softc *sc;
456 	struct g_geom *gp;
457 
458 	gp = pp->geom;
459 	sc = gp->softc;
460 
461 	if (dw > 0) {
462 		if (sc->sc_flags & G_ELI_FLAG_RO) {
463 			/* Deny write attempts. */
464 			return (EROFS);
465 		}
466 		/* Someone is opening us for write, we need to remember that. */
467 		sc->sc_flags |= G_ELI_FLAG_WOPEN;
468 		return (0);
469 	}
470 	/* Is this the last close? */
471 	if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0)
472 		return (0);
473 
474 	/*
475 	 * Automatically detach on last close if requested.
476 	 */
477 	if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) ||
478 	    (sc->sc_flags & G_ELI_FLAG_WOPEN)) {
479 		g_eli_last_close(sc);
480 	}
481 	return (0);
482 }
483 
484 static int
485 g_eli_cpu_is_disabled(int cpu)
486 {
487 #ifdef SMP
488 	return ((hlt_cpus_mask & (1 << cpu)) != 0);
489 #else
490 	return (0);
491 #endif
492 }
493 
494 struct g_geom *
495 g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp,
496     const struct g_eli_metadata *md, const u_char *mkey, int nkey)
497 {
498 	struct g_eli_softc *sc;
499 	struct g_eli_worker *wr;
500 	struct g_geom *gp;
501 	struct g_provider *pp;
502 	struct g_consumer *cp;
503 	struct cryptoini crie, cria;
504 	u_int i, threads;
505 	int error;
506 
507 	G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX);
508 
509 	gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX);
510 	gp->softc = NULL;	/* for a moment */
511 
512 	sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO);
513 	gp->start = g_eli_start;
514 	/*
515 	 * Spoiling cannot happen actually, because we keep provider open for
516 	 * writing all the time or provider is read-only.
517 	 */
518 	gp->spoiled = g_eli_orphan_spoil_assert;
519 	gp->orphan = g_eli_orphan;
520 	gp->dumpconf = g_eli_dumpconf;
521 	/*
522 	 * If detach-on-last-close feature is not enabled and we don't operate
523 	 * on read-only provider, we can simply use g_std_access().
524 	 */
525 	if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO))
526 		gp->access = g_eli_access;
527 	else
528 		gp->access = g_std_access;
529 
530 	sc->sc_crypto = G_ELI_CRYPTO_SW;
531 	sc->sc_flags = md->md_flags;
532 	/* Backward compatibility. */
533 	if (md->md_version < 2)
534 		sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER;
535 	sc->sc_ealgo = md->md_ealgo;
536 	sc->sc_nkey = nkey;
537 	/*
538 	 * Remember the keys in our softc structure.
539 	 */
540 	g_eli_mkey_propagate(sc, mkey);
541 	sc->sc_ekeylen = md->md_keylen;
542 
543 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
544 		sc->sc_akeylen = sizeof(sc->sc_akey) * 8;
545 		sc->sc_aalgo = md->md_aalgo;
546 		sc->sc_alen = g_eli_hashlen(sc->sc_aalgo);
547 
548 		sc->sc_data_per_sector = bpp->sectorsize - sc->sc_alen;
549 		/*
550 		 * Some hash functions (like SHA1 and RIPEMD160) generates hash
551 		 * which length is not multiple of 128 bits, but we want data
552 		 * length to be multiple of 128, so we can encrypt without
553 		 * padding. The line below rounds down data length to multiple
554 		 * of 128 bits.
555 		 */
556 		sc->sc_data_per_sector -= sc->sc_data_per_sector % 16;
557 
558 		sc->sc_bytes_per_sector =
559 		    (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1;
560 		sc->sc_bytes_per_sector *= bpp->sectorsize;
561 		/*
562 		 * Precalculate SHA256 for HMAC key generation.
563 		 * This is expensive operation and we can do it only once now or
564 		 * for every access to sector, so now will be much better.
565 		 */
566 		SHA256_Init(&sc->sc_akeyctx);
567 		SHA256_Update(&sc->sc_akeyctx, sc->sc_akey,
568 		    sizeof(sc->sc_akey));
569 	}
570 
571 	/*
572 	 * Precalculate SHA256 for IV generation.
573 	 * This is expensive operation and we can do it only once now or for
574 	 * every access to sector, so now will be much better.
575 	 */
576 	SHA256_Init(&sc->sc_ivctx);
577 	SHA256_Update(&sc->sc_ivctx, sc->sc_ivkey, sizeof(sc->sc_ivkey));
578 
579 	gp->softc = sc;
580 	sc->sc_geom = gp;
581 
582 	bioq_init(&sc->sc_queue);
583 	mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF);
584 
585 	pp = NULL;
586 	cp = g_new_consumer(gp);
587 	error = g_attach(cp, bpp);
588 	if (error != 0) {
589 		if (req != NULL) {
590 			gctl_error(req, "Cannot attach to %s (error=%d).",
591 			    bpp->name, error);
592 		} else {
593 			G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).",
594 			    bpp->name, error);
595 		}
596 		goto failed;
597 	}
598 	/*
599 	 * Keep provider open all the time, so we can run critical tasks,
600 	 * like Master Keys deletion, without wondering if we can open
601 	 * provider or not.
602 	 * We don't open provider for writing only when user requested read-only
603 	 * access.
604 	 */
605 	if (sc->sc_flags & G_ELI_FLAG_RO)
606 		error = g_access(cp, 1, 0, 1);
607 	else
608 		error = g_access(cp, 1, 1, 1);
609 	if (error != 0) {
610 		if (req != NULL) {
611 			gctl_error(req, "Cannot access %s (error=%d).",
612 			    bpp->name, error);
613 		} else {
614 			G_ELI_DEBUG(1, "Cannot access %s (error=%d).",
615 			    bpp->name, error);
616 		}
617 		goto failed;
618 	}
619 
620 	LIST_INIT(&sc->sc_workers);
621 
622 	bzero(&crie, sizeof(crie));
623 	crie.cri_alg = sc->sc_ealgo;
624 	crie.cri_klen = sc->sc_ekeylen;
625 	crie.cri_key = sc->sc_ekey;
626 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
627 		bzero(&cria, sizeof(cria));
628 		cria.cri_alg = sc->sc_aalgo;
629 		cria.cri_klen = sc->sc_akeylen;
630 		cria.cri_key = sc->sc_akey;
631 		crie.cri_next = &cria;
632 	}
633 
634 	threads = g_eli_threads;
635 	if (threads == 0)
636 		threads = mp_ncpus;
637 	else if (threads > mp_ncpus) {
638 		/* There is really no need for too many worker threads. */
639 		threads = mp_ncpus;
640 		G_ELI_DEBUG(0, "Reducing number of threads to %u.", threads);
641 	}
642 	for (i = 0; i < threads; i++) {
643 		if (g_eli_cpu_is_disabled(i)) {
644 			G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.",
645 			    bpp->name, i);
646 			continue;
647 		}
648 		wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO);
649 		wr->w_softc = sc;
650 		wr->w_number = i;
651 
652 		/*
653 		 * If this is the first pass, try to get hardware support.
654 		 * Use software cryptography, if we cannot get it.
655 		 */
656 		if (LIST_EMPTY(&sc->sc_workers)) {
657 			error = crypto_newsession(&wr->w_sid, &crie, 1);
658 			if (error == 0)
659 				sc->sc_crypto = G_ELI_CRYPTO_HW;
660 		}
661 		if (sc->sc_crypto == G_ELI_CRYPTO_SW)
662 			error = crypto_newsession(&wr->w_sid, &crie, 0);
663 		if (error != 0) {
664 			free(wr, M_ELI);
665 			if (req != NULL) {
666 				gctl_error(req, "Cannot set up crypto session "
667 				    "for %s (error=%d).", bpp->name, error);
668 			} else {
669 				G_ELI_DEBUG(1, "Cannot set up crypto session "
670 				    "for %s (error=%d).", bpp->name, error);
671 			}
672 			goto failed;
673 		}
674 
675 		error = kthread_create(g_eli_worker, wr, &wr->w_proc, 0, 0,
676 		    "g_eli[%u] %s", i, bpp->name);
677 		if (error != 0) {
678 			crypto_freesession(wr->w_sid);
679 			free(wr, M_ELI);
680 			if (req != NULL) {
681 				gctl_error(req, "Cannot create kernel thread "
682 				    "for %s (error=%d).", bpp->name, error);
683 			} else {
684 				G_ELI_DEBUG(1, "Cannot create kernel thread "
685 				    "for %s (error=%d).", bpp->name, error);
686 			}
687 			goto failed;
688 		}
689 		LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next);
690 		/* If we have hardware support, one thread is enough. */
691 		if (sc->sc_crypto == G_ELI_CRYPTO_HW)
692 			break;
693 	}
694 
695 	/*
696 	 * Create decrypted provider.
697 	 */
698 	pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX);
699 	pp->sectorsize = md->md_sectorsize;
700 	pp->mediasize = bpp->mediasize;
701 	if (!(sc->sc_flags & G_ELI_FLAG_ONETIME))
702 		pp->mediasize -= bpp->sectorsize;
703 	if (!(sc->sc_flags & G_ELI_FLAG_AUTH))
704 		pp->mediasize -= (pp->mediasize % pp->sectorsize);
705 	else {
706 		pp->mediasize /= sc->sc_bytes_per_sector;
707 		pp->mediasize *= pp->sectorsize;
708 	}
709 
710 	g_error_provider(pp, 0);
711 
712 	G_ELI_DEBUG(0, "Device %s created.", pp->name);
713 	G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo),
714 	    sc->sc_ekeylen);
715 	if (sc->sc_flags & G_ELI_FLAG_AUTH)
716 		G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo));
717 	G_ELI_DEBUG(0, "    Crypto: %s",
718 	    sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware");
719 	return (gp);
720 failed:
721 	mtx_lock(&sc->sc_queue_mtx);
722 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
723 	wakeup(sc);
724 	/*
725 	 * Wait for kernel threads self destruction.
726 	 */
727 	while (!LIST_EMPTY(&sc->sc_workers)) {
728 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
729 		    "geli:destroy", 0);
730 	}
731 	mtx_destroy(&sc->sc_queue_mtx);
732 	if (cp->provider != NULL) {
733 		if (cp->acr == 1)
734 			g_access(cp, -1, -1, -1);
735 		g_detach(cp);
736 	}
737 	g_destroy_consumer(cp);
738 	g_destroy_geom(gp);
739 	bzero(sc, sizeof(*sc));
740 	free(sc, M_ELI);
741 	return (NULL);
742 }
743 
744 int
745 g_eli_destroy(struct g_eli_softc *sc, boolean_t force)
746 {
747 	struct g_geom *gp;
748 	struct g_provider *pp;
749 
750 	g_topology_assert();
751 
752 	if (sc == NULL)
753 		return (ENXIO);
754 
755 	gp = sc->sc_geom;
756 	pp = LIST_FIRST(&gp->provider);
757 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
758 		if (force) {
759 			G_ELI_DEBUG(1, "Device %s is still open, so it "
760 			    "cannot be definitely removed.", pp->name);
761 		} else {
762 			G_ELI_DEBUG(1,
763 			    "Device %s is still open (r%dw%de%d).", pp->name,
764 			    pp->acr, pp->acw, pp->ace);
765 			return (EBUSY);
766 		}
767 	}
768 
769 	mtx_lock(&sc->sc_queue_mtx);
770 	sc->sc_flags |= G_ELI_FLAG_DESTROY;
771 	wakeup(sc);
772 	while (!LIST_EMPTY(&sc->sc_workers)) {
773 		msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
774 		    "geli:destroy", 0);
775 	}
776 	mtx_destroy(&sc->sc_queue_mtx);
777 	gp->softc = NULL;
778 	bzero(sc, sizeof(*sc));
779 	free(sc, M_ELI);
780 
781 	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
782 		G_ELI_DEBUG(0, "Device %s destroyed.", gp->name);
783 	g_wither_geom_close(gp, ENXIO);
784 
785 	return (0);
786 }
787 
788 static int
789 g_eli_destroy_geom(struct gctl_req *req __unused,
790     struct g_class *mp __unused, struct g_geom *gp)
791 {
792 	struct g_eli_softc *sc;
793 
794 	sc = gp->softc;
795 	return (g_eli_destroy(sc, 0));
796 }
797 
798 static int
799 g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider)
800 {
801 	u_char *keyfile, *data, *size;
802 	char *file, name[64];
803 	int i;
804 
805 	for (i = 0; ; i++) {
806 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
807 		keyfile = preload_search_by_type(name);
808 		if (keyfile == NULL)
809 			return (i);	/* Return number of loaded keyfiles. */
810 		data = preload_search_info(keyfile, MODINFO_ADDR);
811 		if (data == NULL) {
812 			G_ELI_DEBUG(0, "Cannot find key file data for %s.",
813 			    name);
814 			return (0);
815 		}
816 		data = *(void **)data;
817 		size = preload_search_info(keyfile, MODINFO_SIZE);
818 		if (size == NULL) {
819 			G_ELI_DEBUG(0, "Cannot find key file size for %s.",
820 			    name);
821 			return (0);
822 		}
823 		file = preload_search_info(keyfile, MODINFO_NAME);
824 		if (file == NULL) {
825 			G_ELI_DEBUG(0, "Cannot find key file name for %s.",
826 			    name);
827 			return (0);
828 		}
829 		G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file,
830 		    provider, name);
831 		g_eli_crypto_hmac_update(ctx, data, *(size_t *)size);
832 	}
833 }
834 
835 static void
836 g_eli_keyfiles_clear(const char *provider)
837 {
838 	u_char *keyfile, *data, *size;
839 	char name[64];
840 	int i;
841 
842 	for (i = 0; ; i++) {
843 		snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
844 		keyfile = preload_search_by_type(name);
845 		if (keyfile == NULL)
846 			return;
847 		data = preload_search_info(keyfile, MODINFO_ADDR);
848 		size = preload_search_info(keyfile, MODINFO_SIZE);
849 		if (data == NULL || size == NULL)
850 			continue;
851 		data = *(void **)data;
852 		bzero(data, *(size_t *)size);
853 	}
854 }
855 
856 /*
857  * Tasting is only made on boot.
858  * We detect providers which should be attached before root is mounted.
859  */
860 static struct g_geom *
861 g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
862 {
863 	struct g_eli_metadata md;
864 	struct g_geom *gp;
865 	struct hmac_ctx ctx;
866 	char passphrase[256];
867 	u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
868 	u_int i, nkey, nkeyfiles, tries;
869 	int error;
870 
871 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
872 	g_topology_assert();
873 
874 	if (rootvnode != NULL || g_eli_tries == 0)
875 		return (NULL);
876 
877 	G_ELI_DEBUG(3, "Tasting %s.", pp->name);
878 
879 	error = g_eli_read_metadata(mp, pp, &md);
880 	if (error != 0)
881 		return (NULL);
882 	gp = NULL;
883 
884 	if (strcmp(md.md_magic, G_ELI_MAGIC) != 0)
885 		return (NULL);
886 	if (md.md_version > G_ELI_VERSION) {
887 		printf("geom_eli.ko module is too old to handle %s.\n",
888 		    pp->name);
889 		return (NULL);
890 	}
891 	if (md.md_provsize != pp->mediasize)
892 		return (NULL);
893 	/* Should we attach it on boot? */
894 	if (!(md.md_flags & G_ELI_FLAG_BOOT))
895 		return (NULL);
896 	if (md.md_keys == 0x00) {
897 		G_ELI_DEBUG(0, "No valid keys on %s.", pp->name);
898 		return (NULL);
899 	}
900 	if (md.md_iterations == -1) {
901 		/* If there is no passphrase, we try only once. */
902 		tries = 1;
903 	} else {
904 		/* Ask for the passphrase no more than g_eli_tries times. */
905 		tries = g_eli_tries;
906 	}
907 
908 	for (i = 0; i < tries; i++) {
909 		g_eli_crypto_hmac_init(&ctx, NULL, 0);
910 
911 		/*
912 		 * Load all key files.
913 		 */
914 		nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name);
915 
916 		if (nkeyfiles == 0 && md.md_iterations == -1) {
917 			/*
918 			 * No key files and no passphrase, something is
919 			 * definitely wrong here.
920 			 * geli(8) doesn't allow for such situation, so assume
921 			 * that there was really no passphrase and in that case
922 			 * key files are no properly defined in loader.conf.
923 			 */
924 			G_ELI_DEBUG(0,
925 			    "Found no key files in loader.conf for %s.",
926 			    pp->name);
927 			return (NULL);
928 		}
929 
930 		/* Ask for the passphrase if defined. */
931 		if (md.md_iterations >= 0) {
932 			printf("Enter passphrase for %s: ", pp->name);
933 			gets(passphrase, sizeof(passphrase),
934 			    g_eli_visible_passphrase);
935 		}
936 
937 		/*
938 		 * Prepare Derived-Key from the user passphrase.
939 		 */
940 		if (md.md_iterations == 0) {
941 			g_eli_crypto_hmac_update(&ctx, md.md_salt,
942 			    sizeof(md.md_salt));
943 			g_eli_crypto_hmac_update(&ctx, passphrase,
944 			    strlen(passphrase));
945 		} else if (md.md_iterations > 0) {
946 			u_char dkey[G_ELI_USERKEYLEN];
947 
948 			pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt,
949 			    sizeof(md.md_salt), passphrase, md.md_iterations);
950 			g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey));
951 			bzero(dkey, sizeof(dkey));
952 		}
953 
954 		g_eli_crypto_hmac_final(&ctx, key, 0);
955 
956 		/*
957 		 * Decrypt Master-Key.
958 		 */
959 		error = g_eli_mkey_decrypt(&md, key, mkey, &nkey);
960 		bzero(key, sizeof(key));
961 		if (error == -1) {
962 			if (i == tries - 1) {
963 				G_ELI_DEBUG(0,
964 				    "Wrong key for %s. No tries left.",
965 				    pp->name);
966 				g_eli_keyfiles_clear(pp->name);
967 				return (NULL);
968 			}
969 			G_ELI_DEBUG(0, "Wrong key for %s. Tries left: %u.",
970 			    pp->name, tries - i - 1);
971 			/* Try again. */
972 			continue;
973 		} else if (error > 0) {
974 			G_ELI_DEBUG(0, "Cannot decrypt Master Key for %s (error=%d).",
975 			    pp->name, error);
976 			g_eli_keyfiles_clear(pp->name);
977 			return (NULL);
978 		}
979 		G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name);
980 		break;
981 	}
982 
983 	/*
984 	 * We have correct key, let's attach provider.
985 	 */
986 	gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey);
987 	bzero(mkey, sizeof(mkey));
988 	bzero(&md, sizeof(md));
989 	if (gp == NULL) {
990 		G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name,
991 		    G_ELI_SUFFIX);
992 		return (NULL);
993 	}
994 	return (gp);
995 }
996 
997 static void
998 g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
999     struct g_consumer *cp, struct g_provider *pp)
1000 {
1001 	struct g_eli_softc *sc;
1002 
1003 	g_topology_assert();
1004 	sc = gp->softc;
1005 	if (sc == NULL)
1006 		return;
1007 	if (pp != NULL || cp != NULL)
1008 		return;	/* Nothing here. */
1009 	sbuf_printf(sb, "%s<Flags>", indent);
1010 	if (sc->sc_flags == 0)
1011 		sbuf_printf(sb, "NONE");
1012 	else {
1013 		int first = 1;
1014 
1015 #define ADD_FLAG(flag, name)	do {					\
1016 	if (sc->sc_flags & (flag)) {					\
1017 		if (!first)						\
1018 			sbuf_printf(sb, ", ");				\
1019 		else							\
1020 			first = 0;					\
1021 		sbuf_printf(sb, name);					\
1022 	}								\
1023 } while (0)
1024 		ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER");
1025 		ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME");
1026 		ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT");
1027 		ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH");
1028 		ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH");
1029 		ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH");
1030 		ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN");
1031 		ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY");
1032 		ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY");
1033 #undef  ADD_FLAG
1034 	}
1035 	sbuf_printf(sb, "</Flags>\n");
1036 
1037 	if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) {
1038 		sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent,
1039 		    sc->sc_nkey);
1040 	}
1041 	sbuf_printf(sb, "%s<Crypto>", indent);
1042 	switch (sc->sc_crypto) {
1043 	case G_ELI_CRYPTO_HW:
1044 		sbuf_printf(sb, "hardware");
1045 		break;
1046 	case G_ELI_CRYPTO_SW:
1047 		sbuf_printf(sb, "software");
1048 		break;
1049 	default:
1050 		sbuf_printf(sb, "UNKNOWN");
1051 		break;
1052 	}
1053 	sbuf_printf(sb, "</Crypto>\n");
1054 	if (sc->sc_flags & G_ELI_FLAG_AUTH) {
1055 		sbuf_printf(sb,
1056 		    "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n",
1057 		    indent, g_eli_algo2str(sc->sc_aalgo));
1058 	}
1059 	sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent,
1060 	    sc->sc_ekeylen);
1061 	sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", indent,
1062 	    g_eli_algo2str(sc->sc_ealgo));
1063 }
1064 
1065 DECLARE_GEOM_CLASS(g_eli_class, g_eli);
1066 MODULE_DEPEND(g_eli, crypto, 1, 1, 1);
1067