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