Home | History | Annotate | Line # | Download | only in dev
cgd_crypto.c revision 1.12.10.2
      1  1.12.10.2     skrll /* $NetBSD: cgd_crypto.c,v 1.12.10.2 2017/02/05 13:40:26 skrll Exp $ */
      2        1.1     elric 
      3        1.1     elric /*-
      4        1.1     elric  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5        1.1     elric  * All rights reserved.
      6        1.1     elric  *
      7        1.1     elric  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1     elric  * by Roland C. Dowdeswell.
      9        1.1     elric  *
     10        1.1     elric  * Redistribution and use in source and binary forms, with or without
     11        1.1     elric  * modification, are permitted provided that the following conditions
     12        1.1     elric  * are met:
     13        1.1     elric  * 1. Redistributions of source code must retain the above copyright
     14        1.1     elric  *    notice, this list of conditions and the following disclaimer.
     15        1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17        1.1     elric  *    documentation and/or other materials provided with the distribution.
     18        1.1     elric  *
     19        1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1     elric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1     elric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1     elric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1     elric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1     elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1     elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1     elric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1     elric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1     elric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1     elric  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1     elric  */
     31        1.1     elric 
     32        1.1     elric /*
     33        1.1     elric  *  Crypto Framework For cgd.c
     34        1.1     elric  *
     35        1.1     elric  *	This framework is temporary and awaits a more complete
     36        1.1     elric  *	kernel wide crypto implementation.
     37        1.1     elric  */
     38        1.1     elric 
     39        1.1     elric #include <sys/cdefs.h>
     40  1.12.10.2     skrll __KERNEL_RCSID(0, "$NetBSD: cgd_crypto.c,v 1.12.10.2 2017/02/05 13:40:26 skrll Exp $");
     41        1.1     elric 
     42        1.1     elric #include <sys/param.h>
     43        1.1     elric #include <sys/systm.h>
     44        1.1     elric #include <sys/malloc.h>
     45        1.1     elric 
     46        1.1     elric #include <dev/cgd_crypto.h>
     47        1.1     elric 
     48       1.11  christos #include <crypto/rijndael/rijndael-api-fst.h>
     49       1.11  christos #include <crypto/des/des.h>
     50       1.11  christos #include <crypto/blowfish/blowfish.h>
     51       1.11  christos 
     52        1.1     elric #ifdef DIAGNOSTIC
     53        1.6  christos #define DIAGPANIC(x)	panic x
     54        1.1     elric #else
     55        1.1     elric #define DIAGPANIC(x)
     56        1.1     elric #endif
     57        1.1     elric 
     58        1.1     elric /*
     59        1.1     elric  * The general framework provides only one generic function.
     60        1.1     elric  * It takes the name of an algorith and returns a struct cryptfuncs *
     61        1.1     elric  * for it.  It is up to the initialisation routines of the algorithm
     62        1.1     elric  * to check key size and block size.
     63        1.1     elric  */
     64        1.1     elric 
     65  1.12.10.2     skrll static cfunc_init		cgd_cipher_aes_cbc_init;
     66  1.12.10.2     skrll static cfunc_destroy		cgd_cipher_aes_cbc_destroy;
     67  1.12.10.2     skrll static cfunc_cipher		cgd_cipher_aes_cbc;
     68  1.12.10.2     skrll static cfunc_cipher_prep	cgd_cipher_aes_cbc_prep;
     69  1.12.10.2     skrll 
     70  1.12.10.2     skrll static cfunc_init		cgd_cipher_aes_xts_init;
     71  1.12.10.2     skrll static cfunc_destroy		cgd_cipher_aes_xts_destroy;
     72  1.12.10.2     skrll static cfunc_cipher		cgd_cipher_aes_xts;
     73  1.12.10.2     skrll static cfunc_cipher_prep	cgd_cipher_aes_xts_prep;
     74  1.12.10.2     skrll 
     75  1.12.10.2     skrll static cfunc_init		cgd_cipher_3des_init;
     76  1.12.10.2     skrll static cfunc_destroy		cgd_cipher_3des_destroy;
     77  1.12.10.2     skrll static cfunc_cipher		cgd_cipher_3des_cbc;
     78  1.12.10.2     skrll static cfunc_cipher_prep	cgd_cipher_3des_cbc_prep;
     79  1.12.10.2     skrll 
     80  1.12.10.2     skrll static cfunc_init		cgd_cipher_bf_init;
     81  1.12.10.2     skrll static cfunc_destroy		cgd_cipher_bf_destroy;
     82  1.12.10.2     skrll static cfunc_cipher		cgd_cipher_bf_cbc;
     83  1.12.10.2     skrll static cfunc_cipher_prep	cgd_cipher_bf_cbc_prep;
     84       1.11  christos 
     85       1.11  christos static const struct cryptfuncs cf[] = {
     86       1.11  christos 	{
     87  1.12.10.2     skrll 		.cf_name	= "aes-xts",
     88  1.12.10.2     skrll 		.cf_init	= cgd_cipher_aes_xts_init,
     89  1.12.10.2     skrll 		.cf_destroy	= cgd_cipher_aes_xts_destroy,
     90  1.12.10.2     skrll 		.cf_cipher	= cgd_cipher_aes_xts,
     91  1.12.10.2     skrll 		.cf_cipher_prep	= cgd_cipher_aes_xts_prep,
     92  1.12.10.2     skrll 	},
     93  1.12.10.2     skrll 	{
     94       1.11  christos 		.cf_name	= "aes-cbc",
     95  1.12.10.2     skrll 		.cf_init	= cgd_cipher_aes_cbc_init,
     96  1.12.10.2     skrll 		.cf_destroy	= cgd_cipher_aes_cbc_destroy,
     97       1.11  christos 		.cf_cipher	= cgd_cipher_aes_cbc,
     98  1.12.10.2     skrll 		.cf_cipher_prep	= cgd_cipher_aes_cbc_prep,
     99       1.11  christos 	},
    100       1.11  christos 	{
    101       1.11  christos 		.cf_name	= "3des-cbc",
    102       1.11  christos 		.cf_init	= cgd_cipher_3des_init,
    103       1.11  christos 		.cf_destroy	= cgd_cipher_3des_destroy,
    104       1.11  christos 		.cf_cipher	= cgd_cipher_3des_cbc,
    105  1.12.10.2     skrll 		.cf_cipher_prep	= cgd_cipher_3des_cbc_prep,
    106       1.11  christos 	},
    107       1.11  christos 	{
    108       1.11  christos 		.cf_name	= "blowfish-cbc",
    109       1.11  christos 		.cf_init	= cgd_cipher_bf_init,
    110       1.11  christos 		.cf_destroy	= cgd_cipher_bf_destroy,
    111       1.11  christos 		.cf_cipher	= cgd_cipher_bf_cbc,
    112  1.12.10.2     skrll 		.cf_cipher_prep	= cgd_cipher_bf_cbc_prep,
    113       1.11  christos 	},
    114       1.11  christos };
    115       1.11  christos const struct cryptfuncs *
    116        1.6  christos cryptfuncs_find(const char *alg)
    117        1.1     elric {
    118        1.1     elric 
    119       1.11  christos 	for (size_t i = 0; i < __arraycount(cf); i++)
    120       1.11  christos 		if (strcmp(cf[i].cf_name, alg) == 0)
    121       1.11  christos 			return &cf[i];
    122       1.11  christos 
    123        1.1     elric 	return NULL;
    124        1.1     elric }
    125        1.1     elric 
    126        1.7    cbiere typedef void	(*cipher_func)(void *, void *, const void *, size_t);
    127        1.1     elric 
    128       1.11  christos static void
    129  1.12.10.2     skrll cgd_cipher_uio(void *privdata, cipher_func cipher,
    130        1.1     elric 	struct uio *dstuio, struct uio *srcuio);
    131        1.1     elric 
    132        1.1     elric /*
    133  1.12.10.2     skrll  * cgd_cipher_uio takes a simple cbc or xts cipher and iterates
    134        1.1     elric  * it over two struct uio's.  It presumes that the cipher function
    135        1.1     elric  * that is passed to it keeps the IV state between calls.
    136        1.1     elric  *
    137        1.1     elric  * We assume that the caller has ensured that each segment is evenly
    138        1.1     elric  * divisible by the block size, which for the cgd is a valid assumption.
    139        1.1     elric  * If we were to make this code more generic, we might need to take care
    140        1.1     elric  * of this case, either by issuing an error or copying the data.
    141        1.1     elric  */
    142        1.1     elric 
    143       1.11  christos static void
    144  1.12.10.2     skrll cgd_cipher_uio(void *privdata, cipher_func cipher,
    145        1.6  christos     struct uio *dstuio, struct uio *srcuio)
    146        1.1     elric {
    147  1.12.10.1     skrll 	const struct iovec	*dst;
    148  1.12.10.1     skrll 	const struct iovec	*src;
    149        1.1     elric 	int		 dstnum;
    150        1.1     elric 	int		 dstoff = 0;
    151        1.1     elric 	int		 srcnum;
    152        1.1     elric 	int		 srcoff = 0;
    153        1.1     elric 
    154        1.1     elric 	dst = dstuio->uio_iov;
    155        1.1     elric 	dstnum = dstuio->uio_iovcnt;
    156        1.1     elric 	src = srcuio->uio_iov;
    157        1.1     elric 	srcnum = srcuio->uio_iovcnt;
    158        1.1     elric 	for (;;) {
    159        1.1     elric 		int	  l = MIN(dst->iov_len - dstoff, src->iov_len - srcoff);
    160        1.1     elric 		u_int8_t *d = (u_int8_t *)dst->iov_base + dstoff;
    161  1.12.10.1     skrll 		const u_int8_t *s = (const u_int8_t *)src->iov_base + srcoff;
    162        1.1     elric 
    163        1.1     elric 		cipher(privdata, d, s, l);
    164        1.1     elric 
    165        1.1     elric 		dstoff += l;
    166        1.1     elric 		srcoff += l;
    167        1.1     elric 		/*
    168        1.1     elric 		 * We assume that {dst,src} == {dst,src}->iov_len,
    169        1.1     elric 		 * because it should not be possible for it not to be.
    170        1.1     elric 		 */
    171        1.1     elric 		if (dstoff == dst->iov_len) {
    172        1.1     elric 			dstoff = 0;
    173        1.1     elric 			dstnum--;
    174        1.1     elric 			dst++;
    175        1.1     elric 		}
    176        1.1     elric 		if (srcoff == src->iov_len) {
    177        1.1     elric 			srcoff = 0;
    178        1.1     elric 			srcnum--;
    179        1.1     elric 			src++;
    180        1.1     elric 		}
    181        1.1     elric 		if (!srcnum || !dstnum)
    182        1.1     elric 			break;
    183        1.1     elric 	}
    184        1.1     elric }
    185        1.1     elric 
    186        1.1     elric /*
    187        1.1     elric  *  AES Framework
    188        1.1     elric  */
    189        1.1     elric 
    190        1.1     elric /*
    191        1.1     elric  * NOTE: we do not store the blocksize in here, because it is not
    192        1.1     elric  *       variable [yet], we hardcode the blocksize to 16 (128 bits).
    193        1.1     elric  */
    194        1.1     elric 
    195        1.1     elric struct aes_privdata {
    196        1.1     elric 	keyInstance	ap_enckey;
    197        1.1     elric 	keyInstance	ap_deckey;
    198        1.1     elric };
    199        1.1     elric 
    200        1.1     elric struct aes_encdata {
    201        1.1     elric 	keyInstance	*ae_key;	/* key for this direction */
    202  1.12.10.2     skrll 	u_int8_t	 ae_iv[CGD_AES_BLOCK_SIZE]; /* Initialization Vector */
    203        1.1     elric };
    204        1.1     elric 
    205       1.11  christos static void *
    206  1.12.10.2     skrll cgd_cipher_aes_cbc_init(size_t keylen, const void *key, size_t *blocksize)
    207        1.1     elric {
    208        1.1     elric 	struct	aes_privdata *ap;
    209        1.1     elric 
    210        1.1     elric 	if (!blocksize)
    211        1.1     elric 		return NULL;
    212        1.1     elric 	if (keylen != 128 && keylen != 192 && keylen != 256)
    213        1.1     elric 		return NULL;
    214        1.6  christos 	if (*blocksize == (size_t)-1)
    215        1.1     elric 		*blocksize = 128;
    216        1.1     elric 	if (*blocksize != 128)
    217        1.1     elric 		return NULL;
    218        1.1     elric 	ap = malloc(sizeof(*ap), M_DEVBUF, 0);
    219        1.1     elric 	if (!ap)
    220        1.1     elric 		return NULL;
    221        1.1     elric 	rijndael_makeKey(&ap->ap_enckey, DIR_ENCRYPT, keylen, key);
    222        1.1     elric 	rijndael_makeKey(&ap->ap_deckey, DIR_DECRYPT, keylen, key);
    223        1.6  christos 	return ap;
    224        1.1     elric }
    225        1.1     elric 
    226       1.11  christos static void
    227  1.12.10.2     skrll cgd_cipher_aes_cbc_destroy(void *data)
    228        1.1     elric {
    229        1.6  christos 	struct aes_privdata *apd = data;
    230        1.1     elric 
    231       1.12  riastrad 	explicit_memset(apd, 0, sizeof(*apd));
    232        1.1     elric 	free(apd, M_DEVBUF);
    233        1.1     elric }
    234        1.1     elric 
    235       1.11  christos static void
    236  1.12.10.2     skrll cgd_cipher_aes_cbc_prep(void *privdata, char *iv,
    237  1.12.10.2     skrll     const char *blkno_buf, size_t blocksize, int dir)
    238  1.12.10.2     skrll {
    239  1.12.10.2     skrll 	struct aes_privdata	*apd = privdata;
    240  1.12.10.2     skrll 	cipherInstance		 cipher;
    241  1.12.10.2     skrll 	int			 cipher_ok __diagused;
    242  1.12.10.2     skrll 
    243  1.12.10.2     skrll 	cipher_ok = rijndael_cipherInit(&cipher, MODE_CBC, NULL);
    244  1.12.10.2     skrll 	KASSERT(cipher_ok > 0);
    245  1.12.10.2     skrll 	rijndael_blockEncrypt(&cipher, &apd->ap_enckey,
    246  1.12.10.2     skrll 	    blkno_buf, blocksize * 8, iv);
    247  1.12.10.2     skrll 	if (blocksize > CGD_AES_BLOCK_SIZE) {
    248  1.12.10.2     skrll 		(void)memmove(iv, iv + blocksize - CGD_AES_BLOCK_SIZE,
    249  1.12.10.2     skrll 		    CGD_AES_BLOCK_SIZE);
    250  1.12.10.2     skrll 	}
    251  1.12.10.2     skrll }
    252  1.12.10.2     skrll 
    253  1.12.10.2     skrll static void
    254        1.7    cbiere aes_cbc_enc_int(void *privdata, void *dst, const void *src, size_t len)
    255        1.1     elric {
    256        1.6  christos 	struct aes_encdata	*ae = privdata;
    257        1.1     elric 	cipherInstance		 cipher;
    258  1.12.10.2     skrll 	int			 cipher_ok __diagused;
    259        1.1     elric 
    260  1.12.10.2     skrll 	cipher_ok = rijndael_cipherInit(&cipher, MODE_CBC, ae->ae_iv);
    261  1.12.10.2     skrll 	KASSERT(cipher_ok > 0);
    262        1.1     elric 	rijndael_blockEncrypt(&cipher, ae->ae_key, src, len * 8, dst);
    263  1.12.10.2     skrll 	(void)memcpy(ae->ae_iv, (u_int8_t *)dst +
    264  1.12.10.2     skrll 	    (len - CGD_AES_BLOCK_SIZE), CGD_AES_BLOCK_SIZE);
    265        1.1     elric }
    266        1.1     elric 
    267       1.11  christos static void
    268        1.7    cbiere aes_cbc_dec_int(void *privdata, void *dst, const void *src, size_t len)
    269        1.1     elric {
    270        1.6  christos 	struct aes_encdata	*ae = privdata;
    271        1.1     elric 	cipherInstance		 cipher;
    272  1.12.10.2     skrll 	int			 cipher_ok __diagused;
    273        1.1     elric 
    274  1.12.10.2     skrll 	cipher_ok = rijndael_cipherInit(&cipher, MODE_CBC, ae->ae_iv);
    275  1.12.10.2     skrll 	KASSERT(cipher_ok > 0);
    276        1.1     elric 	rijndael_blockDecrypt(&cipher, ae->ae_key, src, len * 8, dst);
    277  1.12.10.2     skrll 	(void)memcpy(ae->ae_iv, (const u_int8_t *)src +
    278  1.12.10.2     skrll 	    (len - CGD_AES_BLOCK_SIZE), CGD_AES_BLOCK_SIZE);
    279        1.1     elric }
    280        1.1     elric 
    281       1.11  christos static void
    282        1.6  christos cgd_cipher_aes_cbc(void *privdata, struct uio *dstuio,
    283  1.12.10.1     skrll     struct uio *srcuio, const void *iv, int dir)
    284        1.1     elric {
    285        1.6  christos 	struct aes_privdata	*apd = privdata;
    286        1.1     elric 	struct aes_encdata	 encd;
    287        1.1     elric 
    288  1.12.10.2     skrll 	(void)memcpy(encd.ae_iv, iv, CGD_AES_BLOCK_SIZE);
    289        1.1     elric 	switch (dir) {
    290        1.1     elric 	case CGD_CIPHER_ENCRYPT:
    291        1.1     elric 		encd.ae_key = &apd->ap_enckey;
    292  1.12.10.2     skrll 		cgd_cipher_uio(&encd, aes_cbc_enc_int, dstuio, srcuio);
    293        1.1     elric 		break;
    294        1.1     elric 	case CGD_CIPHER_DECRYPT:
    295        1.1     elric 		encd.ae_key = &apd->ap_deckey;
    296  1.12.10.2     skrll 		cgd_cipher_uio(&encd, aes_cbc_dec_int, dstuio, srcuio);
    297  1.12.10.2     skrll 		break;
    298  1.12.10.2     skrll 	default:
    299  1.12.10.2     skrll 		DIAGPANIC(("%s: unrecognised direction %d", __func__, dir));
    300  1.12.10.2     skrll 	}
    301  1.12.10.2     skrll }
    302  1.12.10.2     skrll 
    303  1.12.10.2     skrll static void *
    304  1.12.10.2     skrll cgd_cipher_aes_xts_init(size_t keylen, const void *xtskey, size_t *blocksize)
    305  1.12.10.2     skrll {
    306  1.12.10.2     skrll 	struct aes_privdata *ap;
    307  1.12.10.2     skrll 	const char *key, *key2; /* XTS key is made of two AES keys. */
    308  1.12.10.2     skrll 
    309  1.12.10.2     skrll 	if (!blocksize)
    310  1.12.10.2     skrll 		return NULL;
    311  1.12.10.2     skrll 	if (keylen != 256 && keylen != 512)
    312  1.12.10.2     skrll 		return NULL;
    313  1.12.10.2     skrll 	if (*blocksize == (size_t)-1)
    314  1.12.10.2     skrll 		*blocksize = 128;
    315  1.12.10.2     skrll 	if (*blocksize != 128)
    316  1.12.10.2     skrll 		return NULL;
    317  1.12.10.2     skrll 	ap = malloc(2 * sizeof(*ap), M_DEVBUF, 0);
    318  1.12.10.2     skrll 	if (!ap)
    319  1.12.10.2     skrll 		return NULL;
    320  1.12.10.2     skrll 
    321  1.12.10.2     skrll 	keylen /= 2;
    322  1.12.10.2     skrll 	key = xtskey;
    323  1.12.10.2     skrll 	key2 = key + keylen / CHAR_BIT;
    324  1.12.10.2     skrll 
    325  1.12.10.2     skrll 	rijndael_makeKey(&ap[0].ap_enckey, DIR_ENCRYPT, keylen, key);
    326  1.12.10.2     skrll 	rijndael_makeKey(&ap[0].ap_deckey, DIR_DECRYPT, keylen, key);
    327  1.12.10.2     skrll 	rijndael_makeKey(&ap[1].ap_enckey, DIR_ENCRYPT, keylen, key2);
    328  1.12.10.2     skrll 
    329  1.12.10.2     skrll 	return ap;
    330  1.12.10.2     skrll }
    331  1.12.10.2     skrll 
    332  1.12.10.2     skrll static void
    333  1.12.10.2     skrll cgd_cipher_aes_xts_destroy(void *data)
    334  1.12.10.2     skrll {
    335  1.12.10.2     skrll 	struct aes_privdata *apd = data;
    336  1.12.10.2     skrll 
    337  1.12.10.2     skrll 	explicit_memset(apd, 0, 2 * sizeof(*apd));
    338  1.12.10.2     skrll 	free(apd, M_DEVBUF);
    339  1.12.10.2     skrll }
    340  1.12.10.2     skrll 
    341  1.12.10.2     skrll static void
    342  1.12.10.2     skrll cgd_cipher_aes_xts_prep(void *privdata, char *iv,
    343  1.12.10.2     skrll     const char *blkno_buf, size_t blocksize, int dir)
    344  1.12.10.2     skrll {
    345  1.12.10.2     skrll 	struct aes_privdata	*apd = privdata;
    346  1.12.10.2     skrll 	cipherInstance		 cipher;
    347  1.12.10.2     skrll 	int			 cipher_ok __diagused;
    348  1.12.10.2     skrll 
    349  1.12.10.2     skrll 	cipher_ok = rijndael_cipherInit(&cipher, MODE_ECB, NULL);
    350  1.12.10.2     skrll 	KASSERT(cipher_ok > 0);
    351  1.12.10.2     skrll 	rijndael_blockEncrypt(&cipher, &apd[1].ap_enckey,
    352  1.12.10.2     skrll 	    blkno_buf, blocksize * 8, iv);
    353  1.12.10.2     skrll }
    354  1.12.10.2     skrll 
    355  1.12.10.2     skrll static void
    356  1.12.10.2     skrll aes_xts_enc_int(void *privdata, void *dst, const void *src, size_t len)
    357  1.12.10.2     skrll {
    358  1.12.10.2     skrll 	struct aes_encdata	*ae = privdata;
    359  1.12.10.2     skrll 	cipherInstance		 cipher;
    360  1.12.10.2     skrll 	int			 cipher_ok __diagused;
    361  1.12.10.2     skrll 
    362  1.12.10.2     skrll 	cipher_ok = rijndael_cipherInit(&cipher, MODE_XTS, ae->ae_iv);
    363  1.12.10.2     skrll 	KASSERT(cipher_ok > 0);
    364  1.12.10.2     skrll 	rijndael_blockEncrypt(&cipher, ae->ae_key, src, len * 8, dst);
    365  1.12.10.2     skrll 	(void)memcpy(ae->ae_iv, cipher.IV, CGD_AES_BLOCK_SIZE);
    366  1.12.10.2     skrll }
    367  1.12.10.2     skrll 
    368  1.12.10.2     skrll static void
    369  1.12.10.2     skrll aes_xts_dec_int(void *privdata, void *dst, const void *src, size_t len)
    370  1.12.10.2     skrll {
    371  1.12.10.2     skrll 	struct aes_encdata	*ae = privdata;
    372  1.12.10.2     skrll 	cipherInstance		 cipher;
    373  1.12.10.2     skrll 	int			 cipher_ok __diagused;
    374  1.12.10.2     skrll 
    375  1.12.10.2     skrll 	cipher_ok = rijndael_cipherInit(&cipher, MODE_XTS, ae->ae_iv);
    376  1.12.10.2     skrll 	KASSERT(cipher_ok > 0);
    377  1.12.10.2     skrll 	rijndael_blockDecrypt(&cipher, ae->ae_key, src, len * 8, dst);
    378  1.12.10.2     skrll 	(void)memcpy(ae->ae_iv, cipher.IV, CGD_AES_BLOCK_SIZE);
    379  1.12.10.2     skrll }
    380  1.12.10.2     skrll 
    381  1.12.10.2     skrll static void
    382  1.12.10.2     skrll cgd_cipher_aes_xts(void *privdata, struct uio *dstuio,
    383  1.12.10.2     skrll     struct uio *srcuio, const void *iv, int dir)
    384  1.12.10.2     skrll {
    385  1.12.10.2     skrll 	struct aes_privdata	*apd = privdata;
    386  1.12.10.2     skrll 	struct aes_encdata	 encd;
    387  1.12.10.2     skrll 
    388  1.12.10.2     skrll 	(void)memcpy(encd.ae_iv, iv, CGD_AES_BLOCK_SIZE);
    389  1.12.10.2     skrll 	switch (dir) {
    390  1.12.10.2     skrll 	case CGD_CIPHER_ENCRYPT:
    391  1.12.10.2     skrll 		encd.ae_key = &apd->ap_enckey;
    392  1.12.10.2     skrll 		cgd_cipher_uio(&encd, aes_xts_enc_int, dstuio, srcuio);
    393  1.12.10.2     skrll 		break;
    394  1.12.10.2     skrll 	case CGD_CIPHER_DECRYPT:
    395  1.12.10.2     skrll 		encd.ae_key = &apd->ap_deckey;
    396  1.12.10.2     skrll 		cgd_cipher_uio(&encd, aes_xts_dec_int, dstuio, srcuio);
    397        1.1     elric 		break;
    398        1.1     elric 	default:
    399        1.8     perry 		DIAGPANIC(("%s: unrecognised direction %d", __func__, dir));
    400        1.1     elric 	}
    401        1.1     elric }
    402        1.1     elric 
    403        1.1     elric /*
    404        1.1     elric  * 3DES Framework
    405        1.1     elric  */
    406        1.1     elric 
    407        1.1     elric struct c3des_privdata {
    408        1.1     elric 	des_key_schedule	cp_key1;
    409        1.1     elric 	des_key_schedule	cp_key2;
    410        1.1     elric 	des_key_schedule	cp_key3;
    411        1.1     elric };
    412        1.1     elric 
    413        1.1     elric struct c3des_encdata {
    414        1.1     elric 	des_key_schedule	*ce_key1;
    415        1.1     elric 	des_key_schedule	*ce_key2;
    416        1.1     elric 	des_key_schedule	*ce_key3;
    417  1.12.10.2     skrll 	u_int8_t		ce_iv[CGD_3DES_BLOCK_SIZE];
    418        1.1     elric };
    419        1.1     elric 
    420       1.11  christos static void *
    421        1.7    cbiere cgd_cipher_3des_init(size_t keylen, const void *key, size_t *blocksize)
    422        1.1     elric {
    423        1.1     elric 	struct	c3des_privdata *cp;
    424        1.1     elric 	int	error = 0;
    425        1.7    cbiere 	des_cblock *block;
    426        1.1     elric 
    427        1.1     elric 	if (!blocksize)
    428        1.1     elric 		return NULL;
    429        1.6  christos 	if (*blocksize == (size_t)-1)
    430        1.1     elric 		*blocksize = 64;
    431        1.1     elric 	if (keylen != (DES_KEY_SZ * 3 * 8) || *blocksize != 64)
    432        1.1     elric 		return NULL;
    433        1.1     elric 	cp = malloc(sizeof(*cp), M_DEVBUF, 0);
    434        1.1     elric 	if (!cp)
    435        1.1     elric 		return NULL;
    436        1.7    cbiere 	block = __UNCONST(key);
    437        1.7    cbiere 	error  = des_key_sched(block, cp->cp_key1);
    438        1.7    cbiere 	error |= des_key_sched(block + 1, cp->cp_key2);
    439        1.7    cbiere 	error |= des_key_sched(block + 2, cp->cp_key3);
    440        1.1     elric 	if (error) {
    441       1.12  riastrad 		explicit_memset(cp, 0, sizeof(*cp));
    442        1.1     elric 		free(cp, M_DEVBUF);
    443        1.1     elric 		return NULL;
    444        1.1     elric 	}
    445        1.6  christos 	return cp;
    446        1.1     elric }
    447        1.1     elric 
    448       1.11  christos static void
    449        1.6  christos cgd_cipher_3des_destroy(void *data)
    450        1.1     elric {
    451        1.6  christos 	struct c3des_privdata *cp = data;
    452        1.1     elric 
    453       1.12  riastrad 	explicit_memset(cp, 0, sizeof(*cp));
    454        1.1     elric 	free(cp, M_DEVBUF);
    455        1.1     elric }
    456        1.1     elric 
    457        1.1     elric static void
    458  1.12.10.2     skrll cgd_cipher_3des_cbc_prep(void *privdata, char *iv,
    459  1.12.10.2     skrll     const char *blkno_buf, size_t blocksize, int dir)
    460  1.12.10.2     skrll {
    461  1.12.10.2     skrll 	struct	c3des_privdata *cp = privdata;
    462  1.12.10.2     skrll 	char	zero_iv[CGD_3DES_BLOCK_SIZE];
    463  1.12.10.2     skrll 
    464  1.12.10.2     skrll 	memset(zero_iv, 0, sizeof(zero_iv));
    465  1.12.10.2     skrll 	des_ede3_cbc_encrypt(blkno_buf, iv, blocksize,
    466  1.12.10.2     skrll 	    cp->cp_key1, cp->cp_key2, cp->cp_key3, (des_cblock *)zero_iv, 1);
    467  1.12.10.2     skrll 	if (blocksize > CGD_3DES_BLOCK_SIZE) {
    468  1.12.10.2     skrll 		(void)memmove(iv, iv + blocksize - CGD_3DES_BLOCK_SIZE,
    469  1.12.10.2     skrll 		    CGD_3DES_BLOCK_SIZE);
    470  1.12.10.2     skrll 	}
    471  1.12.10.2     skrll }
    472  1.12.10.2     skrll 
    473  1.12.10.2     skrll static void
    474        1.7    cbiere c3des_cbc_enc_int(void *privdata, void *dst, const void *src, size_t len)
    475        1.1     elric {
    476        1.6  christos 	struct	c3des_encdata *ce = privdata;
    477        1.1     elric 
    478        1.1     elric 	des_ede3_cbc_encrypt(src, dst, len, *ce->ce_key1, *ce->ce_key2,
    479        1.1     elric 	    *ce->ce_key3, (des_cblock *)ce->ce_iv, 1);
    480  1.12.10.2     skrll 	(void)memcpy(ce->ce_iv, (const u_int8_t *)dst +
    481  1.12.10.2     skrll 	    (len - CGD_3DES_BLOCK_SIZE), CGD_3DES_BLOCK_SIZE);
    482        1.1     elric }
    483        1.1     elric 
    484        1.1     elric static void
    485        1.7    cbiere c3des_cbc_dec_int(void *privdata, void *dst, const void *src, size_t len)
    486        1.1     elric {
    487        1.6  christos 	struct	c3des_encdata *ce = privdata;
    488        1.1     elric 
    489        1.1     elric 	des_ede3_cbc_encrypt(src, dst, len, *ce->ce_key1, *ce->ce_key2,
    490        1.1     elric 	    *ce->ce_key3, (des_cblock *)ce->ce_iv, 0);
    491  1.12.10.2     skrll 	(void)memcpy(ce->ce_iv, (const u_int8_t *)src +
    492  1.12.10.2     skrll 	    (len - CGD_3DES_BLOCK_SIZE), CGD_3DES_BLOCK_SIZE);
    493        1.1     elric }
    494        1.1     elric 
    495       1.11  christos static void
    496        1.6  christos cgd_cipher_3des_cbc(void *privdata, struct uio *dstuio,
    497  1.12.10.1     skrll 	struct uio *srcuio, const void *iv, int dir)
    498        1.1     elric {
    499        1.6  christos 	struct	c3des_privdata *cp = privdata;
    500        1.1     elric 	struct	c3des_encdata ce;
    501        1.1     elric 
    502  1.12.10.2     skrll 	(void)memcpy(ce.ce_iv, iv, CGD_3DES_BLOCK_SIZE);
    503        1.1     elric 	ce.ce_key1 = &cp->cp_key1;
    504        1.1     elric 	ce.ce_key2 = &cp->cp_key2;
    505        1.1     elric 	ce.ce_key3 = &cp->cp_key3;
    506        1.1     elric 	switch (dir) {
    507        1.1     elric 	case CGD_CIPHER_ENCRYPT:
    508  1.12.10.2     skrll 		cgd_cipher_uio(&ce, c3des_cbc_enc_int, dstuio, srcuio);
    509        1.1     elric 		break;
    510        1.1     elric 	case CGD_CIPHER_DECRYPT:
    511  1.12.10.2     skrll 		cgd_cipher_uio(&ce, c3des_cbc_dec_int, dstuio, srcuio);
    512        1.1     elric 		break;
    513        1.1     elric 	default:
    514        1.8     perry 		DIAGPANIC(("%s: unrecognised direction %d", __func__, dir));
    515        1.1     elric 	}
    516        1.1     elric }
    517        1.1     elric 
    518        1.1     elric /*
    519        1.1     elric  * Blowfish Framework
    520        1.1     elric  */
    521        1.1     elric 
    522        1.1     elric struct bf_privdata {
    523        1.1     elric 	BF_KEY	bp_key;
    524        1.1     elric };
    525        1.1     elric 
    526        1.1     elric struct bf_encdata {
    527        1.1     elric 	BF_KEY		*be_key;
    528  1.12.10.2     skrll 	u_int8_t	 be_iv[CGD_BF_BLOCK_SIZE];
    529        1.1     elric };
    530        1.1     elric 
    531       1.11  christos static void *
    532        1.7    cbiere cgd_cipher_bf_init(size_t keylen, const void *key, size_t *blocksize)
    533        1.1     elric {
    534        1.1     elric 	struct	bf_privdata *bp;
    535        1.1     elric 
    536        1.1     elric 	if (!blocksize)
    537        1.1     elric 		return NULL;
    538        1.3       dan 	if (keylen < 40 || keylen > 448 || (keylen % 8 != 0))
    539        1.1     elric 		return NULL;
    540        1.6  christos 	if (*blocksize == (size_t)-1)
    541        1.1     elric 		*blocksize = 64;
    542        1.1     elric 	if (*blocksize != 64)
    543        1.1     elric 		return NULL;
    544        1.1     elric 	bp = malloc(sizeof(*bp), M_DEVBUF, 0);
    545        1.1     elric 	if (!bp)
    546        1.1     elric 		return NULL;
    547        1.3       dan 	BF_set_key(&bp->bp_key, keylen / 8, key);
    548        1.6  christos 	return bp;
    549        1.1     elric }
    550        1.1     elric 
    551       1.11  christos static void
    552        1.6  christos cgd_cipher_bf_destroy(void *data)
    553        1.1     elric {
    554        1.6  christos 	struct	bf_privdata *bp = data;
    555        1.1     elric 
    556       1.12  riastrad 	explicit_memset(bp, 0, sizeof(*bp));
    557        1.1     elric 	free(bp, M_DEVBUF);
    558        1.1     elric }
    559        1.1     elric 
    560       1.11  christos static void
    561  1.12.10.2     skrll cgd_cipher_bf_cbc_prep(void *privdata, char *iv,
    562  1.12.10.2     skrll     const char *blkno_buf, size_t blocksize, int dir)
    563  1.12.10.2     skrll {
    564  1.12.10.2     skrll 	struct	bf_privdata *bp = privdata;
    565  1.12.10.2     skrll 	char	zero_iv[CGD_BF_BLOCK_SIZE];
    566  1.12.10.2     skrll 
    567  1.12.10.2     skrll 	memset(zero_iv, 0, sizeof(zero_iv));
    568  1.12.10.2     skrll 	BF_cbc_encrypt(blkno_buf, iv, blocksize, &bp->bp_key, zero_iv, 1);
    569  1.12.10.2     skrll 	if (blocksize > CGD_BF_BLOCK_SIZE) {
    570  1.12.10.2     skrll 		(void)memmove(iv, iv + blocksize - CGD_BF_BLOCK_SIZE,
    571  1.12.10.2     skrll 		    CGD_BF_BLOCK_SIZE);
    572  1.12.10.2     skrll 	}
    573  1.12.10.2     skrll }
    574  1.12.10.2     skrll 
    575  1.12.10.2     skrll static void
    576        1.7    cbiere bf_cbc_enc_int(void *privdata, void *dst, const void *src, size_t len)
    577        1.1     elric {
    578        1.6  christos 	struct	bf_encdata *be = privdata;
    579        1.1     elric 
    580        1.1     elric 	BF_cbc_encrypt(src, dst, len, be->be_key, be->be_iv, 1);
    581  1.12.10.2     skrll 	(void)memcpy(be->be_iv, (u_int8_t *)dst +
    582  1.12.10.2     skrll 	    (len - CGD_BF_BLOCK_SIZE), CGD_BF_BLOCK_SIZE);
    583        1.1     elric }
    584        1.1     elric 
    585       1.11  christos static void
    586        1.7    cbiere bf_cbc_dec_int(void *privdata, void *dst, const void *src, size_t len)
    587        1.1     elric {
    588        1.6  christos 	struct	bf_encdata *be = privdata;
    589        1.1     elric 
    590        1.1     elric 	BF_cbc_encrypt(src, dst, len, be->be_key, be->be_iv, 0);
    591  1.12.10.2     skrll 	(void)memcpy(be->be_iv, (const u_int8_t *)src +
    592  1.12.10.2     skrll 	    (len - CGD_BF_BLOCK_SIZE), CGD_BF_BLOCK_SIZE);
    593        1.1     elric }
    594        1.1     elric 
    595       1.11  christos static void
    596        1.6  christos cgd_cipher_bf_cbc(void *privdata, struct uio *dstuio,
    597  1.12.10.1     skrll     struct uio *srcuio, const void *iv, int dir)
    598        1.1     elric {
    599        1.6  christos 	struct	bf_privdata *bp = privdata;
    600        1.1     elric 	struct	bf_encdata be;
    601        1.1     elric 
    602  1.12.10.2     skrll 	(void)memcpy(be.be_iv, iv, CGD_BF_BLOCK_SIZE);
    603        1.1     elric 	be.be_key = &bp->bp_key;
    604        1.1     elric 	switch (dir) {
    605        1.1     elric 	case CGD_CIPHER_ENCRYPT:
    606  1.12.10.2     skrll 		cgd_cipher_uio(&be, bf_cbc_enc_int, dstuio, srcuio);
    607        1.1     elric 		break;
    608        1.1     elric 	case CGD_CIPHER_DECRYPT:
    609  1.12.10.2     skrll 		cgd_cipher_uio(&be, bf_cbc_dec_int, dstuio, srcuio);
    610        1.1     elric 		break;
    611        1.1     elric 	default:
    612        1.8     perry 		DIAGPANIC(("%s: unrecognised direction %d", __func__, dir));
    613        1.1     elric 	}
    614        1.1     elric 
    615        1.1     elric }
    616