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