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