xref: /titanic_44/usr/src/uts/common/io/cryptmod.c (revision 8793b36b40d14ad0a0fecc97738dc118a928f46c)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  *
5  * STREAMS Crypto Module
6  *
7  * This module is used to facilitate Kerberos encryption
8  * operations for the telnet daemon and rlogin daemon.
9  * Because the Solaris telnet and rlogin daemons run mostly
10  * in-kernel via 'telmod' and 'rlmod', this module must be
11  * pushed on the STREAM *below* telmod or rlmod.
12  *
13  * Parts of the 3DES key derivation code are covered by the
14  * following copyright.
15  *
16  * Copyright (C) 1998 by the FundsXpress, INC.
17  *
18  * All rights reserved.
19  *
20  * Export of this software from the United States of America may require
21  * a specific license from the United States Government.  It is the
22  * responsibility of any person or organization contemplating export to
23  * obtain such a license before exporting.
24  *
25  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
26  * distribute this software and its documentation for any purpose and
27  * without fee is hereby granted, provided that the above copyright
28  * notice appear in all copies and that both that copyright notice and
29  * this permission notice appear in supporting documentation, and that
30  * the name of FundsXpress. not be used in advertising or publicity pertaining
31  * to distribution of the software without specific, written prior
32  * permission.  FundsXpress makes no representations about the suitability of
33  * this software for any purpose.  It is provided "as is" without express
34  * or implied warranty.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
37  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
38  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39  */
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <sys/types.h>
43 #include <sys/sysmacros.h>
44 #include <sys/errno.h>
45 #include <sys/debug.h>
46 #include <sys/time.h>
47 #include <sys/stropts.h>
48 #include <sys/stream.h>
49 #include <sys/strsubr.h>
50 #include <sys/strlog.h>
51 #include <sys/cmn_err.h>
52 #include <sys/conf.h>
53 #include <sys/sunddi.h>
54 #include <sys/kmem.h>
55 #include <sys/strsun.h>
56 #include <sys/random.h>
57 #include <sys/types.h>
58 #include <sys/byteorder.h>
59 #include <sys/cryptmod.h>
60 #include <sys/crc32.h>
61 #include <sys/policy.h>
62 
63 #include <sys/crypto/api.h>
64 
65 #include <sys/strft.h>
66 /*
67  * Function prototypes.
68  */
69 static	int	cryptmodopen(queue_t *, dev_t *, int, int, cred_t *);
70 static  void	cryptmodrput(queue_t *, mblk_t *);
71 static  void	cryptmodwput(queue_t *, mblk_t *);
72 static	int	cryptmodclose(queue_t *);
73 static	int	cryptmodwsrv(queue_t *);
74 static	int	cryptmodrsrv(queue_t *);
75 
76 static mblk_t *do_encrypt(queue_t *q, mblk_t *mp);
77 static mblk_t *do_decrypt(queue_t *q, mblk_t *mp);
78 
79 #define	CRYPTMOD_ID 5150
80 
81 #define	CFB_BLKSZ 8
82 
83 #define	K5CLENGTH 5
84 
85 static struct module_info	cryptmod_minfo = {
86 	CRYPTMOD_ID,	/* mi_idnum */
87 	"cryptmod",	/* mi_idname */
88 	0,		/* mi_minpsz */
89 	INFPSZ,		/* mi_maxpsz */
90 	65536,		/* mi_hiwat */
91 	1024		/* mi_lowat */
92 };
93 
94 static struct qinit	cryptmod_rinit = {
95 	(int (*)())cryptmodrput,	/* qi_putp */
96 	cryptmodrsrv,	/* qi_svc */
97 	cryptmodopen,	/* qi_qopen */
98 	cryptmodclose,	/* qi_qclose */
99 	NULL,		/* qi_qadmin */
100 	&cryptmod_minfo,	/* qi_minfo */
101 	NULL		/* qi_mstat */
102 };
103 
104 static struct qinit	cryptmod_winit = {
105 	(int (*)())cryptmodwput,	/* qi_putp */
106 	cryptmodwsrv,	/* qi_srvp */
107 	NULL,		/* qi_qopen */
108 	NULL,		/* qi_qclose */
109 	NULL,		/* qi_qadmin */
110 	&cryptmod_minfo,	/* qi_minfo */
111 	NULL		/* qi_mstat */
112 };
113 
114 static struct streamtab	cryptmod_info = {
115 	&cryptmod_rinit,	/* st_rdinit */
116 	&cryptmod_winit,	/* st_wrinit */
117 	NULL,	/* st_muxrinit */
118 	NULL	/* st_muxwinit */
119 };
120 
121 typedef struct {
122 	uint_t hash_len;
123 	uint_t confound_len;
124 	int (*hashfunc)();
125 } hash_info_t;
126 
127 #define	MAX_CKSUM_LEN 20
128 #define	CONFOUNDER_LEN 8
129 
130 #define	SHA1_HASHSIZE 20
131 #define	MD5_HASHSIZE 16
132 #define	CRC32_HASHSIZE 4
133 #define	MSGBUF_SIZE 4096
134 #define	CONFOUNDER_BYTES 128
135 
136 
137 static int crc32_calc(uchar_t *, uchar_t *, uint_t);
138 static int md5_calc(uchar_t *, uchar_t *, uint_t);
139 static int sha1_calc(uchar_t *, uchar_t *, uint_t);
140 
141 static hash_info_t null_hash = {0, 0, NULL};
142 static hash_info_t crc32_hash = {CRC32_HASHSIZE, CONFOUNDER_LEN, crc32_calc};
143 static hash_info_t md5_hash = {MD5_HASHSIZE, CONFOUNDER_LEN, md5_calc};
144 static hash_info_t sha1_hash = {SHA1_HASHSIZE, CONFOUNDER_LEN, sha1_calc};
145 
146 static crypto_mech_type_t sha1_hmac_mech = CRYPTO_MECH_INVALID;
147 static crypto_mech_type_t md5_hmac_mech = CRYPTO_MECH_INVALID;
148 static crypto_mech_type_t sha1_hash_mech = CRYPTO_MECH_INVALID;
149 static crypto_mech_type_t md5_hash_mech = CRYPTO_MECH_INVALID;
150 
151 static int kef_crypt(struct cipher_data_t *, void *,
152 		    crypto_data_format_t, size_t, int);
153 static mblk_t *
154 arcfour_hmac_md5_encrypt(queue_t *, struct tmodinfo *,
155 		mblk_t *, hash_info_t *);
156 static mblk_t *
157 arcfour_hmac_md5_decrypt(queue_t *, struct tmodinfo *,
158 		mblk_t *, hash_info_t *);
159 
160 static int
161 do_hmac(crypto_mech_type_t, crypto_key_t *, char *, int, char *, int);
162 
163 /*
164  * This is the loadable module wrapper.
165  */
166 #include <sys/modctl.h>
167 
168 static struct fmodsw fsw = {
169 	"cryptmod",
170 	&cryptmod_info,
171 	D_MP | D_MTQPAIR
172 };
173 
174 /*
175  * Module linkage information for the kernel.
176  */
177 static struct modlstrmod modlstrmod = {
178 	&mod_strmodops,
179 	"STREAMS encryption module",
180 	&fsw
181 };
182 
183 static struct modlinkage modlinkage = {
184 	MODREV_1,
185 	&modlstrmod,
186 	NULL
187 };
188 
189 int
190 _init(void)
191 {
192 	return (mod_install(&modlinkage));
193 }
194 
195 int
196 _fini(void)
197 {
198 	return (mod_remove(&modlinkage));
199 }
200 
201 int
202 _info(struct modinfo *modinfop)
203 {
204 	return (mod_info(&modlinkage, modinfop));
205 }
206 
207 static void
208 cleanup(struct cipher_data_t *cd)
209 {
210 	if (cd->key != NULL) {
211 		bzero(cd->key, cd->keylen);
212 		kmem_free(cd->key, cd->keylen);
213 		cd->key = NULL;
214 	}
215 
216 	if (cd->ckey != NULL) {
217 		/*
218 		 * ckey is a crypto_key_t structure which references
219 		 * "cd->key" for its raw key data.  Since that was already
220 		 * cleared out, we don't need another "bzero" here.
221 		 */
222 		kmem_free(cd->ckey, sizeof (crypto_key_t));
223 		cd->ckey = NULL;
224 	}
225 
226 	if (cd->block != NULL) {
227 		kmem_free(cd->block, cd->blocklen);
228 		cd->block = NULL;
229 	}
230 
231 	if (cd->saveblock != NULL) {
232 		kmem_free(cd->saveblock, cd->blocklen);
233 		cd->saveblock = NULL;
234 	}
235 
236 	if (cd->ivec != NULL) {
237 		kmem_free(cd->ivec, cd->ivlen);
238 		cd->ivec = NULL;
239 	}
240 
241 	if (cd->d_encr_key.ck_data != NULL) {
242 		bzero(cd->d_encr_key.ck_data, cd->keylen);
243 		kmem_free(cd->d_encr_key.ck_data, cd->keylen);
244 	}
245 
246 	if (cd->d_hmac_key.ck_data != NULL) {
247 		bzero(cd->d_hmac_key.ck_data, cd->keylen);
248 		kmem_free(cd->d_hmac_key.ck_data, cd->keylen);
249 	}
250 
251 	if (cd->enc_tmpl != NULL)
252 		(void) crypto_destroy_ctx_template(cd->enc_tmpl);
253 
254 	if (cd->hmac_tmpl != NULL)
255 		(void) crypto_destroy_ctx_template(cd->hmac_tmpl);
256 
257 	if (cd->ctx != NULL) {
258 		crypto_cancel_ctx(cd->ctx);
259 		cd->ctx = NULL;
260 	}
261 }
262 
263 /* ARGSUSED */
264 static int
265 cryptmodopen(queue_t *rq, dev_t *dev, int oflag, int sflag, cred_t *crp)
266 {
267 	struct tmodinfo	*tmi;
268 	ASSERT(rq);
269 
270 	if (sflag != MODOPEN)
271 		return (EINVAL);
272 
273 	(void) (STRLOG(CRYPTMOD_ID, 0, 5, SL_TRACE|SL_NOTE,
274 			"cryptmodopen: opening module(PID %d)",
275 			ddi_get_pid()));
276 
277 	if (rq->q_ptr != NULL) {
278 		cmn_err(CE_WARN, "cryptmodopen: already opened");
279 		return (0);
280 	}
281 
282 	/*
283 	 * Allocate and initialize per-Stream structure.
284 	 */
285 	tmi = (struct tmodinfo *)kmem_zalloc(sizeof (struct tmodinfo),
286 						KM_SLEEP);
287 
288 	tmi->enc_data.method = CRYPT_METHOD_NONE;
289 	tmi->dec_data.method = CRYPT_METHOD_NONE;
290 
291 	tmi->ready = (CRYPT_READ_READY | CRYPT_WRITE_READY);
292 
293 	rq->q_ptr = WR(rq)->q_ptr = tmi;
294 
295 	sha1_hmac_mech = crypto_mech2id(SUN_CKM_SHA1_HMAC);
296 	md5_hmac_mech = crypto_mech2id(SUN_CKM_MD5_HMAC);
297 	sha1_hash_mech = crypto_mech2id(SUN_CKM_SHA1);
298 	md5_hash_mech = crypto_mech2id(SUN_CKM_MD5);
299 
300 	qprocson(rq);
301 
302 	return (0);
303 }
304 
305 static int
306 cryptmodclose(queue_t *rq)
307 {
308 	struct tmodinfo *tmi = (struct tmodinfo *)rq->q_ptr;
309 	ASSERT(tmi);
310 
311 	qprocsoff(rq);
312 
313 	cleanup(&tmi->enc_data);
314 	cleanup(&tmi->dec_data);
315 
316 	kmem_free(tmi, sizeof (struct tmodinfo));
317 	rq->q_ptr = WR(rq)->q_ptr = NULL;
318 
319 	return (0);
320 }
321 
322 /*
323  * plaintext_offset
324  *
325  * Calculate exactly how much space is needed in front
326  * of the "plaintext" in an mbuf so it can be positioned
327  * 1 time instead of potentially moving the data multiple
328  * times.
329  */
330 static int
331 plaintext_offset(struct cipher_data_t *cd)
332 {
333 	int headspace = 0;
334 
335 	/* 4 byte length prepended to all RCMD msgs */
336 	if (ANY_RCMD_MODE(cd->option_mask))
337 		headspace += RCMD_LEN_SZ;
338 
339 	/* RCMD V2 mode adds an additional 4 byte plaintext length */
340 	if (cd->option_mask & CRYPTOPT_RCMD_MODE_V2)
341 		headspace += RCMD_LEN_SZ;
342 
343 	/* Need extra space for hash and counfounder */
344 	switch (cd->method) {
345 	case CRYPT_METHOD_DES_CBC_NULL:
346 		headspace += null_hash.hash_len + null_hash.confound_len;
347 		break;
348 	case CRYPT_METHOD_DES_CBC_CRC:
349 		headspace += crc32_hash.hash_len + crc32_hash.confound_len;
350 		break;
351 	case CRYPT_METHOD_DES_CBC_MD5:
352 		headspace += md5_hash.hash_len + md5_hash.confound_len;
353 		break;
354 	case CRYPT_METHOD_DES3_CBC_SHA1:
355 		headspace += sha1_hash.confound_len;
356 		break;
357 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
358 		headspace += md5_hash.hash_len + md5_hash.confound_len;
359 		break;
360 	case CRYPT_METHOD_AES128:
361 	case CRYPT_METHOD_AES256:
362 		headspace += DEFAULT_AES_BLOCKLEN;
363 		break;
364 	case CRYPT_METHOD_DES_CFB:
365 	case CRYPT_METHOD_NONE:
366 		break;
367 	}
368 
369 	return (headspace);
370 }
371 /*
372  * encrypt_size
373  *
374  * Calculate the resulting size when encrypting 'plainlen' bytes
375  * of data.
376  */
377 static size_t
378 encrypt_size(struct cipher_data_t *cd, size_t plainlen)
379 {
380 	size_t cipherlen;
381 
382 	switch (cd->method) {
383 	case CRYPT_METHOD_DES_CBC_NULL:
384 		cipherlen = (size_t)P2ROUNDUP(null_hash.hash_len +
385 					    plainlen, 8);
386 		break;
387 	case CRYPT_METHOD_DES_CBC_MD5:
388 		cipherlen = (size_t)P2ROUNDUP(md5_hash.hash_len +
389 					    md5_hash.confound_len +
390 					    plainlen, 8);
391 		break;
392 	case CRYPT_METHOD_DES_CBC_CRC:
393 		cipherlen = (size_t)P2ROUNDUP(crc32_hash.hash_len +
394 					    crc32_hash.confound_len +
395 					    plainlen, 8);
396 		break;
397 	case CRYPT_METHOD_DES3_CBC_SHA1:
398 		cipherlen = (size_t)P2ROUNDUP(sha1_hash.confound_len +
399 					    plainlen, 8) +
400 					    sha1_hash.hash_len;
401 		break;
402 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
403 		cipherlen = (size_t)P2ROUNDUP(md5_hash.confound_len +
404 				plainlen, 1) + md5_hash.hash_len;
405 		break;
406 	case CRYPT_METHOD_AES128:
407 	case CRYPT_METHOD_AES256:
408 		/* No roundup for AES-CBC-CTS */
409 		cipherlen = DEFAULT_AES_BLOCKLEN + plainlen +
410 			AES_TRUNCATED_HMAC_LEN;
411 		break;
412 	case CRYPT_METHOD_DES_CFB:
413 	case CRYPT_METHOD_NONE:
414 		cipherlen = plainlen;
415 		break;
416 	}
417 
418 	return (cipherlen);
419 }
420 
421 /*
422  * des_cfb_encrypt
423  *
424  * Encrypt the mblk data using DES with cipher feedback.
425  *
426  * Given that V[i] is the initial 64 bit vector, V[n] is the nth 64 bit
427  * vector, D[n] is the nth chunk of 64 bits of data to encrypt
428  * (decrypt), and O[n] is the nth chunk of 64 bits of encrypted
429  * (decrypted) data, then:
430  *
431  *  V[0] = DES(V[i], key)
432  *  O[n] = D[n] <exclusive or > V[n]
433  *  V[n+1] = DES(O[n], key)
434  *
435  * The size of the message being encrypted does not change in this
436  * algorithm, num_bytes in == num_bytes out.
437  */
438 static mblk_t *
439 des_cfb_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
440 {
441 	int savedbytes;
442 	char *iptr, *optr, *lastoutput;
443 
444 	lastoutput = optr = (char *)mp->b_rptr;
445 	iptr = (char *)mp->b_rptr;
446 	savedbytes = tmi->enc_data.bytes % CFB_BLKSZ;
447 
448 	while (iptr < (char *)mp->b_wptr) {
449 		/*
450 		 * Do DES-ECB.
451 		 * The first time this runs, the 'tmi->enc_data.block' will
452 		 * contain the initialization vector that should have been
453 		 * passed in with the SETUP ioctl.
454 		 *
455 		 * V[n] = DES(V[n-1], key)
456 		 */
457 		if (!(tmi->enc_data.bytes % CFB_BLKSZ)) {
458 			int retval = 0;
459 			retval = kef_crypt(&tmi->enc_data,
460 					tmi->enc_data.block,
461 					CRYPTO_DATA_RAW,
462 					tmi->enc_data.blocklen,
463 					CRYPT_ENCRYPT);
464 
465 			if (retval != CRYPTO_SUCCESS) {
466 #ifdef DEBUG
467 				cmn_err(CE_WARN, "des_cfb_encrypt: kef_crypt "
468 					"failed - error 0x%0x", retval);
469 #endif
470 				mp->b_datap->db_type = M_ERROR;
471 				mp->b_rptr = mp->b_datap->db_base;
472 				*mp->b_rptr = EIO;
473 				mp->b_wptr = mp->b_rptr + sizeof (char);
474 				freemsg(mp->b_cont);
475 				mp->b_cont = NULL;
476 				qreply(WR(q), mp);
477 				return (NULL);
478 			}
479 		}
480 
481 		/* O[n] = I[n] ^ V[n] */
482 		*(optr++) = *(iptr++) ^
483 		    tmi->enc_data.block[tmi->enc_data.bytes % CFB_BLKSZ];
484 
485 		tmi->enc_data.bytes++;
486 		/*
487 		 * Feedback the encrypted output as the input to next DES call.
488 		 */
489 		if (!(tmi->enc_data.bytes % CFB_BLKSZ)) {
490 			char *dbptr = tmi->enc_data.block;
491 			/*
492 			 * Get the last bits of input from the previous
493 			 * msg block that we haven't yet used as feedback input.
494 			 */
495 			if (savedbytes > 0) {
496 				bcopy(tmi->enc_data.saveblock,
497 				    dbptr, (size_t)savedbytes);
498 				dbptr += savedbytes;
499 			}
500 
501 			/*
502 			 * Now copy the correct bytes from the current input
503 			 * stream and update the 'lastoutput' ptr
504 			 */
505 			bcopy(lastoutput, dbptr,
506 				(size_t)(CFB_BLKSZ - savedbytes));
507 
508 			lastoutput += (CFB_BLKSZ - savedbytes);
509 			savedbytes = 0;
510 		}
511 	}
512 	/*
513 	 * If there are bytes of input here that we need in the next
514 	 * block to build an ivec, save them off here.
515 	 */
516 	if (lastoutput < optr) {
517 		bcopy(lastoutput,
518 		    tmi->enc_data.saveblock + savedbytes,
519 		    (uint_t)(optr - lastoutput));
520 	}
521 	return (mp);
522 }
523 
524 /*
525  * des_cfb_decrypt
526  *
527  * Decrypt the data in the mblk using DES in Cipher Feedback mode
528  *
529  * # bytes in == # bytes out, no padding, confounding, or hashing
530  * is added.
531  *
532  */
533 static mblk_t *
534 des_cfb_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
535 {
536 	uint_t len;
537 	uint_t savedbytes;
538 	char *iptr;
539 	char *lastinput;
540 	uint_t cp;
541 
542 	len = MBLKL(mp);
543 
544 	/* decrypted output goes into the new data buffer */
545 	lastinput = iptr = (char *)mp->b_rptr;
546 
547 	savedbytes = tmi->dec_data.bytes % tmi->dec_data.blocklen;
548 
549 	/*
550 	 * Save the input CFB_BLKSZ bytes at a time.
551 	 * We are trying to decrypt in-place, but need to keep
552 	 * a small sliding window of encrypted text to be
553 	 * used to construct the feedback buffer.
554 	 */
555 	cp = ((tmi->dec_data.blocklen - savedbytes) > len ? len :
556 		tmi->dec_data.blocklen - savedbytes);
557 
558 	bcopy(lastinput, tmi->dec_data.saveblock + savedbytes, cp);
559 	savedbytes += cp;
560 
561 	lastinput += cp;
562 
563 	while (iptr < (char *)mp->b_wptr) {
564 		/*
565 		 * Do DES-ECB.
566 		 * The first time this runs, the 'tmi->dec_data.block' will
567 		 * contain the initialization vector that should have been
568 		 * passed in with the SETUP ioctl.
569 		 */
570 		if (!(tmi->dec_data.bytes % CFB_BLKSZ)) {
571 			int retval;
572 			retval = kef_crypt(&tmi->dec_data,
573 					tmi->dec_data.block,
574 					CRYPTO_DATA_RAW,
575 					tmi->dec_data.blocklen,
576 					CRYPT_ENCRYPT);
577 
578 			if (retval != CRYPTO_SUCCESS) {
579 #ifdef DEBUG
580 				cmn_err(CE_WARN, "des_cfb_decrypt: kef_crypt "
581 					"failed - status 0x%0x", retval);
582 #endif
583 				mp->b_datap->db_type = M_ERROR;
584 				mp->b_rptr = mp->b_datap->db_base;
585 				*mp->b_rptr = EIO;
586 				mp->b_wptr = mp->b_rptr + sizeof (char);
587 				freemsg(mp->b_cont);
588 				mp->b_cont = NULL;
589 				qreply(WR(q), mp);
590 				return (NULL);
591 			}
592 		}
593 
594 		/*
595 		 * To decrypt, XOR the input with the output from the DES call
596 		 */
597 		*(iptr++) ^= tmi->dec_data.block[tmi->dec_data.bytes %
598 				CFB_BLKSZ];
599 
600 		tmi->dec_data.bytes++;
601 
602 		/*
603 		 * Feedback the encrypted input for next DES call.
604 		 */
605 		if (!(tmi->dec_data.bytes % tmi->dec_data.blocklen)) {
606 			char *dbptr = tmi->dec_data.block;
607 			/*
608 			 * Get the last bits of input from the previous block
609 			 * that we haven't yet processed.
610 			 */
611 			if (savedbytes > 0) {
612 				bcopy(tmi->dec_data.saveblock,
613 				    dbptr, savedbytes);
614 				dbptr += savedbytes;
615 			}
616 
617 			savedbytes = 0;
618 
619 			/*
620 			 * This block makes sure that our local
621 			 * buffer of input data is full and can
622 			 * be accessed from the beginning.
623 			 */
624 			if (lastinput < (char *)mp->b_wptr) {
625 
626 				/* How many bytes are left in the mblk? */
627 				cp = (((char *)mp->b_wptr - lastinput) >
628 					tmi->dec_data.blocklen ?
629 					tmi->dec_data.blocklen :
630 					(char *)mp->b_wptr - lastinput);
631 
632 				/* copy what we need */
633 				bcopy(lastinput, tmi->dec_data.saveblock,
634 					cp);
635 
636 				lastinput += cp;
637 				savedbytes = cp;
638 			}
639 		}
640 	}
641 
642 	return (mp);
643 }
644 
645 /*
646  * crc32_calc
647  *
648  * Compute a CRC32 checksum on the input
649  */
650 static int
651 crc32_calc(uchar_t *buf, uchar_t *input, uint_t len)
652 {
653 	uint32_t crc;
654 
655 	CRC32(crc, input, len, 0, crc32_table);
656 
657 	buf[0] = (uchar_t)(crc & 0xff);
658 	buf[1] = (uchar_t)((crc >> 8) & 0xff);
659 	buf[2] = (uchar_t)((crc >> 16) & 0xff);
660 	buf[3] = (uchar_t)((crc >> 24) & 0xff);
661 
662 	return (CRYPTO_SUCCESS);
663 }
664 
665 static int
666 kef_digest(crypto_mech_type_t digest_type,
667 	uchar_t *input, uint_t inlen,
668 	uchar_t *output, uint_t hashlen)
669 {
670 	iovec_t v1, v2;
671 	crypto_data_t d1, d2;
672 	crypto_mechanism_t mech;
673 	int rv;
674 
675 	mech.cm_type = digest_type;
676 	mech.cm_param = 0;
677 	mech.cm_param_len = 0;
678 
679 	v1.iov_base = (void *)input;
680 	v1.iov_len = inlen;
681 
682 	d1.cd_format = CRYPTO_DATA_RAW;
683 	d1.cd_offset = 0;
684 	d1.cd_length = v1.iov_len;
685 	d1.cd_raw = v1;
686 
687 	v2.iov_base = (void *)output;
688 	v2.iov_len = hashlen;
689 
690 	d2.cd_format = CRYPTO_DATA_RAW;
691 	d2.cd_offset = 0;
692 	d2.cd_length = v2.iov_len;
693 	d2.cd_raw = v2;
694 
695 	rv = crypto_digest(&mech, &d1, &d2, NULL);
696 
697 	return (rv);
698 }
699 
700 /*
701  * sha1_calc
702  *
703  * Get a SHA1 hash on the input data.
704  */
705 static int
706 sha1_calc(uchar_t *output, uchar_t *input, uint_t inlen)
707 {
708 	int rv;
709 
710 	rv = kef_digest(sha1_hash_mech, input, inlen, output, SHA1_HASHSIZE);
711 
712 	return (rv);
713 }
714 
715 /*
716  * Get an MD5 hash on the input data.
717  * md5_calc
718  *
719  */
720 static int
721 md5_calc(uchar_t *output, uchar_t *input, uint_t inlen)
722 {
723 	int rv;
724 
725 	rv = kef_digest(md5_hash_mech, input, inlen, output, MD5_HASHSIZE);
726 
727 	return (rv);
728 }
729 
730 /*
731  * nfold
732  * duplicate the functionality of the krb5_nfold function from
733  * the userland kerberos mech.
734  * This is needed to derive keys for use with 3DES/SHA1-HMAC
735  * ciphers.
736  */
737 static void
738 nfold(int inbits, uchar_t *in, int outbits, uchar_t *out)
739 {
740 	int a, b, c, lcm;
741 	int byte, i, msbit;
742 
743 	inbits >>= 3;
744 	outbits >>= 3;
745 
746 	/* first compute lcm(n,k) */
747 	a = outbits;
748 	b = inbits;
749 
750 	while (b != 0) {
751 		c = b;
752 		b = a%b;
753 		a = c;
754 	}
755 
756 	lcm = outbits*inbits/a;
757 
758 	/* now do the real work */
759 
760 	bzero(out, outbits);
761 	byte = 0;
762 
763 	/*
764 	 * Compute the msbit in k which gets added into this byte
765 	 * first, start with the msbit in the first, unrotated byte
766 	 * then, for each byte, shift to the right for each repetition
767 	 * last, pick out the correct byte within that shifted repetition
768 	 */
769 	for (i = lcm-1; i >= 0; i--) {
770 		msbit = (((inbits<<3)-1)
771 			+(((inbits<<3)+13)*(i/inbits))
772 			+((inbits-(i%inbits))<<3)) %(inbits<<3);
773 
774 		/* pull out the byte value itself */
775 		byte += (((in[((inbits-1)-(msbit>>3))%inbits]<<8)|
776 			(in[((inbits)-(msbit>>3))%inbits]))
777 			>>((msbit&7)+1))&0xff;
778 
779 		/* do the addition */
780 		byte += out[i%outbits];
781 		out[i%outbits] = byte&0xff;
782 
783 		byte >>= 8;
784 	}
785 
786 	/* if there's a carry bit left over, add it back in */
787 	if (byte) {
788 		for (i = outbits-1; i >= 0; i--) {
789 			/* do the addition */
790 			byte += out[i];
791 			out[i] = byte&0xff;
792 
793 			/* keep around the carry bit, if any */
794 			byte >>= 8;
795 		}
796 	}
797 }
798 
799 #define	smask(step) ((1<<step)-1)
800 #define	pstep(x, step) (((x)&smask(step))^(((x)>>step)&smask(step)))
801 #define	parity_char(x) pstep(pstep(pstep((x), 4), 2), 1)
802 
803 /*
804  * Duplicate the functionality of the "dk_derive_key" function
805  * in the Kerberos mechanism.
806  */
807 static int
808 derive_key(struct cipher_data_t *cdata, uchar_t *constdata,
809 	int constlen, char *dkey, int keybytes,
810 	int blocklen)
811 {
812 	int rv = 0;
813 	int n = 0, i;
814 	char *inblock;
815 	char *rawkey;
816 	char *zeroblock;
817 	char *saveblock;
818 
819 	inblock = kmem_zalloc(blocklen, KM_SLEEP);
820 	rawkey = kmem_zalloc(keybytes, KM_SLEEP);
821 	zeroblock = kmem_zalloc(blocklen, KM_SLEEP);
822 
823 	if (constlen == blocklen)
824 		bcopy(constdata, inblock, blocklen);
825 	else
826 		nfold(constlen * 8, constdata,
827 			blocklen * 8, (uchar_t *)inblock);
828 
829 	/*
830 	 * zeroblock is an IV of all 0's.
831 	 *
832 	 * The "block" section of the cdata record is used as the
833 	 * IV for crypto operations in the kef_crypt function.
834 	 *
835 	 * We use 'block' as a generic IV data buffer because it
836 	 * is attached to the stream state data and thus can
837 	 * be used to hold information that must carry over
838 	 * from processing of one mblk to another.
839 	 *
840 	 * Here, we save the current IV and replace it with
841 	 * and empty IV (all 0's) for use when deriving the
842 	 * keys.  Once the key derivation is done, we swap the
843 	 * old IV back into place.
844 	 */
845 	saveblock = cdata->block;
846 	cdata->block = zeroblock;
847 
848 	while (n < keybytes) {
849 		rv = kef_crypt(cdata, inblock, CRYPTO_DATA_RAW,
850 				blocklen, CRYPT_ENCRYPT);
851 		if (rv != CRYPTO_SUCCESS) {
852 			/* put the original IV block back in place */
853 			cdata->block = saveblock;
854 			cmn_err(CE_WARN, "failed to derive a key: %0x", rv);
855 			goto cleanup;
856 		}
857 
858 		if (keybytes - n < blocklen) {
859 			bcopy(inblock, rawkey+n, (keybytes-n));
860 			break;
861 		}
862 		bcopy(inblock, rawkey+n, blocklen);
863 		n += blocklen;
864 	}
865 	/* put the original IV block back in place */
866 	cdata->block = saveblock;
867 
868 	/* finally, make the key */
869 	if (cdata->method == CRYPT_METHOD_DES3_CBC_SHA1) {
870 		/*
871 		 * 3DES key derivation requires that we make sure the
872 		 * key has the proper parity.
873 		 */
874 		for (i = 0; i < 3; i++) {
875 			bcopy(rawkey+(i*7), dkey+(i*8), 7);
876 
877 			/* 'dkey' is our derived key output buffer */
878 			dkey[i*8+7] = (((dkey[i*8]&1)<<1) |
879 					((dkey[i*8+1]&1)<<2) |
880 					((dkey[i*8+2]&1)<<3) |
881 					((dkey[i*8+3]&1)<<4) |
882 					((dkey[i*8+4]&1)<<5) |
883 					((dkey[i*8+5]&1)<<6) |
884 					((dkey[i*8+6]&1)<<7));
885 
886 			for (n = 0; n < 8; n++) {
887 				dkey[i*8 + n] &=  0xfe;
888 				dkey[i*8 + n] |= 1^parity_char(dkey[i*8 + n]);
889 			}
890 		}
891 	} else if (IS_AES_METHOD(cdata->method)) {
892 		bcopy(rawkey, dkey, keybytes);
893 	}
894 cleanup:
895 	kmem_free(inblock, blocklen);
896 	kmem_free(zeroblock, blocklen);
897 	kmem_free(rawkey, keybytes);
898 	return (rv);
899 }
900 
901 /*
902  * create_derived_keys
903  *
904  * Algorithm for deriving a new key and an HMAC key
905  * before computing the 3DES-SHA1-HMAC operation on the plaintext
906  * This algorithm matches the work done by Kerberos mechanism
907  * in userland.
908  */
909 static int
910 create_derived_keys(struct cipher_data_t *cdata, uint32_t usage,
911 		crypto_key_t *enckey, crypto_key_t *hmackey)
912 {
913 	uchar_t constdata[K5CLENGTH];
914 	int keybytes;
915 	int rv;
916 
917 	constdata[0] = (usage>>24)&0xff;
918 	constdata[1] = (usage>>16)&0xff;
919 	constdata[2] = (usage>>8)&0xff;
920 	constdata[3] = usage & 0xff;
921 	/* Use "0xAA" for deriving encryption key */
922 	constdata[4] = 0xAA; /* from MIT Kerberos code */
923 
924 	enckey->ck_length = cdata->keylen * 8;
925 	enckey->ck_format = CRYPTO_KEY_RAW;
926 	enckey->ck_data = kmem_zalloc(cdata->keylen, KM_SLEEP);
927 
928 	switch (cdata->method) {
929 		case CRYPT_METHOD_DES_CFB:
930 		case CRYPT_METHOD_DES_CBC_NULL:
931 		case CRYPT_METHOD_DES_CBC_MD5:
932 		case CRYPT_METHOD_DES_CBC_CRC:
933 			keybytes = 8;
934 			break;
935 		case CRYPT_METHOD_DES3_CBC_SHA1:
936 			keybytes = CRYPT_DES3_KEYBYTES;
937 			break;
938 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
939 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
940 			keybytes = CRYPT_ARCFOUR_KEYBYTES;
941 			break;
942 		case CRYPT_METHOD_AES128:
943 			keybytes = CRYPT_AES128_KEYBYTES;
944 			break;
945 		case CRYPT_METHOD_AES256:
946 			keybytes = CRYPT_AES256_KEYBYTES;
947 			break;
948 	}
949 
950 	/* derive main crypto key */
951 	rv = derive_key(cdata, constdata, sizeof (constdata),
952 		enckey->ck_data, keybytes, cdata->blocklen);
953 
954 	if (rv == CRYPTO_SUCCESS) {
955 
956 		/* Use "0x55" for deriving mac key */
957 		constdata[4] = 0x55;
958 
959 		hmackey->ck_length = cdata->keylen * 8;
960 		hmackey->ck_format = CRYPTO_KEY_RAW;
961 		hmackey->ck_data = kmem_zalloc(cdata->keylen, KM_SLEEP);
962 
963 		rv = derive_key(cdata, constdata, sizeof (constdata),
964 				hmackey->ck_data, keybytes,
965 				cdata->blocklen);
966 	} else {
967 		cmn_err(CE_WARN, "failed to derive crypto key: %02x", rv);
968 	}
969 
970 	return (rv);
971 }
972 
973 /*
974  * Compute 3-DES crypto and HMAC.
975  */
976 static int
977 kef_decr_hmac(struct cipher_data_t *cdata,
978 	mblk_t *mp, int length,
979 	char *hmac, int hmaclen)
980 {
981 	int rv = CRYPTO_FAILED;
982 
983 	crypto_mechanism_t encr_mech;
984 	crypto_mechanism_t mac_mech;
985 	crypto_data_t dd;
986 	crypto_data_t mac;
987 	iovec_t v1;
988 
989 	ASSERT(cdata != NULL);
990 	ASSERT(mp != NULL);
991 	ASSERT(hmac != NULL);
992 
993 	bzero(&dd, sizeof (dd));
994 	dd.cd_format = CRYPTO_DATA_MBLK;
995 	dd.cd_offset = 0;
996 	dd.cd_length = length;
997 	dd.cd_mp = mp;
998 
999 	v1.iov_base = hmac;
1000 	v1.iov_len = hmaclen;
1001 
1002 	mac.cd_format = CRYPTO_DATA_RAW;
1003 	mac.cd_offset = 0;
1004 	mac.cd_length = hmaclen;
1005 	mac.cd_raw = v1;
1006 
1007 	/*
1008 	 * cdata->block holds the IVEC
1009 	 */
1010 	encr_mech.cm_type = cdata->mech_type;
1011 	encr_mech.cm_param = cdata->block;
1012 
1013 	if (cdata->block != NULL)
1014 		encr_mech.cm_param_len = cdata->blocklen;
1015 	else
1016 		encr_mech.cm_param_len = 0;
1017 
1018 	rv = crypto_decrypt(&encr_mech, &dd, &cdata->d_encr_key,
1019 			cdata->enc_tmpl, NULL, NULL);
1020 	if (rv != CRYPTO_SUCCESS) {
1021 		cmn_err(CE_WARN, "crypto_decrypt failed: %0x", rv);
1022 		return (rv);
1023 	}
1024 
1025 	mac_mech.cm_type = sha1_hmac_mech;
1026 	mac_mech.cm_param = NULL;
1027 	mac_mech.cm_param_len = 0;
1028 
1029 	/*
1030 	 * Compute MAC of the plaintext decrypted above.
1031 	 */
1032 	rv = crypto_mac(&mac_mech, &dd, &cdata->d_hmac_key,
1033 			cdata->hmac_tmpl, &mac, NULL);
1034 
1035 	if (rv != CRYPTO_SUCCESS) {
1036 		cmn_err(CE_WARN, "crypto_mac failed: %0x", rv);
1037 	}
1038 
1039 	return (rv);
1040 }
1041 
1042 /*
1043  * Compute 3-DES crypto and HMAC.
1044  */
1045 static int
1046 kef_encr_hmac(struct cipher_data_t *cdata,
1047 	mblk_t *mp, int length,
1048 	char *hmac, int hmaclen)
1049 {
1050 	int rv = CRYPTO_FAILED;
1051 
1052 	crypto_mechanism_t encr_mech;
1053 	crypto_mechanism_t mac_mech;
1054 	crypto_data_t dd;
1055 	crypto_data_t mac;
1056 	iovec_t v1;
1057 
1058 	ASSERT(cdata != NULL);
1059 	ASSERT(mp != NULL);
1060 	ASSERT(hmac != NULL);
1061 
1062 	bzero(&dd, sizeof (dd));
1063 	dd.cd_format = CRYPTO_DATA_MBLK;
1064 	dd.cd_offset = 0;
1065 	dd.cd_length = length;
1066 	dd.cd_mp = mp;
1067 
1068 	v1.iov_base = hmac;
1069 	v1.iov_len = hmaclen;
1070 
1071 	mac.cd_format = CRYPTO_DATA_RAW;
1072 	mac.cd_offset = 0;
1073 	mac.cd_length = hmaclen;
1074 	mac.cd_raw = v1;
1075 
1076 	/*
1077 	 * cdata->block holds the IVEC
1078 	 */
1079 	encr_mech.cm_type = cdata->mech_type;
1080 	encr_mech.cm_param = cdata->block;
1081 
1082 	if (cdata->block != NULL)
1083 		encr_mech.cm_param_len = cdata->blocklen;
1084 	else
1085 		encr_mech.cm_param_len = 0;
1086 
1087 	mac_mech.cm_type = sha1_hmac_mech;
1088 	mac_mech.cm_param = NULL;
1089 	mac_mech.cm_param_len = 0;
1090 
1091 	rv = crypto_mac(&mac_mech, &dd, &cdata->d_hmac_key,
1092 			cdata->hmac_tmpl, &mac, NULL);
1093 
1094 	if (rv != CRYPTO_SUCCESS) {
1095 		cmn_err(CE_WARN, "crypto_mac failed: %0x", rv);
1096 		return (rv);
1097 	}
1098 
1099 	rv = crypto_encrypt(&encr_mech, &dd, &cdata->d_encr_key,
1100 			cdata->enc_tmpl, NULL, NULL);
1101 	if (rv != CRYPTO_SUCCESS) {
1102 		cmn_err(CE_WARN, "crypto_encrypt failed: %0x", rv);
1103 	}
1104 
1105 	return (rv);
1106 }
1107 
1108 /*
1109  * kef_crypt
1110  *
1111  * Use the Kernel encryption framework to provide the
1112  * crypto operations for the indicated data.
1113  */
1114 static int
1115 kef_crypt(struct cipher_data_t *cdata,
1116 	void *indata, crypto_data_format_t fmt,
1117 	size_t length, int mode)
1118 {
1119 	int rv = CRYPTO_FAILED;
1120 
1121 	crypto_mechanism_t mech;
1122 	crypto_key_t crkey;
1123 	iovec_t v1;
1124 	crypto_data_t d1;
1125 
1126 	ASSERT(cdata != NULL);
1127 	ASSERT(indata != NULL);
1128 	ASSERT(fmt == CRYPTO_DATA_RAW || fmt == CRYPTO_DATA_MBLK);
1129 
1130 	bzero(&crkey, sizeof (crkey));
1131 	bzero(&d1, sizeof (d1));
1132 
1133 	crkey.ck_format = CRYPTO_KEY_RAW;
1134 	crkey.ck_data =  cdata->key;
1135 
1136 	/* keys are measured in bits, not bytes, so multiply by 8 */
1137 	crkey.ck_length = cdata->keylen * 8;
1138 
1139 	if (fmt == CRYPTO_DATA_RAW) {
1140 		v1.iov_base = (char *)indata;
1141 		v1.iov_len = length;
1142 	}
1143 
1144 	d1.cd_format = fmt;
1145 	d1.cd_offset = 0;
1146 	d1.cd_length = length;
1147 	if (fmt == CRYPTO_DATA_RAW)
1148 		d1.cd_raw = v1;
1149 	else if (fmt == CRYPTO_DATA_MBLK)
1150 		d1.cd_mp = (mblk_t *)indata;
1151 
1152 	mech.cm_type = cdata->mech_type;
1153 	mech.cm_param = cdata->block;
1154 	/*
1155 	 * cdata->block holds the IVEC
1156 	 */
1157 	if (cdata->block != NULL)
1158 		mech.cm_param_len = cdata->blocklen;
1159 	else
1160 		mech.cm_param_len = 0;
1161 
1162 	/*
1163 	 * encrypt and decrypt in-place
1164 	 */
1165 	if (mode == CRYPT_ENCRYPT)
1166 		rv = crypto_encrypt(&mech, &d1, &crkey, NULL, NULL, NULL);
1167 	else
1168 		rv = crypto_decrypt(&mech, &d1, &crkey, NULL, NULL, NULL);
1169 
1170 	if (rv != CRYPTO_SUCCESS) {
1171 		cmn_err(CE_WARN, "%s returned error %08x",
1172 			(mode == CRYPT_ENCRYPT ? "crypto_encrypt" :
1173 				"crypto_decrypt"), rv);
1174 		return (CRYPTO_FAILED);
1175 	}
1176 
1177 	return (rv);
1178 }
1179 
1180 static int
1181 do_hmac(crypto_mech_type_t mech,
1182 	crypto_key_t *key,
1183 	char *data, int datalen,
1184 	char *hmac, int hmaclen)
1185 {
1186 	int rv = 0;
1187 	crypto_mechanism_t mac_mech;
1188 	crypto_data_t dd;
1189 	crypto_data_t mac;
1190 	iovec_t vdata, vmac;
1191 
1192 	mac_mech.cm_type = mech;
1193 	mac_mech.cm_param = NULL;
1194 	mac_mech.cm_param_len = 0;
1195 
1196 	vdata.iov_base = data;
1197 	vdata.iov_len = datalen;
1198 
1199 	bzero(&dd, sizeof (dd));
1200 	dd.cd_format = CRYPTO_DATA_RAW;
1201 	dd.cd_offset = 0;
1202 	dd.cd_length = datalen;
1203 	dd.cd_raw = vdata;
1204 
1205 	vmac.iov_base = hmac;
1206 	vmac.iov_len = hmaclen;
1207 
1208 	mac.cd_format = CRYPTO_DATA_RAW;
1209 	mac.cd_offset = 0;
1210 	mac.cd_length = hmaclen;
1211 	mac.cd_raw = vmac;
1212 
1213 	/*
1214 	 * Compute MAC of the plaintext decrypted above.
1215 	 */
1216 	rv = crypto_mac(&mac_mech, &dd, key, NULL, &mac, NULL);
1217 
1218 	if (rv != CRYPTO_SUCCESS) {
1219 		cmn_err(CE_WARN, "crypto_mac failed: %0x", rv);
1220 	}
1221 
1222 	return (rv);
1223 }
1224 
1225 #define	XOR_BLOCK(src, dst) \
1226 	(dst)[0] ^= (src)[0]; \
1227 	(dst)[1] ^= (src)[1]; \
1228 	(dst)[2] ^= (src)[2]; \
1229 	(dst)[3] ^= (src)[3]; \
1230 	(dst)[4] ^= (src)[4]; \
1231 	(dst)[5] ^= (src)[5]; \
1232 	(dst)[6] ^= (src)[6]; \
1233 	(dst)[7] ^= (src)[7]; \
1234 	(dst)[8] ^= (src)[8]; \
1235 	(dst)[9] ^= (src)[9]; \
1236 	(dst)[10] ^= (src)[10]; \
1237 	(dst)[11] ^= (src)[11]; \
1238 	(dst)[12] ^= (src)[12]; \
1239 	(dst)[13] ^= (src)[13]; \
1240 	(dst)[14] ^= (src)[14]; \
1241 	(dst)[15] ^= (src)[15]
1242 
1243 #define	xorblock(x, y) XOR_BLOCK(y, x)
1244 
1245 static int
1246 aes_cbc_cts_encrypt(struct tmodinfo *tmi, uchar_t *plain, size_t length)
1247 {
1248 	int result = CRYPTO_SUCCESS;
1249 	unsigned char tmp[DEFAULT_AES_BLOCKLEN];
1250 	unsigned char tmp2[DEFAULT_AES_BLOCKLEN];
1251 	unsigned char tmp3[DEFAULT_AES_BLOCKLEN];
1252 	int nblocks = 0, blockno;
1253 	crypto_data_t ct, pt;
1254 	crypto_mechanism_t mech;
1255 
1256 	mech.cm_type = tmi->enc_data.mech_type;
1257 	if (tmi->enc_data.ivlen > 0 && tmi->enc_data.ivec != NULL) {
1258 		bcopy(tmi->enc_data.ivec, tmp, DEFAULT_AES_BLOCKLEN);
1259 		mech.cm_param = tmi->enc_data.ivec;
1260 		mech.cm_param_len = tmi->enc_data.ivlen;
1261 	} else {
1262 		bzero(tmp, sizeof (tmp));
1263 		mech.cm_param = NULL;
1264 		mech.cm_param_len = 0;
1265 	}
1266 
1267 	nblocks = (length + DEFAULT_AES_BLOCKLEN - 1) / DEFAULT_AES_BLOCKLEN;
1268 
1269 	bzero(&ct, sizeof (crypto_data_t));
1270 	bzero(&pt, sizeof (crypto_data_t));
1271 
1272 	if (nblocks == 1) {
1273 		pt.cd_format = CRYPTO_DATA_RAW;
1274 		pt.cd_length = length;
1275 		pt.cd_raw.iov_base = (char *)plain;
1276 		pt.cd_raw.iov_len = length;
1277 
1278 		result = crypto_encrypt(&mech, &pt,
1279 			&tmi->enc_data.d_encr_key, NULL, NULL, NULL);
1280 
1281 		if (result != CRYPTO_SUCCESS) {
1282 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
1283 				"crypto_encrypt failed: %0x", result);
1284 		}
1285 	} else {
1286 		size_t nleft;
1287 
1288 		ct.cd_format = CRYPTO_DATA_RAW;
1289 		ct.cd_offset = 0;
1290 		ct.cd_length = DEFAULT_AES_BLOCKLEN;
1291 
1292 		pt.cd_format = CRYPTO_DATA_RAW;
1293 		pt.cd_offset = 0;
1294 		pt.cd_length = DEFAULT_AES_BLOCKLEN;
1295 
1296 		result = crypto_encrypt_init(&mech,
1297 				&tmi->enc_data.d_encr_key,
1298 				tmi->enc_data.enc_tmpl,
1299 				&tmi->enc_data.ctx, NULL);
1300 
1301 		if (result != CRYPTO_SUCCESS) {
1302 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
1303 				"crypto_encrypt_init failed: %0x", result);
1304 			goto cleanup;
1305 		}
1306 
1307 		for (blockno = 0; blockno < nblocks - 2; blockno++) {
1308 			xorblock(tmp, plain + blockno * DEFAULT_AES_BLOCKLEN);
1309 
1310 			pt.cd_raw.iov_base = (char *)tmp;
1311 			pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1312 
1313 			ct.cd_raw.iov_base = (char *)plain +
1314 				blockno * DEFAULT_AES_BLOCKLEN;
1315 			ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1316 
1317 			result = crypto_encrypt_update(tmi->enc_data.ctx,
1318 					&pt, &ct, NULL);
1319 
1320 			if (result != CRYPTO_SUCCESS) {
1321 				cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
1322 					"crypto_encrypt_update failed: %0x",
1323 					result);
1324 				goto cleanup;
1325 			}
1326 			/* copy result over original bytes */
1327 			/* make another copy for the next XOR step */
1328 			bcopy(plain + blockno * DEFAULT_AES_BLOCKLEN,
1329 				tmp, DEFAULT_AES_BLOCKLEN);
1330 		}
1331 		/* XOR cipher text from n-3 with plain text from n-2 */
1332 		xorblock(tmp, plain + (nblocks - 2) * DEFAULT_AES_BLOCKLEN);
1333 
1334 		pt.cd_raw.iov_base = (char *)tmp;
1335 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1336 
1337 		ct.cd_raw.iov_base = (char *)tmp2;
1338 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1339 
1340 		/* encrypt XOR-ed block N-2 */
1341 		result = crypto_encrypt_update(tmi->enc_data.ctx,
1342 				&pt, &ct, NULL);
1343 		if (result != CRYPTO_SUCCESS) {
1344 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
1345 				"crypto_encrypt_update(2) failed: %0x",
1346 				result);
1347 			goto cleanup;
1348 		}
1349 		nleft = length - (nblocks - 1) * DEFAULT_AES_BLOCKLEN;
1350 
1351 		bzero(tmp3, sizeof (tmp3));
1352 		/* Save final plaintext bytes from n-1 */
1353 		bcopy(plain + (nblocks - 1) * DEFAULT_AES_BLOCKLEN, tmp3,
1354 			nleft);
1355 
1356 		/* Overwrite n-1 with cipher text from n-2 */
1357 		bcopy(tmp2, plain + (nblocks - 1) * DEFAULT_AES_BLOCKLEN,
1358 			nleft);
1359 
1360 		bcopy(tmp2, tmp, DEFAULT_AES_BLOCKLEN);
1361 		/* XOR cipher text from n-1 with plain text from n-1 */
1362 		xorblock(tmp, tmp3);
1363 
1364 		pt.cd_raw.iov_base = (char *)tmp;
1365 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1366 
1367 		ct.cd_raw.iov_base = (char *)tmp2;
1368 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1369 
1370 		/* encrypt block N-2 */
1371 		result = crypto_encrypt_update(tmi->enc_data.ctx,
1372 			&pt, &ct, NULL);
1373 
1374 		if (result != CRYPTO_SUCCESS) {
1375 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
1376 				"crypto_encrypt_update(3) failed: %0x",
1377 				result);
1378 			goto cleanup;
1379 		}
1380 
1381 		bcopy(tmp2, plain + (nblocks - 2) * DEFAULT_AES_BLOCKLEN,
1382 			DEFAULT_AES_BLOCKLEN);
1383 
1384 
1385 		ct.cd_raw.iov_base = (char *)tmp2;
1386 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1387 
1388 		/*
1389 		 * Ignore the output on the final step.
1390 		 */
1391 		result = crypto_encrypt_final(tmi->enc_data.ctx, &ct, NULL);
1392 		if (result != CRYPTO_SUCCESS) {
1393 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
1394 				"crypto_encrypt_final(3) failed: %0x",
1395 				result);
1396 		}
1397 		tmi->enc_data.ctx = NULL;
1398 	}
1399 cleanup:
1400 	bzero(tmp, sizeof (tmp));
1401 	bzero(tmp2, sizeof (tmp));
1402 	bzero(tmp3, sizeof (tmp));
1403 	bzero(tmi->enc_data.block, tmi->enc_data.blocklen);
1404 	return (result);
1405 }
1406 
1407 static int
1408 aes_cbc_cts_decrypt(struct tmodinfo *tmi, uchar_t *buff, size_t length)
1409 {
1410 	int result = CRYPTO_SUCCESS;
1411 	unsigned char tmp[DEFAULT_AES_BLOCKLEN];
1412 	unsigned char tmp2[DEFAULT_AES_BLOCKLEN];
1413 	unsigned char tmp3[DEFAULT_AES_BLOCKLEN];
1414 	int nblocks = 0, blockno;
1415 	crypto_data_t ct, pt;
1416 	crypto_mechanism_t mech;
1417 
1418 	mech.cm_type = tmi->enc_data.mech_type;
1419 
1420 	if (tmi->dec_data.ivec_usage != IVEC_NEVER &&
1421 	    tmi->dec_data.ivlen > 0 && tmi->dec_data.ivec != NULL) {
1422 		bcopy(tmi->dec_data.ivec, tmp, DEFAULT_AES_BLOCKLEN);
1423 		mech.cm_param = tmi->dec_data.ivec;
1424 		mech.cm_param_len = tmi->dec_data.ivlen;
1425 	} else {
1426 		bzero(tmp, sizeof (tmp));
1427 		mech.cm_param_len = 0;
1428 		mech.cm_param = NULL;
1429 	}
1430 	nblocks = (length + DEFAULT_AES_BLOCKLEN - 1) / DEFAULT_AES_BLOCKLEN;
1431 
1432 	bzero(&pt, sizeof (pt));
1433 	bzero(&ct, sizeof (ct));
1434 
1435 	if (nblocks == 1) {
1436 		ct.cd_format = CRYPTO_DATA_RAW;
1437 		ct.cd_length = length;
1438 		ct.cd_raw.iov_base = (char *)buff;
1439 		ct.cd_raw.iov_len = length;
1440 
1441 		result = crypto_decrypt(&mech, &ct,
1442 			&tmi->dec_data.d_encr_key, NULL, NULL, NULL);
1443 
1444 		if (result != CRYPTO_SUCCESS) {
1445 			cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
1446 				"crypto_decrypt failed: %0x", result);
1447 			goto cleanup;
1448 		}
1449 	} else {
1450 		ct.cd_format = CRYPTO_DATA_RAW;
1451 		ct.cd_offset = 0;
1452 		ct.cd_length = DEFAULT_AES_BLOCKLEN;
1453 
1454 		pt.cd_format = CRYPTO_DATA_RAW;
1455 		pt.cd_offset = 0;
1456 		pt.cd_length = DEFAULT_AES_BLOCKLEN;
1457 
1458 		result = crypto_encrypt_init(&mech,
1459 				&tmi->dec_data.d_encr_key,
1460 				tmi->dec_data.enc_tmpl,
1461 				&tmi->dec_data.ctx, NULL);
1462 
1463 		if (result != CRYPTO_SUCCESS) {
1464 			cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
1465 				"crypto_decrypt_init failed: %0x", result);
1466 			goto cleanup;
1467 		}
1468 		for (blockno = 0; blockno < nblocks - 2; blockno++) {
1469 			ct.cd_raw.iov_base = (char *)buff +
1470 				(blockno * DEFAULT_AES_BLOCKLEN);
1471 			ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1472 
1473 			pt.cd_raw.iov_base = (char *)tmp2;
1474 			pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1475 
1476 			/*
1477 			 * Save the input to the decrypt so it can
1478 			 * be used later for an XOR operation
1479 			 */
1480 			bcopy(buff + (blockno * DEFAULT_AES_BLOCKLEN),
1481 				tmi->dec_data.block, DEFAULT_AES_BLOCKLEN);
1482 
1483 			result = crypto_decrypt_update(tmi->dec_data.ctx,
1484 					&ct, &pt, NULL);
1485 			if (result != CRYPTO_SUCCESS) {
1486 				cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
1487 					"crypto_decrypt_update(1) error - "
1488 					"result = 0x%08x", result);
1489 				goto cleanup;
1490 			}
1491 			xorblock(tmp2, tmp);
1492 			bcopy(tmp2, buff + blockno * DEFAULT_AES_BLOCKLEN,
1493 				DEFAULT_AES_BLOCKLEN);
1494 			/*
1495 			 * The original cipher text is used as the xor
1496 			 * for the next block, save it here.
1497 			 */
1498 			bcopy(tmi->dec_data.block, tmp, DEFAULT_AES_BLOCKLEN);
1499 		}
1500 		ct.cd_raw.iov_base = (char *)buff +
1501 			((nblocks - 2) * DEFAULT_AES_BLOCKLEN);
1502 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1503 		pt.cd_raw.iov_base = (char *)tmp2;
1504 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1505 
1506 		result = crypto_decrypt_update(tmi->dec_data.ctx,
1507 				&ct, &pt, NULL);
1508 		if (result != CRYPTO_SUCCESS) {
1509 			cmn_err(CE_WARN,
1510 				"aes_cbc_cts_decrypt: "
1511 				"crypto_decrypt_update(2) error -"
1512 				" result = 0x%08x", result);
1513 			goto cleanup;
1514 		}
1515 		bzero(tmp3, sizeof (tmp3));
1516 		bcopy(buff + (nblocks - 1) * DEFAULT_AES_BLOCKLEN, tmp3,
1517 			length - ((nblocks - 1) * DEFAULT_AES_BLOCKLEN));
1518 
1519 		xorblock(tmp2, tmp3);
1520 		bcopy(tmp2, buff + (nblocks - 1) * DEFAULT_AES_BLOCKLEN,
1521 			length - ((nblocks - 1) * DEFAULT_AES_BLOCKLEN));
1522 
1523 		/* 2nd to last block ... */
1524 		bcopy(tmp3, tmp2,
1525 			length - ((nblocks - 1) * DEFAULT_AES_BLOCKLEN));
1526 
1527 		ct.cd_raw.iov_base = (char *)tmp2;
1528 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1529 		pt.cd_raw.iov_base = (char *)tmp3;
1530 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1531 
1532 		result = crypto_decrypt_update(tmi->dec_data.ctx,
1533 				&ct, &pt, NULL);
1534 		if (result != CRYPTO_SUCCESS) {
1535 			cmn_err(CE_WARN,
1536 				"aes_cbc_cts_decrypt: "
1537 				"crypto_decrypt_update(3) error - "
1538 				"result = 0x%08x", result);
1539 			goto cleanup;
1540 		}
1541 		xorblock(tmp3, tmp);
1542 
1543 
1544 		/* Finally, update the 2nd to last block and we are done. */
1545 		bcopy(tmp3, buff + (nblocks - 2) * DEFAULT_AES_BLOCKLEN,
1546 			DEFAULT_AES_BLOCKLEN);
1547 
1548 		/* Do Final step, but ignore output */
1549 		pt.cd_raw.iov_base = (char *)tmp2;
1550 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
1551 		result = crypto_decrypt_final(tmi->dec_data.ctx, &pt, NULL);
1552 		if (result != CRYPTO_SUCCESS) {
1553 			cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
1554 				"crypto_decrypt_final error - "
1555 				"result = 0x%0x", result);
1556 		}
1557 		tmi->dec_data.ctx = NULL;
1558 	}
1559 
1560 cleanup:
1561 	bzero(tmp, sizeof (tmp));
1562 	bzero(tmp2, sizeof (tmp));
1563 	bzero(tmp3, sizeof (tmp));
1564 	bzero(tmi->dec_data.block, tmi->dec_data.blocklen);
1565 	return (result);
1566 }
1567 
1568 /*
1569  * AES decrypt
1570  *
1571  * format of ciphertext when using AES
1572  *  +-------------+------------+------------+
1573  *  |  confounder | msg-data   |  hmac      |
1574  *  +-------------+------------+------------+
1575  */
1576 static mblk_t *
1577 aes_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
1578 	hash_info_t *hash)
1579 {
1580 	int result;
1581 	size_t enclen;
1582 	size_t inlen;
1583 	uchar_t hmacbuff[64];
1584 	uchar_t tmpiv[DEFAULT_AES_BLOCKLEN];
1585 
1586 	inlen = (size_t)MBLKL(mp);
1587 
1588 	enclen = inlen - AES_TRUNCATED_HMAC_LEN;
1589 	if (tmi->dec_data.ivec_usage != IVEC_NEVER &&
1590 		tmi->dec_data.ivec != NULL && tmi->dec_data.ivlen > 0) {
1591 		int nblocks = (enclen + DEFAULT_AES_BLOCKLEN - 1) /
1592 				DEFAULT_AES_BLOCKLEN;
1593 		bcopy(mp->b_rptr + DEFAULT_AES_BLOCKLEN * (nblocks - 2),
1594 			tmpiv, DEFAULT_AES_BLOCKLEN);
1595 	}
1596 
1597 	/* AES Decrypt */
1598 	result = aes_cbc_cts_decrypt(tmi, mp->b_rptr, enclen);
1599 
1600 	if (result != CRYPTO_SUCCESS) {
1601 		cmn_err(CE_WARN,
1602 			"aes_decrypt:  aes_cbc_cts_decrypt "
1603 			"failed - error %0x", result);
1604 		goto cleanup;
1605 	}
1606 
1607 	/* Verify the HMAC */
1608 	result = do_hmac(sha1_hmac_mech,
1609 			&tmi->dec_data.d_hmac_key,
1610 			(char *)mp->b_rptr, enclen,
1611 			(char *)hmacbuff, hash->hash_len);
1612 
1613 	if (result != CRYPTO_SUCCESS) {
1614 		cmn_err(CE_WARN,
1615 			"aes_decrypt:  do_hmac failed - error %0x", result);
1616 		goto cleanup;
1617 	}
1618 
1619 	if (bcmp(hmacbuff, mp->b_rptr + enclen,
1620 		AES_TRUNCATED_HMAC_LEN) != 0) {
1621 		result = -1;
1622 		cmn_err(CE_WARN, "aes_decrypt: checksum verification failed");
1623 		goto cleanup;
1624 	}
1625 
1626 	/* truncate the mblk at the end of the decrypted text */
1627 	mp->b_wptr = mp->b_rptr + enclen;
1628 
1629 	/* Adjust the beginning of the buffer to skip the confounder */
1630 	mp->b_rptr += DEFAULT_AES_BLOCKLEN;
1631 
1632 	if (tmi->dec_data.ivec_usage != IVEC_NEVER &&
1633 		tmi->dec_data.ivec != NULL && tmi->dec_data.ivlen > 0)
1634 		bcopy(tmpiv, tmi->dec_data.ivec, DEFAULT_AES_BLOCKLEN);
1635 
1636 cleanup:
1637 	if (result != CRYPTO_SUCCESS) {
1638 		mp->b_datap->db_type = M_ERROR;
1639 		mp->b_rptr = mp->b_datap->db_base;
1640 		*mp->b_rptr = EIO;
1641 		mp->b_wptr = mp->b_rptr + sizeof (char);
1642 		freemsg(mp->b_cont);
1643 		mp->b_cont = NULL;
1644 		qreply(WR(q), mp);
1645 		return (NULL);
1646 	}
1647 	return (mp);
1648 }
1649 
1650 /*
1651  * AES encrypt
1652  *
1653  * format of ciphertext when using AES
1654  *  +-------------+------------+------------+
1655  *  |  confounder | msg-data   |  hmac      |
1656  *  +-------------+------------+------------+
1657  */
1658 static mblk_t *
1659 aes_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
1660 	hash_info_t *hash)
1661 {
1662 	int result;
1663 	size_t cipherlen;
1664 	size_t inlen;
1665 	uchar_t hmacbuff[64];
1666 
1667 	inlen = (size_t)MBLKL(mp);
1668 
1669 	cipherlen = encrypt_size(&tmi->enc_data, inlen);
1670 
1671 	ASSERT(MBLKSIZE(mp) >= cipherlen);
1672 
1673 	/*
1674 	 * Shift the rptr back enough to insert the confounder.
1675 	 */
1676 	mp->b_rptr -= DEFAULT_AES_BLOCKLEN;
1677 
1678 	/* Get random data for confounder */
1679 	(void) random_get_pseudo_bytes((uint8_t *)mp->b_rptr,
1680 		DEFAULT_AES_BLOCKLEN);
1681 
1682 	/*
1683 	 * Because we encrypt in-place, we need to calculate
1684 	 * the HMAC of the plaintext now, then stick it on
1685 	 * the end of the ciphertext down below.
1686 	 */
1687 	result = do_hmac(sha1_hmac_mech,
1688 			&tmi->enc_data.d_hmac_key,
1689 			(char *)mp->b_rptr, DEFAULT_AES_BLOCKLEN + inlen,
1690 			(char *)hmacbuff, hash->hash_len);
1691 
1692 	if (result != CRYPTO_SUCCESS) {
1693 		cmn_err(CE_WARN, "aes_encrypt:  do_hmac failed - error %0x",
1694 			result);
1695 		goto cleanup;
1696 	}
1697 	/* Encrypt using AES-CBC-CTS */
1698 	result = aes_cbc_cts_encrypt(tmi, mp->b_rptr,
1699 		inlen + DEFAULT_AES_BLOCKLEN);
1700 
1701 	if (result != CRYPTO_SUCCESS) {
1702 		cmn_err(CE_WARN, "aes_encrypt:  aes_cbc_cts_encrypt "
1703 			"failed - error %0x", result);
1704 		goto cleanup;
1705 	}
1706 
1707 	/* copy the truncated HMAC to the end of the mblk */
1708 	bcopy(hmacbuff, mp->b_rptr + DEFAULT_AES_BLOCKLEN + inlen,
1709 		AES_TRUNCATED_HMAC_LEN);
1710 
1711 	mp->b_wptr = mp->b_rptr + cipherlen;
1712 
1713 	/*
1714 	 * The final block of cipher text (not the HMAC) is used
1715 	 * as the next IV.
1716 	 */
1717 	if (tmi->enc_data.ivec_usage != IVEC_NEVER &&
1718 	    tmi->enc_data.ivec != NULL) {
1719 		int nblocks = (inlen + 2 * DEFAULT_AES_BLOCKLEN - 1) /
1720 			DEFAULT_AES_BLOCKLEN;
1721 
1722 		bcopy(mp->b_rptr + (nblocks - 2) * DEFAULT_AES_BLOCKLEN,
1723 			tmi->enc_data.ivec, DEFAULT_AES_BLOCKLEN);
1724 	}
1725 
1726 cleanup:
1727 	if (result != CRYPTO_SUCCESS) {
1728 		mp->b_datap->db_type = M_ERROR;
1729 		mp->b_rptr = mp->b_datap->db_base;
1730 		*mp->b_rptr = EIO;
1731 		mp->b_wptr = mp->b_rptr + sizeof (char);
1732 		freemsg(mp->b_cont);
1733 		mp->b_cont = NULL;
1734 		qreply(WR(q), mp);
1735 		return (NULL);
1736 	}
1737 	return (mp);
1738 }
1739 
1740 /*
1741  * ARCFOUR-HMAC-MD5 decrypt
1742  *
1743  * format of ciphertext when using ARCFOUR-HMAC-MD5
1744  *  +-----------+------------+------------+
1745  *  |  hmac     | confounder |  msg-data  |
1746  *  +-----------+------------+------------+
1747  *
1748  */
1749 static mblk_t *
1750 arcfour_hmac_md5_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
1751 			hash_info_t *hash)
1752 {
1753 	int result;
1754 	size_t cipherlen;
1755 	size_t inlen;
1756 	size_t saltlen;
1757 	crypto_key_t k1, k2;
1758 	crypto_data_t indata;
1759 	iovec_t v1;
1760 	uchar_t ms_exp[9] = {0xab, 0xab, 0xab, 0xab, 0xab,
1761 				0xab, 0xab, 0xab, 0xab };
1762 	uchar_t k1data[CRYPT_ARCFOUR_KEYBYTES];
1763 	uchar_t k2data[CRYPT_ARCFOUR_KEYBYTES];
1764 	uchar_t cksum[MD5_HASHSIZE];
1765 	uchar_t saltdata[CRYPT_ARCFOUR_KEYBYTES];
1766 	crypto_mechanism_t mech;
1767 	int usage;
1768 
1769 	/* The usage constant is 1026 for all "old" rcmd mode operations */
1770 	if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V1)
1771 		usage = RCMDV1_USAGE;
1772 	else
1773 		usage = ARCFOUR_DECRYPT_USAGE;
1774 
1775 	/*
1776 	 * The size at this point should be the size of
1777 	 * all the plaintext plus the optional plaintext length
1778 	 * needed for RCMD V2 mode.  There should also be room
1779 	 * at the head of the mblk for the confounder and hash info.
1780 	 */
1781 	inlen = (size_t)MBLKL(mp);
1782 
1783 	/*
1784 	 * The cipherlen does not include the HMAC at the
1785 	 * head of the buffer.
1786 	 */
1787 	cipherlen = inlen - hash->hash_len;
1788 
1789 	ASSERT(MBLKSIZE(mp) >= cipherlen);
1790 	if (tmi->dec_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
1791 		bcopy(ARCFOUR_EXP_SALT, saltdata, strlen(ARCFOUR_EXP_SALT));
1792 		saltdata[9] = 0;
1793 		saltdata[10] = usage & 0xff;
1794 		saltdata[11] = (usage >> 8) & 0xff;
1795 		saltdata[12] = (usage >> 16) & 0xff;
1796 		saltdata[13] = (usage >> 24) & 0xff;
1797 		saltlen = 14;
1798 	} else {
1799 		saltdata[0] = usage & 0xff;
1800 		saltdata[1] = (usage >> 8) & 0xff;
1801 		saltdata[2] = (usage >> 16) & 0xff;
1802 		saltdata[3] = (usage >> 24) & 0xff;
1803 		saltlen = 4;
1804 	}
1805 	/*
1806 	 * Use the salt value to create a key to be used
1807 	 * for subsequent HMAC operations.
1808 	 */
1809 	result = do_hmac(md5_hmac_mech,
1810 			tmi->dec_data.ckey,
1811 			(char *)saltdata, saltlen,
1812 			(char *)k1data, sizeof (k1data));
1813 	if (result != CRYPTO_SUCCESS) {
1814 		cmn_err(CE_WARN,
1815 			"arcfour_hmac_md5_decrypt:  do_hmac(k1)"
1816 			"failed - error %0x", result);
1817 		goto cleanup;
1818 	}
1819 	bcopy(k1data, k2data, sizeof (k1data));
1820 
1821 	/*
1822 	 * For the neutered MS RC4 encryption type,
1823 	 * set the trailing 9 bytes to 0xab per the
1824 	 * RC4-HMAC spec.
1825 	 */
1826 	if (tmi->dec_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
1827 		bcopy((void *)&k1data[7], ms_exp, sizeof (ms_exp));
1828 	}
1829 
1830 	mech.cm_type = tmi->dec_data.mech_type;
1831 	mech.cm_param = NULL;
1832 	mech.cm_param_len = 0;
1833 
1834 	/*
1835 	 * If we have not yet initialized the decryption key,
1836 	 * context, and template, do it now.
1837 	 */
1838 	if (tmi->dec_data.ctx == NULL ||
1839 	    (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V1)) {
1840 		k1.ck_format = CRYPTO_KEY_RAW;
1841 		k1.ck_length = CRYPT_ARCFOUR_KEYBYTES * 8;
1842 		k1.ck_data = k1data;
1843 
1844 		tmi->dec_data.d_encr_key.ck_format = CRYPTO_KEY_RAW;
1845 		tmi->dec_data.d_encr_key.ck_length = k1.ck_length;
1846 		if (tmi->dec_data.d_encr_key.ck_data == NULL)
1847 			tmi->dec_data.d_encr_key.ck_data = kmem_zalloc(
1848 				CRYPT_ARCFOUR_KEYBYTES, KM_SLEEP);
1849 
1850 		/*
1851 		 * HMAC operation creates the encryption
1852 		 * key to be used for the decrypt operations.
1853 		 */
1854 		result = do_hmac(md5_hmac_mech, &k1,
1855 			(char *)mp->b_rptr, hash->hash_len,
1856 			(char *)tmi->dec_data.d_encr_key.ck_data,
1857 			CRYPT_ARCFOUR_KEYBYTES);
1858 
1859 
1860 		if (result != CRYPTO_SUCCESS) {
1861 			cmn_err(CE_WARN,
1862 				"arcfour_hmac_md5_decrypt:  do_hmac(k3)"
1863 				"failed - error %0x", result);
1864 			goto cleanup;
1865 		}
1866 	}
1867 
1868 	tmi->dec_data.enc_tmpl = NULL;
1869 
1870 	if (tmi->dec_data.ctx == NULL &&
1871 	    (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)) {
1872 		/*
1873 		 * Only create a template if we are doing
1874 		 * chaining from block to block.
1875 		 */
1876 		result = crypto_create_ctx_template(&mech,
1877 			&tmi->dec_data.d_encr_key,
1878 			&tmi->dec_data.enc_tmpl,
1879 			KM_SLEEP);
1880 		if (result == CRYPTO_NOT_SUPPORTED) {
1881 			tmi->dec_data.enc_tmpl = NULL;
1882 		} else if (result != CRYPTO_SUCCESS) {
1883 			cmn_err(CE_WARN,
1884 				"arcfour_hmac_md5_decrypt:  "
1885 				"failed to create dec template "
1886 				"for RC4 encrypt: %0x", result);
1887 			goto cleanup;
1888 		}
1889 
1890 		result = crypto_decrypt_init(&mech,
1891 			&tmi->dec_data.d_encr_key,
1892 			tmi->dec_data.enc_tmpl,
1893 			&tmi->dec_data.ctx, NULL);
1894 
1895 		if (result != CRYPTO_SUCCESS) {
1896 			cmn_err(CE_WARN, "crypto_decrypt_init failed:"
1897 				" %0x", result);
1898 			goto cleanup;
1899 		}
1900 	}
1901 
1902 	/* adjust the rptr so we don't decrypt the original hmac field */
1903 
1904 	v1.iov_base = (char *)mp->b_rptr + hash->hash_len;
1905 	v1.iov_len = cipherlen;
1906 
1907 	indata.cd_format = CRYPTO_DATA_RAW;
1908 	indata.cd_offset = 0;
1909 	indata.cd_length = cipherlen;
1910 	indata.cd_raw = v1;
1911 
1912 	if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
1913 		result = crypto_decrypt_update(tmi->dec_data.ctx,
1914 			&indata, NULL, NULL);
1915 	else
1916 		result = crypto_decrypt(&mech, &indata,
1917 			&tmi->dec_data.d_encr_key, NULL, NULL, NULL);
1918 
1919 	if (result != CRYPTO_SUCCESS) {
1920 		cmn_err(CE_WARN, "crypto_decrypt_update failed:"
1921 			" %0x", result);
1922 		goto cleanup;
1923 	}
1924 
1925 	k2.ck_format = CRYPTO_KEY_RAW;
1926 	k2.ck_length = sizeof (k2data) * 8;
1927 	k2.ck_data = k2data;
1928 
1929 	result = do_hmac(md5_hmac_mech,
1930 			&k2,
1931 			(char *)mp->b_rptr + hash->hash_len, cipherlen,
1932 			(char *)cksum, hash->hash_len);
1933 
1934 	if (result != CRYPTO_SUCCESS) {
1935 		cmn_err(CE_WARN,
1936 			"arcfour_hmac_md5_decrypt:  do_hmac(k2)"
1937 			"failed - error %0x", result);
1938 		goto cleanup;
1939 	}
1940 
1941 	if (bcmp(cksum, mp->b_rptr, hash->hash_len) != 0) {
1942 		cmn_err(CE_WARN, "arcfour_decrypt HMAC comparison failed");
1943 		result = -1;
1944 		goto cleanup;
1945 	}
1946 
1947 	/*
1948 	 * adjust the start of the mblk to skip over the
1949 	 * hash and confounder.
1950 	 */
1951 	mp->b_rptr += hash->hash_len + hash->confound_len;
1952 
1953 cleanup:
1954 	bzero(k1data, sizeof (k1data));
1955 	bzero(k2data, sizeof (k2data));
1956 	bzero(cksum, sizeof (cksum));
1957 	bzero(saltdata, sizeof (saltdata));
1958 	if (result != CRYPTO_SUCCESS) {
1959 		mp->b_datap->db_type = M_ERROR;
1960 		mp->b_rptr = mp->b_datap->db_base;
1961 		*mp->b_rptr = EIO;
1962 		mp->b_wptr = mp->b_rptr + sizeof (char);
1963 		freemsg(mp->b_cont);
1964 		mp->b_cont = NULL;
1965 		qreply(WR(q), mp);
1966 		return (NULL);
1967 	}
1968 	return (mp);
1969 }
1970 
1971 /*
1972  * ARCFOUR-HMAC-MD5 encrypt
1973  *
1974  * format of ciphertext when using ARCFOUR-HMAC-MD5
1975  *  +-----------+------------+------------+
1976  *  |  hmac     | confounder |  msg-data  |
1977  *  +-----------+------------+------------+
1978  *
1979  */
1980 static mblk_t *
1981 arcfour_hmac_md5_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
1982 			hash_info_t *hash)
1983 {
1984 	int result;
1985 	size_t cipherlen;
1986 	size_t inlen;
1987 	size_t saltlen;
1988 	crypto_key_t k1, k2;
1989 	crypto_data_t indata;
1990 	iovec_t v1;
1991 	uchar_t ms_exp[9] = {0xab, 0xab, 0xab, 0xab, 0xab,
1992 				0xab, 0xab, 0xab, 0xab };
1993 	uchar_t k1data[CRYPT_ARCFOUR_KEYBYTES];
1994 	uchar_t k2data[CRYPT_ARCFOUR_KEYBYTES];
1995 	uchar_t saltdata[CRYPT_ARCFOUR_KEYBYTES];
1996 	crypto_mechanism_t mech;
1997 	int usage;
1998 
1999 	bzero(&indata, sizeof (indata));
2000 
2001 	/* The usage constant is 1026 for all "old" rcmd mode operations */
2002 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V1)
2003 		usage = RCMDV1_USAGE;
2004 	else
2005 		usage = ARCFOUR_ENCRYPT_USAGE;
2006 
2007 	mech.cm_type = tmi->enc_data.mech_type;
2008 	mech.cm_param = NULL;
2009 	mech.cm_param_len = 0;
2010 
2011 	/*
2012 	 * The size at this point should be the size of
2013 	 * all the plaintext plus the optional plaintext length
2014 	 * needed for RCMD V2 mode.  There should also be room
2015 	 * at the head of the mblk for the confounder and hash info.
2016 	 */
2017 	inlen = (size_t)MBLKL(mp);
2018 
2019 	cipherlen = encrypt_size(&tmi->enc_data, inlen);
2020 
2021 	ASSERT(MBLKSIZE(mp) >= cipherlen);
2022 
2023 	/*
2024 	 * Shift the rptr back enough to insert
2025 	 * the confounder and hash.
2026 	 */
2027 	mp->b_rptr -= (hash->confound_len + hash->hash_len);
2028 
2029 	/* zero out the hash area */
2030 	bzero(mp->b_rptr, (size_t)hash->hash_len);
2031 
2032 	if (cipherlen > inlen) {
2033 		bzero(mp->b_wptr, MBLKTAIL(mp));
2034 	}
2035 
2036 	if (tmi->enc_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
2037 		bcopy(ARCFOUR_EXP_SALT, saltdata, strlen(ARCFOUR_EXP_SALT));
2038 		saltdata[9] = 0;
2039 		saltdata[10] = usage & 0xff;
2040 		saltdata[11] = (usage >> 8) & 0xff;
2041 		saltdata[12] = (usage >> 16) & 0xff;
2042 		saltdata[13] = (usage >> 24) & 0xff;
2043 		saltlen = 14;
2044 	} else {
2045 		saltdata[0] = usage & 0xff;
2046 		saltdata[1] = (usage >> 8) & 0xff;
2047 		saltdata[2] = (usage >> 16) & 0xff;
2048 		saltdata[3] = (usage >> 24) & 0xff;
2049 		saltlen = 4;
2050 	}
2051 	/*
2052 	 * Use the salt value to create a key to be used
2053 	 * for subsequent HMAC operations.
2054 	 */
2055 	result = do_hmac(md5_hmac_mech,
2056 			tmi->enc_data.ckey,
2057 			(char *)saltdata, saltlen,
2058 			(char *)k1data, sizeof (k1data));
2059 	if (result != CRYPTO_SUCCESS) {
2060 		cmn_err(CE_WARN,
2061 			"arcfour_hmac_md5_encrypt:  do_hmac(k1)"
2062 			"failed - error %0x", result);
2063 		goto cleanup;
2064 	}
2065 
2066 	bcopy(k1data, k2data, sizeof (k2data));
2067 
2068 	/*
2069 	 * For the neutered MS RC4 encryption type,
2070 	 * set the trailing 9 bytes to 0xab per the
2071 	 * RC4-HMAC spec.
2072 	 */
2073 	if (tmi->enc_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
2074 		bcopy((void *)&k1data[7], ms_exp, sizeof (ms_exp));
2075 	}
2076 
2077 	/*
2078 	 * Get the confounder bytes.
2079 	 */
2080 	(void) random_get_pseudo_bytes(
2081 			(uint8_t *)(mp->b_rptr + hash->hash_len),
2082 			(size_t)hash->confound_len);
2083 
2084 	k2.ck_data = k2data;
2085 	k2.ck_format = CRYPTO_KEY_RAW;
2086 	k2.ck_length = sizeof (k2data) * 8;
2087 
2088 	/*
2089 	 * This writes the HMAC to the hash area in the
2090 	 * mblk.  The key used is the one just created by
2091 	 * the previous HMAC operation.
2092 	 * The data being processed is the confounder bytes
2093 	 * PLUS the input plaintext.
2094 	 */
2095 	result = do_hmac(md5_hmac_mech, &k2,
2096 			(char *)mp->b_rptr + hash->hash_len,
2097 			hash->confound_len + inlen,
2098 			(char *)mp->b_rptr, hash->hash_len);
2099 	if (result != CRYPTO_SUCCESS) {
2100 		cmn_err(CE_WARN,
2101 			"arcfour_hmac_md5_encrypt:  do_hmac(k2)"
2102 			"failed - error %0x", result);
2103 		goto cleanup;
2104 	}
2105 	/*
2106 	 * Because of the odd way that MIT uses RC4 keys
2107 	 * on the rlogin stream, we only need to create
2108 	 * this key once.
2109 	 * However, if using "old" rcmd mode, we need to do
2110 	 * it every time.
2111 	 */
2112 	if (tmi->enc_data.ctx == NULL ||
2113 	    (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V1)) {
2114 		crypto_key_t *key = &tmi->enc_data.d_encr_key;
2115 
2116 		k1.ck_data = k1data;
2117 		k1.ck_format = CRYPTO_KEY_RAW;
2118 		k1.ck_length = sizeof (k1data) * 8;
2119 
2120 		key->ck_format = CRYPTO_KEY_RAW;
2121 		key->ck_length = k1.ck_length;
2122 		if (key->ck_data == NULL)
2123 			key->ck_data = kmem_zalloc(
2124 				CRYPT_ARCFOUR_KEYBYTES, KM_SLEEP);
2125 
2126 		/*
2127 		 * The final HMAC operation creates the encryption
2128 		 * key to be used for the encrypt operation.
2129 		 */
2130 		result = do_hmac(md5_hmac_mech, &k1,
2131 			(char *)mp->b_rptr, hash->hash_len,
2132 			(char *)key->ck_data, CRYPT_ARCFOUR_KEYBYTES);
2133 
2134 		if (result != CRYPTO_SUCCESS) {
2135 			cmn_err(CE_WARN,
2136 				"arcfour_hmac_md5_encrypt:  do_hmac(k3)"
2137 				"failed - error %0x", result);
2138 			goto cleanup;
2139 		}
2140 	}
2141 
2142 	/*
2143 	 * If the context has not been initialized, do it now.
2144 	 */
2145 	if (tmi->enc_data.ctx == NULL &&
2146 	    (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)) {
2147 		/*
2148 		 * Only create a template if we are doing
2149 		 * chaining from block to block.
2150 		 */
2151 		result = crypto_create_ctx_template(&mech,
2152 				&tmi->enc_data.d_encr_key,
2153 				&tmi->enc_data.enc_tmpl,
2154 				KM_SLEEP);
2155 		if (result == CRYPTO_NOT_SUPPORTED) {
2156 			tmi->enc_data.enc_tmpl = NULL;
2157 		} else if (result != CRYPTO_SUCCESS) {
2158 			cmn_err(CE_WARN, "failed to create enc template "
2159 				"for RC4 encrypt: %0x", result);
2160 			goto cleanup;
2161 		}
2162 
2163 		result = crypto_encrypt_init(&mech,
2164 					&tmi->enc_data.d_encr_key,
2165 					tmi->enc_data.enc_tmpl,
2166 					&tmi->enc_data.ctx, NULL);
2167 		if (result != CRYPTO_SUCCESS) {
2168 			cmn_err(CE_WARN, "crypto_encrypt_init failed:"
2169 				" %0x", result);
2170 			goto cleanup;
2171 		}
2172 	}
2173 	v1.iov_base = (char *)mp->b_rptr + hash->hash_len;
2174 	v1.iov_len = hash->confound_len + inlen;
2175 
2176 	indata.cd_format = CRYPTO_DATA_RAW;
2177 	indata.cd_offset = 0;
2178 	indata.cd_length = hash->confound_len + inlen;
2179 	indata.cd_raw = v1;
2180 
2181 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
2182 		result = crypto_encrypt_update(tmi->enc_data.ctx,
2183 			&indata, NULL, NULL);
2184 	else
2185 		result = crypto_encrypt(&mech, &indata,
2186 			&tmi->enc_data.d_encr_key, NULL,
2187 			NULL, NULL);
2188 
2189 	if (result != CRYPTO_SUCCESS) {
2190 		cmn_err(CE_WARN, "crypto_encrypt_update failed: 0x%0x",
2191 			result);
2192 	}
2193 
2194 cleanup:
2195 	bzero(k1data, sizeof (k1data));
2196 	bzero(k2data, sizeof (k2data));
2197 	bzero(saltdata, sizeof (saltdata));
2198 	if (result != CRYPTO_SUCCESS) {
2199 		mp->b_datap->db_type = M_ERROR;
2200 		mp->b_rptr = mp->b_datap->db_base;
2201 		*mp->b_rptr = EIO;
2202 		mp->b_wptr = mp->b_rptr + sizeof (char);
2203 		freemsg(mp->b_cont);
2204 		mp->b_cont = NULL;
2205 		qreply(WR(q), mp);
2206 		return (NULL);
2207 	}
2208 	return (mp);
2209 }
2210 
2211 /*
2212  * DES-CBC-[HASH] encrypt
2213  *
2214  * Needed to support userland apps that must support Kerberos V5
2215  * encryption DES-CBC encryption modes.
2216  *
2217  * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
2218  *
2219  * format of ciphertext for DES-CBC functions, per RFC1510 is:
2220  *  +-----------+----------+-------------+-----+
2221  *  |confounder |  cksum   |   msg-data  | pad |
2222  *  +-----------+----------+-------------+-----+
2223  *
2224  * format of ciphertext when using DES3-SHA1-HMAC
2225  *  +-----------+----------+-------------+-----+
2226  *  |confounder |  msg-data  |   hmac    | pad |
2227  *  +-----------+----------+-------------+-----+
2228  *
2229  *  The confounder is 8 bytes of random data.
2230  *  The cksum depends on the hash being used.
2231  *   4 bytes for CRC32
2232  *  16 bytes for MD5
2233  *  20 bytes for SHA1
2234  *   0 bytes for RAW
2235  *
2236  */
2237 static mblk_t *
2238 des_cbc_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, hash_info_t *hash)
2239 {
2240 	int result;
2241 	size_t cipherlen;
2242 	size_t inlen;
2243 	size_t plainlen;
2244 
2245 	/*
2246 	 * The size at this point should be the size of
2247 	 * all the plaintext plus the optional plaintext length
2248 	 * needed for RCMD V2 mode.  There should also be room
2249 	 * at the head of the mblk for the confounder and hash info.
2250 	 */
2251 	inlen = (size_t)MBLKL(mp);
2252 
2253 	/*
2254 	 * The output size will be a multiple of 8 because this algorithm
2255 	 * only works on 8 byte chunks.
2256 	 */
2257 	cipherlen = encrypt_size(&tmi->enc_data, inlen);
2258 
2259 	ASSERT(MBLKSIZE(mp) >= cipherlen);
2260 
2261 	if (cipherlen > inlen) {
2262 		bzero(mp->b_wptr, MBLKTAIL(mp));
2263 	}
2264 
2265 	/*
2266 	 * Shift the rptr back enough to insert
2267 	 * the confounder and hash.
2268 	 */
2269 	if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
2270 		mp->b_rptr -= hash->confound_len;
2271 	} else {
2272 		mp->b_rptr -= (hash->confound_len + hash->hash_len);
2273 
2274 		/* zero out the hash area */
2275 		bzero(mp->b_rptr + hash->confound_len, (size_t)hash->hash_len);
2276 	}
2277 
2278 	/* get random confounder from our friend, the 'random' module */
2279 	if (hash->confound_len > 0) {
2280 		(void) random_get_pseudo_bytes((uint8_t *)mp->b_rptr,
2281 				    (size_t)hash->confound_len);
2282 	}
2283 
2284 	/*
2285 	 * For 3DES we calculate an HMAC later.
2286 	 */
2287 	if (tmi->enc_data.method != CRYPT_METHOD_DES3_CBC_SHA1) {
2288 		/* calculate chksum of confounder + input */
2289 		if (hash->hash_len > 0 && hash->hashfunc != NULL) {
2290 			uchar_t cksum[MAX_CKSUM_LEN];
2291 
2292 			result = hash->hashfunc(cksum, mp->b_rptr,
2293 				cipherlen);
2294 			if (result != CRYPTO_SUCCESS) {
2295 				goto failure;
2296 			}
2297 
2298 			/* put hash in place right after the confounder */
2299 			bcopy(cksum, (mp->b_rptr + hash->confound_len),
2300 			    (size_t)hash->hash_len);
2301 		}
2302 	}
2303 	/*
2304 	 * In order to support the "old" Kerberos RCMD protocol,
2305 	 * we must use the IVEC 3 different ways:
2306 	 *   IVEC_REUSE = keep using the same IV each time, this is
2307 	 *		ugly and insecure, but necessary for
2308 	 *		backwards compatibility with existing MIT code.
2309 	 *   IVEC_ONETIME = Use the ivec as initialized when the crypto
2310 	 *		was setup (see setup_crypto routine).
2311 	 *   IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
2312 	 */
2313 	if (tmi->enc_data.ivec_usage == IVEC_NEVER) {
2314 		bzero(tmi->enc_data.block, tmi->enc_data.blocklen);
2315 	} else if (tmi->enc_data.ivec_usage == IVEC_REUSE) {
2316 		bcopy(tmi->enc_data.ivec, tmi->enc_data.block,
2317 		    tmi->enc_data.blocklen);
2318 	}
2319 
2320 	if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
2321 		/*
2322 		 * The input length already included the hash size,
2323 		 * don't include this in the plaintext length
2324 		 * calculations.
2325 		 */
2326 		plainlen = cipherlen - hash->hash_len;
2327 
2328 		mp->b_wptr = mp->b_rptr + plainlen;
2329 
2330 		result = kef_encr_hmac(&tmi->enc_data,
2331 			(void *)mp, (size_t)plainlen,
2332 			(char *)(mp->b_rptr + plainlen),
2333 			hash->hash_len);
2334 	} else {
2335 		ASSERT(mp->b_rptr + cipherlen <= DB_LIM(mp));
2336 		mp->b_wptr = mp->b_rptr + cipherlen;
2337 		result = kef_crypt(&tmi->enc_data, (void *)mp,
2338 			CRYPTO_DATA_MBLK, (size_t)cipherlen,
2339 			CRYPT_ENCRYPT);
2340 	}
2341 failure:
2342 	if (result != CRYPTO_SUCCESS) {
2343 #ifdef DEBUG
2344 		cmn_err(CE_WARN,
2345 			"des_cbc_encrypt: kef_crypt encrypt "
2346 			"failed (len: %ld) - error %0x",
2347 			cipherlen, result);
2348 #endif
2349 		mp->b_datap->db_type = M_ERROR;
2350 		mp->b_rptr = mp->b_datap->db_base;
2351 		*mp->b_rptr = EIO;
2352 		mp->b_wptr = mp->b_rptr + sizeof (char);
2353 		freemsg(mp->b_cont);
2354 		mp->b_cont = NULL;
2355 		qreply(WR(q), mp);
2356 		return (NULL);
2357 	} else if (tmi->enc_data.ivec_usage == IVEC_ONETIME) {
2358 		/*
2359 		 * Because we are using KEF, we must manually
2360 		 * update our IV.
2361 		 */
2362 		bcopy(mp->b_wptr - tmi->enc_data.ivlen,
2363 			tmi->enc_data.block, tmi->enc_data.ivlen);
2364 	}
2365 	if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
2366 		mp->b_wptr = mp->b_rptr + cipherlen;
2367 	}
2368 
2369 	return (mp);
2370 }
2371 
2372 /*
2373  * des_cbc_decrypt
2374  *
2375  *
2376  * Needed to support userland apps that must support Kerberos V5
2377  * encryption DES-CBC decryption modes.
2378  *
2379  * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
2380  *
2381  * format of ciphertext for DES-CBC functions, per RFC1510 is:
2382  *  +-----------+----------+-------------+-----+
2383  *  |confounder |  cksum   |   msg-data  | pad |
2384  *  +-----------+----------+-------------+-----+
2385  *
2386  * format of ciphertext when using DES3-SHA1-HMAC
2387  *  +-----------+----------+-------------+-----+
2388  *  |confounder |  msg-data  |   hmac    | pad |
2389  *  +-----------+----------+-------------+-----+
2390  *
2391  *  The confounder is 8 bytes of random data.
2392  *  The cksum depends on the hash being used.
2393  *   4 bytes for CRC32
2394  *  16 bytes for MD5
2395  *  20 bytes for SHA1
2396  *   0 bytes for RAW
2397  *
2398  */
2399 static mblk_t *
2400 des_cbc_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, hash_info_t *hash)
2401 {
2402 	uint_t inlen, datalen;
2403 	int result = 0;
2404 	uchar_t *optr = NULL;
2405 	uchar_t cksum[MAX_CKSUM_LEN], newcksum[MAX_CKSUM_LEN];
2406 	uchar_t nextiv[DEFAULT_DES_BLOCKLEN];
2407 
2408 	/* Compute adjusted size */
2409 	inlen = MBLKL(mp);
2410 
2411 	optr = mp->b_rptr;
2412 
2413 	/*
2414 	 * In order to support the "old" Kerberos RCMD protocol,
2415 	 * we must use the IVEC 3 different ways:
2416 	 *   IVEC_REUSE = keep using the same IV each time, this is
2417 	 *		ugly and insecure, but necessary for
2418 	 *		backwards compatibility with existing MIT code.
2419 	 *   IVEC_ONETIME = Use the ivec as initialized when the crypto
2420 	 *		was setup (see setup_crypto routine).
2421 	 *   IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
2422 	 */
2423 	if (tmi->dec_data.ivec_usage == IVEC_NEVER)
2424 		bzero(tmi->dec_data.block, tmi->dec_data.blocklen);
2425 	else if (tmi->dec_data.ivec_usage == IVEC_REUSE)
2426 		bcopy(tmi->dec_data.ivec, tmi->dec_data.block,
2427 		    tmi->dec_data.blocklen);
2428 
2429 	if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
2430 		/*
2431 		 * Do not decrypt the HMAC at the end
2432 		 */
2433 		int decrypt_len = inlen - hash->hash_len;
2434 
2435 		/*
2436 		 * Move the wptr so the mblk appears to end
2437 		 * BEFORE the HMAC section.
2438 		 */
2439 		mp->b_wptr = mp->b_rptr + decrypt_len;
2440 
2441 		/*
2442 		 * Because we are using KEF, we must manually update our
2443 		 * IV.
2444 		 */
2445 		if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
2446 			bcopy(mp->b_rptr + decrypt_len - tmi->dec_data.ivlen,
2447 				nextiv, tmi->dec_data.ivlen);
2448 		}
2449 
2450 		result = kef_decr_hmac(&tmi->dec_data, mp, decrypt_len,
2451 			(char *)newcksum, hash->hash_len);
2452 	} else {
2453 		/*
2454 		 * Because we are using KEF, we must manually update our
2455 		 * IV.
2456 		 */
2457 		if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
2458 			bcopy(mp->b_wptr - tmi->enc_data.ivlen, nextiv,
2459 				tmi->dec_data.ivlen);
2460 		}
2461 		result = kef_crypt(&tmi->dec_data, (void *)mp,
2462 			CRYPTO_DATA_MBLK, (size_t)inlen, CRYPT_DECRYPT);
2463 	}
2464 	if (result != CRYPTO_SUCCESS) {
2465 #ifdef DEBUG
2466 		cmn_err(CE_WARN,
2467 			"des_cbc_decrypt: kef_crypt decrypt "
2468 			"failed - error %0x", result);
2469 #endif
2470 		mp->b_datap->db_type = M_ERROR;
2471 		mp->b_rptr = mp->b_datap->db_base;
2472 		*mp->b_rptr = EIO;
2473 		mp->b_wptr = mp->b_rptr + sizeof (char);
2474 		freemsg(mp->b_cont);
2475 		mp->b_cont = NULL;
2476 		qreply(WR(q), mp);
2477 		return (NULL);
2478 	}
2479 
2480 	/*
2481 	 * Manually update the IV, KEF does not track this for us.
2482 	 */
2483 	if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
2484 		bcopy(nextiv, tmi->dec_data.block, tmi->dec_data.ivlen);
2485 	}
2486 
2487 	/* Verify the checksum(if necessary) */
2488 	if (hash->hash_len > 0) {
2489 		if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
2490 			bcopy(mp->b_rptr + inlen - hash->hash_len, cksum,
2491 				hash->hash_len);
2492 		} else {
2493 			bcopy(optr + hash->confound_len, cksum, hash->hash_len);
2494 
2495 			/* zero the cksum in the buffer */
2496 			ASSERT(optr + hash->confound_len + hash->hash_len <=
2497 				DB_LIM(mp));
2498 			bzero(optr + hash->confound_len, hash->hash_len);
2499 
2500 			/* calculate MD5 chksum of confounder + input */
2501 			if (hash->hashfunc) {
2502 				(void) hash->hashfunc(newcksum, optr, inlen);
2503 			}
2504 		}
2505 
2506 		if (bcmp(cksum, newcksum, hash->hash_len)) {
2507 #ifdef DEBUG
2508 			cmn_err(CE_WARN, "des_cbc_decrypt: checksum "
2509 				"verification failed");
2510 #endif
2511 			mp->b_datap->db_type = M_ERROR;
2512 			mp->b_rptr = mp->b_datap->db_base;
2513 			*mp->b_rptr = EIO;
2514 			mp->b_wptr = mp->b_rptr + sizeof (char);
2515 			freemsg(mp->b_cont);
2516 			mp->b_cont = NULL;
2517 			qreply(WR(q), mp);
2518 			return (NULL);
2519 		}
2520 	}
2521 
2522 	datalen = inlen - hash->confound_len - hash->hash_len;
2523 
2524 	/* Move just the decrypted input into place if necessary */
2525 	if (hash->confound_len > 0 || hash->hash_len > 0) {
2526 		if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1)
2527 			mp->b_rptr += hash->confound_len;
2528 		else
2529 			mp->b_rptr += hash->confound_len + hash->hash_len;
2530 	}
2531 
2532 	ASSERT(mp->b_rptr + datalen <= DB_LIM(mp));
2533 	mp->b_wptr = mp->b_rptr + datalen;
2534 
2535 	return (mp);
2536 }
2537 
2538 static mblk_t *
2539 do_decrypt(queue_t *q, mblk_t *mp)
2540 {
2541 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
2542 	mblk_t *outmp;
2543 
2544 	switch (tmi->dec_data.method) {
2545 	case CRYPT_METHOD_DES_CFB:
2546 		outmp = des_cfb_decrypt(q, tmi, mp);
2547 		break;
2548 	case CRYPT_METHOD_NONE:
2549 		outmp = mp;
2550 		break;
2551 	case CRYPT_METHOD_DES_CBC_NULL:
2552 		outmp = des_cbc_decrypt(q, tmi, mp, &null_hash);
2553 		break;
2554 	case CRYPT_METHOD_DES_CBC_MD5:
2555 		outmp = des_cbc_decrypt(q, tmi, mp, &md5_hash);
2556 		break;
2557 	case CRYPT_METHOD_DES_CBC_CRC:
2558 		outmp = des_cbc_decrypt(q, tmi, mp, &crc32_hash);
2559 		break;
2560 	case CRYPT_METHOD_DES3_CBC_SHA1:
2561 		outmp = des_cbc_decrypt(q, tmi, mp, &sha1_hash);
2562 		break;
2563 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
2564 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
2565 		outmp = arcfour_hmac_md5_decrypt(q, tmi, mp, &md5_hash);
2566 		break;
2567 	case CRYPT_METHOD_AES128:
2568 	case CRYPT_METHOD_AES256:
2569 		outmp = aes_decrypt(q, tmi, mp, &sha1_hash);
2570 		break;
2571 	}
2572 	return (outmp);
2573 }
2574 
2575 /*
2576  * do_encrypt
2577  *
2578  * Generic encryption routine for a single message block.
2579  * The input mblk may be replaced by some encrypt routines
2580  * because they add extra data in some cases that may exceed
2581  * the input mblk_t size limit.
2582  */
2583 static mblk_t *
2584 do_encrypt(queue_t *q, mblk_t *mp)
2585 {
2586 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
2587 	mblk_t *outmp;
2588 
2589 	switch (tmi->enc_data.method) {
2590 	case CRYPT_METHOD_DES_CFB:
2591 		outmp = des_cfb_encrypt(q, tmi, mp);
2592 		break;
2593 	case CRYPT_METHOD_DES_CBC_NULL:
2594 		outmp = des_cbc_encrypt(q, tmi, mp, &null_hash);
2595 		break;
2596 	case CRYPT_METHOD_DES_CBC_MD5:
2597 		outmp = des_cbc_encrypt(q, tmi, mp, &md5_hash);
2598 		break;
2599 	case CRYPT_METHOD_DES_CBC_CRC:
2600 		outmp = des_cbc_encrypt(q, tmi, mp, &crc32_hash);
2601 		break;
2602 	case CRYPT_METHOD_DES3_CBC_SHA1:
2603 		outmp = des_cbc_encrypt(q, tmi, mp, &sha1_hash);
2604 		break;
2605 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
2606 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
2607 		outmp = arcfour_hmac_md5_encrypt(q, tmi, mp, &md5_hash);
2608 		break;
2609 	case CRYPT_METHOD_AES128:
2610 	case CRYPT_METHOD_AES256:
2611 		outmp = aes_encrypt(q, tmi, mp, &sha1_hash);
2612 		break;
2613 	case CRYPT_METHOD_NONE:
2614 		outmp = mp;
2615 		break;
2616 	}
2617 	return (outmp);
2618 }
2619 
2620 /*
2621  * setup_crypto
2622  *
2623  * This takes the data from the CRYPTIOCSETUP ioctl
2624  * and sets up a cipher_data_t structure for either
2625  * encryption or decryption.  This is where the
2626  * key and initialization vector data get stored
2627  * prior to beginning any crypto functions.
2628  *
2629  * Special note:
2630  *   Some applications(e.g. telnetd) have ability to switch
2631  * crypto on/off periodically.  Thus, the application may call
2632  * the CRYPTIOCSETUP ioctl many times for the same stream.
2633  * If the CRYPTIOCSETUP is called with 0 length key or ivec fields
2634  * assume that the key, block, and saveblock fields that are already
2635  * set from a previous CRIOCSETUP call are still valid.  This helps avoid
2636  * a rekeying error that could occur if we overwrite these fields
2637  * with each CRYPTIOCSETUP call.
2638  *   In short, sometimes, CRYPTIOCSETUP is used to simply toggle on/off
2639  * without resetting the original crypto parameters.
2640  *
2641  */
2642 static int
2643 setup_crypto(struct cr_info_t *ci, struct cipher_data_t *cd, int encrypt)
2644 {
2645 	uint_t newblocklen;
2646 	uint32_t enc_usage = 0, dec_usage = 0;
2647 	int rv;
2648 
2649 	/*
2650 	 * Initial sanity checks
2651 	 */
2652 	if (!CR_METHOD_OK(ci->crypto_method)) {
2653 		cmn_err(CE_WARN, "Illegal crypto method (%d)",
2654 			ci->crypto_method);
2655 		return (EINVAL);
2656 	}
2657 	if (!CR_OPTIONS_OK(ci->option_mask)) {
2658 		cmn_err(CE_WARN, "Illegal crypto options (%d)",
2659 			ci->option_mask);
2660 		return (EINVAL);
2661 	}
2662 	if (!CR_IVUSAGE_OK(ci->ivec_usage)) {
2663 		cmn_err(CE_WARN, "Illegal ivec usage value (%d)",
2664 			ci->ivec_usage);
2665 		return (EINVAL);
2666 	}
2667 
2668 	cd->method = ci->crypto_method;
2669 	cd->bytes = 0;
2670 
2671 	if (ci->keylen > 0) {
2672 		if (cd->key != NULL) {
2673 			kmem_free(cd->key, cd->keylen);
2674 			cd->key = NULL;
2675 			cd->keylen = 0;
2676 		}
2677 		/*
2678 		 * cd->key holds the copy of the raw key bytes passed in
2679 		 * from the userland app.
2680 		 */
2681 		cd->key = (char *)kmem_alloc((size_t)ci->keylen, KM_SLEEP);
2682 
2683 		cd->keylen = ci->keylen;
2684 		bcopy(ci->key, cd->key, (size_t)ci->keylen);
2685 	}
2686 
2687 	/*
2688 	 * Configure the block size based on the type of cipher.
2689 	 */
2690 	switch (cd->method) {
2691 		case CRYPT_METHOD_NONE:
2692 			newblocklen = 0;
2693 			break;
2694 		case CRYPT_METHOD_DES_CFB:
2695 			newblocklen = DEFAULT_DES_BLOCKLEN;
2696 			cd->mech_type = crypto_mech2id(SUN_CKM_DES_ECB);
2697 			break;
2698 		case CRYPT_METHOD_DES_CBC_NULL:
2699 		case CRYPT_METHOD_DES_CBC_MD5:
2700 		case CRYPT_METHOD_DES_CBC_CRC:
2701 			newblocklen = DEFAULT_DES_BLOCKLEN;
2702 			cd->mech_type = crypto_mech2id(SUN_CKM_DES_CBC);
2703 			break;
2704 		case CRYPT_METHOD_DES3_CBC_SHA1:
2705 			newblocklen = DEFAULT_DES_BLOCKLEN;
2706 			cd->mech_type = crypto_mech2id(SUN_CKM_DES3_CBC);
2707 			/* 3DES always uses the old usage constant */
2708 			enc_usage = RCMDV1_USAGE;
2709 			dec_usage = RCMDV1_USAGE;
2710 			break;
2711 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
2712 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
2713 			newblocklen = 0;
2714 			cd->mech_type = crypto_mech2id(SUN_CKM_RC4);
2715 			break;
2716 		case CRYPT_METHOD_AES128:
2717 		case CRYPT_METHOD_AES256:
2718 			newblocklen = DEFAULT_AES_BLOCKLEN;
2719 			cd->mech_type = crypto_mech2id(SUN_CKM_AES_ECB);
2720 			enc_usage = AES_ENCRYPT_USAGE;
2721 			dec_usage = AES_DECRYPT_USAGE;
2722 			break;
2723 	}
2724 	if (cd->mech_type == CRYPTO_MECH_INVALID) {
2725 		return (CRYPTO_FAILED);
2726 	}
2727 
2728 	/*
2729 	 * If RC4, initialize the master crypto key used by
2730 	 * the RC4 algorithm to derive the final encrypt and decrypt keys.
2731 	 */
2732 	if (cd->keylen > 0 && IS_RC4_METHOD(cd->method)) {
2733 		/*
2734 		 * cd->ckey is a kernel crypto key structure used as the
2735 		 * master key in the RC4-HMAC crypto operations.
2736 		 */
2737 		if (cd->ckey == NULL) {
2738 			cd->ckey = (crypto_key_t *)kmem_zalloc(
2739 				sizeof (crypto_key_t), KM_SLEEP);
2740 		}
2741 
2742 		cd->ckey->ck_format = CRYPTO_KEY_RAW;
2743 		cd->ckey->ck_data = cd->key;
2744 
2745 		/* key length for EF is measured in bits */
2746 		cd->ckey->ck_length = cd->keylen * 8;
2747 	}
2748 
2749 	/*
2750 	 * cd->block and cd->saveblock are used as temporary storage for
2751 	 * data that must be carried over between encrypt/decrypt operations
2752 	 * in some of the "feedback" modes.
2753 	 */
2754 	if (newblocklen != cd->blocklen) {
2755 		if (cd->block != NULL) {
2756 			kmem_free(cd->block, cd->blocklen);
2757 			cd->block = NULL;
2758 		}
2759 
2760 		if (cd->saveblock != NULL) {
2761 			kmem_free(cd->saveblock, cd->blocklen);
2762 			cd->saveblock = NULL;
2763 		}
2764 
2765 		cd->blocklen = newblocklen;
2766 		if (cd->blocklen) {
2767 			cd->block = (char *)kmem_zalloc((size_t)cd->blocklen,
2768 				KM_SLEEP);
2769 		}
2770 
2771 		if (cd->method == CRYPT_METHOD_DES_CFB)
2772 			cd->saveblock = (char *)kmem_zalloc(cd->blocklen,
2773 						KM_SLEEP);
2774 		else
2775 			cd->saveblock = NULL;
2776 	}
2777 
2778 	if (ci->iveclen != cd->ivlen) {
2779 		if (cd->ivec != NULL) {
2780 			kmem_free(cd->ivec, cd->ivlen);
2781 			cd->ivec = NULL;
2782 		}
2783 		if (ci->ivec_usage != IVEC_NEVER && ci->iveclen > 0) {
2784 			cd->ivec = (char *)kmem_zalloc((size_t)ci->iveclen,
2785 						KM_SLEEP);
2786 			cd->ivlen = ci->iveclen;
2787 		} else {
2788 			cd->ivlen = 0;
2789 			cd->ivec = NULL;
2790 		}
2791 	}
2792 	cd->option_mask = ci->option_mask;
2793 
2794 	/*
2795 	 * Old protocol requires a static 'usage' value for
2796 	 * deriving keys.  Yuk.
2797 	 */
2798 	if (cd->option_mask & CRYPTOPT_RCMD_MODE_V1) {
2799 		enc_usage = dec_usage = RCMDV1_USAGE;
2800 	}
2801 
2802 	if (cd->ivlen > cd->blocklen) {
2803 		cmn_err(CE_WARN, "setup_crypto: IV longer than block size");
2804 		return (EINVAL);
2805 	}
2806 
2807 	/*
2808 	 * If we are using an IVEC "correctly" (i.e. set it once)
2809 	 * copy it here.
2810 	 */
2811 	if (ci->ivec_usage == IVEC_ONETIME && cd->block != NULL)
2812 		bcopy(ci->ivec, cd->block, (size_t)cd->ivlen);
2813 
2814 	cd->ivec_usage = ci->ivec_usage;
2815 	if (cd->ivec != NULL) {
2816 		/* Save the original IVEC in case we need it later */
2817 		bcopy(ci->ivec, cd->ivec, (size_t)cd->ivlen);
2818 	}
2819 	/*
2820 	 * Special handling for 3DES-SHA1-HMAC and AES crypto:
2821 	 * generate derived keys and context templates
2822 	 * for better performance.
2823 	 */
2824 	if (cd->method == CRYPT_METHOD_DES3_CBC_SHA1 ||
2825 	    IS_AES_METHOD(cd->method)) {
2826 		crypto_mechanism_t enc_mech;
2827 		crypto_mechanism_t hmac_mech;
2828 
2829 		if (cd->d_encr_key.ck_data != NULL) {
2830 			bzero(cd->d_encr_key.ck_data, cd->keylen);
2831 			kmem_free(cd->d_encr_key.ck_data, cd->keylen);
2832 		}
2833 
2834 		if (cd->d_hmac_key.ck_data != NULL) {
2835 			bzero(cd->d_hmac_key.ck_data, cd->keylen);
2836 			kmem_free(cd->d_hmac_key.ck_data, cd->keylen);
2837 		}
2838 
2839 		if (cd->enc_tmpl != NULL)
2840 			(void) crypto_destroy_ctx_template(cd->enc_tmpl);
2841 
2842 		if (cd->hmac_tmpl != NULL)
2843 			(void) crypto_destroy_ctx_template(cd->hmac_tmpl);
2844 
2845 		enc_mech.cm_type = cd->mech_type;
2846 		enc_mech.cm_param = cd->ivec;
2847 		enc_mech.cm_param_len = cd->ivlen;
2848 
2849 		hmac_mech.cm_type = sha1_hmac_mech;
2850 		hmac_mech.cm_param = NULL;
2851 		hmac_mech.cm_param_len = 0;
2852 
2853 		/*
2854 		 * Create the derived keys.
2855 		 */
2856 		rv = create_derived_keys(cd,
2857 			(encrypt ? enc_usage : dec_usage),
2858 			&cd->d_encr_key, &cd->d_hmac_key);
2859 
2860 		if (rv != CRYPTO_SUCCESS) {
2861 			cmn_err(CE_WARN, "failed to create derived "
2862 				"keys: %0x", rv);
2863 			return (CRYPTO_FAILED);
2864 		}
2865 
2866 		rv = crypto_create_ctx_template(&enc_mech,
2867 					&cd->d_encr_key,
2868 					&cd->enc_tmpl, KM_SLEEP);
2869 		if (rv == CRYPTO_MECH_NOT_SUPPORTED) {
2870 			cd->enc_tmpl = NULL;
2871 		} else if (rv != CRYPTO_SUCCESS) {
2872 			cmn_err(CE_WARN, "failed to create enc template "
2873 				"for d_encr_key: %0x", rv);
2874 			return (CRYPTO_FAILED);
2875 		}
2876 
2877 		rv = crypto_create_ctx_template(&hmac_mech,
2878 				&cd->d_hmac_key,
2879 				&cd->hmac_tmpl, KM_SLEEP);
2880 		if (rv == CRYPTO_MECH_NOT_SUPPORTED) {
2881 			cd->hmac_tmpl = NULL;
2882 		} else if (rv != CRYPTO_SUCCESS) {
2883 			cmn_err(CE_WARN, "failed to create hmac template:"
2884 				" %0x", rv);
2885 			return (CRYPTO_FAILED);
2886 		}
2887 	} else if (IS_RC4_METHOD(cd->method)) {
2888 		bzero(&cd->d_encr_key, sizeof (crypto_key_t));
2889 		bzero(&cd->d_hmac_key, sizeof (crypto_key_t));
2890 		cd->ctx = NULL;
2891 		cd->enc_tmpl = NULL;
2892 		cd->hmac_tmpl = NULL;
2893 	}
2894 
2895 	/* Final sanity checks, make sure no fields are NULL */
2896 	if (cd->method != CRYPT_METHOD_NONE) {
2897 		if (cd->block == NULL && cd->blocklen > 0) {
2898 #ifdef DEBUG
2899 			cmn_err(CE_WARN,
2900 				"setup_crypto: IV block not allocated");
2901 #endif
2902 			return (ENOMEM);
2903 		}
2904 		if (cd->key == NULL && cd->keylen > 0) {
2905 #ifdef DEBUG
2906 			cmn_err(CE_WARN,
2907 				"setup_crypto: key block not allocated");
2908 #endif
2909 			return (ENOMEM);
2910 		}
2911 		if (cd->method == CRYPT_METHOD_DES_CFB &&
2912 		    cd->saveblock == NULL && cd->blocklen > 0) {
2913 #ifdef DEBUG
2914 			cmn_err(CE_WARN,
2915 				"setup_crypto: save block not allocated");
2916 #endif
2917 			return (ENOMEM);
2918 		}
2919 		if (cd->ivec == NULL && cd->ivlen > 0) {
2920 #ifdef DEBUG
2921 			cmn_err(CE_WARN,
2922 				"setup_crypto: IV not allocated");
2923 #endif
2924 			return (ENOMEM);
2925 		}
2926 	}
2927 	return (0);
2928 }
2929 
2930 /*
2931  * RCMDS require a 4 byte, clear text
2932  * length field before each message.
2933  * Add it now.
2934  */
2935 static mblk_t *
2936 mklenmp(mblk_t *bp, uint32_t len)
2937 {
2938 	mblk_t *lenmp;
2939 	uchar_t *ucp;
2940 
2941 	if (bp->b_rptr - 4 < DB_BASE(bp) || DB_REF(bp) > 1) {
2942 		lenmp = allocb(4, BPRI_MED);
2943 		if (lenmp != NULL) {
2944 			lenmp->b_rptr = lenmp->b_wptr = DB_LIM(lenmp);
2945 			linkb(lenmp, bp);
2946 			bp = lenmp;
2947 		}
2948 	}
2949 	ucp = bp->b_rptr;
2950 	*--ucp = len;
2951 	*--ucp = len >> 8;
2952 	*--ucp = len >> 16;
2953 	*--ucp = len >> 24;
2954 
2955 	bp->b_rptr = ucp;
2956 
2957 	return (bp);
2958 }
2959 
2960 static mblk_t *
2961 encrypt_block(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, size_t plainlen)
2962 {
2963 	mblk_t *newmp;
2964 	size_t headspace;
2965 
2966 	mblk_t *cbp;
2967 	size_t cipherlen;
2968 	size_t extra = 0;
2969 	uint32_t ptlen = (uint32_t)plainlen;
2970 	/*
2971 	 * If we are using the "NEW" RCMD mode,
2972 	 * add 4 bytes to the plaintext for the
2973 	 * plaintext length that gets prepended
2974 	 * before encrypting.
2975 	 */
2976 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
2977 		ptlen += 4;
2978 
2979 	cipherlen = encrypt_size(&tmi->enc_data, (size_t)ptlen);
2980 
2981 	/*
2982 	 * if we must allocb, then make sure its enough
2983 	 * to hold the length field so we dont have to allocb
2984 	 * again down below in 'mklenmp'
2985 	 */
2986 	if (ANY_RCMD_MODE(tmi->enc_data.option_mask)) {
2987 		extra = sizeof (uint32_t);
2988 	}
2989 
2990 	/*
2991 	 * Calculate how much space is needed in front of
2992 	 * the data.
2993 	 */
2994 	headspace = plaintext_offset(&tmi->enc_data);
2995 
2996 	/*
2997 	 * If the current block is too small, reallocate
2998 	 * one large enough to hold the hdr, tail, and
2999 	 * ciphertext.
3000 	 */
3001 	if ((cipherlen + extra >= MBLKSIZE(mp)) || DB_REF(mp) > 1) {
3002 		int sz = P2ROUNDUP(cipherlen+extra, 8);
3003 
3004 		cbp = allocb_tmpl(sz, mp);
3005 		if (cbp == NULL) {
3006 			cmn_err(CE_WARN,
3007 				"allocb (%d bytes) failed", sz);
3008 				return (NULL);
3009 		}
3010 
3011 		cbp->b_cont = mp->b_cont;
3012 
3013 		/*
3014 		 * headspace includes the length fields needed
3015 		 * for the RCMD modes (v1 == 4 bytes, V2 = 8)
3016 		 */
3017 		cbp->b_rptr = DB_BASE(cbp) + headspace;
3018 
3019 		ASSERT(cbp->b_rptr + P2ROUNDUP(plainlen, 8)
3020 			<= DB_LIM(cbp));
3021 
3022 		bcopy(mp->b_rptr, cbp->b_rptr, plainlen);
3023 		cbp->b_wptr = cbp->b_rptr + plainlen;
3024 
3025 		freeb(mp);
3026 	} else {
3027 		size_t extra = 0;
3028 		cbp = mp;
3029 
3030 		/*
3031 		 * Some ciphers add HMAC after the final block
3032 		 * of the ciphertext, not at the beginning like the
3033 		 * 1-DES ciphers.
3034 		 */
3035 		if (tmi->enc_data.method ==
3036 			CRYPT_METHOD_DES3_CBC_SHA1 ||
3037 		    IS_AES_METHOD(tmi->enc_data.method)) {
3038 			extra = sha1_hash.hash_len;
3039 		}
3040 
3041 		/*
3042 		 * Make sure the rptr is positioned correctly so that
3043 		 * routines later do not have to shift this data around
3044 		 */
3045 		if ((cbp->b_rptr + P2ROUNDUP(cipherlen + extra, 8) >
3046 			DB_LIM(cbp)) ||
3047 			(cbp->b_rptr - headspace < DB_BASE(cbp))) {
3048 			ovbcopy(cbp->b_rptr, DB_BASE(cbp) + headspace,
3049 				plainlen);
3050 			cbp->b_rptr = DB_BASE(cbp) + headspace;
3051 			cbp->b_wptr = cbp->b_rptr + plainlen;
3052 		}
3053 	}
3054 
3055 	ASSERT(cbp->b_rptr - headspace >= DB_BASE(cbp));
3056 	ASSERT(cbp->b_wptr <= DB_LIM(cbp));
3057 
3058 	/*
3059 	 * If using RCMD_MODE_V2 (new rcmd mode), prepend
3060 	 * the plaintext length before the actual plaintext.
3061 	 */
3062 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2) {
3063 		cbp->b_rptr -= RCMD_LEN_SZ;
3064 
3065 		/* put plaintext length at head of buffer */
3066 		*(cbp->b_rptr + 3) = (uchar_t)(plainlen & 0xff);
3067 		*(cbp->b_rptr + 2) = (uchar_t)((plainlen >> 8) & 0xff);
3068 		*(cbp->b_rptr + 1) = (uchar_t)((plainlen >> 16) & 0xff);
3069 		*(cbp->b_rptr) = (uchar_t)((plainlen >> 24) & 0xff);
3070 	}
3071 
3072 	newmp = do_encrypt(q, cbp);
3073 
3074 	if (newmp != NULL &&
3075 	    (tmi->enc_data.option_mask &
3076 	    (CRYPTOPT_RCMD_MODE_V1 | CRYPTOPT_RCMD_MODE_V2))) {
3077 		mblk_t *lp;
3078 		/*
3079 		 * Add length field, required when this is
3080 		 * used to encrypt "r*" commands(rlogin, rsh)
3081 		 * with Kerberos.
3082 		 */
3083 		lp = mklenmp(newmp, plainlen);
3084 
3085 		if (lp == NULL) {
3086 			freeb(newmp);
3087 			return (NULL);
3088 		} else {
3089 			newmp = lp;
3090 		}
3091 	}
3092 	return (newmp);
3093 }
3094 
3095 /*
3096  * encrypt_msgb
3097  *
3098  * encrypt a single message. This routine adds the
3099  * RCMD overhead bytes when necessary.
3100  */
3101 static mblk_t *
3102 encrypt_msgb(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
3103 {
3104 	size_t plainlen, outlen;
3105 	mblk_t *newmp = NULL;
3106 
3107 	/* If not encrypting, do nothing */
3108 	if (tmi->enc_data.method == CRYPT_METHOD_NONE) {
3109 		return (mp);
3110 	}
3111 
3112 	plainlen = MBLKL(mp);
3113 	if (plainlen == 0)
3114 		return (NULL);
3115 
3116 	/*
3117 	 * If the block is too big, we encrypt in 4K chunks so that
3118 	 * older rlogin clients do not choke on the larger buffers.
3119 	 */
3120 	while ((plainlen = MBLKL(mp)) > MSGBUF_SIZE) {
3121 		mblk_t *mp1 = NULL;
3122 		outlen = MSGBUF_SIZE;
3123 		/*
3124 		 * Allocate a new buffer that is only 4K bytes, the
3125 		 * extra bytes are for crypto overhead.
3126 		 */
3127 		mp1 = allocb(outlen + CONFOUNDER_BYTES, BPRI_MED);
3128 		if (mp1 == NULL) {
3129 			cmn_err(CE_WARN,
3130 				"allocb (%d bytes) failed",
3131 				(int)(outlen + CONFOUNDER_BYTES));
3132 			return (NULL);
3133 		}
3134 		/* Copy the next 4K bytes from the old block. */
3135 		bcopy(mp->b_rptr, mp1->b_rptr, outlen);
3136 		mp1->b_wptr = mp1->b_rptr + outlen;
3137 		/* Advance the old block. */
3138 		mp->b_rptr += outlen;
3139 
3140 		/* encrypt the new block */
3141 		newmp = encrypt_block(q, tmi, mp1, outlen);
3142 		if (newmp == NULL)
3143 			return (NULL);
3144 
3145 		putnext(q, newmp);
3146 	}
3147 	newmp = NULL;
3148 	/* If there is data left (< MSGBUF_SIZE), encrypt it. */
3149 	if ((plainlen = MBLKL(mp)) > 0)
3150 		newmp = encrypt_block(q, tmi, mp, plainlen);
3151 
3152 	return (newmp);
3153 }
3154 
3155 /*
3156  * cryptmodwsrv
3157  *
3158  * Service routine for the write queue.
3159  *
3160  * Because data may be placed in the queue to hold between
3161  * the CRYPTIOCSTOP and CRYPTIOCSTART ioctls, the service routine is needed.
3162  */
3163 static int
3164 cryptmodwsrv(queue_t *q)
3165 {
3166 	mblk_t *mp;
3167 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
3168 
3169 	while ((mp = getq(q)) != NULL) {
3170 		switch (mp->b_datap->db_type) {
3171 		default:
3172 			/*
3173 			 * wput does not queue anything > QPCTL
3174 			 */
3175 			if (!canputnext(q) ||
3176 			    !(tmi->ready & CRYPT_WRITE_READY)) {
3177 				if (!putbq(q, mp)) {
3178 					freemsg(mp);
3179 				}
3180 				return (0);
3181 			}
3182 			putnext(q, mp);
3183 			break;
3184 		case M_DATA:
3185 			if (canputnext(q) && (tmi->ready & CRYPT_WRITE_READY)) {
3186 				mblk_t *bp;
3187 				mblk_t *newmsg = NULL;
3188 
3189 				/*
3190 				 * If multiple msgs, concat into 1
3191 				 * to minimize crypto operations later.
3192 				 */
3193 				if (mp->b_cont != NULL) {
3194 					bp = msgpullup(mp, -1);
3195 					if (bp != NULL) {
3196 						freemsg(mp);
3197 						mp = bp;
3198 					}
3199 				}
3200 				newmsg = encrypt_msgb(q, tmi, mp);
3201 				if (newmsg != NULL)
3202 					putnext(q, newmsg);
3203 			} else {
3204 				if (!putbq(q, mp)) {
3205 					freemsg(mp);
3206 				}
3207 				return (0);
3208 			}
3209 			break;
3210 		}
3211 	}
3212 	return (0);
3213 }
3214 
3215 static void
3216 start_stream(queue_t *wq, mblk_t *mp, uchar_t dir)
3217 {
3218 	mblk_t *newmp = NULL;
3219 	struct tmodinfo *tmi = (struct tmodinfo *)wq->q_ptr;
3220 
3221 	if (dir == CRYPT_ENCRYPT) {
3222 		tmi->ready |= CRYPT_WRITE_READY;
3223 		(void) (STRLOG(CRYPTMOD_ID, 0, 5, SL_TRACE|SL_NOTE,
3224 				"start_stream: restart ENCRYPT/WRITE q"));
3225 
3226 		enableok(wq);
3227 		qenable(wq);
3228 	} else if (dir == CRYPT_DECRYPT) {
3229 		/*
3230 		 * put any extra data in the RD
3231 		 * queue to be processed and
3232 		 * sent back up.
3233 		 */
3234 		newmp = mp->b_cont;
3235 		mp->b_cont = NULL;
3236 
3237 		tmi->ready |= CRYPT_READ_READY;
3238 		(void) (STRLOG(CRYPTMOD_ID, 0, 5,
3239 				SL_TRACE|SL_NOTE,
3240 				"start_stream: restart "
3241 				"DECRYPT/READ q"));
3242 
3243 		if (newmp != NULL)
3244 			if (!putbq(RD(wq), newmp))
3245 				freemsg(newmp);
3246 
3247 		enableok(RD(wq));
3248 		qenable(RD(wq));
3249 	}
3250 
3251 	miocack(wq, mp, 0, 0);
3252 }
3253 
3254 /*
3255  * Write-side put procedure.  Its main task is to detect ioctls and
3256  * FLUSH operations.  Other message types are passed on through.
3257  */
3258 static void
3259 cryptmodwput(queue_t *wq, mblk_t *mp)
3260 {
3261 	struct iocblk *iocp;
3262 	struct tmodinfo *tmi = (struct tmodinfo *)wq->q_ptr;
3263 	int ret, err;
3264 
3265 	switch (mp->b_datap->db_type) {
3266 	case M_DATA:
3267 		if (wq->q_first == NULL && canputnext(wq) &&
3268 		    (tmi->ready & CRYPT_WRITE_READY) &&
3269 		    tmi->enc_data.method == CRYPT_METHOD_NONE) {
3270 			putnext(wq, mp);
3271 			return;
3272 		}
3273 		/* else, put it in the service queue */
3274 		if (!putq(wq, mp)) {
3275 			freemsg(mp);
3276 		}
3277 		break;
3278 	case M_FLUSH:
3279 		if (*mp->b_rptr & FLUSHW) {
3280 			flushq(wq, FLUSHDATA);
3281 		}
3282 		putnext(wq, mp);
3283 		break;
3284 	case M_IOCTL:
3285 		iocp = (struct iocblk *)mp->b_rptr;
3286 		switch (iocp->ioc_cmd) {
3287 		case CRYPTIOCSETUP:
3288 			ret = 0;
3289 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
3290 					SL_TRACE | SL_NOTE,
3291 					"wput: got CRYPTIOCSETUP "
3292 					"ioctl(%d)", iocp->ioc_cmd));
3293 
3294 			if ((err = miocpullup(mp,
3295 					sizeof (struct cr_info_t))) != 0) {
3296 				cmn_err(CE_WARN,
3297 				"wput: miocpullup failed for cr_info_t");
3298 				miocnak(wq, mp, 0, err);
3299 			} else {
3300 				struct cr_info_t *ci;
3301 				ci = (struct cr_info_t *)mp->b_cont->b_rptr;
3302 
3303 				if (ci->direction_mask & CRYPT_ENCRYPT) {
3304 				    ret = setup_crypto(ci, &tmi->enc_data, 1);
3305 				}
3306 
3307 				if (ret == 0 &&
3308 				    (ci->direction_mask & CRYPT_DECRYPT)) {
3309 				    ret = setup_crypto(ci, &tmi->dec_data, 0);
3310 				}
3311 				if (ret == 0 &&
3312 				    (ci->direction_mask & CRYPT_DECRYPT) &&
3313 				    ANY_RCMD_MODE(tmi->dec_data.option_mask)) {
3314 					bzero(&tmi->rcmd_state,
3315 					    sizeof (tmi->rcmd_state));
3316 				}
3317 				if (ret == 0) {
3318 					miocack(wq, mp, 0, 0);
3319 				} else {
3320 					cmn_err(CE_WARN,
3321 						"wput: setup_crypto failed");
3322 					miocnak(wq, mp, 0, ret);
3323 				}
3324 				(void) (STRLOG(CRYPTMOD_ID, 0, 5,
3325 						SL_TRACE|SL_NOTE,
3326 						"wput: done with SETUP "
3327 						"ioctl"));
3328 			}
3329 			break;
3330 		case CRYPTIOCSTOP:
3331 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
3332 					SL_TRACE|SL_NOTE,
3333 					"wput: got CRYPTIOCSTOP "
3334 					"ioctl(%d)", iocp->ioc_cmd));
3335 
3336 			if ((err = miocpullup(mp, sizeof (uint32_t))) != 0) {
3337 				cmn_err(CE_WARN,
3338 					"wput: CRYPTIOCSTOP ioctl wrong "
3339 					"size (%d should be %d)",
3340 					(int)iocp->ioc_count,
3341 					(int)sizeof (uint32_t));
3342 				miocnak(wq, mp, 0, err);
3343 			} else {
3344 				uint32_t *stopdir;
3345 
3346 				stopdir = (uint32_t *)mp->b_cont->b_rptr;
3347 				if (!CR_DIRECTION_OK(*stopdir)) {
3348 					miocnak(wq, mp, 0, EINVAL);
3349 					return;
3350 				}
3351 
3352 				/* disable the queues until further notice */
3353 				if (*stopdir & CRYPT_ENCRYPT) {
3354 					noenable(wq);
3355 					tmi->ready &= ~CRYPT_WRITE_READY;
3356 				}
3357 				if (*stopdir & CRYPT_DECRYPT) {
3358 					noenable(RD(wq));
3359 					tmi->ready &= ~CRYPT_READ_READY;
3360 				}
3361 
3362 				miocack(wq, mp, 0, 0);
3363 			}
3364 			break;
3365 		case CRYPTIOCSTARTDEC:
3366 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
3367 					SL_TRACE|SL_NOTE,
3368 					"wput: got CRYPTIOCSTARTDEC "
3369 					"ioctl(%d)", iocp->ioc_cmd));
3370 
3371 			start_stream(wq, mp, CRYPT_DECRYPT);
3372 			break;
3373 		case CRYPTIOCSTARTENC:
3374 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
3375 					SL_TRACE|SL_NOTE,
3376 					"wput: got CRYPTIOCSTARTENC "
3377 					"ioctl(%d)", iocp->ioc_cmd));
3378 
3379 			start_stream(wq, mp, CRYPT_ENCRYPT);
3380 			break;
3381 		default:
3382 			putnext(wq, mp);
3383 			break;
3384 		}
3385 		break;
3386 	default:
3387 		if (queclass(mp) < QPCTL) {
3388 			if (wq->q_first != NULL || !canputnext(wq)) {
3389 				if (!putq(wq, mp))
3390 					freemsg(mp);
3391 				return;
3392 			}
3393 		}
3394 		putnext(wq, mp);
3395 		break;
3396 	}
3397 }
3398 
3399 /*
3400  * decrypt_rcmd_mblks
3401  *
3402  * Because kerberized r* commands(rsh, rlogin, etc)
3403  * use a 4 byte length field to indicate the # of
3404  * PLAINTEXT bytes that are encrypted in the field
3405  * that follows, we must parse out each message and
3406  * break out the length fields prior to sending them
3407  * upstream to our Solaris r* clients/servers which do
3408  * NOT understand this format.
3409  *
3410  * Kerberized/encrypted message format:
3411  * -------------------------------
3412  * | XXXX | N bytes of ciphertext|
3413  * -------------------------------
3414  *
3415  * Where: XXXX = number of plaintext bytes that were encrypted in
3416  *               to make the ciphertext field.  This is done
3417  *               because we are using a cipher that pads out to
3418  *               an 8 byte boundary.  We only want the application
3419  *               layer to see the correct number of plain text bytes,
3420  *               not plaintext + pad.  So, after we decrypt, we
3421  *               must trim the output block down to the intended
3422  *               plaintext length and eliminate the pad bytes.
3423  *
3424  * This routine takes the entire input message, breaks it into
3425  * a new message that does not contain these length fields and
3426  * returns a message consisting of mblks filled with just ciphertext.
3427  *
3428  */
3429 static mblk_t *
3430 decrypt_rcmd_mblks(queue_t *q, mblk_t *mp)
3431 {
3432 	mblk_t *newmp = NULL;
3433 	size_t msglen;
3434 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
3435 
3436 	msglen = msgsize(mp);
3437 
3438 	/*
3439 	 * If we need the length field, get it here.
3440 	 * Test the "plaintext length" indicator.
3441 	 */
3442 	if (tmi->rcmd_state.pt_len == 0) {
3443 		uint32_t elen;
3444 		int tocopy;
3445 		mblk_t *nextp;
3446 
3447 		/*
3448 		 * Make sure we have recieved all 4 bytes of the
3449 		 * length field.
3450 		 */
3451 		while (mp != NULL) {
3452 			ASSERT(tmi->rcmd_state.cd_len < sizeof (uint32_t));
3453 
3454 			tocopy = sizeof (uint32_t) -
3455 				tmi->rcmd_state.cd_len;
3456 			if (tocopy > msglen)
3457 				tocopy = msglen;
3458 
3459 			ASSERT(mp->b_rptr + tocopy <= DB_LIM(mp));
3460 			bcopy(mp->b_rptr,
3461 				(char *)(&tmi->rcmd_state.next_len +
3462 					tmi->rcmd_state.cd_len), tocopy);
3463 
3464 			tmi->rcmd_state.cd_len += tocopy;
3465 
3466 			if (tmi->rcmd_state.cd_len >= sizeof (uint32_t)) {
3467 				tmi->rcmd_state.next_len =
3468 					ntohl(tmi->rcmd_state.next_len);
3469 				break;
3470 			}
3471 
3472 			nextp = mp->b_cont;
3473 			mp->b_cont = NULL;
3474 			freeb(mp);
3475 			mp = nextp;
3476 		}
3477 
3478 		if (mp == NULL) {
3479 			return (NULL);
3480 		}
3481 		/*
3482 		 * recalculate the msglen now that we've read the
3483 		 * length and adjusted the bufptr (b_rptr).
3484 		 */
3485 		msglen -= tocopy;
3486 		mp->b_rptr += tocopy;
3487 
3488 		tmi->rcmd_state.pt_len = tmi->rcmd_state.next_len;
3489 
3490 		if (tmi->rcmd_state.pt_len <= 0) {
3491 			/*
3492 			 * Return an IO error to break the connection. there
3493 			 * is no way to recover from this.  Usually it means
3494 			 * the app has incorrectly requested decryption on
3495 			 * a non-encrypted stream, thus the "pt_len" field
3496 			 * is negative.
3497 			 */
3498 			mp->b_datap->db_type = M_ERROR;
3499 			mp->b_rptr = mp->b_datap->db_base;
3500 			*mp->b_rptr = EIO;
3501 			mp->b_wptr = mp->b_rptr + sizeof (char);
3502 
3503 			freemsg(mp->b_cont);
3504 			mp->b_cont = NULL;
3505 			qreply(WR(q), mp);
3506 			tmi->rcmd_state.cd_len = tmi->rcmd_state.pt_len = 0;
3507 			return (NULL);
3508 		}
3509 
3510 		/*
3511 		 * If this is V2 mode, then the encrypted data is actually
3512 		 * 4 bytes bigger than the indicated len because the plaintext
3513 		 * length is encrypted for an additional security check, but
3514 		 * its not counted as part of the overall length we just read.
3515 		 * Strange and confusing, but true.
3516 		 */
3517 
3518 		if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
3519 			elen = tmi->rcmd_state.pt_len + 4;
3520 		else
3521 			elen = tmi->rcmd_state.pt_len;
3522 
3523 		tmi->rcmd_state.cd_len  = encrypt_size(&tmi->dec_data, elen);
3524 
3525 		/*
3526 		 * Allocate an mblk to hold the cipher text until it is
3527 		 * all ready to be processed.
3528 		 */
3529 		tmi->rcmd_state.c_msg = allocb(tmi->rcmd_state.cd_len,
3530 						BPRI_HI);
3531 		if (tmi->rcmd_state.c_msg == NULL) {
3532 #ifdef DEBUG
3533 			cmn_err(CE_WARN, "decrypt_rcmd_msgb: allocb failed "
3534 				"for %d bytes",
3535 				(int)tmi->rcmd_state.cd_len);
3536 #endif
3537 			/*
3538 			 * Return an IO error to break the connection.
3539 			 */
3540 			mp->b_datap->db_type = M_ERROR;
3541 			mp->b_rptr = mp->b_datap->db_base;
3542 			*mp->b_rptr = EIO;
3543 			mp->b_wptr = mp->b_rptr + sizeof (char);
3544 			freemsg(mp->b_cont);
3545 			mp->b_cont = NULL;
3546 			tmi->rcmd_state.cd_len = tmi->rcmd_state.pt_len = 0;
3547 			qreply(WR(q), mp);
3548 			return (NULL);
3549 		}
3550 	}
3551 
3552 	/*
3553 	 * If this entire message was just the length field,
3554 	 * free and return.  The actual data will probably be next.
3555 	 */
3556 	if (msglen == 0) {
3557 		freemsg(mp);
3558 		return (NULL);
3559 	}
3560 
3561 	/*
3562 	 * Copy as much of the cipher text as possible into
3563 	 * the new msgb (c_msg).
3564 	 *
3565 	 * Logic:  if we got some bytes (msglen) and we still
3566 	 * 	"need" some bytes (len-rcvd), get them here.
3567 	 */
3568 	ASSERT(tmi->rcmd_state.c_msg != NULL);
3569 	if (msglen > 0 &&
3570 	    (tmi->rcmd_state.cd_len > MBLKL(tmi->rcmd_state.c_msg))) {
3571 		mblk_t *bp, *nextp;
3572 		size_t n;
3573 
3574 		/*
3575 		 * Walk the mblks and copy just as many bytes as we need
3576 		 * for this particular block of cipher text.
3577 		 */
3578 		bp = mp;
3579 		while (bp != NULL) {
3580 			size_t needed;
3581 			size_t tocopy;
3582 			n = MBLKL(bp);
3583 
3584 			needed = tmi->rcmd_state.cd_len -
3585 				MBLKL(tmi->rcmd_state.c_msg);
3586 
3587 			tocopy = (needed >= n ? n : needed);
3588 
3589 			ASSERT(bp->b_rptr + tocopy <= DB_LIM(bp));
3590 			ASSERT(tmi->rcmd_state.c_msg->b_wptr + tocopy <=
3591 				DB_LIM(tmi->rcmd_state.c_msg));
3592 
3593 			/* Copy to end of new mblk */
3594 			bcopy(bp->b_rptr, tmi->rcmd_state.c_msg->b_wptr,
3595 				tocopy);
3596 
3597 			tmi->rcmd_state.c_msg->b_wptr += tocopy;
3598 
3599 			bp->b_rptr += tocopy;
3600 
3601 			nextp = bp->b_cont;
3602 
3603 			/*
3604 			 * If we used this whole block, free it and
3605 			 * move on.
3606 			 */
3607 			if (!MBLKL(bp)) {
3608 				freeb(bp);
3609 				bp = NULL;
3610 			}
3611 
3612 			/* If we got what we needed, stop the loop */
3613 			if (MBLKL(tmi->rcmd_state.c_msg) ==
3614 			    tmi->rcmd_state.cd_len) {
3615 				/*
3616 				 * If there is more data in the message,
3617 				 * its for another block of cipher text,
3618 				 * put it back in the queue for next time.
3619 				 */
3620 				if (bp) {
3621 					if (!putbq(q, bp))
3622 						freemsg(bp);
3623 				} else if (nextp != NULL) {
3624 					/*
3625 					 * If there is more, put it back in the
3626 					 * queue for another pass thru.
3627 					 */
3628 					if (!putbq(q, nextp))
3629 						freemsg(nextp);
3630 				}
3631 				break;
3632 			}
3633 			bp = nextp;
3634 		}
3635 	}
3636 	/*
3637 	 * Finally, if we received all the cipher text data for
3638 	 * this message, decrypt it into a new msg and send it up
3639 	 * to the app.
3640 	 */
3641 	if (tmi->rcmd_state.pt_len > 0 &&
3642 	    MBLKL(tmi->rcmd_state.c_msg) == tmi->rcmd_state.cd_len) {
3643 		mblk_t *bp;
3644 		mblk_t *newbp;
3645 
3646 		/*
3647 		 * Now we can use our msg that we created when the
3648 		 * initial message boundary was detected.
3649 		 */
3650 		bp = tmi->rcmd_state.c_msg;
3651 		tmi->rcmd_state.c_msg = NULL;
3652 
3653 		newbp = do_decrypt(q, bp);
3654 		if (newbp != NULL) {
3655 			bp = newbp;
3656 			/*
3657 			 * If using RCMD_MODE_V2 ("new" mode),
3658 			 * look at the 4 byte plaintext length that
3659 			 * was just decrypted and compare with the
3660 			 * original pt_len value that was received.
3661 			 */
3662 			if (tmi->dec_data.option_mask &
3663 			    CRYPTOPT_RCMD_MODE_V2) {
3664 				uint32_t pt_len2;
3665 
3666 				pt_len2 = *(uint32_t *)bp->b_rptr;
3667 				pt_len2 = ntohl(pt_len2);
3668 				/*
3669 				 * Make sure the 2 pt len fields agree.
3670 				 */
3671 				if (pt_len2 != tmi->rcmd_state.pt_len) {
3672 					cmn_err(CE_WARN,
3673 						"Inconsistent length fields"
3674 						" received %d != %d",
3675 						(int)tmi->rcmd_state.pt_len,
3676 						(int)pt_len2);
3677 					bp->b_datap->db_type = M_ERROR;
3678 					bp->b_rptr = bp->b_datap->db_base;
3679 					*bp->b_rptr = EIO;
3680 					bp->b_wptr = bp->b_rptr + sizeof (char);
3681 					freemsg(bp->b_cont);
3682 					bp->b_cont = NULL;
3683 					tmi->rcmd_state.cd_len = 0;
3684 					qreply(WR(q), bp);
3685 					return (NULL);
3686 				}
3687 				bp->b_rptr += sizeof (uint32_t);
3688 			}
3689 
3690 			/*
3691 			 * Trim the decrypted block the length originally
3692 			 * indicated by the sender.  This is to remove any
3693 			 * padding bytes that the sender added to satisfy
3694 			 * requirements of the crypto algorithm.
3695 			 */
3696 			bp->b_wptr = bp->b_rptr + tmi->rcmd_state.pt_len;
3697 
3698 			newmp = bp;
3699 
3700 			/*
3701 			 * Reset our state to indicate we are ready
3702 			 * for a new message.
3703 			 */
3704 			tmi->rcmd_state.pt_len = 0;
3705 			tmi->rcmd_state.cd_len = 0;
3706 		} else {
3707 #ifdef DEBUG
3708 			cmn_err(CE_WARN,
3709 				"decrypt_rcmd: do_decrypt on %d bytes failed",
3710 				(int)tmi->rcmd_state.cd_len);
3711 #endif
3712 			/*
3713 			 * do_decrypt already handled failures, just
3714 			 * return NULL.
3715 			 */
3716 			tmi->rcmd_state.pt_len = 0;
3717 			tmi->rcmd_state.cd_len = 0;
3718 			return (NULL);
3719 		}
3720 	}
3721 
3722 	/*
3723 	 * return the new message with the 'length' fields removed
3724 	 */
3725 	return (newmp);
3726 }
3727 
3728 /*
3729  * cryptmodrsrv
3730  *
3731  * Read queue service routine
3732  * Necessary because if the ready flag is not set
3733  * (via CRYPTIOCSTOP/CRYPTIOCSTART ioctls) then the data
3734  * must remain on queue and not be passed along.
3735  */
3736 static int
3737 cryptmodrsrv(queue_t *q)
3738 {
3739 	mblk_t *mp, *bp;
3740 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
3741 
3742 	while ((mp = getq(q)) != NULL) {
3743 		switch (mp->b_datap->db_type) {
3744 		case M_DATA:
3745 			if (canputnext(q) && tmi->ready & CRYPT_READ_READY) {
3746 				/*
3747 				 * Process "rcmd" messages differently because
3748 				 * they contain a 4 byte plaintext length
3749 				 * id that needs to be removed.
3750 				 */
3751 				if (tmi->dec_data.method != CRYPT_METHOD_NONE &&
3752 				    (tmi->dec_data.option_mask &
3753 				    (CRYPTOPT_RCMD_MODE_V1 |
3754 				    CRYPTOPT_RCMD_MODE_V2))) {
3755 					mp = decrypt_rcmd_mblks(q, mp);
3756 					if (mp)
3757 						putnext(q, mp);
3758 					continue;
3759 				}
3760 				if ((bp = msgpullup(mp, -1)) != NULL) {
3761 					freemsg(mp);
3762 					if (MBLKL(bp) > 0) {
3763 						mp = do_decrypt(q, bp);
3764 						if (mp != NULL)
3765 							putnext(q, mp);
3766 					}
3767 				}
3768 			} else {
3769 				if (!putbq(q, mp)) {
3770 					freemsg(mp);
3771 				}
3772 				return (0);
3773 			}
3774 			break;
3775 		default:
3776 			/*
3777 			 * rput does not queue anything > QPCTL, so we don't
3778 			 * need to check for it here.
3779 			 */
3780 			if (!canputnext(q)) {
3781 				if (!putbq(q, mp))
3782 					freemsg(mp);
3783 				return (0);
3784 			}
3785 			putnext(q, mp);
3786 			break;
3787 		}
3788 	}
3789 	return (0);
3790 }
3791 
3792 
3793 /*
3794  * Read-side put procedure.
3795  */
3796 static void
3797 cryptmodrput(queue_t *rq, mblk_t *mp)
3798 {
3799 	switch (mp->b_datap->db_type) {
3800 	case M_DATA:
3801 		if (!putq(rq, mp)) {
3802 			freemsg(mp);
3803 		}
3804 		break;
3805 	case M_FLUSH:
3806 		if (*mp->b_rptr & FLUSHR) {
3807 			flushq(rq, FLUSHALL);
3808 		}
3809 		putnext(rq, mp);
3810 		break;
3811 	default:
3812 		if (queclass(mp) < QPCTL) {
3813 			if (rq->q_first != NULL || !canputnext(rq)) {
3814 				if (!putq(rq, mp))
3815 					freemsg(mp);
3816 				return;
3817 			}
3818 		}
3819 		putnext(rq, mp);
3820 		break;
3821 	}
3822 }
3823