2 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
36 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <sys/eventhandler.h>
41 #include <sys/kthread.h>
43 #include <sys/sched.h>
46 #include <sys/vnode.h>
50 #include <geom/geom.h>
51 #include <geom/eli/g_eli.h>
52 #include <geom/eli/pkcs5v2.h>
55 MALLOC_DEFINE(M_ELI, "eli data", "GEOM_ELI Data");
57 SYSCTL_DECL(_kern_geom);
58 SYSCTL_NODE(_kern_geom, OID_AUTO, eli, CTLFLAG_RW, 0, "GEOM_ELI stuff");
59 u_int g_eli_debug = 0;
60 TUNABLE_INT("kern.geom.eli.debug", &g_eli_debug);
61 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, debug, CTLFLAG_RW, &g_eli_debug, 0,
63 static u_int g_eli_tries = 3;
64 TUNABLE_INT("kern.geom.eli.tries", &g_eli_tries);
65 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, tries, CTLFLAG_RW, &g_eli_tries, 0,
66 "Number of tries for entering the passphrase");
67 static u_int g_eli_visible_passphrase = 0;
68 TUNABLE_INT("kern.geom.eli.visible_passphrase", &g_eli_visible_passphrase);
69 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, visible_passphrase, CTLFLAG_RW,
70 &g_eli_visible_passphrase, 0,
71 "Turn on echo when entering the passphrase (for debug purposes only!!)");
72 u_int g_eli_overwrites = 5;
73 TUNABLE_INT("kern.geom.eli.overwrites", &g_eli_overwrites);
74 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RW, &g_eli_overwrites,
75 0, "Number of times on-disk keys should be overwritten when destroying them");
76 static u_int g_eli_threads = 0;
77 TUNABLE_INT("kern.geom.eli.threads", &g_eli_threads);
78 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RW, &g_eli_threads, 0,
79 "Number of threads doing crypto work");
80 u_int g_eli_batch = 0;
81 TUNABLE_INT("kern.geom.eli.batch", &g_eli_batch);
82 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RW, &g_eli_batch, 0,
83 "Use crypto operations batching");
85 static eventhandler_tag g_eli_pre_sync = NULL;
87 static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp,
89 static void g_eli_init(struct g_class *mp);
90 static void g_eli_fini(struct g_class *mp);
92 static g_taste_t g_eli_taste;
93 static g_dumpconf_t g_eli_dumpconf;
95 struct g_class g_eli_class = {
96 .name = G_ELI_CLASS_NAME,
98 .ctlreq = g_eli_config,
100 .destroy_geom = g_eli_destroy_geom,
109 * g_eli_start -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
111 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
116 * EAGAIN from crypto(9) means, that we were probably balanced to another crypto
117 * accelerator or something like this.
118 * The function updates the SID and rerun the operation.
121 g_eli_crypto_rerun(struct cryptop *crp)
123 struct g_eli_softc *sc;
124 struct g_eli_worker *wr;
128 bp = (struct bio *)crp->crp_opaque;
129 sc = bp->bio_to->geom->softc;
130 LIST_FOREACH(wr, &sc->sc_workers, w_next) {
131 if (wr->w_number == bp->bio_pflags)
134 KASSERT(wr != NULL, ("Invalid worker (%u).", bp->bio_pflags));
135 G_ELI_DEBUG(1, "Rerunning crypto %s request (sid: %ju -> %ju).",
136 bp->bio_cmd == BIO_READ ? "READ" : "WRITE", (uintmax_t)wr->w_sid,
137 (uintmax_t)crp->crp_sid);
138 wr->w_sid = crp->crp_sid;
140 error = crypto_dispatch(crp);
143 G_ELI_DEBUG(1, "%s: crypto_dispatch() returned %d.", __func__, error);
144 crp->crp_etype = error;
149 * The function is called afer reading encrypted data from the provider.
151 * g_eli_start -> g_io_request -> G_ELI_READ_DONE -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
154 g_eli_read_done(struct bio *bp)
156 struct g_eli_softc *sc;
159 G_ELI_LOGREQ(2, bp, "Request done.");
160 pbp = bp->bio_parent;
161 if (pbp->bio_error == 0)
162 pbp->bio_error = bp->bio_error;
164 * Do we have all sectors already?
167 if (pbp->bio_inbed < pbp->bio_children)
170 if (pbp->bio_error != 0) {
171 G_ELI_LOGREQ(0, pbp, "%s() failed", __func__);
172 pbp->bio_completed = 0;
173 if (pbp->bio_driver2 != NULL) {
174 free(pbp->bio_driver2, M_ELI);
175 pbp->bio_driver2 = NULL;
177 g_io_deliver(pbp, pbp->bio_error);
180 sc = pbp->bio_to->geom->softc;
181 mtx_lock(&sc->sc_queue_mtx);
182 bioq_insert_tail(&sc->sc_queue, pbp);
183 mtx_unlock(&sc->sc_queue_mtx);
188 * The function is called after we encrypt and write data.
190 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver
193 g_eli_write_done(struct bio *bp)
197 G_ELI_LOGREQ(2, bp, "Request done.");
198 pbp = bp->bio_parent;
199 if (pbp->bio_error == 0) {
200 if (bp->bio_error != 0)
201 pbp->bio_error = bp->bio_error;
204 * Do we have all sectors already?
207 if (pbp->bio_inbed < pbp->bio_children)
209 free(pbp->bio_driver2, M_ELI);
210 pbp->bio_driver2 = NULL;
211 if (pbp->bio_error != 0) {
212 G_ELI_LOGREQ(0, pbp, "Crypto WRITE request failed (error=%d).",
214 pbp->bio_completed = 0;
218 * Write is finished, send it up.
220 pbp->bio_completed = pbp->bio_length;
221 g_io_deliver(pbp, pbp->bio_error);
225 * This function should never be called, but GEOM made as it set ->orphan()
226 * method for every geom.
229 g_eli_orphan_spoil_assert(struct g_consumer *cp)
232 panic("Function %s() called for %s.", __func__, cp->geom->name);
236 g_eli_orphan(struct g_consumer *cp)
238 struct g_eli_softc *sc;
241 sc = cp->geom->softc;
244 g_eli_destroy(sc, 1);
248 * BIO_READ : G_ELI_START -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver
249 * BIO_WRITE: G_ELI_START -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
252 g_eli_start(struct bio *bp)
254 struct g_eli_softc *sc;
255 struct g_consumer *cp;
258 sc = bp->bio_to->geom->softc;
260 ("Provider's error should be set (error=%d)(device=%s).",
261 bp->bio_to->error, bp->bio_to->name));
262 G_ELI_LOGREQ(2, bp, "Request received.");
264 switch (bp->bio_cmd) {
272 * We could eventually support BIO_DELETE request.
273 * It could be done by overwritting requested sector with
274 * random data g_eli_overwrites number of times.
277 g_io_deliver(bp, EOPNOTSUPP);
280 cbp = g_clone_bio(bp);
282 g_io_deliver(bp, ENOMEM);
285 switch (bp->bio_cmd) {
287 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) {
288 bp->bio_driver2 = NULL;
289 cbp->bio_done = g_eli_read_done;
290 cp = LIST_FIRST(&sc->sc_geom->consumer);
291 cbp->bio_to = cp->provider;
292 G_ELI_LOGREQ(2, cbp, "Sending request.");
294 * Read encrypted data from provider.
296 g_io_request(cbp, cp);
299 bp->bio_pflags = 255;
302 bp->bio_driver1 = cbp;
303 mtx_lock(&sc->sc_queue_mtx);
304 bioq_insert_tail(&sc->sc_queue, bp);
305 mtx_unlock(&sc->sc_queue_mtx);
310 cbp->bio_done = g_std_done;
311 cp = LIST_FIRST(&sc->sc_geom->consumer);
312 cbp->bio_to = cp->provider;
313 G_ELI_LOGREQ(2, cbp, "Sending request.");
314 g_io_request(cbp, cp);
320 * This is the main function for kernel worker thread when we don't have
321 * hardware acceleration and we have to do cryptography in software.
322 * Dedicated thread is needed, so we don't slow down g_up/g_down GEOM
323 * threads with crypto work.
326 g_eli_worker(void *arg)
328 struct g_eli_softc *sc;
329 struct g_eli_worker *wr;
335 /* Before sched_bind() to a CPU, wait for all CPUs to go on-line. */
336 if (mp_ncpus > 1 && sc->sc_crypto == G_ELI_CRYPTO_SW &&
337 g_eli_threads == 0) {
339 tsleep(wr, 0, "geli:smp", hz / 4);
342 thread_lock(curthread);
343 sched_prio(curthread, PUSER);
344 if (sc->sc_crypto == G_ELI_CRYPTO_SW && g_eli_threads == 0)
345 sched_bind(curthread, wr->w_number);
346 thread_unlock(curthread);
348 G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm);
351 mtx_lock(&sc->sc_queue_mtx);
352 bp = bioq_takefirst(&sc->sc_queue);
354 if (sc->sc_flags & G_ELI_FLAG_DESTROY) {
355 LIST_REMOVE(wr, w_next);
356 crypto_freesession(wr->w_sid);
358 G_ELI_DEBUG(1, "Thread %s exiting.",
359 curthread->td_proc->p_comm);
360 wakeup(&sc->sc_workers);
361 mtx_unlock(&sc->sc_queue_mtx);
364 msleep(sc, &sc->sc_queue_mtx, PDROP, "geli:w", 0);
367 mtx_unlock(&sc->sc_queue_mtx);
368 if (bp->bio_cmd == BIO_READ && bp->bio_pflags == 255)
369 g_eli_auth_read(sc, bp);
370 else if (sc->sc_flags & G_ELI_FLAG_AUTH)
371 g_eli_auth_run(wr, bp);
373 g_eli_crypto_run(wr, bp);
378 * Here we generate IV. It is unique for every sector.
381 g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
384 u_char off[8], hash[SHA256_DIGEST_LENGTH];
387 if (!(sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER))
388 le64enc(off, (uint64_t)offset);
389 /* Copy precalculated SHA256 context for IV-Key. */
390 bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx));
391 SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset));
392 SHA256_Final(hash, &ctx);
393 bcopy(hash, iv, size);
397 g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
398 struct g_eli_metadata *md)
401 struct g_consumer *cp;
407 gp = g_new_geomf(mp, "eli:taste");
408 gp->start = g_eli_start;
409 gp->access = g_std_access;
411 * g_eli_read_metadata() is always called from the event thread.
412 * Our geom is created and destroyed in the same event, so there
413 * could be no orphan nor spoil event in the meantime.
415 gp->orphan = g_eli_orphan_spoil_assert;
416 gp->spoiled = g_eli_orphan_spoil_assert;
417 cp = g_new_consumer(gp);
418 error = g_attach(cp, pp);
421 error = g_access(cp, 1, 0, 0);
425 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
430 eli_metadata_decode(buf, md);
434 if (cp->provider != NULL) {
436 g_access(cp, -1, 0, 0);
439 g_destroy_consumer(cp);
445 * The function is called when we had last close on provider and user requested
446 * to close it when this situation occur.
449 g_eli_last_close(struct g_eli_softc *sc)
452 struct g_provider *pp;
458 pp = LIST_FIRST(&gp->provider);
459 strlcpy(ppname, pp->name, sizeof(ppname));
460 error = g_eli_destroy(sc, 1);
461 KASSERT(error == 0, ("Cannot detach %s on last close (error=%d).",
463 G_ELI_DEBUG(0, "Detached %s on last close.", ppname);
467 g_eli_access(struct g_provider *pp, int dr, int dw, int de)
469 struct g_eli_softc *sc;
476 if (sc->sc_flags & G_ELI_FLAG_RO) {
477 /* Deny write attempts. */
480 /* Someone is opening us for write, we need to remember that. */
481 sc->sc_flags |= G_ELI_FLAG_WOPEN;
484 /* Is this the last close? */
485 if (pp->acr + dr > 0 || pp->acw + dw > 0 || pp->ace + de > 0)
489 * Automatically detach on last close if requested.
491 if ((sc->sc_flags & G_ELI_FLAG_RW_DETACH) ||
492 (sc->sc_flags & G_ELI_FLAG_WOPEN)) {
493 g_eli_last_close(sc);
499 g_eli_cpu_is_disabled(int cpu)
502 return ((hlt_cpus_mask & (1 << cpu)) != 0);
509 g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp,
510 const struct g_eli_metadata *md, const u_char *mkey, int nkey)
512 struct g_eli_softc *sc;
513 struct g_eli_worker *wr;
515 struct g_provider *pp;
516 struct g_consumer *cp;
517 struct cryptoini crie, cria;
521 G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX);
523 gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX);
524 gp->softc = NULL; /* for a moment */
526 sc = malloc(sizeof(*sc), M_ELI, M_WAITOK | M_ZERO);
527 gp->start = g_eli_start;
529 * Spoiling cannot happen actually, because we keep provider open for
530 * writing all the time or provider is read-only.
532 gp->spoiled = g_eli_orphan_spoil_assert;
533 gp->orphan = g_eli_orphan;
534 gp->dumpconf = g_eli_dumpconf;
536 * If detach-on-last-close feature is not enabled and we don't operate
537 * on read-only provider, we can simply use g_std_access().
539 if (md->md_flags & (G_ELI_FLAG_WO_DETACH | G_ELI_FLAG_RO))
540 gp->access = g_eli_access;
542 gp->access = g_std_access;
544 sc->sc_crypto = G_ELI_CRYPTO_SW;
545 sc->sc_flags = md->md_flags;
546 /* Backward compatibility. */
547 if (md->md_version < 2)
548 sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER;
549 sc->sc_ealgo = md->md_ealgo;
552 * Remember the keys in our softc structure.
554 g_eli_mkey_propagate(sc, mkey);
555 sc->sc_ekeylen = md->md_keylen;
557 if (sc->sc_flags & G_ELI_FLAG_AUTH) {
558 sc->sc_akeylen = sizeof(sc->sc_akey) * 8;
559 sc->sc_aalgo = md->md_aalgo;
560 sc->sc_alen = g_eli_hashlen(sc->sc_aalgo);
562 sc->sc_data_per_sector = bpp->sectorsize - sc->sc_alen;
564 * Some hash functions (like SHA1 and RIPEMD160) generates hash
565 * which length is not multiple of 128 bits, but we want data
566 * length to be multiple of 128, so we can encrypt without
567 * padding. The line below rounds down data length to multiple
570 sc->sc_data_per_sector -= sc->sc_data_per_sector % 16;
572 sc->sc_bytes_per_sector =
573 (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1;
574 sc->sc_bytes_per_sector *= bpp->sectorsize;
576 * Precalculate SHA256 for HMAC key generation.
577 * This is expensive operation and we can do it only once now or
578 * for every access to sector, so now will be much better.
580 SHA256_Init(&sc->sc_akeyctx);
581 SHA256_Update(&sc->sc_akeyctx, sc->sc_akey,
582 sizeof(sc->sc_akey));
586 * Precalculate SHA256 for IV generation.
587 * This is expensive operation and we can do it only once now or for
588 * every access to sector, so now will be much better.
590 SHA256_Init(&sc->sc_ivctx);
591 SHA256_Update(&sc->sc_ivctx, sc->sc_ivkey, sizeof(sc->sc_ivkey));
596 bioq_init(&sc->sc_queue);
597 mtx_init(&sc->sc_queue_mtx, "geli:queue", NULL, MTX_DEF);
600 cp = g_new_consumer(gp);
601 error = g_attach(cp, bpp);
604 gctl_error(req, "Cannot attach to %s (error=%d).",
607 G_ELI_DEBUG(1, "Cannot attach to %s (error=%d).",
613 * Keep provider open all the time, so we can run critical tasks,
614 * like Master Keys deletion, without wondering if we can open
616 * We don't open provider for writing only when user requested read-only
619 if (sc->sc_flags & G_ELI_FLAG_RO)
620 error = g_access(cp, 1, 0, 1);
622 error = g_access(cp, 1, 1, 1);
625 gctl_error(req, "Cannot access %s (error=%d).",
628 G_ELI_DEBUG(1, "Cannot access %s (error=%d).",
634 LIST_INIT(&sc->sc_workers);
636 bzero(&crie, sizeof(crie));
637 crie.cri_alg = sc->sc_ealgo;
638 crie.cri_klen = sc->sc_ekeylen;
639 crie.cri_key = sc->sc_ekey;
640 if (sc->sc_flags & G_ELI_FLAG_AUTH) {
641 bzero(&cria, sizeof(cria));
642 cria.cri_alg = sc->sc_aalgo;
643 cria.cri_klen = sc->sc_akeylen;
644 cria.cri_key = sc->sc_akey;
645 crie.cri_next = &cria;
648 threads = g_eli_threads;
651 else if (threads > mp_ncpus) {
652 /* There is really no need for too many worker threads. */
654 G_ELI_DEBUG(0, "Reducing number of threads to %u.", threads);
656 for (i = 0; i < threads; i++) {
657 if (g_eli_cpu_is_disabled(i)) {
658 G_ELI_DEBUG(1, "%s: CPU %u disabled, skipping.",
662 wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO);
667 * If this is the first pass, try to get hardware support.
668 * Use software cryptography, if we cannot get it.
670 if (LIST_EMPTY(&sc->sc_workers)) {
671 error = crypto_newsession(&wr->w_sid, &crie,
672 CRYPTOCAP_F_HARDWARE);
674 sc->sc_crypto = G_ELI_CRYPTO_HW;
676 if (sc->sc_crypto == G_ELI_CRYPTO_SW) {
677 error = crypto_newsession(&wr->w_sid, &crie,
678 CRYPTOCAP_F_SOFTWARE);
683 gctl_error(req, "Cannot set up crypto session "
684 "for %s (error=%d).", bpp->name, error);
686 G_ELI_DEBUG(1, "Cannot set up crypto session "
687 "for %s (error=%d).", bpp->name, error);
692 error = kproc_create(g_eli_worker, wr, &wr->w_proc, 0, 0,
693 "g_eli[%u] %s", i, bpp->name);
695 crypto_freesession(wr->w_sid);
698 gctl_error(req, "Cannot create kernel thread "
699 "for %s (error=%d).", bpp->name, error);
701 G_ELI_DEBUG(1, "Cannot create kernel thread "
702 "for %s (error=%d).", bpp->name, error);
706 LIST_INSERT_HEAD(&sc->sc_workers, wr, w_next);
707 /* If we have hardware support, one thread is enough. */
708 if (sc->sc_crypto == G_ELI_CRYPTO_HW)
713 * Create decrypted provider.
715 pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX);
716 pp->sectorsize = md->md_sectorsize;
717 pp->mediasize = bpp->mediasize;
718 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME))
719 pp->mediasize -= bpp->sectorsize;
720 if (!(sc->sc_flags & G_ELI_FLAG_AUTH))
721 pp->mediasize -= (pp->mediasize % pp->sectorsize);
723 pp->mediasize /= sc->sc_bytes_per_sector;
724 pp->mediasize *= pp->sectorsize;
727 g_error_provider(pp, 0);
729 G_ELI_DEBUG(0, "Device %s created.", pp->name);
730 G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo),
732 if (sc->sc_flags & G_ELI_FLAG_AUTH)
733 G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo));
734 G_ELI_DEBUG(0, " Crypto: %s",
735 sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware");
738 mtx_lock(&sc->sc_queue_mtx);
739 sc->sc_flags |= G_ELI_FLAG_DESTROY;
742 * Wait for kernel threads self destruction.
744 while (!LIST_EMPTY(&sc->sc_workers)) {
745 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
748 mtx_destroy(&sc->sc_queue_mtx);
749 if (cp->provider != NULL) {
751 g_access(cp, -1, -1, -1);
754 g_destroy_consumer(cp);
756 bzero(sc, sizeof(*sc));
762 g_eli_destroy(struct g_eli_softc *sc, boolean_t force)
765 struct g_provider *pp;
773 pp = LIST_FIRST(&gp->provider);
774 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
776 G_ELI_DEBUG(1, "Device %s is still open, so it "
777 "cannot be definitely removed.", pp->name);
780 "Device %s is still open (r%dw%de%d).", pp->name,
781 pp->acr, pp->acw, pp->ace);
786 mtx_lock(&sc->sc_queue_mtx);
787 sc->sc_flags |= G_ELI_FLAG_DESTROY;
789 while (!LIST_EMPTY(&sc->sc_workers)) {
790 msleep(&sc->sc_workers, &sc->sc_queue_mtx, PRIBIO,
793 mtx_destroy(&sc->sc_queue_mtx);
795 bzero(sc, sizeof(*sc));
798 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
799 G_ELI_DEBUG(0, "Device %s destroyed.", gp->name);
800 g_wither_geom_close(gp, ENXIO);
806 g_eli_destroy_geom(struct gctl_req *req __unused,
807 struct g_class *mp __unused, struct g_geom *gp)
809 struct g_eli_softc *sc;
812 return (g_eli_destroy(sc, 0));
816 g_eli_keyfiles_load(struct hmac_ctx *ctx, const char *provider)
818 u_char *keyfile, *data, *size;
819 char *file, name[64];
823 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
824 keyfile = preload_search_by_type(name);
826 return (i); /* Return number of loaded keyfiles. */
827 data = preload_search_info(keyfile, MODINFO_ADDR);
829 G_ELI_DEBUG(0, "Cannot find key file data for %s.",
833 data = *(void **)data;
834 size = preload_search_info(keyfile, MODINFO_SIZE);
836 G_ELI_DEBUG(0, "Cannot find key file size for %s.",
840 file = preload_search_info(keyfile, MODINFO_NAME);
842 G_ELI_DEBUG(0, "Cannot find key file name for %s.",
846 G_ELI_DEBUG(1, "Loaded keyfile %s for %s (type: %s).", file,
848 g_eli_crypto_hmac_update(ctx, data, *(size_t *)size);
853 g_eli_keyfiles_clear(const char *provider)
855 u_char *keyfile, *data, *size;
860 snprintf(name, sizeof(name), "%s:geli_keyfile%d", provider, i);
861 keyfile = preload_search_by_type(name);
864 data = preload_search_info(keyfile, MODINFO_ADDR);
865 size = preload_search_info(keyfile, MODINFO_SIZE);
866 if (data == NULL || size == NULL)
868 data = *(void **)data;
869 bzero(data, *(size_t *)size);
874 * Tasting is only made on boot.
875 * We detect providers which should be attached before root is mounted.
877 static struct g_geom *
878 g_eli_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
880 struct g_eli_metadata md;
883 char passphrase[256];
884 u_char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
885 u_int i, nkey, nkeyfiles, tries;
888 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
891 if (root_mounted() || g_eli_tries == 0)
894 G_ELI_DEBUG(3, "Tasting %s.", pp->name);
896 error = g_eli_read_metadata(mp, pp, &md);
901 if (strcmp(md.md_magic, G_ELI_MAGIC) != 0)
903 if (md.md_version > G_ELI_VERSION) {
904 printf("geom_eli.ko module is too old to handle %s.\n",
908 if (md.md_provsize != pp->mediasize)
910 /* Should we attach it on boot? */
911 if (!(md.md_flags & G_ELI_FLAG_BOOT))
913 if (md.md_keys == 0x00) {
914 G_ELI_DEBUG(0, "No valid keys on %s.", pp->name);
917 if (md.md_iterations == -1) {
918 /* If there is no passphrase, we try only once. */
921 /* Ask for the passphrase no more than g_eli_tries times. */
925 for (i = 0; i < tries; i++) {
926 g_eli_crypto_hmac_init(&ctx, NULL, 0);
929 * Load all key files.
931 nkeyfiles = g_eli_keyfiles_load(&ctx, pp->name);
933 if (nkeyfiles == 0 && md.md_iterations == -1) {
935 * No key files and no passphrase, something is
936 * definitely wrong here.
937 * geli(8) doesn't allow for such situation, so assume
938 * that there was really no passphrase and in that case
939 * key files are no properly defined in loader.conf.
942 "Found no key files in loader.conf for %s.",
947 /* Ask for the passphrase if defined. */
948 if (md.md_iterations >= 0) {
949 printf("Enter passphrase for %s: ", pp->name);
950 gets(passphrase, sizeof(passphrase),
951 g_eli_visible_passphrase);
955 * Prepare Derived-Key from the user passphrase.
957 if (md.md_iterations == 0) {
958 g_eli_crypto_hmac_update(&ctx, md.md_salt,
960 g_eli_crypto_hmac_update(&ctx, passphrase,
962 bzero(passphrase, sizeof(passphrase));
963 } else if (md.md_iterations > 0) {
964 u_char dkey[G_ELI_USERKEYLEN];
966 pkcs5v2_genkey(dkey, sizeof(dkey), md.md_salt,
967 sizeof(md.md_salt), passphrase, md.md_iterations);
968 bzero(passphrase, sizeof(passphrase));
969 g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey));
970 bzero(dkey, sizeof(dkey));
973 g_eli_crypto_hmac_final(&ctx, key, 0);
976 * Decrypt Master-Key.
978 error = g_eli_mkey_decrypt(&md, key, mkey, &nkey);
979 bzero(key, sizeof(key));
981 if (i == tries - 1) {
983 "Wrong key for %s. No tries left.",
985 g_eli_keyfiles_clear(pp->name);
988 G_ELI_DEBUG(0, "Wrong key for %s. Tries left: %u.",
989 pp->name, tries - i - 1);
992 } else if (error > 0) {
993 G_ELI_DEBUG(0, "Cannot decrypt Master Key for %s (error=%d).",
995 g_eli_keyfiles_clear(pp->name);
998 G_ELI_DEBUG(1, "Using Master Key %u for %s.", nkey, pp->name);
1003 * We have correct key, let's attach provider.
1005 gp = g_eli_create(NULL, mp, pp, &md, mkey, nkey);
1006 bzero(mkey, sizeof(mkey));
1007 bzero(&md, sizeof(md));
1009 G_ELI_DEBUG(0, "Cannot create device %s%s.", pp->name,
1017 g_eli_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1018 struct g_consumer *cp, struct g_provider *pp)
1020 struct g_eli_softc *sc;
1022 g_topology_assert();
1026 if (pp != NULL || cp != NULL)
1027 return; /* Nothing here. */
1028 sbuf_printf(sb, "%s<Flags>", indent);
1029 if (sc->sc_flags == 0)
1030 sbuf_printf(sb, "NONE");
1034 #define ADD_FLAG(flag, name) do { \
1035 if (sc->sc_flags & (flag)) { \
1037 sbuf_printf(sb, ", "); \
1040 sbuf_printf(sb, name); \
1043 ADD_FLAG(G_ELI_FLAG_NATIVE_BYTE_ORDER, "NATIVE-BYTE-ORDER");
1044 ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME");
1045 ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT");
1046 ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH");
1047 ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH");
1048 ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH");
1049 ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN");
1050 ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY");
1051 ADD_FLAG(G_ELI_FLAG_RO, "READ-ONLY");
1054 sbuf_printf(sb, "</Flags>\n");
1056 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) {
1057 sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent,
1060 sbuf_printf(sb, "%s<Crypto>", indent);
1061 switch (sc->sc_crypto) {
1062 case G_ELI_CRYPTO_HW:
1063 sbuf_printf(sb, "hardware");
1065 case G_ELI_CRYPTO_SW:
1066 sbuf_printf(sb, "software");
1069 sbuf_printf(sb, "UNKNOWN");
1072 sbuf_printf(sb, "</Crypto>\n");
1073 if (sc->sc_flags & G_ELI_FLAG_AUTH) {
1075 "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n",
1076 indent, g_eli_algo2str(sc->sc_aalgo));
1078 sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent,
1080 sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", indent,
1081 g_eli_algo2str(sc->sc_ealgo));
1085 g_eli_shutdown_pre_sync(void *arg, int howto)
1088 struct g_geom *gp, *gp2;
1089 struct g_provider *pp;
1090 struct g_eli_softc *sc;
1096 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
1100 pp = LIST_FIRST(&gp->provider);
1101 KASSERT(pp != NULL, ("No provider? gp=%p (%s)", gp, gp->name));
1102 if (pp->acr + pp->acw + pp->ace == 0)
1103 error = g_eli_destroy(sc, 1);
1105 sc->sc_flags |= G_ELI_FLAG_RW_DETACH;
1106 gp->access = g_eli_access;
1109 g_topology_unlock();
1114 g_eli_init(struct g_class *mp)
1117 g_eli_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
1118 g_eli_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
1119 if (g_eli_pre_sync == NULL)
1120 G_ELI_DEBUG(0, "Warning! Cannot register shutdown event.");
1124 g_eli_fini(struct g_class *mp)
1127 if (g_eli_pre_sync != NULL)
1128 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_eli_pre_sync);
1131 DECLARE_GEOM_CLASS(g_eli_class, g_eli);
1132 MODULE_DEPEND(g_eli, crypto, 1, 1, 1);