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