Home | History | Annotate | Line # | Download | only in opencrypto
cryptosoft_xform.c revision 1.12.6.1
      1  1.12.6.1    jruoho /*	$NetBSD: cryptosoft_xform.c,v 1.12.6.1 2011/06/06 09:10:04 jruoho 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.12.6.1    jruoho __KERNEL_RCSID(1, "$NetBSD: cryptosoft_xform.c,v 1.12.6.1 2011/06/06 09:10:04 jruoho 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.12.6.1    jruoho #include <crypto/camellia/camellia.h>
     51       1.1   thorpej 
     52       1.1   thorpej #include <opencrypto/deflate.h>
     53       1.1   thorpej 
     54       1.1   thorpej #include <sys/md5.h>
     55       1.3  christos #include <sys/rmd160.h>
     56       1.1   thorpej #include <sys/sha1.h>
     57  1.12.6.1    jruoho #include <sys/sha2.h>
     58  1.12.6.1    jruoho #include <opencrypto/aesxcbcmac.h>
     59  1.12.6.1    jruoho #include <opencrypto/gmac.h>
     60       1.1   thorpej 
     61       1.1   thorpej struct swcr_auth_hash {
     62  1.12.6.1    jruoho 	const struct auth_hash *auth_hash;
     63  1.12.6.1    jruoho 	int ctxsize;
     64       1.1   thorpej 	void (*Init)(void *);
     65  1.12.6.1    jruoho 	void (*Setkey)(void *, const uint8_t *, uint16_t);
     66  1.12.6.1    jruoho 	void (*Reinit)(void *, const uint8_t *, uint16_t);
     67       1.1   thorpej 	int  (*Update)(void *, const uint8_t *, uint16_t);
     68       1.1   thorpej 	void (*Final)(uint8_t *, void *);
     69       1.1   thorpej };
     70       1.1   thorpej 
     71       1.1   thorpej struct swcr_enc_xform {
     72  1.12.6.1    jruoho 	const struct enc_xform *enc_xform;
     73       1.5  christos 	void (*encrypt)(void *, uint8_t *);
     74       1.5  christos 	void (*decrypt)(void *, uint8_t *);
     75  1.12.6.1    jruoho 	int  (*setkey)(uint8_t **, const uint8_t *, int);
     76       1.1   thorpej 	void (*zerokey)(uint8_t **);
     77  1.12.6.1    jruoho 	void (*reinit)(void *, const uint8_t *, uint8_t *);
     78       1.1   thorpej };
     79       1.1   thorpej 
     80       1.1   thorpej struct swcr_comp_algo {
     81  1.12.6.1    jruoho 	const struct comp_algo *unused_comp_algo;
     82       1.1   thorpej 	uint32_t (*compress)(uint8_t *, uint32_t, uint8_t **);
     83  1.12.6.1    jruoho 	uint32_t (*decompress)(uint8_t *, uint32_t, uint8_t **, int);
     84       1.1   thorpej };
     85       1.1   thorpej 
     86       1.5  christos static void null_encrypt(void *, u_int8_t *);
     87       1.5  christos static void null_decrypt(void *, u_int8_t *);
     88       1.1   thorpej static int null_setkey(u_int8_t **, const u_int8_t *, int);
     89       1.1   thorpej static void null_zerokey(u_int8_t **);
     90       1.1   thorpej 
     91       1.1   thorpej static	int des1_setkey(u_int8_t **, const u_int8_t *, int);
     92       1.1   thorpej static	int des3_setkey(u_int8_t **, const u_int8_t *, int);
     93       1.1   thorpej static	int blf_setkey(u_int8_t **, const u_int8_t *, int);
     94       1.1   thorpej static	int cast5_setkey(u_int8_t **, const u_int8_t *, int);
     95       1.1   thorpej static  int skipjack_setkey(u_int8_t **, const u_int8_t *, int);
     96       1.1   thorpej static  int rijndael128_setkey(u_int8_t **, const u_int8_t *, int);
     97  1.12.6.1    jruoho static  int cml_setkey(u_int8_t **, const u_int8_t *, int);
     98  1.12.6.1    jruoho static  int aes_ctr_setkey(u_int8_t **, const u_int8_t *, int);
     99       1.5  christos static	void des1_encrypt(void *, u_int8_t *);
    100       1.5  christos static	void des3_encrypt(void *, u_int8_t *);
    101       1.5  christos static	void blf_encrypt(void *, u_int8_t *);
    102       1.5  christos static	void cast5_encrypt(void *, u_int8_t *);
    103       1.5  christos static	void skipjack_encrypt(void *, u_int8_t *);
    104       1.5  christos static	void rijndael128_encrypt(void *, u_int8_t *);
    105  1.12.6.1    jruoho static  void cml_encrypt(void *, u_int8_t *);
    106       1.5  christos static	void des1_decrypt(void *, u_int8_t *);
    107       1.5  christos static	void des3_decrypt(void *, u_int8_t *);
    108       1.5  christos static	void blf_decrypt(void *, u_int8_t *);
    109       1.5  christos static	void cast5_decrypt(void *, u_int8_t *);
    110       1.5  christos static	void skipjack_decrypt(void *, u_int8_t *);
    111       1.5  christos static	void rijndael128_decrypt(void *, u_int8_t *);
    112  1.12.6.1    jruoho static  void cml_decrypt(void *, u_int8_t *);
    113  1.12.6.1    jruoho static  void aes_ctr_crypt(void *, u_int8_t *);
    114       1.1   thorpej static	void des1_zerokey(u_int8_t **);
    115       1.1   thorpej static	void des3_zerokey(u_int8_t **);
    116       1.1   thorpej static	void blf_zerokey(u_int8_t **);
    117       1.1   thorpej static	void cast5_zerokey(u_int8_t **);
    118       1.1   thorpej static	void skipjack_zerokey(u_int8_t **);
    119       1.1   thorpej static	void rijndael128_zerokey(u_int8_t **);
    120  1.12.6.1    jruoho static  void cml_zerokey(u_int8_t **);
    121  1.12.6.1    jruoho static  void aes_ctr_zerokey(u_int8_t **);
    122  1.12.6.1    jruoho static  void aes_ctr_reinit(void *, const u_int8_t *, u_int8_t *);
    123  1.12.6.1    jruoho static  void aes_gcm_reinit(void *, const u_int8_t *, u_int8_t *);
    124       1.1   thorpej 
    125       1.1   thorpej static	void null_init(void *);
    126       1.1   thorpej static	int null_update(void *, const u_int8_t *, u_int16_t);
    127       1.1   thorpej static	void null_final(u_int8_t *, void *);
    128       1.1   thorpej 
    129       1.1   thorpej static int	MD5Update_int(void *, const u_int8_t *, u_int16_t);
    130       1.1   thorpej static void	SHA1Init_int(void *);
    131       1.1   thorpej static	int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
    132       1.1   thorpej static	void SHA1Final_int(u_int8_t *, void *);
    133       1.1   thorpej 
    134       1.1   thorpej 
    135       1.1   thorpej static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
    136       1.1   thorpej static	int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
    137       1.1   thorpej static	void SHA1Final_int(u_int8_t *, void *);
    138       1.1   thorpej static	int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
    139       1.1   thorpej static	int SHA256Update_int(void *, const u_int8_t *, u_int16_t);
    140       1.1   thorpej static	int SHA384Update_int(void *, const u_int8_t *, u_int16_t);
    141       1.1   thorpej static	int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
    142       1.1   thorpej 
    143       1.1   thorpej static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
    144  1.12.6.1    jruoho static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
    145      1.12    darran static u_int32_t gzip_compress(u_int8_t *, u_int32_t, u_int8_t **);
    146  1.12.6.1    jruoho static u_int32_t gzip_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
    147       1.1   thorpej 
    148       1.1   thorpej /* Encryption instances */
    149       1.1   thorpej static const struct swcr_enc_xform swcr_enc_xform_null = {
    150       1.1   thorpej 	&enc_xform_null,
    151       1.1   thorpej 	null_encrypt,
    152       1.1   thorpej 	null_decrypt,
    153       1.1   thorpej 	null_setkey,
    154       1.1   thorpej 	null_zerokey,
    155  1.12.6.1    jruoho 	NULL
    156       1.1   thorpej };
    157       1.1   thorpej 
    158       1.1   thorpej static const struct swcr_enc_xform swcr_enc_xform_des = {
    159       1.1   thorpej 	&enc_xform_des,
    160       1.1   thorpej 	des1_encrypt,
    161       1.1   thorpej 	des1_decrypt,
    162       1.1   thorpej 	des1_setkey,
    163       1.1   thorpej 	des1_zerokey,
    164  1.12.6.1    jruoho 	NULL
    165       1.1   thorpej };
    166       1.1   thorpej 
    167       1.1   thorpej static const struct swcr_enc_xform swcr_enc_xform_3des = {
    168       1.1   thorpej 	&enc_xform_3des,
    169       1.1   thorpej 	des3_encrypt,
    170       1.1   thorpej 	des3_decrypt,
    171       1.1   thorpej 	des3_setkey,
    172  1.12.6.1    jruoho 	des3_zerokey,
    173  1.12.6.1    jruoho 	NULL
    174       1.1   thorpej };
    175       1.1   thorpej 
    176       1.1   thorpej static const struct swcr_enc_xform swcr_enc_xform_blf = {
    177       1.1   thorpej 	&enc_xform_blf,
    178       1.1   thorpej 	blf_encrypt,
    179       1.1   thorpej 	blf_decrypt,
    180       1.1   thorpej 	blf_setkey,
    181  1.12.6.1    jruoho 	blf_zerokey,
    182  1.12.6.1    jruoho 	NULL
    183       1.1   thorpej };
    184       1.1   thorpej 
    185       1.1   thorpej static const struct swcr_enc_xform swcr_enc_xform_cast5 = {
    186       1.1   thorpej 	&enc_xform_cast5,
    187       1.1   thorpej 	cast5_encrypt,
    188       1.1   thorpej 	cast5_decrypt,
    189       1.1   thorpej 	cast5_setkey,
    190  1.12.6.1    jruoho 	cast5_zerokey,
    191  1.12.6.1    jruoho 	NULL
    192       1.1   thorpej };
    193       1.1   thorpej 
    194       1.1   thorpej static const struct swcr_enc_xform swcr_enc_xform_skipjack = {
    195       1.1   thorpej 	&enc_xform_skipjack,
    196       1.1   thorpej 	skipjack_encrypt,
    197       1.1   thorpej 	skipjack_decrypt,
    198       1.1   thorpej 	skipjack_setkey,
    199  1.12.6.1    jruoho 	skipjack_zerokey,
    200  1.12.6.1    jruoho 	NULL
    201       1.1   thorpej };
    202       1.1   thorpej 
    203       1.1   thorpej static const struct swcr_enc_xform swcr_enc_xform_rijndael128 = {
    204       1.1   thorpej 	&enc_xform_rijndael128,
    205       1.1   thorpej 	rijndael128_encrypt,
    206       1.1   thorpej 	rijndael128_decrypt,
    207       1.1   thorpej 	rijndael128_setkey,
    208       1.1   thorpej 	rijndael128_zerokey,
    209  1.12.6.1    jruoho 	NULL
    210       1.1   thorpej };
    211       1.1   thorpej 
    212  1.12.6.1    jruoho static const struct swcr_enc_xform swcr_enc_xform_aes_ctr = {
    213  1.12.6.1    jruoho 	&enc_xform_aes_ctr,
    214  1.12.6.1    jruoho 	aes_ctr_crypt,
    215  1.12.6.1    jruoho 	aes_ctr_crypt,
    216  1.12.6.1    jruoho 	aes_ctr_setkey,
    217  1.12.6.1    jruoho 	aes_ctr_zerokey,
    218  1.12.6.1    jruoho 	aes_ctr_reinit
    219  1.12.6.1    jruoho };
    220  1.12.6.1    jruoho 
    221  1.12.6.1    jruoho static const struct swcr_enc_xform swcr_enc_xform_aes_gcm = {
    222  1.12.6.1    jruoho 	&enc_xform_aes_gcm,
    223  1.12.6.1    jruoho 	aes_ctr_crypt,
    224  1.12.6.1    jruoho 	aes_ctr_crypt,
    225  1.12.6.1    jruoho 	aes_ctr_setkey,
    226  1.12.6.1    jruoho 	aes_ctr_zerokey,
    227  1.12.6.1    jruoho 	aes_gcm_reinit
    228  1.12.6.1    jruoho };
    229  1.12.6.1    jruoho 
    230  1.12.6.1    jruoho static const struct swcr_enc_xform swcr_enc_xform_aes_gmac = {
    231  1.12.6.1    jruoho 	&enc_xform_aes_gmac,
    232       1.1   thorpej 	NULL,
    233       1.1   thorpej 	NULL,
    234       1.1   thorpej 	NULL,
    235       1.1   thorpej 	NULL,
    236  1.12.6.1    jruoho 	NULL
    237  1.12.6.1    jruoho };
    238  1.12.6.1    jruoho 
    239  1.12.6.1    jruoho static const struct swcr_enc_xform swcr_enc_xform_camellia = {
    240  1.12.6.1    jruoho 	&enc_xform_camellia,
    241  1.12.6.1    jruoho 	cml_encrypt,
    242  1.12.6.1    jruoho 	cml_decrypt,
    243  1.12.6.1    jruoho 	cml_setkey,
    244  1.12.6.1    jruoho 	cml_zerokey,
    245  1.12.6.1    jruoho 	NULL
    246       1.1   thorpej };
    247       1.1   thorpej 
    248       1.1   thorpej /* Authentication instances */
    249       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_null = {
    250  1.12.6.1    jruoho 	&auth_hash_null, sizeof(int), /* NB: context isn't used */
    251  1.12.6.1    jruoho 	null_init, NULL, NULL, null_update, null_final
    252       1.1   thorpej };
    253       1.1   thorpej 
    254       1.7       tls static const struct swcr_auth_hash swcr_auth_hash_hmac_md5 = {
    255  1.12.6.1    jruoho 	&auth_hash_hmac_md5, sizeof(MD5_CTX),
    256  1.12.6.1    jruoho 	(void (*) (void *)) MD5Init, NULL, NULL, MD5Update_int,
    257       1.7       tls 	(void (*) (u_int8_t *, void *)) MD5Final
    258       1.7       tls };
    259       1.7       tls 
    260       1.7       tls static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1 = {
    261  1.12.6.1    jruoho 	&auth_hash_hmac_sha1, sizeof(SHA1_CTX),
    262  1.12.6.1    jruoho 	SHA1Init_int, NULL, NULL, SHA1Update_int, SHA1Final_int
    263       1.7       tls };
    264       1.7       tls 
    265       1.7       tls static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160 = {
    266  1.12.6.1    jruoho 	&auth_hash_hmac_ripemd_160, sizeof(RMD160_CTX),
    267  1.12.6.1    jruoho 	(void (*)(void *)) RMD160Init, NULL, NULL, RMD160Update_int,
    268       1.7       tls 	(void (*)(u_int8_t *, void *)) RMD160Final
    269       1.7       tls };
    270       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_md5_96 = {
    271  1.12.6.1    jruoho 	&auth_hash_hmac_md5_96, sizeof(MD5_CTX),
    272  1.12.6.1    jruoho 	(void (*) (void *)) MD5Init, NULL, NULL, MD5Update_int,
    273       1.1   thorpej 	(void (*) (u_int8_t *, void *)) MD5Final
    274       1.1   thorpej };
    275       1.1   thorpej 
    276       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1_96 = {
    277  1.12.6.1    jruoho 	&auth_hash_hmac_sha1_96, sizeof(SHA1_CTX),
    278  1.12.6.1    jruoho 	SHA1Init_int, NULL, NULL, SHA1Update_int, SHA1Final_int
    279       1.1   thorpej };
    280       1.1   thorpej 
    281       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160_96 = {
    282  1.12.6.1    jruoho 	&auth_hash_hmac_ripemd_160_96, sizeof(RMD160_CTX),
    283  1.12.6.1    jruoho 	(void (*)(void *)) RMD160Init, NULL, NULL, RMD160Update_int,
    284       1.1   thorpej 	(void (*)(u_int8_t *, void *)) RMD160Final
    285       1.1   thorpej };
    286       1.1   thorpej 
    287       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_key_md5 = {
    288  1.12.6.1    jruoho 	&auth_hash_key_md5, sizeof(MD5_CTX),
    289  1.12.6.1    jruoho 	(void (*)(void *)) MD5Init, NULL, NULL, MD5Update_int,
    290       1.1   thorpej 	(void (*)(u_int8_t *, void *)) MD5Final
    291       1.1   thorpej };
    292       1.1   thorpej 
    293       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_key_sha1 = {
    294  1.12.6.1    jruoho 	&auth_hash_key_sha1, sizeof(SHA1_CTX),
    295  1.12.6.1    jruoho 	SHA1Init_int, NULL, NULL, SHA1Update_int, SHA1Final_int
    296       1.1   thorpej };
    297       1.1   thorpej 
    298       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_md5 = {
    299  1.12.6.1    jruoho 	&auth_hash_md5, sizeof(MD5_CTX),
    300  1.12.6.1    jruoho 	(void (*) (void *)) MD5Init, NULL, NULL, MD5Update_int,
    301       1.1   thorpej 	(void (*) (u_int8_t *, void *)) MD5Final
    302       1.1   thorpej };
    303       1.1   thorpej 
    304       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_sha1 = {
    305  1.12.6.1    jruoho 	&auth_hash_sha1, sizeof(SHA1_CTX),
    306  1.12.6.1    jruoho 	(void (*)(void *)) SHA1Init, NULL, NULL, SHA1Update_int,
    307       1.1   thorpej 	(void (*)(u_int8_t *, void *)) SHA1Final
    308       1.1   thorpej };
    309       1.1   thorpej 
    310       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_256 = {
    311  1.12.6.1    jruoho 	&auth_hash_hmac_sha2_256, sizeof(SHA256_CTX),
    312  1.12.6.1    jruoho 	(void (*)(void *)) SHA256_Init, NULL, NULL, SHA256Update_int,
    313       1.1   thorpej 	(void (*)(u_int8_t *, void *)) SHA256_Final
    314       1.1   thorpej };
    315       1.1   thorpej 
    316       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_384 = {
    317  1.12.6.1    jruoho 	&auth_hash_hmac_sha2_384, sizeof(SHA384_CTX),
    318  1.12.6.1    jruoho 	(void (*)(void *)) SHA384_Init, NULL, NULL, SHA384Update_int,
    319       1.1   thorpej 	(void (*)(u_int8_t *, void *)) SHA384_Final
    320       1.1   thorpej };
    321       1.1   thorpej 
    322       1.1   thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_512 = {
    323  1.12.6.1    jruoho 	&auth_hash_hmac_sha2_512, sizeof(SHA512_CTX),
    324  1.12.6.1    jruoho 	(void (*)(void *)) SHA512_Init, NULL, NULL, SHA512Update_int,
    325       1.1   thorpej 	(void (*)(u_int8_t *, void *)) SHA512_Final
    326       1.1   thorpej };
    327       1.1   thorpej 
    328  1.12.6.1    jruoho static const struct swcr_auth_hash swcr_auth_hash_aes_xcbc_mac = {
    329  1.12.6.1    jruoho 	&auth_hash_aes_xcbc_mac_96, sizeof(aesxcbc_ctx),
    330  1.12.6.1    jruoho 	null_init,
    331  1.12.6.1    jruoho 	(void (*)(void *, const u_int8_t *, u_int16_t))aes_xcbc_mac_init,
    332  1.12.6.1    jruoho 	NULL, aes_xcbc_mac_loop, aes_xcbc_mac_result
    333  1.12.6.1    jruoho };
    334  1.12.6.1    jruoho 
    335  1.12.6.1    jruoho static const struct swcr_auth_hash swcr_auth_hash_gmac_aes_128 = {
    336  1.12.6.1    jruoho 	&auth_hash_gmac_aes_128, sizeof(AES_GMAC_CTX),
    337  1.12.6.1    jruoho 	(void (*)(void *))AES_GMAC_Init,
    338  1.12.6.1    jruoho 	(void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Setkey,
    339  1.12.6.1    jruoho 	(void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Reinit,
    340  1.12.6.1    jruoho 	(int (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Update,
    341  1.12.6.1    jruoho 	(void (*)(u_int8_t *, void *))AES_GMAC_Final
    342  1.12.6.1    jruoho };
    343  1.12.6.1    jruoho 
    344  1.12.6.1    jruoho static const struct swcr_auth_hash swcr_auth_hash_gmac_aes_192 = {
    345  1.12.6.1    jruoho 	&auth_hash_gmac_aes_192, sizeof(AES_GMAC_CTX),
    346  1.12.6.1    jruoho 	(void (*)(void *))AES_GMAC_Init,
    347  1.12.6.1    jruoho 	(void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Setkey,
    348  1.12.6.1    jruoho 	(void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Reinit,
    349  1.12.6.1    jruoho 	(int (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Update,
    350  1.12.6.1    jruoho 	(void (*)(u_int8_t *, void *))AES_GMAC_Final
    351  1.12.6.1    jruoho };
    352  1.12.6.1    jruoho 
    353  1.12.6.1    jruoho static const struct swcr_auth_hash swcr_auth_hash_gmac_aes_256 = {
    354  1.12.6.1    jruoho 	&auth_hash_gmac_aes_256, sizeof(AES_GMAC_CTX),
    355  1.12.6.1    jruoho 	(void (*)(void *))AES_GMAC_Init,
    356  1.12.6.1    jruoho 	(void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Setkey,
    357  1.12.6.1    jruoho 	(void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Reinit,
    358  1.12.6.1    jruoho 	(int (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Update,
    359  1.12.6.1    jruoho 	(void (*)(u_int8_t *, void *))AES_GMAC_Final
    360  1.12.6.1    jruoho };
    361  1.12.6.1    jruoho 
    362       1.1   thorpej /* Compression instance */
    363       1.1   thorpej static const struct swcr_comp_algo swcr_comp_algo_deflate = {
    364       1.1   thorpej 	&comp_algo_deflate,
    365       1.1   thorpej 	deflate_compress,
    366       1.1   thorpej 	deflate_decompress
    367       1.1   thorpej };
    368       1.1   thorpej 
    369  1.12.6.1    jruoho static const struct swcr_comp_algo swcr_comp_algo_deflate_nogrow = {
    370  1.12.6.1    jruoho 	&comp_algo_deflate_nogrow,
    371  1.12.6.1    jruoho 	deflate_compress,
    372  1.12.6.1    jruoho 	deflate_decompress
    373  1.12.6.1    jruoho };
    374  1.12.6.1    jruoho 
    375      1.12    darran static const struct swcr_comp_algo swcr_comp_algo_gzip = {
    376      1.12    darran 	&comp_algo_deflate,
    377      1.12    darran 	gzip_compress,
    378      1.12    darran 	gzip_decompress
    379      1.12    darran };
    380      1.12    darran 
    381       1.1   thorpej /*
    382       1.1   thorpej  * Encryption wrapper routines.
    383       1.1   thorpej  */
    384       1.1   thorpej static void
    385       1.5  christos null_encrypt(void *key, u_int8_t *blk)
    386       1.1   thorpej {
    387       1.1   thorpej }
    388       1.1   thorpej static void
    389       1.5  christos null_decrypt(void *key, u_int8_t *blk)
    390       1.1   thorpej {
    391       1.1   thorpej }
    392       1.1   thorpej static int
    393       1.4  christos null_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    394       1.1   thorpej {
    395       1.1   thorpej 	*sched = NULL;
    396       1.1   thorpej 	return 0;
    397       1.1   thorpej }
    398       1.1   thorpej static void
    399       1.1   thorpej null_zerokey(u_int8_t **sched)
    400       1.1   thorpej {
    401       1.1   thorpej 	*sched = NULL;
    402       1.1   thorpej }
    403       1.1   thorpej 
    404       1.1   thorpej static void
    405       1.5  christos des1_encrypt(void *key, u_int8_t *blk)
    406       1.1   thorpej {
    407       1.1   thorpej 	des_cblock *cb = (des_cblock *) blk;
    408       1.1   thorpej 	des_key_schedule *p = (des_key_schedule *) key;
    409       1.1   thorpej 
    410       1.1   thorpej 	des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
    411       1.1   thorpej }
    412       1.1   thorpej 
    413       1.1   thorpej static void
    414       1.5  christos des1_decrypt(void *key, u_int8_t *blk)
    415       1.1   thorpej {
    416       1.1   thorpej 	des_cblock *cb = (des_cblock *) blk;
    417       1.1   thorpej 	des_key_schedule *p = (des_key_schedule *) key;
    418       1.1   thorpej 
    419       1.1   thorpej 	des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
    420       1.1   thorpej }
    421       1.1   thorpej 
    422       1.1   thorpej static int
    423       1.4  christos des1_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    424       1.1   thorpej {
    425       1.1   thorpej 	des_key_schedule *p;
    426       1.1   thorpej 	int err;
    427       1.1   thorpej 
    428       1.9    cegger 	p = malloc(sizeof (des_key_schedule),
    429       1.8       tls 		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
    430       1.1   thorpej 	if (p != NULL) {
    431       1.1   thorpej 		des_set_key((des_cblock *)__UNCONST(key), p[0]);
    432       1.1   thorpej 		err = 0;
    433       1.1   thorpej 	} else
    434       1.1   thorpej 		err = ENOMEM;
    435       1.1   thorpej 	*sched = (u_int8_t *) p;
    436       1.1   thorpej 	return err;
    437       1.1   thorpej }
    438       1.1   thorpej 
    439       1.1   thorpej static void
    440       1.1   thorpej des1_zerokey(u_int8_t **sched)
    441       1.1   thorpej {
    442      1.11    cegger 	memset(*sched, 0, sizeof (des_key_schedule));
    443       1.9    cegger 	free(*sched, M_CRYPTO_DATA);
    444       1.1   thorpej 	*sched = NULL;
    445       1.1   thorpej }
    446       1.1   thorpej 
    447       1.1   thorpej static void
    448       1.5  christos des3_encrypt(void *key, u_int8_t *blk)
    449       1.1   thorpej {
    450       1.1   thorpej 	des_cblock *cb = (des_cblock *) blk;
    451       1.1   thorpej 	des_key_schedule *p = (des_key_schedule *) key;
    452       1.1   thorpej 
    453       1.1   thorpej 	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
    454       1.1   thorpej }
    455       1.1   thorpej 
    456       1.1   thorpej static void
    457       1.5  christos des3_decrypt(void *key, u_int8_t *blk)
    458       1.1   thorpej {
    459       1.1   thorpej 	des_cblock *cb = (des_cblock *) blk;
    460       1.1   thorpej 	des_key_schedule *p = (des_key_schedule *) key;
    461       1.1   thorpej 
    462       1.1   thorpej 	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
    463       1.1   thorpej }
    464       1.1   thorpej 
    465       1.1   thorpej static int
    466       1.4  christos des3_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    467       1.1   thorpej {
    468       1.1   thorpej 	des_key_schedule *p;
    469       1.1   thorpej 	int err;
    470       1.1   thorpej 
    471       1.9    cegger 	p = malloc(3*sizeof (des_key_schedule),
    472       1.8       tls 		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
    473       1.1   thorpej 	if (p != NULL) {
    474       1.1   thorpej 		des_set_key((des_cblock *)__UNCONST(key +  0), p[0]);
    475       1.1   thorpej 		des_set_key((des_cblock *)__UNCONST(key +  8), p[1]);
    476       1.1   thorpej 		des_set_key((des_cblock *)__UNCONST(key + 16), p[2]);
    477       1.1   thorpej 		err = 0;
    478       1.1   thorpej 	} else
    479       1.1   thorpej 		err = ENOMEM;
    480       1.1   thorpej 	*sched = (u_int8_t *) p;
    481       1.1   thorpej 	return err;
    482       1.1   thorpej }
    483       1.1   thorpej 
    484       1.1   thorpej static void
    485       1.1   thorpej des3_zerokey(u_int8_t **sched)
    486       1.1   thorpej {
    487      1.11    cegger 	memset(*sched, 0, 3*sizeof (des_key_schedule));
    488       1.9    cegger 	free(*sched, M_CRYPTO_DATA);
    489       1.1   thorpej 	*sched = NULL;
    490       1.1   thorpej }
    491       1.1   thorpej 
    492       1.1   thorpej static void
    493       1.5  christos blf_encrypt(void *key, u_int8_t *blk)
    494       1.1   thorpej {
    495       1.1   thorpej 
    496       1.1   thorpej 	BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 1);
    497       1.1   thorpej }
    498       1.1   thorpej 
    499       1.1   thorpej static void
    500       1.5  christos blf_decrypt(void *key, u_int8_t *blk)
    501       1.1   thorpej {
    502       1.1   thorpej 
    503       1.1   thorpej 	BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 0);
    504       1.1   thorpej }
    505       1.1   thorpej 
    506       1.1   thorpej static int
    507       1.1   thorpej blf_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    508       1.1   thorpej {
    509       1.1   thorpej 	int err;
    510       1.1   thorpej 
    511       1.9    cegger 	*sched = malloc(sizeof(BF_KEY),
    512       1.8       tls 		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
    513       1.1   thorpej 	if (*sched != NULL) {
    514       1.1   thorpej 		BF_set_key((BF_KEY *) *sched, len, key);
    515       1.1   thorpej 		err = 0;
    516       1.1   thorpej 	} else
    517       1.1   thorpej 		err = ENOMEM;
    518       1.1   thorpej 	return err;
    519       1.1   thorpej }
    520       1.1   thorpej 
    521       1.1   thorpej static void
    522       1.1   thorpej blf_zerokey(u_int8_t **sched)
    523       1.1   thorpej {
    524      1.11    cegger 	memset(*sched, 0, sizeof(BF_KEY));
    525       1.9    cegger 	free(*sched, M_CRYPTO_DATA);
    526       1.1   thorpej 	*sched = NULL;
    527       1.1   thorpej }
    528       1.1   thorpej 
    529       1.1   thorpej static void
    530       1.5  christos cast5_encrypt(void *key, u_int8_t *blk)
    531       1.1   thorpej {
    532       1.1   thorpej 	cast128_encrypt((cast128_key *) key, blk, blk);
    533       1.1   thorpej }
    534       1.1   thorpej 
    535       1.1   thorpej static void
    536       1.5  christos cast5_decrypt(void *key, u_int8_t *blk)
    537       1.1   thorpej {
    538       1.1   thorpej 	cast128_decrypt((cast128_key *) key, blk, blk);
    539       1.1   thorpej }
    540       1.1   thorpej 
    541       1.1   thorpej static int
    542       1.1   thorpej cast5_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    543       1.1   thorpej {
    544       1.1   thorpej 	int err;
    545       1.1   thorpej 
    546       1.9    cegger 	*sched = malloc(sizeof(cast128_key), M_CRYPTO_DATA,
    547       1.8       tls 	       M_NOWAIT|M_ZERO);
    548       1.1   thorpej 	if (*sched != NULL) {
    549       1.1   thorpej 		cast128_setkey((cast128_key *)*sched, key, len);
    550       1.1   thorpej 		err = 0;
    551       1.1   thorpej 	} else
    552       1.1   thorpej 		err = ENOMEM;
    553       1.1   thorpej 	return err;
    554       1.1   thorpej }
    555       1.1   thorpej 
    556       1.1   thorpej static void
    557       1.1   thorpej cast5_zerokey(u_int8_t **sched)
    558       1.1   thorpej {
    559      1.11    cegger 	memset(*sched, 0, sizeof(cast128_key));
    560       1.9    cegger 	free(*sched, M_CRYPTO_DATA);
    561       1.1   thorpej 	*sched = NULL;
    562       1.1   thorpej }
    563       1.1   thorpej 
    564       1.1   thorpej static void
    565       1.5  christos skipjack_encrypt(void *key, u_int8_t *blk)
    566       1.1   thorpej {
    567       1.1   thorpej 	skipjack_forwards(blk, blk, (u_int8_t **) key);
    568       1.1   thorpej }
    569       1.1   thorpej 
    570       1.1   thorpej static void
    571       1.5  christos skipjack_decrypt(void *key, u_int8_t *blk)
    572       1.1   thorpej {
    573       1.1   thorpej 	skipjack_backwards(blk, blk, (u_int8_t **) key);
    574       1.1   thorpej }
    575       1.1   thorpej 
    576       1.1   thorpej static int
    577       1.4  christos skipjack_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    578       1.1   thorpej {
    579       1.1   thorpej 	int err;
    580       1.1   thorpej 
    581       1.1   thorpej 	/* NB: allocate all the memory that's needed at once */
    582       1.1   thorpej 	/* XXX assumes bytes are aligned on sizeof(u_char) == 1 boundaries.
    583       1.1   thorpej 	 * Will this break a pdp-10, Cray-1, or GE-645 port?
    584       1.1   thorpej 	 */
    585       1.9    cegger 	*sched = malloc(10 * (sizeof(u_int8_t *) + 0x100),
    586       1.8       tls 		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
    587       1.1   thorpej 
    588       1.1   thorpej 	if (*sched != NULL) {
    589       1.1   thorpej 
    590       1.1   thorpej 		u_int8_t** key_tables = (u_int8_t**) *sched;
    591       1.1   thorpej 		u_int8_t* table = (u_int8_t*) &key_tables[10];
    592       1.1   thorpej 		int k;
    593       1.1   thorpej 
    594       1.1   thorpej 		for (k = 0; k < 10; k++) {
    595       1.1   thorpej 			key_tables[k] = table;
    596       1.1   thorpej 			table += 0x100;
    597       1.1   thorpej 		}
    598       1.1   thorpej 		subkey_table_gen(key, (u_int8_t **) *sched);
    599       1.1   thorpej 		err = 0;
    600       1.1   thorpej 	} else
    601       1.1   thorpej 		err = ENOMEM;
    602       1.1   thorpej 	return err;
    603       1.1   thorpej }
    604       1.1   thorpej 
    605       1.1   thorpej static void
    606       1.1   thorpej skipjack_zerokey(u_int8_t **sched)
    607       1.1   thorpej {
    608      1.11    cegger 	memset(*sched, 0, 10 * (sizeof(u_int8_t *) + 0x100));
    609       1.9    cegger 	free(*sched, M_CRYPTO_DATA);
    610       1.1   thorpej 	*sched = NULL;
    611       1.1   thorpej }
    612       1.1   thorpej 
    613       1.1   thorpej static void
    614       1.5  christos rijndael128_encrypt(void *key, u_int8_t *blk)
    615       1.1   thorpej {
    616       1.1   thorpej 	rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
    617       1.1   thorpej }
    618       1.1   thorpej 
    619       1.1   thorpej static void
    620       1.5  christos rijndael128_decrypt(void *key, u_int8_t *blk)
    621       1.1   thorpej {
    622       1.1   thorpej 	rijndael_decrypt((rijndael_ctx *) key, (u_char *) blk,
    623       1.1   thorpej 	    (u_char *) blk);
    624       1.1   thorpej }
    625       1.1   thorpej 
    626       1.1   thorpej static int
    627       1.1   thorpej rijndael128_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    628       1.1   thorpej {
    629       1.1   thorpej 	int err;
    630       1.1   thorpej 
    631  1.12.6.1    jruoho 	if (len != 16 && len != 24 && len != 32)
    632  1.12.6.1    jruoho 		return EINVAL;
    633       1.9    cegger 	*sched = malloc(sizeof(rijndael_ctx), M_CRYPTO_DATA,
    634       1.8       tls 	    M_NOWAIT|M_ZERO);
    635       1.1   thorpej 	if (*sched != NULL) {
    636       1.1   thorpej 		rijndael_set_key((rijndael_ctx *) *sched, key, len * 8);
    637       1.1   thorpej 		err = 0;
    638       1.1   thorpej 	} else
    639       1.1   thorpej 		err = ENOMEM;
    640       1.1   thorpej 	return err;
    641       1.1   thorpej }
    642       1.1   thorpej 
    643       1.1   thorpej static void
    644       1.1   thorpej rijndael128_zerokey(u_int8_t **sched)
    645       1.1   thorpej {
    646      1.11    cegger 	memset(*sched, 0, sizeof(rijndael_ctx));
    647       1.9    cegger 	free(*sched, M_CRYPTO_DATA);
    648       1.1   thorpej 	*sched = NULL;
    649       1.1   thorpej }
    650       1.1   thorpej 
    651  1.12.6.1    jruoho static void
    652  1.12.6.1    jruoho cml_encrypt(void *key, u_int8_t *blk)
    653  1.12.6.1    jruoho {
    654  1.12.6.1    jruoho 
    655  1.12.6.1    jruoho 	camellia_encrypt(key, blk, blk);
    656  1.12.6.1    jruoho }
    657  1.12.6.1    jruoho 
    658  1.12.6.1    jruoho static void
    659  1.12.6.1    jruoho cml_decrypt(void *key, u_int8_t *blk)
    660  1.12.6.1    jruoho {
    661  1.12.6.1    jruoho 
    662  1.12.6.1    jruoho 	camellia_decrypt(key, blk, blk);
    663  1.12.6.1    jruoho }
    664  1.12.6.1    jruoho 
    665  1.12.6.1    jruoho static int
    666  1.12.6.1    jruoho cml_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    667  1.12.6.1    jruoho {
    668  1.12.6.1    jruoho 	int err;
    669  1.12.6.1    jruoho 
    670  1.12.6.1    jruoho 	if (len != 16 && len != 24 && len != 32)
    671  1.12.6.1    jruoho 		return (EINVAL);
    672  1.12.6.1    jruoho 	*sched = malloc(sizeof(camellia_ctx), M_CRYPTO_DATA,
    673  1.12.6.1    jruoho 			M_NOWAIT|M_ZERO);
    674  1.12.6.1    jruoho 	if (*sched != NULL) {
    675  1.12.6.1    jruoho 		camellia_set_key((camellia_ctx *) *sched, key, len * 8);
    676  1.12.6.1    jruoho 		err = 0;
    677  1.12.6.1    jruoho 	} else
    678  1.12.6.1    jruoho 		err = ENOMEM;
    679  1.12.6.1    jruoho 	return err;
    680  1.12.6.1    jruoho }
    681  1.12.6.1    jruoho 
    682  1.12.6.1    jruoho static void
    683  1.12.6.1    jruoho cml_zerokey(u_int8_t **sched)
    684  1.12.6.1    jruoho {
    685  1.12.6.1    jruoho 
    686  1.12.6.1    jruoho 	memset(*sched, 0, sizeof(camellia_ctx));
    687  1.12.6.1    jruoho 	free(*sched, M_CRYPTO_DATA);
    688  1.12.6.1    jruoho 	*sched = NULL;
    689  1.12.6.1    jruoho }
    690  1.12.6.1    jruoho 
    691  1.12.6.1    jruoho #define AESCTR_NONCESIZE	4
    692  1.12.6.1    jruoho #define AESCTR_IVSIZE		8
    693  1.12.6.1    jruoho #define AESCTR_BLOCKSIZE	16
    694  1.12.6.1    jruoho 
    695  1.12.6.1    jruoho struct aes_ctr_ctx {
    696  1.12.6.1    jruoho 	/* need only encryption half */
    697  1.12.6.1    jruoho 	u_int32_t ac_ek[4*(RIJNDAEL_MAXNR + 1)];
    698  1.12.6.1    jruoho 	u_int8_t ac_block[AESCTR_BLOCKSIZE];
    699  1.12.6.1    jruoho 	int ac_nr;
    700  1.12.6.1    jruoho 	struct {
    701  1.12.6.1    jruoho 		u_int64_t lastiv;
    702  1.12.6.1    jruoho 	} ivgenctx;
    703  1.12.6.1    jruoho };
    704  1.12.6.1    jruoho 
    705  1.12.6.1    jruoho static void
    706  1.12.6.1    jruoho aes_ctr_crypt(void *key, u_int8_t *blk)
    707  1.12.6.1    jruoho {
    708  1.12.6.1    jruoho 	struct aes_ctr_ctx *ctx;
    709  1.12.6.1    jruoho 	u_int8_t keystream[AESCTR_BLOCKSIZE];
    710  1.12.6.1    jruoho 	int i;
    711  1.12.6.1    jruoho 
    712  1.12.6.1    jruoho 	ctx = key;
    713  1.12.6.1    jruoho 	/* increment counter */
    714  1.12.6.1    jruoho 	for (i = AESCTR_BLOCKSIZE - 1;
    715  1.12.6.1    jruoho 	     i >= AESCTR_NONCESIZE + AESCTR_IVSIZE; i--)
    716  1.12.6.1    jruoho 		if (++ctx->ac_block[i]) /* continue on overflow */
    717  1.12.6.1    jruoho 			break;
    718  1.12.6.1    jruoho 	rijndaelEncrypt(ctx->ac_ek, ctx->ac_nr, ctx->ac_block, keystream);
    719  1.12.6.1    jruoho 	for (i = 0; i < AESCTR_BLOCKSIZE; i++)
    720  1.12.6.1    jruoho 		blk[i] ^= keystream[i];
    721  1.12.6.1    jruoho 	memset(keystream, 0, sizeof(keystream));
    722  1.12.6.1    jruoho }
    723  1.12.6.1    jruoho 
    724  1.12.6.1    jruoho int
    725  1.12.6.1    jruoho aes_ctr_setkey(u_int8_t **sched, const u_int8_t *key, int len)
    726  1.12.6.1    jruoho {
    727  1.12.6.1    jruoho 	struct aes_ctr_ctx *ctx;
    728  1.12.6.1    jruoho 
    729  1.12.6.1    jruoho 	if (len < AESCTR_NONCESIZE)
    730  1.12.6.1    jruoho 		return EINVAL;
    731  1.12.6.1    jruoho 
    732  1.12.6.1    jruoho 	ctx = malloc(sizeof(struct aes_ctr_ctx), M_CRYPTO_DATA,
    733  1.12.6.1    jruoho 		     M_NOWAIT|M_ZERO);
    734  1.12.6.1    jruoho 	if (!ctx)
    735  1.12.6.1    jruoho 		return ENOMEM;
    736  1.12.6.1    jruoho 	ctx->ac_nr = rijndaelKeySetupEnc(ctx->ac_ek, (const u_char *)key,
    737  1.12.6.1    jruoho 			(len - AESCTR_NONCESIZE) * 8);
    738  1.12.6.1    jruoho 	if (!ctx->ac_nr) { /* wrong key len */
    739  1.12.6.1    jruoho 		aes_ctr_zerokey((u_int8_t **)&ctx);
    740  1.12.6.1    jruoho 		return EINVAL;
    741  1.12.6.1    jruoho 	}
    742  1.12.6.1    jruoho 	memcpy(ctx->ac_block, key + len - AESCTR_NONCESIZE, AESCTR_NONCESIZE);
    743  1.12.6.1    jruoho 	/* random start value for simple counter */
    744  1.12.6.1    jruoho 	arc4randbytes(&ctx->ivgenctx.lastiv, sizeof(ctx->ivgenctx.lastiv));
    745  1.12.6.1    jruoho 	*sched = (void *)ctx;
    746  1.12.6.1    jruoho 	return 0;
    747  1.12.6.1    jruoho }
    748  1.12.6.1    jruoho 
    749  1.12.6.1    jruoho void
    750  1.12.6.1    jruoho aes_ctr_zerokey(u_int8_t **sched)
    751  1.12.6.1    jruoho {
    752  1.12.6.1    jruoho 
    753  1.12.6.1    jruoho 	memset(*sched, 0, sizeof(struct aes_ctr_ctx));
    754  1.12.6.1    jruoho 	free(*sched, M_CRYPTO_DATA);
    755  1.12.6.1    jruoho 	*sched = NULL;
    756  1.12.6.1    jruoho }
    757  1.12.6.1    jruoho 
    758  1.12.6.1    jruoho void
    759  1.12.6.1    jruoho aes_ctr_reinit(void *key, const u_int8_t *iv, u_int8_t *ivout)
    760  1.12.6.1    jruoho {
    761  1.12.6.1    jruoho 	struct aes_ctr_ctx *ctx = key;
    762  1.12.6.1    jruoho 
    763  1.12.6.1    jruoho 	if (!iv) {
    764  1.12.6.1    jruoho 		ctx->ivgenctx.lastiv++;
    765  1.12.6.1    jruoho 		iv = (const u_int8_t *)&ctx->ivgenctx.lastiv;
    766  1.12.6.1    jruoho 	}
    767  1.12.6.1    jruoho 	if (ivout)
    768  1.12.6.1    jruoho 		memcpy(ivout, iv, AESCTR_IVSIZE);
    769  1.12.6.1    jruoho 	memcpy(ctx->ac_block + AESCTR_NONCESIZE, iv, AESCTR_IVSIZE);
    770  1.12.6.1    jruoho 	/* reset counter */
    771  1.12.6.1    jruoho 	memset(ctx->ac_block + AESCTR_NONCESIZE + AESCTR_IVSIZE, 0, 4);
    772  1.12.6.1    jruoho }
    773  1.12.6.1    jruoho 
    774  1.12.6.1    jruoho void
    775  1.12.6.1    jruoho aes_gcm_reinit(void *key, const u_int8_t *iv, u_int8_t *ivout)
    776  1.12.6.1    jruoho {
    777  1.12.6.1    jruoho 	struct aes_ctr_ctx *ctx = key;
    778  1.12.6.1    jruoho 
    779  1.12.6.1    jruoho 	if (!iv) {
    780  1.12.6.1    jruoho 		ctx->ivgenctx.lastiv++;
    781  1.12.6.1    jruoho 		iv = (const u_int8_t *)&ctx->ivgenctx.lastiv;
    782  1.12.6.1    jruoho 	}
    783  1.12.6.1    jruoho 	if (ivout)
    784  1.12.6.1    jruoho 		memcpy(ivout, iv, AESCTR_IVSIZE);
    785  1.12.6.1    jruoho 	memcpy(ctx->ac_block + AESCTR_NONCESIZE, iv, AESCTR_IVSIZE);
    786  1.12.6.1    jruoho 	/* reset counter */
    787  1.12.6.1    jruoho 	memset(ctx->ac_block + AESCTR_NONCESIZE + AESCTR_IVSIZE, 0, 4);
    788  1.12.6.1    jruoho 	ctx->ac_block[AESCTR_BLOCKSIZE - 1] = 1; /* GCM starts with 1 */
    789  1.12.6.1    jruoho }
    790  1.12.6.1    jruoho 
    791       1.1   thorpej /*
    792       1.1   thorpej  * And now for auth.
    793       1.1   thorpej  */
    794       1.1   thorpej 
    795       1.1   thorpej static void
    796       1.4  christos null_init(void *ctx)
    797       1.1   thorpej {
    798       1.1   thorpej }
    799       1.1   thorpej 
    800       1.1   thorpej static int
    801       1.4  christos null_update(void *ctx, const u_int8_t *buf,
    802       1.4  christos     u_int16_t len)
    803       1.1   thorpej {
    804       1.1   thorpej 	return 0;
    805       1.1   thorpej }
    806       1.1   thorpej 
    807       1.1   thorpej static void
    808       1.4  christos null_final(u_int8_t *buf, void *ctx)
    809       1.1   thorpej {
    810       1.1   thorpej 	if (buf != (u_int8_t *) 0)
    811      1.11    cegger 		memset(buf, 0, 12);
    812       1.1   thorpej }
    813       1.1   thorpej 
    814       1.1   thorpej static int
    815       1.1   thorpej RMD160Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    816       1.1   thorpej {
    817       1.1   thorpej 	RMD160Update(ctx, buf, len);
    818       1.1   thorpej 	return 0;
    819       1.1   thorpej }
    820       1.1   thorpej 
    821       1.1   thorpej static int
    822       1.1   thorpej MD5Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    823       1.1   thorpej {
    824       1.1   thorpej 	MD5Update(ctx, buf, len);
    825       1.1   thorpej 	return 0;
    826       1.1   thorpej }
    827       1.1   thorpej 
    828       1.1   thorpej static void
    829       1.1   thorpej SHA1Init_int(void *ctx)
    830       1.1   thorpej {
    831       1.1   thorpej 	SHA1Init(ctx);
    832       1.1   thorpej }
    833       1.1   thorpej 
    834       1.1   thorpej static int
    835       1.1   thorpej SHA1Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    836       1.1   thorpej {
    837       1.1   thorpej 	SHA1Update(ctx, buf, len);
    838       1.1   thorpej 	return 0;
    839       1.1   thorpej }
    840       1.1   thorpej 
    841       1.1   thorpej static void
    842       1.1   thorpej SHA1Final_int(u_int8_t *blk, void *ctx)
    843       1.1   thorpej {
    844       1.1   thorpej 	SHA1Final(blk, ctx);
    845       1.1   thorpej }
    846       1.1   thorpej 
    847       1.1   thorpej static int
    848       1.1   thorpej SHA256Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    849       1.1   thorpej {
    850       1.1   thorpej 	SHA256_Update(ctx, buf, len);
    851       1.1   thorpej 	return 0;
    852       1.1   thorpej }
    853       1.1   thorpej 
    854       1.1   thorpej static int
    855       1.1   thorpej SHA384Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    856       1.1   thorpej {
    857       1.1   thorpej 	SHA384_Update(ctx, buf, len);
    858       1.1   thorpej 	return 0;
    859       1.1   thorpej }
    860       1.1   thorpej 
    861       1.1   thorpej static int
    862       1.1   thorpej SHA512Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
    863       1.1   thorpej {
    864       1.1   thorpej 	SHA512_Update(ctx, buf, len);
    865       1.1   thorpej 	return 0;
    866       1.1   thorpej }
    867       1.1   thorpej 
    868       1.1   thorpej /*
    869       1.1   thorpej  * And compression
    870       1.1   thorpej  */
    871       1.1   thorpej 
    872       1.1   thorpej static u_int32_t
    873      1.10       dsl deflate_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
    874       1.1   thorpej {
    875  1.12.6.1    jruoho 	return deflate_global(data, size, 0, out, 0);
    876       1.1   thorpej }
    877       1.1   thorpej 
    878       1.1   thorpej static u_int32_t
    879  1.12.6.1    jruoho deflate_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
    880  1.12.6.1    jruoho 		   int size_hint)
    881       1.1   thorpej {
    882  1.12.6.1    jruoho 	return deflate_global(data, size, 1, out, size_hint);
    883       1.1   thorpej }
    884      1.12    darran 
    885      1.12    darran static u_int32_t
    886      1.12    darran gzip_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
    887      1.12    darran {
    888  1.12.6.1    jruoho 	return gzip_global(data, size, 0, out, 0);
    889      1.12    darran }
    890      1.12    darran 
    891      1.12    darran static u_int32_t
    892  1.12.6.1    jruoho gzip_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
    893  1.12.6.1    jruoho 		int size_hint)
    894      1.12    darran {
    895  1.12.6.1    jruoho 	return gzip_global(data, size, 1, out, size_hint);
    896      1.12    darran }
    897