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