Home | History | Annotate | Line # | Download | only in dev
cgd_crypto.c revision 1.26
      1  1.26  riastrad /* $NetBSD: cgd_crypto.c,v 1.26 2020/06/29 23:44:01 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.26  riastrad __KERNEL_RCSID(0, "$NetBSD: cgd_crypto.c,v 1.26 2020/06/29 23:44:01 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.26  riastrad #include <crypto/adiantum/adiantum.h>
     49  1.24  riastrad #include <crypto/aes/aes.h>
     50  1.19  riastrad #include <crypto/blowfish/blowfish.h>
     51  1.19  riastrad #include <crypto/des/des.h>
     52  1.11  christos 
     53   1.1     elric /*
     54   1.1     elric  * The general framework provides only one generic function.
     55  1.16  gutterid  * It takes the name of an algorithm and returns a struct cryptfuncs *
     56   1.1     elric  * for it.  It is up to the initialisation routines of the algorithm
     57   1.1     elric  * to check key size and block size.
     58   1.1     elric  */
     59   1.1     elric 
     60  1.14     alnsn static cfunc_init		cgd_cipher_aes_cbc_init;
     61  1.14     alnsn static cfunc_destroy		cgd_cipher_aes_cbc_destroy;
     62  1.14     alnsn static cfunc_cipher		cgd_cipher_aes_cbc;
     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 
     68  1.14     alnsn static cfunc_init		cgd_cipher_3des_init;
     69  1.14     alnsn static cfunc_destroy		cgd_cipher_3des_destroy;
     70  1.14     alnsn static cfunc_cipher		cgd_cipher_3des_cbc;
     71  1.14     alnsn 
     72  1.14     alnsn static cfunc_init		cgd_cipher_bf_init;
     73  1.14     alnsn static cfunc_destroy		cgd_cipher_bf_destroy;
     74  1.14     alnsn static cfunc_cipher		cgd_cipher_bf_cbc;
     75  1.11  christos 
     76  1.26  riastrad static cfunc_init		cgd_cipher_adiantum_init;
     77  1.26  riastrad static cfunc_destroy		cgd_cipher_adiantum_destroy;
     78  1.26  riastrad static cfunc_cipher		cgd_cipher_adiantum_crypt;
     79  1.26  riastrad 
     80  1.11  christos static const struct cryptfuncs cf[] = {
     81  1.11  christos 	{
     82  1.14     alnsn 		.cf_name	= "aes-xts",
     83  1.14     alnsn 		.cf_init	= cgd_cipher_aes_xts_init,
     84  1.14     alnsn 		.cf_destroy	= cgd_cipher_aes_xts_destroy,
     85  1.14     alnsn 		.cf_cipher	= cgd_cipher_aes_xts,
     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.11  christos 	},
     93  1.11  christos 	{
     94  1.11  christos 		.cf_name	= "3des-cbc",
     95  1.11  christos 		.cf_init	= cgd_cipher_3des_init,
     96  1.11  christos 		.cf_destroy	= cgd_cipher_3des_destroy,
     97  1.11  christos 		.cf_cipher	= cgd_cipher_3des_cbc,
     98  1.11  christos 	},
     99  1.11  christos 	{
    100  1.11  christos 		.cf_name	= "blowfish-cbc",
    101  1.11  christos 		.cf_init	= cgd_cipher_bf_init,
    102  1.11  christos 		.cf_destroy	= cgd_cipher_bf_destroy,
    103  1.11  christos 		.cf_cipher	= cgd_cipher_bf_cbc,
    104  1.11  christos 	},
    105  1.26  riastrad 	{
    106  1.26  riastrad 		.cf_name	= "adiantum",
    107  1.26  riastrad 		.cf_init	= cgd_cipher_adiantum_init,
    108  1.26  riastrad 		.cf_destroy	= cgd_cipher_adiantum_destroy,
    109  1.26  riastrad 		.cf_cipher	= cgd_cipher_adiantum_crypt,
    110  1.26  riastrad 	},
    111  1.11  christos };
    112  1.11  christos const struct cryptfuncs *
    113   1.6  christos cryptfuncs_find(const char *alg)
    114   1.1     elric {
    115   1.1     elric 
    116  1.11  christos 	for (size_t i = 0; i < __arraycount(cf); i++)
    117  1.11  christos 		if (strcmp(cf[i].cf_name, alg) == 0)
    118  1.11  christos 			return &cf[i];
    119  1.11  christos 
    120   1.1     elric 	return NULL;
    121   1.1     elric }
    122   1.1     elric 
    123   1.1     elric /*
    124   1.1     elric  *  AES Framework
    125   1.1     elric  */
    126   1.1     elric 
    127   1.1     elric struct aes_privdata {
    128  1.24  riastrad 	struct aesenc	ap_enckey;
    129  1.24  riastrad 	struct aesdec	ap_deckey;
    130  1.24  riastrad 	uint32_t	ap_nrounds;
    131   1.1     elric };
    132   1.1     elric 
    133  1.11  christos static void *
    134  1.14     alnsn cgd_cipher_aes_cbc_init(size_t keylen, const void *key, size_t *blocksize)
    135   1.1     elric {
    136   1.1     elric 	struct	aes_privdata *ap;
    137   1.1     elric 
    138   1.1     elric 	if (!blocksize)
    139   1.1     elric 		return NULL;
    140   1.1     elric 	if (keylen != 128 && keylen != 192 && keylen != 256)
    141   1.1     elric 		return NULL;
    142   1.6  christos 	if (*blocksize == (size_t)-1)
    143   1.1     elric 		*blocksize = 128;
    144   1.1     elric 	if (*blocksize != 128)
    145   1.1     elric 		return NULL;
    146  1.21  riastrad 	ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    147  1.24  riastrad 	switch (keylen) {
    148  1.24  riastrad 	case 128:
    149  1.24  riastrad 		aes_setenckey128(&ap->ap_enckey, key);
    150  1.24  riastrad 		aes_setdeckey128(&ap->ap_deckey, key);
    151  1.24  riastrad 		ap->ap_nrounds = AES_128_NROUNDS;
    152  1.24  riastrad 		break;
    153  1.24  riastrad 	case 192:
    154  1.24  riastrad 		aes_setenckey192(&ap->ap_enckey, key);
    155  1.24  riastrad 		aes_setdeckey192(&ap->ap_deckey, key);
    156  1.24  riastrad 		ap->ap_nrounds = AES_192_NROUNDS;
    157  1.24  riastrad 		break;
    158  1.24  riastrad 	case 256:
    159  1.24  riastrad 		aes_setenckey256(&ap->ap_enckey, key);
    160  1.24  riastrad 		aes_setdeckey256(&ap->ap_deckey, key);
    161  1.24  riastrad 		ap->ap_nrounds = AES_256_NROUNDS;
    162  1.24  riastrad 		break;
    163  1.24  riastrad 	}
    164   1.6  christos 	return ap;
    165   1.1     elric }
    166   1.1     elric 
    167  1.11  christos static void
    168  1.14     alnsn cgd_cipher_aes_cbc_destroy(void *data)
    169   1.1     elric {
    170   1.6  christos 	struct aes_privdata *apd = data;
    171   1.1     elric 
    172  1.12  riastrad 	explicit_memset(apd, 0, sizeof(*apd));
    173  1.21  riastrad 	kmem_free(apd, sizeof(*apd));
    174   1.1     elric }
    175   1.1     elric 
    176  1.11  christos static void
    177  1.23  riastrad cgd_cipher_aes_cbc(void *privdata, void *dst, const void *src, size_t nbytes,
    178  1.23  riastrad     const void *blkno, int dir)
    179   1.1     elric {
    180   1.6  christos 	struct aes_privdata	*apd = privdata;
    181  1.25  riastrad 	uint8_t iv[CGD_AES_BLOCK_SIZE] __aligned(CGD_AES_BLOCK_SIZE) = {0};
    182  1.18  riastrad 
    183  1.23  riastrad 	/* Compute the CBC IV as AES_k(blkno).  */
    184  1.24  riastrad 	aes_enc(&apd->ap_enckey, blkno, iv, apd->ap_nrounds);
    185   1.1     elric 
    186   1.1     elric 	switch (dir) {
    187   1.1     elric 	case CGD_CIPHER_ENCRYPT:
    188  1.24  riastrad 		aes_cbc_enc(&apd->ap_enckey, src, dst, nbytes, iv,
    189  1.24  riastrad 		    apd->ap_nrounds);
    190   1.1     elric 		break;
    191   1.1     elric 	case CGD_CIPHER_DECRYPT:
    192  1.24  riastrad 		aes_cbc_dec(&apd->ap_deckey, src, dst, nbytes, iv,
    193  1.24  riastrad 		    apd->ap_nrounds);
    194  1.14     alnsn 		break;
    195  1.14     alnsn 	default:
    196  1.17  riastrad 		panic("%s: unrecognised direction %d", __func__, dir);
    197  1.14     alnsn 	}
    198  1.14     alnsn }
    199  1.14     alnsn 
    200  1.22  riastrad /*
    201  1.22  riastrad  * AES-XTS
    202  1.22  riastrad  */
    203  1.22  riastrad 
    204  1.22  riastrad struct aesxts {
    205  1.24  riastrad 	struct aesenc	ax_enckey;
    206  1.24  riastrad 	struct aesdec	ax_deckey;
    207  1.24  riastrad 	struct aesenc	ax_tweakkey;
    208  1.24  riastrad 	uint32_t	ax_nrounds;
    209  1.22  riastrad };
    210  1.22  riastrad 
    211  1.14     alnsn static void *
    212  1.14     alnsn cgd_cipher_aes_xts_init(size_t keylen, const void *xtskey, size_t *blocksize)
    213  1.14     alnsn {
    214  1.22  riastrad 	struct aesxts *ax;
    215  1.14     alnsn 	const char *key, *key2; /* XTS key is made of two AES keys. */
    216  1.14     alnsn 
    217  1.14     alnsn 	if (!blocksize)
    218  1.14     alnsn 		return NULL;
    219  1.14     alnsn 	if (keylen != 256 && keylen != 512)
    220  1.14     alnsn 		return NULL;
    221  1.14     alnsn 	if (*blocksize == (size_t)-1)
    222  1.14     alnsn 		*blocksize = 128;
    223  1.14     alnsn 	if (*blocksize != 128)
    224  1.14     alnsn 		return NULL;
    225  1.14     alnsn 
    226  1.22  riastrad 	ax = kmem_zalloc(sizeof(*ax), KM_SLEEP);
    227  1.14     alnsn 	keylen /= 2;
    228  1.14     alnsn 	key = xtskey;
    229  1.14     alnsn 	key2 = key + keylen / CHAR_BIT;
    230  1.14     alnsn 
    231  1.24  riastrad 	switch (keylen) {
    232  1.24  riastrad 	case 128:
    233  1.24  riastrad 		aes_setenckey128(&ax->ax_enckey, key);
    234  1.24  riastrad 		aes_setdeckey128(&ax->ax_deckey, key);
    235  1.24  riastrad 		aes_setenckey128(&ax->ax_tweakkey, key2);
    236  1.24  riastrad 		ax->ax_nrounds = AES_128_NROUNDS;
    237  1.24  riastrad 		break;
    238  1.24  riastrad 	case 256:
    239  1.24  riastrad 		aes_setenckey256(&ax->ax_enckey, key);
    240  1.24  riastrad 		aes_setdeckey256(&ax->ax_deckey, key);
    241  1.24  riastrad 		aes_setenckey256(&ax->ax_tweakkey, key2);
    242  1.24  riastrad 		ax->ax_nrounds = AES_256_NROUNDS;
    243  1.24  riastrad 		break;
    244  1.24  riastrad 	}
    245  1.14     alnsn 
    246  1.22  riastrad 	return ax;
    247  1.14     alnsn }
    248  1.14     alnsn 
    249  1.14     alnsn static void
    250  1.22  riastrad cgd_cipher_aes_xts_destroy(void *cookie)
    251  1.14     alnsn {
    252  1.22  riastrad 	struct aesxts *ax = cookie;
    253  1.14     alnsn 
    254  1.22  riastrad 	explicit_memset(ax, 0, sizeof(*ax));
    255  1.22  riastrad 	kmem_free(ax, sizeof(*ax));
    256  1.14     alnsn }
    257  1.14     alnsn 
    258  1.14     alnsn static void
    259  1.23  riastrad cgd_cipher_aes_xts(void *cookie, void *dst, const void *src, size_t nbytes,
    260  1.23  riastrad     const void *blkno, int dir)
    261  1.14     alnsn {
    262  1.22  riastrad 	struct aesxts *ax = cookie;
    263  1.23  riastrad 	uint8_t tweak[CGD_AES_BLOCK_SIZE];
    264  1.18  riastrad 
    265  1.23  riastrad 	/* Compute the initial tweak as AES_k(blkno).  */
    266  1.24  riastrad 	aes_enc(&ax->ax_tweakkey, blkno, tweak, ax->ax_nrounds);
    267  1.14     alnsn 
    268  1.14     alnsn 	switch (dir) {
    269  1.14     alnsn 	case CGD_CIPHER_ENCRYPT:
    270  1.24  riastrad 		aes_xts_enc(&ax->ax_enckey, src, dst, nbytes, tweak,
    271  1.24  riastrad 		    ax->ax_nrounds);
    272  1.14     alnsn 		break;
    273  1.14     alnsn 	case CGD_CIPHER_DECRYPT:
    274  1.24  riastrad 		aes_xts_dec(&ax->ax_deckey, src, dst, nbytes, tweak,
    275  1.24  riastrad 		    ax->ax_nrounds);
    276   1.1     elric 		break;
    277   1.1     elric 	default:
    278  1.17  riastrad 		panic("%s: unrecognised direction %d", __func__, dir);
    279   1.1     elric 	}
    280   1.1     elric }
    281   1.1     elric 
    282   1.1     elric /*
    283   1.1     elric  * 3DES Framework
    284   1.1     elric  */
    285   1.1     elric 
    286   1.1     elric struct c3des_privdata {
    287   1.1     elric 	des_key_schedule	cp_key1;
    288   1.1     elric 	des_key_schedule	cp_key2;
    289   1.1     elric 	des_key_schedule	cp_key3;
    290   1.1     elric };
    291   1.1     elric 
    292  1.11  christos static void *
    293   1.7    cbiere cgd_cipher_3des_init(size_t keylen, const void *key, size_t *blocksize)
    294   1.1     elric {
    295   1.1     elric 	struct	c3des_privdata *cp;
    296   1.1     elric 	int	error = 0;
    297   1.7    cbiere 	des_cblock *block;
    298   1.1     elric 
    299   1.1     elric 	if (!blocksize)
    300   1.1     elric 		return NULL;
    301   1.6  christos 	if (*blocksize == (size_t)-1)
    302   1.1     elric 		*blocksize = 64;
    303   1.1     elric 	if (keylen != (DES_KEY_SZ * 3 * 8) || *blocksize != 64)
    304   1.1     elric 		return NULL;
    305  1.21  riastrad 	cp = kmem_zalloc(sizeof(*cp), KM_SLEEP);
    306   1.7    cbiere 	block = __UNCONST(key);
    307   1.7    cbiere 	error  = des_key_sched(block, cp->cp_key1);
    308   1.7    cbiere 	error |= des_key_sched(block + 1, cp->cp_key2);
    309   1.7    cbiere 	error |= des_key_sched(block + 2, cp->cp_key3);
    310   1.1     elric 	if (error) {
    311  1.12  riastrad 		explicit_memset(cp, 0, sizeof(*cp));
    312  1.21  riastrad 		kmem_free(cp, sizeof(*cp));
    313   1.1     elric 		return NULL;
    314   1.1     elric 	}
    315   1.6  christos 	return cp;
    316   1.1     elric }
    317   1.1     elric 
    318  1.11  christos static void
    319   1.6  christos cgd_cipher_3des_destroy(void *data)
    320   1.1     elric {
    321   1.6  christos 	struct c3des_privdata *cp = data;
    322   1.1     elric 
    323  1.12  riastrad 	explicit_memset(cp, 0, sizeof(*cp));
    324  1.21  riastrad 	kmem_free(cp, sizeof(*cp));
    325   1.1     elric }
    326   1.1     elric 
    327   1.1     elric static void
    328  1.23  riastrad cgd_cipher_3des_cbc(void *privdata, void *dst, const void *src, size_t nbytes,
    329  1.23  riastrad     const void *blkno, int dir)
    330   1.1     elric {
    331   1.6  christos 	struct	c3des_privdata *cp = privdata;
    332  1.18  riastrad 	des_cblock zero;
    333  1.23  riastrad 	uint8_t iv[CGD_3DES_BLOCK_SIZE];
    334  1.18  riastrad 
    335  1.23  riastrad 	/* Compute the CBC IV as 3DES_k(blkno) = 3DES-CBC_k(iv=blkno, 0).  */
    336  1.18  riastrad 	memset(&zero, 0, sizeof(zero));
    337  1.23  riastrad 	des_ede3_cbc_encrypt(blkno, iv, CGD_3DES_BLOCK_SIZE,
    338  1.18  riastrad 	    cp->cp_key1, cp->cp_key2, cp->cp_key3, &zero, /*encrypt*/1);
    339   1.1     elric 
    340   1.1     elric 	switch (dir) {
    341   1.1     elric 	case CGD_CIPHER_ENCRYPT:
    342  1.23  riastrad 		des_ede3_cbc_encrypt(src, dst, nbytes,
    343  1.23  riastrad 		    cp->cp_key1, cp->cp_key2, cp->cp_key3,
    344  1.23  riastrad 		    (des_cblock *)iv, /*encrypt*/1);
    345   1.1     elric 		break;
    346   1.1     elric 	case CGD_CIPHER_DECRYPT:
    347  1.23  riastrad 		des_ede3_cbc_encrypt(src, dst, nbytes,
    348  1.23  riastrad 		    cp->cp_key1, cp->cp_key2, cp->cp_key3,
    349  1.23  riastrad 		    (des_cblock *)iv, /*encrypt*/0);
    350   1.1     elric 		break;
    351   1.1     elric 	default:
    352  1.17  riastrad 		panic("%s: unrecognised direction %d", __func__, dir);
    353   1.1     elric 	}
    354   1.1     elric }
    355   1.1     elric 
    356   1.1     elric /*
    357   1.1     elric  * Blowfish Framework
    358   1.1     elric  */
    359   1.1     elric 
    360   1.1     elric struct bf_privdata {
    361   1.1     elric 	BF_KEY	bp_key;
    362   1.1     elric };
    363   1.1     elric 
    364   1.1     elric struct bf_encdata {
    365   1.1     elric 	BF_KEY		*be_key;
    366  1.18  riastrad 	uint8_t		 be_iv[CGD_BF_BLOCK_SIZE];
    367   1.1     elric };
    368   1.1     elric 
    369  1.11  christos static void *
    370   1.7    cbiere cgd_cipher_bf_init(size_t keylen, const void *key, size_t *blocksize)
    371   1.1     elric {
    372   1.1     elric 	struct	bf_privdata *bp;
    373   1.1     elric 
    374   1.1     elric 	if (!blocksize)
    375   1.1     elric 		return NULL;
    376   1.3       dan 	if (keylen < 40 || keylen > 448 || (keylen % 8 != 0))
    377   1.1     elric 		return NULL;
    378   1.6  christos 	if (*blocksize == (size_t)-1)
    379   1.1     elric 		*blocksize = 64;
    380   1.1     elric 	if (*blocksize != 64)
    381   1.1     elric 		return NULL;
    382  1.21  riastrad 	bp = kmem_zalloc(sizeof(*bp), KM_SLEEP);
    383   1.1     elric 	if (!bp)
    384   1.1     elric 		return NULL;
    385   1.3       dan 	BF_set_key(&bp->bp_key, keylen / 8, key);
    386   1.6  christos 	return bp;
    387   1.1     elric }
    388   1.1     elric 
    389  1.11  christos static void
    390   1.6  christos cgd_cipher_bf_destroy(void *data)
    391   1.1     elric {
    392   1.6  christos 	struct	bf_privdata *bp = data;
    393   1.1     elric 
    394  1.12  riastrad 	explicit_memset(bp, 0, sizeof(*bp));
    395  1.21  riastrad 	kmem_free(bp, sizeof(*bp));
    396   1.1     elric }
    397   1.1     elric 
    398  1.11  christos static void
    399  1.23  riastrad cgd_cipher_bf_cbc(void *privdata, void *dst, const void *src, size_t nbytes,
    400  1.23  riastrad     const void *blkno, int dir)
    401   1.1     elric {
    402   1.6  christos 	struct	bf_privdata *bp = privdata;
    403  1.23  riastrad 	uint8_t zero[CGD_BF_BLOCK_SIZE], iv[CGD_BF_BLOCK_SIZE];
    404  1.18  riastrad 
    405  1.23  riastrad 	/* Compute the CBC IV as Blowfish_k(blkno) = BF_CBC_k(blkno, 0).  */
    406  1.23  riastrad 	memset(zero, 0, sizeof(zero));
    407  1.23  riastrad 	BF_cbc_encrypt(blkno, iv, CGD_BF_BLOCK_SIZE, &bp->bp_key, zero,
    408  1.18  riastrad 	    /*encrypt*/1);
    409   1.1     elric 
    410   1.1     elric 	switch (dir) {
    411   1.1     elric 	case CGD_CIPHER_ENCRYPT:
    412  1.23  riastrad 		BF_cbc_encrypt(src, dst, nbytes, &bp->bp_key, iv,
    413  1.23  riastrad 		    /*encrypt*/1);
    414   1.1     elric 		break;
    415   1.1     elric 	case CGD_CIPHER_DECRYPT:
    416  1.23  riastrad 		BF_cbc_encrypt(src, dst, nbytes, &bp->bp_key, iv,
    417  1.23  riastrad 		    /*encrypt*/0);
    418   1.1     elric 		break;
    419   1.1     elric 	default:
    420  1.17  riastrad 		panic("%s: unrecognised direction %d", __func__, dir);
    421   1.1     elric 	}
    422   1.1     elric }
    423  1.26  riastrad 
    424  1.26  riastrad /*
    425  1.26  riastrad  * Adiantum
    426  1.26  riastrad  */
    427  1.26  riastrad 
    428  1.26  riastrad static void *
    429  1.26  riastrad cgd_cipher_adiantum_init(size_t keylen, const void *key, size_t *blocksize)
    430  1.26  riastrad {
    431  1.26  riastrad 	struct adiantum *A;
    432  1.26  riastrad 
    433  1.26  riastrad 	if (!blocksize)
    434  1.26  riastrad 		return NULL;
    435  1.26  riastrad 	if (keylen != 256)
    436  1.26  riastrad 		return NULL;
    437  1.26  riastrad 	if (*blocksize == (size_t)-1)
    438  1.26  riastrad 		*blocksize = 128;
    439  1.26  riastrad 	if (*blocksize != 128)
    440  1.26  riastrad 		return NULL;
    441  1.26  riastrad 
    442  1.26  riastrad 	A = kmem_zalloc(sizeof(*A), KM_SLEEP);
    443  1.26  riastrad 	adiantum_init(A, key);
    444  1.26  riastrad 
    445  1.26  riastrad 	return A;
    446  1.26  riastrad }
    447  1.26  riastrad 
    448  1.26  riastrad static void
    449  1.26  riastrad cgd_cipher_adiantum_destroy(void *cookie)
    450  1.26  riastrad {
    451  1.26  riastrad 	struct adiantum *A = cookie;
    452  1.26  riastrad 
    453  1.26  riastrad 	explicit_memset(A, 0, sizeof(*A));
    454  1.26  riastrad 	kmem_free(A, sizeof(*A));
    455  1.26  riastrad }
    456  1.26  riastrad 
    457  1.26  riastrad static void
    458  1.26  riastrad cgd_cipher_adiantum_crypt(void *cookie, void *dst, const void *src,
    459  1.26  riastrad     size_t nbytes, const void *blkno, int dir)
    460  1.26  riastrad {
    461  1.26  riastrad 	/*
    462  1.26  riastrad 	 * Treat the block number as a 128-bit block.  This is more
    463  1.26  riastrad 	 * than twice as big as the largest number of reasonable
    464  1.26  riastrad 	 * blocks, but it doesn't hurt (it would be rounded up to a
    465  1.26  riastrad 	 * 128-bit input anyway).
    466  1.26  riastrad 	 */
    467  1.26  riastrad 	const unsigned tweaklen = 16;
    468  1.26  riastrad 	struct adiantum *A = cookie;
    469  1.26  riastrad 
    470  1.26  riastrad 	switch (dir) {
    471  1.26  riastrad 	case CGD_CIPHER_ENCRYPT:
    472  1.26  riastrad 		adiantum_enc(dst, src, nbytes, blkno, tweaklen, A);
    473  1.26  riastrad 		break;
    474  1.26  riastrad 	case CGD_CIPHER_DECRYPT:
    475  1.26  riastrad 		adiantum_dec(dst, src, nbytes, blkno, tweaklen, A);
    476  1.26  riastrad 		break;
    477  1.26  riastrad 	default:
    478  1.26  riastrad 		panic("%s: unrecognised direction %d", __func__, dir);
    479  1.26  riastrad 	}
    480  1.26  riastrad }
    481