Home | History | Annotate | Line # | Download | only in opencrypto
xform.c revision 1.12
      1  1.12       tls /*	$NetBSD: xform.c,v 1.12 2003/11/16 12:07:50 tls Exp $ */
      2   1.1  jonathan /*	$FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $	*/
      3   1.1  jonathan /*	$OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $	*/
      4   1.1  jonathan 
      5   1.1  jonathan /*
      6   1.1  jonathan  * The authors of this code are John Ioannidis (ji (at) tla.org),
      7   1.1  jonathan  * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
      8   1.1  jonathan  * Niels Provos (provos (at) physnet.uni-hamburg.de).
      9   1.1  jonathan  *
     10   1.1  jonathan  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
     11   1.1  jonathan  * in November 1995.
     12   1.1  jonathan  *
     13   1.1  jonathan  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
     14   1.1  jonathan  * by Angelos D. Keromytis.
     15   1.1  jonathan  *
     16   1.1  jonathan  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
     17   1.1  jonathan  * and Niels Provos.
     18   1.1  jonathan  *
     19   1.1  jonathan  * Additional features in 1999 by Angelos D. Keromytis.
     20   1.1  jonathan  *
     21   1.1  jonathan  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
     22   1.1  jonathan  * Angelos D. Keromytis and Niels Provos.
     23   1.1  jonathan  *
     24   1.1  jonathan  * Copyright (C) 2001, Angelos D. Keromytis.
     25   1.1  jonathan  *
     26   1.1  jonathan  * Permission to use, copy, and modify this software with or without fee
     27   1.1  jonathan  * is hereby granted, provided that this entire notice is included in
     28   1.1  jonathan  * all copies of any software which is or includes a copy or
     29   1.1  jonathan  * modification of this software.
     30   1.1  jonathan  * You may use this code under the GNU public license if you so wish. Please
     31   1.1  jonathan  * contribute changes back to the authors under this freer than GPL license
     32   1.1  jonathan  * so that we may further the use of strong encryption without limitations to
     33   1.1  jonathan  * all.
     34   1.1  jonathan  *
     35   1.1  jonathan  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
     36   1.1  jonathan  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
     37   1.1  jonathan  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
     38   1.1  jonathan  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
     39   1.1  jonathan  * PURPOSE.
     40   1.1  jonathan  */
     41   1.1  jonathan 
     42   1.1  jonathan #include <sys/cdefs.h>
     43  1.12       tls __KERNEL_RCSID(0, "$NetBSD: xform.c,v 1.12 2003/11/16 12:07:50 tls Exp $");
     44   1.1  jonathan 
     45   1.1  jonathan #include <sys/param.h>
     46   1.1  jonathan #include <sys/systm.h>
     47   1.1  jonathan #include <sys/malloc.h>
     48   1.1  jonathan #include <sys/sysctl.h>
     49   1.1  jonathan #include <sys/errno.h>
     50   1.1  jonathan #include <sys/time.h>
     51   1.1  jonathan #include <sys/kernel.h>
     52   1.1  jonathan #include <machine/cpu.h>
     53   1.1  jonathan 
     54   1.1  jonathan #include <crypto/blowfish/blowfish.h>
     55   1.7   thorpej #include <crypto/cast128/cast128.h>
     56   1.1  jonathan #include <crypto/des/des.h>
     57   1.5   thorpej #include <crypto/rijndael/rijndael.h>
     58   1.6   thorpej #include <crypto/ripemd160/rmd160.h>
     59  1.12       tls #include <crypto/skipjack/skipjack.h>
     60   1.1  jonathan 
     61   1.1  jonathan #include <opencrypto/deflate.h>
     62   1.1  jonathan 
     63   1.2  jonathan #include <sys/md5.h>
     64   1.1  jonathan #include <sys/sha1.h>
     65   1.1  jonathan 
     66   1.1  jonathan #include <opencrypto/cryptodev.h>
     67   1.1  jonathan #include <opencrypto/xform.h>
     68   1.1  jonathan 
     69   1.1  jonathan static void null_encrypt(caddr_t, u_int8_t *);
     70   1.1  jonathan static void null_decrypt(caddr_t, u_int8_t *);
     71   1.9   thorpej static int null_setkey(u_int8_t **, const u_int8_t *, int);
     72   1.1  jonathan static void null_zerokey(u_int8_t **);
     73   1.1  jonathan 
     74   1.9   thorpej static	int des1_setkey(u_int8_t **, const u_int8_t *, int);
     75   1.9   thorpej static	int des3_setkey(u_int8_t **, const u_int8_t *, int);
     76   1.9   thorpej static	int blf_setkey(u_int8_t **, const u_int8_t *, int);
     77   1.9   thorpej static	int cast5_setkey(u_int8_t **, const u_int8_t *, int);
     78   1.9   thorpej static  int skipjack_setkey(u_int8_t **, const u_int8_t *, int);
     79   1.9   thorpej static  int rijndael128_setkey(u_int8_t **, const u_int8_t *, int);
     80   1.1  jonathan static	void des1_encrypt(caddr_t, u_int8_t *);
     81   1.1  jonathan static	void des3_encrypt(caddr_t, u_int8_t *);
     82   1.1  jonathan static	void blf_encrypt(caddr_t, u_int8_t *);
     83   1.1  jonathan static	void cast5_encrypt(caddr_t, u_int8_t *);
     84   1.1  jonathan static	void skipjack_encrypt(caddr_t, u_int8_t *);
     85   1.1  jonathan static	void rijndael128_encrypt(caddr_t, u_int8_t *);
     86   1.1  jonathan static	void des1_decrypt(caddr_t, u_int8_t *);
     87   1.1  jonathan static	void des3_decrypt(caddr_t, u_int8_t *);
     88   1.1  jonathan static	void blf_decrypt(caddr_t, u_int8_t *);
     89   1.1  jonathan static	void cast5_decrypt(caddr_t, u_int8_t *);
     90   1.1  jonathan static	void skipjack_decrypt(caddr_t, u_int8_t *);
     91   1.1  jonathan static	void rijndael128_decrypt(caddr_t, u_int8_t *);
     92   1.1  jonathan static	void des1_zerokey(u_int8_t **);
     93   1.1  jonathan static	void des3_zerokey(u_int8_t **);
     94   1.1  jonathan static	void blf_zerokey(u_int8_t **);
     95   1.1  jonathan static	void cast5_zerokey(u_int8_t **);
     96   1.1  jonathan static	void skipjack_zerokey(u_int8_t **);
     97   1.1  jonathan static	void rijndael128_zerokey(u_int8_t **);
     98   1.1  jonathan 
     99   1.1  jonathan static	void null_init(void *);
    100   1.9   thorpej static	int null_update(void *, const u_int8_t *, u_int16_t);
    101   1.1  jonathan static	void null_final(u_int8_t *, void *);
    102   1.1  jonathan 
    103   1.9   thorpej static int	MD5Update_int(void *, const u_int8_t *, u_int16_t);
    104   1.1  jonathan static void	SHA1Init_int(void *);
    105   1.9   thorpej static	int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
    106   1.1  jonathan static	void SHA1Final_int(u_int8_t *, void *);
    107   1.1  jonathan 
    108   1.1  jonathan 
    109   1.9   thorpej static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
    110   1.9   thorpej static	int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
    111   1.1  jonathan static	void SHA1Final_int(u_int8_t *, void *);
    112   1.9   thorpej static	int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
    113   1.9   thorpej static	int SHA256Update_int(void *, const u_int8_t *, u_int16_t);
    114   1.9   thorpej static	int SHA384Update_int(void *, const u_int8_t *, u_int16_t);
    115   1.9   thorpej static	int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
    116   1.1  jonathan 
    117   1.1  jonathan static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
    118   1.1  jonathan static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
    119   1.1  jonathan 
    120   1.1  jonathan MALLOC_DEFINE(M_XDATA, "xform", "xform data buffers");
    121   1.1  jonathan 
    122   1.1  jonathan /* Encryption instances */
    123   1.1  jonathan struct enc_xform enc_xform_null = {
    124   1.1  jonathan 	CRYPTO_NULL_CBC, "NULL",
    125   1.1  jonathan 	/* NB: blocksize of 4 is to generate a properly aligned ESP header */
    126   1.1  jonathan 	4, 0, 256, /* 2048 bits, max key */
    127   1.1  jonathan 	null_encrypt,
    128   1.1  jonathan 	null_decrypt,
    129   1.1  jonathan 	null_setkey,
    130   1.1  jonathan 	null_zerokey,
    131   1.1  jonathan };
    132   1.1  jonathan 
    133   1.1  jonathan struct enc_xform enc_xform_des = {
    134   1.1  jonathan 	CRYPTO_DES_CBC, "DES",
    135   1.1  jonathan 	8, 8, 8,
    136   1.1  jonathan 	des1_encrypt,
    137   1.1  jonathan 	des1_decrypt,
    138   1.1  jonathan 	des1_setkey,
    139   1.1  jonathan 	des1_zerokey,
    140   1.1  jonathan };
    141   1.1  jonathan 
    142   1.1  jonathan struct enc_xform enc_xform_3des = {
    143   1.1  jonathan 	CRYPTO_3DES_CBC, "3DES",
    144   1.1  jonathan 	8, 24, 24,
    145   1.1  jonathan 	des3_encrypt,
    146   1.1  jonathan 	des3_decrypt,
    147   1.1  jonathan 	des3_setkey,
    148   1.1  jonathan 	des3_zerokey
    149   1.1  jonathan };
    150   1.1  jonathan 
    151   1.1  jonathan struct enc_xform enc_xform_blf = {
    152   1.1  jonathan 	CRYPTO_BLF_CBC, "Blowfish",
    153   1.1  jonathan 	8, 5, 56 /* 448 bits, max key */,
    154   1.1  jonathan 	blf_encrypt,
    155   1.1  jonathan 	blf_decrypt,
    156   1.1  jonathan 	blf_setkey,
    157   1.1  jonathan 	blf_zerokey
    158   1.1  jonathan };
    159   1.1  jonathan 
    160   1.1  jonathan struct enc_xform enc_xform_cast5 = {
    161   1.1  jonathan 	CRYPTO_CAST_CBC, "CAST-128",
    162   1.1  jonathan 	8, 5, 16,
    163   1.1  jonathan 	cast5_encrypt,
    164   1.1  jonathan 	cast5_decrypt,
    165   1.1  jonathan 	cast5_setkey,
    166   1.1  jonathan 	cast5_zerokey
    167   1.1  jonathan };
    168   1.1  jonathan 
    169   1.1  jonathan struct enc_xform enc_xform_skipjack = {
    170   1.1  jonathan 	CRYPTO_SKIPJACK_CBC, "Skipjack",
    171   1.1  jonathan 	8, 10, 10,
    172   1.1  jonathan 	skipjack_encrypt,
    173   1.1  jonathan 	skipjack_decrypt,
    174   1.1  jonathan 	skipjack_setkey,
    175   1.1  jonathan 	skipjack_zerokey
    176   1.1  jonathan };
    177   1.1  jonathan 
    178   1.1  jonathan struct enc_xform enc_xform_rijndael128 = {
    179   1.1  jonathan 	CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
    180   1.1  jonathan 	16, 8, 32,
    181   1.1  jonathan 	rijndael128_encrypt,
    182   1.1  jonathan 	rijndael128_decrypt,
    183   1.1  jonathan 	rijndael128_setkey,
    184   1.1  jonathan 	rijndael128_zerokey,
    185   1.1  jonathan };
    186   1.1  jonathan 
    187   1.1  jonathan struct enc_xform enc_xform_arc4 = {
    188   1.1  jonathan 	CRYPTO_ARC4, "ARC4",
    189   1.1  jonathan 	1, 1, 32,
    190   1.1  jonathan 	NULL,
    191   1.1  jonathan 	NULL,
    192   1.1  jonathan 	NULL,
    193   1.1  jonathan 	NULL,
    194   1.1  jonathan };
    195   1.1  jonathan 
    196   1.1  jonathan /* Authentication instances */
    197   1.1  jonathan struct auth_hash auth_hash_null = {
    198   1.1  jonathan 	CRYPTO_NULL_HMAC, "NULL-HMAC",
    199   1.1  jonathan 	0, 0, 12, sizeof(int),			/* NB: context isn't used */
    200   1.1  jonathan 	null_init, null_update, null_final
    201   1.1  jonathan };
    202   1.1  jonathan 
    203   1.1  jonathan struct auth_hash auth_hash_hmac_md5_96 = {
    204   1.1  jonathan 	CRYPTO_MD5_HMAC, "HMAC-MD5",
    205   1.1  jonathan 	16, 16, 12, sizeof(MD5_CTX),
    206   1.1  jonathan 	(void (*) (void *)) MD5Init, MD5Update_int,
    207   1.1  jonathan 	(void (*) (u_int8_t *, void *)) MD5Final
    208   1.1  jonathan };
    209   1.1  jonathan 
    210   1.1  jonathan struct auth_hash auth_hash_hmac_sha1_96 = {
    211   1.1  jonathan 	CRYPTO_SHA1_HMAC, "HMAC-SHA1",
    212   1.1  jonathan 	20, 20, 12, sizeof(SHA1_CTX),
    213   1.1  jonathan 	SHA1Init_int, SHA1Update_int, SHA1Final_int
    214   1.1  jonathan };
    215   1.1  jonathan 
    216   1.1  jonathan struct auth_hash auth_hash_hmac_ripemd_160_96 = {
    217   1.1  jonathan 	CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
    218   1.1  jonathan 	20, 20, 12, sizeof(RMD160_CTX),
    219   1.1  jonathan 	(void (*)(void *)) RMD160Init, RMD160Update_int,
    220   1.1  jonathan 	(void (*)(u_int8_t *, void *)) RMD160Final
    221   1.1  jonathan };
    222   1.1  jonathan 
    223   1.1  jonathan struct auth_hash auth_hash_key_md5 = {
    224   1.1  jonathan 	CRYPTO_MD5_KPDK, "Keyed MD5",
    225   1.3    itojun 	0, 16, 16, sizeof(MD5_CTX),
    226   1.1  jonathan 	(void (*)(void *)) MD5Init, MD5Update_int,
    227   1.1  jonathan 	(void (*)(u_int8_t *, void *)) MD5Final
    228   1.1  jonathan };
    229   1.1  jonathan 
    230   1.1  jonathan struct auth_hash auth_hash_key_sha1 = {
    231   1.1  jonathan 	CRYPTO_SHA1_KPDK, "Keyed SHA1",
    232   1.3    itojun 	0, 20, 20, sizeof(SHA1_CTX),
    233   1.1  jonathan 	SHA1Init_int, SHA1Update_int, SHA1Final_int
    234   1.1  jonathan };
    235   1.1  jonathan 
    236   1.1  jonathan struct auth_hash auth_hash_md5 = {
    237   1.1  jonathan 	CRYPTO_MD5, "MD5",
    238   1.1  jonathan 	0, 16, 16, sizeof(MD5_CTX),
    239   1.1  jonathan 	(void (*) (void *)) MD5Init, MD5Update_int,
    240   1.1  jonathan 	(void (*) (u_int8_t *, void *)) MD5Final
    241   1.1  jonathan };
    242   1.1  jonathan 
    243   1.1  jonathan struct auth_hash auth_hash_sha1 = {
    244   1.1  jonathan 	CRYPTO_SHA1, "SHA1",
    245   1.1  jonathan 	0, 20, 20, sizeof(SHA1_CTX),
    246   1.1  jonathan 	(void (*)(void *)) SHA1Init, SHA1Update_int,
    247   1.1  jonathan 	(void (*)(u_int8_t *, void *)) SHA1Final
    248   1.1  jonathan };
    249   1.1  jonathan 
    250   1.1  jonathan struct auth_hash auth_hash_hmac_sha2_256 = {
    251   1.1  jonathan 	CRYPTO_SHA2_HMAC, "HMAC-SHA2",
    252   1.1  jonathan 	32, 32, 12, sizeof(SHA256_CTX),
    253   1.1  jonathan 	(void (*)(void *)) SHA256_Init, SHA256Update_int,
    254   1.1  jonathan 	(void (*)(u_int8_t *, void *)) SHA256_Final
    255   1.1  jonathan };
    256   1.1  jonathan 
    257   1.1  jonathan struct auth_hash auth_hash_hmac_sha2_384 = {
    258   1.1  jonathan 	CRYPTO_SHA2_HMAC, "HMAC-SHA2-384",
    259   1.1  jonathan 	48, 48, 12, sizeof(SHA384_CTX),
    260   1.1  jonathan 	(void (*)(void *)) SHA384_Init, SHA384Update_int,
    261   1.1  jonathan 	(void (*)(u_int8_t *, void *)) SHA384_Final
    262   1.1  jonathan };
    263   1.1  jonathan 
    264   1.1  jonathan struct auth_hash auth_hash_hmac_sha2_512 = {
    265   1.1  jonathan 	CRYPTO_SHA2_HMAC, "HMAC-SHA2-512",
    266   1.1  jonathan 	64, 64, 12, sizeof(SHA512_CTX),
    267   1.1  jonathan 	(void (*)(void *)) SHA512_Init, SHA512Update_int,
    268   1.1  jonathan 	(void (*)(u_int8_t *, void *)) SHA512_Final
    269   1.1  jonathan };
    270   1.1  jonathan 
    271   1.1  jonathan /* Compression instance */
    272   1.1  jonathan struct comp_algo comp_algo_deflate = {
    273   1.1  jonathan 	CRYPTO_DEFLATE_COMP, "Deflate",
    274   1.1  jonathan 	90, deflate_compress,
    275   1.1  jonathan 	deflate_decompress
    276   1.1  jonathan };
    277   1.1  jonathan 
    278   1.1  jonathan /*
    279   1.1  jonathan  * Encryption wrapper routines.
    280   1.1  jonathan  */
    281   1.1  jonathan static void
    282   1.1  jonathan null_encrypt(caddr_t key, u_int8_t *blk)
    283   1.1  jonathan {
    284   1.1  jonathan }
    285   1.1  jonathan static void
    286   1.1  jonathan null_decrypt(caddr_t key, u_int8_t *blk)
    287   1.1  jonathan {
    288   1.1  jonathan }
    289   1.1  jonathan static int
    290   1.9   thorpej null_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    291   1.1  jonathan {
    292   1.1  jonathan 	*sched = NULL;
    293   1.1  jonathan 	return 0;
    294   1.1  jonathan }
    295   1.1  jonathan static void
    296   1.1  jonathan null_zerokey(u_int8_t **sched)
    297   1.1  jonathan {
    298   1.1  jonathan 	*sched = NULL;
    299   1.1  jonathan }
    300   1.1  jonathan 
    301   1.1  jonathan static void
    302   1.1  jonathan des1_encrypt(caddr_t key, u_int8_t *blk)
    303   1.1  jonathan {
    304   1.1  jonathan 	des_cblock *cb = (des_cblock *) blk;
    305   1.1  jonathan 	des_key_schedule *p = (des_key_schedule *) key;
    306   1.1  jonathan 
    307   1.1  jonathan 	des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
    308   1.1  jonathan }
    309   1.1  jonathan 
    310   1.1  jonathan static void
    311   1.1  jonathan des1_decrypt(caddr_t key, u_int8_t *blk)
    312   1.1  jonathan {
    313   1.1  jonathan 	des_cblock *cb = (des_cblock *) blk;
    314   1.1  jonathan 	des_key_schedule *p = (des_key_schedule *) key;
    315   1.1  jonathan 
    316   1.1  jonathan 	des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
    317   1.1  jonathan }
    318   1.1  jonathan 
    319   1.1  jonathan static int
    320   1.9   thorpej des1_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    321   1.1  jonathan {
    322   1.1  jonathan 	des_key_schedule *p;
    323   1.1  jonathan 	int err;
    324   1.1  jonathan 
    325   1.1  jonathan 	MALLOC(p, des_key_schedule *, sizeof (des_key_schedule),
    326   1.1  jonathan 		M_CRYPTO_DATA, M_NOWAIT);
    327   1.1  jonathan 	if (p != NULL) {
    328   1.1  jonathan 		bzero(p, sizeof(des_key_schedule));
    329   1.1  jonathan 		des_set_key((des_cblock *) key, p[0]);
    330   1.1  jonathan 		err = 0;
    331   1.1  jonathan 	} else
    332   1.1  jonathan 		err = ENOMEM;
    333   1.1  jonathan 	*sched = (u_int8_t *) p;
    334   1.1  jonathan 	return err;
    335   1.1  jonathan }
    336   1.1  jonathan 
    337   1.1  jonathan static void
    338   1.1  jonathan des1_zerokey(u_int8_t **sched)
    339   1.1  jonathan {
    340   1.1  jonathan 	bzero(*sched, sizeof (des_key_schedule));
    341   1.1  jonathan 	FREE(*sched, M_CRYPTO_DATA);
    342   1.1  jonathan 	*sched = NULL;
    343   1.1  jonathan }
    344   1.1  jonathan 
    345   1.1  jonathan static void
    346   1.1  jonathan des3_encrypt(caddr_t key, u_int8_t *blk)
    347   1.1  jonathan {
    348   1.1  jonathan 	des_cblock *cb = (des_cblock *) blk;
    349   1.1  jonathan 	des_key_schedule *p = (des_key_schedule *) key;
    350   1.1  jonathan 
    351   1.1  jonathan 	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
    352   1.1  jonathan }
    353   1.1  jonathan 
    354   1.1  jonathan static void
    355   1.1  jonathan des3_decrypt(caddr_t key, u_int8_t *blk)
    356   1.1  jonathan {
    357   1.1  jonathan 	des_cblock *cb = (des_cblock *) blk;
    358   1.1  jonathan 	des_key_schedule *p = (des_key_schedule *) key;
    359   1.1  jonathan 
    360   1.1  jonathan 	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
    361   1.1  jonathan }
    362   1.1  jonathan 
    363   1.1  jonathan static int
    364   1.9   thorpej des3_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    365   1.1  jonathan {
    366   1.1  jonathan 	des_key_schedule *p;
    367   1.1  jonathan 	int err;
    368   1.1  jonathan 
    369   1.1  jonathan 	MALLOC(p, des_key_schedule *, 3*sizeof (des_key_schedule),
    370   1.1  jonathan 		M_CRYPTO_DATA, M_NOWAIT);
    371   1.1  jonathan 	if (p != NULL) {
    372   1.1  jonathan 		bzero(p, 3*sizeof(des_key_schedule));
    373   1.1  jonathan 		des_set_key((des_cblock *)(key +  0), p[0]);
    374   1.1  jonathan 		des_set_key((des_cblock *)(key +  8), p[1]);
    375   1.1  jonathan 		des_set_key((des_cblock *)(key + 16), p[2]);
    376   1.1  jonathan 		err = 0;
    377   1.1  jonathan 	} else
    378   1.1  jonathan 		err = ENOMEM;
    379   1.1  jonathan 	*sched = (u_int8_t *) p;
    380   1.1  jonathan 	return err;
    381   1.1  jonathan }
    382   1.1  jonathan 
    383   1.1  jonathan static void
    384   1.1  jonathan des3_zerokey(u_int8_t **sched)
    385   1.1  jonathan {
    386   1.1  jonathan 	bzero(*sched, 3*sizeof (des_key_schedule));
    387   1.1  jonathan 	FREE(*sched, M_CRYPTO_DATA);
    388   1.1  jonathan 	*sched = NULL;
    389   1.1  jonathan }
    390   1.1  jonathan 
    391   1.1  jonathan static void
    392   1.1  jonathan blf_encrypt(caddr_t key, u_int8_t *blk)
    393   1.1  jonathan {
    394   1.8   thorpej 
    395   1.8   thorpej #if defined(__NetBSD__)
    396   1.8   thorpej 	BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 1);
    397   1.8   thorpej #else
    398   1.1  jonathan 	blf_ecb_encrypt((blf_ctx *) key, blk, 8);
    399   1.8   thorpej #endif
    400   1.1  jonathan }
    401   1.1  jonathan 
    402   1.1  jonathan static void
    403   1.1  jonathan blf_decrypt(caddr_t key, u_int8_t *blk)
    404   1.1  jonathan {
    405   1.8   thorpej 
    406   1.8   thorpej #if defined(__NetBSD__)
    407   1.8   thorpej 	BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 0);
    408   1.8   thorpej #else
    409   1.1  jonathan 	blf_ecb_decrypt((blf_ctx *) key, blk, 8);
    410   1.8   thorpej #endif
    411   1.1  jonathan }
    412   1.1  jonathan 
    413   1.1  jonathan static int
    414   1.9   thorpej blf_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    415   1.1  jonathan {
    416   1.1  jonathan 	int err;
    417   1.1  jonathan 
    418   1.8   thorpej #if defined(__FreeBSD__) || defined(__NetBSD__)
    419   1.8   thorpej #define	BLF_SIZ	sizeof(BF_KEY)
    420   1.1  jonathan #else
    421   1.8   thorpej #define	BLF_SIZ	sizeof(blf_ctx)
    422   1.1  jonathan #endif
    423   1.1  jonathan 
    424   1.1  jonathan 	MALLOC(*sched, u_int8_t *, BLF_SIZ,
    425   1.1  jonathan 		M_CRYPTO_DATA, M_NOWAIT);
    426   1.1  jonathan 	if (*sched != NULL) {
    427   1.1  jonathan 		bzero(*sched, BLF_SIZ);
    428   1.8   thorpej #if defined(__FreeBSD__) || defined(__NetBSD__)
    429   1.1  jonathan 		BF_set_key((BF_KEY *) *sched, len, key);
    430   1.1  jonathan #else
    431   1.1  jonathan 		blf_key((blf_ctx *)*sched, key, len);
    432   1.1  jonathan #endif
    433   1.1  jonathan 		err = 0;
    434   1.1  jonathan 	} else
    435   1.1  jonathan 		err = ENOMEM;
    436   1.1  jonathan 	return err;
    437   1.1  jonathan }
    438   1.1  jonathan 
    439   1.1  jonathan static void
    440   1.1  jonathan blf_zerokey(u_int8_t **sched)
    441   1.1  jonathan {
    442   1.1  jonathan 	bzero(*sched, BLF_SIZ);
    443   1.1  jonathan 	FREE(*sched, M_CRYPTO_DATA);
    444   1.1  jonathan 	*sched = NULL;
    445   1.1  jonathan }
    446   1.1  jonathan 
    447   1.1  jonathan static void
    448   1.1  jonathan cast5_encrypt(caddr_t key, u_int8_t *blk)
    449   1.1  jonathan {
    450   1.7   thorpej 	cast128_encrypt((cast128_key *) key, blk, blk);
    451   1.1  jonathan }
    452   1.1  jonathan 
    453   1.1  jonathan static void
    454   1.1  jonathan cast5_decrypt(caddr_t key, u_int8_t *blk)
    455   1.1  jonathan {
    456   1.7   thorpej 	cast128_decrypt((cast128_key *) key, blk, blk);
    457   1.1  jonathan }
    458   1.1  jonathan 
    459   1.1  jonathan static int
    460   1.9   thorpej cast5_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    461   1.1  jonathan {
    462   1.1  jonathan 	int err;
    463   1.1  jonathan 
    464   1.7   thorpej 	MALLOC(*sched, u_int8_t *, sizeof(cast128_key), M_CRYPTO_DATA,
    465   1.1  jonathan 	       M_NOWAIT);
    466   1.1  jonathan 	if (*sched != NULL) {
    467   1.7   thorpej 		bzero(*sched, sizeof(cast128_key));
    468   1.7   thorpej 		cast128_setkey((cast128_key *)*sched, key, len);
    469   1.1  jonathan 		err = 0;
    470   1.1  jonathan 	} else
    471   1.1  jonathan 		err = ENOMEM;
    472   1.1  jonathan 	return err;
    473   1.1  jonathan }
    474   1.1  jonathan 
    475   1.1  jonathan static void
    476   1.1  jonathan cast5_zerokey(u_int8_t **sched)
    477   1.1  jonathan {
    478   1.7   thorpej 	bzero(*sched, sizeof(cast128_key));
    479   1.1  jonathan 	FREE(*sched, M_CRYPTO_DATA);
    480   1.1  jonathan 	*sched = NULL;
    481   1.1  jonathan }
    482   1.1  jonathan 
    483   1.1  jonathan static void
    484   1.1  jonathan skipjack_encrypt(caddr_t key, u_int8_t *blk)
    485   1.1  jonathan {
    486   1.1  jonathan 	skipjack_forwards(blk, blk, (u_int8_t **) key);
    487   1.1  jonathan }
    488   1.1  jonathan 
    489   1.1  jonathan static void
    490   1.1  jonathan skipjack_decrypt(caddr_t key, u_int8_t *blk)
    491   1.1  jonathan {
    492   1.1  jonathan 	skipjack_backwards(blk, blk, (u_int8_t **) key);
    493   1.1  jonathan }
    494   1.1  jonathan 
    495   1.1  jonathan static int
    496   1.9   thorpej skipjack_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    497   1.1  jonathan {
    498   1.1  jonathan 	int err;
    499   1.1  jonathan 
    500   1.1  jonathan 	/* NB: allocate all the memory that's needed at once */
    501   1.1  jonathan 	/* XXX assumes bytes are aligned on sizeof(u_char) == 1 boundaries.
    502   1.1  jonathan 	 * Will this break a pdp-10, Cray-1, or GE-645 port?
    503   1.1  jonathan 	 */
    504   1.1  jonathan 	MALLOC(*sched, u_int8_t *, 10 * (sizeof(u_int8_t *) + 0x100),
    505   1.1  jonathan 		M_CRYPTO_DATA, M_NOWAIT);
    506   1.1  jonathan 
    507   1.1  jonathan 	if (*sched != NULL) {
    508   1.1  jonathan 
    509   1.1  jonathan 		u_int8_t** key_tables = (u_int8_t**) *sched;
    510   1.1  jonathan 		u_int8_t* table = (u_int8_t*) &key_tables[10];
    511   1.1  jonathan 		int k;
    512   1.1  jonathan 
    513   1.1  jonathan 		bzero(*sched, 10 * sizeof(u_int8_t *)+0x100);
    514   1.1  jonathan 
    515   1.1  jonathan 		for (k = 0; k < 10; k++) {
    516   1.1  jonathan 			key_tables[k] = table;
    517   1.1  jonathan 			table += 0x100;
    518   1.1  jonathan 		}
    519   1.1  jonathan 		subkey_table_gen(key, (u_int8_t **) *sched);
    520   1.1  jonathan 		err = 0;
    521   1.1  jonathan 	} else
    522   1.1  jonathan 		err = ENOMEM;
    523   1.1  jonathan 	return err;
    524   1.1  jonathan }
    525   1.1  jonathan 
    526   1.1  jonathan static void
    527   1.1  jonathan skipjack_zerokey(u_int8_t **sched)
    528   1.1  jonathan {
    529   1.1  jonathan 	bzero(*sched, 10 * (sizeof(u_int8_t *) + 0x100));
    530   1.1  jonathan 	FREE(*sched, M_CRYPTO_DATA);
    531   1.1  jonathan 	*sched = NULL;
    532   1.1  jonathan }
    533   1.1  jonathan 
    534   1.1  jonathan static void
    535   1.1  jonathan rijndael128_encrypt(caddr_t key, u_int8_t *blk)
    536   1.1  jonathan {
    537   1.1  jonathan 	rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
    538   1.1  jonathan }
    539   1.1  jonathan 
    540   1.1  jonathan static void
    541   1.1  jonathan rijndael128_decrypt(caddr_t key, u_int8_t *blk)
    542   1.1  jonathan {
    543  1.11   thorpej 	rijndael_decrypt((rijndael_ctx *) key, (u_char *) blk,
    544   1.1  jonathan 	    (u_char *) blk);
    545   1.1  jonathan }
    546   1.1  jonathan 
    547   1.1  jonathan static int
    548   1.9   thorpej rijndael128_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    549   1.1  jonathan {
    550   1.1  jonathan 	int err;
    551   1.1  jonathan 
    552  1.10    itojun 	MALLOC(*sched, u_int8_t *, sizeof(rijndael_ctx), M_CRYPTO_DATA,
    553   1.1  jonathan 	    M_WAITOK);
    554   1.1  jonathan 	if (*sched != NULL) {
    555  1.11   thorpej 		bzero(*sched, sizeof(rijndael_ctx));
    556  1.11   thorpej 		rijndael_set_key((rijndael_ctx *) *sched, key, len * 8);
    557   1.1  jonathan 		err = 0;
    558   1.1  jonathan 	} else
    559   1.1  jonathan 		err = ENOMEM;
    560   1.1  jonathan 	return err;
    561   1.1  jonathan }
    562   1.1  jonathan 
    563   1.1  jonathan static void
    564   1.1  jonathan rijndael128_zerokey(u_int8_t **sched)
    565   1.1  jonathan {
    566   1.1  jonathan 	bzero(*sched, 2 * sizeof(rijndael_ctx));
    567   1.1  jonathan 	FREE(*sched, M_CRYPTO_DATA);
    568   1.1  jonathan 	*sched = NULL;
    569   1.1  jonathan }
    570   1.1  jonathan 
    571   1.1  jonathan /*
    572   1.1  jonathan  * And now for auth.
    573   1.1  jonathan  */
    574   1.1  jonathan 
    575   1.1  jonathan static void
    576   1.1  jonathan null_init(void *ctx)
    577   1.1  jonathan {
    578   1.1  jonathan }
    579   1.1  jonathan 
    580   1.1  jonathan static int
    581   1.9   thorpej null_update(void *ctx, const u_int8_t *buf, u_int16_t len)
    582   1.1  jonathan {
    583   1.1  jonathan 	return 0;
    584   1.1  jonathan }
    585   1.1  jonathan 
    586   1.1  jonathan static void
    587   1.1  jonathan null_final(u_int8_t *buf, void *ctx)
    588   1.1  jonathan {
    589   1.1  jonathan 	if (buf != (u_int8_t *) 0)
    590   1.1  jonathan 		bzero(buf, 12);
    591   1.1  jonathan }
    592   1.1  jonathan 
    593   1.1  jonathan static int
    594   1.9   thorpej RMD160Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    595   1.1  jonathan {
    596   1.1  jonathan 	RMD160Update(ctx, buf, len);
    597   1.1  jonathan 	return 0;
    598   1.1  jonathan }
    599   1.1  jonathan 
    600   1.1  jonathan static int
    601   1.9   thorpej MD5Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    602   1.1  jonathan {
    603   1.1  jonathan 	MD5Update(ctx, buf, len);
    604   1.1  jonathan 	return 0;
    605   1.1  jonathan }
    606   1.1  jonathan 
    607   1.1  jonathan static void
    608   1.1  jonathan SHA1Init_int(void *ctx)
    609   1.1  jonathan {
    610   1.1  jonathan 	SHA1Init(ctx);
    611   1.1  jonathan }
    612   1.1  jonathan 
    613   1.1  jonathan static int
    614   1.9   thorpej SHA1Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    615   1.1  jonathan {
    616   1.1  jonathan 	SHA1Update(ctx, buf, len);
    617   1.1  jonathan 	return 0;
    618   1.1  jonathan }
    619   1.1  jonathan 
    620   1.1  jonathan static void
    621   1.1  jonathan SHA1Final_int(u_int8_t *blk, void *ctx)
    622   1.1  jonathan {
    623   1.1  jonathan 	SHA1Final(blk, ctx);
    624   1.1  jonathan }
    625   1.1  jonathan 
    626   1.1  jonathan static int
    627   1.9   thorpej SHA256Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    628   1.1  jonathan {
    629   1.1  jonathan 	SHA256_Update(ctx, buf, len);
    630   1.1  jonathan 	return 0;
    631   1.1  jonathan }
    632   1.1  jonathan 
    633   1.1  jonathan static int
    634   1.9   thorpej SHA384Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    635   1.1  jonathan {
    636   1.1  jonathan 	SHA384_Update(ctx, buf, len);
    637   1.1  jonathan 	return 0;
    638   1.1  jonathan }
    639   1.1  jonathan 
    640   1.1  jonathan static int
    641   1.9   thorpej SHA512Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    642   1.1  jonathan {
    643   1.1  jonathan 	SHA512_Update(ctx, buf, len);
    644   1.1  jonathan 	return 0;
    645   1.1  jonathan }
    646   1.1  jonathan 
    647   1.1  jonathan /*
    648   1.1  jonathan  * And compression
    649   1.1  jonathan  */
    650   1.1  jonathan 
    651   1.1  jonathan static u_int32_t
    652   1.1  jonathan deflate_compress(data, size, out)
    653   1.1  jonathan 	u_int8_t *data;
    654   1.1  jonathan 	u_int32_t size;
    655   1.1  jonathan 	u_int8_t **out;
    656   1.1  jonathan {
    657   1.1  jonathan 	return deflate_global(data, size, 0, out);
    658   1.1  jonathan }
    659   1.1  jonathan 
    660   1.1  jonathan static u_int32_t
    661   1.1  jonathan deflate_decompress(data, size, out)
    662   1.1  jonathan 	u_int8_t *data;
    663   1.1  jonathan 	u_int32_t size;
    664   1.1  jonathan 	u_int8_t **out;
    665   1.1  jonathan {
    666   1.1  jonathan 	return deflate_global(data, size, 1, out);
    667   1.1  jonathan }
    668