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, char *params)
511 dm_target_crypt_config_t *priv;
514 char *crypto_alg, *crypto_mode, *iv_mode, *iv_opt, *key, *dev;
516 int i, argc, klen, error;
517 uint64_t iv_offset, block_offset;
522 len = strlen(params) + 1;
525 status_str = kmalloc(len, M_DMCRYPT, M_WAITOK);
527 * Parse a string, containing tokens delimited by white space,
528 * into an argument vector
530 for (ap = args; ap < &args[5] &&
531 (*ap = strsep(¶ms, " \t")) != NULL;) {
539 kprintf("dm_target_crypt: not enough arguments, "
541 kfree(status_str, M_DMCRYPT);
542 return ENOMEM; /* XXX */
545 crypto_alg = strsep(&args[0], "-");
546 crypto_mode = strsep(&args[0], "-");
547 iv_opt = strsep(&args[0], "-");
548 iv_mode = strsep(&iv_opt, ":");
550 iv_offset = strtouq(args[2], NULL, 0);
552 block_offset = strtouq(args[4], NULL, 0);
553 /* bits / 8 = bytes, 1 byte = 2 hexa chars, so << 2 */
554 klen = strlen(key) << 2;
557 kprintf("dm_target_crypt - new: dev=%s, crypto_alg=%s, crypto_mode=%s, "
558 "iv_mode=%s, iv_opt=%s, key=%s, iv_offset=%ju, "
559 "block_offset=%ju\n",
560 dev, crypto_alg, crypto_mode, iv_mode, iv_opt, key, iv_offset,
564 priv = kmalloc(sizeof(dm_target_crypt_config_t), M_DMCRYPT, M_WAITOK);
566 /* Insert dmp to global pdev list */
567 if ((priv->pdev = dm_pdev_insert(dev)) == NULL) {
568 kprintf("dm_target_crypt: dm_pdev_insert failed\n");
569 kfree(status_str, M_DMCRYPT);
574 * This code checks for valid combinations of algorithm and mode.
575 * Currently supported options are:
582 if ((strcmp(crypto_mode, "cbc") != 0) &&
583 !((strcmp(crypto_mode, "xts") == 0) &&
584 ((strcmp(crypto_alg, "aes") == 0) ||
585 (strcmp(crypto_alg, "twofish") == 0) ||
586 (strcmp(crypto_alg, "serpent") == 0))))
588 kprintf("dm_target_crypt: only support 'cbc' chaining mode,"
589 " aes-xts, twofish-xts and serpent-xts, "
590 "invalid mode '%s-%s'\n",
591 crypto_alg, crypto_mode);
595 if (!strcmp(crypto_alg, "aes")) {
596 if (!strcmp(crypto_mode, "xts")) {
597 priv->crypto_alg = CRYPTO_AES_XTS;
598 if (klen != 256 && klen != 512)
600 } else if (!strcmp(crypto_mode, "cbc")) {
601 priv->crypto_alg = CRYPTO_AES_CBC;
602 if (klen != 128 && klen != 192 && klen != 256)
607 priv->crypto_klen = klen;
608 } else if (!strcmp(crypto_alg, "twofish")) {
609 if (!strcmp(crypto_mode, "xts")) {
610 priv->crypto_alg = CRYPTO_TWOFISH_XTS;
611 if (klen != 256 && klen != 512)
613 } else if (!strcmp(crypto_mode, "cbc")) {
614 priv->crypto_alg = CRYPTO_TWOFISH_CBC;
615 if (klen != 128 && klen != 192 && klen != 256)
620 priv->crypto_klen = klen;
621 } else if (!strcmp(crypto_alg, "serpent")) {
622 if (!strcmp(crypto_mode, "xts")) {
623 priv->crypto_alg = CRYPTO_SERPENT_XTS;
624 if (klen != 256 && klen != 512)
626 } else if (!strcmp(crypto_mode, "cbc")) {
627 priv->crypto_alg = CRYPTO_SERPENT_CBC;
628 if (klen != 128 && klen != 192 && klen != 256)
633 priv->crypto_klen = klen;
634 } else if (!strcmp(crypto_alg, "blowfish")) {
635 priv->crypto_alg = CRYPTO_BLF_CBC;
636 if (klen < 128 || klen > 448 || (klen % 8) != 0)
638 priv->crypto_klen = klen;
639 } else if (!strcmp(crypto_alg, "3des") ||
640 !strncmp(crypto_alg, "des3", 4)) {
641 priv->crypto_alg = CRYPTO_3DES_CBC;
644 priv->crypto_klen = 168;
645 } else if (!strcmp(crypto_alg, "camellia")) {
646 priv->crypto_alg = CRYPTO_CAMELLIA_CBC;
647 if (klen != 128 && klen != 192 && klen != 256)
649 priv->crypto_klen = klen;
650 } else if (!strcmp(crypto_alg, "skipjack")) {
651 priv->crypto_alg = CRYPTO_SKIPJACK_CBC;
654 priv->crypto_klen = 80;
655 } else if (!strcmp(crypto_alg, "cast5")) {
656 priv->crypto_alg = CRYPTO_CAST_CBC;
659 priv->crypto_klen = 128;
660 } else if (!strcmp(crypto_alg, "null")) {
661 priv->crypto_alg = CRYPTO_NULL_CBC;
664 priv->crypto_klen = 128;
666 kprintf("dm_target_crypt: Unsupported crypto algorithm: %s\n",
671 /* Save length of param string */
672 priv->params_len = len;
673 priv->block_offset = block_offset;
674 priv->iv_offset = iv_offset - block_offset;
676 dm_table_add_deps(table_en, priv->pdev);
678 dm_table_init_target(table_en, DM_CRYPTO_DEV, priv);
680 error = hex2key(key, priv->crypto_klen >> 3,
681 (u_int8_t *)priv->crypto_key);
684 kprintf("dm_target_crypt: hex2key failed, "
685 "invalid key format\n");
690 for(i = 0; ivgens[i].name != NULL; i++) {
691 if (!strcmp(iv_mode, ivgens[i].name))
695 if (ivgens[i].name == NULL) {
696 kprintf("dm_target_crypt: iv_mode='%s' unsupported\n",
701 /* Call our ivgen constructor */
702 if (ivgens[i].ctor != NULL) {
703 error = ivgens[i].ctor(priv, iv_opt,
706 kprintf("dm_target_crypt: ctor for '%s' failed\n",
712 priv->ivgen = &ivgens[i];
714 priv->crypto_session.cri_alg = priv->crypto_alg;
715 priv->crypto_session.cri_key = (u_int8_t *)priv->crypto_key;
716 priv->crypto_session.cri_klen = priv->crypto_klen;
717 priv->crypto_session.cri_mlen = 0;
718 priv->crypto_session.cri_next = NULL;
720 error = crypto_newsession(&priv->crypto_sid,
721 &priv->crypto_session,
722 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
724 kprintf("dm_target_crypt: Error during crypto_newsession, "
730 memset(key, '0', strlen(key));
732 ksprintf(status_str, "%s-%s-%s:%s %s %ju %s %ju",
733 crypto_alg, crypto_mode, iv_mode, iv_opt,
734 key, iv_offset, dev, block_offset);
736 ksprintf(status_str, "%s-%s-%s %s %ju %s %ju",
737 crypto_alg, crypto_mode, iv_mode,
738 key, iv_offset, dev, block_offset);
740 priv->status_str = status_str;
742 /* Initialize mpipes */
743 dmtc_init_mpipe(priv);
748 kprintf("dm_target_crypt: ENOTSUP\n");
749 kfree(status_str, M_DMCRYPT);
753 /* Table routine called to get params string. */
755 dm_target_crypt_table(void *target_config)
757 dm_target_crypt_config_t *priv;
760 priv = target_config;
762 /* caller expects use of M_DM */
763 params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
765 ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s",
772 dm_target_crypt_destroy(dm_table_entry_t *table_en)
774 dm_target_crypt_config_t *priv;
777 * Disconnect the crypt config before unbusying the target.
779 priv = table_en->target_config;
782 table_en->target_config = NULL;
783 dm_pdev_decr(priv->pdev);
786 * Clean up the crypt config
788 * Overwrite the private information before freeing memory to
791 if (priv->status_str) {
792 dmtc_crypto_clear(priv->status_str, strlen(priv->status_str));
793 kfree(priv->status_str, M_DMCRYPT);
794 crypto_freesession(priv->crypto_sid);
797 if ((priv->ivgen) && (priv->ivgen->dtor != NULL)) {
798 priv->ivgen->dtor(priv, priv->ivgen_priv);
802 dmtc_destroy_mpipe(priv);
804 dmtc_crypto_clear(priv, sizeof(dm_target_crypt_config_t));
805 kfree(priv, M_DMCRYPT);
810 /* Unsupported for this target. */
812 dm_target_crypt_upcall(dm_table_entry_t *table_en, struct buf *bp)
817 /************************************************************************
818 * STRATEGY SUPPORT FUNCTIONS *
819 ************************************************************************
821 * READ PATH: doio -> bio_read_done -> crypto_work -> crypto_cb_read_done
822 * WRITE PATH: crypto_work -> crypto_cb_write_done -> doio -> bio_write_done
826 * Wrapper around crypto_dispatch() to match dispatch_t type
829 dmtc_crypto_dispatch(void *arg)
833 crp = (struct cryptop *)arg;
834 KKASSERT(crp != NULL);
835 KTR_LOG(dmcrypt_crypto_dispatch, crp);
836 crypto_dispatch(crp);
840 * Start IO operation, called from dmstrategy routine.
843 dm_target_crypt_strategy(dm_table_entry_t *table_en, struct buf *bp)
847 dm_target_crypt_config_t *priv;
848 priv = table_en->target_config;
850 /* Get rid of stuff we can't really handle */
851 if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
852 if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
853 kprintf("dm_target_crypt_strategy: can't really "
854 "handle bp->b_bcount = %d\n",
856 bp->b_error = EINVAL;
857 bp->b_flags |= B_ERROR | B_INVAL;
858 biodone(&bp->b_bio1);
863 KTR_LOG(dmcrypt_crypt_strategy, bp->b_cmd, bp);
867 bio = push_bio(&bp->b_bio1);
868 bio->bio_offset = bp->b_bio1.bio_offset +
869 priv->block_offset * DEV_BSIZE;
870 bio->bio_caller_info1.ptr = priv;
871 bio->bio_done = dmtc_bio_read_done;
872 vn_strategy(priv->pdev->pdev_vnode, bio);
875 bio = push_bio(&bp->b_bio1);
876 bio->bio_offset = bp->b_bio1.bio_offset +
877 priv->block_offset * DEV_BSIZE;
878 bio->bio_caller_info1.ptr = priv;
879 dmtc_crypto_write_start(priv, bio);
882 vn_strategy(priv->pdev->pdev_vnode, &bp->b_bio1);
889 * STRATEGY READ PATH PART 1/3 (after read BIO completes)
892 dmtc_bio_read_done(struct bio *bio)
896 dm_target_crypt_config_t *priv;
898 KTR_LOG(dmcrypt_bio_read_done, bio->bio_buf);
901 * If a read error occurs we shortcut the operation, otherwise
904 if (bio->bio_buf->b_flags & B_ERROR) {
908 priv = bio->bio_caller_info1.ptr;
909 dmtc_crypto_read_start(priv, bio);
914 * STRATEGY READ PATH PART 2/3
917 dmtc_crypto_read_retry(void *arg1, void *arg2)
919 dm_target_crypt_config_t *priv = arg1;
920 struct bio *bio = arg2;
922 dmtc_crypto_read_start(priv, bio);
926 dmtc_crypto_read_start(dm_target_crypt_config_t *priv, struct bio *bio)
928 struct dmtc_helper *dmtc;
929 struct cryptodesc *crd;
931 int i, bytes, sectors, sz;
936 * Note: b_resid no good after read I/O, it will be 0, use
939 bytes = bio->bio_buf->b_bcount;
940 isector = bio->bio_offset / DEV_BSIZE; /* ivgen salt base? */
941 sectors = bytes / DEV_BSIZE; /* Number of sectors */
942 sz = sectors * (sizeof(*crp) + sizeof(*crd));
945 * For reads with bogus page we can't decrypt in place as stuff
946 * can get ripped out from under us.
948 * XXX actually it looks like we can, and in any case the initial
949 * read already completed and threw crypted data into the buffer
950 * cache buffer. Disable for now.
952 space = mpipe_alloc_callback(&priv->read_mpipe,
953 dmtc_crypto_read_retry, priv, bio);
957 dmtc = (struct dmtc_helper *)space;
958 dmtc->free_addr = space;
959 space += sizeof(struct dmtc_helper);
960 dmtc->orig_buf = NULL;
961 dmtc->data_buf = bio->bio_buf->b_data;
963 bio->bio_caller_info2.ptr = dmtc;
964 bio->bio_buf->b_error = 0;
967 * Load crypto descriptors (crp/crd loop)
971 bio->bio_caller_info3.value = sectors;
974 kprintf("Read, bytes = %d (b_bcount), "
975 "sectors = %d (bio = %p, b_cmd = %d)\n",
976 bytes, sectors, bio, bio->bio_buf->b_cmd);
978 for (i = 0; i < sectors; i++) {
979 crp = (struct cryptop *)ptr;
981 crd = (struct cryptodesc *)ptr;
982 ptr += sizeof (*crd);
984 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
986 crp->crp_sid = priv->crypto_sid;
987 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
989 crp->crp_opaque = (void *)bio;
991 crp->crp_callback = dmtc_crypto_cb_read_done;
994 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
997 crd->crd_alg = priv->crypto_alg;
999 crd->crd_key = (caddr_t)priv->crypto_key;
1000 crd->crd_klen = priv->crypto_klen;
1004 crd->crd_len = DEV_BSIZE /* XXX */;
1005 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1006 crd->crd_next = NULL;
1008 crd->crd_flags &= ~CRD_F_ENCRYPT;
1010 KTR_LOG(dmcrypt_crypto_read_start, crp, bio->bio_buf, i,
1014 * Note: last argument is used to generate salt(?) and is
1015 * a 64 bit value, but the original code passed an
1016 * int. Changing it now will break pre-existing
1019 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1025 * STRATEGY READ PATH PART 3/3
1028 dmtc_crypto_cb_read_done(struct cryptop *crp)
1030 struct dmtc_helper *dmtc;
1031 struct bio *bio, *obio;
1034 if (crp->crp_etype == EAGAIN)
1035 return crypto_dispatch(crp);
1037 bio = (struct bio *)crp->crp_opaque;
1038 KKASSERT(bio != NULL);
1043 if (crp->crp_etype) {
1044 kprintf("dm_target_crypt: dmtc_crypto_cb_read_done "
1047 bio->bio_buf->b_error = crp->crp_etype;
1051 * On the last chunk of the decryption we do any required copybacks
1052 * and complete the I/O.
1054 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
1056 kprintf("dmtc_crypto_cb_read_done %p, n = %d\n", bio, n);
1059 KTR_LOG(dmcrypt_crypto_cb_read_done, crp, bio->bio_buf, n);
1063 * For the B_HASBOGUS case we didn't decrypt in place,
1064 * so we need to copy stuff back into the buf.
1066 * (disabled for now).
1068 dmtc = bio->bio_caller_info2.ptr;
1069 if (bio->bio_buf->b_error) {
1070 bio->bio_buf->b_flags |= B_ERROR;
1073 else if (bio->bio_buf->b_flags & B_HASBOGUS) {
1074 memcpy(bio->bio_buf->b_data, dmtc->data_buf,
1075 bio->bio_buf->b_bcount);
1078 mpipe_free(&dmtc->priv->read_mpipe, dmtc->free_addr);
1079 obio = pop_bio(bio);
1084 /* END OF STRATEGY READ SECTION */
1087 * STRATEGY WRITE PATH PART 1/3
1091 dmtc_crypto_write_retry(void *arg1, void *arg2)
1093 dm_target_crypt_config_t *priv = arg1;
1094 struct bio *bio = arg2;
1096 KTR_LOG(dmcrypt_crypto_write_retry, bio->bio_buf);
1098 dmtc_crypto_write_start(priv, bio);
1102 dmtc_crypto_write_start(dm_target_crypt_config_t *priv, struct bio *bio)
1104 struct dmtc_helper *dmtc;
1105 struct cryptodesc *crd;
1106 struct cryptop *crp;
1107 int i, bytes, sectors, sz;
1109 u_char *ptr, *space;
1112 * Use b_bcount for consistency
1114 bytes = bio->bio_buf->b_bcount;
1116 isector = bio->bio_offset / DEV_BSIZE; /* ivgen salt base? */
1117 sectors = bytes / DEV_BSIZE; /* Number of sectors */
1118 sz = sectors * (sizeof(*crp) + sizeof(*crd));
1121 * For writes and reads with bogus page don't decrypt in place.
1123 space = mpipe_alloc_callback(&priv->write_mpipe,
1124 dmtc_crypto_write_retry, priv, bio);
1128 dmtc = (struct dmtc_helper *)space;
1129 dmtc->free_addr = space;
1130 space += sizeof(struct dmtc_helper);
1131 memcpy(space + sz, bio->bio_buf->b_data, bytes);
1133 bio->bio_caller_info2.ptr = dmtc;
1134 bio->bio_buf->b_error = 0;
1136 dmtc->orig_buf = bio->bio_buf->b_data;
1137 dmtc->data_buf = space + sz;
1141 * Load crypto descriptors (crp/crd loop)
1145 bio->bio_caller_info3.value = sectors;
1148 kprintf("Write, bytes = %d (b_bcount), "
1149 "sectors = %d (bio = %p, b_cmd = %d)\n",
1150 bytes, sectors, bio, bio->bio_buf->b_cmd);
1152 for (i = 0; i < sectors; i++) {
1153 crp = (struct cryptop *)ptr;
1154 ptr += sizeof(*crp);
1155 crd = (struct cryptodesc *)ptr;
1156 ptr += sizeof (*crd);
1158 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
1160 crp->crp_sid = priv->crypto_sid;
1161 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1163 crp->crp_opaque = (void *)bio;
1165 crp->crp_callback = dmtc_crypto_cb_write_done;
1166 crp->crp_desc = crd;
1168 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1171 crd->crd_alg = priv->crypto_alg;
1173 crd->crd_key = (caddr_t)priv->crypto_key;
1174 crd->crd_klen = priv->crypto_klen;
1178 crd->crd_len = DEV_BSIZE /* XXX */;
1179 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1180 crd->crd_next = NULL;
1182 crd->crd_flags |= CRD_F_ENCRYPT;
1185 * Note: last argument is used to generate salt(?) and is
1186 * a 64 bit value, but the original code passed an
1187 * int. Changing it now will break pre-existing
1191 KTR_LOG(dmcrypt_crypto_write_start, crp, bio->bio_buf,
1194 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1200 * STRATEGY WRITE PATH PART 2/3
1203 dmtc_crypto_cb_write_done(struct cryptop *crp)
1205 struct dmtc_helper *dmtc;
1206 dm_target_crypt_config_t *priv;
1207 struct bio *bio, *obio;
1210 if (crp->crp_etype == EAGAIN)
1211 return crypto_dispatch(crp);
1213 bio = (struct bio *)crp->crp_opaque;
1214 KKASSERT(bio != NULL);
1219 if (crp->crp_etype != 0) {
1220 kprintf("dm_target_crypt: dmtc_crypto_cb_write_done "
1223 bio->bio_buf->b_error = crp->crp_etype;
1227 * On the last chunk of the encryption we issue the write
1229 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
1231 kprintf("dmtc_crypto_cb_write_done %p, n = %d\n", bio, n);
1234 KTR_LOG(dmcrypt_crypto_cb_write_done, crp, bio->bio_buf, n);
1237 dmtc = bio->bio_caller_info2.ptr;
1238 priv = (dm_target_crypt_config_t *)bio->bio_caller_info1.ptr;
1240 if (bio->bio_buf->b_error) {
1241 bio->bio_buf->b_flags |= B_ERROR;
1242 mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
1243 obio = pop_bio(bio);
1246 dmtc->orig_buf = bio->bio_buf->b_data;
1247 bio->bio_buf->b_data = dmtc->data_buf;
1248 bio->bio_done = dmtc_bio_write_done;
1249 vn_strategy(priv->pdev->pdev_vnode, bio);
1256 * STRATEGY WRITE PATH PART 3/3
1259 dmtc_bio_write_done(struct bio *bio)
1261 struct dmtc_helper *dmtc;
1264 dmtc = bio->bio_caller_info2.ptr;
1265 bio->bio_buf->b_data = dmtc->orig_buf;
1266 mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
1268 KTR_LOG(dmcrypt_bio_write_done, bio->bio_buf);
1270 obio = pop_bio(bio);
1273 /* END OF STRATEGY WRITE SECTION */
1279 extern int tsleep_crypto_dump;
1282 dm_target_crypt_dump(dm_table_entry_t *table_en, void *data, size_t length, off_t offset)
1284 static struct dmtc_dump_helper dump_helper;
1285 dm_target_crypt_config_t *priv;
1287 static int first_call = 1;
1289 priv = table_en->target_config;
1293 dump_reactivate_cpus();
1296 /* Magically enable tsleep */
1297 tsleep_crypto_dump = 1;
1301 * 0 length means flush buffers and return
1304 if (priv->pdev->pdev_vnode->v_rdev == NULL) {
1305 tsleep_crypto_dump = 0;
1308 dev_ddump(priv->pdev->pdev_vnode->v_rdev,
1309 data, 0, offset, 0);
1310 tsleep_crypto_dump = 0;
1314 bzero(&dump_helper, sizeof(dump_helper));
1315 dump_helper.priv = priv;
1316 dump_helper.data = data;
1317 dump_helper.length = length;
1318 dump_helper.offset = offset +
1319 priv->block_offset * DEV_BSIZE;
1320 dump_helper.ident = &id;
1321 dmtc_crypto_dump_start(priv, &dump_helper);
1324 * Hackery to make stuff appear synchronous. The crypto callback will
1325 * set id to 1 and call wakeup on it. If the request completed
1326 * synchronously, id will be 1 and we won't bother to sleep. If not,
1327 * the crypto request will complete asynchronously and we sleep until
1331 tsleep(&dump_helper, 0, "cryptdump", 0);
1333 dump_helper.offset = dm_pdev_correct_dump_offset(priv->pdev,
1334 dump_helper.offset);
1336 dev_ddump(priv->pdev->pdev_vnode->v_rdev,
1337 dump_helper.space, 0, dump_helper.offset,
1338 dump_helper.length);
1340 tsleep_crypto_dump = 0;
1345 dmtc_crypto_dump_start(dm_target_crypt_config_t *priv, struct dmtc_dump_helper *dump_helper)
1347 struct cryptodesc *crd;
1348 struct cryptop *crp;
1349 int i, bytes, sectors;
1352 bytes = dump_helper->length;
1354 isector = dump_helper->offset / DEV_BSIZE; /* ivgen salt base? */
1355 sectors = bytes / DEV_BSIZE; /* Number of sectors */
1356 dump_helper->sectors = sectors;
1358 kprintf("Dump, bytes = %d, "
1359 "sectors = %d, LENGTH=%zu\n", bytes, sectors, dump_helper->length);
1361 KKASSERT(dump_helper->length <= 65536);
1363 memcpy(dump_helper->space, dump_helper->data, bytes);
1367 for (i = 0; i < sectors; i++) {
1368 crp = &dump_helper->crp[i];
1369 crd = &dump_helper->crd[i];
1371 crp->crp_buf = dump_helper->space + i * DEV_BSIZE;
1373 crp->crp_sid = priv->crypto_sid;
1374 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1376 crp->crp_opaque = (void *)dump_helper;
1378 crp->crp_callback = dmtc_crypto_cb_dump_done;
1379 crp->crp_desc = crd;
1381 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1384 crd->crd_alg = priv->crypto_alg;
1387 crd->crd_len = DEV_BSIZE /* XXX */;
1388 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1389 crd->crd_next = NULL;
1391 crd->crd_flags |= CRD_F_ENCRYPT;
1394 * Note: last argument is used to generate salt(?) and is
1395 * a 64 bit value, but the original code passed an
1396 * int. Changing it now will break pre-existing
1399 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1405 dmtc_crypto_cb_dump_done(struct cryptop *crp)
1407 struct dmtc_dump_helper *dump_helper;
1410 if (crp->crp_etype == EAGAIN)
1411 return crypto_dispatch(crp);
1413 dump_helper = (struct dmtc_dump_helper *)crp->crp_opaque;
1414 KKASSERT(dump_helper != NULL);
1416 if (crp->crp_etype != 0) {
1417 kprintf("dm_target_crypt: dmtc_crypto_cb_dump_done "
1420 return crp->crp_etype;
1424 * On the last chunk of the encryption we return control
1426 n = atomic_fetchadd_int(&dump_helper->sectors, -1);
1429 atomic_add_int(dump_helper->ident, 1);
1430 wakeup(dump_helper);
1437 dmtc_mod_handler(module_t mod, int type, void *unused)
1439 dm_target_t *dmt = NULL;
1444 if ((dmt = dm_target_lookup("crypt")) != NULL) {
1445 dm_target_unbusy(dmt);
1448 dmt = dm_target_alloc("crypt");
1449 dmt->version[0] = 1;
1450 dmt->version[1] = 6;
1451 dmt->version[2] = 0;
1452 strlcpy(dmt->name, "crypt", DM_MAX_TYPE_NAME);
1453 dmt->init = &dm_target_crypt_init;
1454 dmt->table = &dm_target_crypt_table;
1455 dmt->strategy = &dm_target_crypt_strategy;
1456 dmt->destroy = &dm_target_crypt_destroy;
1457 dmt->upcall = &dm_target_crypt_upcall;
1458 dmt->dump = &dm_target_crypt_dump;
1460 err = dm_target_insert(dmt);
1462 kprintf("dm_target_crypt: Successfully initialized\n");
1466 err = dm_target_rem("crypt");
1468 kprintf("dm_target_crypt: unloaded\n");
1479 DM_TARGET_MODULE(dm_target_crypt, dmtc_mod_handler);