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/endian.h>
43 #include <sys/globaldata.h>
44 #include <sys/kerneldump.h>
45 #include <sys/malloc.h>
46 #include <sys/mpipe.h>
48 #include <sys/mutex2.h>
49 #include <sys/vnode.h>
50 #include <crypto/sha1.h>
51 #include <crypto/sha2/sha2.h>
52 #include <opencrypto/cryptodev.h>
53 #include <opencrypto/rmd160.h>
54 #include <machine/cpufunc.h>
58 #include <dev/disk/dm/dm.h>
59 MALLOC_DEFINE(M_DMCRYPT, "dm_crypt", "Device Mapper Target Crypt");
61 KTR_INFO_MASTER(dmcrypt);
63 #if !defined(KTR_DMCRYPT)
64 #define KTR_DMCRYPT KTR_ALL
67 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_dispatch, 0,
68 "crypto_dispatch(%p)", struct cryptop *crp);
69 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypt_strategy, 0,
70 "crypt_strategy(b_cmd = %d, bp = %p)", int cmd, struct buf *bp);
71 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_write_start, 1,
72 "crypto_write_start(crp = %p, bp = %p, sector = %d/%d)",
73 struct cryptop *crp, struct buf *bp, int i, int sectors);
74 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_cb_write_done, 1,
75 "crypto_cb_write_done(crp = %p, bp = %p, n = %d)",
76 struct cryptop *crp, struct buf *bp, int n);
77 KTR_INFO(KTR_DMCRYPT, dmcrypt, bio_write_done, 1,
78 "bio_write_done(bp = %p)", struct buf *bp);
79 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_write_retry, 1,
80 "crypto_write_retry(crp = %p)", struct buf *bp);
81 KTR_INFO(KTR_DMCRYPT, dmcrypt, bio_read_done, 2,
82 "bio_read_done(bp = %p)", struct buf *bp);
83 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_read_start, 2,
84 "crypto_read_start(crp = %p, bp = %p, sector = %d/%d)",
85 struct cryptop *crp, struct buf *bp, int i, int sectors);
86 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_cb_read_done, 2,
87 "crypto_cb_read_done(crp = %p, bp = %p, n = %d)",
88 struct cryptop *crp, struct buf *bp, int n);
90 struct target_crypt_config;
92 typedef void dispatch_t(void *);
93 typedef void ivgen_t(struct target_crypt_config *, u_int8_t *, size_t, off_t,
96 typedef int ivgen_ctor_t(struct target_crypt_config *, char *, void **);
97 typedef int ivgen_dtor_t(struct target_crypt_config *, void *);
106 struct essiv_ivgen_priv {
107 struct cryptoini crypto_session;
108 struct objcache *crp_crd_cache;
109 u_int64_t crypto_sid;
111 u_int8_t crypto_keyhash[SHA512_DIGEST_LENGTH];
114 typedef struct target_crypt_config {
120 u_int8_t crypto_key[512>>3];
122 u_int64_t crypto_sid;
123 u_int64_t block_offset;
125 SHA512_CTX essivsha512_ctx;
127 struct cryptoini crypto_session;
129 struct iv_generator *ivgen;
132 struct malloc_pipe read_mpipe;
133 struct malloc_pipe write_mpipe;
134 } dm_target_crypt_config_t;
137 dm_target_crypt_config_t *priv;
143 struct dmtc_dump_helper {
144 dm_target_crypt_config_t *priv;
152 struct cryptodesc crd[128];
153 struct cryptop crp[128];
157 #define DMTC_BUF_SIZE_WRITE \
158 MAXPHYS + sizeof(struct dmtc_helper) + \
159 MAXPHYS/DEV_BSIZE*(sizeof(struct cryptop) + sizeof(struct cryptodesc))
160 #define DMTC_BUF_SIZE_READ \
161 sizeof(struct dmtc_helper) + \
162 MAXPHYS/DEV_BSIZE*(sizeof(struct cryptop) + sizeof(struct cryptodesc))
164 static void dmtc_crypto_dispatch(void *arg);
165 static void dmtc_crypto_dump_start(dm_target_crypt_config_t *priv,
166 struct dmtc_dump_helper *dump_helper);
167 static void dmtc_crypto_read_start(dm_target_crypt_config_t *priv,
169 static void dmtc_crypto_write_start(dm_target_crypt_config_t *priv,
171 static void dmtc_bio_read_done(struct bio *bio);
172 static void dmtc_bio_write_done(struct bio *bio);
173 static int dmtc_crypto_cb_dump_done(struct cryptop *crp);
174 static int dmtc_crypto_cb_read_done(struct cryptop *crp);
175 static int dmtc_crypto_cb_write_done(struct cryptop *crp);
177 static ivgen_ctor_t essiv_ivgen_ctor;
178 static ivgen_dtor_t essiv_ivgen_dtor;
179 static ivgen_t essiv_ivgen;
180 static ivgen_t plain_ivgen;
181 static ivgen_t plain64_ivgen;
183 static struct iv_generator ivgens[] = {
184 { .name = "essiv", .ctor = essiv_ivgen_ctor, .dtor = essiv_ivgen_dtor,
185 .gen_iv = essiv_ivgen },
186 { .name = "plain", .ctor = NULL, .dtor = NULL, .gen_iv = plain_ivgen },
187 { .name = "plain64", .ctor = NULL, .dtor = NULL, .gen_iv = plain64_ivgen },
188 { NULL, NULL, NULL, NULL }
191 struct objcache_malloc_args essiv_ivgen_malloc_args = {
192 2*sizeof(void *) + (sizeof(struct cryptodesc) +
193 sizeof(struct cryptop)), M_DMCRYPT };
196 dmtc_init_mpipe(struct target_crypt_config *priv)
200 nmax = (physmem*2/1000*PAGE_SIZE)/(DMTC_BUF_SIZE_WRITE + DMTC_BUF_SIZE_READ) + 1;
205 kprintf("dm_target_crypt: Setting min/max mpipe buffers: %d/%d\n", 2, nmax);
207 mpipe_init(&priv->write_mpipe, M_DMCRYPT, DMTC_BUF_SIZE_WRITE,
208 2, nmax, MPF_NOZERO | MPF_CALLBACK, NULL, NULL, NULL);
209 mpipe_init(&priv->read_mpipe, M_DMCRYPT, DMTC_BUF_SIZE_READ,
210 2, nmax, MPF_NOZERO | MPF_CALLBACK, NULL, NULL, NULL);
214 dmtc_destroy_mpipe(struct target_crypt_config *priv)
216 mpipe_done(&priv->write_mpipe);
217 mpipe_done(&priv->read_mpipe);
221 * Overwrite private information (in buf) to avoid leaking it
224 dmtc_crypto_clear(void *buf, size_t len)
226 memset(buf, 0xFF, len);
231 * ESSIV IV Generator Routines
234 essiv_ivgen_ctor(struct target_crypt_config *priv, char *iv_hash, void **p_ivpriv)
236 struct essiv_ivgen_priv *ivpriv;
237 u_int8_t crypto_keyhash[SHA512_DIGEST_LENGTH];
238 unsigned int klen, hashlen;
241 klen = (priv->crypto_klen >> 3);
246 if (!strcmp(iv_hash, "sha1")) {
249 hashlen = SHA1_RESULTLEN;
251 SHA1Update(&ctx, priv->crypto_key, klen);
252 SHA1Final(crypto_keyhash, &ctx);
253 } else if (!strcmp(iv_hash, "sha256")) {
256 hashlen = SHA256_DIGEST_LENGTH;
258 SHA256_Update(&ctx, priv->crypto_key, klen);
259 SHA256_Final(crypto_keyhash, &ctx);
260 } else if (!strcmp(iv_hash, "sha384")) {
263 hashlen = SHA384_DIGEST_LENGTH;
265 SHA384_Update(&ctx, priv->crypto_key, klen);
266 SHA384_Final(crypto_keyhash, &ctx);
267 } else if (!strcmp(iv_hash, "sha512")) {
270 hashlen = SHA512_DIGEST_LENGTH;
272 SHA512_Update(&ctx, priv->crypto_key, klen);
273 SHA512_Final(crypto_keyhash, &ctx);
274 } else if (!strcmp(iv_hash, "md5")) {
277 hashlen = MD5_DIGEST_LENGTH;
279 MD5Update(&ctx, priv->crypto_key, klen);
280 MD5Final(crypto_keyhash, &ctx);
281 } else if (!strcmp(iv_hash, "rmd160") ||
282 !strcmp(iv_hash, "ripemd160")) {
287 RMD160Update(&ctx, priv->crypto_key, klen);
288 RMD160Final(crypto_keyhash, &ctx);
293 /* Convert hashlen to bits */
296 ivpriv = kmalloc(sizeof(struct essiv_ivgen_priv), M_DMCRYPT,
298 memcpy(ivpriv->crypto_keyhash, crypto_keyhash, sizeof(crypto_keyhash));
299 ivpriv->keyhash_len = sizeof(crypto_keyhash);
300 dmtc_crypto_clear(crypto_keyhash, sizeof(crypto_keyhash));
302 ivpriv->crypto_session.cri_alg = priv->crypto_alg;
303 ivpriv->crypto_session.cri_key = (u_int8_t *)ivpriv->crypto_keyhash;
304 ivpriv->crypto_session.cri_klen = hashlen;
305 ivpriv->crypto_session.cri_mlen = 0;
306 ivpriv->crypto_session.cri_next = NULL;
309 * XXX: in principle we also need to check if the block size of the
310 * cipher is a valid iv size for the block cipher.
313 error = crypto_newsession(&ivpriv->crypto_sid,
314 &ivpriv->crypto_session,
315 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
317 kprintf("dm_target_crypt: Error during crypto_newsession "
318 "for essiv_ivgen, error = %d\n",
320 dmtc_crypto_clear(ivpriv->crypto_keyhash, ivpriv->keyhash_len);
321 kfree(ivpriv, M_DMCRYPT);
325 ivpriv->crp_crd_cache = objcache_create(
326 "dmcrypt-essiv-cache", 0, 0,
328 objcache_malloc_alloc,
329 objcache_malloc_free,
330 &essiv_ivgen_malloc_args );
337 essiv_ivgen_dtor(struct target_crypt_config *priv, void *arg)
339 struct essiv_ivgen_priv *ivpriv;
341 ivpriv = (struct essiv_ivgen_priv *)arg;
342 KKASSERT(ivpriv != NULL);
344 crypto_freesession(ivpriv->crypto_sid);
346 objcache_destroy(ivpriv->crp_crd_cache);
348 dmtc_crypto_clear(ivpriv->crypto_keyhash, ivpriv->keyhash_len);
349 kfree(ivpriv, M_DMCRYPT);
355 essiv_ivgen_done(struct cryptop *crp)
357 struct essiv_ivgen_priv *ivpriv;
362 if (crp->crp_etype == EAGAIN)
363 return crypto_dispatch(crp);
365 if (crp->crp_etype != 0) {
366 kprintf("dm_target_crypt: essiv_ivgen_done, "
367 "crp->crp_etype = %d\n", crp->crp_etype);
370 free_addr = crp->crp_opaque;
372 * In-memory structure is:
373 * | ivpriv | opaque | crp | crd |
374 * | (void *) | (void *) | (cryptop) | (cryptodesc) |
376 ivpriv = *((struct essiv_ivgen_priv **)crp->crp_opaque);
377 crp->crp_opaque += sizeof(void *);
378 opaque = *((void **)crp->crp_opaque);
380 objcache_put(ivpriv->crp_crd_cache, free_addr);
381 dmtc_crypto_dispatch(opaque);
386 essiv_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
387 size_t iv_len, off_t sector, void *opaque)
389 struct essiv_ivgen_priv *ivpriv;
390 struct cryptodesc *crd;
392 caddr_t space, alloc_addr;
395 ivpriv = priv->ivgen_priv;
396 KKASSERT(ivpriv != NULL);
399 * In-memory structure is:
400 * | ivpriv | opaque | crp | crd |
401 * | (void *) | (void *) | (cryptop) | (cryptodesc) |
403 alloc_addr = space = objcache_get(ivpriv->crp_crd_cache, M_WAITOK);
404 *((struct essiv_ivgen_priv **)space) = ivpriv;
405 space += sizeof(void *);
406 *((void **)space) = opaque;
407 space += sizeof(void *);
408 crp = (struct cryptop *)space;
409 space += sizeof(struct cryptop);
410 crd = (struct cryptodesc *)space;
413 bzero(crd, sizeof(struct cryptodesc));
414 bzero(crp, sizeof(struct cryptop));
415 *((off_t *)iv) = htole64(sector + priv->iv_offset);
416 crp->crp_buf = (caddr_t)iv;
418 crp->crp_sid = ivpriv->crypto_sid;
419 crp->crp_ilen = crp->crp_olen = iv_len;
421 crp->crp_opaque = alloc_addr;
423 crp->crp_callback = essiv_ivgen_done;
427 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL | CRYPTO_F_BATCH;
429 crd->crd_alg = priv->crypto_alg;
431 crd->crd_key = (caddr_t)priv->crypto_keyhash;
432 crd->crd_klen = priv->crypto_klen;
435 bzero(crd->crd_iv, sizeof(crd->crd_iv));
438 crd->crd_len = iv_len;
439 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
440 crd->crd_flags |= CRD_F_ENCRYPT;
441 crd->crd_next = NULL;
443 error = crypto_dispatch(crp);
445 kprintf("dm_target_crypt: essiv_ivgen, error = %d\n", error);
450 plain_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
451 size_t iv_len, off_t sector, void *opaque)
454 *((uint32_t *)iv) = htole32((uint32_t)(sector + priv->iv_offset));
455 dmtc_crypto_dispatch(opaque);
459 plain64_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
460 size_t iv_len, off_t sector, void *opaque)
463 *((uint64_t *)iv) = htole64((uint64_t)(sector + priv->iv_offset));
464 dmtc_crypto_dispatch(opaque);
469 geli_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
470 size_t iv_len, off_t sector, void *opaque)
474 u_int8_t md[SHA512_DIGEST_LENGTH]; /* Max. Digest Size */
476 memcpy(&ctx512, &priv->essivsha512_ctx, sizeof(SHA512_CTX));
477 SHA512_Update(&ctx512, (u_int8_t*)§or, sizeof(off_t));
478 SHA512_Final(md, &ctx512);
480 memcpy(iv, md, iv_len);
481 dmtc_crypto_dispatch(opaque);
486 * Init function called from dm_table_load_ioctl.
487 * cryptsetup actually passes us this:
488 * aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8
491 hex2key(char *hex, size_t key_len, u_int8_t *key)
497 for (key_idx = 0; key_idx < key_len; ++key_idx) {
500 key[key_idx] = (u_int8_t)strtoul(hex_buf, NULL, 16);
509 dm_target_crypt_init(dm_table_entry_t *table_en, int argc, char **argv)
511 dm_target_crypt_config_t *priv;
513 char *crypto_alg, *crypto_mode, *iv_mode, *iv_opt, *key, *dev;
516 uint64_t iv_offset, block_offset;
519 kprintf("dm_target_crypt: not enough arguments, "
525 for (i = 0; i < argc; i++) {
526 len += strlen(argv[i]);
529 /* len is strlen() of input string +1 */
530 status_str = kmalloc(len, M_DMCRYPT, M_WAITOK);
532 crypto_alg = strsep(&argv[0], "-");
533 crypto_mode = strsep(&argv[0], "-");
534 iv_opt = strsep(&argv[0], "-");
535 iv_mode = strsep(&iv_opt, ":");
537 iv_offset = strtouq(argv[2], NULL, 0);
539 block_offset = strtouq(argv[4], NULL, 0);
540 /* bits / 8 = bytes, 1 byte = 2 hexa chars, so << 2 */
541 klen = strlen(key) << 2;
544 kprintf("dm_target_crypt - new: dev=%s, crypto_alg=%s, crypto_mode=%s, "
545 "iv_mode=%s, iv_opt=%s, key=%s, iv_offset=%ju, "
546 "block_offset=%ju\n",
547 dev, crypto_alg, crypto_mode, iv_mode, iv_opt, key, iv_offset,
551 priv = kmalloc(sizeof(dm_target_crypt_config_t), M_DMCRYPT, M_WAITOK);
553 /* Insert dmp to global pdev list */
554 if ((priv->pdev = dm_pdev_insert(dev)) == NULL) {
555 kprintf("dm_target_crypt: dm_pdev_insert failed\n");
556 kfree(status_str, M_DMCRYPT);
561 * This code checks for valid combinations of algorithm and mode.
562 * Currently supported options are:
569 if ((strcmp(crypto_mode, "cbc") != 0) &&
570 !((strcmp(crypto_mode, "xts") == 0) &&
571 ((strcmp(crypto_alg, "aes") == 0) ||
572 (strcmp(crypto_alg, "twofish") == 0) ||
573 (strcmp(crypto_alg, "serpent") == 0))))
575 kprintf("dm_target_crypt: only support 'cbc' chaining mode,"
576 " aes-xts, twofish-xts and serpent-xts, "
577 "invalid mode '%s-%s'\n",
578 crypto_alg, crypto_mode);
582 if (!strcmp(crypto_alg, "aes")) {
583 if (!strcmp(crypto_mode, "xts")) {
584 priv->crypto_alg = CRYPTO_AES_XTS;
585 if (klen != 256 && klen != 512)
587 } else if (!strcmp(crypto_mode, "cbc")) {
588 priv->crypto_alg = CRYPTO_AES_CBC;
589 if (klen != 128 && klen != 192 && klen != 256)
594 priv->crypto_klen = klen;
595 } else if (!strcmp(crypto_alg, "twofish")) {
596 if (!strcmp(crypto_mode, "xts")) {
597 priv->crypto_alg = CRYPTO_TWOFISH_XTS;
598 if (klen != 256 && klen != 512)
600 } else if (!strcmp(crypto_mode, "cbc")) {
601 priv->crypto_alg = CRYPTO_TWOFISH_CBC;
602 if (klen != 128 && klen != 192 && klen != 256)
607 priv->crypto_klen = klen;
608 } else if (!strcmp(crypto_alg, "serpent")) {
609 if (!strcmp(crypto_mode, "xts")) {
610 priv->crypto_alg = CRYPTO_SERPENT_XTS;
611 if (klen != 256 && klen != 512)
613 } else if (!strcmp(crypto_mode, "cbc")) {
614 priv->crypto_alg = CRYPTO_SERPENT_CBC;
615 if (klen != 128 && klen != 192 && klen != 256)
620 priv->crypto_klen = klen;
621 } else if (!strcmp(crypto_alg, "blowfish")) {
622 priv->crypto_alg = CRYPTO_BLF_CBC;
623 if (klen < 128 || klen > 448 || (klen % 8) != 0)
625 priv->crypto_klen = klen;
626 } else if (!strcmp(crypto_alg, "3des") ||
627 !strncmp(crypto_alg, "des3", 4)) {
628 priv->crypto_alg = CRYPTO_3DES_CBC;
631 priv->crypto_klen = 168;
632 } else if (!strcmp(crypto_alg, "camellia")) {
633 priv->crypto_alg = CRYPTO_CAMELLIA_CBC;
634 if (klen != 128 && klen != 192 && klen != 256)
636 priv->crypto_klen = klen;
637 } else if (!strcmp(crypto_alg, "skipjack")) {
638 priv->crypto_alg = CRYPTO_SKIPJACK_CBC;
641 priv->crypto_klen = 80;
642 } else if (!strcmp(crypto_alg, "cast5")) {
643 priv->crypto_alg = CRYPTO_CAST_CBC;
646 priv->crypto_klen = 128;
647 } else if (!strcmp(crypto_alg, "null")) {
648 priv->crypto_alg = CRYPTO_NULL_CBC;
651 priv->crypto_klen = 128;
653 kprintf("dm_target_crypt: Unsupported crypto algorithm: %s\n",
658 /* Save length of param string */
659 priv->params_len = len;
660 priv->block_offset = block_offset;
661 priv->iv_offset = iv_offset - block_offset;
663 dm_table_add_deps(table_en, priv->pdev);
665 dm_table_init_target(table_en, DM_CRYPTO_DEV, priv);
667 error = hex2key(key, priv->crypto_klen >> 3,
668 (u_int8_t *)priv->crypto_key);
671 kprintf("dm_target_crypt: hex2key failed, "
672 "invalid key format\n");
677 for(i = 0; ivgens[i].name != NULL; i++) {
678 if (!strcmp(iv_mode, ivgens[i].name))
682 if (ivgens[i].name == NULL) {
683 kprintf("dm_target_crypt: iv_mode='%s' unsupported\n",
688 /* Call our ivgen constructor */
689 if (ivgens[i].ctor != NULL) {
690 error = ivgens[i].ctor(priv, iv_opt,
693 kprintf("dm_target_crypt: ctor for '%s' failed\n",
699 priv->ivgen = &ivgens[i];
701 priv->crypto_session.cri_alg = priv->crypto_alg;
702 priv->crypto_session.cri_key = (u_int8_t *)priv->crypto_key;
703 priv->crypto_session.cri_klen = priv->crypto_klen;
704 priv->crypto_session.cri_mlen = 0;
705 priv->crypto_session.cri_next = NULL;
707 error = crypto_newsession(&priv->crypto_sid,
708 &priv->crypto_session,
709 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
711 kprintf("dm_target_crypt: Error during crypto_newsession, "
717 memset(key, '0', strlen(key));
719 ksprintf(status_str, "%s-%s-%s:%s %s %ju %s %ju",
720 crypto_alg, crypto_mode, iv_mode, iv_opt,
721 key, iv_offset, dev, block_offset);
723 ksprintf(status_str, "%s-%s-%s %s %ju %s %ju",
724 crypto_alg, crypto_mode, iv_mode,
725 key, iv_offset, dev, block_offset);
727 priv->status_str = status_str;
729 /* Initialize mpipes */
730 dmtc_init_mpipe(priv);
735 kprintf("dm_target_crypt: ENOTSUP\n");
736 kfree(status_str, M_DMCRYPT);
740 /* Table routine called to get params string. */
742 dm_target_crypt_table(void *target_config)
744 dm_target_crypt_config_t *priv;
747 priv = target_config;
749 /* caller expects use of M_DM */
750 params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
752 ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s",
759 dm_target_crypt_destroy(dm_table_entry_t *table_en)
761 dm_target_crypt_config_t *priv;
764 * Disconnect the crypt config before unbusying the target.
766 priv = table_en->target_config;
769 table_en->target_config = NULL;
770 dm_pdev_decr(priv->pdev);
773 * Clean up the crypt config
775 * Overwrite the private information before freeing memory to
778 if (priv->status_str) {
779 dmtc_crypto_clear(priv->status_str, strlen(priv->status_str));
780 kfree(priv->status_str, M_DMCRYPT);
781 crypto_freesession(priv->crypto_sid);
784 if ((priv->ivgen) && (priv->ivgen->dtor != NULL)) {
785 priv->ivgen->dtor(priv, priv->ivgen_priv);
789 dmtc_destroy_mpipe(priv);
791 dmtc_crypto_clear(priv, sizeof(dm_target_crypt_config_t));
792 kfree(priv, M_DMCRYPT);
797 /* Unsupported for this target. */
799 dm_target_crypt_upcall(dm_table_entry_t *table_en, struct buf *bp)
804 /************************************************************************
805 * STRATEGY SUPPORT FUNCTIONS *
806 ************************************************************************
808 * READ PATH: doio -> bio_read_done -> crypto_work -> crypto_cb_read_done
809 * WRITE PATH: crypto_work -> crypto_cb_write_done -> doio -> bio_write_done
813 * Wrapper around crypto_dispatch() to match dispatch_t type
816 dmtc_crypto_dispatch(void *arg)
820 crp = (struct cryptop *)arg;
821 KKASSERT(crp != NULL);
822 KTR_LOG(dmcrypt_crypto_dispatch, crp);
823 crypto_dispatch(crp);
827 * Start IO operation, called from dmstrategy routine.
830 dm_target_crypt_strategy(dm_table_entry_t *table_en, struct buf *bp)
834 dm_target_crypt_config_t *priv;
835 priv = table_en->target_config;
837 /* Get rid of stuff we can't really handle */
838 if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
839 if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
840 kprintf("dm_target_crypt_strategy: can't really "
841 "handle bp->b_bcount = %d\n",
843 bp->b_error = EINVAL;
844 bp->b_flags |= B_ERROR | B_INVAL;
845 biodone(&bp->b_bio1);
850 KTR_LOG(dmcrypt_crypt_strategy, bp->b_cmd, bp);
854 bio = push_bio(&bp->b_bio1);
855 bio->bio_offset = bp->b_bio1.bio_offset +
856 priv->block_offset * DEV_BSIZE;
857 bio->bio_caller_info1.ptr = priv;
858 bio->bio_done = dmtc_bio_read_done;
859 vn_strategy(priv->pdev->pdev_vnode, bio);
862 bio = push_bio(&bp->b_bio1);
863 bio->bio_offset = bp->b_bio1.bio_offset +
864 priv->block_offset * DEV_BSIZE;
865 bio->bio_caller_info1.ptr = priv;
866 dmtc_crypto_write_start(priv, bio);
869 vn_strategy(priv->pdev->pdev_vnode, &bp->b_bio1);
876 * STRATEGY READ PATH PART 1/3 (after read BIO completes)
879 dmtc_bio_read_done(struct bio *bio)
883 dm_target_crypt_config_t *priv;
885 KTR_LOG(dmcrypt_bio_read_done, bio->bio_buf);
888 * If a read error occurs we shortcut the operation, otherwise
891 if (bio->bio_buf->b_flags & B_ERROR) {
895 priv = bio->bio_caller_info1.ptr;
896 dmtc_crypto_read_start(priv, bio);
901 * STRATEGY READ PATH PART 2/3
904 dmtc_crypto_read_retry(void *arg1, void *arg2)
906 dm_target_crypt_config_t *priv = arg1;
907 struct bio *bio = arg2;
909 dmtc_crypto_read_start(priv, bio);
913 dmtc_crypto_read_start(dm_target_crypt_config_t *priv, struct bio *bio)
915 struct dmtc_helper *dmtc;
916 struct cryptodesc *crd;
918 int i, bytes, sectors, sz;
923 * Note: b_resid no good after read I/O, it will be 0, use
926 bytes = bio->bio_buf->b_bcount;
927 isector = bio->bio_offset / DEV_BSIZE; /* ivgen salt base? */
928 sectors = bytes / DEV_BSIZE; /* Number of sectors */
929 sz = sectors * (sizeof(*crp) + sizeof(*crd));
932 * For reads with bogus page we can't decrypt in place as stuff
933 * can get ripped out from under us.
935 * XXX actually it looks like we can, and in any case the initial
936 * read already completed and threw crypted data into the buffer
937 * cache buffer. Disable for now.
939 space = mpipe_alloc_callback(&priv->read_mpipe,
940 dmtc_crypto_read_retry, priv, bio);
944 dmtc = (struct dmtc_helper *)space;
945 dmtc->free_addr = space;
946 space += sizeof(struct dmtc_helper);
947 dmtc->orig_buf = NULL;
948 dmtc->data_buf = bio->bio_buf->b_data;
950 bio->bio_caller_info2.ptr = dmtc;
951 bio->bio_buf->b_error = 0;
954 * Load crypto descriptors (crp/crd loop)
958 bio->bio_caller_info3.value = sectors;
961 kprintf("Read, bytes = %d (b_bcount), "
962 "sectors = %d (bio = %p, b_cmd = %d)\n",
963 bytes, sectors, bio, bio->bio_buf->b_cmd);
965 for (i = 0; i < sectors; i++) {
966 crp = (struct cryptop *)ptr;
968 crd = (struct cryptodesc *)ptr;
969 ptr += sizeof (*crd);
971 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
973 crp->crp_sid = priv->crypto_sid;
974 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
976 crp->crp_opaque = (void *)bio;
978 crp->crp_callback = dmtc_crypto_cb_read_done;
981 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
984 crd->crd_alg = priv->crypto_alg;
986 crd->crd_key = (caddr_t)priv->crypto_key;
987 crd->crd_klen = priv->crypto_klen;
991 crd->crd_len = DEV_BSIZE /* XXX */;
992 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
993 crd->crd_next = NULL;
995 crd->crd_flags &= ~CRD_F_ENCRYPT;
997 KTR_LOG(dmcrypt_crypto_read_start, crp, bio->bio_buf, i,
1001 * Note: last argument is used to generate salt(?) and is
1002 * a 64 bit value, but the original code passed an
1003 * int. Changing it now will break pre-existing
1006 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1012 * STRATEGY READ PATH PART 3/3
1015 dmtc_crypto_cb_read_done(struct cryptop *crp)
1017 struct dmtc_helper *dmtc;
1018 struct bio *bio, *obio;
1021 if (crp->crp_etype == EAGAIN)
1022 return crypto_dispatch(crp);
1024 bio = (struct bio *)crp->crp_opaque;
1025 KKASSERT(bio != NULL);
1030 if (crp->crp_etype) {
1031 kprintf("dm_target_crypt: dmtc_crypto_cb_read_done "
1034 bio->bio_buf->b_error = crp->crp_etype;
1038 * On the last chunk of the decryption we do any required copybacks
1039 * and complete the I/O.
1041 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
1043 kprintf("dmtc_crypto_cb_read_done %p, n = %d\n", bio, n);
1046 KTR_LOG(dmcrypt_crypto_cb_read_done, crp, bio->bio_buf, n);
1050 * For the B_HASBOGUS case we didn't decrypt in place,
1051 * so we need to copy stuff back into the buf.
1053 * (disabled for now).
1055 dmtc = bio->bio_caller_info2.ptr;
1056 if (bio->bio_buf->b_error) {
1057 bio->bio_buf->b_flags |= B_ERROR;
1060 else if (bio->bio_buf->b_flags & B_HASBOGUS) {
1061 memcpy(bio->bio_buf->b_data, dmtc->data_buf,
1062 bio->bio_buf->b_bcount);
1065 mpipe_free(&dmtc->priv->read_mpipe, dmtc->free_addr);
1066 obio = pop_bio(bio);
1071 /* END OF STRATEGY READ SECTION */
1074 * STRATEGY WRITE PATH PART 1/3
1078 dmtc_crypto_write_retry(void *arg1, void *arg2)
1080 dm_target_crypt_config_t *priv = arg1;
1081 struct bio *bio = arg2;
1083 KTR_LOG(dmcrypt_crypto_write_retry, bio->bio_buf);
1085 dmtc_crypto_write_start(priv, bio);
1089 dmtc_crypto_write_start(dm_target_crypt_config_t *priv, struct bio *bio)
1091 struct dmtc_helper *dmtc;
1092 struct cryptodesc *crd;
1093 struct cryptop *crp;
1094 int i, bytes, sectors, sz;
1096 u_char *ptr, *space;
1099 * Use b_bcount for consistency
1101 bytes = bio->bio_buf->b_bcount;
1103 isector = bio->bio_offset / DEV_BSIZE; /* ivgen salt base? */
1104 sectors = bytes / DEV_BSIZE; /* Number of sectors */
1105 sz = sectors * (sizeof(*crp) + sizeof(*crd));
1108 * For writes and reads with bogus page don't decrypt in place.
1110 space = mpipe_alloc_callback(&priv->write_mpipe,
1111 dmtc_crypto_write_retry, priv, bio);
1115 dmtc = (struct dmtc_helper *)space;
1116 dmtc->free_addr = space;
1117 space += sizeof(struct dmtc_helper);
1118 memcpy(space + sz, bio->bio_buf->b_data, bytes);
1120 bio->bio_caller_info2.ptr = dmtc;
1121 bio->bio_buf->b_error = 0;
1123 dmtc->orig_buf = bio->bio_buf->b_data;
1124 dmtc->data_buf = space + sz;
1128 * Load crypto descriptors (crp/crd loop)
1132 bio->bio_caller_info3.value = sectors;
1135 kprintf("Write, bytes = %d (b_bcount), "
1136 "sectors = %d (bio = %p, b_cmd = %d)\n",
1137 bytes, sectors, bio, bio->bio_buf->b_cmd);
1139 for (i = 0; i < sectors; i++) {
1140 crp = (struct cryptop *)ptr;
1141 ptr += sizeof(*crp);
1142 crd = (struct cryptodesc *)ptr;
1143 ptr += sizeof (*crd);
1145 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
1147 crp->crp_sid = priv->crypto_sid;
1148 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1150 crp->crp_opaque = (void *)bio;
1152 crp->crp_callback = dmtc_crypto_cb_write_done;
1153 crp->crp_desc = crd;
1155 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1158 crd->crd_alg = priv->crypto_alg;
1160 crd->crd_key = (caddr_t)priv->crypto_key;
1161 crd->crd_klen = priv->crypto_klen;
1165 crd->crd_len = DEV_BSIZE /* XXX */;
1166 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1167 crd->crd_next = NULL;
1169 crd->crd_flags |= CRD_F_ENCRYPT;
1172 * Note: last argument is used to generate salt(?) and is
1173 * a 64 bit value, but the original code passed an
1174 * int. Changing it now will break pre-existing
1178 KTR_LOG(dmcrypt_crypto_write_start, crp, bio->bio_buf,
1181 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1187 * STRATEGY WRITE PATH PART 2/3
1190 dmtc_crypto_cb_write_done(struct cryptop *crp)
1192 struct dmtc_helper *dmtc;
1193 dm_target_crypt_config_t *priv;
1194 struct bio *bio, *obio;
1197 if (crp->crp_etype == EAGAIN)
1198 return crypto_dispatch(crp);
1200 bio = (struct bio *)crp->crp_opaque;
1201 KKASSERT(bio != NULL);
1206 if (crp->crp_etype != 0) {
1207 kprintf("dm_target_crypt: dmtc_crypto_cb_write_done "
1210 bio->bio_buf->b_error = crp->crp_etype;
1214 * On the last chunk of the encryption we issue the write
1216 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
1218 kprintf("dmtc_crypto_cb_write_done %p, n = %d\n", bio, n);
1221 KTR_LOG(dmcrypt_crypto_cb_write_done, crp, bio->bio_buf, n);
1224 dmtc = bio->bio_caller_info2.ptr;
1225 priv = (dm_target_crypt_config_t *)bio->bio_caller_info1.ptr;
1227 if (bio->bio_buf->b_error) {
1228 bio->bio_buf->b_flags |= B_ERROR;
1229 mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
1230 obio = pop_bio(bio);
1233 dmtc->orig_buf = bio->bio_buf->b_data;
1234 bio->bio_buf->b_data = dmtc->data_buf;
1235 bio->bio_done = dmtc_bio_write_done;
1236 vn_strategy(priv->pdev->pdev_vnode, bio);
1243 * STRATEGY WRITE PATH PART 3/3
1246 dmtc_bio_write_done(struct bio *bio)
1248 struct dmtc_helper *dmtc;
1251 dmtc = bio->bio_caller_info2.ptr;
1252 bio->bio_buf->b_data = dmtc->orig_buf;
1253 mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
1255 KTR_LOG(dmcrypt_bio_write_done, bio->bio_buf);
1257 obio = pop_bio(bio);
1260 /* END OF STRATEGY WRITE SECTION */
1266 extern int tsleep_crypto_dump;
1269 dm_target_crypt_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
1271 static struct dmtc_dump_helper dump_helper;
1272 dm_target_crypt_config_t *priv;
1274 static int first_call = 1;
1276 priv = table_en->target_config;
1280 dump_reactivate_cpus();
1283 /* Magically enable tsleep */
1284 tsleep_crypto_dump = 1;
1288 * 0 length means flush buffers and return
1291 if (priv->pdev->pdev_vnode->v_rdev == NULL) {
1292 tsleep_crypto_dump = 0;
1295 dev_ddump(priv->pdev->pdev_vnode->v_rdev,
1296 data, 0, offset, 0);
1297 tsleep_crypto_dump = 0;
1301 bzero(&dump_helper, sizeof(dump_helper));
1302 dump_helper.priv = priv;
1303 dump_helper.data = data;
1304 dump_helper.length = length;
1305 dump_helper.offset = offset +
1306 priv->block_offset * DEV_BSIZE;
1307 dump_helper.ident = &id;
1308 dmtc_crypto_dump_start(priv, &dump_helper);
1311 * Hackery to make stuff appear synchronous. The crypto callback will
1312 * set id to 1 and call wakeup on it. If the request completed
1313 * synchronously, id will be 1 and we won't bother to sleep. If not,
1314 * the crypto request will complete asynchronously and we sleep until
1318 tsleep(&dump_helper, 0, "cryptdump", 0);
1320 dump_helper.offset = dm_pdev_correct_dump_offset(priv->pdev,
1321 dump_helper.offset);
1323 dev_ddump(priv->pdev->pdev_vnode->v_rdev,
1324 dump_helper.space, 0, dump_helper.offset,
1325 dump_helper.length);
1327 tsleep_crypto_dump = 0;
1332 dmtc_crypto_dump_start(dm_target_crypt_config_t *priv, struct dmtc_dump_helper *dump_helper)
1334 struct cryptodesc *crd;
1335 struct cryptop *crp;
1336 int i, bytes, sectors;
1339 bytes = dump_helper->length;
1341 isector = dump_helper->offset / DEV_BSIZE; /* ivgen salt base? */
1342 sectors = bytes / DEV_BSIZE; /* Number of sectors */
1343 dump_helper->sectors = sectors;
1345 kprintf("Dump, bytes = %d, "
1346 "sectors = %d, LENGTH=%zu\n", bytes, sectors, dump_helper->length);
1348 KKASSERT(dump_helper->length <= 65536);
1350 memcpy(dump_helper->space, dump_helper->data, bytes);
1354 for (i = 0; i < sectors; i++) {
1355 crp = &dump_helper->crp[i];
1356 crd = &dump_helper->crd[i];
1358 crp->crp_buf = dump_helper->space + i * DEV_BSIZE;
1360 crp->crp_sid = priv->crypto_sid;
1361 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1363 crp->crp_opaque = (void *)dump_helper;
1365 crp->crp_callback = dmtc_crypto_cb_dump_done;
1366 crp->crp_desc = crd;
1368 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1371 crd->crd_alg = priv->crypto_alg;
1374 crd->crd_len = DEV_BSIZE /* XXX */;
1375 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1376 crd->crd_next = NULL;
1378 crd->crd_flags |= CRD_F_ENCRYPT;
1381 * Note: last argument is used to generate salt(?) and is
1382 * a 64 bit value, but the original code passed an
1383 * int. Changing it now will break pre-existing
1386 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1392 dmtc_crypto_cb_dump_done(struct cryptop *crp)
1394 struct dmtc_dump_helper *dump_helper;
1397 if (crp->crp_etype == EAGAIN)
1398 return crypto_dispatch(crp);
1400 dump_helper = (struct dmtc_dump_helper *)crp->crp_opaque;
1401 KKASSERT(dump_helper != NULL);
1403 if (crp->crp_etype != 0) {
1404 kprintf("dm_target_crypt: dmtc_crypto_cb_dump_done "
1407 return crp->crp_etype;
1411 * On the last chunk of the encryption we return control
1413 n = atomic_fetchadd_int(&dump_helper->sectors, -1);
1416 atomic_add_int(dump_helper->ident, 1);
1417 wakeup(dump_helper);
1424 dmtc_mod_handler(module_t mod, int type, void *unused)
1426 dm_target_t *dmt = NULL;
1431 if ((dmt = dm_target_lookup("crypt")) != NULL) {
1432 dm_target_unbusy(dmt);
1435 dmt = dm_target_alloc("crypt");
1436 dmt->version[0] = 1;
1437 dmt->version[1] = 6;
1438 dmt->version[2] = 0;
1439 strlcpy(dmt->name, "crypt", DM_MAX_TYPE_NAME);
1440 dmt->init = &dm_target_crypt_init;
1441 dmt->table = &dm_target_crypt_table;
1442 dmt->strategy = &dm_target_crypt_strategy;
1443 dmt->destroy = &dm_target_crypt_destroy;
1444 dmt->upcall = &dm_target_crypt_upcall;
1445 dmt->dump = &dm_target_crypt_dump;
1447 err = dm_target_insert(dmt);
1449 kprintf("dm_target_crypt: Successfully initialized\n");
1453 err = dm_target_rem("crypt");
1455 kprintf("dm_target_crypt: unloaded\n");
1466 DM_TARGET_MODULE(dm_target_crypt, dmtc_mod_handler);