2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This file implements initial version of device-mapper crypt target.
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/endian.h>
44 #include <sys/malloc.h>
46 #include <sys/vnode.h>
47 #include <crypto/sha1.h>
48 #include <crypto/sha2/sha2.h>
49 #include <opencrypto/cryptodev.h>
50 #include <opencrypto/rmd160.h>
53 MALLOC_DEFINE(M_DMCRYPT, "dm_crypt", "Device Mapper Target Crypt");
55 struct target_crypt_config;
56 typedef void ivgen_t(struct target_crypt_config *, u_int8_t *, size_t, off_t);
58 typedef struct target_crypt_config {
64 u_int8_t crypto_key[512>>3];
65 u_int8_t crypto_keyhash[SHA512_DIGEST_LENGTH];
67 u_int64_t block_offset;
69 SHA512_CTX essivsha512_ctx;
70 struct cryptoini crypto_session;
71 ivgen_t *crypto_ivgen;
72 } dm_target_crypt_config_t;
79 static void dmtc_crypto_start(dm_target_crypt_config_t *priv, struct bio *bio);
80 static void dmtc_bio_read_done(struct bio *bio);
81 static void dmtc_bio_write_done(struct bio *bio);
82 static int dmtc_crypto_cb_read_done(struct cryptop *crp);
83 static int dmtc_crypto_cb_write_done(struct cryptop *crp);
88 essiv_hash_mkey(dm_target_crypt_config_t *priv, char *iv_hash)
92 klen = (priv->crypto_klen >> 3);
97 if (!strcmp(iv_hash, "sha1")) {
100 if (klen != SHA1_RESULTLEN)
104 SHA1Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
105 SHA1Final(priv->crypto_keyhash, &ctx);
106 } else if (!strcmp(iv_hash, "sha256")) {
109 if (klen != SHA256_DIGEST_LENGTH)
113 SHA256_Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
114 SHA256_Final(priv->crypto_keyhash, &ctx);
115 } else if (!strcmp(iv_hash, "sha384")) {
118 if (klen != SHA384_DIGEST_LENGTH)
122 SHA384_Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
123 SHA384_Final(priv->crypto_keyhash, &ctx);
124 } else if (!strcmp(iv_hash, "sha512")) {
127 if (klen != SHA512_DIGEST_LENGTH)
131 SHA512_Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
132 SHA512_Final(priv->crypto_keyhash, &ctx);
133 } else if (!strcmp(iv_hash, "md5")) {
136 if (klen != MD5_DIGEST_LENGTH)
140 MD5Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
141 MD5Final(priv->crypto_keyhash, &ctx);
142 } else if (!strcmp(iv_hash, "rmd160") || !strcmp(iv_hash, "ripemd160")) {
149 RMD160Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
150 RMD160Final(priv->crypto_keyhash, &ctx);
159 essiv_ivgen_done(struct cryptop *crp)
162 if (crp->crp_etype == EAGAIN)
163 return crypto_dispatch(crp);
165 if (crp->crp_etype != 0) {
166 kprintf("dm_target_crypt: essiv_ivgen_done, crp->crp_etype = %d\n", crp->crp_etype);
169 atomic_add_int((int *)crp->crp_opaque, 1);
170 wakeup(crp->crp_opaque);
175 plain_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv, size_t iv_len, off_t sector)
178 *((off_t *)iv) = htole64(sector + priv->iv_offset);
182 essiv_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv, size_t iv_len, off_t sector)
184 struct cryptodesc crd;
190 *((off_t *)iv) = htole64(sector + priv->iv_offset);
191 crp.crp_buf = (caddr_t)iv;
193 crp.crp_sid = priv->crypto_sid;
194 crp.crp_ilen = crp.crp_olen = iv_len;
196 crp.crp_opaque = (void *)&id;
198 crp.crp_callback = essiv_ivgen_done;
202 crp.crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
204 crd.crd_alg = priv->crypto_alg;
205 crd.crd_key = (caddr_t)priv->crypto_keyhash;
206 crd.crd_klen = priv->crypto_klen;
208 bzero(crd.crd_iv, sizeof(crd.crd_iv));
211 crd.crd_len = iv_len;
212 crd.crd_flags = CRD_F_KEY_EXPLICIT | CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
213 crd.crd_flags |= CRD_F_ENCRYPT;
216 error = crypto_dispatch(&crp);
218 kprintf("dm_target_crypt: essiv_ivgen, error = %d\n", error);
221 * id is modified in the callback, so that if crypto_dispatch finishes
222 * synchronously we don't tsleep() forever.
225 tsleep((void *)&error, 0, "essivgen", 0);
230 geli_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv, size_t iv_len, off_t sector)
234 u_int8_t md[SHA512_DIGEST_LENGTH]; /* Max. Digest Size */
236 memcpy(&ctx512, &priv->essivsha512_ctx, sizeof(SHA512_CTX));
237 SHA512_Update(&ctx512, (u_int8_t*)§or, sizeof(off_t));
238 SHA512_Final(md, &ctx512);
240 memcpy(iv, md, iv_len);
245 dmtc_crypto_start(dm_target_crypt_config_t *priv, struct bio *bio)
247 struct dmtc_helper *dmtc;
248 struct cryptodesc *crd;
250 struct cryptoini *cri;
252 int error, i, bytes, isector, sectors, write, sz;
253 u_char *ptr, *space, *data;
255 cri = &priv->crypto_session;
257 write = (bio->bio_buf->b_cmd == BUF_CMD_WRITE) ? 1 : 0;
258 bytes = bio->bio_buf->b_bcount; /* XXX: b_resid no good after reads... == 0 */
259 isector = bio->bio_offset/DEV_BSIZE; /* Initial sector */
260 sectors = bytes/DEV_BSIZE; /* Number of sectors affected by bio */
261 sz = sectors * (sizeof(*crp) + sizeof(*crd));
264 space = kmalloc(sizeof(struct dmtc_helper) + sz + bytes, M_DMCRYPT, M_WAITOK);
265 dmtc = (struct dmtc_helper *)space;
266 dmtc->free_addr = space;
267 dmtc->orig_buf = bio->bio_buf->b_data;
268 space += sizeof(struct dmtc_helper);
269 memcpy(space + sz, bio->bio_buf->b_data, bytes);
270 bio->bio_caller_info2.ptr = dmtc;
271 bio->bio_buf->b_data = data = space + sz;
273 space = kmalloc(sz, M_DMCRYPT, M_WAITOK);
274 data = bio->bio_buf->b_data;
275 bio->bio_caller_info2.ptr = space;
279 bio->bio_caller_info3.value = sectors;
281 kprintf("Write? %d, bytes = %d (b_bcount), sectors = %d (bio = %p, b_cmd = %d)\n", write, bytes, sectors, bio, bio->bio_buf->b_cmd);
283 for (i = 0; i < sectors; i++) {
284 crp = (struct cryptop *)ptr;
286 crd = (struct cryptodesc *)ptr;
287 ptr += sizeof (*crd);
289 crp->crp_buf = (data + i*DEV_BSIZE);
291 crp->crp_sid = priv->crypto_sid;
292 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
294 crp->crp_opaque = (void *)bio;
297 crp->crp_callback = dmtc_crypto_cb_write_done;
299 crp->crp_callback = dmtc_crypto_cb_read_done;
302 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
304 crd->crd_alg = priv->crypto_alg;
305 crd->crd_key = (caddr_t)priv->crypto_key;
306 crd->crd_klen = priv->crypto_klen;
308 priv->crypto_ivgen(priv, crd->crd_iv, sizeof(crd->crd_iv), isector + i);
311 crd->crd_len = DEV_BSIZE /* XXX */;
312 crd->crd_flags = CRD_F_KEY_EXPLICIT | CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
313 crd->crd_next = NULL;
316 crd->crd_flags |= CRD_F_ENCRYPT;
318 crd->crd_flags &= ~CRD_F_ENCRYPT;
320 error = crypto_dispatch(crp);
325 dmtc_bio_read_done(struct bio *bio)
327 dm_target_crypt_config_t *priv;
329 priv = bio->bio_caller_info1.ptr;
331 dmtc_crypto_start(priv, bio);
335 dmtc_bio_write_done(struct bio *bio)
337 struct dmtc_helper *dmtc;
340 dmtc = bio->bio_caller_info2.ptr;
341 bio->bio_buf->b_data = dmtc->orig_buf;
342 kfree(dmtc->free_addr, M_DMCRYPT);
348 dmtc_crypto_cb_read_done(struct cryptop *crp)
350 struct bio *bio, *obio;
353 if (crp->crp_etype == EAGAIN)
354 return crypto_dispatch(crp);
356 bio = (struct bio *)crp->crp_opaque;
357 KKASSERT(bio != NULL);
359 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
361 kprintf("dmtc_crypto_cb_read_done %p, n = %d\n", bio, n);
363 if (crp->crp_etype != 0) {
364 kprintf("dm_target_crypt: dmtc_crypto_cb_read_done crp_etype = %d\n", crp->crp_etype);
365 bio->bio_buf->b_error = crp->crp_etype;
368 kfree(bio->bio_caller_info2.ptr, M_DMCRYPT);
369 /* This is the last chunk of the read */
378 dmtc_crypto_cb_write_done(struct cryptop *crp)
380 struct dmtc_helper *dmtc;
381 dm_target_crypt_config_t *priv;
382 struct bio *bio, *obio;
385 if (crp->crp_etype == EAGAIN)
386 return crypto_dispatch(crp);
388 bio = (struct bio *)crp->crp_opaque;
389 KKASSERT(bio != NULL);
391 n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
393 kprintf("dmtc_crypto_cb_write_done %p, n = %d\n", bio, n);
395 if (crp->crp_etype != 0) {
396 kprintf("dm_target_crypt: dmtc_crypto_cb_write_done crp_etype = %d\n", crp->crp_etype);
397 bio->bio_buf->b_error = crp->crp_etype;
400 /* This is the last chunk of the write */
401 if (bio->bio_buf->b_error != 0) {
403 dmtc = bio->bio_caller_info2.ptr;
404 bio->bio_buf->b_data = dmtc->orig_buf;
405 kfree(dmtc->free_addr, M_DMCRYPT);
409 priv = (dm_target_crypt_config_t *)bio->bio_caller_info1.ptr;
410 vn_strategy(priv->pdev->pdev_vnode, bio);
418 strategy -> read_done -> crypto_work -> crypto_done_read -> FINISH READ
419 strategy -> crypto_work -> crypto_done_write -> write dispatch -> write_done -> FINISH WRITE
422 #ifdef DM_TARGET_MODULE
424 * Every target can be compiled directly to dm driver or as a
425 * separate module this part of target is used for loading targets
427 * Target can be unloaded from kernel only if there are no users of
428 * it e.g. there are no devices which uses that target.
430 #include <sys/kernel.h>
431 #include <sys/module.h>
434 dm_target_crypt_modcmd(modcmd_t cmd, void *arg)
441 case MODULE_CMD_INIT:
442 if ((dmt = dm_target_lookup("crypt")) != NULL) {
443 dm_target_unbusy(dmt);
446 dmt = dm_target_alloc("crypt");
451 strlcpy(dmt->name, "crypt", DM_MAX_TYPE_NAME);
452 dmt->init = &dm_target_crypt_init;
453 dmt->status = &dm_target_crypt_status;
454 dmt->strategy = &dm_target_crypt_strategy;
455 dmt->deps = &dm_target_crypt_deps;
456 dmt->destroy = &dm_target_crypt_destroy;
457 dmt->upcall = &dm_target_crypt_upcall;
459 r = dm_target_insert(dmt);
463 case MODULE_CMD_FINI:
464 r = dm_target_rem("crypt");
467 case MODULE_CMD_STAT:
479 * Init function called from dm_table_load_ioctl.
480 * cryptsetup actually passes us this:
481 * aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8
485 hex2key(char *hex, size_t hex_length, u_int8_t *key)
491 bzero(hex_buf, sizeof(hex_buf));
493 for (; hex_length > 0; hex_length -= 2) {
496 key[key_idx++] = (u_int8_t)strtoul(hex_buf, NULL, 16);
503 dm_target_crypt_init(dm_dev_t * dmv, void **target_config, char *params)
505 dm_target_crypt_config_t *priv;
508 char *crypto_alg, *crypto_mode, *iv_mode, *iv_opt, *key, *dev;
510 int argc, klen, error;
511 uint64_t iv_offset, block_offset;
516 len = strlen(params) + 1;
519 status_str = kstrdup(params, M_DMCRYPT);
521 * Parse a string, containing tokens delimited by white space,
522 * into an argument vector
524 for (ap = args; ap < &args[5] &&
525 (*ap = strsep(¶ms, " \t")) != NULL;) {
533 kprintf("dm_target_crypt: not enough arguments, need exactly 5\n");
534 kfree(status_str, M_DMCRYPT);
535 return ENOMEM; /* XXX */
538 crypto_alg = strsep(&args[0], "-");
539 crypto_mode = strsep(&args[0], "-");
540 iv_opt = strsep(&args[0], "-");
541 iv_mode = strsep(&iv_opt, ":");
543 iv_offset = strtouq(args[2], NULL, 0);
545 block_offset = strtouq(args[4], NULL, 0);
546 /* bits / 8 = bytes, 1 byte = 2 hexa chars, so << 2 */
547 klen = strlen(key) << 2;
549 kprintf("dm_target_crypt: dev=%s, crypto_alg=%s, crypto_mode=%s, "
550 "iv_mode=%s, iv_opt=%s, key=%s, iv_offset=%ju, block_offset=%ju\n",
551 dev, crypto_alg, crypto_mode, iv_mode, iv_opt, key, iv_offset,
554 priv = kmalloc(sizeof(dm_target_crypt_config_t), M_DMCRYPT, M_WAITOK);
556 kprintf("dm_target_crypt: could not allocate memory\n");
557 kfree(status_str, M_DMCRYPT);
561 /* Insert dmp to global pdev list */
562 if ((priv->pdev = dm_pdev_insert(dev)) == NULL) {
563 kprintf("dm_target_crypt: dm_pdev_insert failed\n");
564 kfree(status_str, M_DMCRYPT);
568 if (strcmp(crypto_mode, "cbc") != 0) {
569 kprintf("dm_target_crypt: only support 'cbc' chaining mode, invalid mode '%s'\n", crypto_mode);
573 if (!strcmp(crypto_alg, "aes")) {
574 priv->crypto_alg = CRYPTO_AES_CBC;
575 if (klen != 128 && klen != 192 && klen != 256)
577 priv->crypto_klen = klen;
579 } else if (!strcmp(crypto_alg, "blowfish")) {
580 priv->crypto_alg = CRYPTO_BLF_CBC;
581 if (klen < 128 || klen > 448 || (klen % 8) != 0)
583 priv->crypto_klen = klen;
585 } else if (!strcmp(crypto_alg, "3des") || !strncmp(crypto_alg, "des3", 4)) {
586 priv->crypto_alg = CRYPTO_3DES_CBC;
589 priv->crypto_klen = 168;
591 } else if (!strcmp(crypto_alg, "camellia")) {
592 priv->crypto_alg = CRYPTO_CAMELLIA_CBC;
593 if (klen != 128 && klen != 192 && klen != 256)
595 priv->crypto_klen = klen;
597 } else if (!strcmp(crypto_alg, "skipjack")) {
598 priv->crypto_alg = CRYPTO_SKIPJACK_CBC;
601 priv->crypto_klen = 80;
603 } else if (!strcmp(crypto_alg, "cast5")) {
604 priv->crypto_alg = CRYPTO_CAST_CBC;
607 priv->crypto_klen = 128;
609 } else if (!strcmp(crypto_alg, "null")) {
610 priv->crypto_alg = CRYPTO_NULL_CBC;
613 priv->crypto_klen = 128;
616 kprintf("dm_target_crypt: Unsupported crypto algorithm: %s\n", crypto_alg);
620 /* Save length of param string */
621 priv->params_len = len;
622 priv->block_offset = block_offset;
623 priv->iv_offset = iv_offset;
625 *target_config = priv;
627 dmv->dev_type = DM_CRYPTO_DEV;
629 priv->crypto_session.cri_alg = priv->crypto_alg;
630 priv->crypto_session.cri_klen = priv->crypto_klen;
631 priv->crypto_session.cri_mlen = 0;
633 error = hex2key(key, priv->crypto_klen >> 3, (u_int8_t *)priv->crypto_key);
635 kprintf("dm_target_crypt: hex2key failed, invalid key format\n");
640 if (!strcmp(iv_mode, "essiv")) {
641 error = essiv_hash_mkey(priv, iv_opt);
643 kprintf("dm_target_crypt: essiv_hash_mkey failed\n");
646 priv->crypto_ivgen = essiv_ivgen;
647 } else if (!strcmp(iv_mode, "plain")) {
648 priv->crypto_ivgen = plain_ivgen;
650 kprintf("dm_target_crypt: only support iv_mode='essiv' and 'plain', iv_mode='%s' unsupported\n", iv_mode);
653 priv->crypto_session.cri_key = (u_int8_t *)priv->crypto_key;
654 priv->crypto_session.cri_next = NULL;
656 error = crypto_newsession(&priv->crypto_sid,
657 &priv->crypto_session,
658 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
660 kprintf("dm_target_crypt: Error during crypto_newsession, error = %d\n", error);
664 priv->status_str = status_str;
668 kprintf("dm_target_crypt: ENOTSUP\n");
669 kfree(status_str, M_DMCRYPT);
673 /* Status routine called to get params string. */
675 dm_target_crypt_status(void *target_config)
677 dm_target_crypt_config_t *priv;
680 priv = target_config;
682 /* caller expects use of M_DM */
683 params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
685 ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s",
691 /* Strategy routine called from dm_strategy. */
693 * Do IO operation, called from dmstrategy routine.
696 dm_target_crypt_strategy(dm_table_entry_t * table_en, struct buf * bp)
700 dm_target_crypt_config_t *priv;
701 priv = table_en->target_config;
703 /* Get rid of stuff we can't really handle */
704 if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
705 if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
706 kprintf("dm_target_crypt_strategy: can't really handle bp->b_bcount = %d\n", bp->b_bcount);
707 bp->b_error = EINVAL;
708 bp->b_flags |= B_ERROR | B_INVAL;
709 biodone(&bp->b_bio1);
716 bio = push_bio(&bp->b_bio1);
717 bio->bio_offset = bp->b_bio1.bio_offset + priv->block_offset*DEV_BSIZE;
718 bio->bio_caller_info1.ptr = priv;
719 bio->bio_done = dmtc_bio_read_done;
720 vn_strategy(priv->pdev->pdev_vnode, bio);
724 bio = push_bio(&bp->b_bio1);
725 bio->bio_offset = bp->b_bio1.bio_offset + priv->block_offset*DEV_BSIZE;
726 bio->bio_caller_info1.ptr = priv;
727 bio->bio_done = dmtc_bio_write_done;
728 dmtc_crypto_start(priv, bio);
732 vn_strategy(priv->pdev->pdev_vnode, &bp->b_bio1);
740 dm_target_crypt_destroy(dm_table_entry_t * table_en)
742 dm_target_crypt_config_t *priv;
744 priv = table_en->target_config;
749 dm_pdev_decr(priv->pdev);
751 /* Unbusy target so we can unload it */
752 dm_target_unbusy(table_en->target);
755 * Overwrite the private information before freeing memory to
758 memset(priv->status_str, 0xFF, strlen(priv->status_str));
759 bzero(priv->status_str, strlen(priv->status_str));
760 memset(priv, 0xFF, sizeof(dm_target_crypt_config_t));
761 bzero(priv, sizeof(dm_target_crypt_config_t));
763 kfree(priv->status_str, M_DMCRYPT);
764 kfree(priv, M_DMCRYPT);
766 table_en->target_config = NULL;
772 dm_target_crypt_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
774 dm_target_crypt_config_t *priv;
779 if (table_en->target_config == NULL)
782 priv = table_en->target_config;
784 if ((error = VOP_GETATTR(priv->pdev->pdev_vnode, &va)) != 0)
787 prop_array_add_uint64(prop_array, (uint64_t) makeudev(va.va_rmajor, va.va_rminor));
792 /* Unsupported for this target. */
794 dm_target_crypt_upcall(dm_table_entry_t * table_en, struct buf * bp)