Home | History | Annotate | Line # | Download | only in opencrypto
cryptosoft.c revision 1.11.10.1
      1  1.11.10.1      elad /*	$NetBSD: cryptosoft.c,v 1.11.10.1 2006/04/19 04:33:31 elad Exp $ */
      2        1.1  jonathan /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
      3        1.1  jonathan /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
      4        1.1  jonathan 
      5        1.1  jonathan /*
      6        1.1  jonathan  * The author of this code is Angelos D. Keromytis (angelos (at) cis.upenn.edu)
      7        1.1  jonathan  *
      8        1.1  jonathan  * This code was written by Angelos D. Keromytis in Athens, Greece, in
      9        1.1  jonathan  * February 2000. Network Security Technologies Inc. (NSTI) kindly
     10        1.1  jonathan  * supported the development of this code.
     11        1.1  jonathan  *
     12        1.1  jonathan  * Copyright (c) 2000, 2001 Angelos D. Keromytis
     13        1.1  jonathan  *
     14        1.1  jonathan  * Permission to use, copy, and modify this software with or without fee
     15        1.1  jonathan  * is hereby granted, provided that this entire notice is included in
     16        1.1  jonathan  * all source code copies of any software which is or includes a copy or
     17        1.1  jonathan  * modification of this software.
     18        1.1  jonathan  *
     19        1.1  jonathan  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
     20        1.1  jonathan  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
     21        1.1  jonathan  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
     22        1.1  jonathan  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
     23        1.1  jonathan  * PURPOSE.
     24        1.1  jonathan  */
     25        1.1  jonathan 
     26        1.1  jonathan #include <sys/cdefs.h>
     27  1.11.10.1      elad __KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.11.10.1 2006/04/19 04:33:31 elad Exp $");
     28        1.1  jonathan 
     29        1.1  jonathan #include <sys/param.h>
     30        1.1  jonathan #include <sys/systm.h>
     31        1.1  jonathan #include <sys/malloc.h>
     32        1.1  jonathan #include <sys/mbuf.h>
     33        1.1  jonathan #include <sys/sysctl.h>
     34        1.1  jonathan #include <sys/errno.h>
     35        1.5  jonathan 
     36        1.1  jonathan #include <opencrypto/cryptodev.h>
     37        1.1  jonathan #include <opencrypto/cryptosoft.h>
     38        1.1  jonathan #include <opencrypto/xform.h>
     39        1.1  jonathan 
     40       1.10   thorpej #include <opencrypto/cryptosoft_xform.c>
     41        1.1  jonathan 
     42       1.10   thorpej union authctx {
     43       1.10   thorpej 	MD5_CTX md5ctx;
     44       1.10   thorpej 	SHA1_CTX sha1ctx;
     45       1.10   thorpej 	RMD160_CTX rmd160ctx;
     46       1.10   thorpej 	SHA256_CTX sha256ctx;
     47       1.10   thorpej 	SHA384_CTX sha384ctx;
     48       1.10   thorpej 	SHA512_CTX sha512ctx;
     49        1.1  jonathan };
     50        1.1  jonathan 
     51        1.1  jonathan struct swcr_data **swcr_sessions = NULL;
     52        1.1  jonathan u_int32_t swcr_sesnum = 0;
     53        1.1  jonathan int32_t swcr_id = -1;
     54        1.1  jonathan 
     55        1.1  jonathan #define COPYBACK(x, a, b, c, d) \
     56        1.1  jonathan 	(x) == CRYPTO_BUF_MBUF ? m_copyback((struct mbuf *)a,b,c,d) \
     57        1.1  jonathan 	: cuio_copyback((struct uio *)a,b,c,d)
     58        1.1  jonathan #define COPYDATA(x, a, b, c, d) \
     59        1.1  jonathan 	(x) == CRYPTO_BUF_MBUF ? m_copydata((struct mbuf *)a,b,c,d) \
     60        1.1  jonathan 	: cuio_copydata((struct uio *)a,b,c,d)
     61        1.1  jonathan 
     62        1.1  jonathan static	int swcr_encdec(struct cryptodesc *, struct swcr_data *, caddr_t, int);
     63        1.1  jonathan static	int swcr_authcompute(struct cryptop *crp, struct cryptodesc *crd,
     64        1.1  jonathan 			     struct swcr_data *sw, caddr_t buf, int outtype);
     65        1.1  jonathan static	int swcr_compdec(struct cryptodesc *, struct swcr_data *, caddr_t, int);
     66        1.1  jonathan static	int swcr_process(void *, struct cryptop *, int);
     67        1.1  jonathan static	int swcr_newsession(void *, u_int32_t *, struct cryptoini *);
     68        1.1  jonathan static	int swcr_freesession(void *, u_int64_t);
     69        1.1  jonathan 
     70        1.1  jonathan /*
     71        1.1  jonathan  * Apply a symmetric encryption/decryption algorithm.
     72        1.1  jonathan  */
     73        1.1  jonathan static int
     74        1.1  jonathan swcr_encdec(struct cryptodesc *crd, struct swcr_data *sw, caddr_t buf,
     75        1.1  jonathan     int outtype)
     76        1.1  jonathan {
     77        1.1  jonathan 	unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN], *idat;
     78        1.1  jonathan 	unsigned char *ivp, piv[EALG_MAX_BLOCK_LEN];
     79       1.10   thorpej 	const struct swcr_enc_xform *exf;
     80        1.1  jonathan 	int i, k, j, blks;
     81        1.1  jonathan 	int count, ind;
     82        1.1  jonathan 
     83        1.1  jonathan 	exf = sw->sw_exf;
     84       1.10   thorpej 	blks = exf->enc_xform->blocksize;
     85        1.1  jonathan 
     86        1.1  jonathan 	/* Check for non-padded data */
     87        1.1  jonathan 	if (crd->crd_len % blks)
     88        1.1  jonathan 		return EINVAL;
     89        1.1  jonathan 
     90        1.1  jonathan 	/* Initialize the IV */
     91        1.1  jonathan 	if (crd->crd_flags & CRD_F_ENCRYPT) {
     92        1.1  jonathan 		/* IV explicitly provided ? */
     93        1.1  jonathan 		if (crd->crd_flags & CRD_F_IV_EXPLICIT)
     94        1.1  jonathan 			bcopy(crd->crd_iv, iv, blks);
     95        1.1  jonathan 		else {
     96        1.1  jonathan 			/* Get random IV */
     97        1.1  jonathan 			for (i = 0;
     98        1.1  jonathan 			    i + sizeof (u_int32_t) < EALG_MAX_BLOCK_LEN;
     99        1.1  jonathan 			    i += sizeof (u_int32_t)) {
    100        1.1  jonathan 				u_int32_t temp = arc4random();
    101        1.1  jonathan 
    102        1.1  jonathan 				bcopy(&temp, iv + i, sizeof(u_int32_t));
    103        1.1  jonathan 			}
    104        1.1  jonathan 			/*
    105        1.1  jonathan 			 * What if the block size is not a multiple
    106        1.1  jonathan 			 * of sizeof (u_int32_t), which is the size of
    107        1.1  jonathan 			 * what arc4random() returns ?
    108        1.1  jonathan 			 */
    109        1.1  jonathan 			if (EALG_MAX_BLOCK_LEN % sizeof (u_int32_t) != 0) {
    110        1.1  jonathan 				u_int32_t temp = arc4random();
    111        1.1  jonathan 
    112        1.1  jonathan 				bcopy (&temp, iv + i,
    113        1.1  jonathan 				    EALG_MAX_BLOCK_LEN - i);
    114        1.1  jonathan 			}
    115        1.1  jonathan 		}
    116        1.1  jonathan 
    117        1.1  jonathan 		/* Do we need to write the IV */
    118        1.1  jonathan 		if (!(crd->crd_flags & CRD_F_IV_PRESENT)) {
    119        1.1  jonathan 			COPYBACK(outtype, buf, crd->crd_inject, blks, iv);
    120        1.1  jonathan 		}
    121        1.1  jonathan 
    122        1.1  jonathan 	} else {	/* Decryption */
    123        1.1  jonathan 			/* IV explicitly provided ? */
    124        1.1  jonathan 		if (crd->crd_flags & CRD_F_IV_EXPLICIT)
    125        1.1  jonathan 			bcopy(crd->crd_iv, iv, blks);
    126        1.1  jonathan 		else {
    127        1.1  jonathan 			/* Get IV off buf */
    128        1.1  jonathan 			COPYDATA(outtype, buf, crd->crd_inject, blks, iv);
    129        1.1  jonathan 		}
    130        1.1  jonathan 	}
    131        1.1  jonathan 
    132        1.1  jonathan 	ivp = iv;
    133        1.1  jonathan 
    134        1.1  jonathan 	if (outtype == CRYPTO_BUF_CONTIG) {
    135        1.1  jonathan 		if (crd->crd_flags & CRD_F_ENCRYPT) {
    136        1.1  jonathan 			for (i = crd->crd_skip;
    137        1.1  jonathan 			    i < crd->crd_skip + crd->crd_len; i += blks) {
    138        1.1  jonathan 				/* XOR with the IV/previous block, as appropriate. */
    139        1.1  jonathan 				if (i == crd->crd_skip)
    140        1.1  jonathan 					for (k = 0; k < blks; k++)
    141        1.1  jonathan 						buf[i + k] ^= ivp[k];
    142        1.1  jonathan 				else
    143        1.1  jonathan 					for (k = 0; k < blks; k++)
    144        1.1  jonathan 						buf[i + k] ^= buf[i + k - blks];
    145        1.1  jonathan 				exf->encrypt(sw->sw_kschedule, buf + i);
    146        1.1  jonathan 			}
    147        1.1  jonathan 		} else {		/* Decrypt */
    148        1.1  jonathan 			/*
    149        1.1  jonathan 			 * Start at the end, so we don't need to keep the encrypted
    150        1.1  jonathan 			 * block as the IV for the next block.
    151        1.1  jonathan 			 */
    152        1.1  jonathan 			for (i = crd->crd_skip + crd->crd_len - blks;
    153        1.1  jonathan 			    i >= crd->crd_skip; i -= blks) {
    154        1.1  jonathan 				exf->decrypt(sw->sw_kschedule, buf + i);
    155        1.1  jonathan 
    156        1.1  jonathan 				/* XOR with the IV/previous block, as appropriate */
    157        1.1  jonathan 				if (i == crd->crd_skip)
    158        1.1  jonathan 					for (k = 0; k < blks; k++)
    159        1.1  jonathan 						buf[i + k] ^= ivp[k];
    160        1.1  jonathan 				else
    161        1.1  jonathan 					for (k = 0; k < blks; k++)
    162        1.1  jonathan 						buf[i + k] ^= buf[i + k - blks];
    163        1.1  jonathan 			}
    164        1.1  jonathan 		}
    165        1.1  jonathan 
    166        1.1  jonathan 		return 0;
    167        1.1  jonathan 	} else if (outtype == CRYPTO_BUF_MBUF) {
    168        1.1  jonathan 		struct mbuf *m = (struct mbuf *) buf;
    169        1.1  jonathan 
    170        1.1  jonathan 		/* Find beginning of data */
    171        1.1  jonathan 		m = m_getptr(m, crd->crd_skip, &k);
    172        1.1  jonathan 		if (m == NULL)
    173        1.1  jonathan 			return EINVAL;
    174        1.1  jonathan 
    175        1.1  jonathan 		i = crd->crd_len;
    176        1.1  jonathan 
    177        1.1  jonathan 		while (i > 0) {
    178        1.1  jonathan 			/*
    179        1.1  jonathan 			 * If there's insufficient data at the end of
    180        1.1  jonathan 			 * an mbuf, we have to do some copying.
    181        1.1  jonathan 			 */
    182        1.1  jonathan 			if (m->m_len < k + blks && m->m_len != k) {
    183        1.1  jonathan 				m_copydata(m, k, blks, blk);
    184        1.1  jonathan 
    185        1.1  jonathan 				/* Actual encryption/decryption */
    186        1.1  jonathan 				if (crd->crd_flags & CRD_F_ENCRYPT) {
    187        1.1  jonathan 					/* XOR with previous block */
    188        1.1  jonathan 					for (j = 0; j < blks; j++)
    189        1.1  jonathan 						blk[j] ^= ivp[j];
    190        1.1  jonathan 
    191        1.1  jonathan 					exf->encrypt(sw->sw_kschedule, blk);
    192        1.1  jonathan 
    193        1.1  jonathan 					/*
    194        1.1  jonathan 					 * Keep encrypted block for XOR'ing
    195        1.1  jonathan 					 * with next block
    196        1.1  jonathan 					 */
    197        1.1  jonathan 					bcopy(blk, iv, blks);
    198        1.1  jonathan 					ivp = iv;
    199        1.1  jonathan 				} else {	/* decrypt */
    200        1.1  jonathan 					/*
    201        1.1  jonathan 					 * Keep encrypted block for XOR'ing
    202        1.1  jonathan 					 * with next block
    203        1.1  jonathan 					 */
    204        1.1  jonathan 					if (ivp == iv)
    205        1.1  jonathan 						bcopy(blk, piv, blks);
    206        1.1  jonathan 					else
    207        1.1  jonathan 						bcopy(blk, iv, blks);
    208        1.1  jonathan 
    209        1.1  jonathan 					exf->decrypt(sw->sw_kschedule, blk);
    210        1.1  jonathan 
    211        1.1  jonathan 					/* XOR with previous block */
    212        1.1  jonathan 					for (j = 0; j < blks; j++)
    213        1.1  jonathan 						blk[j] ^= ivp[j];
    214        1.1  jonathan 
    215        1.1  jonathan 					if (ivp == iv)
    216        1.1  jonathan 						bcopy(piv, iv, blks);
    217        1.1  jonathan 					else
    218        1.1  jonathan 						ivp = iv;
    219        1.1  jonathan 				}
    220        1.1  jonathan 
    221        1.1  jonathan 				/* Copy back decrypted block */
    222        1.1  jonathan 				m_copyback(m, k, blks, blk);
    223        1.1  jonathan 
    224        1.1  jonathan 				/* Advance pointer */
    225        1.1  jonathan 				m = m_getptr(m, k + blks, &k);
    226        1.1  jonathan 				if (m == NULL)
    227        1.1  jonathan 					return EINVAL;
    228        1.1  jonathan 
    229        1.1  jonathan 				i -= blks;
    230        1.1  jonathan 
    231        1.1  jonathan 				/* Could be done... */
    232        1.1  jonathan 				if (i == 0)
    233        1.1  jonathan 					break;
    234        1.1  jonathan 			}
    235        1.1  jonathan 
    236        1.1  jonathan 			/* Skip possibly empty mbufs */
    237        1.1  jonathan 			if (k == m->m_len) {
    238        1.1  jonathan 				for (m = m->m_next; m && m->m_len == 0;
    239        1.1  jonathan 				    m = m->m_next)
    240        1.1  jonathan 					;
    241        1.1  jonathan 				k = 0;
    242        1.1  jonathan 			}
    243        1.1  jonathan 
    244        1.1  jonathan 			/* Sanity check */
    245        1.1  jonathan 			if (m == NULL)
    246        1.1  jonathan 				return EINVAL;
    247        1.1  jonathan 
    248        1.1  jonathan 			/*
    249        1.1  jonathan 			 * Warning: idat may point to garbage here, but
    250        1.1  jonathan 			 * we only use it in the while() loop, only if
    251        1.1  jonathan 			 * there are indeed enough data.
    252        1.1  jonathan 			 */
    253        1.1  jonathan 			idat = mtod(m, unsigned char *) + k;
    254        1.1  jonathan 
    255        1.1  jonathan 			while (m->m_len >= k + blks && i > 0) {
    256        1.1  jonathan 				if (crd->crd_flags & CRD_F_ENCRYPT) {
    257        1.1  jonathan 					/* XOR with previous block/IV */
    258        1.1  jonathan 					for (j = 0; j < blks; j++)
    259        1.1  jonathan 						idat[j] ^= ivp[j];
    260        1.1  jonathan 
    261        1.1  jonathan 					exf->encrypt(sw->sw_kschedule, idat);
    262        1.1  jonathan 					ivp = idat;
    263        1.1  jonathan 				} else {	/* decrypt */
    264        1.1  jonathan 					/*
    265        1.1  jonathan 					 * Keep encrypted block to be used
    266        1.1  jonathan 					 * in next block's processing.
    267        1.1  jonathan 					 */
    268        1.1  jonathan 					if (ivp == iv)
    269        1.1  jonathan 						bcopy(idat, piv, blks);
    270        1.1  jonathan 					else
    271        1.1  jonathan 						bcopy(idat, iv, blks);
    272        1.1  jonathan 
    273        1.1  jonathan 					exf->decrypt(sw->sw_kschedule, idat);
    274        1.1  jonathan 
    275        1.1  jonathan 					/* XOR with previous block/IV */
    276        1.1  jonathan 					for (j = 0; j < blks; j++)
    277        1.1  jonathan 						idat[j] ^= ivp[j];
    278        1.1  jonathan 
    279        1.1  jonathan 					if (ivp == iv)
    280        1.1  jonathan 						bcopy(piv, iv, blks);
    281        1.1  jonathan 					else
    282        1.1  jonathan 						ivp = iv;
    283        1.1  jonathan 				}
    284        1.1  jonathan 
    285        1.1  jonathan 				idat += blks;
    286        1.1  jonathan 				k += blks;
    287        1.1  jonathan 				i -= blks;
    288        1.1  jonathan 			}
    289        1.1  jonathan 		}
    290        1.1  jonathan 
    291        1.1  jonathan 		return 0; /* Done with mbuf encryption/decryption */
    292        1.1  jonathan 	} else if (outtype == CRYPTO_BUF_IOV) {
    293        1.1  jonathan 		struct uio *uio = (struct uio *) buf;
    294        1.1  jonathan 
    295        1.1  jonathan #ifdef __FreeBSD__
    296        1.1  jonathan 		struct iovec *iov;
    297        1.1  jonathan 		/* Find beginning of data */
    298        1.1  jonathan 		iov = cuio_getptr(uio, crd->crd_skip, &k);
    299        1.1  jonathan 		if (iov == NULL)
    300        1.1  jonathan 			return EINVAL;
    301        1.1  jonathan 
    302        1.1  jonathan 		i = crd->crd_len;
    303        1.1  jonathan 
    304        1.1  jonathan 		while (i > 0) {
    305        1.1  jonathan 			/*
    306        1.1  jonathan 			 * If there's insufficient data at the end of
    307        1.1  jonathan 			 * an iovec, we have to do some copying.
    308        1.1  jonathan 			 */
    309        1.1  jonathan 			if (iov->iov_len < k + blks && iov->iov_len != k) {
    310        1.1  jonathan 				cuio_copydata(uio, k, blks, blk);
    311        1.1  jonathan 
    312        1.1  jonathan 				/* Actual encryption/decryption */
    313        1.1  jonathan 				if (crd->crd_flags & CRD_F_ENCRYPT) {
    314        1.1  jonathan 					/* XOR with previous block */
    315        1.1  jonathan 					for (j = 0; j < blks; j++)
    316        1.1  jonathan 						blk[j] ^= ivp[j];
    317        1.1  jonathan 
    318        1.1  jonathan 					exf->encrypt(sw->sw_kschedule, blk);
    319        1.1  jonathan 
    320        1.1  jonathan 					/*
    321        1.1  jonathan 					 * Keep encrypted block for XOR'ing
    322        1.1  jonathan 					 * with next block
    323        1.1  jonathan 					 */
    324        1.1  jonathan 					bcopy(blk, iv, blks);
    325        1.1  jonathan 					ivp = iv;
    326        1.1  jonathan 				} else {	/* decrypt */
    327        1.9     perry 					/*
    328        1.1  jonathan 					 * Keep encrypted block for XOR'ing
    329        1.1  jonathan 					 * with next block
    330        1.1  jonathan 					 */
    331        1.1  jonathan 					if (ivp == iv)
    332        1.1  jonathan 						bcopy(blk, piv, blks);
    333        1.1  jonathan 					else
    334        1.1  jonathan 						bcopy(blk, iv, blks);
    335        1.1  jonathan 
    336        1.1  jonathan 					exf->decrypt(sw->sw_kschedule, blk);
    337        1.1  jonathan 
    338        1.1  jonathan 					/* XOR with previous block */
    339        1.1  jonathan 					for (j = 0; j < blks; j++)
    340        1.1  jonathan 						blk[j] ^= ivp[j];
    341        1.1  jonathan 
    342        1.1  jonathan 					if (ivp == iv)
    343        1.1  jonathan 						bcopy(piv, iv, blks);
    344        1.1  jonathan 					else
    345        1.1  jonathan 						ivp = iv;
    346        1.1  jonathan 				}
    347        1.1  jonathan 
    348        1.1  jonathan 				/* Copy back decrypted block */
    349        1.1  jonathan 				cuio_copyback(uio, k, blks, blk);
    350        1.1  jonathan 
    351        1.1  jonathan 				/* Advance pointer */
    352        1.1  jonathan 				iov = cuio_getptr(uio, k + blks, &k);
    353        1.1  jonathan 				if (iov == NULL)
    354        1.1  jonathan 					return EINVAL;
    355        1.1  jonathan 
    356        1.1  jonathan 				i -= blks;
    357        1.1  jonathan 
    358        1.1  jonathan 				/* Could be done... */
    359        1.1  jonathan 				if (i == 0)
    360        1.1  jonathan 					break;
    361        1.1  jonathan 			}
    362        1.1  jonathan 
    363        1.1  jonathan 			/*
    364        1.1  jonathan 			 * Warning: idat may point to garbage here, but
    365        1.1  jonathan 			 * we only use it in the while() loop, only if
    366        1.1  jonathan 			 * there are indeed enough data.
    367        1.1  jonathan 			 */
    368        1.1  jonathan 			idat = (char *)iov->iov_base + k;
    369        1.1  jonathan 
    370        1.1  jonathan 	   		while (iov->iov_len >= k + blks && i > 0) {
    371        1.1  jonathan 				if (crd->crd_flags & CRD_F_ENCRYPT) {
    372        1.1  jonathan 					/* XOR with previous block/IV */
    373        1.1  jonathan 					for (j = 0; j < blks; j++)
    374        1.1  jonathan 						idat[j] ^= ivp[j];
    375        1.1  jonathan 
    376        1.1  jonathan 					exf->encrypt(sw->sw_kschedule, idat);
    377        1.1  jonathan 					ivp = idat;
    378        1.1  jonathan 				} else {	/* decrypt */
    379        1.1  jonathan 					/*
    380        1.1  jonathan 					 * Keep encrypted block to be used
    381        1.1  jonathan 					 * in next block's processing.
    382        1.1  jonathan 					 */
    383        1.1  jonathan 					if (ivp == iv)
    384        1.1  jonathan 						bcopy(idat, piv, blks);
    385        1.1  jonathan 					else
    386        1.1  jonathan 						bcopy(idat, iv, blks);
    387        1.1  jonathan 
    388        1.1  jonathan 					exf->decrypt(sw->sw_kschedule, idat);
    389        1.1  jonathan 
    390        1.1  jonathan 					/* XOR with previous block/IV */
    391        1.1  jonathan 					for (j = 0; j < blks; j++)
    392        1.1  jonathan 						idat[j] ^= ivp[j];
    393        1.1  jonathan 
    394        1.1  jonathan 					if (ivp == iv)
    395        1.1  jonathan 						bcopy(piv, iv, blks);
    396        1.1  jonathan 					else
    397        1.1  jonathan 						ivp = iv;
    398        1.1  jonathan 				}
    399        1.1  jonathan 
    400        1.1  jonathan 				idat += blks;
    401        1.1  jonathan 				k += blks;
    402        1.1  jonathan 				i -= blks;
    403        1.1  jonathan 			}
    404        1.1  jonathan 		}
    405        1.1  jonathan 
    406        1.1  jonathan 		return 0; /* Done with mbuf encryption/decryption */
    407        1.1  jonathan #else  /* !freebsd iov */
    408        1.1  jonathan 		/* Find beginning of data */
    409        1.1  jonathan 		count = crd->crd_skip;
    410        1.1  jonathan 		ind = cuio_getptr(uio, count, &k);
    411        1.1  jonathan 		if (ind == -1)
    412        1.1  jonathan 			return EINVAL;
    413        1.1  jonathan 
    414        1.1  jonathan 		i = crd->crd_len;
    415        1.1  jonathan 
    416        1.1  jonathan 		while (i > 0) {
    417        1.1  jonathan 			/*
    418        1.1  jonathan 			 * If there's insufficient data at the end,
    419        1.1  jonathan 			 * we have to do some copying.
    420        1.1  jonathan 			 */
    421        1.1  jonathan 			if (uio->uio_iov[ind].iov_len < k + blks &&
    422        1.1  jonathan 			    uio->uio_iov[ind].iov_len != k) {
    423        1.1  jonathan 				cuio_copydata(uio, k, blks, blk);
    424        1.1  jonathan 
    425        1.1  jonathan 				/* Actual encryption/decryption */
    426        1.1  jonathan 				if (crd->crd_flags & CRD_F_ENCRYPT) {
    427        1.1  jonathan 					/* XOR with previous block */
    428        1.1  jonathan 					for (j = 0; j < blks; j++)
    429        1.1  jonathan 						blk[j] ^= ivp[j];
    430        1.1  jonathan 
    431        1.1  jonathan 					exf->encrypt(sw->sw_kschedule, blk);
    432        1.1  jonathan 
    433        1.1  jonathan 					/*
    434        1.1  jonathan 					 * Keep encrypted block for XOR'ing
    435        1.1  jonathan 					 * with next block
    436        1.1  jonathan 					 */
    437        1.1  jonathan 					bcopy(blk, iv, blks);
    438        1.1  jonathan 					ivp = iv;
    439        1.1  jonathan 				} else {	/* decrypt */
    440        1.1  jonathan 					/*
    441        1.1  jonathan 					 * Keep encrypted block for XOR'ing
    442        1.1  jonathan 					 * with next block
    443        1.1  jonathan 					 */
    444        1.1  jonathan 					if (ivp == iv)
    445        1.1  jonathan 						bcopy(blk, piv, blks);
    446        1.1  jonathan 					else
    447        1.1  jonathan 						bcopy(blk, iv, blks);
    448        1.1  jonathan 
    449        1.1  jonathan 					exf->decrypt(sw->sw_kschedule, blk);
    450        1.1  jonathan 
    451        1.1  jonathan 					/* XOR with previous block */
    452        1.1  jonathan 					for (j = 0; j < blks; j++)
    453        1.1  jonathan 						blk[j] ^= ivp[j];
    454        1.1  jonathan 
    455        1.1  jonathan 					if (ivp == iv)
    456        1.1  jonathan 						bcopy(piv, iv, blks);
    457        1.1  jonathan 					else
    458        1.1  jonathan 						ivp = iv;
    459        1.1  jonathan 				}
    460        1.1  jonathan 
    461        1.1  jonathan 				/* Copy back decrypted block */
    462        1.1  jonathan 				cuio_copyback(uio, k, blks, blk);
    463        1.1  jonathan 
    464        1.1  jonathan 				count += blks;
    465        1.1  jonathan 
    466        1.1  jonathan 				/* Advance pointer */
    467        1.1  jonathan 				ind = cuio_getptr(uio, count, &k);
    468        1.1  jonathan 				if (ind == -1)
    469        1.1  jonathan 					return (EINVAL);
    470        1.1  jonathan 
    471        1.1  jonathan 				i -= blks;
    472        1.1  jonathan 
    473        1.1  jonathan 				/* Could be done... */
    474        1.1  jonathan 				if (i == 0)
    475        1.1  jonathan 					break;
    476        1.1  jonathan 			}
    477        1.1  jonathan 
    478        1.1  jonathan 			/*
    479        1.1  jonathan 			 * Warning: idat may point to garbage here, but
    480        1.1  jonathan 			 * we only use it in the while() loop, only if
    481        1.1  jonathan 			 * there are indeed enough data.
    482        1.1  jonathan 			 */
    483        1.1  jonathan 			idat = ((caddr_t)uio->uio_iov[ind].iov_base) + k;
    484        1.1  jonathan 
    485        1.1  jonathan 			while (uio->uio_iov[ind].iov_len >= k + blks &&
    486        1.1  jonathan 			    i > 0) {
    487        1.1  jonathan 				if (crd->crd_flags & CRD_F_ENCRYPT) {
    488        1.1  jonathan 					/* XOR with previous block/IV */
    489        1.1  jonathan 					for (j = 0; j < blks; j++)
    490        1.1  jonathan 						idat[j] ^= ivp[j];
    491        1.1  jonathan 
    492        1.1  jonathan 					exf->encrypt(sw->sw_kschedule, idat);
    493        1.1  jonathan 					ivp = idat;
    494        1.1  jonathan 				} else {	/* decrypt */
    495        1.1  jonathan 					/*
    496        1.1  jonathan 					 * Keep encrypted block to be used
    497        1.1  jonathan 					 * in next block's processing.
    498        1.1  jonathan 					 */
    499        1.1  jonathan 					if (ivp == iv)
    500        1.1  jonathan 						bcopy(idat, piv, blks);
    501        1.1  jonathan 					else
    502        1.1  jonathan 						bcopy(idat, iv, blks);
    503        1.1  jonathan 
    504        1.1  jonathan 					exf->decrypt(sw->sw_kschedule, idat);
    505        1.1  jonathan 
    506        1.1  jonathan 					/* XOR with previous block/IV */
    507        1.1  jonathan 					for (j = 0; j < blks; j++)
    508        1.1  jonathan 						idat[j] ^= ivp[j];
    509        1.1  jonathan 
    510        1.1  jonathan 					if (ivp == iv)
    511        1.1  jonathan 						bcopy(piv, iv, blks);
    512        1.1  jonathan 					else
    513        1.1  jonathan 						ivp = iv;
    514        1.1  jonathan 				}
    515        1.1  jonathan 
    516        1.1  jonathan 				idat += blks;
    517        1.1  jonathan 				count += blks;
    518        1.1  jonathan 				k += blks;
    519        1.1  jonathan 				i -= blks;
    520        1.1  jonathan 			}
    521        1.1  jonathan 		}
    522        1.9     perry #endif
    523        1.1  jonathan 		return 0; /* Done with mbuf encryption/decryption */
    524        1.1  jonathan 	}
    525        1.1  jonathan 
    526        1.1  jonathan 	/* Unreachable */
    527        1.1  jonathan 	return EINVAL;
    528        1.1  jonathan }
    529        1.1  jonathan 
    530        1.1  jonathan /*
    531        1.1  jonathan  * Compute keyed-hash authenticator.
    532        1.1  jonathan  */
    533        1.1  jonathan static int
    534        1.1  jonathan swcr_authcompute(struct cryptop *crp, struct cryptodesc *crd,
    535        1.1  jonathan     struct swcr_data *sw, caddr_t buf, int outtype)
    536        1.1  jonathan {
    537        1.1  jonathan 	unsigned char aalg[AALG_MAX_RESULT_LEN];
    538       1.10   thorpej 	const struct swcr_auth_hash *axf;
    539        1.1  jonathan 	union authctx ctx;
    540        1.1  jonathan 	int err;
    541        1.1  jonathan 
    542        1.1  jonathan 	if (sw->sw_ictx == 0)
    543        1.1  jonathan 		return EINVAL;
    544        1.1  jonathan 
    545        1.1  jonathan 	axf = sw->sw_axf;
    546        1.1  jonathan 
    547       1.10   thorpej 	bcopy(sw->sw_ictx, &ctx, axf->auth_hash->ctxsize);
    548        1.1  jonathan 
    549        1.1  jonathan 	switch (outtype) {
    550        1.1  jonathan 	case CRYPTO_BUF_CONTIG:
    551        1.1  jonathan 		axf->Update(&ctx, buf + crd->crd_skip, crd->crd_len);
    552        1.1  jonathan 		break;
    553        1.1  jonathan 	case CRYPTO_BUF_MBUF:
    554        1.1  jonathan 		err = m_apply((struct mbuf *) buf, crd->crd_skip, crd->crd_len,
    555        1.3  jonathan 		    (int (*)(void*, caddr_t, unsigned int)) axf->Update,
    556        1.1  jonathan 		    (caddr_t) &ctx);
    557        1.1  jonathan 		if (err)
    558        1.1  jonathan 			return err;
    559        1.1  jonathan 		break;
    560        1.1  jonathan 	case CRYPTO_BUF_IOV:
    561        1.2  jonathan #ifdef __FreeBSD__
    562        1.2  jonathan 		/*XXX FIXME: handle iov case*/
    563        1.2  jonathan 		return EINVAL;
    564        1.2  jonathan #else
    565        1.2  jonathan 		err = cuio_apply((struct uio *) buf, crd->crd_skip,
    566        1.2  jonathan 		    crd->crd_len,
    567        1.2  jonathan 		    (int (*)(caddr_t, caddr_t, unsigned int)) axf->Update,
    568        1.2  jonathan 		    (caddr_t) &ctx);
    569        1.2  jonathan 		if (err) {
    570        1.2  jonathan 			return err;
    571        1.2  jonathan 		}
    572        1.2  jonathan #endif
    573        1.2  jonathan 		break;
    574        1.1  jonathan 	default:
    575        1.1  jonathan 		return EINVAL;
    576        1.1  jonathan 	}
    577        1.1  jonathan 
    578        1.1  jonathan 	switch (sw->sw_alg) {
    579        1.1  jonathan 	case CRYPTO_MD5_HMAC:
    580        1.1  jonathan 	case CRYPTO_SHA1_HMAC:
    581        1.1  jonathan 	case CRYPTO_SHA2_HMAC:
    582        1.1  jonathan 	case CRYPTO_RIPEMD160_HMAC:
    583        1.1  jonathan 		if (sw->sw_octx == NULL)
    584        1.1  jonathan 			return EINVAL;
    585        1.1  jonathan 
    586        1.1  jonathan 		axf->Final(aalg, &ctx);
    587       1.10   thorpej 		bcopy(sw->sw_octx, &ctx, axf->auth_hash->ctxsize);
    588       1.10   thorpej 		axf->Update(&ctx, aalg, axf->auth_hash->hashsize);
    589        1.1  jonathan 		axf->Final(aalg, &ctx);
    590        1.1  jonathan 		break;
    591        1.1  jonathan 
    592        1.1  jonathan 	case CRYPTO_MD5_KPDK:
    593        1.1  jonathan 	case CRYPTO_SHA1_KPDK:
    594        1.1  jonathan 		if (sw->sw_octx == NULL)
    595        1.1  jonathan 			return EINVAL;
    596        1.1  jonathan 
    597        1.1  jonathan 		axf->Update(&ctx, sw->sw_octx, sw->sw_klen);
    598        1.1  jonathan 		axf->Final(aalg, &ctx);
    599        1.1  jonathan 		break;
    600        1.1  jonathan 
    601        1.1  jonathan 	case CRYPTO_NULL_HMAC:
    602        1.1  jonathan 	case CRYPTO_MD5:
    603        1.1  jonathan 	case CRYPTO_SHA1:
    604        1.1  jonathan 		axf->Final(aalg, &ctx);
    605        1.1  jonathan 		break;
    606        1.1  jonathan 	}
    607        1.1  jonathan 
    608        1.1  jonathan 	/* Inject the authentication data */
    609        1.2  jonathan 	switch (outtype) {
    610        1.2  jonathan 	case CRYPTO_BUF_CONTIG:
    611       1.10   thorpej 		bcopy(aalg, buf + crd->crd_inject, axf->auth_hash->authsize);
    612        1.2  jonathan 		break;
    613        1.2  jonathan 	case CRYPTO_BUF_MBUF:
    614        1.1  jonathan 		m_copyback((struct mbuf *) buf, crd->crd_inject,
    615       1.10   thorpej 		    axf->auth_hash->authsize, aalg);
    616        1.2  jonathan 		break;
    617        1.2  jonathan 	case CRYPTO_BUF_IOV:
    618       1.10   thorpej 		bcopy(aalg, crp->crp_mac, axf->auth_hash->authsize);
    619        1.2  jonathan 		break;
    620        1.2  jonathan 	default:
    621        1.2  jonathan 		return EINVAL;
    622        1.2  jonathan 	}
    623        1.1  jonathan 	return 0;
    624        1.1  jonathan }
    625        1.1  jonathan 
    626        1.1  jonathan /*
    627        1.1  jonathan  * Apply a compression/decompression algorithm
    628        1.1  jonathan  */
    629        1.1  jonathan static int
    630        1.1  jonathan swcr_compdec(struct cryptodesc *crd, struct swcr_data *sw,
    631        1.1  jonathan     caddr_t buf, int outtype)
    632        1.1  jonathan {
    633        1.1  jonathan 	u_int8_t *data, *out;
    634       1.10   thorpej 	const struct swcr_comp_algo *cxf;
    635        1.1  jonathan 	int adj;
    636        1.1  jonathan 	u_int32_t result;
    637        1.1  jonathan 
    638        1.1  jonathan 	cxf = sw->sw_cxf;
    639        1.1  jonathan 
    640        1.1  jonathan 	/* We must handle the whole buffer of data in one time
    641        1.1  jonathan 	 * then if there is not all the data in the mbuf, we must
    642        1.1  jonathan 	 * copy in a buffer.
    643        1.1  jonathan 	 */
    644        1.1  jonathan 
    645  1.11.10.1      elad 	data = malloc(crd->crd_len, M_CRYPTO_DATA, M_NOWAIT);
    646        1.1  jonathan 	if (data == NULL)
    647        1.1  jonathan 		return (EINVAL);
    648        1.1  jonathan 	COPYDATA(outtype, buf, crd->crd_skip, crd->crd_len, data);
    649        1.1  jonathan 
    650        1.1  jonathan 	if (crd->crd_flags & CRD_F_COMP)
    651        1.1  jonathan 		result = cxf->compress(data, crd->crd_len, &out);
    652        1.1  jonathan 	else
    653        1.1  jonathan 		result = cxf->decompress(data, crd->crd_len, &out);
    654        1.1  jonathan 
    655        1.1  jonathan 	FREE(data, M_CRYPTO_DATA);
    656        1.1  jonathan 	if (result == 0)
    657        1.1  jonathan 		return EINVAL;
    658        1.1  jonathan 
    659        1.1  jonathan 	/* Copy back the (de)compressed data. m_copyback is
    660        1.1  jonathan 	 * extending the mbuf as necessary.
    661        1.1  jonathan 	 */
    662        1.1  jonathan 	sw->sw_size = result;
    663        1.1  jonathan 	/* Check the compressed size when doing compression */
    664        1.1  jonathan 	if (crd->crd_flags & CRD_F_COMP) {
    665        1.1  jonathan 		if (result > crd->crd_len) {
    666        1.1  jonathan 			/* Compression was useless, we lost time */
    667        1.1  jonathan 			FREE(out, M_CRYPTO_DATA);
    668        1.1  jonathan 			return 0;
    669        1.1  jonathan 		}
    670        1.1  jonathan 	}
    671        1.1  jonathan 
    672        1.1  jonathan 	COPYBACK(outtype, buf, crd->crd_skip, result, out);
    673        1.1  jonathan 	if (result < crd->crd_len) {
    674        1.1  jonathan 		adj = result - crd->crd_len;
    675        1.1  jonathan 		if (outtype == CRYPTO_BUF_MBUF) {
    676        1.1  jonathan 			adj = result - crd->crd_len;
    677        1.1  jonathan 			m_adj((struct mbuf *)buf, adj);
    678        1.1  jonathan 		} else {
    679        1.1  jonathan 			struct uio *uio = (struct uio *)buf;
    680        1.1  jonathan 			int ind;
    681        1.1  jonathan 
    682        1.1  jonathan 			adj = crd->crd_len - result;
    683        1.1  jonathan 			ind = uio->uio_iovcnt - 1;
    684        1.1  jonathan 
    685        1.1  jonathan 			while (adj > 0 && ind >= 0) {
    686        1.1  jonathan 				if (adj < uio->uio_iov[ind].iov_len) {
    687        1.1  jonathan 					uio->uio_iov[ind].iov_len -= adj;
    688        1.1  jonathan 					break;
    689        1.1  jonathan 				}
    690        1.1  jonathan 
    691        1.1  jonathan 				adj -= uio->uio_iov[ind].iov_len;
    692        1.1  jonathan 				uio->uio_iov[ind].iov_len = 0;
    693        1.1  jonathan 				ind--;
    694        1.1  jonathan 				uio->uio_iovcnt--;
    695        1.1  jonathan 			}
    696        1.1  jonathan 		}
    697        1.1  jonathan 	}
    698        1.1  jonathan 	FREE(out, M_CRYPTO_DATA);
    699        1.1  jonathan 	return 0;
    700        1.1  jonathan }
    701        1.1  jonathan 
    702        1.1  jonathan /*
    703        1.1  jonathan  * Generate a new software session.
    704        1.1  jonathan  */
    705        1.1  jonathan static int
    706        1.1  jonathan swcr_newsession(void *arg, u_int32_t *sid, struct cryptoini *cri)
    707        1.1  jonathan {
    708        1.1  jonathan 	struct swcr_data **swd;
    709       1.10   thorpej 	const struct swcr_auth_hash *axf;
    710       1.10   thorpej 	const struct swcr_enc_xform *txf;
    711       1.10   thorpej 	const struct swcr_comp_algo *cxf;
    712        1.1  jonathan 	u_int32_t i;
    713        1.1  jonathan 	int k, error;
    714        1.1  jonathan 
    715        1.1  jonathan 	if (sid == NULL || cri == NULL)
    716        1.1  jonathan 		return EINVAL;
    717        1.1  jonathan 
    718        1.1  jonathan 	if (swcr_sessions) {
    719        1.1  jonathan 		for (i = 1; i < swcr_sesnum; i++)
    720        1.1  jonathan 			if (swcr_sessions[i] == NULL)
    721        1.1  jonathan 				break;
    722        1.1  jonathan 	} else
    723        1.1  jonathan 		i = 1;		/* NB: to silence compiler warning */
    724        1.1  jonathan 
    725        1.1  jonathan 	if (swcr_sessions == NULL || i == swcr_sesnum) {
    726        1.1  jonathan 		if (swcr_sessions == NULL) {
    727        1.1  jonathan 			i = 1; /* We leave swcr_sessions[0] empty */
    728        1.1  jonathan 			swcr_sesnum = CRYPTO_SW_SESSIONS;
    729        1.1  jonathan 		} else
    730        1.1  jonathan 			swcr_sesnum *= 2;
    731        1.1  jonathan 
    732        1.1  jonathan 		swd = malloc(swcr_sesnum * sizeof(struct swcr_data *),
    733        1.1  jonathan 		    M_CRYPTO_DATA, M_NOWAIT);
    734        1.1  jonathan 		if (swd == NULL) {
    735        1.1  jonathan 			/* Reset session number */
    736        1.1  jonathan 			if (swcr_sesnum == CRYPTO_SW_SESSIONS)
    737        1.1  jonathan 				swcr_sesnum = 0;
    738        1.1  jonathan 			else
    739        1.1  jonathan 				swcr_sesnum /= 2;
    740        1.1  jonathan 			return ENOBUFS;
    741        1.1  jonathan 		}
    742        1.1  jonathan 
    743        1.1  jonathan 		bzero(swd, swcr_sesnum * sizeof(struct swcr_data *));
    744        1.1  jonathan 
    745        1.1  jonathan 		/* Copy existing sessions */
    746        1.1  jonathan 		if (swcr_sessions) {
    747        1.1  jonathan 			bcopy(swcr_sessions, swd,
    748        1.1  jonathan 			    (swcr_sesnum / 2) * sizeof(struct swcr_data *));
    749        1.1  jonathan 			free(swcr_sessions, M_CRYPTO_DATA);
    750        1.1  jonathan 		}
    751        1.1  jonathan 
    752        1.1  jonathan 		swcr_sessions = swd;
    753        1.1  jonathan 	}
    754        1.1  jonathan 
    755        1.1  jonathan 	swd = &swcr_sessions[i];
    756        1.1  jonathan 	*sid = i;
    757        1.1  jonathan 
    758        1.1  jonathan 	while (cri) {
    759  1.11.10.1      elad 		*swd = malloc(sizeof **swd, M_CRYPTO_DATA, M_NOWAIT);
    760        1.1  jonathan 		if (*swd == NULL) {
    761        1.1  jonathan 			swcr_freesession(NULL, i);
    762        1.1  jonathan 			return ENOBUFS;
    763        1.1  jonathan 		}
    764        1.1  jonathan 		bzero(*swd, sizeof(struct swcr_data));
    765        1.1  jonathan 
    766        1.1  jonathan 		switch (cri->cri_alg) {
    767        1.1  jonathan 		case CRYPTO_DES_CBC:
    768       1.10   thorpej 			txf = &swcr_enc_xform_des;
    769        1.1  jonathan 			goto enccommon;
    770        1.1  jonathan 		case CRYPTO_3DES_CBC:
    771       1.10   thorpej 			txf = &swcr_enc_xform_3des;
    772        1.1  jonathan 			goto enccommon;
    773        1.1  jonathan 		case CRYPTO_BLF_CBC:
    774       1.10   thorpej 			txf = &swcr_enc_xform_blf;
    775        1.1  jonathan 			goto enccommon;
    776        1.1  jonathan 		case CRYPTO_CAST_CBC:
    777       1.10   thorpej 			txf = &swcr_enc_xform_cast5;
    778        1.1  jonathan 			goto enccommon;
    779        1.1  jonathan 		case CRYPTO_SKIPJACK_CBC:
    780       1.10   thorpej 			txf = &swcr_enc_xform_skipjack;
    781        1.1  jonathan 			goto enccommon;
    782        1.1  jonathan 		case CRYPTO_RIJNDAEL128_CBC:
    783       1.10   thorpej 			txf = &swcr_enc_xform_rijndael128;
    784        1.1  jonathan 			goto enccommon;
    785        1.1  jonathan 		case CRYPTO_NULL_CBC:
    786       1.10   thorpej 			txf = &swcr_enc_xform_null;
    787        1.1  jonathan 			goto enccommon;
    788        1.1  jonathan 		enccommon:
    789        1.1  jonathan 			error = txf->setkey(&((*swd)->sw_kschedule),
    790        1.1  jonathan 					cri->cri_key, cri->cri_klen / 8);
    791        1.1  jonathan 			if (error) {
    792        1.1  jonathan 				swcr_freesession(NULL, i);
    793        1.1  jonathan 				return error;
    794        1.1  jonathan 			}
    795        1.1  jonathan 			(*swd)->sw_exf = txf;
    796        1.1  jonathan 			break;
    797        1.1  jonathan 
    798        1.1  jonathan 		case CRYPTO_MD5_HMAC:
    799       1.10   thorpej 			axf = &swcr_auth_hash_hmac_md5_96;
    800        1.1  jonathan 			goto authcommon;
    801        1.1  jonathan 		case CRYPTO_SHA1_HMAC:
    802       1.10   thorpej 			axf = &swcr_auth_hash_hmac_sha1_96;
    803        1.1  jonathan 			goto authcommon;
    804        1.1  jonathan 		case CRYPTO_SHA2_HMAC:
    805        1.1  jonathan 			if (cri->cri_klen == 256)
    806       1.10   thorpej 				axf = &swcr_auth_hash_hmac_sha2_256;
    807        1.1  jonathan 			else if (cri->cri_klen == 384)
    808       1.10   thorpej 				axf = &swcr_auth_hash_hmac_sha2_384;
    809        1.1  jonathan 			else if (cri->cri_klen == 512)
    810       1.10   thorpej 				axf = &swcr_auth_hash_hmac_sha2_512;
    811        1.1  jonathan 			else {
    812        1.1  jonathan 				swcr_freesession(NULL, i);
    813        1.1  jonathan 				return EINVAL;
    814        1.1  jonathan 			}
    815        1.1  jonathan 			goto authcommon;
    816        1.1  jonathan 		case CRYPTO_NULL_HMAC:
    817       1.10   thorpej 			axf = &swcr_auth_hash_null;
    818        1.1  jonathan 			goto authcommon;
    819        1.1  jonathan 		case CRYPTO_RIPEMD160_HMAC:
    820       1.10   thorpej 			axf = &swcr_auth_hash_hmac_ripemd_160_96;
    821        1.1  jonathan 		authcommon:
    822       1.10   thorpej 			(*swd)->sw_ictx = malloc(axf->auth_hash->ctxsize,
    823       1.10   thorpej 			    M_CRYPTO_DATA, M_NOWAIT);
    824        1.1  jonathan 			if ((*swd)->sw_ictx == NULL) {
    825        1.1  jonathan 				swcr_freesession(NULL, i);
    826        1.1  jonathan 				return ENOBUFS;
    827        1.1  jonathan 			}
    828        1.1  jonathan 
    829       1.10   thorpej 			(*swd)->sw_octx = malloc(axf->auth_hash->ctxsize,
    830       1.10   thorpej 			    M_CRYPTO_DATA, M_NOWAIT);
    831        1.1  jonathan 			if ((*swd)->sw_octx == NULL) {
    832        1.1  jonathan 				swcr_freesession(NULL, i);
    833        1.1  jonathan 				return ENOBUFS;
    834        1.1  jonathan 			}
    835        1.1  jonathan 
    836        1.1  jonathan 			for (k = 0; k < cri->cri_klen / 8; k++)
    837        1.1  jonathan 				cri->cri_key[k] ^= HMAC_IPAD_VAL;
    838        1.1  jonathan 
    839        1.1  jonathan 			axf->Init((*swd)->sw_ictx);
    840        1.1  jonathan 			axf->Update((*swd)->sw_ictx, cri->cri_key,
    841        1.1  jonathan 			    cri->cri_klen / 8);
    842        1.1  jonathan 			axf->Update((*swd)->sw_ictx, hmac_ipad_buffer,
    843        1.1  jonathan 			    HMAC_BLOCK_LEN - (cri->cri_klen / 8));
    844        1.1  jonathan 
    845        1.1  jonathan 			for (k = 0; k < cri->cri_klen / 8; k++)
    846        1.1  jonathan 				cri->cri_key[k] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
    847        1.1  jonathan 
    848        1.1  jonathan 			axf->Init((*swd)->sw_octx);
    849        1.1  jonathan 			axf->Update((*swd)->sw_octx, cri->cri_key,
    850        1.1  jonathan 			    cri->cri_klen / 8);
    851        1.1  jonathan 			axf->Update((*swd)->sw_octx, hmac_opad_buffer,
    852        1.1  jonathan 			    HMAC_BLOCK_LEN - (cri->cri_klen / 8));
    853        1.1  jonathan 
    854        1.1  jonathan 			for (k = 0; k < cri->cri_klen / 8; k++)
    855        1.1  jonathan 				cri->cri_key[k] ^= HMAC_OPAD_VAL;
    856        1.1  jonathan 			(*swd)->sw_axf = axf;
    857        1.1  jonathan 			break;
    858        1.1  jonathan 
    859        1.1  jonathan 		case CRYPTO_MD5_KPDK:
    860       1.10   thorpej 			axf = &swcr_auth_hash_key_md5;
    861        1.1  jonathan 			goto auth2common;
    862        1.1  jonathan 
    863        1.1  jonathan 		case CRYPTO_SHA1_KPDK:
    864       1.10   thorpej 			axf = &swcr_auth_hash_key_sha1;
    865        1.1  jonathan 		auth2common:
    866       1.10   thorpej 			(*swd)->sw_ictx = malloc(axf->auth_hash->ctxsize,
    867       1.10   thorpej 			    M_CRYPTO_DATA, M_NOWAIT);
    868        1.1  jonathan 			if ((*swd)->sw_ictx == NULL) {
    869        1.1  jonathan 				swcr_freesession(NULL, i);
    870        1.1  jonathan 				return ENOBUFS;
    871        1.1  jonathan 			}
    872        1.1  jonathan 
    873        1.1  jonathan 			/* Store the key so we can "append" it to the payload */
    874        1.1  jonathan 			(*swd)->sw_octx = malloc(cri->cri_klen / 8, M_CRYPTO_DATA,
    875        1.1  jonathan 			    M_NOWAIT);
    876        1.1  jonathan 			if ((*swd)->sw_octx == NULL) {
    877        1.1  jonathan 				swcr_freesession(NULL, i);
    878        1.1  jonathan 				return ENOBUFS;
    879        1.1  jonathan 			}
    880        1.1  jonathan 
    881        1.1  jonathan 			(*swd)->sw_klen = cri->cri_klen / 8;
    882        1.1  jonathan 			bcopy(cri->cri_key, (*swd)->sw_octx, cri->cri_klen / 8);
    883        1.1  jonathan 			axf->Init((*swd)->sw_ictx);
    884        1.1  jonathan 			axf->Update((*swd)->sw_ictx, cri->cri_key,
    885        1.1  jonathan 			    cri->cri_klen / 8);
    886        1.1  jonathan 			axf->Final(NULL, (*swd)->sw_ictx);
    887        1.1  jonathan 			(*swd)->sw_axf = axf;
    888        1.1  jonathan 			break;
    889        1.1  jonathan 
    890        1.1  jonathan 		case CRYPTO_MD5:
    891       1.10   thorpej 			axf = &swcr_auth_hash_md5;
    892        1.1  jonathan 			goto auth3common;
    893        1.1  jonathan 
    894        1.1  jonathan 		case CRYPTO_SHA1:
    895       1.10   thorpej 			axf = &swcr_auth_hash_sha1;
    896        1.1  jonathan 		auth3common:
    897       1.10   thorpej 			(*swd)->sw_ictx = malloc(axf->auth_hash->ctxsize,
    898       1.10   thorpej 			    M_CRYPTO_DATA, M_NOWAIT);
    899        1.1  jonathan 			if ((*swd)->sw_ictx == NULL) {
    900        1.1  jonathan 				swcr_freesession(NULL, i);
    901        1.1  jonathan 				return ENOBUFS;
    902        1.1  jonathan 			}
    903        1.1  jonathan 
    904        1.1  jonathan 			axf->Init((*swd)->sw_ictx);
    905        1.1  jonathan 			(*swd)->sw_axf = axf;
    906        1.1  jonathan 			break;
    907        1.1  jonathan 
    908        1.1  jonathan 		case CRYPTO_DEFLATE_COMP:
    909       1.10   thorpej 			cxf = &swcr_comp_algo_deflate;
    910        1.1  jonathan 			(*swd)->sw_cxf = cxf;
    911        1.1  jonathan 			break;
    912        1.1  jonathan 		default:
    913        1.1  jonathan 			swcr_freesession(NULL, i);
    914        1.1  jonathan 			return EINVAL;
    915        1.1  jonathan 		}
    916        1.1  jonathan 
    917        1.1  jonathan 		(*swd)->sw_alg = cri->cri_alg;
    918        1.1  jonathan 		cri = cri->cri_next;
    919        1.1  jonathan 		swd = &((*swd)->sw_next);
    920        1.1  jonathan 	}
    921        1.1  jonathan 	return 0;
    922        1.1  jonathan }
    923        1.1  jonathan 
    924        1.1  jonathan /*
    925        1.1  jonathan  * Free a session.
    926        1.1  jonathan  */
    927        1.1  jonathan static int
    928        1.1  jonathan swcr_freesession(void *arg, u_int64_t tid)
    929        1.1  jonathan {
    930        1.1  jonathan 	struct swcr_data *swd;
    931       1.10   thorpej 	const struct swcr_enc_xform *txf;
    932       1.10   thorpej 	const struct swcr_auth_hash *axf;
    933       1.10   thorpej 	const struct swcr_comp_algo *cxf;
    934        1.1  jonathan 	u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
    935        1.1  jonathan 
    936        1.1  jonathan 	if (sid > swcr_sesnum || swcr_sessions == NULL ||
    937        1.1  jonathan 	    swcr_sessions[sid] == NULL)
    938        1.1  jonathan 		return EINVAL;
    939        1.1  jonathan 
    940        1.1  jonathan 	/* Silently accept and return */
    941        1.1  jonathan 	if (sid == 0)
    942        1.1  jonathan 		return 0;
    943        1.1  jonathan 
    944        1.1  jonathan 	while ((swd = swcr_sessions[sid]) != NULL) {
    945        1.1  jonathan 		swcr_sessions[sid] = swd->sw_next;
    946        1.1  jonathan 
    947        1.1  jonathan 		switch (swd->sw_alg) {
    948        1.1  jonathan 		case CRYPTO_DES_CBC:
    949        1.1  jonathan 		case CRYPTO_3DES_CBC:
    950        1.1  jonathan 		case CRYPTO_BLF_CBC:
    951        1.1  jonathan 		case CRYPTO_CAST_CBC:
    952        1.1  jonathan 		case CRYPTO_SKIPJACK_CBC:
    953        1.1  jonathan 		case CRYPTO_RIJNDAEL128_CBC:
    954        1.1  jonathan 		case CRYPTO_NULL_CBC:
    955        1.1  jonathan 			txf = swd->sw_exf;
    956        1.1  jonathan 
    957        1.1  jonathan 			if (swd->sw_kschedule)
    958        1.1  jonathan 				txf->zerokey(&(swd->sw_kschedule));
    959        1.1  jonathan 			break;
    960        1.1  jonathan 
    961        1.1  jonathan 		case CRYPTO_MD5_HMAC:
    962        1.1  jonathan 		case CRYPTO_SHA1_HMAC:
    963        1.1  jonathan 		case CRYPTO_SHA2_HMAC:
    964        1.1  jonathan 		case CRYPTO_RIPEMD160_HMAC:
    965        1.1  jonathan 		case CRYPTO_NULL_HMAC:
    966        1.1  jonathan 			axf = swd->sw_axf;
    967        1.1  jonathan 
    968        1.1  jonathan 			if (swd->sw_ictx) {
    969       1.10   thorpej 				bzero(swd->sw_ictx, axf->auth_hash->ctxsize);
    970        1.1  jonathan 				free(swd->sw_ictx, M_CRYPTO_DATA);
    971        1.1  jonathan 			}
    972        1.1  jonathan 			if (swd->sw_octx) {
    973       1.10   thorpej 				bzero(swd->sw_octx, axf->auth_hash->ctxsize);
    974        1.1  jonathan 				free(swd->sw_octx, M_CRYPTO_DATA);
    975        1.1  jonathan 			}
    976        1.1  jonathan 			break;
    977        1.1  jonathan 
    978        1.1  jonathan 		case CRYPTO_MD5_KPDK:
    979        1.1  jonathan 		case CRYPTO_SHA1_KPDK:
    980        1.1  jonathan 			axf = swd->sw_axf;
    981        1.1  jonathan 
    982        1.1  jonathan 			if (swd->sw_ictx) {
    983       1.10   thorpej 				bzero(swd->sw_ictx, axf->auth_hash->ctxsize);
    984        1.1  jonathan 				free(swd->sw_ictx, M_CRYPTO_DATA);
    985        1.1  jonathan 			}
    986        1.1  jonathan 			if (swd->sw_octx) {
    987        1.1  jonathan 				bzero(swd->sw_octx, swd->sw_klen);
    988        1.1  jonathan 				free(swd->sw_octx, M_CRYPTO_DATA);
    989        1.1  jonathan 			}
    990        1.1  jonathan 			break;
    991        1.1  jonathan 
    992        1.1  jonathan 		case CRYPTO_MD5:
    993        1.1  jonathan 		case CRYPTO_SHA1:
    994        1.1  jonathan 			axf = swd->sw_axf;
    995        1.1  jonathan 
    996        1.1  jonathan 			if (swd->sw_ictx)
    997        1.1  jonathan 				free(swd->sw_ictx, M_CRYPTO_DATA);
    998        1.1  jonathan 			break;
    999        1.1  jonathan 
   1000        1.1  jonathan 		case CRYPTO_DEFLATE_COMP:
   1001        1.1  jonathan 			cxf = swd->sw_cxf;
   1002        1.1  jonathan 			break;
   1003        1.1  jonathan 		}
   1004        1.1  jonathan 
   1005        1.1  jonathan 		FREE(swd, M_CRYPTO_DATA);
   1006        1.1  jonathan 	}
   1007        1.1  jonathan 	return 0;
   1008        1.1  jonathan }
   1009        1.1  jonathan 
   1010        1.1  jonathan /*
   1011        1.1  jonathan  * Process a software request.
   1012        1.1  jonathan  */
   1013        1.1  jonathan static int
   1014        1.1  jonathan swcr_process(void *arg, struct cryptop *crp, int hint)
   1015        1.1  jonathan {
   1016        1.1  jonathan 	struct cryptodesc *crd;
   1017        1.1  jonathan 	struct swcr_data *sw;
   1018        1.1  jonathan 	u_int32_t lid;
   1019        1.1  jonathan 	int type;
   1020        1.1  jonathan 
   1021        1.1  jonathan 	/* Sanity check */
   1022        1.1  jonathan 	if (crp == NULL)
   1023        1.1  jonathan 		return EINVAL;
   1024        1.1  jonathan 
   1025        1.1  jonathan 	if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
   1026        1.1  jonathan 		crp->crp_etype = EINVAL;
   1027        1.1  jonathan 		goto done;
   1028        1.1  jonathan 	}
   1029        1.1  jonathan 
   1030        1.1  jonathan 	lid = crp->crp_sid & 0xffffffff;
   1031        1.1  jonathan 	if (lid >= swcr_sesnum || lid == 0 || swcr_sessions[lid] == NULL) {
   1032        1.1  jonathan 		crp->crp_etype = ENOENT;
   1033        1.1  jonathan 		goto done;
   1034        1.1  jonathan 	}
   1035        1.1  jonathan 
   1036        1.1  jonathan 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
   1037        1.1  jonathan 		type = CRYPTO_BUF_MBUF;
   1038        1.1  jonathan 	} else if (crp->crp_flags & CRYPTO_F_IOV) {
   1039        1.1  jonathan 		type = CRYPTO_BUF_IOV;
   1040        1.1  jonathan 	} else {
   1041        1.1  jonathan 		type = CRYPTO_BUF_CONTIG;
   1042        1.1  jonathan 	}
   1043        1.1  jonathan 
   1044        1.1  jonathan 	/* Go through crypto descriptors, processing as we go */
   1045        1.1  jonathan 	for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
   1046        1.1  jonathan 		/*
   1047        1.1  jonathan 		 * Find the crypto context.
   1048        1.1  jonathan 		 *
   1049        1.1  jonathan 		 * XXX Note that the logic here prevents us from having
   1050        1.1  jonathan 		 * XXX the same algorithm multiple times in a session
   1051        1.1  jonathan 		 * XXX (or rather, we can but it won't give us the right
   1052        1.1  jonathan 		 * XXX results). To do that, we'd need some way of differentiating
   1053        1.1  jonathan 		 * XXX between the various instances of an algorithm (so we can
   1054        1.1  jonathan 		 * XXX locate the correct crypto context).
   1055        1.1  jonathan 		 */
   1056        1.1  jonathan 		for (sw = swcr_sessions[lid];
   1057        1.1  jonathan 		    sw && sw->sw_alg != crd->crd_alg;
   1058        1.1  jonathan 		    sw = sw->sw_next)
   1059        1.1  jonathan 			;
   1060        1.1  jonathan 
   1061        1.1  jonathan 		/* No such context ? */
   1062        1.1  jonathan 		if (sw == NULL) {
   1063        1.1  jonathan 			crp->crp_etype = EINVAL;
   1064        1.1  jonathan 			goto done;
   1065        1.1  jonathan 		}
   1066        1.1  jonathan 
   1067        1.1  jonathan 		switch (sw->sw_alg) {
   1068        1.1  jonathan 		case CRYPTO_DES_CBC:
   1069        1.1  jonathan 		case CRYPTO_3DES_CBC:
   1070        1.1  jonathan 		case CRYPTO_BLF_CBC:
   1071        1.1  jonathan 		case CRYPTO_CAST_CBC:
   1072        1.1  jonathan 		case CRYPTO_SKIPJACK_CBC:
   1073        1.1  jonathan 		case CRYPTO_RIJNDAEL128_CBC:
   1074        1.1  jonathan 			if ((crp->crp_etype = swcr_encdec(crd, sw,
   1075        1.1  jonathan 			    crp->crp_buf, type)) != 0)
   1076        1.1  jonathan 				goto done;
   1077        1.1  jonathan 			break;
   1078        1.1  jonathan 		case CRYPTO_NULL_CBC:
   1079        1.1  jonathan 			crp->crp_etype = 0;
   1080        1.1  jonathan 			break;
   1081        1.1  jonathan 		case CRYPTO_MD5_HMAC:
   1082        1.1  jonathan 		case CRYPTO_SHA1_HMAC:
   1083        1.1  jonathan 		case CRYPTO_SHA2_HMAC:
   1084        1.1  jonathan 		case CRYPTO_RIPEMD160_HMAC:
   1085        1.1  jonathan 		case CRYPTO_NULL_HMAC:
   1086        1.1  jonathan 		case CRYPTO_MD5_KPDK:
   1087        1.1  jonathan 		case CRYPTO_SHA1_KPDK:
   1088        1.1  jonathan 		case CRYPTO_MD5:
   1089        1.1  jonathan 		case CRYPTO_SHA1:
   1090        1.1  jonathan 			if ((crp->crp_etype = swcr_authcompute(crp, crd, sw,
   1091        1.1  jonathan 			    crp->crp_buf, type)) != 0)
   1092        1.1  jonathan 				goto done;
   1093        1.1  jonathan 			break;
   1094        1.1  jonathan 
   1095        1.1  jonathan 		case CRYPTO_DEFLATE_COMP:
   1096        1.9     perry 			if ((crp->crp_etype = swcr_compdec(crd, sw,
   1097        1.1  jonathan 			    crp->crp_buf, type)) != 0)
   1098        1.1  jonathan 				goto done;
   1099        1.1  jonathan 			else
   1100        1.1  jonathan 				crp->crp_olen = (int)sw->sw_size;
   1101        1.1  jonathan 			break;
   1102        1.1  jonathan 
   1103        1.1  jonathan 		default:
   1104        1.1  jonathan 			/* Unknown/unsupported algorithm */
   1105        1.1  jonathan 			crp->crp_etype = EINVAL;
   1106        1.1  jonathan 			goto done;
   1107        1.1  jonathan 		}
   1108        1.1  jonathan 	}
   1109        1.1  jonathan 
   1110        1.1  jonathan done:
   1111        1.1  jonathan 	crypto_done(crp);
   1112        1.1  jonathan 	return 0;
   1113        1.1  jonathan }
   1114        1.1  jonathan 
   1115       1.10   thorpej static void
   1116        1.1  jonathan swcr_init(void)
   1117        1.1  jonathan {
   1118        1.1  jonathan 	swcr_id = crypto_get_driverid(CRYPTOCAP_F_SOFTWARE);
   1119        1.1  jonathan 	if (swcr_id < 0) {
   1120        1.1  jonathan 		/* This should never happen */
   1121        1.1  jonathan 		panic("Software crypto device cannot initialize!");
   1122        1.1  jonathan 	}
   1123        1.1  jonathan 
   1124        1.1  jonathan 	crypto_register(swcr_id, CRYPTO_DES_CBC,
   1125        1.1  jonathan 	    0, 0, swcr_newsession, swcr_freesession, swcr_process, NULL);
   1126        1.1  jonathan #define	REGISTER(alg) \
   1127        1.1  jonathan 	crypto_register(swcr_id, alg, 0, 0, NULL, NULL, NULL, NULL)
   1128        1.1  jonathan 
   1129        1.1  jonathan 	REGISTER(CRYPTO_3DES_CBC);
   1130        1.1  jonathan 	REGISTER(CRYPTO_BLF_CBC);
   1131        1.1  jonathan 	REGISTER(CRYPTO_CAST_CBC);
   1132        1.1  jonathan 	REGISTER(CRYPTO_SKIPJACK_CBC);
   1133        1.1  jonathan 	REGISTER(CRYPTO_NULL_CBC);
   1134        1.1  jonathan 	REGISTER(CRYPTO_MD5_HMAC);
   1135        1.1  jonathan 	REGISTER(CRYPTO_SHA1_HMAC);
   1136        1.1  jonathan 	REGISTER(CRYPTO_SHA2_HMAC);
   1137        1.1  jonathan 	REGISTER(CRYPTO_RIPEMD160_HMAC);
   1138        1.1  jonathan 	REGISTER(CRYPTO_NULL_HMAC);
   1139        1.1  jonathan 	REGISTER(CRYPTO_MD5_KPDK);
   1140        1.1  jonathan 	REGISTER(CRYPTO_SHA1_KPDK);
   1141        1.1  jonathan 	REGISTER(CRYPTO_MD5);
   1142        1.1  jonathan 	REGISTER(CRYPTO_SHA1);
   1143        1.1  jonathan 	REGISTER(CRYPTO_RIJNDAEL128_CBC);
   1144        1.1  jonathan 	REGISTER(CRYPTO_DEFLATE_COMP);
   1145        1.1  jonathan #undef REGISTER
   1146        1.1  jonathan }
   1147        1.1  jonathan 
   1148        1.1  jonathan #ifdef __FreeBSD__
   1149        1.1  jonathan SYSINIT(cryptosoft_init, SI_SUB_PSEUDO, SI_ORDER_ANY, swcr_init, NULL)
   1150        1.1  jonathan #endif
   1151       1.10   thorpej 
   1152       1.10   thorpej #ifdef __NetBSD__
   1153       1.10   thorpej /*
   1154       1.10   thorpej  * Pseudo-device init routine for software crypto.
   1155       1.10   thorpej  */
   1156       1.11   thorpej void	swcryptoattach(int);
   1157       1.10   thorpej 
   1158       1.10   thorpej void
   1159       1.11   thorpej swcryptoattach(int num)
   1160       1.10   thorpej {
   1161       1.10   thorpej 
   1162       1.10   thorpej 	swcr_init();
   1163       1.10   thorpej }
   1164       1.10   thorpej #endif /* __NetBSD__ */
   1165