dm_target_crypt - Make compatible with luks
[dragonfly.git] / sys / dev / disk / dm / dm_target_crypt.c
1 /*
2  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * This file implements initial version of device-mapper crypt target.
37  */
38 #include <sys/types.h>
39 #include <sys/param.h>
40
41 #include <sys/bio.h>
42 #include <sys/buf.h>
43 #include <sys/malloc.h>
44 #include <sys/md5.h>
45 #include <sys/vnode.h>
46 #include <crypto/sha1.h>
47 #include <crypto/sha2/sha2.h>
48 #include <opencrypto/cryptodev.h>
49 #include <opencrypto/rmd160.h>
50
51 #include "dm.h"
52 MALLOC_DEFINE(M_DMCRYPT, "dm_crypt", "Device Mapper Target Crypt");
53
54 struct target_crypt_config;
55 typedef void ivgen_t(struct target_crypt_config *, u_int8_t *, size_t, off_t);
56
57 typedef struct target_crypt_config {
58         size_t  params_len;
59         dm_pdev_t *pdev;
60         char    *status_str;
61         int     crypto_alg;
62         int     crypto_klen;
63         u_int8_t        crypto_key[512>>3];
64         u_int8_t        crypto_keyhash[SHA512_DIGEST_LENGTH];
65         u_int64_t       crypto_sid;
66         u_int64_t       block_offset;
67         u_int64_t       iv_offset;
68         SHA512_CTX      essivsha512_ctx;
69         struct cryptoini        crypto_session;
70         ivgen_t *crypto_ivgen;
71         /* XXX: uuid */
72 } dm_target_crypt_config_t;
73
74 struct dmtc_helper {
75         caddr_t free_addr;
76         caddr_t orig_buf;
77 };
78
79 static void dm_target_crypt_work(dm_target_crypt_config_t *priv, struct bio *bio);
80 static void dm_target_crypt_read_done(struct bio *bio);
81 static void dm_target_crypt_write_done(struct bio *bio);
82 static int dm_target_crypt_crypto_done_read(struct cryptop *crp);
83 static int dm_target_crypt_crypto_done_write(struct cryptop *crp);
84
85
86
87 static int
88 essiv_hash_mkey(dm_target_crypt_config_t *priv, char *iv_hash)
89 {
90         unsigned int klen;
91
92         klen = (priv->crypto_klen >> 3);
93
94         if (iv_hash == NULL)
95                 return EINVAL;
96
97         if (!strcmp(iv_hash, "sha1")) {
98                 SHA1_CTX ctx;
99
100                 if (klen != SHA1_RESULTLEN)
101                         return EINVAL;
102
103                 SHA1Init(&ctx);
104                 SHA1Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
105                 SHA1Final(priv->crypto_keyhash, &ctx);
106         } else if (!strcmp(iv_hash, "sha256")) {
107                 SHA256_CTX ctx;
108
109                 if (klen != SHA256_DIGEST_LENGTH)
110                         return EINVAL;
111
112                 SHA256_Init(&ctx);
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")) {
116                 SHA384_CTX ctx;
117
118                 if (klen != SHA384_DIGEST_LENGTH)
119                         return EINVAL;
120
121                 SHA384_Init(&ctx);
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")) {
125                 SHA512_CTX ctx;
126
127                 if (klen != SHA512_DIGEST_LENGTH)
128                         return EINVAL;
129
130                 SHA512_Init(&ctx);
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")) {
134                 MD5_CTX ctx;
135
136                 if (klen != MD5_DIGEST_LENGTH)
137                         return EINVAL;
138
139                 MD5Init(&ctx);
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")) {
143                 RMD160_CTX ctx;
144
145                 if (klen != (160/8))
146                         return EINVAL;
147
148                 RMD160Init(&ctx);
149                 RMD160Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
150                 RMD160Final(priv->crypto_keyhash, &ctx);
151         } else {
152                 return EINVAL;
153         }
154
155         return 0;
156 }
157
158 static int
159 essiv_ivgen_done(struct cryptop *crp)
160 {
161
162         if (crp->crp_etype == EAGAIN)
163                 return crypto_dispatch(crp);
164
165         if (crp->crp_etype != 0) {
166                 kprintf("essiv_ivgen_done, crp->crp_etype = %d\n", crp->crp_etype);
167         }
168
169         atomic_add_int((int *)crp->crp_opaque, 1);
170         wakeup(crp->crp_opaque);
171         return 0;
172 }
173
174 static void
175 plain_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv, size_t iv_len, off_t sector)
176 {
177         bzero(iv, iv_len);
178         *((off_t *)iv) = sector + priv->iv_offset;
179 }
180
181 static void
182 essiv_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv, size_t iv_len, off_t sector)
183 {
184         struct cryptodesc crd;
185         struct cryptop crp;
186         int error, id;
187
188         id = 0;
189         bzero(iv, iv_len);
190         *((off_t *)iv) = sector + priv->iv_offset;
191         crp.crp_buf = (caddr_t)iv;
192
193         crp.crp_sid = priv->crypto_sid;
194         crp.crp_ilen = crp.crp_olen = iv_len;
195
196         crp.crp_opaque = (void *)&id;
197
198         crp.crp_callback = essiv_ivgen_done;
199
200         crp.crp_desc = &crd;
201         crp.crp_etype = 0;
202         crp.crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
203
204         crd.crd_alg = priv->crypto_alg;
205         crd.crd_key = (caddr_t)priv->crypto_keyhash;
206         crd.crd_klen = priv->crypto_klen;
207
208         bzero(crd.crd_iv, sizeof(crd.crd_iv));
209
210         crd.crd_skip = 0;
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;
214         crd.crd_next = NULL;
215
216         error = crypto_dispatch(&crp);
217         if (error)
218                 kprintf("essiv_ivgen, error = %d\n", error);
219
220         /*
221          * id is modified in the callback, so that if crypto_dispatch finishes
222          * synchronously we don't tsleep() forever.
223          */
224         if (id == 0)
225                 tsleep((void *)&error, 0, "essivgen", 0);
226 }
227
228 #if 0
229 static void
230 geli_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv, size_t iv_len, off_t sector)
231 {
232
233         SHA512_CTX      ctx512;
234         u_int8_t        md[SHA512_DIGEST_LENGTH]; /* Max. Digest Size */
235
236         memcpy(&ctx512, &priv->essivsha512_ctx, sizeof(SHA512_CTX));
237         SHA512_Update(&ctx512, (u_int8_t*)&sector, sizeof(off_t));
238         SHA512_Final(md, &ctx512);
239
240         memcpy(iv, md, iv_len);
241 }
242 #endif
243
244 static void
245 dm_target_crypt_work(dm_target_crypt_config_t *priv, struct bio *bio)
246 {
247         struct dmtc_helper *dmtc;
248         struct cryptodesc *crd;
249         struct cryptop *crp;
250         struct cryptoini *cri;
251
252         int error, i, bytes, isector, sectors, write, sz;
253         u_char *ptr, *space, *data;
254
255         cri = &priv->crypto_session;
256
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));
262
263         if (write) {
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;
272         } else {
273                 space = kmalloc(sz, M_DMCRYPT, M_WAITOK);
274                 data = bio->bio_buf->b_data;
275                 bio->bio_caller_info2.ptr = space;
276         }
277
278         ptr = space;
279         bio->bio_caller_info3.value = sectors;
280         kprintf("Write? %d, bytes = %d (b_bcount), sectors = %d (bio = %p, b_cmd = %d)\n", write, bytes, sectors, bio, bio->bio_buf->b_cmd);
281
282         for (i = 0; i < sectors; i++) {
283                 crp = (struct cryptop *)ptr;
284                 ptr += sizeof(*crp);
285                 crd = (struct cryptodesc *)ptr;
286                 ptr += sizeof (*crd);
287
288                 crp->crp_buf = (data + i*DEV_BSIZE);
289
290                 crp->crp_sid = priv->crypto_sid;
291                 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
292
293                 crp->crp_opaque = (void *)bio;
294
295                 if (write)
296                         crp->crp_callback = dm_target_crypt_crypto_done_write;
297                 else
298                         crp->crp_callback = dm_target_crypt_crypto_done_read;
299                 crp->crp_desc = crd;
300                 crp->crp_etype = 0;
301                 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
302
303                 crd->crd_alg = priv->crypto_alg;
304                 crd->crd_key = (caddr_t)priv->crypto_key;
305                 crd->crd_klen = priv->crypto_klen;
306
307                 priv->crypto_ivgen(priv, crd->crd_iv, sizeof(crd->crd_iv), isector + i);
308
309                 crd->crd_skip = 0;
310                 crd->crd_len = DEV_BSIZE /* XXX */;
311                 crd->crd_flags = CRD_F_KEY_EXPLICIT | CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
312                 crd->crd_next = NULL;
313
314                 if (write)
315                         crd->crd_flags |= CRD_F_ENCRYPT;
316                 else
317                         crd->crd_flags &= ~CRD_F_ENCRYPT;
318
319                 error = crypto_dispatch(crp);
320         }
321 }
322
323 static void
324 dm_target_crypt_read_done(struct bio *bio)
325 {
326         dm_target_crypt_config_t *priv;
327
328         priv = bio->bio_caller_info1.ptr;
329         kprintf("dm_target_crypt_read_done %p\n", bio);
330
331         dm_target_crypt_work(priv, bio);
332 }
333
334 static void
335 dm_target_crypt_write_done(struct bio *bio)
336 {
337         struct dmtc_helper *dmtc;
338         struct bio *obio;
339
340         kprintf("dm_target_crypt_write_done %p\n", bio);
341         dmtc = bio->bio_caller_info2.ptr;
342         bio->bio_buf->b_data = dmtc->orig_buf;
343         kfree(dmtc->free_addr, M_DMCRYPT);
344         obio = pop_bio(bio);
345         biodone(obio);
346 }
347
348 static int
349 dm_target_crypt_crypto_done_read(struct cryptop *crp)
350 {
351         struct bio *bio, *obio;
352         int n;
353
354         if (crp->crp_etype == EAGAIN)
355                 return crypto_dispatch(crp);
356
357         bio = (struct bio *)crp->crp_opaque;
358         KKASSERT(bio != NULL);
359         
360         n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
361 #if 0
362         kprintf("dm_target_crypt_crypto_done_read %p, n = %d\n", bio, n);
363 #endif
364         if (crp->crp_etype != 0) {
365                 kprintf("dm_target_crypt_crypto_done_read crp_etype = %d\n", crp->crp_etype);
366                 /* XXX: Print something out */
367                 bio->bio_buf->b_error = crp->crp_etype; 
368         }
369         if (n == 1) {
370                 kprintf("dm_target_crypt_crypt_done_read: n == 1\n");
371                 kfree(bio->bio_caller_info2.ptr, M_DMCRYPT);
372                 /* This is the last chunk of the read */
373                 obio = pop_bio(bio);
374                 biodone(obio);
375         }
376
377         return 0;
378 }
379
380 static int
381 dm_target_crypt_crypto_done_write(struct cryptop *crp)
382 {
383         struct dmtc_helper *dmtc;
384         dm_target_crypt_config_t *priv;
385         struct bio *bio, *obio;
386         int n;
387
388         if (crp->crp_etype == EAGAIN)
389                 return crypto_dispatch(crp);
390
391         bio = (struct bio *)crp->crp_opaque;
392         KKASSERT(bio != NULL);
393         
394         n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
395 #if 0
396         kprintf("dm_target_crypt_crypto_done_write %p, n = %d\n", bio, n);
397 #endif
398         if (crp->crp_etype != 0) {
399                 kprintf("dm_target_crypt_crypto_done_write crp_etype = %d\n", crp->crp_etype);
400                 /* XXX: Print something out */
401                 bio->bio_buf->b_error = crp->crp_etype; 
402         }
403         if (n == 1) {
404                 kprintf("dm_target_crypt_crypt_done_write: n == 1\n");
405                 /* This is the last chunk of the write */
406                 if (bio->bio_buf->b_error != 0) {
407                         /* XXX */
408                         dmtc = bio->bio_caller_info2.ptr;
409                         bio->bio_buf->b_data = dmtc->orig_buf;
410                         kfree(dmtc->free_addr, M_DMCRYPT);
411                         obio = pop_bio(bio);
412                         biodone(obio);
413                 } else {
414                         priv = (dm_target_crypt_config_t *)bio->bio_caller_info1.ptr;
415                         vn_strategy(priv->pdev->pdev_vnode, bio);
416                 }
417         }
418
419         return 0;
420 }
421
422 /*
423 strategy -> read_done -> crypto_work -> crypto_done_read -> FINISH READ
424 strategy -> crypto_work -> crypto_done_write -> write dispatch -> write_done -> FINISH WRITE
425 */
426
427 #ifdef DM_TARGET_MODULE
428 /*
429  * Every target can be compiled directly to dm driver or as a
430  * separate module this part of target is used for loading targets
431  * to dm driver.
432  * Target can be unloaded from kernel only if there are no users of
433  * it e.g. there are no devices which uses that target.
434  */
435 #include <sys/kernel.h>
436 #include <sys/module.h>
437
438 static int
439 dm_target_crypt_modcmd(modcmd_t cmd, void *arg)
440 {
441         dm_target_t *dmt;
442         int r;
443         dmt = NULL;
444
445         switch (cmd) {
446         case MODULE_CMD_INIT:
447                 if ((dmt = dm_target_lookup("crypt")) != NULL) {
448                         dm_target_unbusy(dmt);
449                         return EEXIST;
450                 }
451                 dmt = dm_target_alloc("crypt");
452
453                 dmt->version[0] = 1;
454                 dmt->version[1] = 0;
455                 dmt->version[2] = 0;
456                 strlcpy(dmt->name, "crypt", DM_MAX_TYPE_NAME);
457                 dmt->init = &dm_target_crypt_init;
458                 dmt->status = &dm_target_crypt_status;
459                 dmt->strategy = &dm_target_crypt_strategy;
460                 dmt->deps = &dm_target_crypt_deps;
461                 dmt->destroy = &dm_target_crypt_destroy;
462                 dmt->upcall = &dm_target_crypt_upcall;
463
464                 r = dm_target_insert(dmt);
465
466                 break;
467
468         case MODULE_CMD_FINI:
469                 r = dm_target_rem("crypt");
470                 break;
471
472         case MODULE_CMD_STAT:
473                 return ENOTTY;
474
475         default:
476                 return ENOTTY;
477         }
478
479         return r;
480 }
481 #endif
482
483 /*
484  * Init function called from dm_table_load_ioctl.
485  * Accepted format:
486  * <device>     <crypto algorithm>[-<keysize>]  <iv generator>  <passphrase>
487  * /dev/foo     aes-256                         essiv           foobar
488  * cryptsetup actually passes us this:
489  * aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8
490  */
491
492 static int
493 hex2key(char *hex, size_t hex_length, u_int8_t *key)
494 {
495         char hex_buf[3];
496         size_t key_idx;
497
498         key_idx = 0;
499         bzero(hex_buf, sizeof(hex_buf));
500
501         for (; hex_length > 0; hex_length -= 2) {
502                 hex_buf[0] = *hex++;
503                 hex_buf[1] = *hex++;
504                 key[key_idx++] = (u_int8_t)strtoul(hex_buf, NULL, 16);
505         }
506
507         return 0;
508 }
509
510 int
511 dm_target_crypt_init(dm_dev_t * dmv, void **target_config, char *params)
512 {
513         dm_target_crypt_config_t *priv;
514         size_t len;
515         char **ap, *args[5];
516         char *crypto_alg, *crypto_mode, *iv_mode, *iv_opt, *key, *dev;
517         char *status_str;
518         int argc, klen, error;
519         uint64_t iv_offset, block_offset;
520
521         if (params == NULL)
522                 return EINVAL;
523
524         len = strlen(params) + 1;
525         argc = 0;
526
527         status_str = kstrdup(params, M_DMCRYPT);
528         /*
529          * Parse a string, containing tokens delimited by white space,
530          * into an argument vector
531          */
532         for (ap = args; ap < &args[5] &&
533             (*ap = strsep(&params, " \t")) != NULL;) {
534                 if (**ap != '\0') {
535                         argc++;
536                         ap++;
537                 }
538         }
539
540         kprintf("\nCrypto target init function called, argc = %d!!\n", argc);
541         if (argc != 5) {
542                 kprintf("not enough arguments for target crypt, need exactly 5\n");
543                 kfree(status_str, M_DMCRYPT);
544                 return ENOMEM; /* XXX */
545         }
546
547         crypto_alg = strsep(&args[0], "-");
548         crypto_mode = strsep(&args[0], "-");
549         iv_opt = strsep(&args[0], "-");
550         iv_mode = strsep(&iv_opt, ":");
551         key = args[1];
552         iv_offset = strtouq(args[2], NULL, 0);
553         dev = args[3];
554         block_offset = strtouq(args[4], NULL, 0);
555         /* bits / 8 = bytes, 1 byte = 2 hexa chars, so << 2 */
556         klen = strlen(key) << 2;
557
558         kprintf("crypto target - dev=%s, crypto_alg=%s, crypto_mode=%s, "
559                 "iv_mode=%s, iv_opt=%s, key=%s, iv_offset=%ju, block_offset=%ju\n",
560                 dev, crypto_alg, crypto_mode, iv_mode, iv_opt, key, iv_offset,
561                 block_offset);
562
563         if ((priv = kmalloc(sizeof(dm_target_crypt_config_t), M_DMCRYPT, M_NOWAIT))
564             == NULL) {
565                 kprintf("kmalloc in dm_target_crypt_init failed, M_NOWAIT to blame\n");
566                 kfree(status_str, M_DMCRYPT);
567                 return ENOMEM;
568         }
569
570         /* Insert dmp to global pdev list */
571         if ((priv->pdev = dm_pdev_insert(dev)) == NULL) {
572                 kprintf("dm_pdev_insert failed\n");
573                 kfree(status_str, M_DMCRYPT);
574                 return ENOENT;
575         }
576
577         if (strcmp(crypto_mode, "cbc") != 0) {
578                 kprintf("dm_target_crypt: only support 'cbc' chaining mode, invalid mode '%s'\n", crypto_mode);         
579                 goto notsup;
580         }
581
582         if (!strcmp(crypto_alg, "aes")) {
583                 priv->crypto_alg = CRYPTO_AES_CBC;
584                 if (klen != 128 && klen != 192 && klen != 256)
585                         goto notsup;
586                 priv->crypto_klen = klen;
587
588         } else if (!strcmp(crypto_alg, "blowfish")) {
589                 priv->crypto_alg = CRYPTO_BLF_CBC;
590                 if (klen < 128 || klen > 448 || (klen % 8) != 0)
591                         goto notsup;
592                 priv->crypto_klen = klen;
593
594         } else if (!strcmp(crypto_alg, "3des") || !strncmp(crypto_alg, "des3", 4)) {
595                 priv->crypto_alg = CRYPTO_3DES_CBC;
596                 if (klen != 168)
597                         goto notsup;
598                 priv->crypto_klen = 168;
599
600         } else if (!strcmp(crypto_alg, "camellia")) {
601                 priv->crypto_alg = CRYPTO_CAMELLIA_CBC;
602                 if (klen != 128 && klen != 192 && klen != 256)
603                         goto notsup;
604                 priv->crypto_klen = klen;
605
606         } else if (!strcmp(crypto_alg, "skipjack")) {
607                 priv->crypto_alg = CRYPTO_SKIPJACK_CBC;
608                 if (klen != 80)
609                         goto notsup;
610                 priv->crypto_klen = 80;
611
612         } else if (!strcmp(crypto_alg, "cast5")) {
613                 priv->crypto_alg = CRYPTO_CAST_CBC;
614                 if (klen != 128)
615                         goto notsup;
616                 priv->crypto_klen = 128;
617
618         } else if (!strcmp(crypto_alg, "null")) {
619                 priv->crypto_alg = CRYPTO_NULL_CBC;
620                 if (klen != 128)
621                         goto notsup;
622                 priv->crypto_klen = 128;
623
624         } else {
625                 kprintf("Unsupported crypto algorithm: %s\n", crypto_alg);
626                 goto notsup;
627         }
628
629         /* Save length of param string */
630         priv->params_len = len;
631         priv->block_offset = block_offset;
632         priv->iv_offset = iv_offset;
633
634         *target_config = priv;
635
636         dmv->dev_type = DM_CRYPTO_DEV;
637
638         priv->crypto_session.cri_alg = priv->crypto_alg;
639         priv->crypto_session.cri_klen = priv->crypto_klen;
640         priv->crypto_session.cri_mlen = 0;
641
642         error = hex2key(key, priv->crypto_klen >> 3, (u_int8_t *)priv->crypto_key);
643         if (error) {
644                 kprintf("hex2key failed!!\n");
645                 goto notsup;
646         }
647
648         kprintf("priv->crypto_klen >> 3 = %d\n", priv->crypto_klen >> 3);
649
650
651         if (!strcmp(iv_mode, "essiv")) {
652                 error = essiv_hash_mkey(priv, iv_opt);
653                 if (error) {
654                         kprintf("essiv_hash_mkey returned error!\n");
655                         goto notsup;            
656                 }
657                 priv->crypto_ivgen = essiv_ivgen;
658         } else if (!strcmp(iv_mode, "plain")) {
659                 priv->crypto_ivgen = plain_ivgen;
660         } else {
661                 kprintf("dm_target_crypt: only support iv_mode='essiv' and 'plain', iv_mode='%s' unsupported\n", iv_mode);
662         }
663
664         priv->crypto_session.cri_key = (u_int8_t *)priv->crypto_key;
665         priv->crypto_session.cri_next = NULL;
666
667         error = crypto_newsession(&priv->crypto_sid,
668                                   &priv->crypto_session,
669                                   CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
670         if (error) {
671                 kprintf("Error during crypto_newsession, error = %d\n", error);
672                 goto notsup;
673         }
674
675         priv->status_str = status_str;
676         return 0;
677
678 notsup:
679         kprintf("returning ENOTSUP from crypt_init thingie... notsup label\n");
680         kfree(status_str, M_DMCRYPT);
681         return ENOTSUP;
682 }
683
684 /* Status routine called to get params string. */
685 char *
686 dm_target_crypt_status(void *target_config)
687 {
688         dm_target_crypt_config_t *priv;
689         char *params;
690
691         priv = target_config;
692
693         if ((params = kmalloc(DM_MAX_PARAMS_SIZE, M_DMCRYPT, M_WAITOK)) == NULL)
694                 return NULL;
695
696         ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s",
697             priv->pdev->name);
698
699         return params;
700 }
701
702 /* Strategy routine called from dm_strategy. */
703 /*
704  * Do IO operation, called from dmstrategy routine.
705  */
706 int
707 dm_target_crypt_strategy(dm_table_entry_t * table_en, struct buf * bp)
708 {
709         struct bio *bio;
710
711         dm_target_crypt_config_t *priv;
712         priv = table_en->target_config;
713
714         /* Get rid of stuff we can't really handle */
715         if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
716                 if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
717                         kprintf("dm_target_crypt_strategy: can't really handle bp->b_bcount = %d\n", bp->b_bcount);
718                         bp->b_error = EINVAL;
719                         bp->b_flags |= B_ERROR | B_INVAL;
720                         biodone(&bp->b_bio1);
721                         return 0;
722                 }
723         }
724
725         switch (bp->b_cmd) {
726         case BUF_CMD_READ:
727                 bio = push_bio(&bp->b_bio1);
728                 bio->bio_offset = bp->b_bio1.bio_offset + priv->block_offset*DEV_BSIZE;
729                 bio->bio_caller_info1.ptr = priv;
730                 bio->bio_done = dm_target_crypt_read_done;
731                 vn_strategy(priv->pdev->pdev_vnode, bio);
732                 break;
733
734         case BUF_CMD_WRITE:
735                 bio = push_bio(&bp->b_bio1);
736                 bio->bio_offset = bp->b_bio1.bio_offset + priv->block_offset*DEV_BSIZE;
737                 bio->bio_caller_info1.ptr = priv;
738                 bio->bio_done = dm_target_crypt_write_done;
739                 dm_target_crypt_work(priv, bio);
740                 break;
741
742         default:
743                 vn_strategy(priv->pdev->pdev_vnode, &bp->b_bio1);               
744         }
745
746         return 0;
747
748 }
749
750 int
751 dm_target_crypt_destroy(dm_table_entry_t * table_en)
752 {
753         dm_target_crypt_config_t *priv;
754
755         priv = table_en->target_config;
756
757         if (priv == NULL)
758                 return 0;
759
760         dm_pdev_decr(priv->pdev);
761
762         /* Unbusy target so we can unload it */
763         dm_target_unbusy(table_en->target);
764
765         kfree(priv->status_str, M_DMCRYPT);
766         kfree(priv, M_DMCRYPT);
767
768         table_en->target_config = NULL;
769
770         return 0;
771 }
772
773 int
774 dm_target_crypt_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
775 {
776         dm_target_crypt_config_t *priv;
777         struct vattr va;
778
779         int error;
780
781         if (table_en->target_config == NULL)
782                 return ENOENT;
783
784         priv = table_en->target_config;
785
786         if ((error = VOP_GETATTR(priv->pdev->pdev_vnode, &va)) != 0)
787                 return error;
788
789         prop_array_add_uint64(prop_array, (uint64_t) makeudev(va.va_rmajor, va.va_rminor));
790
791         return 0;
792 }
793
794 /* Unsupported for this target. */
795 int
796 dm_target_crypt_upcall(dm_table_entry_t * table_en, struct buf * bp)
797 {
798         return 0;
799 }