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