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