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/endian.h>
41 #include <sys/globaldata.h>
42 #include <sys/kerneldump.h>
43 #include <sys/malloc.h>
44 #include <sys/mpipe.h>
46 #include <sys/mutex2.h>
47 #include <crypto/sha1.h>
48 #include <crypto/sha2/sha2.h>
49 #include <opencrypto/cryptodev.h>
50 #include <opencrypto/rmd160.h>
51 #include <machine/cpufunc.h>
52 #include <cpu/atomic.h>
56 #include <dev/disk/dm/dm.h>
57 MALLOC_DEFINE(M_DMCRYPT, "dm_crypt", "Device Mapper Target Crypt");
59 KTR_INFO_MASTER(dmcrypt);
61 #if !defined(KTR_DMCRYPT)
62 #define KTR_DMCRYPT KTR_ALL
65 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_dispatch, 0,
66 "crypto_dispatch(%p)", struct cryptop *crp);
67 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypt_strategy, 0,
68 "crypt_strategy(b_cmd = %d, bp = %p)", int cmd, struct buf *bp);
69 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_write_start, 1,
70 "crypto_write_start(crp = %p, bp = %p, sector = %d/%d)",
71 struct cryptop *crp, struct buf *bp, int i, int sectors);
72 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_cb_write_done, 1,
73 "crypto_cb_write_done(crp = %p, bp = %p, n = %d)",
74 struct cryptop *crp, struct buf *bp, int n);
75 KTR_INFO(KTR_DMCRYPT, dmcrypt, bio_write_done, 1,
76 "bio_write_done(bp = %p)", struct buf *bp);
77 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_write_retry, 1,
78 "crypto_write_retry(crp = %p)", struct buf *bp);
79 KTR_INFO(KTR_DMCRYPT, dmcrypt, bio_read_done, 2,
80 "bio_read_done(bp = %p)", struct buf *bp);
81 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_read_start, 2,
82 "crypto_read_start(crp = %p, bp = %p, sector = %d/%d)",
83 struct cryptop *crp, struct buf *bp, int i, int sectors);
84 KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_cb_read_done, 2,
85 "crypto_cb_read_done(crp = %p, bp = %p, n = %d)",
86 struct cryptop *crp, struct buf *bp, int n);
88 struct target_crypt_config;
90 typedef void dispatch_t(void *);
91 typedef void ivgen_t(struct target_crypt_config *, u_int8_t *, size_t, off_t,
94 typedef int ivgen_ctor_t(struct target_crypt_config *, char *, void **);
95 typedef int ivgen_dtor_t(struct target_crypt_config *, void *);
104 struct essiv_ivgen_priv {
105 struct cryptoini crypto_session;
106 struct objcache *crp_crd_cache;
107 u_int64_t crypto_sid;
109 u_int8_t crypto_keyhash[SHA512_DIGEST_LENGTH];
112 typedef struct target_crypt_config {
118 u_int8_t crypto_key[512>>3];
120 u_int64_t crypto_sid;
121 u_int64_t block_offset;
123 SHA512_CTX essivsha512_ctx;
125 struct cryptoini crypto_session;
127 struct iv_generator *ivgen;
130 struct malloc_pipe read_mpipe;
131 struct malloc_pipe write_mpipe;
132 } dm_target_crypt_config_t;
135 dm_target_crypt_config_t *priv;
141 struct dmtc_dump_helper {
142 dm_target_crypt_config_t *priv;
150 struct cryptodesc crd[128];
151 struct cryptop crp[128];
155 #define DMTC_BUF_SIZE_WRITE \
156 MAXPHYS + sizeof(struct dmtc_helper) + \
157 MAXPHYS/DEV_BSIZE*(sizeof(struct cryptop) + sizeof(struct cryptodesc))
158 #define DMTC_BUF_SIZE_READ \
159 sizeof(struct dmtc_helper) + \
160 MAXPHYS/DEV_BSIZE*(sizeof(struct cryptop) + sizeof(struct cryptodesc))
162 static void dmtc_crypto_dispatch(void *arg);
163 static void dmtc_crypto_dump_start(dm_target_crypt_config_t *priv,
164 struct dmtc_dump_helper *dump_helper);
165 static void dmtc_crypto_read_start(dm_target_crypt_config_t *priv,
167 static void dmtc_crypto_write_start(dm_target_crypt_config_t *priv,
169 static void dmtc_bio_read_done(struct bio *bio);
170 static void dmtc_bio_write_done(struct bio *bio);
171 static int dmtc_crypto_cb_dump_done(struct cryptop *crp);
172 static int dmtc_crypto_cb_read_done(struct cryptop *crp);
173 static int dmtc_crypto_cb_write_done(struct cryptop *crp);
175 static ivgen_ctor_t essiv_ivgen_ctor;
176 static ivgen_dtor_t essiv_ivgen_dtor;
177 static ivgen_t essiv_ivgen;
178 static ivgen_t plain_ivgen;
179 static ivgen_t plain64_ivgen;
181 static struct iv_generator ivgens[] = {
182 { .name = "essiv", .ctor = essiv_ivgen_ctor, .dtor = essiv_ivgen_dtor,
183 .gen_iv = essiv_ivgen },
184 { .name = "plain", .ctor = NULL, .dtor = NULL, .gen_iv = plain_ivgen },
185 { .name = "plain64", .ctor = NULL, .dtor = NULL, .gen_iv = plain64_ivgen },
186 { NULL, NULL, NULL, NULL }
189 struct objcache_malloc_args essiv_ivgen_malloc_args = {
190 2*sizeof(void *) + (sizeof(struct cryptodesc) +
191 sizeof(struct cryptop)), M_DMCRYPT };
194 dmtc_init_mpipe(struct target_crypt_config *priv)
198 nmax = (physmem*2/1000*PAGE_SIZE)/(DMTC_BUF_SIZE_WRITE + DMTC_BUF_SIZE_READ) + 1;
203 kprintf("dm_target_crypt: Setting min/max mpipe buffers: %d/%d\n", 2, nmax);
205 mpipe_init(&priv->write_mpipe, M_DMCRYPT, DMTC_BUF_SIZE_WRITE,
206 2, nmax, MPF_NOZERO | MPF_CALLBACK, NULL, NULL, NULL);
207 mpipe_init(&priv->read_mpipe, M_DMCRYPT, DMTC_BUF_SIZE_READ,
208 2, nmax, MPF_NOZERO | MPF_CALLBACK, NULL, NULL, NULL);
212 dmtc_destroy_mpipe(struct target_crypt_config *priv)
214 mpipe_done(&priv->write_mpipe);
215 mpipe_done(&priv->read_mpipe);
219 * Overwrite private information (in buf) to avoid leaking it
222 dmtc_crypto_clear(void *buf, size_t len)
224 memset(buf, 0xFF, len);
229 * ESSIV IV Generator Routines
232 essiv_ivgen_ctor(struct target_crypt_config *priv, char *iv_hash, void **p_ivpriv)
234 struct essiv_ivgen_priv *ivpriv;
235 u_int8_t crypto_keyhash[SHA512_DIGEST_LENGTH];
236 unsigned int klen, hashlen;
239 klen = (priv->crypto_klen >> 3);
244 if (!strcmp(iv_hash, "sha1")) {
247 hashlen = SHA1_RESULTLEN;
249 SHA1Update(&ctx, priv->crypto_key, klen);
250 SHA1Final(crypto_keyhash, &ctx);
251 } else if (!strcmp(iv_hash, "sha256")) {
254 hashlen = SHA256_DIGEST_LENGTH;
256 SHA256_Update(&ctx, priv->crypto_key, klen);
257 SHA256_Final(crypto_keyhash, &ctx);
258 } else if (!strcmp(iv_hash, "sha384")) {
261 hashlen = SHA384_DIGEST_LENGTH;
263 SHA384_Update(&ctx, priv->crypto_key, klen);
264 SHA384_Final(crypto_keyhash, &ctx);
265 } else if (!strcmp(iv_hash, "sha512")) {
268 hashlen = SHA512_DIGEST_LENGTH;
270 SHA512_Update(&ctx, priv->crypto_key, klen);
271 SHA512_Final(crypto_keyhash, &ctx);
272 } else if (!strcmp(iv_hash, "md5")) {
275 hashlen = MD5_DIGEST_LENGTH;
277 MD5Update(&ctx, priv->crypto_key, klen);
278 MD5Final(crypto_keyhash, &ctx);
279 } else if (!strcmp(iv_hash, "rmd160") ||
280 !strcmp(iv_hash, "ripemd160")) {
285 RMD160Update(&ctx, priv->crypto_key, klen);
286 RMD160Final(crypto_keyhash, &ctx);
291 /* Convert hashlen to bits */
294 ivpriv = kmalloc(sizeof(struct essiv_ivgen_priv), M_DMCRYPT,
296 memcpy(ivpriv->crypto_keyhash, crypto_keyhash, sizeof(crypto_keyhash));
297 ivpriv->keyhash_len = sizeof(crypto_keyhash);
298 dmtc_crypto_clear(crypto_keyhash, sizeof(crypto_keyhash));
300 ivpriv->crypto_session.cri_alg = priv->crypto_alg;
301 ivpriv->crypto_session.cri_key = (u_int8_t *)ivpriv->crypto_keyhash;
302 ivpriv->crypto_session.cri_klen = hashlen;
303 ivpriv->crypto_session.cri_mlen = 0;
304 ivpriv->crypto_session.cri_next = NULL;
307 * XXX: in principle we also need to check if the block size of the
308 * cipher is a valid iv size for the block cipher.
311 error = crypto_newsession(&ivpriv->crypto_sid,
312 &ivpriv->crypto_session,
313 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
315 kprintf("dm_target_crypt: Error during crypto_newsession "
316 "for essiv_ivgen, error = %d\n",
318 dmtc_crypto_clear(ivpriv->crypto_keyhash, ivpriv->keyhash_len);
319 kfree(ivpriv, M_DMCRYPT);
323 ivpriv->crp_crd_cache = objcache_create(
324 "dmcrypt-essiv-cache", 0, 0,
326 objcache_malloc_alloc,
327 objcache_malloc_free,
328 &essiv_ivgen_malloc_args );
335 essiv_ivgen_dtor(struct target_crypt_config *priv, void *arg)
337 struct essiv_ivgen_priv *ivpriv;
339 ivpriv = (struct essiv_ivgen_priv *)arg;
340 KKASSERT(ivpriv != NULL);
342 crypto_freesession(ivpriv->crypto_sid);
344 objcache_destroy(ivpriv->crp_crd_cache);
346 dmtc_crypto_clear(ivpriv->crypto_keyhash, ivpriv->keyhash_len);
347 kfree(ivpriv, M_DMCRYPT);
353 essiv_ivgen_done(struct cryptop *crp)
355 struct essiv_ivgen_priv *ivpriv;
360 if (crp->crp_etype == EAGAIN)
361 return crypto_dispatch(crp);
363 if (crp->crp_etype != 0) {
364 kprintf("dm_target_crypt: essiv_ivgen_done, "
365 "crp->crp_etype = %d\n", crp->crp_etype);
368 free_addr = crp->crp_opaque;
370 * In-memory structure is:
371 * | ivpriv | opaque | crp | crd |
372 * | (void *) | (void *) | (cryptop) | (cryptodesc) |
374 ivpriv = *((struct essiv_ivgen_priv **)crp->crp_opaque);
375 crp->crp_opaque += sizeof(void *);
376 opaque = *((void **)crp->crp_opaque);
378 objcache_put(ivpriv->crp_crd_cache, free_addr);
379 dmtc_crypto_dispatch(opaque);
384 essiv_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
385 size_t iv_len, off_t sector, void *opaque)
387 struct essiv_ivgen_priv *ivpriv;
388 struct cryptodesc *crd;
390 caddr_t space, alloc_addr;
393 ivpriv = priv->ivgen_priv;
394 KKASSERT(ivpriv != NULL);
397 * In-memory structure is:
398 * | ivpriv | opaque | crp | crd |
399 * | (void *) | (void *) | (cryptop) | (cryptodesc) |
401 alloc_addr = space = objcache_get(ivpriv->crp_crd_cache, M_WAITOK);
402 *((struct essiv_ivgen_priv **)space) = ivpriv;
403 space += sizeof(void *);
404 *((void **)space) = opaque;
405 space += sizeof(void *);
406 crp = (struct cryptop *)space;
407 space += sizeof(struct cryptop);
408 crd = (struct cryptodesc *)space;
411 bzero(crd, sizeof(struct cryptodesc));
412 bzero(crp, sizeof(struct cryptop));
413 *((off_t *)iv) = htole64(sector + priv->iv_offset);
414 crp->crp_buf = (caddr_t)iv;
416 crp->crp_sid = ivpriv->crypto_sid;
417 crp->crp_ilen = crp->crp_olen = iv_len;
419 crp->crp_opaque = alloc_addr;
421 crp->crp_callback = essiv_ivgen_done;
425 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL | CRYPTO_F_BATCH;
427 crd->crd_alg = priv->crypto_alg;
429 crd->crd_key = (caddr_t)priv->crypto_keyhash;
430 crd->crd_klen = priv->crypto_klen;
433 bzero(crd->crd_iv, sizeof(crd->crd_iv));
436 crd->crd_len = iv_len;
437 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
438 crd->crd_flags |= CRD_F_ENCRYPT;
439 crd->crd_next = NULL;
441 error = crypto_dispatch(crp);
443 kprintf("dm_target_crypt: essiv_ivgen, error = %d\n", error);
448 plain_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
449 size_t iv_len, off_t sector, void *opaque)
452 *((uint32_t *)iv) = htole32((uint32_t)(sector + priv->iv_offset));
453 dmtc_crypto_dispatch(opaque);
457 plain64_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
458 size_t iv_len, off_t sector, void *opaque)
461 *((uint64_t *)iv) = htole64((uint64_t)(sector + priv->iv_offset));
462 dmtc_crypto_dispatch(opaque);
467 geli_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
468 size_t iv_len, off_t sector, void *opaque)
472 u_int8_t md[SHA512_DIGEST_LENGTH]; /* Max. Digest Size */
474 memcpy(&ctx512, &priv->essivsha512_ctx, sizeof(SHA512_CTX));
475 SHA512_Update(&ctx512, (u_int8_t*)§or, sizeof(off_t));
476 SHA512_Final(md, &ctx512);
478 memcpy(iv, md, iv_len);
479 dmtc_crypto_dispatch(opaque);
484 * Init function called from dm_table_load_ioctl.
485 * cryptsetup actually passes us this:
486 * aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8
489 hex2key(char *hex, size_t key_len, u_int8_t *key)
495 for (key_idx = 0; key_idx < key_len; ++key_idx) {
498 key[key_idx] = (u_int8_t)strtoul(hex_buf, NULL, 16);
507 dm_target_crypt_init(dm_table_entry_t *table_en, int argc, char **argv)
509 dm_target_crypt_config_t *priv;
511 char *crypto_alg, *crypto_mode, *iv_mode, *iv_opt, *key, *dev;
514 uint64_t iv_offset, block_offset;
517 kprintf("dm_target_crypt: not enough arguments, "
523 for (i = 0; i < argc; i++) {
524 len += strlen(argv[i]);
527 /* len is strlen() of input string +1 */
528 status_str = kmalloc(len, M_DMCRYPT, M_WAITOK);
530 crypto_alg = strsep(&argv[0], "-");
531 crypto_mode = strsep(&argv[0], "-");
532 iv_opt = strsep(&argv[0], "-");
533 iv_mode = strsep(&iv_opt, ":");
535 iv_offset = strtouq(argv[2], NULL, 0);
537 block_offset = strtouq(argv[4], NULL, 0);
538 /* bits / 8 = bytes, 1 byte = 2 hexa chars, so << 2 */
539 klen = strlen(key) << 2;
542 kprintf("dm_target_crypt - new: dev=%s, crypto_alg=%s, crypto_mode=%s, "
543 "iv_mode=%s, iv_opt=%s, key=%s, iv_offset=%ju, "
544 "block_offset=%ju\n",
545 dev, crypto_alg, crypto_mode, iv_mode, iv_opt, key, iv_offset,
549 priv = kmalloc(sizeof(dm_target_crypt_config_t), M_DMCRYPT, M_WAITOK);
551 /* Insert dmp to global pdev list */
552 if ((priv->pdev = dm_pdev_insert(dev)) == NULL) {
553 kprintf("dm_target_crypt: dm_pdev_insert failed\n");
554 kfree(status_str, M_DMCRYPT);
559 * This code checks for valid combinations of algorithm and mode.
560 * Currently supported options are:
567 if ((strcmp(crypto_mode, "cbc") != 0) &&
568 !((strcmp(crypto_mode, "xts") == 0) &&
569 ((strcmp(crypto_alg, "aes") == 0) ||
570 (strcmp(crypto_alg, "twofish") == 0) ||
571 (strcmp(crypto_alg, "serpent") == 0))))
573 kprintf("dm_target_crypt: only support 'cbc' chaining mode,"
574 " aes-xts, twofish-xts and serpent-xts, "
575 "invalid mode '%s-%s'\n",
576 crypto_alg, crypto_mode);
580 if (!strcmp(crypto_alg, "aes")) {
581 if (!strcmp(crypto_mode, "xts")) {
582 priv->crypto_alg = CRYPTO_AES_XTS;
583 if (klen != 256 && klen != 512)
585 } else if (!strcmp(crypto_mode, "cbc")) {
586 priv->crypto_alg = CRYPTO_AES_CBC;
587 if (klen != 128 && klen != 192 && klen != 256)
592 priv->crypto_klen = klen;
593 } else if (!strcmp(crypto_alg, "twofish")) {
594 if (!strcmp(crypto_mode, "xts")) {
595 priv->crypto_alg = CRYPTO_TWOFISH_XTS;
596 if (klen != 256 && klen != 512)
598 } else if (!strcmp(crypto_mode, "cbc")) {
599 priv->crypto_alg = CRYPTO_TWOFISH_CBC;
600 if (klen != 128 && klen != 192 && klen != 256)
605 priv->crypto_klen = klen;
606 } else if (!strcmp(crypto_alg, "serpent")) {
607 if (!strcmp(crypto_mode, "xts")) {
608 priv->crypto_alg = CRYPTO_SERPENT_XTS;
609 if (klen != 256 && klen != 512)
611 } else if (!strcmp(crypto_mode, "cbc")) {
612 priv->crypto_alg = CRYPTO_SERPENT_CBC;
613 if (klen != 128 && klen != 192 && klen != 256)
618 priv->crypto_klen = klen;
619 } else if (!strcmp(crypto_alg, "blowfish")) {
620 priv->crypto_alg = CRYPTO_BLF_CBC;
621 if (klen < 128 || klen > 448 || (klen % 8) != 0)
623 priv->crypto_klen = klen;
624 } else if (!strcmp(crypto_alg, "3des") ||
625 !strncmp(crypto_alg, "des3", 4)) {
626 priv->crypto_alg = CRYPTO_3DES_CBC;
629 priv->crypto_klen = 168;
630 } else if (!strcmp(crypto_alg, "camellia")) {
631 priv->crypto_alg = CRYPTO_CAMELLIA_CBC;
632 if (klen != 128 && klen != 192 && klen != 256)
634 priv->crypto_klen = klen;
635 } else if (!strcmp(crypto_alg, "skipjack")) {
636 priv->crypto_alg = CRYPTO_SKIPJACK_CBC;
639 priv->crypto_klen = 80;
640 } else if (!strcmp(crypto_alg, "cast5")) {
641 priv->crypto_alg = CRYPTO_CAST_CBC;
644 priv->crypto_klen = 128;
645 } else if (!strcmp(crypto_alg, "null")) {
646 priv->crypto_alg = CRYPTO_NULL_CBC;
649 priv->crypto_klen = 128;
651 kprintf("dm_target_crypt: Unsupported crypto algorithm: %s\n",
656 /* Save length of param string */
657 priv->params_len = len;
658 priv->block_offset = block_offset;
659 priv->iv_offset = iv_offset - block_offset;
661 dm_table_add_deps(table_en, priv->pdev);
663 dm_table_init_target(table_en, DM_CRYPTO_DEV, priv);
665 error = hex2key(key, priv->crypto_klen >> 3,
666 (u_int8_t *)priv->crypto_key);
669 kprintf("dm_target_crypt: hex2key failed, "
670 "invalid key format\n");
675 for(i = 0; ivgens[i].name != NULL; i++) {
676 if (!strcmp(iv_mode, ivgens[i].name))
680 if (ivgens[i].name == NULL) {
681 kprintf("dm_target_crypt: iv_mode='%s' unsupported\n",
686 /* Call our ivgen constructor */
687 if (ivgens[i].ctor != NULL) {
688 error = ivgens[i].ctor(priv, iv_opt,
691 kprintf("dm_target_crypt: ctor for '%s' failed\n",
697 priv->ivgen = &ivgens[i];
699 priv->crypto_session.cri_alg = priv->crypto_alg;
700 priv->crypto_session.cri_key = (u_int8_t *)priv->crypto_key;
701 priv->crypto_session.cri_klen = priv->crypto_klen;
702 priv->crypto_session.cri_mlen = 0;
703 priv->crypto_session.cri_next = NULL;
705 error = crypto_newsession(&priv->crypto_sid,
706 &priv->crypto_session,
707 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
709 kprintf("dm_target_crypt: Error during crypto_newsession, "
715 memset(key, '0', strlen(key));
717 ksprintf(status_str, "%s-%s-%s:%s %s %ju %s %ju",
718 crypto_alg, crypto_mode, iv_mode, iv_opt,
719 key, iv_offset, dev, block_offset);
721 ksprintf(status_str, "%s-%s-%s %s %ju %s %ju",
722 crypto_alg, crypto_mode, iv_mode,
723 key, iv_offset, dev, block_offset);
725 priv->status_str = status_str;
727 /* Initialize mpipes */
728 dmtc_init_mpipe(priv);
733 kprintf("dm_target_crypt: ENOTSUP\n");
734 kfree(status_str, M_DMCRYPT);
738 /* Table routine called to get params string. */
740 dm_target_crypt_table(void *target_config)
742 dm_target_crypt_config_t *priv;
745 priv = target_config;
747 params = dm_alloc_string(DM_MAX_PARAMS_SIZE);
749 ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s",
756 dm_target_crypt_destroy(dm_table_entry_t *table_en)
758 dm_target_crypt_config_t *priv;
761 * Disconnect the crypt config before unbusying the target.
763 priv = table_en->target_config;
766 dm_pdev_decr(priv->pdev);
769 * Clean up the crypt config
771 * Overwrite the private information before freeing memory to
774 if (priv->status_str) {
775 dmtc_crypto_clear(priv->status_str, strlen(priv->status_str));
776 kfree(priv->status_str, M_DMCRYPT);
777 crypto_freesession(priv->crypto_sid);
780 if ((priv->ivgen) && (priv->ivgen->dtor != NULL)) {
781 priv->ivgen->dtor(priv, priv->ivgen_priv);
785 dmtc_destroy_mpipe(priv);
787 dmtc_crypto_clear(priv, sizeof(dm_target_crypt_config_t));
788 kfree(priv, M_DMCRYPT);
793 /************************************************************************
794 * STRATEGY SUPPORT FUNCTIONS *
795 ************************************************************************
797 * READ PATH: doio -> bio_read_done -> crypto_work -> crypto_cb_read_done
798 * WRITE PATH: crypto_work -> crypto_cb_write_done -> doio -> bio_write_done
802 * Wrapper around crypto_dispatch() to match dispatch_t type
805 dmtc_crypto_dispatch(void *arg)
809 crp = (struct cryptop *)arg;
810 KKASSERT(crp != NULL);
811 KTR_LOG(dmcrypt_crypto_dispatch, crp);
812 crypto_dispatch(crp);
816 * Start IO operation, called from dmstrategy routine.
819 dm_target_crypt_strategy(dm_table_entry_t *table_en, struct buf *bp)
823 dm_target_crypt_config_t *priv;
824 priv = table_en->target_config;
826 /* Get rid of stuff we can't really handle */
827 if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
828 if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
829 kprintf("dm_target_crypt_strategy: can't really "
830 "handle bp->b_bcount = %d\n",
832 bp->b_error = EINVAL;
833 bp->b_flags |= B_ERROR | B_INVAL;
834 biodone(&bp->b_bio1);
839 KTR_LOG(dmcrypt_crypt_strategy, bp->b_cmd, bp);
843 bio = push_bio(&bp->b_bio1);
844 bio->bio_offset = bp->b_bio1.bio_offset +
845 priv->block_offset * DEV_BSIZE;
846 bio->bio_caller_info1.ptr = priv;
847 bio->bio_done = dmtc_bio_read_done;
848 vn_strategy(priv->pdev->pdev_vnode, bio);
851 bio = push_bio(&bp->b_bio1);
852 bio->bio_offset = bp->b_bio1.bio_offset +
853 priv->block_offset * DEV_BSIZE;
854 bio->bio_caller_info1.ptr = priv;
855 dmtc_crypto_write_start(priv, bio);
858 vn_strategy(priv->pdev->pdev_vnode, &bp->b_bio1);
865 * STRATEGY READ PATH PART 1/3 (after read BIO completes)
868 dmtc_bio_read_done(struct bio *bio)
872 dm_target_crypt_config_t *priv;
874 KTR_LOG(dmcrypt_bio_read_done, bio->bio_buf);
877 * If a read error occurs we shortcut the operation, otherwise
880 if (bio->bio_buf->b_flags & B_ERROR) {
884 priv = bio->bio_caller_info1.ptr;
885 dmtc_crypto_read_start(priv, bio);
890 * STRATEGY READ PATH PART 2/3
893 dmtc_crypto_read_retry(void *arg1, void *arg2)
895 dm_target_crypt_config_t *priv = arg1;
896 struct bio *bio = arg2;
898 dmtc_crypto_read_start(priv, bio);
902 dmtc_crypto_read_start(dm_target_crypt_config_t *priv, struct bio *bio)
904 struct dmtc_helper *dmtc;
905 struct cryptodesc *crd;
907 int i, bytes, sectors, sz;
912 * Note: b_resid no good after read I/O, it will be 0, use
915 bytes = bio->bio_buf->b_bcount;
916 isector = bio->bio_offset / DEV_BSIZE; /* ivgen salt base? */
917 sectors = bytes / DEV_BSIZE; /* Number of sectors */
918 sz = sectors * (sizeof(*crp) + sizeof(*crd));
921 * For reads with bogus page we can't decrypt in place as stuff
922 * can get ripped out from under us.
924 * XXX actually it looks like we can, and in any case the initial
925 * read already completed and threw crypted data into the buffer
926 * cache buffer. Disable for now.
928 space = mpipe_alloc_callback(&priv->read_mpipe,
929 dmtc_crypto_read_retry, priv, bio);
933 dmtc = (struct dmtc_helper *)space;
934 dmtc->free_addr = space;
935 space += sizeof(struct dmtc_helper);
936 dmtc->orig_buf = NULL;
937 dmtc->data_buf = bio->bio_buf->b_data;
939 bio->bio_caller_info2.ptr = dmtc;
940 bio->bio_buf->b_error = 0;
943 * Load crypto descriptors (crp/crd loop)
947 bio->bio_caller_info3.value = sectors;
950 kprintf("Read, bytes = %d (b_bcount), "
951 "sectors = %d (bio = %p, b_cmd = %d)\n",
952 bytes, sectors, bio, bio->bio_buf->b_cmd);
954 for (i = 0; i < sectors; i++) {
955 crp = (struct cryptop *)ptr;
957 crd = (struct cryptodesc *)ptr;
958 ptr += sizeof (*crd);
960 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
962 crp->crp_sid = priv->crypto_sid;
963 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
965 crp->crp_opaque = (void *)bio;
967 crp->crp_callback = dmtc_crypto_cb_read_done;
970 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
973 crd->crd_alg = priv->crypto_alg;
975 crd->crd_key = (caddr_t)priv->crypto_key;
976 crd->crd_klen = priv->crypto_klen;
980 crd->crd_len = DEV_BSIZE /* XXX */;
981 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
982 crd->crd_next = NULL;
984 crd->crd_flags &= ~CRD_F_ENCRYPT;
986 KTR_LOG(dmcrypt_crypto_read_start, crp, bio->bio_buf, i,
990 * Note: last argument is used to generate salt(?) and is
991 * a 64 bit value, but the original code passed an
992 * int. Changing it now will break pre-existing
995 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1001 * STRATEGY READ PATH PART 3/3
1004 dmtc_crypto_cb_read_done(struct cryptop *crp)
1006 struct dmtc_helper *dmtc;
1007 struct bio *bio, *obio;
1010 if (crp->crp_etype == EAGAIN)
1011 return crypto_dispatch(crp);
1013 bio = (struct bio *)crp->crp_opaque;
1014 KKASSERT(bio != NULL);
1019 if (crp->crp_etype) {
1020 kprintf("dm_target_crypt: dmtc_crypto_cb_read_done "
1023 bio->bio_buf->b_error = crp->crp_etype;
1027 * On the last chunk of the decryption we do any required copybacks
1028 * and complete the I/O.
1030 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
1032 kprintf("dmtc_crypto_cb_read_done %p, n = %d\n", bio, n);
1035 KTR_LOG(dmcrypt_crypto_cb_read_done, crp, bio->bio_buf, n);
1039 * For the B_HASBOGUS case we didn't decrypt in place,
1040 * so we need to copy stuff back into the buf.
1042 * (disabled for now).
1044 dmtc = bio->bio_caller_info2.ptr;
1045 if (bio->bio_buf->b_error) {
1046 bio->bio_buf->b_flags |= B_ERROR;
1049 else if (bio->bio_buf->b_flags & B_HASBOGUS) {
1050 memcpy(bio->bio_buf->b_data, dmtc->data_buf,
1051 bio->bio_buf->b_bcount);
1054 mpipe_free(&dmtc->priv->read_mpipe, dmtc->free_addr);
1055 obio = pop_bio(bio);
1060 /* END OF STRATEGY READ SECTION */
1063 * STRATEGY WRITE PATH PART 1/3
1067 dmtc_crypto_write_retry(void *arg1, void *arg2)
1069 dm_target_crypt_config_t *priv = arg1;
1070 struct bio *bio = arg2;
1072 KTR_LOG(dmcrypt_crypto_write_retry, bio->bio_buf);
1074 dmtc_crypto_write_start(priv, bio);
1078 dmtc_crypto_write_start(dm_target_crypt_config_t *priv, struct bio *bio)
1080 struct dmtc_helper *dmtc;
1081 struct cryptodesc *crd;
1082 struct cryptop *crp;
1083 int i, bytes, sectors, sz;
1085 u_char *ptr, *space;
1088 * Use b_bcount for consistency
1090 bytes = bio->bio_buf->b_bcount;
1092 isector = bio->bio_offset / DEV_BSIZE; /* ivgen salt base? */
1093 sectors = bytes / DEV_BSIZE; /* Number of sectors */
1094 sz = sectors * (sizeof(*crp) + sizeof(*crd));
1097 * For writes and reads with bogus page don't decrypt in place.
1099 space = mpipe_alloc_callback(&priv->write_mpipe,
1100 dmtc_crypto_write_retry, priv, bio);
1104 dmtc = (struct dmtc_helper *)space;
1105 dmtc->free_addr = space;
1106 space += sizeof(struct dmtc_helper);
1107 memcpy(space + sz, bio->bio_buf->b_data, bytes);
1109 bio->bio_caller_info2.ptr = dmtc;
1110 bio->bio_buf->b_error = 0;
1112 dmtc->orig_buf = bio->bio_buf->b_data;
1113 dmtc->data_buf = space + sz;
1117 * Load crypto descriptors (crp/crd loop)
1121 bio->bio_caller_info3.value = sectors;
1124 kprintf("Write, bytes = %d (b_bcount), "
1125 "sectors = %d (bio = %p, b_cmd = %d)\n",
1126 bytes, sectors, bio, bio->bio_buf->b_cmd);
1128 for (i = 0; i < sectors; i++) {
1129 crp = (struct cryptop *)ptr;
1130 ptr += sizeof(*crp);
1131 crd = (struct cryptodesc *)ptr;
1132 ptr += sizeof (*crd);
1134 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
1136 crp->crp_sid = priv->crypto_sid;
1137 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1139 crp->crp_opaque = (void *)bio;
1141 crp->crp_callback = dmtc_crypto_cb_write_done;
1142 crp->crp_desc = crd;
1144 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1147 crd->crd_alg = priv->crypto_alg;
1149 crd->crd_key = (caddr_t)priv->crypto_key;
1150 crd->crd_klen = priv->crypto_klen;
1154 crd->crd_len = DEV_BSIZE /* XXX */;
1155 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1156 crd->crd_next = NULL;
1158 crd->crd_flags |= CRD_F_ENCRYPT;
1161 * Note: last argument is used to generate salt(?) and is
1162 * a 64 bit value, but the original code passed an
1163 * int. Changing it now will break pre-existing
1167 KTR_LOG(dmcrypt_crypto_write_start, crp, bio->bio_buf,
1170 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1176 * STRATEGY WRITE PATH PART 2/3
1179 dmtc_crypto_cb_write_done(struct cryptop *crp)
1181 struct dmtc_helper *dmtc;
1182 dm_target_crypt_config_t *priv;
1183 struct bio *bio, *obio;
1186 if (crp->crp_etype == EAGAIN)
1187 return crypto_dispatch(crp);
1189 bio = (struct bio *)crp->crp_opaque;
1190 KKASSERT(bio != NULL);
1195 if (crp->crp_etype != 0) {
1196 kprintf("dm_target_crypt: dmtc_crypto_cb_write_done "
1199 bio->bio_buf->b_error = crp->crp_etype;
1203 * On the last chunk of the encryption we issue the write
1205 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
1207 kprintf("dmtc_crypto_cb_write_done %p, n = %d\n", bio, n);
1210 KTR_LOG(dmcrypt_crypto_cb_write_done, crp, bio->bio_buf, n);
1213 dmtc = bio->bio_caller_info2.ptr;
1214 priv = (dm_target_crypt_config_t *)bio->bio_caller_info1.ptr;
1216 if (bio->bio_buf->b_error) {
1217 bio->bio_buf->b_flags |= B_ERROR;
1218 mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
1219 obio = pop_bio(bio);
1222 dmtc->orig_buf = bio->bio_buf->b_data;
1223 bio->bio_buf->b_data = dmtc->data_buf;
1224 bio->bio_done = dmtc_bio_write_done;
1225 vn_strategy(priv->pdev->pdev_vnode, bio);
1232 * STRATEGY WRITE PATH PART 3/3
1235 dmtc_bio_write_done(struct bio *bio)
1237 struct dmtc_helper *dmtc;
1240 dmtc = bio->bio_caller_info2.ptr;
1241 bio->bio_buf->b_data = dmtc->orig_buf;
1242 mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
1244 KTR_LOG(dmcrypt_bio_write_done, bio->bio_buf);
1246 obio = pop_bio(bio);
1249 /* END OF STRATEGY WRITE SECTION */
1255 extern int tsleep_crypto_dump;
1258 dm_target_crypt_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
1260 static struct dmtc_dump_helper dump_helper;
1261 dm_target_crypt_config_t *priv;
1263 static int first_call = 1;
1265 priv = table_en->target_config;
1269 dump_reactivate_cpus();
1272 /* Magically enable tsleep */
1273 tsleep_crypto_dump = 1;
1277 * 0 length means flush buffers and return
1280 if (priv->pdev->pdev_vnode->v_rdev == NULL) {
1281 tsleep_crypto_dump = 0;
1284 dev_ddump(priv->pdev->pdev_vnode->v_rdev,
1285 data, 0, offset, 0);
1286 tsleep_crypto_dump = 0;
1290 bzero(&dump_helper, sizeof(dump_helper));
1291 dump_helper.priv = priv;
1292 dump_helper.data = data;
1293 dump_helper.length = length;
1294 dump_helper.offset = offset +
1295 priv->block_offset * DEV_BSIZE;
1296 dump_helper.ident = &id;
1297 dmtc_crypto_dump_start(priv, &dump_helper);
1300 * Hackery to make stuff appear synchronous. The crypto callback will
1301 * set id to 1 and call wakeup on it. If the request completed
1302 * synchronously, id will be 1 and we won't bother to sleep. If not,
1303 * the crypto request will complete asynchronously and we sleep until
1307 tsleep(&dump_helper, 0, "cryptdump", 0);
1309 dump_helper.offset = dm_pdev_correct_dump_offset(priv->pdev,
1310 dump_helper.offset);
1312 dev_ddump(priv->pdev->pdev_vnode->v_rdev,
1313 dump_helper.space, 0, dump_helper.offset,
1314 dump_helper.length);
1316 tsleep_crypto_dump = 0;
1321 dmtc_crypto_dump_start(dm_target_crypt_config_t *priv, struct dmtc_dump_helper *dump_helper)
1323 struct cryptodesc *crd;
1324 struct cryptop *crp;
1325 int i, bytes, sectors;
1328 bytes = dump_helper->length;
1330 isector = dump_helper->offset / DEV_BSIZE; /* ivgen salt base? */
1331 sectors = bytes / DEV_BSIZE; /* Number of sectors */
1332 dump_helper->sectors = sectors;
1334 kprintf("Dump, bytes = %d, "
1335 "sectors = %d, LENGTH=%zu\n", bytes, sectors, dump_helper->length);
1337 KKASSERT(dump_helper->length <= 65536);
1339 memcpy(dump_helper->space, dump_helper->data, bytes);
1343 for (i = 0; i < sectors; i++) {
1344 crp = &dump_helper->crp[i];
1345 crd = &dump_helper->crd[i];
1347 crp->crp_buf = dump_helper->space + i * DEV_BSIZE;
1349 crp->crp_sid = priv->crypto_sid;
1350 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1352 crp->crp_opaque = (void *)dump_helper;
1354 crp->crp_callback = dmtc_crypto_cb_dump_done;
1355 crp->crp_desc = crd;
1357 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1360 crd->crd_alg = priv->crypto_alg;
1363 crd->crd_len = DEV_BSIZE /* XXX */;
1364 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1365 crd->crd_next = NULL;
1367 crd->crd_flags |= CRD_F_ENCRYPT;
1370 * Note: last argument is used to generate salt(?) and is
1371 * a 64 bit value, but the original code passed an
1372 * int. Changing it now will break pre-existing
1375 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1381 dmtc_crypto_cb_dump_done(struct cryptop *crp)
1383 struct dmtc_dump_helper *dump_helper;
1386 if (crp->crp_etype == EAGAIN)
1387 return crypto_dispatch(crp);
1389 dump_helper = (struct dmtc_dump_helper *)crp->crp_opaque;
1390 KKASSERT(dump_helper != NULL);
1392 if (crp->crp_etype != 0) {
1393 kprintf("dm_target_crypt: dmtc_crypto_cb_dump_done "
1396 return crp->crp_etype;
1400 * On the last chunk of the encryption we return control
1402 n = atomic_fetchadd_int(&dump_helper->sectors, -1);
1405 atomic_add_int(dump_helper->ident, 1);
1406 wakeup(dump_helper);
1413 dmtc_mod_handler(module_t mod, int type, void *unused)
1415 dm_target_t *dmt = NULL;
1420 if ((dmt = dm_target_lookup("crypt")) != NULL) {
1421 dm_target_unbusy(dmt);
1424 dmt = dm_target_alloc("crypt");
1425 dmt->version[0] = 1;
1426 dmt->version[1] = 6;
1427 dmt->version[2] = 0;
1428 dmt->init = &dm_target_crypt_init;
1429 dmt->destroy = &dm_target_crypt_destroy;
1430 dmt->strategy = &dm_target_crypt_strategy;
1431 dmt->table = &dm_target_crypt_table;
1432 dmt->dump = &dm_target_crypt_dump;
1434 err = dm_target_insert(dmt);
1436 kprintf("dm_target_crypt: Successfully initialized\n");
1440 err = dm_target_remove("crypt");
1442 kprintf("dm_target_crypt: unloaded\n");
1450 DM_TARGET_MODULE(dm_target_crypt, dmtc_mod_handler);