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