Home | History | Annotate | Line # | Download | only in dev
cgd_crypto.c revision 1.21
      1  1.21  riastrad /* $NetBSD: cgd_crypto.c,v 1.21 2020/06/13 18:39:36 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.21  riastrad __KERNEL_RCSID(0, "$NetBSD: cgd_crypto.c,v 1.21 2020/06/13 18:39:36 riastradh Exp $");
     41   1.1     elric 
     42   1.1     elric #include <sys/param.h>
     43  1.21  riastrad #include <sys/kmem.h>
     44   1.1     elric #include <sys/systm.h>
     45   1.1     elric 
     46   1.1     elric #include <dev/cgd_crypto.h>
     47   1.1     elric 
     48  1.19  riastrad #include <crypto/blowfish/blowfish.h>
     49  1.19  riastrad #include <crypto/des/des.h>
     50  1.11  christos #include <crypto/rijndael/rijndael-api-fst.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 struct aes_privdata {
    177   1.1     elric 	keyInstance	ap_enckey;
    178   1.1     elric 	keyInstance	ap_deckey;
    179   1.1     elric };
    180   1.1     elric 
    181   1.1     elric struct aes_encdata {
    182   1.1     elric 	keyInstance	*ae_key;	/* key for this direction */
    183  1.18  riastrad 	uint8_t		 ae_iv[CGD_AES_BLOCK_SIZE]; /* Initialization Vector */
    184   1.1     elric };
    185   1.1     elric 
    186  1.11  christos static void *
    187  1.14     alnsn cgd_cipher_aes_cbc_init(size_t keylen, const void *key, size_t *blocksize)
    188   1.1     elric {
    189   1.1     elric 	struct	aes_privdata *ap;
    190   1.1     elric 
    191   1.1     elric 	if (!blocksize)
    192   1.1     elric 		return NULL;
    193   1.1     elric 	if (keylen != 128 && keylen != 192 && keylen != 256)
    194   1.1     elric 		return NULL;
    195   1.6  christos 	if (*blocksize == (size_t)-1)
    196   1.1     elric 		*blocksize = 128;
    197   1.1     elric 	if (*blocksize != 128)
    198   1.1     elric 		return NULL;
    199  1.21  riastrad 	ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    200   1.1     elric 	rijndael_makeKey(&ap->ap_enckey, DIR_ENCRYPT, keylen, key);
    201   1.1     elric 	rijndael_makeKey(&ap->ap_deckey, DIR_DECRYPT, keylen, key);
    202   1.6  christos 	return ap;
    203   1.1     elric }
    204   1.1     elric 
    205  1.11  christos static void
    206  1.14     alnsn cgd_cipher_aes_cbc_destroy(void *data)
    207   1.1     elric {
    208   1.6  christos 	struct aes_privdata *apd = data;
    209   1.1     elric 
    210  1.12  riastrad 	explicit_memset(apd, 0, sizeof(*apd));
    211  1.21  riastrad 	kmem_free(apd, sizeof(*apd));
    212   1.1     elric }
    213   1.1     elric 
    214  1.11  christos static void
    215   1.7    cbiere aes_cbc_enc_int(void *privdata, void *dst, const void *src, size_t len)
    216   1.1     elric {
    217   1.6  christos 	struct aes_encdata	*ae = privdata;
    218   1.1     elric 	cipherInstance		 cipher;
    219  1.14     alnsn 	int			 cipher_ok __diagused;
    220   1.1     elric 
    221  1.14     alnsn 	cipher_ok = rijndael_cipherInit(&cipher, MODE_CBC, ae->ae_iv);
    222  1.14     alnsn 	KASSERT(cipher_ok > 0);
    223  1.18  riastrad 	rijndael_blockEncrypt(&cipher, ae->ae_key, src, /*inputbits*/len * 8,
    224  1.18  riastrad 	    dst);
    225  1.18  riastrad 	(void)memcpy(ae->ae_iv, (uint8_t *)dst +
    226  1.15     alnsn 	    (len - CGD_AES_BLOCK_SIZE), CGD_AES_BLOCK_SIZE);
    227   1.1     elric }
    228   1.1     elric 
    229  1.11  christos static void
    230   1.7    cbiere aes_cbc_dec_int(void *privdata, void *dst, const void *src, size_t len)
    231   1.1     elric {
    232   1.6  christos 	struct aes_encdata	*ae = privdata;
    233   1.1     elric 	cipherInstance		 cipher;
    234  1.14     alnsn 	int			 cipher_ok __diagused;
    235   1.1     elric 
    236  1.14     alnsn 	cipher_ok = rijndael_cipherInit(&cipher, MODE_CBC, ae->ae_iv);
    237  1.14     alnsn 	KASSERT(cipher_ok > 0);
    238  1.18  riastrad 	rijndael_blockDecrypt(&cipher, ae->ae_key, src, /*inputbits*/len * 8,
    239  1.18  riastrad 	    dst);
    240  1.18  riastrad 	(void)memcpy(ae->ae_iv, (const uint8_t *)src +
    241  1.15     alnsn 	    (len - CGD_AES_BLOCK_SIZE), CGD_AES_BLOCK_SIZE);
    242   1.1     elric }
    243   1.1     elric 
    244  1.11  christos static void
    245   1.6  christos cgd_cipher_aes_cbc(void *privdata, struct uio *dstuio,
    246  1.13  riastrad     struct uio *srcuio, const void *iv, int dir)
    247   1.1     elric {
    248   1.6  christos 	struct aes_privdata	*apd = privdata;
    249   1.1     elric 	struct aes_encdata	 encd;
    250  1.18  riastrad 	cipherInstance		 cipher;
    251  1.18  riastrad 	int			 cipher_ok __diagused;
    252  1.18  riastrad 
    253  1.18  riastrad 	/* Compute the CBC IV as AES_k(iv).  */
    254  1.18  riastrad 	cipher_ok = rijndael_cipherInit(&cipher, MODE_ECB, NULL);
    255  1.18  riastrad 	KASSERT(cipher_ok > 0);
    256  1.18  riastrad 	rijndael_blockEncrypt(&cipher, &apd->ap_enckey, iv, /*inputbits*/128,
    257  1.18  riastrad 	    encd.ae_iv);
    258   1.1     elric 
    259   1.1     elric 	switch (dir) {
    260   1.1     elric 	case CGD_CIPHER_ENCRYPT:
    261   1.1     elric 		encd.ae_key = &apd->ap_enckey;
    262  1.14     alnsn 		cgd_cipher_uio(&encd, aes_cbc_enc_int, dstuio, srcuio);
    263   1.1     elric 		break;
    264   1.1     elric 	case CGD_CIPHER_DECRYPT:
    265   1.1     elric 		encd.ae_key = &apd->ap_deckey;
    266  1.14     alnsn 		cgd_cipher_uio(&encd, aes_cbc_dec_int, dstuio, srcuio);
    267  1.14     alnsn 		break;
    268  1.14     alnsn 	default:
    269  1.17  riastrad 		panic("%s: unrecognised direction %d", __func__, dir);
    270  1.14     alnsn 	}
    271  1.14     alnsn }
    272  1.14     alnsn 
    273  1.14     alnsn static void *
    274  1.14     alnsn cgd_cipher_aes_xts_init(size_t keylen, const void *xtskey, size_t *blocksize)
    275  1.14     alnsn {
    276  1.14     alnsn 	struct aes_privdata *ap;
    277  1.14     alnsn 	const char *key, *key2; /* XTS key is made of two AES keys. */
    278  1.14     alnsn 
    279  1.14     alnsn 	if (!blocksize)
    280  1.14     alnsn 		return NULL;
    281  1.14     alnsn 	if (keylen != 256 && keylen != 512)
    282  1.14     alnsn 		return NULL;
    283  1.14     alnsn 	if (*blocksize == (size_t)-1)
    284  1.14     alnsn 		*blocksize = 128;
    285  1.14     alnsn 	if (*blocksize != 128)
    286  1.14     alnsn 		return NULL;
    287  1.14     alnsn 
    288  1.21  riastrad 	ap = kmem_zalloc(2 * sizeof(*ap), KM_SLEEP);
    289  1.14     alnsn 	keylen /= 2;
    290  1.14     alnsn 	key = xtskey;
    291  1.14     alnsn 	key2 = key + keylen / CHAR_BIT;
    292  1.14     alnsn 
    293  1.14     alnsn 	rijndael_makeKey(&ap[0].ap_enckey, DIR_ENCRYPT, keylen, key);
    294  1.14     alnsn 	rijndael_makeKey(&ap[0].ap_deckey, DIR_DECRYPT, keylen, key);
    295  1.14     alnsn 	rijndael_makeKey(&ap[1].ap_enckey, DIR_ENCRYPT, keylen, key2);
    296  1.14     alnsn 
    297  1.14     alnsn 	return ap;
    298  1.14     alnsn }
    299  1.14     alnsn 
    300  1.14     alnsn static void
    301  1.14     alnsn cgd_cipher_aes_xts_destroy(void *data)
    302  1.14     alnsn {
    303  1.14     alnsn 	struct aes_privdata *apd = data;
    304  1.14     alnsn 
    305  1.14     alnsn 	explicit_memset(apd, 0, 2 * sizeof(*apd));
    306  1.21  riastrad 	kmem_free(apd, 2 * sizeof(*apd));
    307  1.14     alnsn }
    308  1.14     alnsn 
    309  1.14     alnsn static void
    310  1.14     alnsn aes_xts_enc_int(void *privdata, void *dst, const void *src, size_t len)
    311  1.14     alnsn {
    312  1.14     alnsn 	struct aes_encdata	*ae = privdata;
    313  1.14     alnsn 	cipherInstance		 cipher;
    314  1.14     alnsn 	int			 cipher_ok __diagused;
    315  1.14     alnsn 
    316  1.14     alnsn 	cipher_ok = rijndael_cipherInit(&cipher, MODE_XTS, ae->ae_iv);
    317  1.14     alnsn 	KASSERT(cipher_ok > 0);
    318  1.18  riastrad 	rijndael_blockEncrypt(&cipher, ae->ae_key, src, /*inputbits*/len * 8,
    319  1.18  riastrad 	    dst);
    320  1.15     alnsn 	(void)memcpy(ae->ae_iv, cipher.IV, CGD_AES_BLOCK_SIZE);
    321  1.14     alnsn }
    322  1.14     alnsn 
    323  1.14     alnsn static void
    324  1.14     alnsn aes_xts_dec_int(void *privdata, void *dst, const void *src, size_t len)
    325  1.14     alnsn {
    326  1.14     alnsn 	struct aes_encdata	*ae = privdata;
    327  1.14     alnsn 	cipherInstance		 cipher;
    328  1.14     alnsn 	int			 cipher_ok __diagused;
    329  1.14     alnsn 
    330  1.14     alnsn 	cipher_ok = rijndael_cipherInit(&cipher, MODE_XTS, ae->ae_iv);
    331  1.14     alnsn 	KASSERT(cipher_ok > 0);
    332  1.18  riastrad 	rijndael_blockDecrypt(&cipher, ae->ae_key, src, /*inputbits*/len * 8,
    333  1.18  riastrad 	    dst);
    334  1.15     alnsn 	(void)memcpy(ae->ae_iv, cipher.IV, CGD_AES_BLOCK_SIZE);
    335  1.14     alnsn }
    336  1.14     alnsn 
    337  1.14     alnsn static void
    338  1.14     alnsn cgd_cipher_aes_xts(void *privdata, struct uio *dstuio,
    339  1.14     alnsn     struct uio *srcuio, const void *iv, int dir)
    340  1.14     alnsn {
    341  1.14     alnsn 	struct aes_privdata	*apd = privdata;
    342  1.14     alnsn 	struct aes_encdata	 encd;
    343  1.18  riastrad 	cipherInstance		 cipher;
    344  1.18  riastrad 	int			 cipher_ok __diagused;
    345  1.18  riastrad 
    346  1.18  riastrad 	cipher_ok = rijndael_cipherInit(&cipher, MODE_ECB, NULL);
    347  1.18  riastrad 	KASSERT(cipher_ok > 0);
    348  1.18  riastrad 	rijndael_blockEncrypt(&cipher, &apd[1].ap_enckey, iv, /*inputbits*/128,
    349  1.18  riastrad 	    encd.ae_iv);
    350  1.14     alnsn 
    351  1.14     alnsn 	switch (dir) {
    352  1.14     alnsn 	case CGD_CIPHER_ENCRYPT:
    353  1.14     alnsn 		encd.ae_key = &apd->ap_enckey;
    354  1.14     alnsn 		cgd_cipher_uio(&encd, aes_xts_enc_int, dstuio, srcuio);
    355  1.14     alnsn 		break;
    356  1.14     alnsn 	case CGD_CIPHER_DECRYPT:
    357  1.14     alnsn 		encd.ae_key = &apd->ap_deckey;
    358  1.14     alnsn 		cgd_cipher_uio(&encd, aes_xts_dec_int, dstuio, srcuio);
    359   1.1     elric 		break;
    360   1.1     elric 	default:
    361  1.17  riastrad 		panic("%s: unrecognised direction %d", __func__, dir);
    362   1.1     elric 	}
    363   1.1     elric }
    364   1.1     elric 
    365   1.1     elric /*
    366   1.1     elric  * 3DES Framework
    367   1.1     elric  */
    368   1.1     elric 
    369   1.1     elric struct c3des_privdata {
    370   1.1     elric 	des_key_schedule	cp_key1;
    371   1.1     elric 	des_key_schedule	cp_key2;
    372   1.1     elric 	des_key_schedule	cp_key3;
    373   1.1     elric };
    374   1.1     elric 
    375   1.1     elric struct c3des_encdata {
    376   1.1     elric 	des_key_schedule	*ce_key1;
    377   1.1     elric 	des_key_schedule	*ce_key2;
    378   1.1     elric 	des_key_schedule	*ce_key3;
    379  1.18  riastrad 	uint8_t			ce_iv[CGD_3DES_BLOCK_SIZE];
    380   1.1     elric };
    381   1.1     elric 
    382  1.11  christos static void *
    383   1.7    cbiere cgd_cipher_3des_init(size_t keylen, const void *key, size_t *blocksize)
    384   1.1     elric {
    385   1.1     elric 	struct	c3des_privdata *cp;
    386   1.1     elric 	int	error = 0;
    387   1.7    cbiere 	des_cblock *block;
    388   1.1     elric 
    389   1.1     elric 	if (!blocksize)
    390   1.1     elric 		return NULL;
    391   1.6  christos 	if (*blocksize == (size_t)-1)
    392   1.1     elric 		*blocksize = 64;
    393   1.1     elric 	if (keylen != (DES_KEY_SZ * 3 * 8) || *blocksize != 64)
    394   1.1     elric 		return NULL;
    395  1.21  riastrad 	cp = kmem_zalloc(sizeof(*cp), KM_SLEEP);
    396   1.7    cbiere 	block = __UNCONST(key);
    397   1.7    cbiere 	error  = des_key_sched(block, cp->cp_key1);
    398   1.7    cbiere 	error |= des_key_sched(block + 1, cp->cp_key2);
    399   1.7    cbiere 	error |= des_key_sched(block + 2, cp->cp_key3);
    400   1.1     elric 	if (error) {
    401  1.12  riastrad 		explicit_memset(cp, 0, sizeof(*cp));
    402  1.21  riastrad 		kmem_free(cp, sizeof(*cp));
    403   1.1     elric 		return NULL;
    404   1.1     elric 	}
    405   1.6  christos 	return cp;
    406   1.1     elric }
    407   1.1     elric 
    408  1.11  christos static void
    409   1.6  christos cgd_cipher_3des_destroy(void *data)
    410   1.1     elric {
    411   1.6  christos 	struct c3des_privdata *cp = data;
    412   1.1     elric 
    413  1.12  riastrad 	explicit_memset(cp, 0, sizeof(*cp));
    414  1.21  riastrad 	kmem_free(cp, sizeof(*cp));
    415   1.1     elric }
    416   1.1     elric 
    417   1.1     elric static void
    418   1.7    cbiere c3des_cbc_enc_int(void *privdata, void *dst, const void *src, size_t len)
    419   1.1     elric {
    420   1.6  christos 	struct	c3des_encdata *ce = privdata;
    421   1.1     elric 
    422   1.1     elric 	des_ede3_cbc_encrypt(src, dst, len, *ce->ce_key1, *ce->ce_key2,
    423  1.18  riastrad 	    *ce->ce_key3, (des_cblock *)ce->ce_iv, /*encrypt*/1);
    424  1.18  riastrad 	(void)memcpy(ce->ce_iv, (const uint8_t *)dst +
    425  1.15     alnsn 	    (len - CGD_3DES_BLOCK_SIZE), CGD_3DES_BLOCK_SIZE);
    426   1.1     elric }
    427   1.1     elric 
    428   1.1     elric static void
    429   1.7    cbiere c3des_cbc_dec_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*/0);
    435  1.18  riastrad 	(void)memcpy(ce->ce_iv, (const uint8_t *)src +
    436  1.15     alnsn 	    (len - CGD_3DES_BLOCK_SIZE), CGD_3DES_BLOCK_SIZE);
    437   1.1     elric }
    438   1.1     elric 
    439  1.11  christos static void
    440   1.6  christos cgd_cipher_3des_cbc(void *privdata, struct uio *dstuio,
    441  1.13  riastrad 	struct uio *srcuio, const void *iv, int dir)
    442   1.1     elric {
    443   1.6  christos 	struct	c3des_privdata *cp = privdata;
    444   1.1     elric 	struct	c3des_encdata ce;
    445  1.18  riastrad 	des_cblock zero;
    446  1.18  riastrad 
    447  1.18  riastrad 	/* Compute the CBC IV as 3DES_k(iv) = 3DES-CBC_k(iv, 0).  */
    448  1.18  riastrad 	memset(&zero, 0, sizeof(zero));
    449  1.18  riastrad 	des_ede3_cbc_encrypt(iv, ce.ce_iv, CGD_3DES_BLOCK_SIZE,
    450  1.18  riastrad 	    cp->cp_key1, cp->cp_key2, cp->cp_key3, &zero, /*encrypt*/1);
    451   1.1     elric 
    452   1.1     elric 	ce.ce_key1 = &cp->cp_key1;
    453   1.1     elric 	ce.ce_key2 = &cp->cp_key2;
    454   1.1     elric 	ce.ce_key3 = &cp->cp_key3;
    455   1.1     elric 	switch (dir) {
    456   1.1     elric 	case CGD_CIPHER_ENCRYPT:
    457  1.14     alnsn 		cgd_cipher_uio(&ce, c3des_cbc_enc_int, dstuio, srcuio);
    458   1.1     elric 		break;
    459   1.1     elric 	case CGD_CIPHER_DECRYPT:
    460  1.14     alnsn 		cgd_cipher_uio(&ce, c3des_cbc_dec_int, dstuio, srcuio);
    461   1.1     elric 		break;
    462   1.1     elric 	default:
    463  1.17  riastrad 		panic("%s: unrecognised direction %d", __func__, dir);
    464   1.1     elric 	}
    465   1.1     elric }
    466   1.1     elric 
    467   1.1     elric /*
    468   1.1     elric  * Blowfish Framework
    469   1.1     elric  */
    470   1.1     elric 
    471   1.1     elric struct bf_privdata {
    472   1.1     elric 	BF_KEY	bp_key;
    473   1.1     elric };
    474   1.1     elric 
    475   1.1     elric struct bf_encdata {
    476   1.1     elric 	BF_KEY		*be_key;
    477  1.18  riastrad 	uint8_t		 be_iv[CGD_BF_BLOCK_SIZE];
    478   1.1     elric };
    479   1.1     elric 
    480  1.11  christos static void *
    481   1.7    cbiere cgd_cipher_bf_init(size_t keylen, const void *key, size_t *blocksize)
    482   1.1     elric {
    483   1.1     elric 	struct	bf_privdata *bp;
    484   1.1     elric 
    485   1.1     elric 	if (!blocksize)
    486   1.1     elric 		return NULL;
    487   1.3       dan 	if (keylen < 40 || keylen > 448 || (keylen % 8 != 0))
    488   1.1     elric 		return NULL;
    489   1.6  christos 	if (*blocksize == (size_t)-1)
    490   1.1     elric 		*blocksize = 64;
    491   1.1     elric 	if (*blocksize != 64)
    492   1.1     elric 		return NULL;
    493  1.21  riastrad 	bp = kmem_zalloc(sizeof(*bp), KM_SLEEP);
    494   1.1     elric 	if (!bp)
    495   1.1     elric 		return NULL;
    496   1.3       dan 	BF_set_key(&bp->bp_key, keylen / 8, key);
    497   1.6  christos 	return bp;
    498   1.1     elric }
    499   1.1     elric 
    500  1.11  christos static void
    501   1.6  christos cgd_cipher_bf_destroy(void *data)
    502   1.1     elric {
    503   1.6  christos 	struct	bf_privdata *bp = data;
    504   1.1     elric 
    505  1.12  riastrad 	explicit_memset(bp, 0, sizeof(*bp));
    506  1.21  riastrad 	kmem_free(bp, sizeof(*bp));
    507   1.1     elric }
    508   1.1     elric 
    509  1.11  christos static void
    510   1.7    cbiere bf_cbc_enc_int(void *privdata, void *dst, const void *src, size_t len)
    511   1.1     elric {
    512   1.6  christos 	struct	bf_encdata *be = privdata;
    513   1.1     elric 
    514  1.18  riastrad 	BF_cbc_encrypt(src, dst, len, be->be_key, be->be_iv, /*encrypt*/1);
    515  1.18  riastrad 	(void)memcpy(be->be_iv, (uint8_t *)dst +
    516  1.15     alnsn 	    (len - CGD_BF_BLOCK_SIZE), CGD_BF_BLOCK_SIZE);
    517   1.1     elric }
    518   1.1     elric 
    519  1.11  christos static void
    520   1.7    cbiere bf_cbc_dec_int(void *privdata, void *dst, const void *src, size_t len)
    521   1.1     elric {
    522   1.6  christos 	struct	bf_encdata *be = privdata;
    523   1.1     elric 
    524  1.18  riastrad 	BF_cbc_encrypt(src, dst, len, be->be_key, be->be_iv, /*encrypt*/0);
    525  1.18  riastrad 	(void)memcpy(be->be_iv, (const uint8_t *)src +
    526  1.15     alnsn 	    (len - CGD_BF_BLOCK_SIZE), CGD_BF_BLOCK_SIZE);
    527   1.1     elric }
    528   1.1     elric 
    529  1.11  christos static void
    530   1.6  christos cgd_cipher_bf_cbc(void *privdata, struct uio *dstuio,
    531  1.13  riastrad     struct uio *srcuio, const void *iv, int dir)
    532   1.1     elric {
    533   1.6  christos 	struct	bf_privdata *bp = privdata;
    534   1.1     elric 	struct	bf_encdata be;
    535  1.18  riastrad 	char	zero_iv[CGD_BF_BLOCK_SIZE];
    536  1.18  riastrad 
    537  1.18  riastrad 	/* Compute the CBC IV as Blowfish_k(iv) = BF_CBC_k(iv, 0).  */
    538  1.18  riastrad 	memset(zero_iv, 0, sizeof(zero_iv));
    539  1.18  riastrad 	BF_cbc_encrypt(iv, be.be_iv, CGD_BF_BLOCK_SIZE, &bp->bp_key, zero_iv,
    540  1.18  riastrad 	    /*encrypt*/1);
    541   1.1     elric 
    542   1.1     elric 	be.be_key = &bp->bp_key;
    543   1.1     elric 	switch (dir) {
    544   1.1     elric 	case CGD_CIPHER_ENCRYPT:
    545  1.14     alnsn 		cgd_cipher_uio(&be, bf_cbc_enc_int, dstuio, srcuio);
    546   1.1     elric 		break;
    547   1.1     elric 	case CGD_CIPHER_DECRYPT:
    548  1.14     alnsn 		cgd_cipher_uio(&be, bf_cbc_dec_int, dstuio, srcuio);
    549   1.1     elric 		break;
    550   1.1     elric 	default:
    551  1.17  riastrad 		panic("%s: unrecognised direction %d", __func__, dir);
    552   1.1     elric 	}
    553   1.1     elric 
    554   1.1     elric }
    555