2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This file implements initial version of device-mapper crypt target.
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/endian.h>
44 #include <sys/globaldata.h>
45 #include <sys/kerneldump.h>
46 #include <sys/malloc.h>
47 #include <sys/mpipe.h>
49 #include <sys/mutex2.h>
50 #include <sys/vnode.h>
51 #include <crypto/sha1.h>
52 #include <crypto/sha2/sha2.h>
53 #include <opencrypto/cryptodev.h>
54 #include <opencrypto/rmd160.h>
55 #include <machine/cpufunc.h>
59 #include <dev/disk/dm/dm.h>
60 MALLOC_DEFINE(M_DMCRYPT, "dm_crypt", "Device Mapper Target Crypt");
62 KTR_INFO_MASTER(dmcrypt);
64 #if !defined(KTR_DMCRYPT)
65 #define KTR_DMCRYPT KTR_ALL
68 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_dispatch, 0,
69 "crypto_dispatch(%p)", struct cryptop *crp);
70 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypt_strategy, 0,
71 "crypt_strategy(b_cmd = %d, bp = %p)", int cmd, struct buf *bp);
72 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_write_start, 1,
73 "crypto_write_start(crp = %p, bp = %p, sector = %d/%d)",
74 struct cryptop *crp, struct buf *bp, int i, int sectors);
75 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_cb_write_done, 1,
76 "crypto_cb_write_done(crp = %p, bp = %p, n = %d)",
77 struct cryptop *crp, struct buf *bp, int n);
78 KTR_INFO(KTR_DMCRYPT, dmcrypt, bio_write_done, 1,
79 "bio_write_done(bp = %p)", struct buf *bp);
80 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_write_retry, 1,
81 "crypto_write_retry(crp = %p)", struct buf *bp);
82 KTR_INFO(KTR_DMCRYPT, dmcrypt, bio_read_done, 2,
83 "bio_read_done(bp = %p)", struct buf *bp);
84 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_read_start, 2,
85 "crypto_read_start(crp = %p, bp = %p, sector = %d/%d)",
86 struct cryptop *crp, struct buf *bp, int i, int sectors);
87 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_cb_read_done, 2,
88 "crypto_cb_read_done(crp = %p, bp = %p, n = %d)",
89 struct cryptop *crp, struct buf *bp, int n);
91 struct target_crypt_config;
93 typedef void dispatch_t(void *);
94 typedef void ivgen_t(struct target_crypt_config *, u_int8_t *, size_t, off_t,
97 typedef int ivgen_ctor_t(struct target_crypt_config *, char *, void **);
98 typedef int ivgen_dtor_t(struct target_crypt_config *, void *);
100 struct iv_generator {
107 struct essiv_ivgen_priv {
108 struct cryptoini crypto_session;
109 struct objcache *crp_crd_cache;
110 u_int64_t crypto_sid;
112 u_int8_t crypto_keyhash[SHA512_DIGEST_LENGTH];
115 typedef struct target_crypt_config {
121 u_int8_t crypto_key[512>>3];
123 u_int64_t crypto_sid;
124 u_int64_t block_offset;
126 SHA512_CTX essivsha512_ctx;
128 struct cryptoini crypto_session;
130 struct iv_generator *ivgen;
133 struct malloc_pipe read_mpipe;
134 struct malloc_pipe write_mpipe;
135 } dm_target_crypt_config_t;
138 dm_target_crypt_config_t *priv;
144 struct dmtc_dump_helper {
145 dm_target_crypt_config_t *priv;
153 struct cryptodesc crd[128];
154 struct cryptop crp[128];
158 #define DMTC_BUF_SIZE_WRITE \
159 MAXPHYS + sizeof(struct dmtc_helper) + \
160 MAXPHYS/DEV_BSIZE*(sizeof(struct cryptop) + sizeof(struct cryptodesc))
161 #define DMTC_BUF_SIZE_READ \
162 sizeof(struct dmtc_helper) + \
163 MAXPHYS/DEV_BSIZE*(sizeof(struct cryptop) + sizeof(struct cryptodesc))
165 static void dmtc_crypto_dispatch(void *arg);
166 static void dmtc_crypto_dump_start(dm_target_crypt_config_t *priv,
167 struct dmtc_dump_helper *dump_helper);
168 static void dmtc_crypto_read_start(dm_target_crypt_config_t *priv,
170 static void dmtc_crypto_write_start(dm_target_crypt_config_t *priv,
172 static void dmtc_bio_read_done(struct bio *bio);
173 static void dmtc_bio_write_done(struct bio *bio);
174 static int dmtc_crypto_cb_dump_done(struct cryptop *crp);
175 static int dmtc_crypto_cb_read_done(struct cryptop *crp);
176 static int dmtc_crypto_cb_write_done(struct cryptop *crp);
178 static ivgen_ctor_t essiv_ivgen_ctor;
179 static ivgen_dtor_t essiv_ivgen_dtor;
180 static ivgen_t essiv_ivgen;
181 static ivgen_t plain_ivgen;
182 static ivgen_t plain64_ivgen;
184 static struct iv_generator ivgens[] = {
185 { .name = "essiv", .ctor = essiv_ivgen_ctor, .dtor = essiv_ivgen_dtor,
186 .gen_iv = essiv_ivgen },
187 { .name = "plain", .ctor = NULL, .dtor = NULL, .gen_iv = plain_ivgen },
188 { .name = "plain64", .ctor = NULL, .dtor = NULL, .gen_iv = plain64_ivgen },
189 { NULL, NULL, NULL, NULL }
192 struct objcache_malloc_args essiv_ivgen_malloc_args = {
193 2*sizeof(void *) + (sizeof(struct cryptodesc) +
194 sizeof(struct cryptop)), M_DMCRYPT };
197 dmtc_init_mpipe(struct target_crypt_config *priv)
201 nmax = (physmem*2/1000*PAGE_SIZE)/(DMTC_BUF_SIZE_WRITE + DMTC_BUF_SIZE_READ) + 1;
206 kprintf("dm_target_crypt: Setting min/max mpipe buffers: %d/%d\n", 2, nmax);
208 mpipe_init(&priv->write_mpipe, M_DMCRYPT, DMTC_BUF_SIZE_WRITE,
209 2, nmax, MPF_NOZERO | MPF_CALLBACK, NULL, NULL, NULL);
210 mpipe_init(&priv->read_mpipe, M_DMCRYPT, DMTC_BUF_SIZE_READ,
211 2, nmax, MPF_NOZERO | MPF_CALLBACK, NULL, NULL, NULL);
215 dmtc_destroy_mpipe(struct target_crypt_config *priv)
217 mpipe_done(&priv->write_mpipe);
218 mpipe_done(&priv->read_mpipe);
222 * Overwrite private information (in buf) to avoid leaking it
225 dmtc_crypto_clear(void *buf, size_t len)
227 memset(buf, 0xFF, len);
232 * ESSIV IV Generator Routines
235 essiv_ivgen_ctor(struct target_crypt_config *priv, char *iv_hash, void **p_ivpriv)
237 struct essiv_ivgen_priv *ivpriv;
238 u_int8_t crypto_keyhash[SHA512_DIGEST_LENGTH];
239 unsigned int klen, hashlen;
242 klen = (priv->crypto_klen >> 3);
247 if (!strcmp(iv_hash, "sha1")) {
250 hashlen = SHA1_RESULTLEN;
252 SHA1Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
253 SHA1Final(crypto_keyhash, &ctx);
254 } else if (!strcmp(iv_hash, "sha256")) {
257 hashlen = SHA256_DIGEST_LENGTH;
259 SHA256_Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
260 SHA256_Final(crypto_keyhash, &ctx);
261 } else if (!strcmp(iv_hash, "sha384")) {
264 hashlen = SHA384_DIGEST_LENGTH;
266 SHA384_Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
267 SHA384_Final(crypto_keyhash, &ctx);
268 } else if (!strcmp(iv_hash, "sha512")) {
271 hashlen = SHA512_DIGEST_LENGTH;
273 SHA512_Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
274 SHA512_Final(crypto_keyhash, &ctx);
275 } else if (!strcmp(iv_hash, "md5")) {
278 hashlen = MD5_DIGEST_LENGTH;
280 MD5Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
281 MD5Final(crypto_keyhash, &ctx);
282 } else if (!strcmp(iv_hash, "rmd160") ||
283 !strcmp(iv_hash, "ripemd160")) {
288 RMD160Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
289 RMD160Final(crypto_keyhash, &ctx);
294 /* Convert hashlen to bits */
297 ivpriv = kmalloc(sizeof(struct essiv_ivgen_priv), M_DMCRYPT,
299 memcpy(ivpriv->crypto_keyhash, crypto_keyhash, sizeof(crypto_keyhash));
300 ivpriv->keyhash_len = sizeof(crypto_keyhash);
301 dmtc_crypto_clear(crypto_keyhash, sizeof(crypto_keyhash));
303 ivpriv->crypto_session.cri_alg = priv->crypto_alg;
304 ivpriv->crypto_session.cri_key = (u_int8_t *)ivpriv->crypto_keyhash;
305 ivpriv->crypto_session.cri_klen = hashlen;
306 ivpriv->crypto_session.cri_mlen = 0;
307 ivpriv->crypto_session.cri_next = NULL;
310 * XXX: in principle we also need to check if the block size of the
311 * cipher is a valid iv size for the block cipher.
314 error = crypto_newsession(&ivpriv->crypto_sid,
315 &ivpriv->crypto_session,
316 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
318 kprintf("dm_target_crypt: Error during crypto_newsession "
319 "for essiv_ivgen, error = %d\n",
321 dmtc_crypto_clear(ivpriv->crypto_keyhash, ivpriv->keyhash_len);
322 kfree(ivpriv, M_DMCRYPT);
326 ivpriv->crp_crd_cache = objcache_create(
327 "dmcrypt-essiv-cache", 0, 0,
329 objcache_malloc_alloc,
330 objcache_malloc_free,
331 &essiv_ivgen_malloc_args );
338 essiv_ivgen_dtor(struct target_crypt_config *priv, void *arg)
340 struct essiv_ivgen_priv *ivpriv;
342 ivpriv = (struct essiv_ivgen_priv *)arg;
343 KKASSERT(ivpriv != NULL);
345 crypto_freesession(ivpriv->crypto_sid);
347 objcache_destroy(ivpriv->crp_crd_cache);
349 dmtc_crypto_clear(ivpriv->crypto_keyhash, ivpriv->keyhash_len);
350 kfree(ivpriv, M_DMCRYPT);
356 essiv_ivgen_done(struct cryptop *crp)
358 struct essiv_ivgen_priv *ivpriv;
363 if (crp->crp_etype == EAGAIN)
364 return crypto_dispatch(crp);
366 if (crp->crp_etype != 0) {
367 kprintf("dm_target_crypt: essiv_ivgen_done, "
368 "crp->crp_etype = %d\n", crp->crp_etype);
371 free_addr = crp->crp_opaque;
373 * In-memory structure is:
374 * | ivpriv | opaque | crp | crd |
375 * | (void *) | (void *) | (cryptop) | (cryptodesc) |
377 ivpriv = *((struct essiv_ivgen_priv **)crp->crp_opaque);
378 crp->crp_opaque += sizeof(void *);
379 opaque = *((void **)crp->crp_opaque);
381 objcache_put(ivpriv->crp_crd_cache, free_addr);
382 dmtc_crypto_dispatch(opaque);
387 essiv_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
388 size_t iv_len, off_t sector, void *opaque)
390 struct essiv_ivgen_priv *ivpriv;
391 struct cryptodesc *crd;
393 caddr_t space, alloc_addr;
396 ivpriv = priv->ivgen_priv;
397 KKASSERT(ivpriv != NULL);
400 * In-memory structure is:
401 * | ivpriv | opaque | crp | crd |
402 * | (void *) | (void *) | (cryptop) | (cryptodesc) |
404 alloc_addr = space = objcache_get(ivpriv->crp_crd_cache, M_WAITOK);
405 *((struct essiv_ivgen_priv **)space) = ivpriv;
406 space += sizeof(void *);
407 *((void **)space) = opaque;
408 space += sizeof(void *);
409 crp = (struct cryptop *)space;
410 space += sizeof(struct cryptop);
411 crd = (struct cryptodesc *)space;
414 bzero(crd, sizeof(struct cryptodesc));
415 bzero(crp, sizeof(struct cryptop));
416 *((off_t *)iv) = htole64(sector + priv->iv_offset);
417 crp->crp_buf = (caddr_t)iv;
419 crp->crp_sid = ivpriv->crypto_sid;
420 crp->crp_ilen = crp->crp_olen = iv_len;
422 crp->crp_opaque = alloc_addr;
424 crp->crp_callback = essiv_ivgen_done;
428 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL | CRYPTO_F_BATCH;
430 crd->crd_alg = priv->crypto_alg;
432 crd->crd_key = (caddr_t)priv->crypto_keyhash;
433 crd->crd_klen = priv->crypto_klen;
436 bzero(crd->crd_iv, sizeof(crd->crd_iv));
439 crd->crd_len = iv_len;
440 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
441 crd->crd_flags |= CRD_F_ENCRYPT;
442 crd->crd_next = NULL;
444 error = crypto_dispatch(crp);
446 kprintf("dm_target_crypt: essiv_ivgen, error = %d\n", error);
451 plain_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
452 size_t iv_len, off_t sector, void *opaque)
455 *((uint32_t *)iv) = htole32((uint32_t)(sector + priv->iv_offset));
456 dmtc_crypto_dispatch(opaque);
460 plain64_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
461 size_t iv_len, off_t sector, void *opaque)
464 *((uint64_t *)iv) = htole64((uint64_t)(sector + priv->iv_offset));
465 dmtc_crypto_dispatch(opaque);
470 geli_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
471 size_t iv_len, off_t sector, void *opaque)
475 u_int8_t md[SHA512_DIGEST_LENGTH]; /* Max. Digest Size */
477 memcpy(&ctx512, &priv->essivsha512_ctx, sizeof(SHA512_CTX));
478 SHA512_Update(&ctx512, (u_int8_t*)§or, sizeof(off_t));
479 SHA512_Final(md, &ctx512);
481 memcpy(iv, md, iv_len);
482 dmtc_crypto_dispatch(opaque);
487 * Init function called from dm_table_load_ioctl.
488 * cryptsetup actually passes us this:
489 * aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8
492 hex2key(char *hex, size_t key_len, u_int8_t *key)
498 for (key_idx = 0; key_idx < key_len; ++key_idx) {
501 key[key_idx] = (u_int8_t)strtoul(hex_buf, NULL, 16);
510 dm_target_crypt_init(dm_dev_t * dmv, void **target_config, char *params)
512 dm_target_crypt_config_t *priv;
515 char *crypto_alg, *crypto_mode, *iv_mode, *iv_opt, *key, *dev;
517 int i, argc, klen, error;
518 uint64_t iv_offset, block_offset;
523 len = strlen(params) + 1;
526 status_str = kmalloc(len, M_DMCRYPT, M_WAITOK);
528 * Parse a string, containing tokens delimited by white space,
529 * into an argument vector
531 for (ap = args; ap < &args[5] &&
532 (*ap = strsep(¶ms, " \t")) != NULL;) {
540 kprintf("dm_target_crypt: not enough arguments, "
542 kfree(status_str, M_DMCRYPT);
543 return ENOMEM; /* XXX */
546 crypto_alg = strsep(&args[0], "-");
547 crypto_mode = strsep(&args[0], "-");
548 iv_opt = strsep(&args[0], "-");
549 iv_mode = strsep(&iv_opt, ":");
551 iv_offset = strtouq(args[2], NULL, 0);
553 block_offset = strtouq(args[4], NULL, 0);
554 /* bits / 8 = bytes, 1 byte = 2 hexa chars, so << 2 */
555 klen = strlen(key) << 2;
558 kprintf("dm_target_crypt - new: dev=%s, crypto_alg=%s, crypto_mode=%s, "
559 "iv_mode=%s, iv_opt=%s, key=%s, iv_offset=%ju, "
560 "block_offset=%ju\n",
561 dev, crypto_alg, crypto_mode, iv_mode, iv_opt, key, iv_offset,
565 priv = kmalloc(sizeof(dm_target_crypt_config_t), M_DMCRYPT, M_WAITOK);
567 /* Insert dmp to global pdev list */
568 if ((priv->pdev = dm_pdev_insert(dev)) == NULL) {
569 kprintf("dm_target_crypt: dm_pdev_insert failed\n");
570 kfree(status_str, M_DMCRYPT);
575 * This code checks for valid combinations of algorithm and mode.
576 * Currently supported options are:
583 if ((strcmp(crypto_mode, "cbc") != 0) &&
584 !((strcmp(crypto_mode, "xts") == 0) &&
585 ((strcmp(crypto_alg, "aes") == 0) ||
586 (strcmp(crypto_alg, "twofish") == 0) ||
587 (strcmp(crypto_alg, "serpent") == 0))))
589 kprintf("dm_target_crypt: only support 'cbc' chaining mode,"
590 " aes-xts, twofish-xts and serpent-xts, "
591 "invalid mode '%s-%s'\n",
592 crypto_alg, crypto_mode);
596 if (!strcmp(crypto_alg, "aes")) {
597 if (!strcmp(crypto_mode, "xts")) {
598 priv->crypto_alg = CRYPTO_AES_XTS;
599 if (klen != 256 && klen != 512)
601 } else if (!strcmp(crypto_mode, "cbc")) {
602 priv->crypto_alg = CRYPTO_AES_CBC;
603 if (klen != 128 && klen != 192 && klen != 256)
608 priv->crypto_klen = klen;
609 } else if (!strcmp(crypto_alg, "twofish")) {
610 if (!strcmp(crypto_mode, "xts")) {
611 priv->crypto_alg = CRYPTO_TWOFISH_XTS;
612 if (klen != 256 && klen != 512)
614 } else if (!strcmp(crypto_mode, "cbc")) {
615 priv->crypto_alg = CRYPTO_TWOFISH_CBC;
616 if (klen != 128 && klen != 192 && klen != 256)
621 priv->crypto_klen = klen;
622 } else if (!strcmp(crypto_alg, "serpent")) {
623 if (!strcmp(crypto_mode, "xts")) {
624 priv->crypto_alg = CRYPTO_SERPENT_XTS;
625 if (klen != 256 && klen != 512)
627 } else if (!strcmp(crypto_mode, "cbc")) {
628 priv->crypto_alg = CRYPTO_SERPENT_CBC;
629 if (klen != 128 && klen != 192 && klen != 256)
634 priv->crypto_klen = klen;
635 } else if (!strcmp(crypto_alg, "blowfish")) {
636 priv->crypto_alg = CRYPTO_BLF_CBC;
637 if (klen < 128 || klen > 448 || (klen % 8) != 0)
639 priv->crypto_klen = klen;
640 } else if (!strcmp(crypto_alg, "3des") ||
641 !strncmp(crypto_alg, "des3", 4)) {
642 priv->crypto_alg = CRYPTO_3DES_CBC;
645 priv->crypto_klen = 168;
646 } else if (!strcmp(crypto_alg, "camellia")) {
647 priv->crypto_alg = CRYPTO_CAMELLIA_CBC;
648 if (klen != 128 && klen != 192 && klen != 256)
650 priv->crypto_klen = klen;
651 } else if (!strcmp(crypto_alg, "skipjack")) {
652 priv->crypto_alg = CRYPTO_SKIPJACK_CBC;
655 priv->crypto_klen = 80;
656 } else if (!strcmp(crypto_alg, "cast5")) {
657 priv->crypto_alg = CRYPTO_CAST_CBC;
660 priv->crypto_klen = 128;
661 } else if (!strcmp(crypto_alg, "null")) {
662 priv->crypto_alg = CRYPTO_NULL_CBC;
665 priv->crypto_klen = 128;
667 kprintf("dm_target_crypt: Unsupported crypto algorithm: %s\n",
672 /* Save length of param string */
673 priv->params_len = len;
674 priv->block_offset = block_offset;
675 priv->iv_offset = iv_offset - block_offset;
677 *target_config = priv;
679 dmv->dev_type = DM_CRYPTO_DEV;
681 error = hex2key(key, priv->crypto_klen >> 3,
682 (u_int8_t *)priv->crypto_key);
685 kprintf("dm_target_crypt: hex2key failed, "
686 "invalid key format\n");
691 for(i = 0; ivgens[i].name != NULL; i++) {
692 if (!strcmp(iv_mode, ivgens[i].name))
696 if (ivgens[i].name == NULL) {
697 kprintf("dm_target_crypt: iv_mode='%s' unsupported\n",
702 /* Call our ivgen constructor */
703 if (ivgens[i].ctor != NULL) {
704 error = ivgens[i].ctor(priv, iv_opt,
707 kprintf("dm_target_crypt: ctor for '%s' failed\n",
713 priv->ivgen = &ivgens[i];
715 priv->crypto_session.cri_alg = priv->crypto_alg;
716 priv->crypto_session.cri_key = (u_int8_t *)priv->crypto_key;
717 priv->crypto_session.cri_klen = priv->crypto_klen;
718 priv->crypto_session.cri_mlen = 0;
719 priv->crypto_session.cri_next = NULL;
721 error = crypto_newsession(&priv->crypto_sid,
722 &priv->crypto_session,
723 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
725 kprintf("dm_target_crypt: Error during crypto_newsession, "
731 memset(key, '0', strlen(key));
733 ksprintf(status_str, "%s-%s-%s:%s %s %ju %s %ju",
734 crypto_alg, crypto_mode, iv_mode, iv_opt,
735 key, iv_offset, dev, block_offset);
737 ksprintf(status_str, "%s-%s-%s %s %ju %s %ju",
738 crypto_alg, crypto_mode, iv_mode,
739 key, iv_offset, dev, block_offset);
741 priv->status_str = status_str;
743 /* Initialize mpipes */
744 dmtc_init_mpipe(priv);
749 kprintf("dm_target_crypt: ENOTSUP\n");
750 kfree(status_str, M_DMCRYPT);
754 /* Status routine called to get params string. */
756 dm_target_crypt_status(void *target_config)
758 dm_target_crypt_config_t *priv;
761 priv = target_config;
763 /* caller expects use of M_DM */
764 params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
766 ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s",
773 dm_target_crypt_destroy(dm_table_entry_t * table_en)
775 dm_target_crypt_config_t *priv;
778 * Disconnect the crypt config before unbusying the target.
780 priv = table_en->target_config;
783 table_en->target_config = NULL;
784 dm_pdev_decr(priv->pdev);
787 * Clean up the crypt config
789 * Overwrite the private information before freeing memory to
792 if (priv->status_str) {
793 dmtc_crypto_clear(priv->status_str, strlen(priv->status_str));
794 kfree(priv->status_str, M_DMCRYPT);
795 crypto_freesession(priv->crypto_sid);
798 if ((priv->ivgen) && (priv->ivgen->dtor != NULL)) {
799 priv->ivgen->dtor(priv, priv->ivgen_priv);
803 dmtc_destroy_mpipe(priv);
805 dmtc_crypto_clear(priv, sizeof(dm_target_crypt_config_t));
806 kfree(priv, M_DMCRYPT);
812 dm_target_crypt_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
814 dm_target_crypt_config_t *priv;
819 if (table_en->target_config == NULL)
822 priv = table_en->target_config;
824 if ((error = VOP_GETATTR(priv->pdev->pdev_vnode, &va)) != 0)
827 prop_array_add_uint64(prop_array,
828 (uint64_t)makeudev(va.va_rmajor, va.va_rminor));
833 /* Unsupported for this target. */
835 dm_target_crypt_upcall(dm_table_entry_t * table_en, struct buf * bp)
840 /************************************************************************
841 * STRATEGY SUPPORT FUNCTIONS *
842 ************************************************************************
844 * READ PATH: doio -> bio_read_done -> crypto_work -> crypto_cb_read_done
845 * WRITE PATH: crypto_work -> crypto_cb_write_done -> doio -> bio_write_done
849 * Wrapper around crypto_dispatch() to match dispatch_t type
852 dmtc_crypto_dispatch(void *arg)
856 crp = (struct cryptop *)arg;
857 KKASSERT(crp != NULL);
858 KTR_LOG(dmcrypt_crypto_dispatch, crp);
859 crypto_dispatch(crp);
863 * Start IO operation, called from dmstrategy routine.
866 dm_target_crypt_strategy(dm_table_entry_t *table_en, struct buf *bp)
870 dm_target_crypt_config_t *priv;
871 priv = table_en->target_config;
873 /* Get rid of stuff we can't really handle */
874 if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
875 if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
876 kprintf("dm_target_crypt_strategy: can't really "
877 "handle bp->b_bcount = %d\n",
879 bp->b_error = EINVAL;
880 bp->b_flags |= B_ERROR | B_INVAL;
881 biodone(&bp->b_bio1);
886 KTR_LOG(dmcrypt_crypt_strategy, bp->b_cmd, bp);
890 bio = push_bio(&bp->b_bio1);
891 bio->bio_offset = bp->b_bio1.bio_offset +
892 priv->block_offset * DEV_BSIZE;
893 bio->bio_caller_info1.ptr = priv;
894 bio->bio_done = dmtc_bio_read_done;
895 vn_strategy(priv->pdev->pdev_vnode, bio);
898 bio = push_bio(&bp->b_bio1);
899 bio->bio_offset = bp->b_bio1.bio_offset +
900 priv->block_offset * DEV_BSIZE;
901 bio->bio_caller_info1.ptr = priv;
902 dmtc_crypto_write_start(priv, bio);
905 vn_strategy(priv->pdev->pdev_vnode, &bp->b_bio1);
912 * STRATEGY READ PATH PART 1/3 (after read BIO completes)
915 dmtc_bio_read_done(struct bio *bio)
919 dm_target_crypt_config_t *priv;
921 KTR_LOG(dmcrypt_bio_read_done, bio->bio_buf);
924 * If a read error occurs we shortcut the operation, otherwise
927 if (bio->bio_buf->b_flags & B_ERROR) {
931 priv = bio->bio_caller_info1.ptr;
932 dmtc_crypto_read_start(priv, bio);
937 * STRATEGY READ PATH PART 2/3
940 dmtc_crypto_read_retry(void *arg1, void *arg2)
942 dm_target_crypt_config_t *priv = arg1;
943 struct bio *bio = arg2;
945 dmtc_crypto_read_start(priv, bio);
949 dmtc_crypto_read_start(dm_target_crypt_config_t *priv, struct bio *bio)
951 struct dmtc_helper *dmtc;
952 struct cryptodesc *crd;
954 struct cryptoini *cri;
955 int i, bytes, sectors, sz;
959 cri = &priv->crypto_session;
962 * Note: b_resid no good after read I/O, it will be 0, use
965 bytes = bio->bio_buf->b_bcount;
966 isector = bio->bio_offset / DEV_BSIZE; /* ivgen salt base? */
967 sectors = bytes / DEV_BSIZE; /* Number of sectors */
968 sz = sectors * (sizeof(*crp) + sizeof(*crd));
971 * For reads with bogus page we can't decrypt in place as stuff
972 * can get ripped out from under us.
974 * XXX actually it looks like we can, and in any case the initial
975 * read already completed and threw crypted data into the buffer
976 * cache buffer. Disable for now.
978 space = mpipe_alloc_callback(&priv->read_mpipe,
979 dmtc_crypto_read_retry, priv, bio);
983 dmtc = (struct dmtc_helper *)space;
984 dmtc->free_addr = space;
985 space += sizeof(struct dmtc_helper);
986 dmtc->orig_buf = NULL;
987 dmtc->data_buf = bio->bio_buf->b_data;
989 bio->bio_caller_info2.ptr = dmtc;
990 bio->bio_buf->b_error = 0;
993 * Load crypto descriptors (crp/crd loop)
997 bio->bio_caller_info3.value = sectors;
1000 kprintf("Read, bytes = %d (b_bcount), "
1001 "sectors = %d (bio = %p, b_cmd = %d)\n",
1002 bytes, sectors, bio, bio->bio_buf->b_cmd);
1004 for (i = 0; i < sectors; i++) {
1005 crp = (struct cryptop *)ptr;
1006 ptr += sizeof(*crp);
1007 crd = (struct cryptodesc *)ptr;
1008 ptr += sizeof (*crd);
1010 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
1012 crp->crp_sid = priv->crypto_sid;
1013 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1015 crp->crp_opaque = (void *)bio;
1017 crp->crp_callback = dmtc_crypto_cb_read_done;
1018 crp->crp_desc = crd;
1020 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1023 crd->crd_alg = priv->crypto_alg;
1025 crd->crd_key = (caddr_t)priv->crypto_key;
1026 crd->crd_klen = priv->crypto_klen;
1030 crd->crd_len = DEV_BSIZE /* XXX */;
1031 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1032 crd->crd_next = NULL;
1034 crd->crd_flags &= ~CRD_F_ENCRYPT;
1036 KTR_LOG(dmcrypt_crypto_read_start, crp, bio->bio_buf, i,
1040 * Note: last argument is used to generate salt(?) and is
1041 * a 64 bit value, but the original code passed an
1042 * int. Changing it now will break pre-existing
1045 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1051 * STRATEGY READ PATH PART 3/3
1054 dmtc_crypto_cb_read_done(struct cryptop *crp)
1056 struct dmtc_helper *dmtc;
1057 struct bio *bio, *obio;
1060 if (crp->crp_etype == EAGAIN)
1061 return crypto_dispatch(crp);
1063 bio = (struct bio *)crp->crp_opaque;
1064 KKASSERT(bio != NULL);
1069 if (crp->crp_etype) {
1070 kprintf("dm_target_crypt: dmtc_crypto_cb_read_done "
1073 bio->bio_buf->b_error = crp->crp_etype;
1077 * On the last chunk of the decryption we do any required copybacks
1078 * and complete the I/O.
1080 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
1082 kprintf("dmtc_crypto_cb_read_done %p, n = %d\n", bio, n);
1085 KTR_LOG(dmcrypt_crypto_cb_read_done, crp, bio->bio_buf, n);
1089 * For the B_HASBOGUS case we didn't decrypt in place,
1090 * so we need to copy stuff back into the buf.
1092 * (disabled for now).
1094 dmtc = bio->bio_caller_info2.ptr;
1095 if (bio->bio_buf->b_error) {
1096 bio->bio_buf->b_flags |= B_ERROR;
1099 else if (bio->bio_buf->b_flags & B_HASBOGUS) {
1100 memcpy(bio->bio_buf->b_data, dmtc->data_buf,
1101 bio->bio_buf->b_bcount);
1104 mpipe_free(&dmtc->priv->read_mpipe, dmtc->free_addr);
1105 obio = pop_bio(bio);
1110 /* END OF STRATEGY READ SECTION */
1113 * STRATEGY WRITE PATH PART 1/3
1117 dmtc_crypto_write_retry(void *arg1, void *arg2)
1119 dm_target_crypt_config_t *priv = arg1;
1120 struct bio *bio = arg2;
1122 KTR_LOG(dmcrypt_crypto_write_retry, bio->bio_buf);
1124 dmtc_crypto_write_start(priv, bio);
1128 dmtc_crypto_write_start(dm_target_crypt_config_t *priv, struct bio *bio)
1130 struct dmtc_helper *dmtc;
1131 struct cryptodesc *crd;
1132 struct cryptop *crp;
1133 struct cryptoini *cri;
1134 int i, bytes, sectors, sz;
1136 u_char *ptr, *space;
1138 cri = &priv->crypto_session;
1141 * Use b_bcount for consistency
1143 bytes = bio->bio_buf->b_bcount;
1145 isector = bio->bio_offset / DEV_BSIZE; /* ivgen salt base? */
1146 sectors = bytes / DEV_BSIZE; /* Number of sectors */
1147 sz = sectors * (sizeof(*crp) + sizeof(*crd));
1150 * For writes and reads with bogus page don't decrypt in place.
1152 space = mpipe_alloc_callback(&priv->write_mpipe,
1153 dmtc_crypto_write_retry, priv, bio);
1157 dmtc = (struct dmtc_helper *)space;
1158 dmtc->free_addr = space;
1159 space += sizeof(struct dmtc_helper);
1160 memcpy(space + sz, bio->bio_buf->b_data, bytes);
1162 bio->bio_caller_info2.ptr = dmtc;
1163 bio->bio_buf->b_error = 0;
1165 dmtc->orig_buf = bio->bio_buf->b_data;
1166 dmtc->data_buf = space + sz;
1170 * Load crypto descriptors (crp/crd loop)
1174 bio->bio_caller_info3.value = sectors;
1177 kprintf("Write, bytes = %d (b_bcount), "
1178 "sectors = %d (bio = %p, b_cmd = %d)\n",
1179 bytes, sectors, bio, bio->bio_buf->b_cmd);
1181 for (i = 0; i < sectors; i++) {
1182 crp = (struct cryptop *)ptr;
1183 ptr += sizeof(*crp);
1184 crd = (struct cryptodesc *)ptr;
1185 ptr += sizeof (*crd);
1187 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
1189 crp->crp_sid = priv->crypto_sid;
1190 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1192 crp->crp_opaque = (void *)bio;
1194 crp->crp_callback = dmtc_crypto_cb_write_done;
1195 crp->crp_desc = crd;
1197 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1200 crd->crd_alg = priv->crypto_alg;
1202 crd->crd_key = (caddr_t)priv->crypto_key;
1203 crd->crd_klen = priv->crypto_klen;
1207 crd->crd_len = DEV_BSIZE /* XXX */;
1208 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1209 crd->crd_next = NULL;
1211 crd->crd_flags |= CRD_F_ENCRYPT;
1214 * Note: last argument is used to generate salt(?) and is
1215 * a 64 bit value, but the original code passed an
1216 * int. Changing it now will break pre-existing
1220 KTR_LOG(dmcrypt_crypto_write_start, crp, bio->bio_buf,
1223 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1229 * STRATEGY WRITE PATH PART 2/3
1232 dmtc_crypto_cb_write_done(struct cryptop *crp)
1234 struct dmtc_helper *dmtc;
1235 dm_target_crypt_config_t *priv;
1236 struct bio *bio, *obio;
1239 if (crp->crp_etype == EAGAIN)
1240 return crypto_dispatch(crp);
1242 bio = (struct bio *)crp->crp_opaque;
1243 KKASSERT(bio != NULL);
1248 if (crp->crp_etype != 0) {
1249 kprintf("dm_target_crypt: dmtc_crypto_cb_write_done "
1252 bio->bio_buf->b_error = crp->crp_etype;
1256 * On the last chunk of the encryption we issue the write
1258 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
1260 kprintf("dmtc_crypto_cb_write_done %p, n = %d\n", bio, n);
1263 KTR_LOG(dmcrypt_crypto_cb_write_done, crp, bio->bio_buf, n);
1266 dmtc = bio->bio_caller_info2.ptr;
1267 priv = (dm_target_crypt_config_t *)bio->bio_caller_info1.ptr;
1269 if (bio->bio_buf->b_error) {
1270 bio->bio_buf->b_flags |= B_ERROR;
1271 mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
1272 obio = pop_bio(bio);
1275 dmtc->orig_buf = bio->bio_buf->b_data;
1276 bio->bio_buf->b_data = dmtc->data_buf;
1277 bio->bio_done = dmtc_bio_write_done;
1278 vn_strategy(priv->pdev->pdev_vnode, bio);
1285 * STRATEGY WRITE PATH PART 3/3
1288 dmtc_bio_write_done(struct bio *bio)
1290 struct dmtc_helper *dmtc;
1293 dmtc = bio->bio_caller_info2.ptr;
1294 bio->bio_buf->b_data = dmtc->orig_buf;
1295 mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
1297 KTR_LOG(dmcrypt_bio_write_done, bio->bio_buf);
1299 obio = pop_bio(bio);
1302 /* END OF STRATEGY WRITE SECTION */
1308 extern int tsleep_crypto_dump;
1311 dm_target_crypt_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
1313 static struct dmtc_dump_helper dump_helper;
1314 dm_target_crypt_config_t *priv;
1316 static int first_call = 1;
1318 priv = table_en->target_config;
1322 dump_reactivate_cpus();
1325 /* Magically enable tsleep */
1326 tsleep_crypto_dump = 1;
1330 * 0 length means flush buffers and return
1333 if (priv->pdev->pdev_vnode->v_rdev == NULL) {
1334 tsleep_crypto_dump = 0;
1337 dev_ddump(priv->pdev->pdev_vnode->v_rdev,
1338 data, 0, offset, 0);
1339 tsleep_crypto_dump = 0;
1343 bzero(&dump_helper, sizeof(dump_helper));
1344 dump_helper.priv = priv;
1345 dump_helper.data = data;
1346 dump_helper.length = length;
1347 dump_helper.offset = offset +
1348 priv->block_offset * DEV_BSIZE;
1349 dump_helper.ident = &id;
1350 dmtc_crypto_dump_start(priv, &dump_helper);
1353 * Hackery to make stuff appear synchronous. The crypto callback will
1354 * set id to 1 and call wakeup on it. If the request completed
1355 * synchronously, id will be 1 and we won't bother to sleep. If not,
1356 * the crypto request will complete asynchronously and we sleep until
1360 tsleep(&dump_helper, 0, "cryptdump", 0);
1362 dump_helper.offset = dm_pdev_correct_dump_offset(priv->pdev,
1363 dump_helper.offset);
1365 dev_ddump(priv->pdev->pdev_vnode->v_rdev,
1366 dump_helper.space, 0, dump_helper.offset,
1367 dump_helper.length);
1369 tsleep_crypto_dump = 0;
1374 dmtc_crypto_dump_start(dm_target_crypt_config_t *priv, struct dmtc_dump_helper *dump_helper)
1376 struct cryptodesc *crd;
1377 struct cryptop *crp;
1378 struct cryptoini *cri;
1379 int i, bytes, sectors;
1382 cri = &priv->crypto_session;
1384 bytes = dump_helper->length;
1386 isector = dump_helper->offset / DEV_BSIZE; /* ivgen salt base? */
1387 sectors = bytes / DEV_BSIZE; /* Number of sectors */
1388 dump_helper->sectors = sectors;
1390 kprintf("Dump, bytes = %d, "
1391 "sectors = %d, LENGTH=%zu\n", bytes, sectors, dump_helper->length);
1393 KKASSERT(dump_helper->length <= 65536);
1395 memcpy(dump_helper->space, dump_helper->data, bytes);
1399 for (i = 0; i < sectors; i++) {
1400 crp = &dump_helper->crp[i];
1401 crd = &dump_helper->crd[i];
1403 crp->crp_buf = dump_helper->space + i * DEV_BSIZE;
1405 crp->crp_sid = priv->crypto_sid;
1406 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1408 crp->crp_opaque = (void *)dump_helper;
1410 crp->crp_callback = dmtc_crypto_cb_dump_done;
1411 crp->crp_desc = crd;
1413 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1416 crd->crd_alg = priv->crypto_alg;
1419 crd->crd_len = DEV_BSIZE /* XXX */;
1420 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1421 crd->crd_next = NULL;
1423 crd->crd_flags |= CRD_F_ENCRYPT;
1426 * Note: last argument is used to generate salt(?) and is
1427 * a 64 bit value, but the original code passed an
1428 * int. Changing it now will break pre-existing
1431 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1437 dmtc_crypto_cb_dump_done(struct cryptop *crp)
1439 struct dmtc_dump_helper *dump_helper;
1440 dm_target_crypt_config_t *priv;
1443 if (crp->crp_etype == EAGAIN)
1444 return crypto_dispatch(crp);
1446 dump_helper = (struct dmtc_dump_helper *)crp->crp_opaque;
1447 KKASSERT(dump_helper != NULL);
1449 if (crp->crp_etype != 0) {
1450 kprintf("dm_target_crypt: dmtc_crypto_cb_dump_done "
1453 return crp->crp_etype;
1457 * On the last chunk of the encryption we return control
1459 n = atomic_fetchadd_int(&dump_helper->sectors, -1);
1462 priv = (dm_target_crypt_config_t *)dump_helper->priv;
1463 atomic_add_int(dump_helper->ident, 1);
1464 wakeup(dump_helper);
1471 dmtc_mod_handler(module_t mod, int type, void *unused)
1473 dm_target_t *dmt = NULL;
1478 if ((dmt = dm_target_lookup("crypt")) != NULL) {
1479 dm_target_unbusy(dmt);
1482 dmt = dm_target_alloc("crypt");
1483 dmt->version[0] = 1;
1484 dmt->version[1] = 6;
1485 dmt->version[2] = 0;
1486 strlcpy(dmt->name, "crypt", DM_MAX_TYPE_NAME);
1487 dmt->init = &dm_target_crypt_init;
1488 dmt->status = &dm_target_crypt_status;
1489 dmt->strategy = &dm_target_crypt_strategy;
1490 dmt->deps = &dm_target_crypt_deps;
1491 dmt->destroy = &dm_target_crypt_destroy;
1492 dmt->upcall = &dm_target_crypt_upcall;
1493 dmt->dump = &dm_target_crypt_dump;
1495 err = dm_target_insert(dmt);
1497 kprintf("dm_target_crypt: Successfully initialized\n");
1501 err = dm_target_rem("crypt");
1503 kprintf("dm_target_crypt: unloaded\n");
1514 DM_TARGET_MODULE(dm_target_crypt, dmtc_mod_handler);