dm_target_crypt - Fix compatibility with Linux
[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 #include <sys/endian.h>
41
42 #include <sys/bio.h>
43 #include <sys/buf.h>
44 #include <sys/malloc.h>
45 #include <sys/md5.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>
51 #include <machine/cpufunc.h>
52
53 #include "dm.h"
54 MALLOC_DEFINE(M_DMCRYPT, "dm_crypt", "Device Mapper Target Crypt");
55
56 struct target_crypt_config;
57
58 typedef void dispatch_t(void *);
59 typedef void ivgen_t(struct target_crypt_config *, u_int8_t *, size_t, off_t,
60     void *);
61
62 typedef int ivgen_ctor_t(struct target_crypt_config *, char *, void **);
63 typedef int ivgen_dtor_t(struct target_crypt_config *, void *);
64
65 struct iv_generator {
66         const char      *name;
67         ivgen_ctor_t    *ctor;
68         ivgen_dtor_t    *dtor;
69         ivgen_t         *gen_iv;
70 };
71
72 struct essiv_ivgen_priv {
73         struct cryptoini        crypto_session;
74         struct objcache *crp_crd_cache;
75         u_int64_t       crypto_sid;
76         size_t          keyhash_len;
77         u_int8_t        crypto_keyhash[SHA512_DIGEST_LENGTH];
78 };
79
80 typedef struct target_crypt_config {
81         size_t  params_len;
82         dm_pdev_t *pdev;
83         char    *status_str;
84         int     crypto_alg;
85         int     crypto_klen;
86         u_int8_t        crypto_key[512>>3];
87         
88         u_int64_t       crypto_sid;
89         u_int64_t       block_offset;
90         u_int64_t       iv_offset;
91         SHA512_CTX      essivsha512_ctx;
92
93         struct cryptoini        crypto_session;
94
95         struct iv_generator     *ivgen;
96         void    *ivgen_priv;
97 } dm_target_crypt_config_t;
98
99 struct dmtc_helper {
100         caddr_t free_addr;
101         caddr_t orig_buf;
102         caddr_t data_buf;
103 };
104
105 static void dmtc_crypto_dispatch(void *arg);
106 static void dmtc_crypto_read_start(dm_target_crypt_config_t *priv,
107                                 struct bio *bio);
108 static void dmtc_crypto_write_start(dm_target_crypt_config_t *priv,
109                                 struct bio *bio);
110 static void dmtc_bio_read_done(struct bio *bio);
111 static void dmtc_bio_write_done(struct bio *bio);
112 static int dmtc_crypto_cb_read_done(struct cryptop *crp);
113 static int dmtc_crypto_cb_write_done(struct cryptop *crp);
114
115 static ivgen_ctor_t     essiv_ivgen_ctor;
116 static ivgen_dtor_t     essiv_ivgen_dtor;
117 static ivgen_t          essiv_ivgen;
118 static ivgen_t          plain_ivgen;
119
120 static struct iv_generator ivgens[] = {
121         { .name = "essiv", .ctor = essiv_ivgen_ctor, .dtor = essiv_ivgen_dtor,
122             .gen_iv = essiv_ivgen },
123         { .name = "plain", .ctor = NULL, .dtor = NULL, .gen_iv = plain_ivgen },
124         { NULL, NULL, NULL, NULL }
125 };
126
127 struct objcache_malloc_args essiv_ivgen_malloc_args = {
128                 2*sizeof(void *) + (sizeof(struct cryptodesc) +
129                 sizeof(struct cryptop)), M_DMCRYPT };
130
131 /*
132  * Overwrite private information (in buf) to avoid leaking it
133  */
134 static void
135 dmtc_crypto_clear(void *buf, size_t len)
136 {
137         memset(buf, 0xFF, len);
138         bzero(buf, len);
139 }
140
141 /*
142  * ESSIV IV Generator Routines
143  */
144 static int 
145 essiv_ivgen_ctor(struct target_crypt_config *priv, char *iv_hash, void **p_ivpriv)
146 {
147         struct essiv_ivgen_priv *ivpriv;
148         u_int8_t crypto_keyhash[SHA512_DIGEST_LENGTH];
149         unsigned int klen;
150         int error;
151
152         klen = (priv->crypto_klen >> 3);
153
154         if (iv_hash == NULL)
155                 return EINVAL;
156
157         if (!strcmp(iv_hash, "sha1")) {
158                 SHA1_CTX ctx;
159
160                 if (klen != SHA1_RESULTLEN)
161                         return EINVAL;
162
163                 SHA1Init(&ctx);
164                 SHA1Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
165                 SHA1Final(crypto_keyhash, &ctx);
166         } else if (!strcmp(iv_hash, "sha256")) {
167                 SHA256_CTX ctx;
168
169                 if (klen != SHA256_DIGEST_LENGTH)
170                         return EINVAL;
171
172                 SHA256_Init(&ctx);
173                 SHA256_Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
174                 SHA256_Final(crypto_keyhash, &ctx);
175         } else if (!strcmp(iv_hash, "sha384")) {
176                 SHA384_CTX ctx;
177
178                 if (klen != SHA384_DIGEST_LENGTH)
179                         return EINVAL;
180
181                 SHA384_Init(&ctx);
182                 SHA384_Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
183                 SHA384_Final(crypto_keyhash, &ctx);
184         } else if (!strcmp(iv_hash, "sha512")) {
185                 SHA512_CTX ctx;
186
187                 if (klen != SHA512_DIGEST_LENGTH)
188                         return EINVAL;
189
190                 SHA512_Init(&ctx);
191                 SHA512_Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
192                 SHA512_Final(crypto_keyhash, &ctx);
193         } else if (!strcmp(iv_hash, "md5")) {
194                 MD5_CTX ctx;
195
196                 if (klen != MD5_DIGEST_LENGTH)
197                         return EINVAL;
198
199                 MD5Init(&ctx);
200                 MD5Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
201                 MD5Final(crypto_keyhash, &ctx);
202         } else if (!strcmp(iv_hash, "rmd160") ||
203                    !strcmp(iv_hash, "ripemd160")) {
204                 RMD160_CTX ctx;
205
206                 if (klen != (160/8))
207                         return EINVAL;
208
209                 RMD160Init(&ctx);
210                 RMD160Update(&ctx, priv->crypto_key, priv->crypto_klen>>3);
211                 RMD160Final(crypto_keyhash, &ctx);
212         } else {
213                 return EINVAL;
214         }
215
216         ivpriv = kmalloc(sizeof(struct essiv_ivgen_priv), M_DMCRYPT,
217             M_WAITOK | M_ZERO);
218         memcpy(ivpriv->crypto_keyhash, crypto_keyhash, sizeof(crypto_keyhash));
219         ivpriv->keyhash_len = sizeof(crypto_keyhash);
220         dmtc_crypto_clear(crypto_keyhash, sizeof(crypto_keyhash));
221
222         ivpriv->crypto_session.cri_alg = priv->crypto_alg;
223         ivpriv->crypto_session.cri_key = (u_int8_t *)ivpriv->crypto_keyhash;
224         ivpriv->crypto_session.cri_klen = priv->crypto_klen;
225         ivpriv->crypto_session.cri_mlen = 0;
226         ivpriv->crypto_session.cri_next = NULL;
227
228         error = crypto_newsession(&ivpriv->crypto_sid,
229                                   &ivpriv->crypto_session,
230                                   CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
231         if (error) {
232                 kprintf("dm_target_crypt: Error during crypto_newsession "
233                         "for essiv_ivgen, error = %d\n",
234                         error);
235                 dmtc_crypto_clear(ivpriv->crypto_keyhash, ivpriv->keyhash_len);
236                 kfree(ivpriv, M_DMCRYPT);
237                 return ENOTSUP;
238         }
239
240         ivpriv->crp_crd_cache = objcache_create(
241             "dmcrypt-essiv-cache", 0, 0,
242             NULL, NULL, NULL,
243             objcache_malloc_alloc,
244             objcache_malloc_free,
245             &essiv_ivgen_malloc_args );
246
247         *p_ivpriv = ivpriv;
248         return 0;
249 }
250
251 static int 
252 essiv_ivgen_dtor(struct target_crypt_config *priv, void *arg)
253 {
254         struct essiv_ivgen_priv *ivpriv;
255
256         ivpriv = (struct essiv_ivgen_priv *)arg;
257         KKASSERT(ivpriv != NULL);
258
259         crypto_freesession(ivpriv->crypto_sid);
260
261         objcache_destroy(ivpriv->crp_crd_cache);
262
263         dmtc_crypto_clear(ivpriv->crypto_keyhash, ivpriv->keyhash_len);
264         kfree(ivpriv, M_DMCRYPT);
265
266         return 0;
267 }
268
269 static int
270 essiv_ivgen_done(struct cryptop *crp)
271 {
272         struct essiv_ivgen_priv *ivpriv;
273         void *free_addr;
274         void *opaque;
275
276         if (crp->crp_etype == EAGAIN)
277                 return crypto_dispatch(crp);
278
279         if (crp->crp_etype != 0) {
280                 kprintf("dm_target_crypt: essiv_ivgen_done, "
281                         "crp->crp_etype = %d\n", crp->crp_etype);
282         }
283
284         free_addr = crp->crp_opaque;
285         /*
286          * In-memory structure is:
287          * |  ivpriv  |  opaque  |     crp     |      crd      |
288          * | (void *) | (void *) |   (cryptop) |  (cryptodesc) |
289          */
290         ivpriv = *((struct essiv_ivgen_priv **)crp->crp_opaque);
291         crp->crp_opaque += sizeof(void *);
292         opaque = *((void **)crp->crp_opaque);
293         
294         objcache_put(ivpriv->crp_crd_cache, free_addr);
295         dmtc_crypto_dispatch(opaque);
296         return 0;
297 }
298
299 static void
300 essiv_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
301             size_t iv_len, off_t sector, void *opaque)
302 {
303         struct essiv_ivgen_priv *ivpriv;
304         struct cryptodesc *crd;
305         struct cryptop *crp;
306         caddr_t space, alloc_addr;
307         int error;
308
309         ivpriv = priv->ivgen_priv;
310         KKASSERT(ivpriv != NULL);
311
312         /*
313          * In-memory structure is:
314          * |  ivpriv  |  opaque  |     crp     |      crd      |
315          * | (void *) | (void *) |   (cryptop) |  (cryptodesc) |
316          */
317         alloc_addr = space = objcache_get(ivpriv->crp_crd_cache, M_WAITOK);
318         *((struct essiv_ivgen_priv **)space) = ivpriv;
319         space += sizeof(void *);
320         *((void **)space) = opaque;
321         space += sizeof(void *);
322         crp = (struct cryptop *)space;
323         space += sizeof(struct cryptop);
324         crd = (struct cryptodesc *)space;
325
326         bzero(iv, iv_len);
327         bzero(crd, sizeof(struct cryptodesc));
328         bzero(crp, sizeof(struct cryptop));
329         *((off_t *)iv) = htole64(sector + priv->iv_offset);
330         crp->crp_buf = (caddr_t)iv;
331
332         crp->crp_sid = ivpriv->crypto_sid;
333         crp->crp_ilen = crp->crp_olen = iv_len;
334
335         crp->crp_opaque = alloc_addr;
336
337         crp->crp_callback = essiv_ivgen_done;
338
339         crp->crp_desc = crd;
340         crp->crp_etype = 0;
341         crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL | CRYPTO_F_BATCH;
342
343         crd->crd_alg = priv->crypto_alg;
344 #if 0
345         crd->crd_key = (caddr_t)priv->crypto_keyhash;
346         crd->crd_klen = priv->crypto_klen;
347 #endif
348
349         bzero(crd->crd_iv, sizeof(crd->crd_iv));
350
351         crd->crd_skip = 0;
352         crd->crd_len = iv_len;
353         crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
354         crd->crd_flags |= CRD_F_ENCRYPT;
355         crd->crd_next = NULL;
356
357         error = crypto_dispatch(crp);
358         if (error)
359                 kprintf("dm_target_crypt: essiv_ivgen, error = %d\n", error);
360 }
361
362
363 static void
364 plain_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
365             size_t iv_len, off_t sector, void *opaque)
366 {
367         bzero(iv, iv_len);
368         *((uint32_t *)iv) = htole32((uint32_t)(sector + priv->iv_offset));
369         dmtc_crypto_dispatch(opaque);
370 }
371
372
373 #if 0
374 static void
375 geli_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
376            size_t iv_len, off_t sector, void *opaque)
377 {
378
379         SHA512_CTX      ctx512;
380         u_int8_t        md[SHA512_DIGEST_LENGTH]; /* Max. Digest Size */
381
382         memcpy(&ctx512, &priv->essivsha512_ctx, sizeof(SHA512_CTX));
383         SHA512_Update(&ctx512, (u_int8_t*)&sector, sizeof(off_t));
384         SHA512_Final(md, &ctx512);
385
386         memcpy(iv, md, iv_len);
387         dmtc_crypto_dispatch(opaque);
388 }
389 #endif
390
391 #ifdef DM_TARGET_MODULE
392 /*
393  * Every target can be compiled directly to dm driver or as a
394  * separate module this part of target is used for loading targets
395  * to dm driver.
396  * Target can be unloaded from kernel only if there are no users of
397  * it e.g. there are no devices which uses that target.
398  */
399 #include <sys/kernel.h>
400 #include <sys/module.h>
401
402 static int
403 dm_target_crypt_modcmd(modcmd_t cmd, void *arg)
404 {
405         dm_target_t *dmt;
406         int r;
407         dmt = NULL;
408
409         switch (cmd) {
410         case MODULE_CMD_INIT:
411                 if ((dmt = dm_target_lookup("crypt")) != NULL) {
412                         dm_target_unbusy(dmt);
413                         return EEXIST;
414                 }
415                 dmt = dm_target_alloc("crypt");
416
417                 dmt->version[0] = 1;
418                 dmt->version[1] = 0;
419                 dmt->version[2] = 0;
420                 strlcpy(dmt->name, "crypt", DM_MAX_TYPE_NAME);
421                 dmt->init = &dm_target_crypt_init;
422                 dmt->status = &dm_target_crypt_status;
423                 dmt->strategy = &dm_target_crypt_strategy;
424                 dmt->deps = &dm_target_crypt_deps;
425                 dmt->destroy = &dm_target_crypt_destroy;
426                 dmt->upcall = &dm_target_crypt_upcall;
427
428                 r = dm_target_insert(dmt);
429
430                 break;
431
432         case MODULE_CMD_FINI:
433                 r = dm_target_rem("crypt");
434                 break;
435
436         case MODULE_CMD_STAT:
437                 return ENOTTY;
438
439         default:
440                 return ENOTTY;
441         }
442
443         return r;
444 }
445
446 #endif
447
448 /*
449  * Init function called from dm_table_load_ioctl.
450  * cryptsetup actually passes us this:
451  * aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8
452  */
453 static int
454 hex2key(char *hex, size_t key_len, u_int8_t *key)
455 {
456         char hex_buf[3];
457         size_t key_idx;
458
459         hex_buf[2] = 0;
460         for (key_idx = 0; key_idx < key_len; ++key_idx) {
461                 hex_buf[0] = *hex++;
462                 hex_buf[1] = *hex++;
463                 key[key_idx] = (u_int8_t)strtoul(hex_buf, NULL, 16);
464         }
465         hex_buf[0] = 0;
466         hex_buf[1] = 0;
467
468         return 0;
469 }
470
471 int
472 dm_target_crypt_init(dm_dev_t * dmv, void **target_config, char *params)
473 {
474         dm_target_crypt_config_t *priv;
475         size_t len;
476         char **ap, *args[5];
477         char *crypto_alg, *crypto_mode, *iv_mode, *iv_opt, *key, *dev;
478         char *status_str;
479         int i, argc, klen, error;
480         uint64_t iv_offset, block_offset;
481
482         if (params == NULL)
483                 return EINVAL;
484
485         len = strlen(params) + 1;
486         argc = 0;
487
488         status_str = kstrdup(params, M_DMCRYPT);
489         /*
490          * Parse a string, containing tokens delimited by white space,
491          * into an argument vector
492          */
493         for (ap = args; ap < &args[5] &&
494             (*ap = strsep(&params, " \t")) != NULL;) {
495                 if (**ap != '\0') {
496                         argc++;
497                         ap++;
498                 }
499         }
500
501         if (argc != 5) {
502                 kprintf("dm_target_crypt: not enough arguments, "
503                         "need exactly 5\n");
504                 kfree(status_str, M_DMCRYPT);
505                 return ENOMEM; /* XXX */
506         }
507
508         crypto_alg = strsep(&args[0], "-");
509         crypto_mode = strsep(&args[0], "-");
510         iv_opt = strsep(&args[0], "-");
511         iv_mode = strsep(&iv_opt, ":");
512         key = args[1];
513         iv_offset = strtouq(args[2], NULL, 0);
514         dev = args[3];
515         block_offset = strtouq(args[4], NULL, 0);
516         /* bits / 8 = bytes, 1 byte = 2 hexa chars, so << 2 */
517         klen = strlen(key) << 2;
518
519         kprintf("dm_target_crypt - new: dev=%s, crypto_alg=%s, crypto_mode=%s, "
520                 "iv_mode=%s, iv_opt=%s, key=%s, iv_offset=%ju, "
521                 "block_offset=%ju\n",
522                 dev, crypto_alg, crypto_mode, iv_mode, iv_opt, key, iv_offset,
523                 block_offset);
524
525         priv = kmalloc(sizeof(dm_target_crypt_config_t), M_DMCRYPT, M_WAITOK);
526         if (priv == NULL) {
527                 kprintf("dm_target_crypt: could not allocate memory\n");
528                 kfree(status_str, M_DMCRYPT);
529                 return ENOMEM;
530         }
531
532         /* Insert dmp to global pdev list */
533         if ((priv->pdev = dm_pdev_insert(dev)) == NULL) {
534                 kprintf("dm_target_crypt: dm_pdev_insert failed\n");
535                 kfree(status_str, M_DMCRYPT);
536                 return ENOENT;
537         }
538
539         if (strcmp(crypto_mode, "cbc") != 0) {
540                 kprintf("dm_target_crypt: only support 'cbc' chaining mode, "
541                         "invalid mode '%s'\n", crypto_mode);
542                 goto notsup;
543         }
544
545         if (!strcmp(crypto_alg, "aes")) {
546                 priv->crypto_alg = CRYPTO_AES_CBC;
547                 if (klen != 128 && klen != 192 && klen != 256)
548                         goto notsup;
549                 priv->crypto_klen = klen;
550         } else if (!strcmp(crypto_alg, "blowfish")) {
551                 priv->crypto_alg = CRYPTO_BLF_CBC;
552                 if (klen < 128 || klen > 448 || (klen % 8) != 0)
553                         goto notsup;
554                 priv->crypto_klen = klen;
555
556         } else if (!strcmp(crypto_alg, "3des") ||
557                    !strncmp(crypto_alg, "des3", 4)) {
558                 priv->crypto_alg = CRYPTO_3DES_CBC;
559                 if (klen != 168)
560                         goto notsup;
561                 priv->crypto_klen = 168;
562         } else if (!strcmp(crypto_alg, "camellia")) {
563                 priv->crypto_alg = CRYPTO_CAMELLIA_CBC;
564                 if (klen != 128 && klen != 192 && klen != 256)
565                         goto notsup;
566                 priv->crypto_klen = klen;
567         } else if (!strcmp(crypto_alg, "skipjack")) {
568                 priv->crypto_alg = CRYPTO_SKIPJACK_CBC;
569                 if (klen != 80)
570                         goto notsup;
571                 priv->crypto_klen = 80;
572         } else if (!strcmp(crypto_alg, "cast5")) {
573                 priv->crypto_alg = CRYPTO_CAST_CBC;
574                 if (klen != 128)
575                         goto notsup;
576                 priv->crypto_klen = 128;
577         } else if (!strcmp(crypto_alg, "null")) {
578                 priv->crypto_alg = CRYPTO_NULL_CBC;
579                 if (klen != 128)
580                         goto notsup;
581                 priv->crypto_klen = 128;
582         } else {
583                 kprintf("dm_target_crypt: Unsupported crypto algorithm: %s\n",
584                         crypto_alg);
585                 goto notsup;
586         }
587
588         /* Save length of param string */
589         priv->params_len = len;
590         priv->block_offset = block_offset;
591         priv->iv_offset = iv_offset - block_offset;
592
593         *target_config = priv;
594
595         dmv->dev_type = DM_CRYPTO_DEV;
596
597         error = hex2key(key, priv->crypto_klen >> 3,
598                         (u_int8_t *)priv->crypto_key);
599
600         if (error) {
601                 kprintf("dm_target_crypt: hex2key failed, "
602                         "invalid key format\n");
603                 goto notsup;
604         }
605
606         /* Handle cmd */
607         for(i = 0; ivgens[i].name != NULL; i++) {
608                 if (!strcmp(iv_mode, ivgens[i].name))
609                         break;
610         }
611
612         if (ivgens[i].name == NULL) {
613                 kprintf("dm_target_crypt: iv_mode='%s' unsupported\n",
614                         iv_mode);       
615                 goto notsup;
616         }
617
618         /* Call our ivgen constructor */
619         if (ivgens[i].ctor != NULL) {
620                 error = ivgens[i].ctor(priv, iv_opt,
621                     &priv->ivgen_priv);
622                 if (error) {
623                         kprintf("dm_target_crypt: ctor for '%s' failed\n",
624                             ivgens[i].name);
625                         goto notsup;
626                 }
627         }
628
629         priv->ivgen = &ivgens[i];
630
631         priv->crypto_session.cri_alg = priv->crypto_alg;
632         priv->crypto_session.cri_key = (u_int8_t *)priv->crypto_key;
633         priv->crypto_session.cri_klen = priv->crypto_klen;
634         priv->crypto_session.cri_mlen = 0;
635         priv->crypto_session.cri_next = NULL;
636
637         error = crypto_newsession(&priv->crypto_sid,
638                                   &priv->crypto_session,
639                                   CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
640         if (error) {
641                 kprintf("dm_target_crypt: Error during crypto_newsession, "
642                         "error = %d\n",
643                         error);
644                 goto notsup;
645         }
646
647         priv->status_str = status_str;
648         return 0;
649
650 notsup:
651         kprintf("dm_target_crypt: ENOTSUP\n");
652         kfree(status_str, M_DMCRYPT);
653         return ENOTSUP;
654 }
655
656 /* Status routine called to get params string. */
657 char *
658 dm_target_crypt_status(void *target_config)
659 {
660         dm_target_crypt_config_t *priv;
661         char *params;
662
663         priv = target_config;
664
665         /* caller expects use of M_DM */
666         params = kmalloc(DM_MAX_PARAMS_SIZE, M_DM, M_WAITOK);
667
668         ksnprintf(params, DM_MAX_PARAMS_SIZE, "%s",
669             priv->status_str);
670
671         return params;
672 }
673
674 int
675 dm_target_crypt_destroy(dm_table_entry_t * table_en)
676 {
677         dm_target_crypt_config_t *priv;
678
679         /*
680          * Disconnect the crypt config before unbusying the target.
681          */
682         priv = table_en->target_config;
683         if (priv == NULL)
684                 return 0;
685         table_en->target_config = NULL;
686         dm_pdev_decr(priv->pdev);
687
688         dm_target_unbusy(table_en->target);
689
690         /*
691          * Clean up the crypt config
692          *
693          * Overwrite the private information before freeing memory to
694          * avoid leaking it.
695          */
696         dmtc_crypto_clear(priv->status_str, strlen(priv->status_str));
697         kfree(priv->status_str, M_DMCRYPT);
698
699         crypto_freesession(priv->crypto_sid);
700
701         if (priv->ivgen->dtor != NULL) {
702                 priv->ivgen->dtor(priv, priv->ivgen_priv);
703         }
704
705         dmtc_crypto_clear(priv, sizeof(dm_target_crypt_config_t));
706         kfree(priv, M_DMCRYPT);
707
708         return 0;
709 }
710
711 int
712 dm_target_crypt_deps(dm_table_entry_t * table_en, prop_array_t prop_array)
713 {
714         dm_target_crypt_config_t *priv;
715         struct vattr va;
716
717         int error;
718
719         if (table_en->target_config == NULL)
720                 return ENOENT;
721
722         priv = table_en->target_config;
723
724         if ((error = VOP_GETATTR(priv->pdev->pdev_vnode, &va)) != 0)
725                 return error;
726
727         prop_array_add_uint64(prop_array,
728                               (uint64_t)makeudev(va.va_rmajor, va.va_rminor));
729
730         return 0;
731 }
732
733 /* Unsupported for this target. */
734 int
735 dm_target_crypt_upcall(dm_table_entry_t * table_en, struct buf * bp)
736 {
737         return 0;
738 }
739
740 /************************************************************************
741  *                      STRATEGY SUPPORT FUNCTIONS                      *
742  ************************************************************************
743  *
744  * READ PATH:   doio -> bio_read_done -> crypto_work -> crypto_cb_read_done
745  * WRITE PATH:  crypto_work -> crypto_cb_write_done -> doio -> bio_write_done
746  */
747
748 /*
749  * Wrapper around crypto_dispatch() to match dispatch_t type
750  */
751 static void
752 dmtc_crypto_dispatch(void *arg)
753 {
754         struct cryptop *crp;
755
756         crp = (struct cryptop *)arg;
757         KKASSERT(crp != NULL);
758         crypto_dispatch(crp);
759 }
760  
761 /*
762  * Start IO operation, called from dmstrategy routine.
763  */
764 int
765 dm_target_crypt_strategy(dm_table_entry_t *table_en, struct buf *bp)
766 {
767         struct bio *bio;
768
769         dm_target_crypt_config_t *priv;
770         priv = table_en->target_config;
771
772         /* Get rid of stuff we can't really handle */
773         if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
774                 if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
775                         kprintf("dm_target_crypt_strategy: can't really "
776                                 "handle bp->b_bcount = %d\n",
777                                 bp->b_bcount);
778                         bp->b_error = EINVAL;
779                         bp->b_flags |= B_ERROR | B_INVAL;
780                         biodone(&bp->b_bio1);
781                         return 0;
782                 }
783         }
784
785         switch (bp->b_cmd) {
786         case BUF_CMD_READ:
787                 bio = push_bio(&bp->b_bio1);
788                 bio->bio_offset = bp->b_bio1.bio_offset +
789                                   priv->block_offset * DEV_BSIZE;
790                 bio->bio_caller_info1.ptr = priv;
791                 bio->bio_done = dmtc_bio_read_done;
792                 vn_strategy(priv->pdev->pdev_vnode, bio);
793                 break;
794         case BUF_CMD_WRITE:
795                 bio = push_bio(&bp->b_bio1);
796                 bio->bio_offset = bp->b_bio1.bio_offset +
797                                   priv->block_offset * DEV_BSIZE;
798                 bio->bio_caller_info1.ptr = priv;
799                 dmtc_crypto_write_start(priv, bio);
800                 break;
801         default:
802                 vn_strategy(priv->pdev->pdev_vnode, &bp->b_bio1);
803                 break;
804         }
805         return 0;
806 }
807
808 /*
809  * STRATEGY READ PATH PART 1/3 (after read BIO completes)
810  */
811 static void
812 dmtc_bio_read_done(struct bio *bio)
813 {
814         struct bio *obio;
815
816         dm_target_crypt_config_t *priv;
817
818         /*
819          * If a read error occurs we shortcut the operation, otherwise
820          * go on to stage 2.
821          */
822         if (bio->bio_buf->b_flags & B_ERROR) {
823                 obio = pop_bio(bio);
824                 biodone(obio);
825         } else {
826                 priv = bio->bio_caller_info1.ptr;
827                 dmtc_crypto_read_start(priv, bio);
828         }
829 }
830
831 /*
832  * STRATEGY READ PATH PART 2/3
833  */
834 static void
835 dmtc_crypto_read_start(dm_target_crypt_config_t *priv, struct bio *bio)
836 {
837         struct dmtc_helper *dmtc;
838         struct cryptodesc *crd;
839         struct cryptop *crp;
840         struct cryptoini *cri;
841         int i, bytes, sectors, sz;
842         off_t isector;
843         u_char *ptr, *space;
844
845         cri = &priv->crypto_session;
846
847         /*
848          * Note: b_resid no good after read I/O, it will be 0, use
849          *       b_bcount.
850          */
851         bytes = bio->bio_buf->b_bcount;
852         isector = bio->bio_offset / DEV_BSIZE;  /* ivgen salt base? */
853         sectors = bytes / DEV_BSIZE;            /* Number of sectors */
854         sz = sectors * (sizeof(*crp) + sizeof(*crd));
855
856         /*
857          * For reads with bogus page we can't decrypt in place as stuff
858          * can get ripped out from under us.
859          *
860          * XXX actually it looks like we can, and in any case the initial
861          * read already completed and threw crypted data into the buffer
862          * cache buffer.  Disable for now.
863          */
864 #if 0
865         if (bio->bio_buf->b_flags & B_HASBOGUS) {
866                 space = kmalloc(sizeof(struct dmtc_helper) + sz + bytes,
867                                 M_DMCRYPT, M_WAITOK);
868                 dmtc = (struct dmtc_helper *)space;
869                 dmtc->free_addr = space;
870                 space += sizeof(struct dmtc_helper);
871                 dmtc->orig_buf = NULL;
872                 dmtc->data_buf = space + sz;
873                 memcpy(dmtc->data_buf, bio->bio_buf->b_data, bytes);
874         } else
875 #endif
876         {
877                 space = kmalloc(sizeof(struct dmtc_helper) + sz,
878                                 M_DMCRYPT, M_WAITOK);
879                 dmtc = (struct dmtc_helper *)space;
880                 dmtc->free_addr = space;
881                 space += sizeof(struct dmtc_helper);
882                 dmtc->orig_buf = NULL;
883                 dmtc->data_buf = bio->bio_buf->b_data;
884         }
885         bio->bio_caller_info2.ptr = dmtc;
886         bio->bio_buf->b_error = 0;
887
888         /*
889          * Load crypto descriptors (crp/crd loop)
890          */
891         bzero(space, sz);
892         ptr = space;
893         bio->bio_caller_info3.value = sectors;
894         cpu_sfence();
895 #if 0
896         kprintf("Read, bytes = %d (b_bcount), "
897                 "sectors = %d (bio = %p, b_cmd = %d)\n",
898                 bytes, sectors, bio, bio->bio_buf->b_cmd);
899 #endif
900         for (i = 0; i < sectors; i++) {
901                 crp = (struct cryptop *)ptr;
902                 ptr += sizeof(*crp);
903                 crd = (struct cryptodesc *)ptr;
904                 ptr += sizeof (*crd);
905
906                 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
907
908                 crp->crp_sid = priv->crypto_sid;
909                 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
910
911                 crp->crp_opaque = (void *)bio;
912
913                 crp->crp_callback = dmtc_crypto_cb_read_done;
914                 crp->crp_desc = crd;
915                 crp->crp_etype = 0;
916                 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
917                                  CRYPTO_F_BATCH;
918
919                 crd->crd_alg = priv->crypto_alg;
920 #if 0
921                 crd->crd_key = (caddr_t)priv->crypto_key;
922                 crd->crd_klen = priv->crypto_klen;
923 #endif
924
925                 crd->crd_skip = 0;
926                 crd->crd_len = DEV_BSIZE /* XXX */;
927                 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
928                 crd->crd_next = NULL;
929
930                 crd->crd_flags &= ~CRD_F_ENCRYPT;
931
932                 /*
933                  * Note: last argument is used to generate salt(?) and is
934                  *       a 64 bit value, but the original code passed an
935                  *       int.  Changing it now will break pre-existing
936                  *       crypt volumes.
937                  */
938                 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
939                                     isector + i, crp);
940         }
941 }
942
943 /*
944  * STRATEGY READ PATH PART 3/3
945  */
946 static int
947 dmtc_crypto_cb_read_done(struct cryptop *crp)
948 {
949         struct dmtc_helper *dmtc;
950         struct bio *bio, *obio;
951         int n;
952
953         if (crp->crp_etype == EAGAIN)
954                 return crypto_dispatch(crp);
955
956         bio = (struct bio *)crp->crp_opaque;
957         KKASSERT(bio != NULL);
958
959         /*
960          * Cumulative error
961          */
962         if (crp->crp_etype) {
963                 kprintf("dm_target_crypt: dmtc_crypto_cb_read_done "
964                         "crp_etype = %d\n",
965                         crp->crp_etype);
966                 bio->bio_buf->b_error = crp->crp_etype;
967         }
968
969         /*
970          * On the last chunk of the decryption we do any required copybacks
971          * and complete the I/O.
972          */
973         n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
974 #if 0
975         kprintf("dmtc_crypto_cb_read_done %p, n = %d\n", bio, n);
976 #endif
977
978         if (n == 1) {
979                 /*
980                  * For the B_HASBOGUS case we didn't decrypt in place,
981                  * so we need to copy stuff back into the buf.
982                  *
983                  * (disabled for now).
984                  */
985                 dmtc = bio->bio_caller_info2.ptr;
986                 if (bio->bio_buf->b_error) {
987                         bio->bio_buf->b_flags |= B_ERROR;
988                 }
989 #if 0
990                 else if (bio->bio_buf->b_flags & B_HASBOGUS) {
991                         memcpy(bio->bio_buf->b_data, dmtc->data_buf,
992                                bio->bio_buf->b_bcount);
993                 }
994 #endif
995                 kfree(dmtc->free_addr, M_DMCRYPT);
996                 obio = pop_bio(bio);
997                 biodone(obio);
998         }
999         return 0;
1000 }
1001 /* END OF STRATEGY READ SECTION */
1002
1003 /*
1004  * STRATEGY WRITE PATH PART 1/3
1005  */
1006 static void
1007 dmtc_crypto_write_start(dm_target_crypt_config_t *priv, struct bio *bio)
1008 {
1009         struct dmtc_helper *dmtc;
1010         struct cryptodesc *crd;
1011         struct cryptop *crp;
1012         struct cryptoini *cri;
1013         int i, bytes, sectors, sz;
1014         off_t isector;
1015         u_char *ptr, *space;
1016
1017         cri = &priv->crypto_session;
1018
1019         /*
1020          * Use b_bcount for consistency
1021          */
1022         bytes = bio->bio_buf->b_bcount;
1023
1024         isector = bio->bio_offset / DEV_BSIZE;  /* ivgen salt base? */
1025         sectors = bytes / DEV_BSIZE;            /* Number of sectors */
1026         sz = sectors * (sizeof(*crp) + sizeof(*crd));
1027
1028         /*
1029          * For writes and reads with bogus page don't decrypt in place.
1030          */
1031         space = kmalloc(sizeof(struct dmtc_helper) + sz + bytes,
1032                         M_DMCRYPT, M_WAITOK);
1033         dmtc = (struct dmtc_helper *)space;
1034         dmtc->free_addr = space;
1035         space += sizeof(struct dmtc_helper);
1036         memcpy(space + sz, bio->bio_buf->b_data, bytes);
1037
1038         bio->bio_caller_info2.ptr = dmtc;
1039         bio->bio_buf->b_error = 0;
1040
1041         dmtc->orig_buf = bio->bio_buf->b_data;
1042         dmtc->data_buf = space + sz;
1043
1044         /*
1045          * Load crypto descriptors (crp/crd loop)
1046          */
1047         bzero(space, sz);
1048         ptr = space;
1049         bio->bio_caller_info3.value = sectors;
1050         cpu_sfence();
1051 #if 0
1052         kprintf("Write, bytes = %d (b_bcount), "
1053                 "sectors = %d (bio = %p, b_cmd = %d)\n",
1054                 bytes, sectors, bio, bio->bio_buf->b_cmd);
1055 #endif
1056         for (i = 0; i < sectors; i++) {
1057                 crp = (struct cryptop *)ptr;
1058                 ptr += sizeof(*crp);
1059                 crd = (struct cryptodesc *)ptr;
1060                 ptr += sizeof (*crd);
1061
1062                 crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
1063
1064                 crp->crp_sid = priv->crypto_sid;
1065                 crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
1066
1067                 crp->crp_opaque = (void *)bio;
1068
1069                 crp->crp_callback = dmtc_crypto_cb_write_done;
1070                 crp->crp_desc = crd;
1071                 crp->crp_etype = 0;
1072                 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
1073                                  CRYPTO_F_BATCH;
1074
1075                 crd->crd_alg = priv->crypto_alg;
1076 #if 0
1077                 crd->crd_key = (caddr_t)priv->crypto_key;
1078                 crd->crd_klen = priv->crypto_klen;
1079 #endif
1080
1081                 crd->crd_skip = 0;
1082                 crd->crd_len = DEV_BSIZE /* XXX */;
1083                 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
1084                 crd->crd_next = NULL;
1085
1086                 crd->crd_flags |= CRD_F_ENCRYPT;
1087
1088                 /*
1089                  * Note: last argument is used to generate salt(?) and is
1090                  *       a 64 bit value, but the original code passed an
1091                  *       int.  Changing it now will break pre-existing
1092                  *       crypt volumes.
1093                  */
1094                 priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
1095                                     isector + i, crp);
1096         }
1097 }
1098
1099 /*
1100  * STRATEGY WRITE PATH PART 2/3
1101  */
1102 static int
1103 dmtc_crypto_cb_write_done(struct cryptop *crp)
1104 {
1105         struct dmtc_helper *dmtc;
1106         dm_target_crypt_config_t *priv;
1107         struct bio *bio, *obio;
1108         int n;
1109
1110         if (crp->crp_etype == EAGAIN)
1111                 return crypto_dispatch(crp);
1112
1113         bio = (struct bio *)crp->crp_opaque;
1114         KKASSERT(bio != NULL);
1115
1116         /*
1117          * Cumulative error
1118          */
1119         if (crp->crp_etype != 0) {
1120                 kprintf("dm_target_crypt: dmtc_crypto_cb_write_done "
1121                         "crp_etype = %d\n",
1122                 crp->crp_etype);
1123                 bio->bio_buf->b_error = crp->crp_etype;
1124         }
1125
1126         /*
1127          * On the last chunk of the encryption we issue the write
1128          */
1129         n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
1130 #if 0
1131         kprintf("dmtc_crypto_cb_write_done %p, n = %d\n", bio, n);
1132 #endif
1133
1134         if (n == 1) {
1135                 dmtc = bio->bio_caller_info2.ptr;
1136                 priv = (dm_target_crypt_config_t *)bio->bio_caller_info1.ptr;
1137
1138                 if (bio->bio_buf->b_error) {
1139                         bio->bio_buf->b_flags |= B_ERROR;
1140                         kfree(dmtc->free_addr, M_DMCRYPT);
1141                         obio = pop_bio(bio);
1142                         biodone(obio);
1143                 } else {
1144                         dmtc->orig_buf = bio->bio_buf->b_data;
1145                         bio->bio_buf->b_data = dmtc->data_buf;
1146                         bio->bio_done = dmtc_bio_write_done;
1147                         vn_strategy(priv->pdev->pdev_vnode, bio);
1148                 }
1149         }
1150         return 0;
1151 }
1152
1153 /*
1154  * STRATEGY WRITE PATH PART 3/3
1155  */
1156 static void
1157 dmtc_bio_write_done(struct bio *bio)
1158 {
1159         struct dmtc_helper *dmtc;
1160         struct bio *obio;
1161
1162         dmtc = bio->bio_caller_info2.ptr;
1163         bio->bio_buf->b_data = dmtc->orig_buf;
1164         kfree(dmtc->free_addr, M_DMCRYPT);
1165         obio = pop_bio(bio);
1166         biodone(obio);
1167 }
1168 /* END OF STRATEGY WRITE SECTION */