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