xref: /onnv-gate/usr/src/uts/common/io/cryptmod.c (revision 3532:30d0bf379c1a)
10Sstevel@tonic-gate /*
23518Spk193450  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * STREAMS Crypto Module
60Sstevel@tonic-gate  *
70Sstevel@tonic-gate  * This module is used to facilitate Kerberos encryption
80Sstevel@tonic-gate  * operations for the telnet daemon and rlogin daemon.
90Sstevel@tonic-gate  * Because the Solaris telnet and rlogin daemons run mostly
100Sstevel@tonic-gate  * in-kernel via 'telmod' and 'rlmod', this module must be
110Sstevel@tonic-gate  * pushed on the STREAM *below* telmod or rlmod.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * Parts of the 3DES key derivation code are covered by the
140Sstevel@tonic-gate  * following copyright.
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
170Sstevel@tonic-gate  *
180Sstevel@tonic-gate  * All rights reserved.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * Export of this software from the United States of America may require
210Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
220Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
230Sstevel@tonic-gate  * obtain such a license before exporting.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
260Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
270Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
280Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
290Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
300Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
310Sstevel@tonic-gate  * to distribution of the software without specific, written prior
320Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
330Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
340Sstevel@tonic-gate  * or implied warranty.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
370Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
380Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/sysmacros.h>
440Sstevel@tonic-gate #include <sys/errno.h>
450Sstevel@tonic-gate #include <sys/debug.h>
460Sstevel@tonic-gate #include <sys/time.h>
470Sstevel@tonic-gate #include <sys/stropts.h>
480Sstevel@tonic-gate #include <sys/stream.h>
490Sstevel@tonic-gate #include <sys/strsubr.h>
500Sstevel@tonic-gate #include <sys/strlog.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate #include <sys/conf.h>
530Sstevel@tonic-gate #include <sys/sunddi.h>
540Sstevel@tonic-gate #include <sys/kmem.h>
550Sstevel@tonic-gate #include <sys/strsun.h>
560Sstevel@tonic-gate #include <sys/random.h>
570Sstevel@tonic-gate #include <sys/types.h>
580Sstevel@tonic-gate #include <sys/byteorder.h>
590Sstevel@tonic-gate #include <sys/cryptmod.h>
600Sstevel@tonic-gate #include <sys/crc32.h>
610Sstevel@tonic-gate #include <sys/policy.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <sys/crypto/api.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include <sys/strft.h>
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Function prototypes.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate static	int	cryptmodopen(queue_t *, dev_t *, int, int, cred_t *);
700Sstevel@tonic-gate static  void	cryptmodrput(queue_t *, mblk_t *);
710Sstevel@tonic-gate static  void	cryptmodwput(queue_t *, mblk_t *);
720Sstevel@tonic-gate static	int	cryptmodclose(queue_t *);
730Sstevel@tonic-gate static	int	cryptmodwsrv(queue_t *);
740Sstevel@tonic-gate static	int	cryptmodrsrv(queue_t *);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static mblk_t *do_encrypt(queue_t *q, mblk_t *mp);
770Sstevel@tonic-gate static mblk_t *do_decrypt(queue_t *q, mblk_t *mp);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define	CRYPTMOD_ID 5150
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	CFB_BLKSZ 8
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #define	K5CLENGTH 5
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static struct module_info	cryptmod_minfo = {
860Sstevel@tonic-gate 	CRYPTMOD_ID,	/* mi_idnum */
870Sstevel@tonic-gate 	"cryptmod",	/* mi_idname */
880Sstevel@tonic-gate 	0,		/* mi_minpsz */
890Sstevel@tonic-gate 	INFPSZ,		/* mi_maxpsz */
900Sstevel@tonic-gate 	65536,		/* mi_hiwat */
910Sstevel@tonic-gate 	1024		/* mi_lowat */
920Sstevel@tonic-gate };
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static struct qinit	cryptmod_rinit = {
950Sstevel@tonic-gate 	(int (*)())cryptmodrput,	/* qi_putp */
960Sstevel@tonic-gate 	cryptmodrsrv,	/* qi_svc */
970Sstevel@tonic-gate 	cryptmodopen,	/* qi_qopen */
980Sstevel@tonic-gate 	cryptmodclose,	/* qi_qclose */
990Sstevel@tonic-gate 	NULL,		/* qi_qadmin */
1000Sstevel@tonic-gate 	&cryptmod_minfo,	/* qi_minfo */
1010Sstevel@tonic-gate 	NULL		/* qi_mstat */
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static struct qinit	cryptmod_winit = {
1050Sstevel@tonic-gate 	(int (*)())cryptmodwput,	/* qi_putp */
1060Sstevel@tonic-gate 	cryptmodwsrv,	/* qi_srvp */
1070Sstevel@tonic-gate 	NULL,		/* qi_qopen */
1080Sstevel@tonic-gate 	NULL,		/* qi_qclose */
1090Sstevel@tonic-gate 	NULL,		/* qi_qadmin */
1100Sstevel@tonic-gate 	&cryptmod_minfo,	/* qi_minfo */
1110Sstevel@tonic-gate 	NULL		/* qi_mstat */
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static struct streamtab	cryptmod_info = {
1150Sstevel@tonic-gate 	&cryptmod_rinit,	/* st_rdinit */
1160Sstevel@tonic-gate 	&cryptmod_winit,	/* st_wrinit */
1170Sstevel@tonic-gate 	NULL,	/* st_muxrinit */
1180Sstevel@tonic-gate 	NULL	/* st_muxwinit */
1190Sstevel@tonic-gate };
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate typedef struct {
1220Sstevel@tonic-gate 	uint_t hash_len;
1230Sstevel@tonic-gate 	uint_t confound_len;
1240Sstevel@tonic-gate 	int (*hashfunc)();
1250Sstevel@tonic-gate } hash_info_t;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	MAX_CKSUM_LEN 20
1280Sstevel@tonic-gate #define	CONFOUNDER_LEN 8
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	SHA1_HASHSIZE 20
1310Sstevel@tonic-gate #define	MD5_HASHSIZE 16
1320Sstevel@tonic-gate #define	CRC32_HASHSIZE 4
1333518Spk193450 #define	MSGBUF_SIZE 4096
1343518Spk193450 #define	CONFOUNDER_BYTES 128
1353518Spk193450 
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static int crc32_calc(uchar_t *, uchar_t *, uint_t);
1380Sstevel@tonic-gate static int md5_calc(uchar_t *, uchar_t *, uint_t);
1390Sstevel@tonic-gate static int sha1_calc(uchar_t *, uchar_t *, uint_t);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static hash_info_t null_hash = {0, 0, NULL};
1420Sstevel@tonic-gate static hash_info_t crc32_hash = {CRC32_HASHSIZE, CONFOUNDER_LEN, crc32_calc};
1430Sstevel@tonic-gate static hash_info_t md5_hash = {MD5_HASHSIZE, CONFOUNDER_LEN, md5_calc};
1440Sstevel@tonic-gate static hash_info_t sha1_hash = {SHA1_HASHSIZE, CONFOUNDER_LEN, sha1_calc};
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate static crypto_mech_type_t sha1_hmac_mech = CRYPTO_MECH_INVALID;
1470Sstevel@tonic-gate static crypto_mech_type_t md5_hmac_mech = CRYPTO_MECH_INVALID;
1480Sstevel@tonic-gate static crypto_mech_type_t sha1_hash_mech = CRYPTO_MECH_INVALID;
1490Sstevel@tonic-gate static crypto_mech_type_t md5_hash_mech = CRYPTO_MECH_INVALID;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static int kef_crypt(struct cipher_data_t *, void *,
1520Sstevel@tonic-gate 		    crypto_data_format_t, size_t, int);
1530Sstevel@tonic-gate static mblk_t *
1540Sstevel@tonic-gate arcfour_hmac_md5_encrypt(queue_t *, struct tmodinfo *,
1550Sstevel@tonic-gate 		mblk_t *, hash_info_t *);
1560Sstevel@tonic-gate static mblk_t *
1570Sstevel@tonic-gate arcfour_hmac_md5_decrypt(queue_t *, struct tmodinfo *,
1580Sstevel@tonic-gate 		mblk_t *, hash_info_t *);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate static int
1610Sstevel@tonic-gate do_hmac(crypto_mech_type_t, crypto_key_t *, char *, int, char *, int);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * This is the loadable module wrapper.
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate #include <sys/modctl.h>
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static struct fmodsw fsw = {
1690Sstevel@tonic-gate 	"cryptmod",
1700Sstevel@tonic-gate 	&cryptmod_info,
1710Sstevel@tonic-gate 	D_MP | D_MTQPAIR
1720Sstevel@tonic-gate };
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * Module linkage information for the kernel.
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
1780Sstevel@tonic-gate 	&mod_strmodops,
1790Sstevel@tonic-gate 	"STREAMS encryption module %I%",
1800Sstevel@tonic-gate 	&fsw
1810Sstevel@tonic-gate };
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static struct modlinkage modlinkage = {
1840Sstevel@tonic-gate 	MODREV_1,
1850Sstevel@tonic-gate 	&modlstrmod,
1860Sstevel@tonic-gate 	NULL
1870Sstevel@tonic-gate };
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate int
1900Sstevel@tonic-gate _init(void)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate int
1960Sstevel@tonic-gate _fini(void)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate int
2020Sstevel@tonic-gate _info(struct modinfo *modinfop)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate static void
2080Sstevel@tonic-gate cleanup(struct cipher_data_t *cd)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	if (cd->key != NULL) {
2110Sstevel@tonic-gate 		bzero(cd->key, cd->keylen);
2120Sstevel@tonic-gate 		kmem_free(cd->key, cd->keylen);
2130Sstevel@tonic-gate 		cd->key = NULL;
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (cd->ckey != NULL) {
2170Sstevel@tonic-gate 		/*
2180Sstevel@tonic-gate 		 * ckey is a crypto_key_t structure which references
2190Sstevel@tonic-gate 		 * "cd->key" for its raw key data.  Since that was already
2200Sstevel@tonic-gate 		 * cleared out, we don't need another "bzero" here.
2210Sstevel@tonic-gate 		 */
2220Sstevel@tonic-gate 		kmem_free(cd->ckey, sizeof (crypto_key_t));
2230Sstevel@tonic-gate 		cd->ckey = NULL;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (cd->block != NULL) {
2270Sstevel@tonic-gate 		kmem_free(cd->block, cd->blocklen);
2280Sstevel@tonic-gate 		cd->block = NULL;
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (cd->saveblock != NULL) {
2320Sstevel@tonic-gate 		kmem_free(cd->saveblock, cd->blocklen);
2330Sstevel@tonic-gate 		cd->saveblock = NULL;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	if (cd->ivec != NULL) {
2370Sstevel@tonic-gate 		kmem_free(cd->ivec, cd->ivlen);
2380Sstevel@tonic-gate 		cd->ivec = NULL;
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (cd->d_encr_key.ck_data != NULL) {
2420Sstevel@tonic-gate 		bzero(cd->d_encr_key.ck_data, cd->keylen);
2430Sstevel@tonic-gate 		kmem_free(cd->d_encr_key.ck_data, cd->keylen);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (cd->d_hmac_key.ck_data != NULL) {
2470Sstevel@tonic-gate 		bzero(cd->d_hmac_key.ck_data, cd->keylen);
2480Sstevel@tonic-gate 		kmem_free(cd->d_hmac_key.ck_data, cd->keylen);
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (cd->enc_tmpl != NULL)
2520Sstevel@tonic-gate 		(void) crypto_destroy_ctx_template(cd->enc_tmpl);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (cd->hmac_tmpl != NULL)
2550Sstevel@tonic-gate 		(void) crypto_destroy_ctx_template(cd->hmac_tmpl);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (cd->ctx != NULL) {
2580Sstevel@tonic-gate 		crypto_cancel_ctx(cd->ctx);
2590Sstevel@tonic-gate 		cd->ctx = NULL;
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /* ARGSUSED */
2640Sstevel@tonic-gate static int
2650Sstevel@tonic-gate cryptmodopen(queue_t *rq, dev_t *dev, int oflag, int sflag, cred_t *crp)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	struct tmodinfo	*tmi;
2680Sstevel@tonic-gate 	ASSERT(rq);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (sflag != MODOPEN)
2710Sstevel@tonic-gate 		return (EINVAL);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	(void) (STRLOG(CRYPTMOD_ID, 0, 5, SL_TRACE|SL_NOTE,
2740Sstevel@tonic-gate 			"cryptmodopen: opening module(PID %d)",
2750Sstevel@tonic-gate 			ddi_get_pid()));
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (rq->q_ptr != NULL) {
2780Sstevel@tonic-gate 		cmn_err(CE_WARN, "cryptmodopen: already opened");
2790Sstevel@tonic-gate 		return (0);
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/*
2830Sstevel@tonic-gate 	 * Allocate and initialize per-Stream structure.
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	tmi = (struct tmodinfo *)kmem_zalloc(sizeof (struct tmodinfo),
2860Sstevel@tonic-gate 						KM_SLEEP);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	tmi->enc_data.method = CRYPT_METHOD_NONE;
2890Sstevel@tonic-gate 	tmi->dec_data.method = CRYPT_METHOD_NONE;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	tmi->ready = (CRYPT_READ_READY | CRYPT_WRITE_READY);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = tmi;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	sha1_hmac_mech = crypto_mech2id(SUN_CKM_SHA1_HMAC);
2960Sstevel@tonic-gate 	md5_hmac_mech = crypto_mech2id(SUN_CKM_MD5_HMAC);
2970Sstevel@tonic-gate 	sha1_hash_mech = crypto_mech2id(SUN_CKM_SHA1);
2980Sstevel@tonic-gate 	md5_hash_mech = crypto_mech2id(SUN_CKM_MD5);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	qprocson(rq);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	return (0);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate static int
3060Sstevel@tonic-gate cryptmodclose(queue_t *rq)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)rq->q_ptr;
3090Sstevel@tonic-gate 	ASSERT(tmi);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	qprocsoff(rq);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	cleanup(&tmi->enc_data);
3140Sstevel@tonic-gate 	cleanup(&tmi->dec_data);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	kmem_free(tmi, sizeof (struct tmodinfo));
3170Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = NULL;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	return (0);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate  * plaintext_offset
3240Sstevel@tonic-gate  *
3250Sstevel@tonic-gate  * Calculate exactly how much space is needed in front
3260Sstevel@tonic-gate  * of the "plaintext" in an mbuf so it can be positioned
3270Sstevel@tonic-gate  * 1 time instead of potentially moving the data multiple
3280Sstevel@tonic-gate  * times.
3290Sstevel@tonic-gate  */
3300Sstevel@tonic-gate static int
3310Sstevel@tonic-gate plaintext_offset(struct cipher_data_t *cd)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	int headspace = 0;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/* 4 byte length prepended to all RCMD msgs */
3360Sstevel@tonic-gate 	if (ANY_RCMD_MODE(cd->option_mask))
3370Sstevel@tonic-gate 		headspace += RCMD_LEN_SZ;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	/* RCMD V2 mode adds an additional 4 byte plaintext length */
3400Sstevel@tonic-gate 	if (cd->option_mask & CRYPTOPT_RCMD_MODE_V2)
3410Sstevel@tonic-gate 		headspace += RCMD_LEN_SZ;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/* Need extra space for hash and counfounder */
3440Sstevel@tonic-gate 	switch (cd->method) {
3450Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_NULL:
3460Sstevel@tonic-gate 		headspace += null_hash.hash_len + null_hash.confound_len;
3470Sstevel@tonic-gate 		break;
3480Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_CRC:
3490Sstevel@tonic-gate 		headspace += crc32_hash.hash_len + crc32_hash.confound_len;
3500Sstevel@tonic-gate 		break;
3510Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_MD5:
3520Sstevel@tonic-gate 		headspace += md5_hash.hash_len + md5_hash.confound_len;
3530Sstevel@tonic-gate 		break;
3540Sstevel@tonic-gate 	case CRYPT_METHOD_DES3_CBC_SHA1:
3550Sstevel@tonic-gate 		headspace += sha1_hash.confound_len;
3560Sstevel@tonic-gate 		break;
3570Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
3580Sstevel@tonic-gate 		headspace += md5_hash.hash_len + md5_hash.confound_len;
3590Sstevel@tonic-gate 		break;
3600Sstevel@tonic-gate 	case CRYPT_METHOD_AES128:
3610Sstevel@tonic-gate 	case CRYPT_METHOD_AES256:
3620Sstevel@tonic-gate 		headspace += DEFAULT_AES_BLOCKLEN;
3630Sstevel@tonic-gate 		break;
3640Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CFB:
3650Sstevel@tonic-gate 	case CRYPT_METHOD_NONE:
3660Sstevel@tonic-gate 		break;
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	return (headspace);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate /*
3720Sstevel@tonic-gate  * encrypt_size
3730Sstevel@tonic-gate  *
3740Sstevel@tonic-gate  * Calculate the resulting size when encrypting 'plainlen' bytes
3750Sstevel@tonic-gate  * of data.
3760Sstevel@tonic-gate  */
3770Sstevel@tonic-gate static size_t
3780Sstevel@tonic-gate encrypt_size(struct cipher_data_t *cd, size_t plainlen)
3790Sstevel@tonic-gate {
3800Sstevel@tonic-gate 	size_t cipherlen;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	switch (cd->method) {
3830Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_NULL:
3840Sstevel@tonic-gate 		cipherlen = (size_t)P2ROUNDUP(null_hash.hash_len +
3850Sstevel@tonic-gate 					    plainlen, 8);
3860Sstevel@tonic-gate 		break;
3870Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_MD5:
3880Sstevel@tonic-gate 		cipherlen = (size_t)P2ROUNDUP(md5_hash.hash_len +
3890Sstevel@tonic-gate 					    md5_hash.confound_len +
3900Sstevel@tonic-gate 					    plainlen, 8);
3910Sstevel@tonic-gate 		break;
3920Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_CRC:
3930Sstevel@tonic-gate 		cipherlen = (size_t)P2ROUNDUP(crc32_hash.hash_len +
3940Sstevel@tonic-gate 					    crc32_hash.confound_len +
3950Sstevel@tonic-gate 					    plainlen, 8);
3960Sstevel@tonic-gate 		break;
3970Sstevel@tonic-gate 	case CRYPT_METHOD_DES3_CBC_SHA1:
3980Sstevel@tonic-gate 		cipherlen = (size_t)P2ROUNDUP(sha1_hash.confound_len +
3990Sstevel@tonic-gate 					    plainlen, 8) +
4000Sstevel@tonic-gate 					    sha1_hash.hash_len;
4010Sstevel@tonic-gate 		break;
4020Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
4030Sstevel@tonic-gate 		cipherlen = (size_t)P2ROUNDUP(md5_hash.confound_len +
4040Sstevel@tonic-gate 				plainlen, 1) + md5_hash.hash_len;
4050Sstevel@tonic-gate 		break;
4060Sstevel@tonic-gate 	case CRYPT_METHOD_AES128:
4070Sstevel@tonic-gate 	case CRYPT_METHOD_AES256:
4080Sstevel@tonic-gate 		/* No roundup for AES-CBC-CTS */
4090Sstevel@tonic-gate 		cipherlen = DEFAULT_AES_BLOCKLEN + plainlen +
4100Sstevel@tonic-gate 			AES_TRUNCATED_HMAC_LEN;
4110Sstevel@tonic-gate 		break;
4120Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CFB:
4130Sstevel@tonic-gate 	case CRYPT_METHOD_NONE:
4140Sstevel@tonic-gate 		cipherlen = plainlen;
4150Sstevel@tonic-gate 		break;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	return (cipherlen);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * des_cfb_encrypt
4230Sstevel@tonic-gate  *
4240Sstevel@tonic-gate  * Encrypt the mblk data using DES with cipher feedback.
4250Sstevel@tonic-gate  *
4260Sstevel@tonic-gate  * Given that V[i] is the initial 64 bit vector, V[n] is the nth 64 bit
4270Sstevel@tonic-gate  * vector, D[n] is the nth chunk of 64 bits of data to encrypt
4280Sstevel@tonic-gate  * (decrypt), and O[n] is the nth chunk of 64 bits of encrypted
4290Sstevel@tonic-gate  * (decrypted) data, then:
4300Sstevel@tonic-gate  *
4310Sstevel@tonic-gate  *  V[0] = DES(V[i], key)
4320Sstevel@tonic-gate  *  O[n] = D[n] <exclusive or > V[n]
4330Sstevel@tonic-gate  *  V[n+1] = DES(O[n], key)
4340Sstevel@tonic-gate  *
4350Sstevel@tonic-gate  * The size of the message being encrypted does not change in this
4360Sstevel@tonic-gate  * algorithm, num_bytes in == num_bytes out.
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate static mblk_t *
4390Sstevel@tonic-gate des_cfb_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	int savedbytes;
4420Sstevel@tonic-gate 	char *iptr, *optr, *lastoutput;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	lastoutput = optr = (char *)mp->b_rptr;
4450Sstevel@tonic-gate 	iptr = (char *)mp->b_rptr;
4460Sstevel@tonic-gate 	savedbytes = tmi->enc_data.bytes % CFB_BLKSZ;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	while (iptr < (char *)mp->b_wptr) {
4490Sstevel@tonic-gate 		/*
4500Sstevel@tonic-gate 		 * Do DES-ECB.
4510Sstevel@tonic-gate 		 * The first time this runs, the 'tmi->enc_data.block' will
4520Sstevel@tonic-gate 		 * contain the initialization vector that should have been
4530Sstevel@tonic-gate 		 * passed in with the SETUP ioctl.
4540Sstevel@tonic-gate 		 *
4550Sstevel@tonic-gate 		 * V[n] = DES(V[n-1], key)
4560Sstevel@tonic-gate 		 */
4570Sstevel@tonic-gate 		if (!(tmi->enc_data.bytes % CFB_BLKSZ)) {
4580Sstevel@tonic-gate 			int retval = 0;
4590Sstevel@tonic-gate 			retval = kef_crypt(&tmi->enc_data,
4600Sstevel@tonic-gate 					tmi->enc_data.block,
4610Sstevel@tonic-gate 					CRYPTO_DATA_RAW,
4620Sstevel@tonic-gate 					tmi->enc_data.blocklen,
4630Sstevel@tonic-gate 					CRYPT_ENCRYPT);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 			if (retval != CRYPTO_SUCCESS) {
4660Sstevel@tonic-gate #ifdef DEBUG
4670Sstevel@tonic-gate 				cmn_err(CE_WARN, "des_cfb_encrypt: kef_crypt "
4680Sstevel@tonic-gate 					"failed - error 0x%0x", retval);
4690Sstevel@tonic-gate #endif
4700Sstevel@tonic-gate 				mp->b_datap->db_type = M_ERROR;
4710Sstevel@tonic-gate 				mp->b_rptr = mp->b_datap->db_base;
4720Sstevel@tonic-gate 				*mp->b_rptr = EIO;
4730Sstevel@tonic-gate 				mp->b_wptr = mp->b_rptr + sizeof (char);
4740Sstevel@tonic-gate 				freemsg(mp->b_cont);
4750Sstevel@tonic-gate 				mp->b_cont = NULL;
4760Sstevel@tonic-gate 				qreply(WR(q), mp);
4770Sstevel@tonic-gate 				return (NULL);
4780Sstevel@tonic-gate 			}
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 		/* O[n] = I[n] ^ V[n] */
4820Sstevel@tonic-gate 		*(optr++) = *(iptr++) ^
4830Sstevel@tonic-gate 		    tmi->enc_data.block[tmi->enc_data.bytes % CFB_BLKSZ];
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		tmi->enc_data.bytes++;
4860Sstevel@tonic-gate 		/*
4870Sstevel@tonic-gate 		 * Feedback the encrypted output as the input to next DES call.
4880Sstevel@tonic-gate 		 */
4890Sstevel@tonic-gate 		if (!(tmi->enc_data.bytes % CFB_BLKSZ)) {
4900Sstevel@tonic-gate 			char *dbptr = tmi->enc_data.block;
4910Sstevel@tonic-gate 			/*
4920Sstevel@tonic-gate 			 * Get the last bits of input from the previous
4930Sstevel@tonic-gate 			 * msg block that we haven't yet used as feedback input.
4940Sstevel@tonic-gate 			 */
4950Sstevel@tonic-gate 			if (savedbytes > 0) {
4960Sstevel@tonic-gate 				bcopy(tmi->enc_data.saveblock,
4970Sstevel@tonic-gate 				    dbptr, (size_t)savedbytes);
4980Sstevel@tonic-gate 				dbptr += savedbytes;
4990Sstevel@tonic-gate 			}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 			/*
5020Sstevel@tonic-gate 			 * Now copy the correct bytes from the current input
5030Sstevel@tonic-gate 			 * stream and update the 'lastoutput' ptr
5040Sstevel@tonic-gate 			 */
5050Sstevel@tonic-gate 			bcopy(lastoutput, dbptr,
5060Sstevel@tonic-gate 				(size_t)(CFB_BLKSZ - savedbytes));
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 			lastoutput += (CFB_BLKSZ - savedbytes);
5090Sstevel@tonic-gate 			savedbytes = 0;
5100Sstevel@tonic-gate 		}
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * If there are bytes of input here that we need in the next
5140Sstevel@tonic-gate 	 * block to build an ivec, save them off here.
5150Sstevel@tonic-gate 	 */
5160Sstevel@tonic-gate 	if (lastoutput < optr) {
5170Sstevel@tonic-gate 		bcopy(lastoutput,
5180Sstevel@tonic-gate 		    tmi->enc_data.saveblock + savedbytes,
5190Sstevel@tonic-gate 		    (uint_t)(optr - lastoutput));
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	return (mp);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate  * des_cfb_decrypt
5260Sstevel@tonic-gate  *
5270Sstevel@tonic-gate  * Decrypt the data in the mblk using DES in Cipher Feedback mode
5280Sstevel@tonic-gate  *
5290Sstevel@tonic-gate  * # bytes in == # bytes out, no padding, confounding, or hashing
5300Sstevel@tonic-gate  * is added.
5310Sstevel@tonic-gate  *
5320Sstevel@tonic-gate  */
5330Sstevel@tonic-gate static mblk_t *
5340Sstevel@tonic-gate des_cfb_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate 	uint_t len;
5370Sstevel@tonic-gate 	uint_t savedbytes;
5380Sstevel@tonic-gate 	char *iptr;
5390Sstevel@tonic-gate 	char *lastinput;
5400Sstevel@tonic-gate 	uint_t cp;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	len = MBLKL(mp);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/* decrypted output goes into the new data buffer */
5450Sstevel@tonic-gate 	lastinput = iptr = (char *)mp->b_rptr;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	savedbytes = tmi->dec_data.bytes % tmi->dec_data.blocklen;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
5500Sstevel@tonic-gate 	 * Save the input CFB_BLKSZ bytes at a time.
5510Sstevel@tonic-gate 	 * We are trying to decrypt in-place, but need to keep
5520Sstevel@tonic-gate 	 * a small sliding window of encrypted text to be
5530Sstevel@tonic-gate 	 * used to construct the feedback buffer.
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 	cp = ((tmi->dec_data.blocklen - savedbytes) > len ? len :
5560Sstevel@tonic-gate 		tmi->dec_data.blocklen - savedbytes);
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	bcopy(lastinput, tmi->dec_data.saveblock + savedbytes, cp);
5590Sstevel@tonic-gate 	savedbytes += cp;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	lastinput += cp;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	while (iptr < (char *)mp->b_wptr) {
5640Sstevel@tonic-gate 		/*
5650Sstevel@tonic-gate 		 * Do DES-ECB.
5660Sstevel@tonic-gate 		 * The first time this runs, the 'tmi->dec_data.block' will
5670Sstevel@tonic-gate 		 * contain the initialization vector that should have been
5680Sstevel@tonic-gate 		 * passed in with the SETUP ioctl.
5690Sstevel@tonic-gate 		 */
5700Sstevel@tonic-gate 		if (!(tmi->dec_data.bytes % CFB_BLKSZ)) {
5710Sstevel@tonic-gate 			int retval;
5720Sstevel@tonic-gate 			retval = kef_crypt(&tmi->dec_data,
5730Sstevel@tonic-gate 					tmi->dec_data.block,
5740Sstevel@tonic-gate 					CRYPTO_DATA_RAW,
5750Sstevel@tonic-gate 					tmi->dec_data.blocklen,
5760Sstevel@tonic-gate 					CRYPT_ENCRYPT);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 			if (retval != CRYPTO_SUCCESS) {
5790Sstevel@tonic-gate #ifdef DEBUG
5800Sstevel@tonic-gate 				cmn_err(CE_WARN, "des_cfb_decrypt: kef_crypt "
5810Sstevel@tonic-gate 					"failed - status 0x%0x", retval);
5820Sstevel@tonic-gate #endif
5830Sstevel@tonic-gate 				mp->b_datap->db_type = M_ERROR;
5840Sstevel@tonic-gate 				mp->b_rptr = mp->b_datap->db_base;
5850Sstevel@tonic-gate 				*mp->b_rptr = EIO;
5860Sstevel@tonic-gate 				mp->b_wptr = mp->b_rptr + sizeof (char);
5870Sstevel@tonic-gate 				freemsg(mp->b_cont);
5880Sstevel@tonic-gate 				mp->b_cont = NULL;
5890Sstevel@tonic-gate 				qreply(WR(q), mp);
5900Sstevel@tonic-gate 				return (NULL);
5910Sstevel@tonic-gate 			}
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 		/*
5950Sstevel@tonic-gate 		 * To decrypt, XOR the input with the output from the DES call
5960Sstevel@tonic-gate 		 */
5970Sstevel@tonic-gate 		*(iptr++) ^= tmi->dec_data.block[tmi->dec_data.bytes %
5980Sstevel@tonic-gate 				CFB_BLKSZ];
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 		tmi->dec_data.bytes++;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 		/*
6030Sstevel@tonic-gate 		 * Feedback the encrypted input for next DES call.
6040Sstevel@tonic-gate 		 */
6050Sstevel@tonic-gate 		if (!(tmi->dec_data.bytes % tmi->dec_data.blocklen)) {
6060Sstevel@tonic-gate 			char *dbptr = tmi->dec_data.block;
6070Sstevel@tonic-gate 			/*
6080Sstevel@tonic-gate 			 * Get the last bits of input from the previous block
6090Sstevel@tonic-gate 			 * that we haven't yet processed.
6100Sstevel@tonic-gate 			 */
6110Sstevel@tonic-gate 			if (savedbytes > 0) {
6120Sstevel@tonic-gate 				bcopy(tmi->dec_data.saveblock,
6130Sstevel@tonic-gate 				    dbptr, savedbytes);
6140Sstevel@tonic-gate 				dbptr += savedbytes;
6150Sstevel@tonic-gate 			}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 			savedbytes = 0;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 			/*
6200Sstevel@tonic-gate 			 * This block makes sure that our local
6210Sstevel@tonic-gate 			 * buffer of input data is full and can
6220Sstevel@tonic-gate 			 * be accessed from the beginning.
6230Sstevel@tonic-gate 			 */
6240Sstevel@tonic-gate 			if (lastinput < (char *)mp->b_wptr) {
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 				/* How many bytes are left in the mblk? */
6270Sstevel@tonic-gate 				cp = (((char *)mp->b_wptr - lastinput) >
6280Sstevel@tonic-gate 					tmi->dec_data.blocklen ?
6290Sstevel@tonic-gate 					tmi->dec_data.blocklen :
6300Sstevel@tonic-gate 					(char *)mp->b_wptr - lastinput);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 				/* copy what we need */
6330Sstevel@tonic-gate 				bcopy(lastinput, tmi->dec_data.saveblock,
6340Sstevel@tonic-gate 					cp);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 				lastinput += cp;
6370Sstevel@tonic-gate 				savedbytes = cp;
6380Sstevel@tonic-gate 			}
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	return (mp);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate /*
6460Sstevel@tonic-gate  * crc32_calc
6470Sstevel@tonic-gate  *
6480Sstevel@tonic-gate  * Compute a CRC32 checksum on the input
6490Sstevel@tonic-gate  */
6500Sstevel@tonic-gate static int
6510Sstevel@tonic-gate crc32_calc(uchar_t *buf, uchar_t *input, uint_t len)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	uint32_t crc;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	CRC32(crc, input, len, 0, crc32_table);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	buf[0] = (uchar_t)(crc & 0xff);
6580Sstevel@tonic-gate 	buf[1] = (uchar_t)((crc >> 8) & 0xff);
6590Sstevel@tonic-gate 	buf[2] = (uchar_t)((crc >> 16) & 0xff);
6600Sstevel@tonic-gate 	buf[3] = (uchar_t)((crc >> 24) & 0xff);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate static int
6660Sstevel@tonic-gate kef_digest(crypto_mech_type_t digest_type,
6670Sstevel@tonic-gate 	uchar_t *input, uint_t inlen,
6680Sstevel@tonic-gate 	uchar_t *output, uint_t hashlen)
6690Sstevel@tonic-gate {
6700Sstevel@tonic-gate 	iovec_t v1, v2;
6710Sstevel@tonic-gate 	crypto_data_t d1, d2;
6720Sstevel@tonic-gate 	crypto_mechanism_t mech;
6730Sstevel@tonic-gate 	int rv;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	mech.cm_type = digest_type;
6760Sstevel@tonic-gate 	mech.cm_param = 0;
6770Sstevel@tonic-gate 	mech.cm_param_len = 0;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	v1.iov_base = (void *)input;
6800Sstevel@tonic-gate 	v1.iov_len = inlen;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	d1.cd_format = CRYPTO_DATA_RAW;
6830Sstevel@tonic-gate 	d1.cd_offset = 0;
6840Sstevel@tonic-gate 	d1.cd_length = v1.iov_len;
6850Sstevel@tonic-gate 	d1.cd_raw = v1;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	v2.iov_base = (void *)output;
6880Sstevel@tonic-gate 	v2.iov_len = hashlen;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	d2.cd_format = CRYPTO_DATA_RAW;
6910Sstevel@tonic-gate 	d2.cd_offset = 0;
6920Sstevel@tonic-gate 	d2.cd_length = v2.iov_len;
6930Sstevel@tonic-gate 	d2.cd_raw = v2;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	rv = crypto_digest(&mech, &d1, &d2, NULL);
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	return (rv);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate /*
7010Sstevel@tonic-gate  * sha1_calc
7020Sstevel@tonic-gate  *
7030Sstevel@tonic-gate  * Get a SHA1 hash on the input data.
7040Sstevel@tonic-gate  */
7050Sstevel@tonic-gate static int
7060Sstevel@tonic-gate sha1_calc(uchar_t *output, uchar_t *input, uint_t inlen)
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	int rv;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	rv = kef_digest(sha1_hash_mech, input, inlen, output, SHA1_HASHSIZE);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	return (rv);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate /*
7160Sstevel@tonic-gate  * Get an MD5 hash on the input data.
7170Sstevel@tonic-gate  * md5_calc
7180Sstevel@tonic-gate  *
7190Sstevel@tonic-gate  */
7200Sstevel@tonic-gate static int
7210Sstevel@tonic-gate md5_calc(uchar_t *output, uchar_t *input, uint_t inlen)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	int rv;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	rv = kef_digest(md5_hash_mech, input, inlen, output, MD5_HASHSIZE);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	return (rv);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate /*
7310Sstevel@tonic-gate  * nfold
7320Sstevel@tonic-gate  * duplicate the functionality of the krb5_nfold function from
7330Sstevel@tonic-gate  * the userland kerberos mech.
7340Sstevel@tonic-gate  * This is needed to derive keys for use with 3DES/SHA1-HMAC
7350Sstevel@tonic-gate  * ciphers.
7360Sstevel@tonic-gate  */
7370Sstevel@tonic-gate static void
7380Sstevel@tonic-gate nfold(int inbits, uchar_t *in, int outbits, uchar_t *out)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	int a, b, c, lcm;
7410Sstevel@tonic-gate 	int byte, i, msbit;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	inbits >>= 3;
7440Sstevel@tonic-gate 	outbits >>= 3;
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	/* first compute lcm(n,k) */
7470Sstevel@tonic-gate 	a = outbits;
7480Sstevel@tonic-gate 	b = inbits;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	while (b != 0) {
7510Sstevel@tonic-gate 		c = b;
7520Sstevel@tonic-gate 		b = a%b;
7530Sstevel@tonic-gate 		a = c;
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	lcm = outbits*inbits/a;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/* now do the real work */
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	bzero(out, outbits);
7610Sstevel@tonic-gate 	byte = 0;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	/*
7640Sstevel@tonic-gate 	 * Compute the msbit in k which gets added into this byte
7650Sstevel@tonic-gate 	 * first, start with the msbit in the first, unrotated byte
7660Sstevel@tonic-gate 	 * then, for each byte, shift to the right for each repetition
7670Sstevel@tonic-gate 	 * last, pick out the correct byte within that shifted repetition
7680Sstevel@tonic-gate 	 */
7690Sstevel@tonic-gate 	for (i = lcm-1; i >= 0; i--) {
7700Sstevel@tonic-gate 		msbit = (((inbits<<3)-1)
7710Sstevel@tonic-gate 			+(((inbits<<3)+13)*(i/inbits))
7720Sstevel@tonic-gate 			+((inbits-(i%inbits))<<3)) %(inbits<<3);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 		/* pull out the byte value itself */
7750Sstevel@tonic-gate 		byte += (((in[((inbits-1)-(msbit>>3))%inbits]<<8)|
7760Sstevel@tonic-gate 			(in[((inbits)-(msbit>>3))%inbits]))
7770Sstevel@tonic-gate 			>>((msbit&7)+1))&0xff;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		/* do the addition */
7800Sstevel@tonic-gate 		byte += out[i%outbits];
7810Sstevel@tonic-gate 		out[i%outbits] = byte&0xff;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		byte >>= 8;
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	/* if there's a carry bit left over, add it back in */
7870Sstevel@tonic-gate 	if (byte) {
7880Sstevel@tonic-gate 		for (i = outbits-1; i >= 0; i--) {
7890Sstevel@tonic-gate 			/* do the addition */
7900Sstevel@tonic-gate 			byte += out[i];
7910Sstevel@tonic-gate 			out[i] = byte&0xff;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 			/* keep around the carry bit, if any */
7940Sstevel@tonic-gate 			byte >>= 8;
7950Sstevel@tonic-gate 		}
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate #define	smask(step) ((1<<step)-1)
8000Sstevel@tonic-gate #define	pstep(x, step) (((x)&smask(step))^(((x)>>step)&smask(step)))
8010Sstevel@tonic-gate #define	parity_char(x) pstep(pstep(pstep((x), 4), 2), 1)
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate /*
8040Sstevel@tonic-gate  * Duplicate the functionality of the "dk_derive_key" function
8050Sstevel@tonic-gate  * in the Kerberos mechanism.
8060Sstevel@tonic-gate  */
8070Sstevel@tonic-gate static int
8080Sstevel@tonic-gate derive_key(struct cipher_data_t *cdata, uchar_t *constdata,
8090Sstevel@tonic-gate 	int constlen, char *dkey, int keybytes,
8100Sstevel@tonic-gate 	int blocklen)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate 	int rv = 0;
8130Sstevel@tonic-gate 	int n = 0, i;
8140Sstevel@tonic-gate 	char *inblock;
8150Sstevel@tonic-gate 	char *rawkey;
8160Sstevel@tonic-gate 	char *zeroblock;
8170Sstevel@tonic-gate 	char *saveblock;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	inblock = kmem_zalloc(blocklen, KM_SLEEP);
8200Sstevel@tonic-gate 	rawkey = kmem_zalloc(keybytes, KM_SLEEP);
8210Sstevel@tonic-gate 	zeroblock = kmem_zalloc(blocklen, KM_SLEEP);
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	if (constlen == blocklen)
8240Sstevel@tonic-gate 		bcopy(constdata, inblock, blocklen);
8250Sstevel@tonic-gate 	else
8260Sstevel@tonic-gate 		nfold(constlen * 8, constdata,
8270Sstevel@tonic-gate 			blocklen * 8, (uchar_t *)inblock);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	/*
8300Sstevel@tonic-gate 	 * zeroblock is an IV of all 0's.
8310Sstevel@tonic-gate 	 *
8320Sstevel@tonic-gate 	 * The "block" section of the cdata record is used as the
8330Sstevel@tonic-gate 	 * IV for crypto operations in the kef_crypt function.
8340Sstevel@tonic-gate 	 *
8350Sstevel@tonic-gate 	 * We use 'block' as a generic IV data buffer because it
8360Sstevel@tonic-gate 	 * is attached to the stream state data and thus can
8370Sstevel@tonic-gate 	 * be used to hold information that must carry over
8380Sstevel@tonic-gate 	 * from processing of one mblk to another.
8390Sstevel@tonic-gate 	 *
8400Sstevel@tonic-gate 	 * Here, we save the current IV and replace it with
8410Sstevel@tonic-gate 	 * and empty IV (all 0's) for use when deriving the
8420Sstevel@tonic-gate 	 * keys.  Once the key derivation is done, we swap the
8430Sstevel@tonic-gate 	 * old IV back into place.
8440Sstevel@tonic-gate 	 */
8450Sstevel@tonic-gate 	saveblock = cdata->block;
8460Sstevel@tonic-gate 	cdata->block = zeroblock;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	while (n < keybytes) {
8490Sstevel@tonic-gate 		rv = kef_crypt(cdata, inblock, CRYPTO_DATA_RAW,
8500Sstevel@tonic-gate 				blocklen, CRYPT_ENCRYPT);
8510Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
8520Sstevel@tonic-gate 			/* put the original IV block back in place */
8530Sstevel@tonic-gate 			cdata->block = saveblock;
8540Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to derive a key: %0x", rv);
8550Sstevel@tonic-gate 			goto cleanup;
8560Sstevel@tonic-gate 		}
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		if (keybytes - n < blocklen) {
8590Sstevel@tonic-gate 			bcopy(inblock, rawkey+n, (keybytes-n));
8600Sstevel@tonic-gate 			break;
8610Sstevel@tonic-gate 		}
8620Sstevel@tonic-gate 		bcopy(inblock, rawkey+n, blocklen);
8630Sstevel@tonic-gate 		n += blocklen;
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 	/* put the original IV block back in place */
8660Sstevel@tonic-gate 	cdata->block = saveblock;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	/* finally, make the key */
8690Sstevel@tonic-gate 	if (cdata->method == CRYPT_METHOD_DES3_CBC_SHA1) {
8700Sstevel@tonic-gate 		/*
8710Sstevel@tonic-gate 		 * 3DES key derivation requires that we make sure the
8720Sstevel@tonic-gate 		 * key has the proper parity.
8730Sstevel@tonic-gate 		 */
8740Sstevel@tonic-gate 		for (i = 0; i < 3; i++) {
8750Sstevel@tonic-gate 			bcopy(rawkey+(i*7), dkey+(i*8), 7);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 			/* 'dkey' is our derived key output buffer */
8780Sstevel@tonic-gate 			dkey[i*8+7] = (((dkey[i*8]&1)<<1) |
8790Sstevel@tonic-gate 					((dkey[i*8+1]&1)<<2) |
8800Sstevel@tonic-gate 					((dkey[i*8+2]&1)<<3) |
8810Sstevel@tonic-gate 					((dkey[i*8+3]&1)<<4) |
8820Sstevel@tonic-gate 					((dkey[i*8+4]&1)<<5) |
8830Sstevel@tonic-gate 					((dkey[i*8+5]&1)<<6) |
8840Sstevel@tonic-gate 					((dkey[i*8+6]&1)<<7));
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 			for (n = 0; n < 8; n++) {
8870Sstevel@tonic-gate 				dkey[i*8 + n] &=  0xfe;
8880Sstevel@tonic-gate 				dkey[i*8 + n] |= 1^parity_char(dkey[i*8 + n]);
8890Sstevel@tonic-gate 			}
8900Sstevel@tonic-gate 		}
8910Sstevel@tonic-gate 	} else if (IS_AES_METHOD(cdata->method)) {
8920Sstevel@tonic-gate 		bcopy(rawkey, dkey, keybytes);
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate cleanup:
8950Sstevel@tonic-gate 	kmem_free(inblock, blocklen);
8960Sstevel@tonic-gate 	kmem_free(zeroblock, blocklen);
8970Sstevel@tonic-gate 	kmem_free(rawkey, keybytes);
8980Sstevel@tonic-gate 	return (rv);
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate /*
9020Sstevel@tonic-gate  * create_derived_keys
9030Sstevel@tonic-gate  *
9040Sstevel@tonic-gate  * Algorithm for deriving a new key and an HMAC key
9050Sstevel@tonic-gate  * before computing the 3DES-SHA1-HMAC operation on the plaintext
9060Sstevel@tonic-gate  * This algorithm matches the work done by Kerberos mechanism
9070Sstevel@tonic-gate  * in userland.
9080Sstevel@tonic-gate  */
9090Sstevel@tonic-gate static int
9100Sstevel@tonic-gate create_derived_keys(struct cipher_data_t *cdata, uint32_t usage,
9110Sstevel@tonic-gate 		crypto_key_t *enckey, crypto_key_t *hmackey)
9120Sstevel@tonic-gate {
9130Sstevel@tonic-gate 	uchar_t constdata[K5CLENGTH];
9140Sstevel@tonic-gate 	int keybytes;
9150Sstevel@tonic-gate 	int rv;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	constdata[0] = (usage>>24)&0xff;
9180Sstevel@tonic-gate 	constdata[1] = (usage>>16)&0xff;
9190Sstevel@tonic-gate 	constdata[2] = (usage>>8)&0xff;
9200Sstevel@tonic-gate 	constdata[3] = usage & 0xff;
9210Sstevel@tonic-gate 	/* Use "0xAA" for deriving encryption key */
9220Sstevel@tonic-gate 	constdata[4] = 0xAA; /* from MIT Kerberos code */
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	enckey->ck_length = cdata->keylen * 8;
9250Sstevel@tonic-gate 	enckey->ck_format = CRYPTO_KEY_RAW;
9260Sstevel@tonic-gate 	enckey->ck_data = kmem_zalloc(cdata->keylen, KM_SLEEP);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	switch (cdata->method) {
9290Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CFB:
9300Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CBC_NULL:
9310Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CBC_MD5:
9320Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CBC_CRC:
9330Sstevel@tonic-gate 			keybytes = 8;
9340Sstevel@tonic-gate 			break;
9350Sstevel@tonic-gate 		case CRYPT_METHOD_DES3_CBC_SHA1:
9360Sstevel@tonic-gate 			keybytes = CRYPT_DES3_KEYBYTES;
9370Sstevel@tonic-gate 			break;
9380Sstevel@tonic-gate 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
9390Sstevel@tonic-gate 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
9400Sstevel@tonic-gate 			keybytes = CRYPT_ARCFOUR_KEYBYTES;
9410Sstevel@tonic-gate 			break;
9420Sstevel@tonic-gate 		case CRYPT_METHOD_AES128:
9430Sstevel@tonic-gate 			keybytes = CRYPT_AES128_KEYBYTES;
9440Sstevel@tonic-gate 			break;
9450Sstevel@tonic-gate 		case CRYPT_METHOD_AES256:
9460Sstevel@tonic-gate 			keybytes = CRYPT_AES256_KEYBYTES;
9470Sstevel@tonic-gate 			break;
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	/* derive main crypto key */
9510Sstevel@tonic-gate 	rv = derive_key(cdata, constdata, sizeof (constdata),
9520Sstevel@tonic-gate 		enckey->ck_data, keybytes, cdata->blocklen);
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		/* Use "0x55" for deriving mac key */
9570Sstevel@tonic-gate 		constdata[4] = 0x55;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 		hmackey->ck_length = cdata->keylen * 8;
9600Sstevel@tonic-gate 		hmackey->ck_format = CRYPTO_KEY_RAW;
9610Sstevel@tonic-gate 		hmackey->ck_data = kmem_zalloc(cdata->keylen, KM_SLEEP);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 		rv = derive_key(cdata, constdata, sizeof (constdata),
9640Sstevel@tonic-gate 				hmackey->ck_data, keybytes,
9650Sstevel@tonic-gate 				cdata->blocklen);
9660Sstevel@tonic-gate 	} else {
9670Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to derive crypto key: %02x", rv);
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	return (rv);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate /*
9740Sstevel@tonic-gate  * Compute 3-DES crypto and HMAC.
9750Sstevel@tonic-gate  */
9760Sstevel@tonic-gate static int
9770Sstevel@tonic-gate kef_decr_hmac(struct cipher_data_t *cdata,
9780Sstevel@tonic-gate 	mblk_t *mp, int length,
9790Sstevel@tonic-gate 	char *hmac, int hmaclen)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	int rv = CRYPTO_FAILED;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	crypto_mechanism_t encr_mech;
9840Sstevel@tonic-gate 	crypto_mechanism_t mac_mech;
9850Sstevel@tonic-gate 	crypto_data_t dd;
9860Sstevel@tonic-gate 	crypto_data_t mac;
9870Sstevel@tonic-gate 	iovec_t v1;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	ASSERT(cdata != NULL);
9900Sstevel@tonic-gate 	ASSERT(mp != NULL);
9910Sstevel@tonic-gate 	ASSERT(hmac != NULL);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	bzero(&dd, sizeof (dd));
9940Sstevel@tonic-gate 	dd.cd_format = CRYPTO_DATA_MBLK;
9950Sstevel@tonic-gate 	dd.cd_offset = 0;
9960Sstevel@tonic-gate 	dd.cd_length = length;
9970Sstevel@tonic-gate 	dd.cd_mp = mp;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	v1.iov_base = hmac;
10000Sstevel@tonic-gate 	v1.iov_len = hmaclen;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	mac.cd_format = CRYPTO_DATA_RAW;
10030Sstevel@tonic-gate 	mac.cd_offset = 0;
10040Sstevel@tonic-gate 	mac.cd_length = hmaclen;
10050Sstevel@tonic-gate 	mac.cd_raw = v1;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	/*
10080Sstevel@tonic-gate 	 * cdata->block holds the IVEC
10090Sstevel@tonic-gate 	 */
10100Sstevel@tonic-gate 	encr_mech.cm_type = cdata->mech_type;
10110Sstevel@tonic-gate 	encr_mech.cm_param = cdata->block;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	if (cdata->block != NULL)
10140Sstevel@tonic-gate 		encr_mech.cm_param_len = cdata->blocklen;
10150Sstevel@tonic-gate 	else
10160Sstevel@tonic-gate 		encr_mech.cm_param_len = 0;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	rv = crypto_decrypt(&encr_mech, &dd, &cdata->d_encr_key,
10190Sstevel@tonic-gate 			cdata->enc_tmpl, NULL, NULL);
10200Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
10210Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_decrypt failed: %0x", rv);
10220Sstevel@tonic-gate 		return (rv);
10230Sstevel@tonic-gate 	}
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	mac_mech.cm_type = sha1_hmac_mech;
10260Sstevel@tonic-gate 	mac_mech.cm_param = NULL;
10270Sstevel@tonic-gate 	mac_mech.cm_param_len = 0;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	/*
10300Sstevel@tonic-gate 	 * Compute MAC of the plaintext decrypted above.
10310Sstevel@tonic-gate 	 */
10320Sstevel@tonic-gate 	rv = crypto_mac(&mac_mech, &dd, &cdata->d_hmac_key,
10330Sstevel@tonic-gate 			cdata->hmac_tmpl, &mac, NULL);
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
10360Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_mac failed: %0x", rv);
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	return (rv);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate /*
10430Sstevel@tonic-gate  * Compute 3-DES crypto and HMAC.
10440Sstevel@tonic-gate  */
10450Sstevel@tonic-gate static int
10460Sstevel@tonic-gate kef_encr_hmac(struct cipher_data_t *cdata,
10470Sstevel@tonic-gate 	mblk_t *mp, int length,
10480Sstevel@tonic-gate 	char *hmac, int hmaclen)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate 	int rv = CRYPTO_FAILED;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	crypto_mechanism_t encr_mech;
10530Sstevel@tonic-gate 	crypto_mechanism_t mac_mech;
10540Sstevel@tonic-gate 	crypto_data_t dd;
10550Sstevel@tonic-gate 	crypto_data_t mac;
10560Sstevel@tonic-gate 	iovec_t v1;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	ASSERT(cdata != NULL);
10590Sstevel@tonic-gate 	ASSERT(mp != NULL);
10600Sstevel@tonic-gate 	ASSERT(hmac != NULL);
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	bzero(&dd, sizeof (dd));
10630Sstevel@tonic-gate 	dd.cd_format = CRYPTO_DATA_MBLK;
10640Sstevel@tonic-gate 	dd.cd_offset = 0;
10650Sstevel@tonic-gate 	dd.cd_length = length;
10660Sstevel@tonic-gate 	dd.cd_mp = mp;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	v1.iov_base = hmac;
10690Sstevel@tonic-gate 	v1.iov_len = hmaclen;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	mac.cd_format = CRYPTO_DATA_RAW;
10720Sstevel@tonic-gate 	mac.cd_offset = 0;
10730Sstevel@tonic-gate 	mac.cd_length = hmaclen;
10740Sstevel@tonic-gate 	mac.cd_raw = v1;
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	/*
10770Sstevel@tonic-gate 	 * cdata->block holds the IVEC
10780Sstevel@tonic-gate 	 */
10790Sstevel@tonic-gate 	encr_mech.cm_type = cdata->mech_type;
10800Sstevel@tonic-gate 	encr_mech.cm_param = cdata->block;
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	if (cdata->block != NULL)
10830Sstevel@tonic-gate 		encr_mech.cm_param_len = cdata->blocklen;
10840Sstevel@tonic-gate 	else
10850Sstevel@tonic-gate 		encr_mech.cm_param_len = 0;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	mac_mech.cm_type = sha1_hmac_mech;
10880Sstevel@tonic-gate 	mac_mech.cm_param = NULL;
10890Sstevel@tonic-gate 	mac_mech.cm_param_len = 0;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	rv = crypto_mac(&mac_mech, &dd, &cdata->d_hmac_key,
10920Sstevel@tonic-gate 			cdata->hmac_tmpl, &mac, NULL);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
10950Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_mac failed: %0x", rv);
10960Sstevel@tonic-gate 		return (rv);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	rv = crypto_encrypt(&encr_mech, &dd, &cdata->d_encr_key,
11000Sstevel@tonic-gate 			cdata->enc_tmpl, NULL, NULL);
11010Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
11020Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_encrypt failed: %0x", rv);
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	return (rv);
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate /*
11090Sstevel@tonic-gate  * kef_crypt
11100Sstevel@tonic-gate  *
11110Sstevel@tonic-gate  * Use the Kernel encryption framework to provide the
11120Sstevel@tonic-gate  * crypto operations for the indicated data.
11130Sstevel@tonic-gate  */
11140Sstevel@tonic-gate static int
11150Sstevel@tonic-gate kef_crypt(struct cipher_data_t *cdata,
11160Sstevel@tonic-gate 	void *indata, crypto_data_format_t fmt,
11170Sstevel@tonic-gate 	size_t length, int mode)
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate 	int rv = CRYPTO_FAILED;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	crypto_mechanism_t mech;
11220Sstevel@tonic-gate 	crypto_key_t crkey;
11230Sstevel@tonic-gate 	iovec_t v1;
11240Sstevel@tonic-gate 	crypto_data_t d1;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	ASSERT(cdata != NULL);
11270Sstevel@tonic-gate 	ASSERT(indata != NULL);
11280Sstevel@tonic-gate 	ASSERT(fmt == CRYPTO_DATA_RAW || fmt == CRYPTO_DATA_MBLK);
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	bzero(&crkey, sizeof (crkey));
11310Sstevel@tonic-gate 	bzero(&d1, sizeof (d1));
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	crkey.ck_format = CRYPTO_KEY_RAW;
11340Sstevel@tonic-gate 	crkey.ck_data =  cdata->key;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	/* keys are measured in bits, not bytes, so multiply by 8 */
11370Sstevel@tonic-gate 	crkey.ck_length = cdata->keylen * 8;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	if (fmt == CRYPTO_DATA_RAW) {
11400Sstevel@tonic-gate 		v1.iov_base = (char *)indata;
11410Sstevel@tonic-gate 		v1.iov_len = length;
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	d1.cd_format = fmt;
11450Sstevel@tonic-gate 	d1.cd_offset = 0;
11460Sstevel@tonic-gate 	d1.cd_length = length;
11470Sstevel@tonic-gate 	if (fmt == CRYPTO_DATA_RAW)
11480Sstevel@tonic-gate 		d1.cd_raw = v1;
11490Sstevel@tonic-gate 	else if (fmt == CRYPTO_DATA_MBLK)
11500Sstevel@tonic-gate 		d1.cd_mp = (mblk_t *)indata;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	mech.cm_type = cdata->mech_type;
11530Sstevel@tonic-gate 	mech.cm_param = cdata->block;
11540Sstevel@tonic-gate 	/*
11550Sstevel@tonic-gate 	 * cdata->block holds the IVEC
11560Sstevel@tonic-gate 	 */
11570Sstevel@tonic-gate 	if (cdata->block != NULL)
11580Sstevel@tonic-gate 		mech.cm_param_len = cdata->blocklen;
11590Sstevel@tonic-gate 	else
11600Sstevel@tonic-gate 		mech.cm_param_len = 0;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	/*
11630Sstevel@tonic-gate 	 * encrypt and decrypt in-place
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 	if (mode == CRYPT_ENCRYPT)
11660Sstevel@tonic-gate 		rv = crypto_encrypt(&mech, &d1, &crkey, NULL, NULL, NULL);
11670Sstevel@tonic-gate 	else
11680Sstevel@tonic-gate 		rv = crypto_decrypt(&mech, &d1, &crkey, NULL, NULL, NULL);
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
11710Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s returned error %08x",
11720Sstevel@tonic-gate 			(mode == CRYPT_ENCRYPT ? "crypto_encrypt" :
11730Sstevel@tonic-gate 				"crypto_decrypt"), rv);
11740Sstevel@tonic-gate 		return (CRYPTO_FAILED);
11750Sstevel@tonic-gate 	}
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	return (rv);
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate static int
11810Sstevel@tonic-gate do_hmac(crypto_mech_type_t mech,
11820Sstevel@tonic-gate 	crypto_key_t *key,
11830Sstevel@tonic-gate 	char *data, int datalen,
11840Sstevel@tonic-gate 	char *hmac, int hmaclen)
11850Sstevel@tonic-gate {
11860Sstevel@tonic-gate 	int rv = 0;
11870Sstevel@tonic-gate 	crypto_mechanism_t mac_mech;
11880Sstevel@tonic-gate 	crypto_data_t dd;
11890Sstevel@tonic-gate 	crypto_data_t mac;
11900Sstevel@tonic-gate 	iovec_t vdata, vmac;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	mac_mech.cm_type = mech;
11930Sstevel@tonic-gate 	mac_mech.cm_param = NULL;
11940Sstevel@tonic-gate 	mac_mech.cm_param_len = 0;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	vdata.iov_base = data;
11970Sstevel@tonic-gate 	vdata.iov_len = datalen;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	bzero(&dd, sizeof (dd));
12000Sstevel@tonic-gate 	dd.cd_format = CRYPTO_DATA_RAW;
12010Sstevel@tonic-gate 	dd.cd_offset = 0;
12020Sstevel@tonic-gate 	dd.cd_length = datalen;
12030Sstevel@tonic-gate 	dd.cd_raw = vdata;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	vmac.iov_base = hmac;
12060Sstevel@tonic-gate 	vmac.iov_len = hmaclen;
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	mac.cd_format = CRYPTO_DATA_RAW;
12090Sstevel@tonic-gate 	mac.cd_offset = 0;
12100Sstevel@tonic-gate 	mac.cd_length = hmaclen;
12110Sstevel@tonic-gate 	mac.cd_raw = vmac;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	/*
12140Sstevel@tonic-gate 	 * Compute MAC of the plaintext decrypted above.
12150Sstevel@tonic-gate 	 */
12160Sstevel@tonic-gate 	rv = crypto_mac(&mac_mech, &dd, key, NULL, &mac, NULL);
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
12190Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_mac failed: %0x", rv);
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	return (rv);
12230Sstevel@tonic-gate }
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate #define	XOR_BLOCK(src, dst) \
12260Sstevel@tonic-gate 	(dst)[0] ^= (src)[0]; \
12270Sstevel@tonic-gate 	(dst)[1] ^= (src)[1]; \
12280Sstevel@tonic-gate 	(dst)[2] ^= (src)[2]; \
12290Sstevel@tonic-gate 	(dst)[3] ^= (src)[3]; \
12300Sstevel@tonic-gate 	(dst)[4] ^= (src)[4]; \
12310Sstevel@tonic-gate 	(dst)[5] ^= (src)[5]; \
12320Sstevel@tonic-gate 	(dst)[6] ^= (src)[6]; \
12330Sstevel@tonic-gate 	(dst)[7] ^= (src)[7]; \
12340Sstevel@tonic-gate 	(dst)[8] ^= (src)[8]; \
12350Sstevel@tonic-gate 	(dst)[9] ^= (src)[9]; \
12360Sstevel@tonic-gate 	(dst)[10] ^= (src)[10]; \
12370Sstevel@tonic-gate 	(dst)[11] ^= (src)[11]; \
12380Sstevel@tonic-gate 	(dst)[12] ^= (src)[12]; \
12390Sstevel@tonic-gate 	(dst)[13] ^= (src)[13]; \
12400Sstevel@tonic-gate 	(dst)[14] ^= (src)[14]; \
12410Sstevel@tonic-gate 	(dst)[15] ^= (src)[15]
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate #define	xorblock(x, y) XOR_BLOCK(y, x)
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate static int
12460Sstevel@tonic-gate aes_cbc_cts_encrypt(struct tmodinfo *tmi, uchar_t *plain, size_t length)
12470Sstevel@tonic-gate {
12480Sstevel@tonic-gate 	int result = CRYPTO_SUCCESS;
12490Sstevel@tonic-gate 	unsigned char tmp[DEFAULT_AES_BLOCKLEN];
12500Sstevel@tonic-gate 	unsigned char tmp2[DEFAULT_AES_BLOCKLEN];
12510Sstevel@tonic-gate 	unsigned char tmp3[DEFAULT_AES_BLOCKLEN];
12520Sstevel@tonic-gate 	int nblocks = 0, blockno;
12530Sstevel@tonic-gate 	crypto_data_t ct, pt;
12540Sstevel@tonic-gate 	crypto_mechanism_t mech;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	mech.cm_type = tmi->enc_data.mech_type;
12570Sstevel@tonic-gate 	if (tmi->enc_data.ivlen > 0 && tmi->enc_data.ivec != NULL) {
12580Sstevel@tonic-gate 		bcopy(tmi->enc_data.ivec, tmp, DEFAULT_AES_BLOCKLEN);
12590Sstevel@tonic-gate 		mech.cm_param = tmi->enc_data.ivec;
12600Sstevel@tonic-gate 		mech.cm_param_len = tmi->enc_data.ivlen;
12610Sstevel@tonic-gate 	} else {
12620Sstevel@tonic-gate 		bzero(tmp, sizeof (tmp));
12630Sstevel@tonic-gate 		mech.cm_param = NULL;
12640Sstevel@tonic-gate 		mech.cm_param_len = 0;
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	nblocks = (length + DEFAULT_AES_BLOCKLEN - 1) / DEFAULT_AES_BLOCKLEN;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	bzero(&ct, sizeof (crypto_data_t));
12700Sstevel@tonic-gate 	bzero(&pt, sizeof (crypto_data_t));
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	if (nblocks == 1) {
12730Sstevel@tonic-gate 		pt.cd_format = CRYPTO_DATA_RAW;
12740Sstevel@tonic-gate 		pt.cd_length = length;
12750Sstevel@tonic-gate 		pt.cd_raw.iov_base = (char *)plain;
12760Sstevel@tonic-gate 		pt.cd_raw.iov_len = length;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 		result = crypto_encrypt(&mech, &pt,
12790Sstevel@tonic-gate 			&tmi->enc_data.d_encr_key, NULL, NULL, NULL);
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
12820Sstevel@tonic-gate 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
12830Sstevel@tonic-gate 				"crypto_encrypt failed: %0x", result);
12840Sstevel@tonic-gate 		}
12850Sstevel@tonic-gate 	} else {
12860Sstevel@tonic-gate 		size_t nleft;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		ct.cd_format = CRYPTO_DATA_RAW;
12890Sstevel@tonic-gate 		ct.cd_offset = 0;
12900Sstevel@tonic-gate 		ct.cd_length = DEFAULT_AES_BLOCKLEN;
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 		pt.cd_format = CRYPTO_DATA_RAW;
12930Sstevel@tonic-gate 		pt.cd_offset = 0;
12940Sstevel@tonic-gate 		pt.cd_length = DEFAULT_AES_BLOCKLEN;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 		result = crypto_encrypt_init(&mech,
12970Sstevel@tonic-gate 				&tmi->enc_data.d_encr_key,
12980Sstevel@tonic-gate 				tmi->enc_data.enc_tmpl,
12990Sstevel@tonic-gate 				&tmi->enc_data.ctx, NULL);
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
13020Sstevel@tonic-gate 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
13030Sstevel@tonic-gate 				"crypto_encrypt_init failed: %0x", result);
13040Sstevel@tonic-gate 			goto cleanup;
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 		for (blockno = 0; blockno < nblocks - 2; blockno++) {
13080Sstevel@tonic-gate 			xorblock(tmp, plain + blockno * DEFAULT_AES_BLOCKLEN);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 			pt.cd_raw.iov_base = (char *)tmp;
13110Sstevel@tonic-gate 			pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 			ct.cd_raw.iov_base = (char *)plain +
13140Sstevel@tonic-gate 				blockno * DEFAULT_AES_BLOCKLEN;
13150Sstevel@tonic-gate 			ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 			result = crypto_encrypt_update(tmi->enc_data.ctx,
13180Sstevel@tonic-gate 					&pt, &ct, NULL);
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 			if (result != CRYPTO_SUCCESS) {
13210Sstevel@tonic-gate 				cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
13220Sstevel@tonic-gate 					"crypto_encrypt_update failed: %0x",
13230Sstevel@tonic-gate 					result);
13240Sstevel@tonic-gate 				goto cleanup;
13250Sstevel@tonic-gate 			}
13260Sstevel@tonic-gate 			/* copy result over original bytes */
13270Sstevel@tonic-gate 			/* make another copy for the next XOR step */
13280Sstevel@tonic-gate 			bcopy(plain + blockno * DEFAULT_AES_BLOCKLEN,
13290Sstevel@tonic-gate 				tmp, DEFAULT_AES_BLOCKLEN);
13300Sstevel@tonic-gate 		}
13310Sstevel@tonic-gate 		/* XOR cipher text from n-3 with plain text from n-2 */
13320Sstevel@tonic-gate 		xorblock(tmp, plain + (nblocks - 2) * DEFAULT_AES_BLOCKLEN);
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 		pt.cd_raw.iov_base = (char *)tmp;
13350Sstevel@tonic-gate 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 		ct.cd_raw.iov_base = (char *)tmp2;
13380Sstevel@tonic-gate 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 		/* encrypt XOR-ed block N-2 */
13410Sstevel@tonic-gate 		result = crypto_encrypt_update(tmi->enc_data.ctx,
13420Sstevel@tonic-gate 				&pt, &ct, NULL);
13430Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
13440Sstevel@tonic-gate 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
13450Sstevel@tonic-gate 				"crypto_encrypt_update(2) failed: %0x",
13460Sstevel@tonic-gate 				result);
13470Sstevel@tonic-gate 			goto cleanup;
13480Sstevel@tonic-gate 		}
13490Sstevel@tonic-gate 		nleft = length - (nblocks - 1) * DEFAULT_AES_BLOCKLEN;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 		bzero(tmp3, sizeof (tmp3));
13520Sstevel@tonic-gate 		/* Save final plaintext bytes from n-1 */
13530Sstevel@tonic-gate 		bcopy(plain + (nblocks - 1) * DEFAULT_AES_BLOCKLEN, tmp3,
13540Sstevel@tonic-gate 			nleft);
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 		/* Overwrite n-1 with cipher text from n-2 */
13570Sstevel@tonic-gate 		bcopy(tmp2, plain + (nblocks - 1) * DEFAULT_AES_BLOCKLEN,
13580Sstevel@tonic-gate 			nleft);
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 		bcopy(tmp2, tmp, DEFAULT_AES_BLOCKLEN);
13610Sstevel@tonic-gate 		/* XOR cipher text from n-1 with plain text from n-1 */
13620Sstevel@tonic-gate 		xorblock(tmp, tmp3);
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 		pt.cd_raw.iov_base = (char *)tmp;
13650Sstevel@tonic-gate 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 		ct.cd_raw.iov_base = (char *)tmp2;
13680Sstevel@tonic-gate 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 		/* encrypt block N-2 */
13710Sstevel@tonic-gate 		result = crypto_encrypt_update(tmi->enc_data.ctx,
13720Sstevel@tonic-gate 			&pt, &ct, NULL);
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
13750Sstevel@tonic-gate 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
13760Sstevel@tonic-gate 				"crypto_encrypt_update(3) failed: %0x",
13770Sstevel@tonic-gate 				result);
13780Sstevel@tonic-gate 			goto cleanup;
13790Sstevel@tonic-gate 		}
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 		bcopy(tmp2, plain + (nblocks - 2) * DEFAULT_AES_BLOCKLEN,
13820Sstevel@tonic-gate 			DEFAULT_AES_BLOCKLEN);
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 		ct.cd_raw.iov_base = (char *)tmp2;
13860Sstevel@tonic-gate 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		/*
13890Sstevel@tonic-gate 		 * Ignore the output on the final step.
13900Sstevel@tonic-gate 		 */
13910Sstevel@tonic-gate 		result = crypto_encrypt_final(tmi->enc_data.ctx, &ct, NULL);
13920Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
13930Sstevel@tonic-gate 			cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
13940Sstevel@tonic-gate 				"crypto_encrypt_final(3) failed: %0x",
13950Sstevel@tonic-gate 				result);
13960Sstevel@tonic-gate 		}
13970Sstevel@tonic-gate 		tmi->enc_data.ctx = NULL;
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate cleanup:
14000Sstevel@tonic-gate 	bzero(tmp, sizeof (tmp));
14010Sstevel@tonic-gate 	bzero(tmp2, sizeof (tmp));
14020Sstevel@tonic-gate 	bzero(tmp3, sizeof (tmp));
14030Sstevel@tonic-gate 	bzero(tmi->enc_data.block, tmi->enc_data.blocklen);
14040Sstevel@tonic-gate 	return (result);
14050Sstevel@tonic-gate }
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate static int
14080Sstevel@tonic-gate aes_cbc_cts_decrypt(struct tmodinfo *tmi, uchar_t *buff, size_t length)
14090Sstevel@tonic-gate {
14100Sstevel@tonic-gate 	int result = CRYPTO_SUCCESS;
14110Sstevel@tonic-gate 	unsigned char tmp[DEFAULT_AES_BLOCKLEN];
14120Sstevel@tonic-gate 	unsigned char tmp2[DEFAULT_AES_BLOCKLEN];
14130Sstevel@tonic-gate 	unsigned char tmp3[DEFAULT_AES_BLOCKLEN];
14140Sstevel@tonic-gate 	int nblocks = 0, blockno;
14150Sstevel@tonic-gate 	crypto_data_t ct, pt;
14160Sstevel@tonic-gate 	crypto_mechanism_t mech;
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	mech.cm_type = tmi->enc_data.mech_type;
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	if (tmi->dec_data.ivec_usage != IVEC_NEVER &&
14210Sstevel@tonic-gate 	    tmi->dec_data.ivlen > 0 && tmi->dec_data.ivec != NULL) {
14220Sstevel@tonic-gate 		bcopy(tmi->dec_data.ivec, tmp, DEFAULT_AES_BLOCKLEN);
14230Sstevel@tonic-gate 		mech.cm_param = tmi->dec_data.ivec;
14240Sstevel@tonic-gate 		mech.cm_param_len = tmi->dec_data.ivlen;
14250Sstevel@tonic-gate 	} else {
14260Sstevel@tonic-gate 		bzero(tmp, sizeof (tmp));
14270Sstevel@tonic-gate 		mech.cm_param_len = 0;
14280Sstevel@tonic-gate 		mech.cm_param = NULL;
14290Sstevel@tonic-gate 	}
14300Sstevel@tonic-gate 	nblocks = (length + DEFAULT_AES_BLOCKLEN - 1) / DEFAULT_AES_BLOCKLEN;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	bzero(&pt, sizeof (pt));
14330Sstevel@tonic-gate 	bzero(&ct, sizeof (ct));
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	if (nblocks == 1) {
14360Sstevel@tonic-gate 		ct.cd_format = CRYPTO_DATA_RAW;
14370Sstevel@tonic-gate 		ct.cd_length = length;
14380Sstevel@tonic-gate 		ct.cd_raw.iov_base = (char *)buff;
14390Sstevel@tonic-gate 		ct.cd_raw.iov_len = length;
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 		result = crypto_decrypt(&mech, &ct,
14420Sstevel@tonic-gate 			&tmi->dec_data.d_encr_key, NULL, NULL, NULL);
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
14450Sstevel@tonic-gate 			cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
14460Sstevel@tonic-gate 				"crypto_decrypt failed: %0x", result);
14470Sstevel@tonic-gate 			goto cleanup;
14480Sstevel@tonic-gate 		}
14490Sstevel@tonic-gate 	} else {
14500Sstevel@tonic-gate 		ct.cd_format = CRYPTO_DATA_RAW;
14510Sstevel@tonic-gate 		ct.cd_offset = 0;
14520Sstevel@tonic-gate 		ct.cd_length = DEFAULT_AES_BLOCKLEN;
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 		pt.cd_format = CRYPTO_DATA_RAW;
14550Sstevel@tonic-gate 		pt.cd_offset = 0;
14560Sstevel@tonic-gate 		pt.cd_length = DEFAULT_AES_BLOCKLEN;
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 		result = crypto_encrypt_init(&mech,
14590Sstevel@tonic-gate 				&tmi->dec_data.d_encr_key,
14600Sstevel@tonic-gate 				tmi->dec_data.enc_tmpl,
14610Sstevel@tonic-gate 				&tmi->dec_data.ctx, NULL);
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
14640Sstevel@tonic-gate 			cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
14650Sstevel@tonic-gate 				"crypto_decrypt_init failed: %0x", result);
14660Sstevel@tonic-gate 			goto cleanup;
14670Sstevel@tonic-gate 		}
14680Sstevel@tonic-gate 		for (blockno = 0; blockno < nblocks - 2; blockno++) {
14690Sstevel@tonic-gate 			ct.cd_raw.iov_base = (char *)buff +
14700Sstevel@tonic-gate 				(blockno * DEFAULT_AES_BLOCKLEN);
14710Sstevel@tonic-gate 			ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 			pt.cd_raw.iov_base = (char *)tmp2;
14740Sstevel@tonic-gate 			pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 			/*
14770Sstevel@tonic-gate 			 * Save the input to the decrypt so it can
14780Sstevel@tonic-gate 			 * be used later for an XOR operation
14790Sstevel@tonic-gate 			 */
14800Sstevel@tonic-gate 			bcopy(buff + (blockno * DEFAULT_AES_BLOCKLEN),
14810Sstevel@tonic-gate 				tmi->dec_data.block, DEFAULT_AES_BLOCKLEN);
14820Sstevel@tonic-gate 
14831398Sethindra 			result = crypto_decrypt_update(tmi->dec_data.ctx,
14840Sstevel@tonic-gate 					&ct, &pt, NULL);
14850Sstevel@tonic-gate 			if (result != CRYPTO_SUCCESS) {
14860Sstevel@tonic-gate 				cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
14870Sstevel@tonic-gate 					"crypto_decrypt_update(1) error - "
14880Sstevel@tonic-gate 					"result = 0x%08x", result);
14890Sstevel@tonic-gate 				goto cleanup;
14900Sstevel@tonic-gate 			}
14910Sstevel@tonic-gate 			xorblock(tmp2, tmp);
14920Sstevel@tonic-gate 			bcopy(tmp2, buff + blockno * DEFAULT_AES_BLOCKLEN,
14930Sstevel@tonic-gate 				DEFAULT_AES_BLOCKLEN);
14940Sstevel@tonic-gate 			/*
14950Sstevel@tonic-gate 			 * The original cipher text is used as the xor
14960Sstevel@tonic-gate 			 * for the next block, save it here.
14970Sstevel@tonic-gate 			 */
14980Sstevel@tonic-gate 			bcopy(tmi->dec_data.block, tmp, DEFAULT_AES_BLOCKLEN);
14990Sstevel@tonic-gate 		}
15000Sstevel@tonic-gate 		ct.cd_raw.iov_base = (char *)buff +
15010Sstevel@tonic-gate 			((nblocks - 2) * DEFAULT_AES_BLOCKLEN);
15020Sstevel@tonic-gate 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
15030Sstevel@tonic-gate 		pt.cd_raw.iov_base = (char *)tmp2;
15040Sstevel@tonic-gate 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 		result = crypto_decrypt_update(tmi->dec_data.ctx,
15070Sstevel@tonic-gate 				&ct, &pt, NULL);
15080Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
15090Sstevel@tonic-gate 			cmn_err(CE_WARN,
15100Sstevel@tonic-gate 				"aes_cbc_cts_decrypt: "
15110Sstevel@tonic-gate 				"crypto_decrypt_update(2) error -"
15120Sstevel@tonic-gate 				" result = 0x%08x", result);
15130Sstevel@tonic-gate 			goto cleanup;
15140Sstevel@tonic-gate 		}
15150Sstevel@tonic-gate 		bzero(tmp3, sizeof (tmp3));
15160Sstevel@tonic-gate 		bcopy(buff + (nblocks - 1) * DEFAULT_AES_BLOCKLEN, tmp3,
15170Sstevel@tonic-gate 			length - ((nblocks - 1) * DEFAULT_AES_BLOCKLEN));
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 		xorblock(tmp2, tmp3);
15200Sstevel@tonic-gate 		bcopy(tmp2, buff + (nblocks - 1) * DEFAULT_AES_BLOCKLEN,
15210Sstevel@tonic-gate 			length - ((nblocks - 1) * DEFAULT_AES_BLOCKLEN));
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 		/* 2nd to last block ... */
15240Sstevel@tonic-gate 		bcopy(tmp3, tmp2,
15250Sstevel@tonic-gate 			length - ((nblocks - 1) * DEFAULT_AES_BLOCKLEN));
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 		ct.cd_raw.iov_base = (char *)tmp2;
15280Sstevel@tonic-gate 		ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
15290Sstevel@tonic-gate 		pt.cd_raw.iov_base = (char *)tmp3;
15300Sstevel@tonic-gate 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 		result = crypto_decrypt_update(tmi->dec_data.ctx,
15330Sstevel@tonic-gate 				&ct, &pt, NULL);
15340Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
15350Sstevel@tonic-gate 			cmn_err(CE_WARN,
15360Sstevel@tonic-gate 				"aes_cbc_cts_decrypt: "
15370Sstevel@tonic-gate 				"crypto_decrypt_update(3) error - "
15380Sstevel@tonic-gate 				"result = 0x%08x", result);
15390Sstevel@tonic-gate 			goto cleanup;
15400Sstevel@tonic-gate 		}
15410Sstevel@tonic-gate 		xorblock(tmp3, tmp);
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 		/* Finally, update the 2nd to last block and we are done. */
15450Sstevel@tonic-gate 		bcopy(tmp3, buff + (nblocks - 2) * DEFAULT_AES_BLOCKLEN,
15460Sstevel@tonic-gate 			DEFAULT_AES_BLOCKLEN);
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 		/* Do Final step, but ignore output */
15490Sstevel@tonic-gate 		pt.cd_raw.iov_base = (char *)tmp2;
15500Sstevel@tonic-gate 		pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
15510Sstevel@tonic-gate 		result = crypto_decrypt_final(tmi->dec_data.ctx, &pt, NULL);
15520Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
15530Sstevel@tonic-gate 			cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
15540Sstevel@tonic-gate 				"crypto_decrypt_final error - "
15550Sstevel@tonic-gate 				"result = 0x%0x", result);
15560Sstevel@tonic-gate 		}
15570Sstevel@tonic-gate 		tmi->dec_data.ctx = NULL;
15580Sstevel@tonic-gate 	}
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate cleanup:
15610Sstevel@tonic-gate 	bzero(tmp, sizeof (tmp));
15620Sstevel@tonic-gate 	bzero(tmp2, sizeof (tmp));
15630Sstevel@tonic-gate 	bzero(tmp3, sizeof (tmp));
15640Sstevel@tonic-gate 	bzero(tmi->dec_data.block, tmi->dec_data.blocklen);
15650Sstevel@tonic-gate 	return (result);
15660Sstevel@tonic-gate }
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate /*
15690Sstevel@tonic-gate  * AES decrypt
15700Sstevel@tonic-gate  *
15710Sstevel@tonic-gate  * format of ciphertext when using AES
15720Sstevel@tonic-gate  *  +-------------+------------+------------+
15730Sstevel@tonic-gate  *  |  confounder | msg-data   |  hmac      |
15740Sstevel@tonic-gate  *  +-------------+------------+------------+
15750Sstevel@tonic-gate  */
15760Sstevel@tonic-gate static mblk_t *
15770Sstevel@tonic-gate aes_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
15780Sstevel@tonic-gate 	hash_info_t *hash)
15790Sstevel@tonic-gate {
15800Sstevel@tonic-gate 	int result;
15810Sstevel@tonic-gate 	size_t enclen;
15820Sstevel@tonic-gate 	size_t inlen;
15830Sstevel@tonic-gate 	uchar_t hmacbuff[64];
15840Sstevel@tonic-gate 	uchar_t tmpiv[DEFAULT_AES_BLOCKLEN];
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	inlen = (size_t)MBLKL(mp);
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	enclen = inlen - AES_TRUNCATED_HMAC_LEN;
15890Sstevel@tonic-gate 	if (tmi->dec_data.ivec_usage != IVEC_NEVER &&
15900Sstevel@tonic-gate 		tmi->dec_data.ivec != NULL && tmi->dec_data.ivlen > 0) {
15910Sstevel@tonic-gate 		int nblocks = (enclen + DEFAULT_AES_BLOCKLEN - 1) /
15920Sstevel@tonic-gate 				DEFAULT_AES_BLOCKLEN;
15930Sstevel@tonic-gate 		bcopy(mp->b_rptr + DEFAULT_AES_BLOCKLEN * (nblocks - 2),
15940Sstevel@tonic-gate 			tmpiv, DEFAULT_AES_BLOCKLEN);
15950Sstevel@tonic-gate 	}
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	/* AES Decrypt */
15980Sstevel@tonic-gate 	result = aes_cbc_cts_decrypt(tmi, mp->b_rptr, enclen);
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
16010Sstevel@tonic-gate 		cmn_err(CE_WARN,
16020Sstevel@tonic-gate 			"aes_decrypt:  aes_cbc_cts_decrypt "
16030Sstevel@tonic-gate 			"failed - error %0x", result);
16040Sstevel@tonic-gate 		goto cleanup;
16050Sstevel@tonic-gate 	}
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	/* Verify the HMAC */
16080Sstevel@tonic-gate 	result = do_hmac(sha1_hmac_mech,
16090Sstevel@tonic-gate 			&tmi->dec_data.d_hmac_key,
16100Sstevel@tonic-gate 			(char *)mp->b_rptr, enclen,
16110Sstevel@tonic-gate 			(char *)hmacbuff, hash->hash_len);
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
16140Sstevel@tonic-gate 		cmn_err(CE_WARN,
16150Sstevel@tonic-gate 			"aes_decrypt:  do_hmac failed - error %0x", result);
16160Sstevel@tonic-gate 		goto cleanup;
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	if (bcmp(hmacbuff, mp->b_rptr + enclen,
16200Sstevel@tonic-gate 		AES_TRUNCATED_HMAC_LEN) != 0) {
16210Sstevel@tonic-gate 		result = -1;
16220Sstevel@tonic-gate 		cmn_err(CE_WARN, "aes_decrypt: checksum verification failed");
16230Sstevel@tonic-gate 		goto cleanup;
16240Sstevel@tonic-gate 	}
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	/* truncate the mblk at the end of the decrypted text */
16270Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + enclen;
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	/* Adjust the beginning of the buffer to skip the confounder */
16300Sstevel@tonic-gate 	mp->b_rptr += DEFAULT_AES_BLOCKLEN;
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	if (tmi->dec_data.ivec_usage != IVEC_NEVER &&
16330Sstevel@tonic-gate 		tmi->dec_data.ivec != NULL && tmi->dec_data.ivlen > 0)
16340Sstevel@tonic-gate 		bcopy(tmpiv, tmi->dec_data.ivec, DEFAULT_AES_BLOCKLEN);
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate cleanup:
16370Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
16380Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
16390Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
16400Sstevel@tonic-gate 		*mp->b_rptr = EIO;
16410Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
16420Sstevel@tonic-gate 		freemsg(mp->b_cont);
16430Sstevel@tonic-gate 		mp->b_cont = NULL;
16440Sstevel@tonic-gate 		qreply(WR(q), mp);
16450Sstevel@tonic-gate 		return (NULL);
16460Sstevel@tonic-gate 	}
16470Sstevel@tonic-gate 	return (mp);
16480Sstevel@tonic-gate }
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate /*
16510Sstevel@tonic-gate  * AES encrypt
16520Sstevel@tonic-gate  *
16530Sstevel@tonic-gate  * format of ciphertext when using AES
16540Sstevel@tonic-gate  *  +-------------+------------+------------+
16550Sstevel@tonic-gate  *  |  confounder | msg-data   |  hmac      |
16560Sstevel@tonic-gate  *  +-------------+------------+------------+
16570Sstevel@tonic-gate  */
16580Sstevel@tonic-gate static mblk_t *
16590Sstevel@tonic-gate aes_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
16600Sstevel@tonic-gate 	hash_info_t *hash)
16610Sstevel@tonic-gate {
16620Sstevel@tonic-gate 	int result;
16630Sstevel@tonic-gate 	size_t cipherlen;
16640Sstevel@tonic-gate 	size_t inlen;
16650Sstevel@tonic-gate 	uchar_t hmacbuff[64];
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	inlen = (size_t)MBLKL(mp);
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	cipherlen = encrypt_size(&tmi->enc_data, inlen);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mp) >= cipherlen);
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	/*
16740Sstevel@tonic-gate 	 * Shift the rptr back enough to insert the confounder.
16750Sstevel@tonic-gate 	 */
16760Sstevel@tonic-gate 	mp->b_rptr -= DEFAULT_AES_BLOCKLEN;
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	/* Get random data for confounder */
16790Sstevel@tonic-gate 	(void) random_get_pseudo_bytes((uint8_t *)mp->b_rptr,
16800Sstevel@tonic-gate 		DEFAULT_AES_BLOCKLEN);
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	/*
16830Sstevel@tonic-gate 	 * Because we encrypt in-place, we need to calculate
16840Sstevel@tonic-gate 	 * the HMAC of the plaintext now, then stick it on
16850Sstevel@tonic-gate 	 * the end of the ciphertext down below.
16860Sstevel@tonic-gate 	 */
16870Sstevel@tonic-gate 	result = do_hmac(sha1_hmac_mech,
16880Sstevel@tonic-gate 			&tmi->enc_data.d_hmac_key,
16890Sstevel@tonic-gate 			(char *)mp->b_rptr, DEFAULT_AES_BLOCKLEN + inlen,
16900Sstevel@tonic-gate 			(char *)hmacbuff, hash->hash_len);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
16930Sstevel@tonic-gate 		cmn_err(CE_WARN, "aes_encrypt:  do_hmac failed - error %0x",
16940Sstevel@tonic-gate 			result);
16950Sstevel@tonic-gate 		goto cleanup;
16960Sstevel@tonic-gate 	}
16970Sstevel@tonic-gate 	/* Encrypt using AES-CBC-CTS */
16980Sstevel@tonic-gate 	result = aes_cbc_cts_encrypt(tmi, mp->b_rptr,
16990Sstevel@tonic-gate 		inlen + DEFAULT_AES_BLOCKLEN);
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
17020Sstevel@tonic-gate 		cmn_err(CE_WARN, "aes_encrypt:  aes_cbc_cts_encrypt "
17030Sstevel@tonic-gate 			"failed - error %0x", result);
17040Sstevel@tonic-gate 		goto cleanup;
17050Sstevel@tonic-gate 	}
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	/* copy the truncated HMAC to the end of the mblk */
17080Sstevel@tonic-gate 	bcopy(hmacbuff, mp->b_rptr + DEFAULT_AES_BLOCKLEN + inlen,
17090Sstevel@tonic-gate 		AES_TRUNCATED_HMAC_LEN);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + cipherlen;
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	/*
17140Sstevel@tonic-gate 	 * The final block of cipher text (not the HMAC) is used
17150Sstevel@tonic-gate 	 * as the next IV.
17160Sstevel@tonic-gate 	 */
17170Sstevel@tonic-gate 	if (tmi->enc_data.ivec_usage != IVEC_NEVER &&
17180Sstevel@tonic-gate 	    tmi->enc_data.ivec != NULL) {
17190Sstevel@tonic-gate 		int nblocks = (inlen + 2 * DEFAULT_AES_BLOCKLEN - 1) /
17200Sstevel@tonic-gate 			DEFAULT_AES_BLOCKLEN;
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 		bcopy(mp->b_rptr + (nblocks - 2) * DEFAULT_AES_BLOCKLEN,
17230Sstevel@tonic-gate 			tmi->enc_data.ivec, DEFAULT_AES_BLOCKLEN);
17240Sstevel@tonic-gate 	}
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate cleanup:
17270Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
17280Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
17290Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
17300Sstevel@tonic-gate 		*mp->b_rptr = EIO;
17310Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
17320Sstevel@tonic-gate 		freemsg(mp->b_cont);
17330Sstevel@tonic-gate 		mp->b_cont = NULL;
17340Sstevel@tonic-gate 		qreply(WR(q), mp);
17350Sstevel@tonic-gate 		return (NULL);
17360Sstevel@tonic-gate 	}
17370Sstevel@tonic-gate 	return (mp);
17380Sstevel@tonic-gate }
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate /*
17410Sstevel@tonic-gate  * ARCFOUR-HMAC-MD5 decrypt
17420Sstevel@tonic-gate  *
17430Sstevel@tonic-gate  * format of ciphertext when using ARCFOUR-HMAC-MD5
17440Sstevel@tonic-gate  *  +-----------+------------+------------+
17450Sstevel@tonic-gate  *  |  hmac     | confounder |  msg-data  |
17460Sstevel@tonic-gate  *  +-----------+------------+------------+
17470Sstevel@tonic-gate  *
17480Sstevel@tonic-gate  */
17490Sstevel@tonic-gate static mblk_t *
17500Sstevel@tonic-gate arcfour_hmac_md5_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
17510Sstevel@tonic-gate 			hash_info_t *hash)
17520Sstevel@tonic-gate {
17530Sstevel@tonic-gate 	int result;
17540Sstevel@tonic-gate 	size_t cipherlen;
17550Sstevel@tonic-gate 	size_t inlen;
17560Sstevel@tonic-gate 	size_t saltlen;
17570Sstevel@tonic-gate 	crypto_key_t k1, k2;
17580Sstevel@tonic-gate 	crypto_data_t indata;
17590Sstevel@tonic-gate 	iovec_t v1;
17600Sstevel@tonic-gate 	uchar_t ms_exp[9] = {0xab, 0xab, 0xab, 0xab, 0xab,
17610Sstevel@tonic-gate 				0xab, 0xab, 0xab, 0xab };
17620Sstevel@tonic-gate 	uchar_t k1data[CRYPT_ARCFOUR_KEYBYTES];
17630Sstevel@tonic-gate 	uchar_t k2data[CRYPT_ARCFOUR_KEYBYTES];
17640Sstevel@tonic-gate 	uchar_t cksum[MD5_HASHSIZE];
17650Sstevel@tonic-gate 	uchar_t saltdata[CRYPT_ARCFOUR_KEYBYTES];
17660Sstevel@tonic-gate 	crypto_mechanism_t mech;
17670Sstevel@tonic-gate 	int usage;
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	/* The usage constant is 1026 for all "old" rcmd mode operations */
17700Sstevel@tonic-gate 	if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V1)
17710Sstevel@tonic-gate 		usage = RCMDV1_USAGE;
17720Sstevel@tonic-gate 	else
17730Sstevel@tonic-gate 		usage = ARCFOUR_DECRYPT_USAGE;
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	/*
17760Sstevel@tonic-gate 	 * The size at this point should be the size of
17770Sstevel@tonic-gate 	 * all the plaintext plus the optional plaintext length
17780Sstevel@tonic-gate 	 * needed for RCMD V2 mode.  There should also be room
17790Sstevel@tonic-gate 	 * at the head of the mblk for the confounder and hash info.
17800Sstevel@tonic-gate 	 */
17810Sstevel@tonic-gate 	inlen = (size_t)MBLKL(mp);
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	/*
17840Sstevel@tonic-gate 	 * The cipherlen does not include the HMAC at the
17850Sstevel@tonic-gate 	 * head of the buffer.
17860Sstevel@tonic-gate 	 */
17870Sstevel@tonic-gate 	cipherlen = inlen - hash->hash_len;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mp) >= cipherlen);
17900Sstevel@tonic-gate 	if (tmi->dec_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
17910Sstevel@tonic-gate 		bcopy(ARCFOUR_EXP_SALT, saltdata, strlen(ARCFOUR_EXP_SALT));
17920Sstevel@tonic-gate 		saltdata[9] = 0;
17930Sstevel@tonic-gate 		saltdata[10] = usage & 0xff;
17940Sstevel@tonic-gate 		saltdata[11] = (usage >> 8) & 0xff;
17950Sstevel@tonic-gate 		saltdata[12] = (usage >> 16) & 0xff;
17960Sstevel@tonic-gate 		saltdata[13] = (usage >> 24) & 0xff;
17970Sstevel@tonic-gate 		saltlen = 14;
17980Sstevel@tonic-gate 	} else {
17990Sstevel@tonic-gate 		saltdata[0] = usage & 0xff;
18000Sstevel@tonic-gate 		saltdata[1] = (usage >> 8) & 0xff;
18010Sstevel@tonic-gate 		saltdata[2] = (usage >> 16) & 0xff;
18020Sstevel@tonic-gate 		saltdata[3] = (usage >> 24) & 0xff;
18030Sstevel@tonic-gate 		saltlen = 4;
18040Sstevel@tonic-gate 	}
18050Sstevel@tonic-gate 	/*
18060Sstevel@tonic-gate 	 * Use the salt value to create a key to be used
18070Sstevel@tonic-gate 	 * for subsequent HMAC operations.
18080Sstevel@tonic-gate 	 */
18090Sstevel@tonic-gate 	result = do_hmac(md5_hmac_mech,
18100Sstevel@tonic-gate 			tmi->dec_data.ckey,
18110Sstevel@tonic-gate 			(char *)saltdata, saltlen,
18120Sstevel@tonic-gate 			(char *)k1data, sizeof (k1data));
18130Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
18140Sstevel@tonic-gate 		cmn_err(CE_WARN,
18150Sstevel@tonic-gate 			"arcfour_hmac_md5_decrypt:  do_hmac(k1)"
18160Sstevel@tonic-gate 			"failed - error %0x", result);
18170Sstevel@tonic-gate 		goto cleanup;
18180Sstevel@tonic-gate 	}
18190Sstevel@tonic-gate 	bcopy(k1data, k2data, sizeof (k1data));
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	/*
18220Sstevel@tonic-gate 	 * For the neutered MS RC4 encryption type,
18230Sstevel@tonic-gate 	 * set the trailing 9 bytes to 0xab per the
18240Sstevel@tonic-gate 	 * RC4-HMAC spec.
18250Sstevel@tonic-gate 	 */
18260Sstevel@tonic-gate 	if (tmi->dec_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
18270Sstevel@tonic-gate 		bcopy((void *)&k1data[7], ms_exp, sizeof (ms_exp));
18280Sstevel@tonic-gate 	}
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 	mech.cm_type = tmi->dec_data.mech_type;
18310Sstevel@tonic-gate 	mech.cm_param = NULL;
18320Sstevel@tonic-gate 	mech.cm_param_len = 0;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 	/*
18350Sstevel@tonic-gate 	 * If we have not yet initialized the decryption key,
18360Sstevel@tonic-gate 	 * context, and template, do it now.
18370Sstevel@tonic-gate 	 */
18380Sstevel@tonic-gate 	if (tmi->dec_data.ctx == NULL ||
18390Sstevel@tonic-gate 	    (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V1)) {
18400Sstevel@tonic-gate 		k1.ck_format = CRYPTO_KEY_RAW;
18410Sstevel@tonic-gate 		k1.ck_length = CRYPT_ARCFOUR_KEYBYTES * 8;
18420Sstevel@tonic-gate 		k1.ck_data = k1data;
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 		tmi->dec_data.d_encr_key.ck_format = CRYPTO_KEY_RAW;
18450Sstevel@tonic-gate 		tmi->dec_data.d_encr_key.ck_length = k1.ck_length;
18460Sstevel@tonic-gate 		if (tmi->dec_data.d_encr_key.ck_data == NULL)
18470Sstevel@tonic-gate 			tmi->dec_data.d_encr_key.ck_data = kmem_zalloc(
18480Sstevel@tonic-gate 				CRYPT_ARCFOUR_KEYBYTES, KM_SLEEP);
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 		/*
18510Sstevel@tonic-gate 		 * HMAC operation creates the encryption
18520Sstevel@tonic-gate 		 * key to be used for the decrypt operations.
18530Sstevel@tonic-gate 		 */
18540Sstevel@tonic-gate 		result = do_hmac(md5_hmac_mech, &k1,
18550Sstevel@tonic-gate 			(char *)mp->b_rptr, hash->hash_len,
18560Sstevel@tonic-gate 			(char *)tmi->dec_data.d_encr_key.ck_data,
18570Sstevel@tonic-gate 			CRYPT_ARCFOUR_KEYBYTES);
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
18610Sstevel@tonic-gate 			cmn_err(CE_WARN,
18620Sstevel@tonic-gate 				"arcfour_hmac_md5_decrypt:  do_hmac(k3)"
18630Sstevel@tonic-gate 				"failed - error %0x", result);
18640Sstevel@tonic-gate 			goto cleanup;
18650Sstevel@tonic-gate 		}
18660Sstevel@tonic-gate 	}
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 	tmi->dec_data.enc_tmpl = NULL;
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 	if (tmi->dec_data.ctx == NULL &&
18710Sstevel@tonic-gate 	    (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)) {
18720Sstevel@tonic-gate 		/*
18730Sstevel@tonic-gate 		 * Only create a template if we are doing
18740Sstevel@tonic-gate 		 * chaining from block to block.
18750Sstevel@tonic-gate 		 */
18760Sstevel@tonic-gate 		result = crypto_create_ctx_template(&mech,
18770Sstevel@tonic-gate 			&tmi->dec_data.d_encr_key,
18780Sstevel@tonic-gate 			&tmi->dec_data.enc_tmpl,
18790Sstevel@tonic-gate 			KM_SLEEP);
18800Sstevel@tonic-gate 		if (result == CRYPTO_NOT_SUPPORTED) {
18810Sstevel@tonic-gate 			tmi->dec_data.enc_tmpl = NULL;
18820Sstevel@tonic-gate 		} else if (result != CRYPTO_SUCCESS) {
18830Sstevel@tonic-gate 			cmn_err(CE_WARN,
18840Sstevel@tonic-gate 				"arcfour_hmac_md5_decrypt:  "
18850Sstevel@tonic-gate 				"failed to create dec template "
18860Sstevel@tonic-gate 				"for RC4 encrypt: %0x", result);
18870Sstevel@tonic-gate 			goto cleanup;
18880Sstevel@tonic-gate 		}
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 		result = crypto_decrypt_init(&mech,
18910Sstevel@tonic-gate 			&tmi->dec_data.d_encr_key,
18920Sstevel@tonic-gate 			tmi->dec_data.enc_tmpl,
18930Sstevel@tonic-gate 			&tmi->dec_data.ctx, NULL);
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
18960Sstevel@tonic-gate 			cmn_err(CE_WARN, "crypto_decrypt_init failed:"
18970Sstevel@tonic-gate 				" %0x", result);
18980Sstevel@tonic-gate 			goto cleanup;
18990Sstevel@tonic-gate 		}
19000Sstevel@tonic-gate 	}
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	/* adjust the rptr so we don't decrypt the original hmac field */
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	v1.iov_base = (char *)mp->b_rptr + hash->hash_len;
19050Sstevel@tonic-gate 	v1.iov_len = cipherlen;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 	indata.cd_format = CRYPTO_DATA_RAW;
19080Sstevel@tonic-gate 	indata.cd_offset = 0;
19090Sstevel@tonic-gate 	indata.cd_length = cipherlen;
19100Sstevel@tonic-gate 	indata.cd_raw = v1;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
19130Sstevel@tonic-gate 		result = crypto_decrypt_update(tmi->dec_data.ctx,
19140Sstevel@tonic-gate 			&indata, NULL, NULL);
19150Sstevel@tonic-gate 	else
19160Sstevel@tonic-gate 		result = crypto_decrypt(&mech, &indata,
19170Sstevel@tonic-gate 			&tmi->dec_data.d_encr_key, NULL, NULL, NULL);
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
19200Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_decrypt_update failed:"
19210Sstevel@tonic-gate 			" %0x", result);
19220Sstevel@tonic-gate 		goto cleanup;
19230Sstevel@tonic-gate 	}
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	k2.ck_format = CRYPTO_KEY_RAW;
19260Sstevel@tonic-gate 	k2.ck_length = sizeof (k2data) * 8;
19270Sstevel@tonic-gate 	k2.ck_data = k2data;
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	result = do_hmac(md5_hmac_mech,
19300Sstevel@tonic-gate 			&k2,
19310Sstevel@tonic-gate 			(char *)mp->b_rptr + hash->hash_len, cipherlen,
19320Sstevel@tonic-gate 			(char *)cksum, hash->hash_len);
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
19350Sstevel@tonic-gate 		cmn_err(CE_WARN,
19360Sstevel@tonic-gate 			"arcfour_hmac_md5_decrypt:  do_hmac(k2)"
19370Sstevel@tonic-gate 			"failed - error %0x", result);
19380Sstevel@tonic-gate 		goto cleanup;
19390Sstevel@tonic-gate 	}
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	if (bcmp(cksum, mp->b_rptr, hash->hash_len) != 0) {
19420Sstevel@tonic-gate 		cmn_err(CE_WARN, "arcfour_decrypt HMAC comparison failed");
19430Sstevel@tonic-gate 		result = -1;
19440Sstevel@tonic-gate 		goto cleanup;
19450Sstevel@tonic-gate 	}
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	/*
19480Sstevel@tonic-gate 	 * adjust the start of the mblk to skip over the
19490Sstevel@tonic-gate 	 * hash and confounder.
19500Sstevel@tonic-gate 	 */
19510Sstevel@tonic-gate 	mp->b_rptr += hash->hash_len + hash->confound_len;
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate cleanup:
19540Sstevel@tonic-gate 	bzero(k1data, sizeof (k1data));
19550Sstevel@tonic-gate 	bzero(k2data, sizeof (k2data));
19560Sstevel@tonic-gate 	bzero(cksum, sizeof (cksum));
19570Sstevel@tonic-gate 	bzero(saltdata, sizeof (saltdata));
19580Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
19590Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
19600Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
19610Sstevel@tonic-gate 		*mp->b_rptr = EIO;
19620Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
19630Sstevel@tonic-gate 		freemsg(mp->b_cont);
19640Sstevel@tonic-gate 		mp->b_cont = NULL;
19650Sstevel@tonic-gate 		qreply(WR(q), mp);
19660Sstevel@tonic-gate 		return (NULL);
19670Sstevel@tonic-gate 	}
19680Sstevel@tonic-gate 	return (mp);
19690Sstevel@tonic-gate }
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate /*
19720Sstevel@tonic-gate  * ARCFOUR-HMAC-MD5 encrypt
19730Sstevel@tonic-gate  *
19740Sstevel@tonic-gate  * format of ciphertext when using ARCFOUR-HMAC-MD5
19750Sstevel@tonic-gate  *  +-----------+------------+------------+
19760Sstevel@tonic-gate  *  |  hmac     | confounder |  msg-data  |
19770Sstevel@tonic-gate  *  +-----------+------------+------------+
19780Sstevel@tonic-gate  *
19790Sstevel@tonic-gate  */
19800Sstevel@tonic-gate static mblk_t *
19810Sstevel@tonic-gate arcfour_hmac_md5_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
19820Sstevel@tonic-gate 			hash_info_t *hash)
19830Sstevel@tonic-gate {
19840Sstevel@tonic-gate 	int result;
19850Sstevel@tonic-gate 	size_t cipherlen;
19860Sstevel@tonic-gate 	size_t inlen;
19870Sstevel@tonic-gate 	size_t saltlen;
19880Sstevel@tonic-gate 	crypto_key_t k1, k2;
19890Sstevel@tonic-gate 	crypto_data_t indata;
19900Sstevel@tonic-gate 	iovec_t v1;
19910Sstevel@tonic-gate 	uchar_t ms_exp[9] = {0xab, 0xab, 0xab, 0xab, 0xab,
19920Sstevel@tonic-gate 				0xab, 0xab, 0xab, 0xab };
19930Sstevel@tonic-gate 	uchar_t k1data[CRYPT_ARCFOUR_KEYBYTES];
19940Sstevel@tonic-gate 	uchar_t k2data[CRYPT_ARCFOUR_KEYBYTES];
19950Sstevel@tonic-gate 	uchar_t saltdata[CRYPT_ARCFOUR_KEYBYTES];
19960Sstevel@tonic-gate 	crypto_mechanism_t mech;
19970Sstevel@tonic-gate 	int usage;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	/* The usage constant is 1026 for all "old" rcmd mode operations */
20000Sstevel@tonic-gate 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V1)
20010Sstevel@tonic-gate 		usage = RCMDV1_USAGE;
20020Sstevel@tonic-gate 	else
20030Sstevel@tonic-gate 		usage = ARCFOUR_ENCRYPT_USAGE;
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	mech.cm_type = tmi->enc_data.mech_type;
20060Sstevel@tonic-gate 	mech.cm_param = NULL;
20070Sstevel@tonic-gate 	mech.cm_param_len = 0;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	/*
20100Sstevel@tonic-gate 	 * The size at this point should be the size of
20110Sstevel@tonic-gate 	 * all the plaintext plus the optional plaintext length
20120Sstevel@tonic-gate 	 * needed for RCMD V2 mode.  There should also be room
20130Sstevel@tonic-gate 	 * at the head of the mblk for the confounder and hash info.
20140Sstevel@tonic-gate 	 */
20150Sstevel@tonic-gate 	inlen = (size_t)MBLKL(mp);
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	cipherlen = encrypt_size(&tmi->enc_data, inlen);
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mp) >= cipherlen);
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	/*
20220Sstevel@tonic-gate 	 * Shift the rptr back enough to insert
20230Sstevel@tonic-gate 	 * the confounder and hash.
20240Sstevel@tonic-gate 	 */
20250Sstevel@tonic-gate 	mp->b_rptr -= (hash->confound_len + hash->hash_len);
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 	/* zero out the hash area */
20280Sstevel@tonic-gate 	bzero(mp->b_rptr, (size_t)hash->hash_len);
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 	if (cipherlen > inlen) {
20310Sstevel@tonic-gate 		bzero(mp->b_wptr, MBLKTAIL(mp));
20320Sstevel@tonic-gate 	}
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
20350Sstevel@tonic-gate 		bcopy(ARCFOUR_EXP_SALT, saltdata, strlen(ARCFOUR_EXP_SALT));
20360Sstevel@tonic-gate 		saltdata[9] = 0;
20370Sstevel@tonic-gate 		saltdata[10] = usage & 0xff;
20380Sstevel@tonic-gate 		saltdata[11] = (usage >> 8) & 0xff;
20390Sstevel@tonic-gate 		saltdata[12] = (usage >> 16) & 0xff;
20400Sstevel@tonic-gate 		saltdata[13] = (usage >> 24) & 0xff;
20410Sstevel@tonic-gate 		saltlen = 14;
20420Sstevel@tonic-gate 	} else {
20430Sstevel@tonic-gate 		saltdata[0] = usage & 0xff;
20440Sstevel@tonic-gate 		saltdata[1] = (usage >> 8) & 0xff;
20450Sstevel@tonic-gate 		saltdata[2] = (usage >> 16) & 0xff;
20460Sstevel@tonic-gate 		saltdata[3] = (usage >> 24) & 0xff;
20470Sstevel@tonic-gate 		saltlen = 4;
20480Sstevel@tonic-gate 	}
20490Sstevel@tonic-gate 	/*
20500Sstevel@tonic-gate 	 * Use the salt value to create a key to be used
20510Sstevel@tonic-gate 	 * for subsequent HMAC operations.
20520Sstevel@tonic-gate 	 */
20530Sstevel@tonic-gate 	result = do_hmac(md5_hmac_mech,
20540Sstevel@tonic-gate 			tmi->enc_data.ckey,
20550Sstevel@tonic-gate 			(char *)saltdata, saltlen,
20560Sstevel@tonic-gate 			(char *)k1data, sizeof (k1data));
20570Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
20580Sstevel@tonic-gate 		cmn_err(CE_WARN,
20590Sstevel@tonic-gate 			"arcfour_hmac_md5_encrypt:  do_hmac(k1)"
20600Sstevel@tonic-gate 			"failed - error %0x", result);
20610Sstevel@tonic-gate 		goto cleanup;
20620Sstevel@tonic-gate 	}
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	bcopy(k1data, k2data, sizeof (k2data));
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	/*
20670Sstevel@tonic-gate 	 * For the neutered MS RC4 encryption type,
20680Sstevel@tonic-gate 	 * set the trailing 9 bytes to 0xab per the
20690Sstevel@tonic-gate 	 * RC4-HMAC spec.
20700Sstevel@tonic-gate 	 */
20710Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
20720Sstevel@tonic-gate 		bcopy((void *)&k1data[7], ms_exp, sizeof (ms_exp));
20730Sstevel@tonic-gate 	}
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	/*
20760Sstevel@tonic-gate 	 * Get the confounder bytes.
20770Sstevel@tonic-gate 	 */
20780Sstevel@tonic-gate 	(void) random_get_pseudo_bytes(
20790Sstevel@tonic-gate 			(uint8_t *)(mp->b_rptr + hash->hash_len),
20800Sstevel@tonic-gate 			(size_t)hash->confound_len);
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	k2.ck_data = k2data;
20830Sstevel@tonic-gate 	k2.ck_format = CRYPTO_KEY_RAW;
20840Sstevel@tonic-gate 	k2.ck_length = sizeof (k2data) * 8;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	/*
20870Sstevel@tonic-gate 	 * This writes the HMAC to the hash area in the
20880Sstevel@tonic-gate 	 * mblk.  The key used is the one just created by
20890Sstevel@tonic-gate 	 * the previous HMAC operation.
20900Sstevel@tonic-gate 	 * The data being processed is the confounder bytes
20910Sstevel@tonic-gate 	 * PLUS the input plaintext.
20920Sstevel@tonic-gate 	 */
20930Sstevel@tonic-gate 	result = do_hmac(md5_hmac_mech, &k2,
20940Sstevel@tonic-gate 			(char *)mp->b_rptr + hash->hash_len,
20950Sstevel@tonic-gate 			hash->confound_len + inlen,
20960Sstevel@tonic-gate 			(char *)mp->b_rptr, hash->hash_len);
20970Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
20980Sstevel@tonic-gate 		cmn_err(CE_WARN,
20990Sstevel@tonic-gate 			"arcfour_hmac_md5_encrypt:  do_hmac(k2)"
21000Sstevel@tonic-gate 			"failed - error %0x", result);
21010Sstevel@tonic-gate 		goto cleanup;
21020Sstevel@tonic-gate 	}
21030Sstevel@tonic-gate 	/*
21040Sstevel@tonic-gate 	 * Because of the odd way that MIT uses RC4 keys
21050Sstevel@tonic-gate 	 * on the rlogin stream, we only need to create
21060Sstevel@tonic-gate 	 * this key once.
21070Sstevel@tonic-gate 	 * However, if using "old" rcmd mode, we need to do
21080Sstevel@tonic-gate 	 * it every time.
21090Sstevel@tonic-gate 	 */
21100Sstevel@tonic-gate 	if (tmi->enc_data.ctx == NULL ||
21110Sstevel@tonic-gate 	    (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V1)) {
21120Sstevel@tonic-gate 		crypto_key_t *key = &tmi->enc_data.d_encr_key;
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 		k1.ck_data = k1data;
21150Sstevel@tonic-gate 		k1.ck_format = CRYPTO_KEY_RAW;
21160Sstevel@tonic-gate 		k1.ck_length = sizeof (k1data) * 8;
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 		key->ck_format = CRYPTO_KEY_RAW;
21190Sstevel@tonic-gate 		key->ck_length = k1.ck_length;
21200Sstevel@tonic-gate 		if (key->ck_data == NULL)
21210Sstevel@tonic-gate 			key->ck_data = kmem_zalloc(
21220Sstevel@tonic-gate 				CRYPT_ARCFOUR_KEYBYTES, KM_SLEEP);
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 		/*
21250Sstevel@tonic-gate 		 * The final HMAC operation creates the encryption
21260Sstevel@tonic-gate 		 * key to be used for the encrypt operation.
21270Sstevel@tonic-gate 		 */
21280Sstevel@tonic-gate 		result = do_hmac(md5_hmac_mech, &k1,
21290Sstevel@tonic-gate 			(char *)mp->b_rptr, hash->hash_len,
21300Sstevel@tonic-gate 			(char *)key->ck_data, CRYPT_ARCFOUR_KEYBYTES);
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
21330Sstevel@tonic-gate 			cmn_err(CE_WARN,
21340Sstevel@tonic-gate 				"arcfour_hmac_md5_encrypt:  do_hmac(k3)"
21350Sstevel@tonic-gate 				"failed - error %0x", result);
21360Sstevel@tonic-gate 			goto cleanup;
21370Sstevel@tonic-gate 		}
21380Sstevel@tonic-gate 	}
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	/*
21410Sstevel@tonic-gate 	 * If the context has not been initialized, do it now.
21420Sstevel@tonic-gate 	 */
21430Sstevel@tonic-gate 	if (tmi->enc_data.ctx == NULL &&
21440Sstevel@tonic-gate 	    (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)) {
21450Sstevel@tonic-gate 		/*
21460Sstevel@tonic-gate 		 * Only create a template if we are doing
21470Sstevel@tonic-gate 		 * chaining from block to block.
21480Sstevel@tonic-gate 		 */
21490Sstevel@tonic-gate 		result = crypto_create_ctx_template(&mech,
21500Sstevel@tonic-gate 				&tmi->enc_data.d_encr_key,
21510Sstevel@tonic-gate 				&tmi->enc_data.enc_tmpl,
21520Sstevel@tonic-gate 				KM_SLEEP);
21530Sstevel@tonic-gate 		if (result == CRYPTO_NOT_SUPPORTED) {
21540Sstevel@tonic-gate 			tmi->enc_data.enc_tmpl = NULL;
21550Sstevel@tonic-gate 		} else if (result != CRYPTO_SUCCESS) {
21560Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to create enc template "
21570Sstevel@tonic-gate 				"for RC4 encrypt: %0x", result);
21580Sstevel@tonic-gate 			goto cleanup;
21590Sstevel@tonic-gate 		}
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 		result = crypto_encrypt_init(&mech,
21620Sstevel@tonic-gate 					&tmi->enc_data.d_encr_key,
21630Sstevel@tonic-gate 					tmi->enc_data.enc_tmpl,
21640Sstevel@tonic-gate 					&tmi->enc_data.ctx, NULL);
21650Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
21660Sstevel@tonic-gate 			cmn_err(CE_WARN, "crypto_encrypt_init failed:"
21670Sstevel@tonic-gate 				" %0x", result);
21680Sstevel@tonic-gate 			goto cleanup;
21690Sstevel@tonic-gate 		}
21700Sstevel@tonic-gate 	}
21710Sstevel@tonic-gate 	v1.iov_base = (char *)mp->b_rptr + hash->hash_len;
21720Sstevel@tonic-gate 	v1.iov_len = hash->confound_len + inlen;
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	indata.cd_format = CRYPTO_DATA_RAW;
21750Sstevel@tonic-gate 	indata.cd_offset = 0;
21760Sstevel@tonic-gate 	indata.cd_length = hash->confound_len + inlen;
21770Sstevel@tonic-gate 	indata.cd_raw = v1;
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
21800Sstevel@tonic-gate 		result = crypto_encrypt_update(tmi->enc_data.ctx,
21810Sstevel@tonic-gate 			&indata, NULL, NULL);
21820Sstevel@tonic-gate 	else
21830Sstevel@tonic-gate 		result = crypto_encrypt(&mech, &indata,
21840Sstevel@tonic-gate 			&tmi->enc_data.d_encr_key, NULL,
21850Sstevel@tonic-gate 			NULL, NULL);
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
21880Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_encrypt_update failed: 0x%0x",
21890Sstevel@tonic-gate 			result);
21900Sstevel@tonic-gate 	}
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate cleanup:
21930Sstevel@tonic-gate 	bzero(k1data, sizeof (k1data));
21940Sstevel@tonic-gate 	bzero(k2data, sizeof (k2data));
21950Sstevel@tonic-gate 	bzero(saltdata, sizeof (saltdata));
21960Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
21970Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
21980Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
21990Sstevel@tonic-gate 		*mp->b_rptr = EIO;
22000Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
22010Sstevel@tonic-gate 		freemsg(mp->b_cont);
22020Sstevel@tonic-gate 		mp->b_cont = NULL;
22030Sstevel@tonic-gate 		qreply(WR(q), mp);
22040Sstevel@tonic-gate 		return (NULL);
22050Sstevel@tonic-gate 	}
22060Sstevel@tonic-gate 	return (mp);
22070Sstevel@tonic-gate }
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate /*
22100Sstevel@tonic-gate  * DES-CBC-[HASH] encrypt
22110Sstevel@tonic-gate  *
22120Sstevel@tonic-gate  * Needed to support userland apps that must support Kerberos V5
22130Sstevel@tonic-gate  * encryption DES-CBC encryption modes.
22140Sstevel@tonic-gate  *
22150Sstevel@tonic-gate  * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
22160Sstevel@tonic-gate  *
22170Sstevel@tonic-gate  * format of ciphertext for DES-CBC functions, per RFC1510 is:
22180Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
22190Sstevel@tonic-gate  *  |confounder |  cksum   |   msg-data  | pad |
22200Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
22210Sstevel@tonic-gate  *
22220Sstevel@tonic-gate  * format of ciphertext when using DES3-SHA1-HMAC
22230Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
22240Sstevel@tonic-gate  *  |confounder |  msg-data  |   hmac    | pad |
22250Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
22260Sstevel@tonic-gate  *
22270Sstevel@tonic-gate  *  The confounder is 8 bytes of random data.
22280Sstevel@tonic-gate  *  The cksum depends on the hash being used.
22290Sstevel@tonic-gate  *   4 bytes for CRC32
22300Sstevel@tonic-gate  *  16 bytes for MD5
22310Sstevel@tonic-gate  *  20 bytes for SHA1
22320Sstevel@tonic-gate  *   0 bytes for RAW
22330Sstevel@tonic-gate  *
22340Sstevel@tonic-gate  */
22350Sstevel@tonic-gate static mblk_t *
22360Sstevel@tonic-gate des_cbc_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, hash_info_t *hash)
22370Sstevel@tonic-gate {
22380Sstevel@tonic-gate 	int result;
22390Sstevel@tonic-gate 	size_t cipherlen;
22400Sstevel@tonic-gate 	size_t inlen;
22410Sstevel@tonic-gate 	size_t plainlen;
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	/*
22440Sstevel@tonic-gate 	 * The size at this point should be the size of
22450Sstevel@tonic-gate 	 * all the plaintext plus the optional plaintext length
22460Sstevel@tonic-gate 	 * needed for RCMD V2 mode.  There should also be room
22470Sstevel@tonic-gate 	 * at the head of the mblk for the confounder and hash info.
22480Sstevel@tonic-gate 	 */
22490Sstevel@tonic-gate 	inlen = (size_t)MBLKL(mp);
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	/*
22520Sstevel@tonic-gate 	 * The output size will be a multiple of 8 because this algorithm
22530Sstevel@tonic-gate 	 * only works on 8 byte chunks.
22540Sstevel@tonic-gate 	 */
22550Sstevel@tonic-gate 	cipherlen = encrypt_size(&tmi->enc_data, inlen);
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mp) >= cipherlen);
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	if (cipherlen > inlen) {
22600Sstevel@tonic-gate 		bzero(mp->b_wptr, MBLKTAIL(mp));
22610Sstevel@tonic-gate 	}
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	/*
22640Sstevel@tonic-gate 	 * Shift the rptr back enough to insert
22650Sstevel@tonic-gate 	 * the confounder and hash.
22660Sstevel@tonic-gate 	 */
22670Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
22680Sstevel@tonic-gate 		mp->b_rptr -= hash->confound_len;
22690Sstevel@tonic-gate 	} else {
22700Sstevel@tonic-gate 		mp->b_rptr -= (hash->confound_len + hash->hash_len);
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 		/* zero out the hash area */
22730Sstevel@tonic-gate 		bzero(mp->b_rptr + hash->confound_len, (size_t)hash->hash_len);
22740Sstevel@tonic-gate 	}
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 	/* get random confounder from our friend, the 'random' module */
22770Sstevel@tonic-gate 	if (hash->confound_len > 0) {
22780Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)mp->b_rptr,
22790Sstevel@tonic-gate 				    (size_t)hash->confound_len);
22800Sstevel@tonic-gate 	}
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	/*
22830Sstevel@tonic-gate 	 * For 3DES we calculate an HMAC later.
22840Sstevel@tonic-gate 	 */
22850Sstevel@tonic-gate 	if (tmi->enc_data.method != CRYPT_METHOD_DES3_CBC_SHA1) {
22860Sstevel@tonic-gate 		/* calculate chksum of confounder + input */
22870Sstevel@tonic-gate 		if (hash->hash_len > 0 && hash->hashfunc != NULL) {
22880Sstevel@tonic-gate 			uchar_t cksum[MAX_CKSUM_LEN];
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 			result = hash->hashfunc(cksum, mp->b_rptr,
22910Sstevel@tonic-gate 				cipherlen);
22920Sstevel@tonic-gate 			if (result != CRYPTO_SUCCESS) {
22930Sstevel@tonic-gate 				goto failure;
22940Sstevel@tonic-gate 			}
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 			/* put hash in place right after the confounder */
22970Sstevel@tonic-gate 			bcopy(cksum, (mp->b_rptr + hash->confound_len),
22980Sstevel@tonic-gate 			    (size_t)hash->hash_len);
22990Sstevel@tonic-gate 		}
23000Sstevel@tonic-gate 	}
23010Sstevel@tonic-gate 	/*
23020Sstevel@tonic-gate 	 * In order to support the "old" Kerberos RCMD protocol,
23030Sstevel@tonic-gate 	 * we must use the IVEC 3 different ways:
23040Sstevel@tonic-gate 	 *   IVEC_REUSE = keep using the same IV each time, this is
23050Sstevel@tonic-gate 	 *		ugly and insecure, but necessary for
23060Sstevel@tonic-gate 	 *		backwards compatibility with existing MIT code.
23070Sstevel@tonic-gate 	 *   IVEC_ONETIME = Use the ivec as initialized when the crypto
23080Sstevel@tonic-gate 	 *		was setup (see setup_crypto routine).
23090Sstevel@tonic-gate 	 *   IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
23100Sstevel@tonic-gate 	 */
23110Sstevel@tonic-gate 	if (tmi->enc_data.ivec_usage == IVEC_NEVER) {
23120Sstevel@tonic-gate 		bzero(tmi->enc_data.block, tmi->enc_data.blocklen);
23130Sstevel@tonic-gate 	} else if (tmi->enc_data.ivec_usage == IVEC_REUSE) {
23140Sstevel@tonic-gate 		bcopy(tmi->enc_data.ivec, tmi->enc_data.block,
23150Sstevel@tonic-gate 		    tmi->enc_data.blocklen);
23160Sstevel@tonic-gate 	}
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
23190Sstevel@tonic-gate 		/*
23200Sstevel@tonic-gate 		 * The input length already included the hash size,
23210Sstevel@tonic-gate 		 * don't include this in the plaintext length
23220Sstevel@tonic-gate 		 * calculations.
23230Sstevel@tonic-gate 		 */
23240Sstevel@tonic-gate 		plainlen = cipherlen - hash->hash_len;
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + plainlen;
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 		result = kef_encr_hmac(&tmi->enc_data,
23290Sstevel@tonic-gate 			(void *)mp, (size_t)plainlen,
23300Sstevel@tonic-gate 			(char *)(mp->b_rptr + plainlen),
23310Sstevel@tonic-gate 			hash->hash_len);
23320Sstevel@tonic-gate 	} else {
23330Sstevel@tonic-gate 		ASSERT(mp->b_rptr + cipherlen <= DB_LIM(mp));
23340Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + cipherlen;
23350Sstevel@tonic-gate 		result = kef_crypt(&tmi->enc_data, (void *)mp,
23360Sstevel@tonic-gate 			CRYPTO_DATA_MBLK, (size_t)cipherlen,
23370Sstevel@tonic-gate 			CRYPT_ENCRYPT);
23380Sstevel@tonic-gate 	}
23390Sstevel@tonic-gate failure:
23400Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
23410Sstevel@tonic-gate #ifdef DEBUG
23420Sstevel@tonic-gate 		cmn_err(CE_WARN,
23430Sstevel@tonic-gate 			"des_cbc_encrypt: kef_crypt encrypt "
23440Sstevel@tonic-gate 			"failed (len: %ld) - error %0x",
23450Sstevel@tonic-gate 			cipherlen, result);
23460Sstevel@tonic-gate #endif
23470Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
23480Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
23490Sstevel@tonic-gate 		*mp->b_rptr = EIO;
23500Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
23510Sstevel@tonic-gate 		freemsg(mp->b_cont);
23520Sstevel@tonic-gate 		mp->b_cont = NULL;
23530Sstevel@tonic-gate 		qreply(WR(q), mp);
23540Sstevel@tonic-gate 		return (NULL);
23550Sstevel@tonic-gate 	} else if (tmi->enc_data.ivec_usage == IVEC_ONETIME) {
23560Sstevel@tonic-gate 		/*
23570Sstevel@tonic-gate 		 * Because we are using KEF, we must manually
23580Sstevel@tonic-gate 		 * update our IV.
23590Sstevel@tonic-gate 		 */
23600Sstevel@tonic-gate 		bcopy(mp->b_wptr - tmi->enc_data.ivlen,
23610Sstevel@tonic-gate 			tmi->enc_data.block, tmi->enc_data.ivlen);
23620Sstevel@tonic-gate 	}
23630Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
23640Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + cipherlen;
23650Sstevel@tonic-gate 	}
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	return (mp);
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate /*
23710Sstevel@tonic-gate  * des_cbc_decrypt
23720Sstevel@tonic-gate  *
23730Sstevel@tonic-gate  *
23740Sstevel@tonic-gate  * Needed to support userland apps that must support Kerberos V5
23750Sstevel@tonic-gate  * encryption DES-CBC decryption modes.
23760Sstevel@tonic-gate  *
23770Sstevel@tonic-gate  * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
23780Sstevel@tonic-gate  *
23790Sstevel@tonic-gate  * format of ciphertext for DES-CBC functions, per RFC1510 is:
23800Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
23810Sstevel@tonic-gate  *  |confounder |  cksum   |   msg-data  | pad |
23820Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
23830Sstevel@tonic-gate  *
23840Sstevel@tonic-gate  * format of ciphertext when using DES3-SHA1-HMAC
23850Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
23860Sstevel@tonic-gate  *  |confounder |  msg-data  |   hmac    | pad |
23870Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
23880Sstevel@tonic-gate  *
23890Sstevel@tonic-gate  *  The confounder is 8 bytes of random data.
23900Sstevel@tonic-gate  *  The cksum depends on the hash being used.
23910Sstevel@tonic-gate  *   4 bytes for CRC32
23920Sstevel@tonic-gate  *  16 bytes for MD5
23930Sstevel@tonic-gate  *  20 bytes for SHA1
23940Sstevel@tonic-gate  *   0 bytes for RAW
23950Sstevel@tonic-gate  *
23960Sstevel@tonic-gate  */
23970Sstevel@tonic-gate static mblk_t *
23980Sstevel@tonic-gate des_cbc_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, hash_info_t *hash)
23990Sstevel@tonic-gate {
24000Sstevel@tonic-gate 	uint_t inlen, datalen;
24010Sstevel@tonic-gate 	int result = 0;
24020Sstevel@tonic-gate 	uchar_t *optr = NULL;
24030Sstevel@tonic-gate 	uchar_t cksum[MAX_CKSUM_LEN], newcksum[MAX_CKSUM_LEN];
24040Sstevel@tonic-gate 	uchar_t nextiv[DEFAULT_DES_BLOCKLEN];
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 	/* Compute adjusted size */
24070Sstevel@tonic-gate 	inlen = MBLKL(mp);
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	optr = mp->b_rptr;
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 	/*
24120Sstevel@tonic-gate 	 * In order to support the "old" Kerberos RCMD protocol,
24130Sstevel@tonic-gate 	 * we must use the IVEC 3 different ways:
24140Sstevel@tonic-gate 	 *   IVEC_REUSE = keep using the same IV each time, this is
24150Sstevel@tonic-gate 	 *		ugly and insecure, but necessary for
24160Sstevel@tonic-gate 	 *		backwards compatibility with existing MIT code.
24170Sstevel@tonic-gate 	 *   IVEC_ONETIME = Use the ivec as initialized when the crypto
24180Sstevel@tonic-gate 	 *		was setup (see setup_crypto routine).
24190Sstevel@tonic-gate 	 *   IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
24200Sstevel@tonic-gate 	 */
24210Sstevel@tonic-gate 	if (tmi->dec_data.ivec_usage == IVEC_NEVER)
24220Sstevel@tonic-gate 		bzero(tmi->dec_data.block, tmi->dec_data.blocklen);
24230Sstevel@tonic-gate 	else if (tmi->dec_data.ivec_usage == IVEC_REUSE)
24240Sstevel@tonic-gate 		bcopy(tmi->dec_data.ivec, tmi->dec_data.block,
24250Sstevel@tonic-gate 		    tmi->dec_data.blocklen);
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate 	if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
24280Sstevel@tonic-gate 		/*
24290Sstevel@tonic-gate 		 * Do not decrypt the HMAC at the end
24300Sstevel@tonic-gate 		 */
24310Sstevel@tonic-gate 		int decrypt_len = inlen - hash->hash_len;
24320Sstevel@tonic-gate 
24330Sstevel@tonic-gate 		/*
24340Sstevel@tonic-gate 		 * Move the wptr so the mblk appears to end
24350Sstevel@tonic-gate 		 * BEFORE the HMAC section.
24360Sstevel@tonic-gate 		 */
24370Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + decrypt_len;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 		/*
24400Sstevel@tonic-gate 		 * Because we are using KEF, we must manually update our
24410Sstevel@tonic-gate 		 * IV.
24420Sstevel@tonic-gate 		 */
24430Sstevel@tonic-gate 		if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
24440Sstevel@tonic-gate 			bcopy(mp->b_rptr + decrypt_len - tmi->dec_data.ivlen,
24450Sstevel@tonic-gate 				nextiv, tmi->dec_data.ivlen);
24460Sstevel@tonic-gate 		}
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 		result = kef_decr_hmac(&tmi->dec_data, mp, decrypt_len,
24490Sstevel@tonic-gate 			(char *)newcksum, hash->hash_len);
24500Sstevel@tonic-gate 	} else {
24510Sstevel@tonic-gate 		/*
24520Sstevel@tonic-gate 		 * Because we are using KEF, we must manually update our
24530Sstevel@tonic-gate 		 * IV.
24540Sstevel@tonic-gate 		 */
24550Sstevel@tonic-gate 		if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
24560Sstevel@tonic-gate 			bcopy(mp->b_wptr - tmi->enc_data.ivlen, nextiv,
24570Sstevel@tonic-gate 				tmi->dec_data.ivlen);
24580Sstevel@tonic-gate 		}
24590Sstevel@tonic-gate 		result = kef_crypt(&tmi->dec_data, (void *)mp,
24600Sstevel@tonic-gate 			CRYPTO_DATA_MBLK, (size_t)inlen, CRYPT_DECRYPT);
24610Sstevel@tonic-gate 	}
24620Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
24630Sstevel@tonic-gate #ifdef DEBUG
24640Sstevel@tonic-gate 		cmn_err(CE_WARN,
24650Sstevel@tonic-gate 			"des_cbc_decrypt: kef_crypt decrypt "
24660Sstevel@tonic-gate 			"failed - error %0x", result);
24670Sstevel@tonic-gate #endif
24680Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
24690Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
24700Sstevel@tonic-gate 		*mp->b_rptr = EIO;
24710Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
24720Sstevel@tonic-gate 		freemsg(mp->b_cont);
24730Sstevel@tonic-gate 		mp->b_cont = NULL;
24740Sstevel@tonic-gate 		qreply(WR(q), mp);
24750Sstevel@tonic-gate 		return (NULL);
24760Sstevel@tonic-gate 	}
24770Sstevel@tonic-gate 
24780Sstevel@tonic-gate 	/*
24790Sstevel@tonic-gate 	 * Manually update the IV, KEF does not track this for us.
24800Sstevel@tonic-gate 	 */
24810Sstevel@tonic-gate 	if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
24820Sstevel@tonic-gate 		bcopy(nextiv, tmi->dec_data.block, tmi->dec_data.ivlen);
24830Sstevel@tonic-gate 	}
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 	/* Verify the checksum(if necessary) */
24860Sstevel@tonic-gate 	if (hash->hash_len > 0) {
24870Sstevel@tonic-gate 		if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
24880Sstevel@tonic-gate 			bcopy(mp->b_rptr + inlen - hash->hash_len, cksum,
24890Sstevel@tonic-gate 				hash->hash_len);
24900Sstevel@tonic-gate 		} else {
24910Sstevel@tonic-gate 			bcopy(optr + hash->confound_len, cksum, hash->hash_len);
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 			/* zero the cksum in the buffer */
24940Sstevel@tonic-gate 			ASSERT(optr + hash->confound_len + hash->hash_len <=
24950Sstevel@tonic-gate 				DB_LIM(mp));
24960Sstevel@tonic-gate 			bzero(optr + hash->confound_len, hash->hash_len);
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 			/* calculate MD5 chksum of confounder + input */
24990Sstevel@tonic-gate 			if (hash->hashfunc) {
25000Sstevel@tonic-gate 				(void) hash->hashfunc(newcksum, optr, inlen);
25010Sstevel@tonic-gate 			}
25020Sstevel@tonic-gate 		}
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 		if (bcmp(cksum, newcksum, hash->hash_len)) {
25050Sstevel@tonic-gate #ifdef DEBUG
25060Sstevel@tonic-gate 			cmn_err(CE_WARN, "des_cbc_decrypt: checksum "
25070Sstevel@tonic-gate 				"verification failed");
25080Sstevel@tonic-gate #endif
25090Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
25100Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
25110Sstevel@tonic-gate 			*mp->b_rptr = EIO;
25120Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
25130Sstevel@tonic-gate 			freemsg(mp->b_cont);
25140Sstevel@tonic-gate 			mp->b_cont = NULL;
25150Sstevel@tonic-gate 			qreply(WR(q), mp);
25160Sstevel@tonic-gate 			return (NULL);
25170Sstevel@tonic-gate 		}
25180Sstevel@tonic-gate 	}
25190Sstevel@tonic-gate 
25200Sstevel@tonic-gate 	datalen = inlen - hash->confound_len - hash->hash_len;
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 	/* Move just the decrypted input into place if necessary */
25230Sstevel@tonic-gate 	if (hash->confound_len > 0 || hash->hash_len > 0) {
25240Sstevel@tonic-gate 		if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1)
25250Sstevel@tonic-gate 			mp->b_rptr += hash->confound_len;
25260Sstevel@tonic-gate 		else
25270Sstevel@tonic-gate 			mp->b_rptr += hash->confound_len + hash->hash_len;
25280Sstevel@tonic-gate 	}
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	ASSERT(mp->b_rptr + datalen <= DB_LIM(mp));
25310Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + datalen;
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	return (mp);
25340Sstevel@tonic-gate }
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate static mblk_t *
25370Sstevel@tonic-gate do_decrypt(queue_t *q, mblk_t *mp)
25380Sstevel@tonic-gate {
25390Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
25400Sstevel@tonic-gate 	mblk_t *outmp;
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	switch (tmi->dec_data.method) {
25430Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CFB:
25440Sstevel@tonic-gate 		outmp = des_cfb_decrypt(q, tmi, mp);
25450Sstevel@tonic-gate 		break;
25460Sstevel@tonic-gate 	case CRYPT_METHOD_NONE:
25470Sstevel@tonic-gate 		outmp = mp;
25480Sstevel@tonic-gate 		break;
25490Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_NULL:
25500Sstevel@tonic-gate 		outmp = des_cbc_decrypt(q, tmi, mp, &null_hash);
25510Sstevel@tonic-gate 		break;
25520Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_MD5:
25530Sstevel@tonic-gate 		outmp = des_cbc_decrypt(q, tmi, mp, &md5_hash);
25540Sstevel@tonic-gate 		break;
25550Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_CRC:
25560Sstevel@tonic-gate 		outmp = des_cbc_decrypt(q, tmi, mp, &crc32_hash);
25570Sstevel@tonic-gate 		break;
25580Sstevel@tonic-gate 	case CRYPT_METHOD_DES3_CBC_SHA1:
25590Sstevel@tonic-gate 		outmp = des_cbc_decrypt(q, tmi, mp, &sha1_hash);
25600Sstevel@tonic-gate 		break;
25610Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
25620Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
25630Sstevel@tonic-gate 		outmp = arcfour_hmac_md5_decrypt(q, tmi, mp, &md5_hash);
25640Sstevel@tonic-gate 		break;
25650Sstevel@tonic-gate 	case CRYPT_METHOD_AES128:
25660Sstevel@tonic-gate 	case CRYPT_METHOD_AES256:
25670Sstevel@tonic-gate 		outmp = aes_decrypt(q, tmi, mp, &sha1_hash);
25680Sstevel@tonic-gate 		break;
25690Sstevel@tonic-gate 	}
25700Sstevel@tonic-gate 	return (outmp);
25710Sstevel@tonic-gate }
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate /*
25740Sstevel@tonic-gate  * do_encrypt
25750Sstevel@tonic-gate  *
25760Sstevel@tonic-gate  * Generic encryption routine for a single message block.
25770Sstevel@tonic-gate  * The input mblk may be replaced by some encrypt routines
25780Sstevel@tonic-gate  * because they add extra data in some cases that may exceed
25790Sstevel@tonic-gate  * the input mblk_t size limit.
25800Sstevel@tonic-gate  */
25810Sstevel@tonic-gate static mblk_t *
25820Sstevel@tonic-gate do_encrypt(queue_t *q, mblk_t *mp)
25830Sstevel@tonic-gate {
25840Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
25850Sstevel@tonic-gate 	mblk_t *outmp;
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	switch (tmi->enc_data.method) {
25880Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CFB:
25890Sstevel@tonic-gate 		outmp = des_cfb_encrypt(q, tmi, mp);
25900Sstevel@tonic-gate 		break;
25910Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_NULL:
25920Sstevel@tonic-gate 		outmp = des_cbc_encrypt(q, tmi, mp, &null_hash);
25930Sstevel@tonic-gate 		break;
25940Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_MD5:
25950Sstevel@tonic-gate 		outmp = des_cbc_encrypt(q, tmi, mp, &md5_hash);
25960Sstevel@tonic-gate 		break;
25970Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_CRC:
25980Sstevel@tonic-gate 		outmp = des_cbc_encrypt(q, tmi, mp, &crc32_hash);
25990Sstevel@tonic-gate 		break;
26000Sstevel@tonic-gate 	case CRYPT_METHOD_DES3_CBC_SHA1:
26010Sstevel@tonic-gate 		outmp = des_cbc_encrypt(q, tmi, mp, &sha1_hash);
26020Sstevel@tonic-gate 		break;
26030Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
26040Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
26050Sstevel@tonic-gate 		outmp = arcfour_hmac_md5_encrypt(q, tmi, mp, &md5_hash);
26060Sstevel@tonic-gate 		break;
26070Sstevel@tonic-gate 	case CRYPT_METHOD_AES128:
26080Sstevel@tonic-gate 	case CRYPT_METHOD_AES256:
26090Sstevel@tonic-gate 		outmp = aes_encrypt(q, tmi, mp, &sha1_hash);
26100Sstevel@tonic-gate 		break;
26110Sstevel@tonic-gate 	case CRYPT_METHOD_NONE:
26120Sstevel@tonic-gate 		outmp = mp;
26130Sstevel@tonic-gate 		break;
26140Sstevel@tonic-gate 	}
26150Sstevel@tonic-gate 	return (outmp);
26160Sstevel@tonic-gate }
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate /*
26190Sstevel@tonic-gate  * setup_crypto
26200Sstevel@tonic-gate  *
26210Sstevel@tonic-gate  * This takes the data from the CRYPTIOCSETUP ioctl
26220Sstevel@tonic-gate  * and sets up a cipher_data_t structure for either
26230Sstevel@tonic-gate  * encryption or decryption.  This is where the
26240Sstevel@tonic-gate  * key and initialization vector data get stored
26250Sstevel@tonic-gate  * prior to beginning any crypto functions.
26260Sstevel@tonic-gate  *
26270Sstevel@tonic-gate  * Special note:
26280Sstevel@tonic-gate  *   Some applications(e.g. telnetd) have ability to switch
26290Sstevel@tonic-gate  * crypto on/off periodically.  Thus, the application may call
26300Sstevel@tonic-gate  * the CRYPTIOCSETUP ioctl many times for the same stream.
26310Sstevel@tonic-gate  * If the CRYPTIOCSETUP is called with 0 length key or ivec fields
26320Sstevel@tonic-gate  * assume that the key, block, and saveblock fields that are already
26330Sstevel@tonic-gate  * set from a previous CRIOCSETUP call are still valid.  This helps avoid
26340Sstevel@tonic-gate  * a rekeying error that could occur if we overwrite these fields
26350Sstevel@tonic-gate  * with each CRYPTIOCSETUP call.
26360Sstevel@tonic-gate  *   In short, sometimes, CRYPTIOCSETUP is used to simply toggle on/off
26370Sstevel@tonic-gate  * without resetting the original crypto parameters.
26380Sstevel@tonic-gate  *
26390Sstevel@tonic-gate  */
26400Sstevel@tonic-gate static int
26410Sstevel@tonic-gate setup_crypto(struct cr_info_t *ci, struct cipher_data_t *cd, int encrypt)
26420Sstevel@tonic-gate {
26430Sstevel@tonic-gate 	uint_t newblocklen;
26440Sstevel@tonic-gate 	uint32_t enc_usage = 0, dec_usage = 0;
26450Sstevel@tonic-gate 	int rv;
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	/*
26480Sstevel@tonic-gate 	 * Initial sanity checks
26490Sstevel@tonic-gate 	 */
26500Sstevel@tonic-gate 	if (!CR_METHOD_OK(ci->crypto_method)) {
26510Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal crypto method (%d)",
26520Sstevel@tonic-gate 			ci->crypto_method);
26530Sstevel@tonic-gate 		return (EINVAL);
26540Sstevel@tonic-gate 	}
26550Sstevel@tonic-gate 	if (!CR_OPTIONS_OK(ci->option_mask)) {
26560Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal crypto options (%d)",
26570Sstevel@tonic-gate 			ci->option_mask);
26580Sstevel@tonic-gate 		return (EINVAL);
26590Sstevel@tonic-gate 	}
26600Sstevel@tonic-gate 	if (!CR_IVUSAGE_OK(ci->ivec_usage)) {
26610Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal ivec usage value (%d)",
26620Sstevel@tonic-gate 			ci->ivec_usage);
26630Sstevel@tonic-gate 		return (EINVAL);
26640Sstevel@tonic-gate 	}
26650Sstevel@tonic-gate 
26660Sstevel@tonic-gate 	cd->method = ci->crypto_method;
26670Sstevel@tonic-gate 	cd->bytes = 0;
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate 	if (ci->keylen > 0) {
26700Sstevel@tonic-gate 		if (cd->key != NULL) {
26710Sstevel@tonic-gate 			kmem_free(cd->key, cd->keylen);
26720Sstevel@tonic-gate 			cd->key = NULL;
26730Sstevel@tonic-gate 			cd->keylen = 0;
26740Sstevel@tonic-gate 		}
26750Sstevel@tonic-gate 		/*
26760Sstevel@tonic-gate 		 * cd->key holds the copy of the raw key bytes passed in
26770Sstevel@tonic-gate 		 * from the userland app.
26780Sstevel@tonic-gate 		 */
26790Sstevel@tonic-gate 		cd->key = (char *)kmem_alloc((size_t)ci->keylen, KM_SLEEP);
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate 		cd->keylen = ci->keylen;
26820Sstevel@tonic-gate 		bcopy(ci->key, cd->key, (size_t)ci->keylen);
26830Sstevel@tonic-gate 	}
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	/*
26860Sstevel@tonic-gate 	 * Configure the block size based on the type of cipher.
26870Sstevel@tonic-gate 	 */
26880Sstevel@tonic-gate 	switch (cd->method) {
26890Sstevel@tonic-gate 		case CRYPT_METHOD_NONE:
26900Sstevel@tonic-gate 			newblocklen = 0;
26910Sstevel@tonic-gate 			break;
26920Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CFB:
26930Sstevel@tonic-gate 			newblocklen = DEFAULT_DES_BLOCKLEN;
26940Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_DES_ECB);
26950Sstevel@tonic-gate 			break;
26960Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CBC_NULL:
26970Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CBC_MD5:
26980Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CBC_CRC:
26990Sstevel@tonic-gate 			newblocklen = DEFAULT_DES_BLOCKLEN;
27000Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_DES_CBC);
27010Sstevel@tonic-gate 			break;
27020Sstevel@tonic-gate 		case CRYPT_METHOD_DES3_CBC_SHA1:
27030Sstevel@tonic-gate 			newblocklen = DEFAULT_DES_BLOCKLEN;
27040Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_DES3_CBC);
27050Sstevel@tonic-gate 			/* 3DES always uses the old usage constant */
27060Sstevel@tonic-gate 			enc_usage = RCMDV1_USAGE;
27070Sstevel@tonic-gate 			dec_usage = RCMDV1_USAGE;
27080Sstevel@tonic-gate 			break;
27090Sstevel@tonic-gate 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
27100Sstevel@tonic-gate 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
27110Sstevel@tonic-gate 			newblocklen = 0;
27120Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_RC4);
27130Sstevel@tonic-gate 			break;
27140Sstevel@tonic-gate 		case CRYPT_METHOD_AES128:
27150Sstevel@tonic-gate 		case CRYPT_METHOD_AES256:
27160Sstevel@tonic-gate 			newblocklen = DEFAULT_AES_BLOCKLEN;
27170Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_AES_ECB);
27180Sstevel@tonic-gate 			enc_usage = AES_ENCRYPT_USAGE;
27190Sstevel@tonic-gate 			dec_usage = AES_DECRYPT_USAGE;
27200Sstevel@tonic-gate 			break;
27210Sstevel@tonic-gate 	}
27220Sstevel@tonic-gate 	if (cd->mech_type == CRYPTO_MECH_INVALID) {
27230Sstevel@tonic-gate 		return (CRYPTO_FAILED);
27240Sstevel@tonic-gate 	}
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	/*
27270Sstevel@tonic-gate 	 * If RC4, initialize the master crypto key used by
27280Sstevel@tonic-gate 	 * the RC4 algorithm to derive the final encrypt and decrypt keys.
27290Sstevel@tonic-gate 	 */
27300Sstevel@tonic-gate 	if (cd->keylen > 0 && IS_RC4_METHOD(cd->method)) {
27310Sstevel@tonic-gate 		/*
27320Sstevel@tonic-gate 		 * cd->ckey is a kernel crypto key structure used as the
27330Sstevel@tonic-gate 		 * master key in the RC4-HMAC crypto operations.
27340Sstevel@tonic-gate 		 */
27350Sstevel@tonic-gate 		if (cd->ckey == NULL) {
27360Sstevel@tonic-gate 			cd->ckey = (crypto_key_t *)kmem_zalloc(
27370Sstevel@tonic-gate 				sizeof (crypto_key_t), KM_SLEEP);
27380Sstevel@tonic-gate 		}
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 		cd->ckey->ck_format = CRYPTO_KEY_RAW;
27410Sstevel@tonic-gate 		cd->ckey->ck_data = cd->key;
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 		/* key length for EF is measured in bits */
27440Sstevel@tonic-gate 		cd->ckey->ck_length = cd->keylen * 8;
27450Sstevel@tonic-gate 	}
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 	/*
27480Sstevel@tonic-gate 	 * cd->block and cd->saveblock are used as temporary storage for
27490Sstevel@tonic-gate 	 * data that must be carried over between encrypt/decrypt operations
27500Sstevel@tonic-gate 	 * in some of the "feedback" modes.
27510Sstevel@tonic-gate 	 */
27520Sstevel@tonic-gate 	if (newblocklen != cd->blocklen) {
27530Sstevel@tonic-gate 		if (cd->block != NULL) {
27540Sstevel@tonic-gate 			kmem_free(cd->block, cd->blocklen);
27550Sstevel@tonic-gate 			cd->block = NULL;
27560Sstevel@tonic-gate 		}
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 		if (cd->saveblock != NULL) {
27590Sstevel@tonic-gate 			kmem_free(cd->saveblock, cd->blocklen);
27600Sstevel@tonic-gate 			cd->saveblock = NULL;
27610Sstevel@tonic-gate 		}
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate 		cd->blocklen = newblocklen;
27640Sstevel@tonic-gate 		if (cd->blocklen) {
27650Sstevel@tonic-gate 			cd->block = (char *)kmem_zalloc((size_t)cd->blocklen,
27660Sstevel@tonic-gate 				KM_SLEEP);
27670Sstevel@tonic-gate 		}
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 		if (cd->method == CRYPT_METHOD_DES_CFB)
27700Sstevel@tonic-gate 			cd->saveblock = (char *)kmem_zalloc(cd->blocklen,
27710Sstevel@tonic-gate 						KM_SLEEP);
27720Sstevel@tonic-gate 		else
27730Sstevel@tonic-gate 			cd->saveblock = NULL;
27740Sstevel@tonic-gate 	}
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 	if (ci->iveclen != cd->ivlen) {
27770Sstevel@tonic-gate 		if (cd->ivec != NULL) {
27780Sstevel@tonic-gate 			kmem_free(cd->ivec, cd->ivlen);
27790Sstevel@tonic-gate 			cd->ivec = NULL;
27800Sstevel@tonic-gate 		}
27810Sstevel@tonic-gate 		if (ci->ivec_usage != IVEC_NEVER && ci->iveclen > 0) {
27820Sstevel@tonic-gate 			cd->ivec = (char *)kmem_zalloc((size_t)ci->iveclen,
27830Sstevel@tonic-gate 						KM_SLEEP);
27840Sstevel@tonic-gate 			cd->ivlen = ci->iveclen;
27850Sstevel@tonic-gate 		} else {
27860Sstevel@tonic-gate 			cd->ivlen = 0;
27870Sstevel@tonic-gate 			cd->ivec = NULL;
27880Sstevel@tonic-gate 		}
27890Sstevel@tonic-gate 	}
27900Sstevel@tonic-gate 	cd->option_mask = ci->option_mask;
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 	/*
27930Sstevel@tonic-gate 	 * Old protocol requires a static 'usage' value for
27940Sstevel@tonic-gate 	 * deriving keys.  Yuk.
27950Sstevel@tonic-gate 	 */
27960Sstevel@tonic-gate 	if (cd->option_mask & CRYPTOPT_RCMD_MODE_V1) {
27970Sstevel@tonic-gate 		enc_usage = dec_usage = RCMDV1_USAGE;
27980Sstevel@tonic-gate 	}
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 	if (cd->ivlen > cd->blocklen) {
28010Sstevel@tonic-gate 		cmn_err(CE_WARN, "setup_crypto: IV longer than block size");
28020Sstevel@tonic-gate 		return (EINVAL);
28030Sstevel@tonic-gate 	}
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate 	/*
28060Sstevel@tonic-gate 	 * If we are using an IVEC "correctly" (i.e. set it once)
28070Sstevel@tonic-gate 	 * copy it here.
28080Sstevel@tonic-gate 	 */
28090Sstevel@tonic-gate 	if (ci->ivec_usage == IVEC_ONETIME && cd->block != NULL)
28100Sstevel@tonic-gate 		bcopy(ci->ivec, cd->block, (size_t)cd->ivlen);
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 	cd->ivec_usage = ci->ivec_usage;
28130Sstevel@tonic-gate 	if (cd->ivec != NULL) {
28140Sstevel@tonic-gate 		/* Save the original IVEC in case we need it later */
28150Sstevel@tonic-gate 		bcopy(ci->ivec, cd->ivec, (size_t)cd->ivlen);
28160Sstevel@tonic-gate 	}
28170Sstevel@tonic-gate 	/*
28180Sstevel@tonic-gate 	 * Special handling for 3DES-SHA1-HMAC and AES crypto:
28190Sstevel@tonic-gate 	 * generate derived keys and context templates
28200Sstevel@tonic-gate 	 * for better performance.
28210Sstevel@tonic-gate 	 */
28220Sstevel@tonic-gate 	if (cd->method == CRYPT_METHOD_DES3_CBC_SHA1 ||
28230Sstevel@tonic-gate 	    IS_AES_METHOD(cd->method)) {
28240Sstevel@tonic-gate 		crypto_mechanism_t enc_mech;
28250Sstevel@tonic-gate 		crypto_mechanism_t hmac_mech;
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 		if (cd->d_encr_key.ck_data != NULL) {
28280Sstevel@tonic-gate 			bzero(cd->d_encr_key.ck_data, cd->keylen);
28290Sstevel@tonic-gate 			kmem_free(cd->d_encr_key.ck_data, cd->keylen);
28300Sstevel@tonic-gate 		}
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 		if (cd->d_hmac_key.ck_data != NULL) {
28330Sstevel@tonic-gate 			bzero(cd->d_hmac_key.ck_data, cd->keylen);
28340Sstevel@tonic-gate 			kmem_free(cd->d_hmac_key.ck_data, cd->keylen);
28350Sstevel@tonic-gate 		}
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate 		if (cd->enc_tmpl != NULL)
28380Sstevel@tonic-gate 			(void) crypto_destroy_ctx_template(cd->enc_tmpl);
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 		if (cd->hmac_tmpl != NULL)
28410Sstevel@tonic-gate 			(void) crypto_destroy_ctx_template(cd->hmac_tmpl);
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate 		enc_mech.cm_type = cd->mech_type;
28440Sstevel@tonic-gate 		enc_mech.cm_param = cd->ivec;
28450Sstevel@tonic-gate 		enc_mech.cm_param_len = cd->ivlen;
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 		hmac_mech.cm_type = sha1_hmac_mech;
28480Sstevel@tonic-gate 		hmac_mech.cm_param = NULL;
28490Sstevel@tonic-gate 		hmac_mech.cm_param_len = 0;
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate 		/*
28520Sstevel@tonic-gate 		 * Create the derived keys.
28530Sstevel@tonic-gate 		 */
28540Sstevel@tonic-gate 		rv = create_derived_keys(cd,
28550Sstevel@tonic-gate 			(encrypt ? enc_usage : dec_usage),
28560Sstevel@tonic-gate 			&cd->d_encr_key, &cd->d_hmac_key);
28570Sstevel@tonic-gate 
28580Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
28590Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to create derived "
28600Sstevel@tonic-gate 				"keys: %0x", rv);
28610Sstevel@tonic-gate 			return (CRYPTO_FAILED);
28620Sstevel@tonic-gate 		}
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 		rv = crypto_create_ctx_template(&enc_mech,
28650Sstevel@tonic-gate 					&cd->d_encr_key,
28660Sstevel@tonic-gate 					&cd->enc_tmpl, KM_SLEEP);
28670Sstevel@tonic-gate 		if (rv == CRYPTO_MECH_NOT_SUPPORTED) {
28680Sstevel@tonic-gate 			cd->enc_tmpl = NULL;
28690Sstevel@tonic-gate 		} else if (rv != CRYPTO_SUCCESS) {
28700Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to create enc template "
28710Sstevel@tonic-gate 				"for d_encr_key: %0x", rv);
28720Sstevel@tonic-gate 			return (CRYPTO_FAILED);
28730Sstevel@tonic-gate 		}
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 		rv = crypto_create_ctx_template(&hmac_mech,
28760Sstevel@tonic-gate 				&cd->d_hmac_key,
28770Sstevel@tonic-gate 				&cd->hmac_tmpl, KM_SLEEP);
28780Sstevel@tonic-gate 		if (rv == CRYPTO_MECH_NOT_SUPPORTED) {
28790Sstevel@tonic-gate 			cd->hmac_tmpl = NULL;
28800Sstevel@tonic-gate 		} else if (rv != CRYPTO_SUCCESS) {
28810Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to create hmac template:"
28820Sstevel@tonic-gate 				" %0x", rv);
28830Sstevel@tonic-gate 			return (CRYPTO_FAILED);
28840Sstevel@tonic-gate 		}
28850Sstevel@tonic-gate 	} else if (IS_RC4_METHOD(cd->method)) {
28860Sstevel@tonic-gate 		bzero(&cd->d_encr_key, sizeof (crypto_key_t));
28870Sstevel@tonic-gate 		bzero(&cd->d_hmac_key, sizeof (crypto_key_t));
28880Sstevel@tonic-gate 		cd->ctx = NULL;
28890Sstevel@tonic-gate 		cd->enc_tmpl = NULL;
28900Sstevel@tonic-gate 		cd->hmac_tmpl = NULL;
28910Sstevel@tonic-gate 	}
28920Sstevel@tonic-gate 
28930Sstevel@tonic-gate 	/* Final sanity checks, make sure no fields are NULL */
28940Sstevel@tonic-gate 	if (cd->method != CRYPT_METHOD_NONE) {
28950Sstevel@tonic-gate 		if (cd->block == NULL && cd->blocklen > 0) {
28960Sstevel@tonic-gate #ifdef DEBUG
28970Sstevel@tonic-gate 			cmn_err(CE_WARN,
28980Sstevel@tonic-gate 				"setup_crypto: IV block not allocated");
28990Sstevel@tonic-gate #endif
29000Sstevel@tonic-gate 			return (ENOMEM);
29010Sstevel@tonic-gate 		}
29020Sstevel@tonic-gate 		if (cd->key == NULL && cd->keylen > 0) {
29030Sstevel@tonic-gate #ifdef DEBUG
29040Sstevel@tonic-gate 			cmn_err(CE_WARN,
29050Sstevel@tonic-gate 				"setup_crypto: key block not allocated");
29060Sstevel@tonic-gate #endif
29070Sstevel@tonic-gate 			return (ENOMEM);
29080Sstevel@tonic-gate 		}
29090Sstevel@tonic-gate 		if (cd->method == CRYPT_METHOD_DES_CFB &&
29100Sstevel@tonic-gate 		    cd->saveblock == NULL && cd->blocklen > 0) {
29110Sstevel@tonic-gate #ifdef DEBUG
29120Sstevel@tonic-gate 			cmn_err(CE_WARN,
29130Sstevel@tonic-gate 				"setup_crypto: save block not allocated");
29140Sstevel@tonic-gate #endif
29150Sstevel@tonic-gate 			return (ENOMEM);
29160Sstevel@tonic-gate 		}
29170Sstevel@tonic-gate 		if (cd->ivec == NULL && cd->ivlen > 0) {
29180Sstevel@tonic-gate #ifdef DEBUG
29190Sstevel@tonic-gate 			cmn_err(CE_WARN,
29200Sstevel@tonic-gate 				"setup_crypto: IV not allocated");
29210Sstevel@tonic-gate #endif
29220Sstevel@tonic-gate 			return (ENOMEM);
29230Sstevel@tonic-gate 		}
29240Sstevel@tonic-gate 	}
29250Sstevel@tonic-gate 	return (0);
29260Sstevel@tonic-gate }
29270Sstevel@tonic-gate 
29280Sstevel@tonic-gate /*
29290Sstevel@tonic-gate  * RCMDS require a 4 byte, clear text
29300Sstevel@tonic-gate  * length field before each message.
29310Sstevel@tonic-gate  * Add it now.
29320Sstevel@tonic-gate  */
29330Sstevel@tonic-gate static mblk_t *
29340Sstevel@tonic-gate mklenmp(mblk_t *bp, uint32_t len)
29350Sstevel@tonic-gate {
29360Sstevel@tonic-gate 	mblk_t *lenmp;
29370Sstevel@tonic-gate 	uchar_t *ucp;
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 	if (bp->b_rptr - 4 < DB_BASE(bp) || DB_REF(bp) > 1) {
29400Sstevel@tonic-gate 		lenmp = allocb(4, BPRI_MED);
29410Sstevel@tonic-gate 		if (lenmp != NULL) {
29420Sstevel@tonic-gate 			lenmp->b_rptr = lenmp->b_wptr = DB_LIM(lenmp);
29430Sstevel@tonic-gate 			linkb(lenmp, bp);
29440Sstevel@tonic-gate 			bp = lenmp;
29450Sstevel@tonic-gate 		}
29460Sstevel@tonic-gate 	}
29470Sstevel@tonic-gate 	ucp = bp->b_rptr;
29480Sstevel@tonic-gate 	*--ucp = len;
29490Sstevel@tonic-gate 	*--ucp = len >> 8;
29500Sstevel@tonic-gate 	*--ucp = len >> 16;
29510Sstevel@tonic-gate 	*--ucp = len >> 24;
29520Sstevel@tonic-gate 
29530Sstevel@tonic-gate 	bp->b_rptr = ucp;
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 	return (bp);
29560Sstevel@tonic-gate }
29570Sstevel@tonic-gate 
29583518Spk193450 static mblk_t *
29593518Spk193450 encrypt_block(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, size_t plainlen)
29603518Spk193450 {
29613518Spk193450 	mblk_t *newmp;
29623518Spk193450 	size_t headspace;
29633518Spk193450 
29643518Spk193450 	mblk_t *cbp;
29653518Spk193450 	size_t cipherlen;
29663518Spk193450 	size_t extra = 0;
29673518Spk193450 	uint32_t ptlen = (uint32_t)plainlen;
29683518Spk193450 	/*
29693518Spk193450 	 * If we are using the "NEW" RCMD mode,
29703518Spk193450 	 * add 4 bytes to the plaintext for the
29713518Spk193450 	 * plaintext length that gets prepended
29723518Spk193450 	 * before encrypting.
29733518Spk193450 	 */
29743518Spk193450 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
29753518Spk193450 		ptlen += 4;
29763518Spk193450 
29773518Spk193450 	cipherlen = encrypt_size(&tmi->enc_data, (size_t)ptlen);
29783518Spk193450 
29793518Spk193450 	/*
29803518Spk193450 	 * if we must allocb, then make sure its enough
29813518Spk193450 	 * to hold the length field so we dont have to allocb
29823518Spk193450 	 * again down below in 'mklenmp'
29833518Spk193450 	 */
29843518Spk193450 	if (ANY_RCMD_MODE(tmi->enc_data.option_mask)) {
29853518Spk193450 		extra = sizeof (uint32_t);
29863518Spk193450 	}
29873518Spk193450 
29883518Spk193450 	/*
29893518Spk193450 	 * Calculate how much space is needed in front of
29903518Spk193450 	 * the data.
29913518Spk193450 	 */
29923518Spk193450 	headspace = plaintext_offset(&tmi->enc_data);
29933518Spk193450 
29943518Spk193450 	/*
29953518Spk193450 	 * If the current block is too small, reallocate
29963518Spk193450 	 * one large enough to hold the hdr, tail, and
29973518Spk193450 	 * ciphertext.
29983518Spk193450 	 */
29993518Spk193450 	if ((cipherlen + extra >= MBLKSIZE(mp)) || DB_REF(mp) > 1) {
30003518Spk193450 		int sz = P2ROUNDUP(cipherlen+extra, 8);
30013518Spk193450 
30023518Spk193450 		cbp = allocb_tmpl(sz, mp);
30033518Spk193450 		if (cbp == NULL) {
30043518Spk193450 			cmn_err(CE_WARN,
30053518Spk193450 				"allocb (%d bytes) failed", sz);
30063518Spk193450 				return (NULL);
30073518Spk193450 		}
30083518Spk193450 
30093518Spk193450 		cbp->b_cont = mp->b_cont;
30103518Spk193450 
30113518Spk193450 		/*
30123518Spk193450 		 * headspace includes the length fields needed
30133518Spk193450 		 * for the RCMD modes (v1 == 4 bytes, V2 = 8)
30143518Spk193450 		 */
30153518Spk193450 		cbp->b_rptr = DB_BASE(cbp) + headspace;
30163518Spk193450 
30173518Spk193450 		ASSERT(cbp->b_rptr + P2ROUNDUP(plainlen, 8)
30183518Spk193450 			<= DB_LIM(cbp));
30193518Spk193450 
30203518Spk193450 		bcopy(mp->b_rptr, cbp->b_rptr, plainlen);
30213518Spk193450 		cbp->b_wptr = cbp->b_rptr + plainlen;
30223518Spk193450 
30233518Spk193450 		freeb(mp);
30243518Spk193450 	} else {
30253518Spk193450 		size_t extra = 0;
30263518Spk193450 		cbp = mp;
30273518Spk193450 
30283518Spk193450 		/*
30293518Spk193450 		 * Some ciphers add HMAC after the final block
30303518Spk193450 		 * of the ciphertext, not at the beginning like the
30313518Spk193450 		 * 1-DES ciphers.
30323518Spk193450 		 */
30333518Spk193450 		if (tmi->enc_data.method ==
30343518Spk193450 			CRYPT_METHOD_DES3_CBC_SHA1 ||
30353518Spk193450 		    IS_AES_METHOD(tmi->enc_data.method)) {
30363518Spk193450 			extra = sha1_hash.hash_len;
30373518Spk193450 		}
30383518Spk193450 
30393518Spk193450 		/*
30403518Spk193450 		 * Make sure the rptr is positioned correctly so that
30413518Spk193450 		 * routines later do not have to shift this data around
30423518Spk193450 		 */
30433518Spk193450 		if ((cbp->b_rptr + P2ROUNDUP(plainlen + extra, 8) >
30443518Spk193450 			DB_LIM(cbp)) ||
30453518Spk193450 			(cbp->b_rptr - headspace < DB_BASE(cbp))) {
30463518Spk193450 			ovbcopy(cbp->b_rptr, DB_BASE(cbp) + headspace,
30473518Spk193450 				plainlen);
30483518Spk193450 			cbp->b_rptr = DB_BASE(cbp) + headspace;
30493518Spk193450 			cbp->b_wptr = cbp->b_rptr + plainlen;
30503518Spk193450 		}
30513518Spk193450 	}
30523518Spk193450 
30533518Spk193450 	ASSERT(cbp->b_rptr - headspace >= DB_BASE(cbp));
30543518Spk193450 	ASSERT(cbp->b_wptr <= DB_LIM(cbp));
30553518Spk193450 
30563518Spk193450 	/*
30573518Spk193450 	 * If using RCMD_MODE_V2 (new rcmd mode), prepend
30583518Spk193450 	 * the plaintext length before the actual plaintext.
30593518Spk193450 	 */
30603518Spk193450 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2) {
30613518Spk193450 		cbp->b_rptr -= RCMD_LEN_SZ;
30623518Spk193450 
30633518Spk193450 		/* put plaintext length at head of buffer */
30643518Spk193450 		*(cbp->b_rptr + 3) = (uchar_t)(plainlen & 0xff);
30653518Spk193450 		*(cbp->b_rptr + 2) = (uchar_t)((plainlen >> 8) & 0xff);
30663518Spk193450 		*(cbp->b_rptr + 1) = (uchar_t)((plainlen >> 16) & 0xff);
30673518Spk193450 		*(cbp->b_rptr) = (uchar_t)((plainlen >> 24) & 0xff);
30683518Spk193450 	}
30693518Spk193450 
30703518Spk193450 	newmp = do_encrypt(q, cbp);
30713518Spk193450 
30723518Spk193450 	if (newmp != NULL &&
30733518Spk193450 	    (tmi->enc_data.option_mask &
30743518Spk193450 	    (CRYPTOPT_RCMD_MODE_V1 | CRYPTOPT_RCMD_MODE_V2))) {
30753518Spk193450 		mblk_t *lp;
30763518Spk193450 		/*
30773518Spk193450 		 * Add length field, required when this is
30783518Spk193450 		 * used to encrypt "r*" commands(rlogin, rsh)
30793518Spk193450 		 * with Kerberos.
30803518Spk193450 		 */
30813518Spk193450 		lp = mklenmp(newmp, plainlen);
30823518Spk193450 
30833518Spk193450 		if (lp == NULL) {
30843518Spk193450 			freeb(newmp);
30853518Spk193450 			return (NULL);
30863518Spk193450 		} else {
30873518Spk193450 			newmp = lp;
30883518Spk193450 		}
30893518Spk193450 	}
30903518Spk193450 	return (newmp);
30913518Spk193450 }
30923518Spk193450 
30930Sstevel@tonic-gate /*
30940Sstevel@tonic-gate  * encrypt_msgb
30950Sstevel@tonic-gate  *
30960Sstevel@tonic-gate  * encrypt a single message. This routine adds the
30970Sstevel@tonic-gate  * RCMD overhead bytes when necessary.
30980Sstevel@tonic-gate  */
30990Sstevel@tonic-gate static mblk_t *
31000Sstevel@tonic-gate encrypt_msgb(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
31010Sstevel@tonic-gate {
31023518Spk193450 	size_t plainlen, outlen;
31033518Spk193450 	mblk_t *newmp = NULL;
31043518Spk193450 
31053518Spk193450 	/* If not encrypting, do nothing */
31060Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_NONE) {
31070Sstevel@tonic-gate 		return (mp);
31080Sstevel@tonic-gate 	}
31090Sstevel@tonic-gate 
31103518Spk193450 	plainlen = MBLKL(mp);
31113518Spk193450 	if (plainlen == 0)
31123518Spk193450 		return (NULL);
31133518Spk193450 
31140Sstevel@tonic-gate 	/*
31153518Spk193450 	 * If the block is too big, we encrypt in 4K chunks so that
31163518Spk193450 	 * older rlogin clients do not choke on the larger buffers.
31170Sstevel@tonic-gate 	 */
31183518Spk193450 	while ((plainlen = MBLKL(mp)) > MSGBUF_SIZE) {
31193518Spk193450 		mblk_t *mp1 = NULL;
31203518Spk193450 		outlen = MSGBUF_SIZE;
31210Sstevel@tonic-gate 		/*
31223518Spk193450 		 * Allocate a new buffer that is only 4K bytes, the
31233518Spk193450 		 * extra bytes are for crypto overhead.
31240Sstevel@tonic-gate 		 */
31253518Spk193450 		mp1 = allocb(outlen + CONFOUNDER_BYTES, BPRI_MED);
31263518Spk193450 		if (mp1 == NULL) {
31273518Spk193450 			cmn_err(CE_WARN,
31283518Spk193450 				"allocb (%d bytes) failed",
31293518Spk193450 				(int)(outlen + CONFOUNDER_BYTES));
31303518Spk193450 			return (NULL);
31310Sstevel@tonic-gate 		}
31323518Spk193450 		/* Copy the next 4K bytes from the old block. */
31333518Spk193450 		bcopy(mp->b_rptr, mp1->b_rptr, outlen);
31343518Spk193450 		mp1->b_wptr = mp1->b_rptr + outlen;
31353518Spk193450 		/* Advance the old block. */
31363518Spk193450 		mp->b_rptr += outlen;
31373518Spk193450 
31383518Spk193450 		/* encrypt the new block */
31393518Spk193450 		newmp = encrypt_block(q, tmi, mp1, outlen);
31403518Spk193450 		if (newmp == NULL)
31413518Spk193450 			return (NULL);
31423518Spk193450 
31433518Spk193450 		putnext(q, newmp);
31440Sstevel@tonic-gate 	}
31453518Spk193450 	newmp = NULL;
31463518Spk193450 	/* If there is data left (< MSGBUF_SIZE), encrypt it. */
3147*3532Spk193450 	if ((plainlen = MBLKL(mp)) > 0)
31483518Spk193450 		newmp = encrypt_block(q, tmi, mp, plainlen);
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate 	return (newmp);
31510Sstevel@tonic-gate }
31520Sstevel@tonic-gate 
31530Sstevel@tonic-gate /*
31540Sstevel@tonic-gate  * cryptmodwsrv
31550Sstevel@tonic-gate  *
31560Sstevel@tonic-gate  * Service routine for the write queue.
31570Sstevel@tonic-gate  *
31580Sstevel@tonic-gate  * Because data may be placed in the queue to hold between
31590Sstevel@tonic-gate  * the CRYPTIOCSTOP and CRYPTIOCSTART ioctls, the service routine is needed.
31600Sstevel@tonic-gate  */
31610Sstevel@tonic-gate static int
31620Sstevel@tonic-gate cryptmodwsrv(queue_t *q)
31630Sstevel@tonic-gate {
31640Sstevel@tonic-gate 	mblk_t *mp;
31650Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
31660Sstevel@tonic-gate 
31670Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
31680Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
31690Sstevel@tonic-gate 		default:
31700Sstevel@tonic-gate 			/*
31710Sstevel@tonic-gate 			 * wput does not queue anything > QPCTL
31720Sstevel@tonic-gate 			 */
31730Sstevel@tonic-gate 			if (!canputnext(q) ||
31740Sstevel@tonic-gate 			    !(tmi->ready & CRYPT_WRITE_READY)) {
31750Sstevel@tonic-gate 				if (!putbq(q, mp)) {
31760Sstevel@tonic-gate 					freemsg(mp);
31770Sstevel@tonic-gate 				}
31780Sstevel@tonic-gate 				return (0);
31790Sstevel@tonic-gate 			}
31800Sstevel@tonic-gate 			putnext(q, mp);
31810Sstevel@tonic-gate 			break;
31820Sstevel@tonic-gate 		case M_DATA:
31830Sstevel@tonic-gate 			if (canputnext(q) && (tmi->ready & CRYPT_WRITE_READY)) {
31840Sstevel@tonic-gate 				mblk_t *bp;
31850Sstevel@tonic-gate 				mblk_t *newmsg = NULL;
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 				/*
31880Sstevel@tonic-gate 				 * If multiple msgs, concat into 1
31890Sstevel@tonic-gate 				 * to minimize crypto operations later.
31900Sstevel@tonic-gate 				 */
31910Sstevel@tonic-gate 				if (mp->b_cont != NULL) {
31920Sstevel@tonic-gate 					bp = msgpullup(mp, -1);
31930Sstevel@tonic-gate 					if (bp != NULL) {
31940Sstevel@tonic-gate 						freemsg(mp);
31950Sstevel@tonic-gate 						mp = bp;
31960Sstevel@tonic-gate 					}
31970Sstevel@tonic-gate 				}
31980Sstevel@tonic-gate 				newmsg = encrypt_msgb(q, tmi, mp);
31990Sstevel@tonic-gate 				if (newmsg != NULL)
32000Sstevel@tonic-gate 					putnext(q, newmsg);
32010Sstevel@tonic-gate 			} else {
32020Sstevel@tonic-gate 				if (!putbq(q, mp)) {
32030Sstevel@tonic-gate 					freemsg(mp);
32040Sstevel@tonic-gate 				}
32050Sstevel@tonic-gate 				return (0);
32060Sstevel@tonic-gate 			}
32070Sstevel@tonic-gate 			break;
32080Sstevel@tonic-gate 		}
32090Sstevel@tonic-gate 	}
32100Sstevel@tonic-gate 	return (0);
32110Sstevel@tonic-gate }
32120Sstevel@tonic-gate 
32130Sstevel@tonic-gate static void
32140Sstevel@tonic-gate start_stream(queue_t *wq, mblk_t *mp, uchar_t dir)
32150Sstevel@tonic-gate {
32160Sstevel@tonic-gate 	mblk_t *newmp = NULL;
32170Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)wq->q_ptr;
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate 	if (dir == CRYPT_ENCRYPT) {
32200Sstevel@tonic-gate 		tmi->ready |= CRYPT_WRITE_READY;
32210Sstevel@tonic-gate 		(void) (STRLOG(CRYPTMOD_ID, 0, 5, SL_TRACE|SL_NOTE,
32220Sstevel@tonic-gate 				"start_stream: restart ENCRYPT/WRITE q"));
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate 		enableok(wq);
32250Sstevel@tonic-gate 		qenable(wq);
32260Sstevel@tonic-gate 	} else if (dir == CRYPT_DECRYPT) {
32270Sstevel@tonic-gate 		/*
32280Sstevel@tonic-gate 		 * put any extra data in the RD
32290Sstevel@tonic-gate 		 * queue to be processed and
32300Sstevel@tonic-gate 		 * sent back up.
32310Sstevel@tonic-gate 		 */
32320Sstevel@tonic-gate 		newmp = mp->b_cont;
32330Sstevel@tonic-gate 		mp->b_cont = NULL;
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate 		tmi->ready |= CRYPT_READ_READY;
32360Sstevel@tonic-gate 		(void) (STRLOG(CRYPTMOD_ID, 0, 5,
32370Sstevel@tonic-gate 				SL_TRACE|SL_NOTE,
32380Sstevel@tonic-gate 				"start_stream: restart "
32390Sstevel@tonic-gate 				"DECRYPT/READ q"));
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate 		if (newmp != NULL)
32420Sstevel@tonic-gate 			if (!putbq(RD(wq), newmp))
32430Sstevel@tonic-gate 				freemsg(newmp);
32440Sstevel@tonic-gate 
32450Sstevel@tonic-gate 		enableok(RD(wq));
32460Sstevel@tonic-gate 		qenable(RD(wq));
32470Sstevel@tonic-gate 	}
32480Sstevel@tonic-gate 
32490Sstevel@tonic-gate 	miocack(wq, mp, 0, 0);
32500Sstevel@tonic-gate }
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate /*
32530Sstevel@tonic-gate  * Write-side put procedure.  Its main task is to detect ioctls and
32540Sstevel@tonic-gate  * FLUSH operations.  Other message types are passed on through.
32550Sstevel@tonic-gate  */
32560Sstevel@tonic-gate static void
32570Sstevel@tonic-gate cryptmodwput(queue_t *wq, mblk_t *mp)
32580Sstevel@tonic-gate {
32590Sstevel@tonic-gate 	struct iocblk *iocp;
32600Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)wq->q_ptr;
32610Sstevel@tonic-gate 	int ret, err;
32620Sstevel@tonic-gate 
32630Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
32640Sstevel@tonic-gate 	case M_DATA:
32650Sstevel@tonic-gate 		if (wq->q_first == NULL && canputnext(wq) &&
32660Sstevel@tonic-gate 		    (tmi->ready & CRYPT_WRITE_READY) &&
32670Sstevel@tonic-gate 		    tmi->enc_data.method == CRYPT_METHOD_NONE) {
32680Sstevel@tonic-gate 			putnext(wq, mp);
32690Sstevel@tonic-gate 			return;
32700Sstevel@tonic-gate 		}
32710Sstevel@tonic-gate 		/* else, put it in the service queue */
32720Sstevel@tonic-gate 		if (!putq(wq, mp)) {
32730Sstevel@tonic-gate 			freemsg(mp);
32740Sstevel@tonic-gate 		}
32750Sstevel@tonic-gate 		break;
32760Sstevel@tonic-gate 	case M_FLUSH:
32770Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
32780Sstevel@tonic-gate 			flushq(wq, FLUSHDATA);
32790Sstevel@tonic-gate 		}
32800Sstevel@tonic-gate 		putnext(wq, mp);
32810Sstevel@tonic-gate 		break;
32820Sstevel@tonic-gate 	case M_IOCTL:
32830Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
32840Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
32850Sstevel@tonic-gate 		case CRYPTIOCSETUP:
32860Sstevel@tonic-gate 			ret = 0;
32870Sstevel@tonic-gate 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
32880Sstevel@tonic-gate 					SL_TRACE | SL_NOTE,
32890Sstevel@tonic-gate 					"wput: got CRYPTIOCSETUP "
32900Sstevel@tonic-gate 					"ioctl(%d)", iocp->ioc_cmd));
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 			if ((err = miocpullup(mp,
32930Sstevel@tonic-gate 					sizeof (struct cr_info_t))) != 0) {
32940Sstevel@tonic-gate 				cmn_err(CE_WARN,
32950Sstevel@tonic-gate 				"wput: miocpullup failed for cr_info_t");
32960Sstevel@tonic-gate 				miocnak(wq, mp, 0, err);
32970Sstevel@tonic-gate 			} else {
32980Sstevel@tonic-gate 				struct cr_info_t *ci;
32990Sstevel@tonic-gate 				ci = (struct cr_info_t *)mp->b_cont->b_rptr;
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 				if (ci->direction_mask & CRYPT_ENCRYPT) {
33020Sstevel@tonic-gate 				    ret = setup_crypto(ci, &tmi->enc_data, 1);
33030Sstevel@tonic-gate 				}
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 				if (ret == 0 &&
33060Sstevel@tonic-gate 				    (ci->direction_mask & CRYPT_DECRYPT)) {
33070Sstevel@tonic-gate 				    ret = setup_crypto(ci, &tmi->dec_data, 0);
33080Sstevel@tonic-gate 				}
33090Sstevel@tonic-gate 				if (ret == 0 &&
33100Sstevel@tonic-gate 				    (ci->direction_mask & CRYPT_DECRYPT) &&
33110Sstevel@tonic-gate 				    ANY_RCMD_MODE(tmi->dec_data.option_mask)) {
33120Sstevel@tonic-gate 					bzero(&tmi->rcmd_state,
33130Sstevel@tonic-gate 					    sizeof (tmi->rcmd_state));
33140Sstevel@tonic-gate 				}
33150Sstevel@tonic-gate 				if (ret == 0) {
33160Sstevel@tonic-gate 					miocack(wq, mp, 0, 0);
33170Sstevel@tonic-gate 				} else {
33180Sstevel@tonic-gate 					cmn_err(CE_WARN,
33190Sstevel@tonic-gate 						"wput: setup_crypto failed");
33200Sstevel@tonic-gate 					miocnak(wq, mp, 0, ret);
33210Sstevel@tonic-gate 				}
33220Sstevel@tonic-gate 				(void) (STRLOG(CRYPTMOD_ID, 0, 5,
33230Sstevel@tonic-gate 						SL_TRACE|SL_NOTE,
33240Sstevel@tonic-gate 						"wput: done with SETUP "
33250Sstevel@tonic-gate 						"ioctl"));
33260Sstevel@tonic-gate 			}
33270Sstevel@tonic-gate 			break;
33280Sstevel@tonic-gate 		case CRYPTIOCSTOP:
33290Sstevel@tonic-gate 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
33300Sstevel@tonic-gate 					SL_TRACE|SL_NOTE,
33310Sstevel@tonic-gate 					"wput: got CRYPTIOCSTOP "
33320Sstevel@tonic-gate 					"ioctl(%d)", iocp->ioc_cmd));
33330Sstevel@tonic-gate 
33340Sstevel@tonic-gate 			if ((err = miocpullup(mp, sizeof (uint32_t))) != 0) {
33350Sstevel@tonic-gate 				cmn_err(CE_WARN,
33360Sstevel@tonic-gate 					"wput: CRYPTIOCSTOP ioctl wrong "
33370Sstevel@tonic-gate 					"size (%d should be %d)",
33380Sstevel@tonic-gate 					(int)iocp->ioc_count,
33390Sstevel@tonic-gate 					(int)sizeof (uint32_t));
33400Sstevel@tonic-gate 				miocnak(wq, mp, 0, err);
33410Sstevel@tonic-gate 			} else {
33420Sstevel@tonic-gate 				uint32_t *stopdir;
33430Sstevel@tonic-gate 
33440Sstevel@tonic-gate 				stopdir = (uint32_t *)mp->b_cont->b_rptr;
33450Sstevel@tonic-gate 				if (!CR_DIRECTION_OK(*stopdir)) {
33460Sstevel@tonic-gate 					miocnak(wq, mp, 0, EINVAL);
33470Sstevel@tonic-gate 					return;
33480Sstevel@tonic-gate 				}
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate 				/* disable the queues until further notice */
33510Sstevel@tonic-gate 				if (*stopdir & CRYPT_ENCRYPT) {
33520Sstevel@tonic-gate 					noenable(wq);
33530Sstevel@tonic-gate 					tmi->ready &= ~CRYPT_WRITE_READY;
33540Sstevel@tonic-gate 				}
33550Sstevel@tonic-gate 				if (*stopdir & CRYPT_DECRYPT) {
33560Sstevel@tonic-gate 					noenable(RD(wq));
33570Sstevel@tonic-gate 					tmi->ready &= ~CRYPT_READ_READY;
33580Sstevel@tonic-gate 				}
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate 				miocack(wq, mp, 0, 0);
33610Sstevel@tonic-gate 			}
33620Sstevel@tonic-gate 			break;
33630Sstevel@tonic-gate 		case CRYPTIOCSTARTDEC:
33640Sstevel@tonic-gate 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
33650Sstevel@tonic-gate 					SL_TRACE|SL_NOTE,
33660Sstevel@tonic-gate 					"wput: got CRYPTIOCSTARTDEC "
33670Sstevel@tonic-gate 					"ioctl(%d)", iocp->ioc_cmd));
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 			start_stream(wq, mp, CRYPT_DECRYPT);
33700Sstevel@tonic-gate 			break;
33710Sstevel@tonic-gate 		case CRYPTIOCSTARTENC:
33720Sstevel@tonic-gate 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
33730Sstevel@tonic-gate 					SL_TRACE|SL_NOTE,
33740Sstevel@tonic-gate 					"wput: got CRYPTIOCSTARTENC "
33750Sstevel@tonic-gate 					"ioctl(%d)", iocp->ioc_cmd));
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate 			start_stream(wq, mp, CRYPT_ENCRYPT);
33780Sstevel@tonic-gate 			break;
33790Sstevel@tonic-gate 		default:
33800Sstevel@tonic-gate 			putnext(wq, mp);
33810Sstevel@tonic-gate 			break;
33820Sstevel@tonic-gate 		}
33830Sstevel@tonic-gate 		break;
33840Sstevel@tonic-gate 	default:
33850Sstevel@tonic-gate 		if (queclass(mp) < QPCTL) {
33860Sstevel@tonic-gate 			if (wq->q_first != NULL || !canputnext(wq)) {
33870Sstevel@tonic-gate 				if (!putq(wq, mp))
33880Sstevel@tonic-gate 					freemsg(mp);
33890Sstevel@tonic-gate 				return;
33900Sstevel@tonic-gate 			}
33910Sstevel@tonic-gate 		}
33920Sstevel@tonic-gate 		putnext(wq, mp);
33930Sstevel@tonic-gate 		break;
33940Sstevel@tonic-gate 	}
33950Sstevel@tonic-gate }
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate /*
33980Sstevel@tonic-gate  * decrypt_rcmd_mblks
33990Sstevel@tonic-gate  *
34000Sstevel@tonic-gate  * Because kerberized r* commands(rsh, rlogin, etc)
34010Sstevel@tonic-gate  * use a 4 byte length field to indicate the # of
34020Sstevel@tonic-gate  * PLAINTEXT bytes that are encrypted in the field
34030Sstevel@tonic-gate  * that follows, we must parse out each message and
34040Sstevel@tonic-gate  * break out the length fields prior to sending them
34050Sstevel@tonic-gate  * upstream to our Solaris r* clients/servers which do
34060Sstevel@tonic-gate  * NOT understand this format.
34070Sstevel@tonic-gate  *
34080Sstevel@tonic-gate  * Kerberized/encrypted message format:
34090Sstevel@tonic-gate  * -------------------------------
34100Sstevel@tonic-gate  * | XXXX | N bytes of ciphertext|
34110Sstevel@tonic-gate  * -------------------------------
34120Sstevel@tonic-gate  *
34130Sstevel@tonic-gate  * Where: XXXX = number of plaintext bytes that were encrypted in
34140Sstevel@tonic-gate  *               to make the ciphertext field.  This is done
34150Sstevel@tonic-gate  *               because we are using a cipher that pads out to
34160Sstevel@tonic-gate  *               an 8 byte boundary.  We only want the application
34170Sstevel@tonic-gate  *               layer to see the correct number of plain text bytes,
34180Sstevel@tonic-gate  *               not plaintext + pad.  So, after we decrypt, we
34190Sstevel@tonic-gate  *               must trim the output block down to the intended
34200Sstevel@tonic-gate  *               plaintext length and eliminate the pad bytes.
34210Sstevel@tonic-gate  *
34220Sstevel@tonic-gate  * This routine takes the entire input message, breaks it into
34230Sstevel@tonic-gate  * a new message that does not contain these length fields and
34240Sstevel@tonic-gate  * returns a message consisting of mblks filled with just ciphertext.
34250Sstevel@tonic-gate  *
34260Sstevel@tonic-gate  */
34270Sstevel@tonic-gate static mblk_t *
34280Sstevel@tonic-gate decrypt_rcmd_mblks(queue_t *q, mblk_t *mp)
34290Sstevel@tonic-gate {
34300Sstevel@tonic-gate 	mblk_t *newmp = NULL;
34310Sstevel@tonic-gate 	size_t msglen;
34320Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	msglen = msgsize(mp);
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 	/*
34370Sstevel@tonic-gate 	 * If we need the length field, get it here.
34380Sstevel@tonic-gate 	 * Test the "plaintext length" indicator.
34390Sstevel@tonic-gate 	 */
34400Sstevel@tonic-gate 	if (tmi->rcmd_state.pt_len == 0) {
34410Sstevel@tonic-gate 		uint32_t elen;
34420Sstevel@tonic-gate 		int tocopy;
34430Sstevel@tonic-gate 		mblk_t *nextp;
34440Sstevel@tonic-gate 
34450Sstevel@tonic-gate 		/*
34460Sstevel@tonic-gate 		 * Make sure we have recieved all 4 bytes of the
34470Sstevel@tonic-gate 		 * length field.
34480Sstevel@tonic-gate 		 */
34490Sstevel@tonic-gate 		while (mp != NULL) {
34500Sstevel@tonic-gate 			ASSERT(tmi->rcmd_state.cd_len < sizeof (uint32_t));
34510Sstevel@tonic-gate 
34520Sstevel@tonic-gate 			tocopy = sizeof (uint32_t) -
34530Sstevel@tonic-gate 				tmi->rcmd_state.cd_len;
34540Sstevel@tonic-gate 			if (tocopy > msglen)
34550Sstevel@tonic-gate 				tocopy = msglen;
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 			ASSERT(mp->b_rptr + tocopy <= DB_LIM(mp));
34580Sstevel@tonic-gate 			bcopy(mp->b_rptr,
34590Sstevel@tonic-gate 				(char *)(&tmi->rcmd_state.next_len +
34600Sstevel@tonic-gate 					tmi->rcmd_state.cd_len), tocopy);
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 			tmi->rcmd_state.cd_len += tocopy;
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 			if (tmi->rcmd_state.cd_len >= sizeof (uint32_t)) {
34650Sstevel@tonic-gate 				tmi->rcmd_state.next_len =
34660Sstevel@tonic-gate 					ntohl(tmi->rcmd_state.next_len);
34670Sstevel@tonic-gate 				break;
34680Sstevel@tonic-gate 			}
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate 			nextp = mp->b_cont;
34710Sstevel@tonic-gate 			mp->b_cont = NULL;
34720Sstevel@tonic-gate 			freeb(mp);
34730Sstevel@tonic-gate 			mp = nextp;
34740Sstevel@tonic-gate 		}
34750Sstevel@tonic-gate 
34760Sstevel@tonic-gate 		if (mp == NULL) {
34770Sstevel@tonic-gate 			return (NULL);
34780Sstevel@tonic-gate 		}
34790Sstevel@tonic-gate 		/*
34800Sstevel@tonic-gate 		 * recalculate the msglen now that we've read the
34810Sstevel@tonic-gate 		 * length and adjusted the bufptr (b_rptr).
34820Sstevel@tonic-gate 		 */
34830Sstevel@tonic-gate 		msglen -= tocopy;
34840Sstevel@tonic-gate 		mp->b_rptr += tocopy;
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 		tmi->rcmd_state.pt_len = tmi->rcmd_state.next_len;
34870Sstevel@tonic-gate 
34880Sstevel@tonic-gate 		if (tmi->rcmd_state.pt_len <= 0) {
34890Sstevel@tonic-gate 			/*
34900Sstevel@tonic-gate 			 * Return an IO error to break the connection. there
34910Sstevel@tonic-gate 			 * is no way to recover from this.  Usually it means
34920Sstevel@tonic-gate 			 * the app has incorrectly requested decryption on
34930Sstevel@tonic-gate 			 * a non-encrypted stream, thus the "pt_len" field
34940Sstevel@tonic-gate 			 * is negative.
34950Sstevel@tonic-gate 			 */
34960Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
34970Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
34980Sstevel@tonic-gate 			*mp->b_rptr = EIO;
34990Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate 			freemsg(mp->b_cont);
35020Sstevel@tonic-gate 			mp->b_cont = NULL;
35030Sstevel@tonic-gate 			qreply(WR(q), mp);
35040Sstevel@tonic-gate 			tmi->rcmd_state.cd_len = tmi->rcmd_state.pt_len = 0;
35050Sstevel@tonic-gate 			return (NULL);
35060Sstevel@tonic-gate 		}
35070Sstevel@tonic-gate 
35080Sstevel@tonic-gate 		/*
35090Sstevel@tonic-gate 		 * If this is V2 mode, then the encrypted data is actually
35100Sstevel@tonic-gate 		 * 4 bytes bigger than the indicated len because the plaintext
35110Sstevel@tonic-gate 		 * length is encrypted for an additional security check, but
35120Sstevel@tonic-gate 		 * its not counted as part of the overall length we just read.
35130Sstevel@tonic-gate 		 * Strange and confusing, but true.
35140Sstevel@tonic-gate 		 */
35150Sstevel@tonic-gate 
35160Sstevel@tonic-gate 		if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
35170Sstevel@tonic-gate 			elen = tmi->rcmd_state.pt_len + 4;
35180Sstevel@tonic-gate 		else
35190Sstevel@tonic-gate 			elen = tmi->rcmd_state.pt_len;
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 		tmi->rcmd_state.cd_len  = encrypt_size(&tmi->dec_data, elen);
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate 		/*
35240Sstevel@tonic-gate 		 * Allocate an mblk to hold the cipher text until it is
35250Sstevel@tonic-gate 		 * all ready to be processed.
35260Sstevel@tonic-gate 		 */
35270Sstevel@tonic-gate 		tmi->rcmd_state.c_msg = allocb(tmi->rcmd_state.cd_len,
35280Sstevel@tonic-gate 						BPRI_HI);
35290Sstevel@tonic-gate 		if (tmi->rcmd_state.c_msg == NULL) {
35300Sstevel@tonic-gate #ifdef DEBUG
35310Sstevel@tonic-gate 			cmn_err(CE_WARN, "decrypt_rcmd_msgb: allocb failed "
35320Sstevel@tonic-gate 				"for %d bytes",
35330Sstevel@tonic-gate 				(int)tmi->rcmd_state.cd_len);
35340Sstevel@tonic-gate #endif
35350Sstevel@tonic-gate 			/*
35360Sstevel@tonic-gate 			 * Return an IO error to break the connection.
35370Sstevel@tonic-gate 			 */
35380Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
35390Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
35400Sstevel@tonic-gate 			*mp->b_rptr = EIO;
35410Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
35420Sstevel@tonic-gate 			freemsg(mp->b_cont);
35430Sstevel@tonic-gate 			mp->b_cont = NULL;
35440Sstevel@tonic-gate 			tmi->rcmd_state.cd_len = tmi->rcmd_state.pt_len = 0;
35450Sstevel@tonic-gate 			qreply(WR(q), mp);
35460Sstevel@tonic-gate 			return (NULL);
35470Sstevel@tonic-gate 		}
35480Sstevel@tonic-gate 	}
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate 	/*
35510Sstevel@tonic-gate 	 * If this entire message was just the length field,
35520Sstevel@tonic-gate 	 * free and return.  The actual data will probably be next.
35530Sstevel@tonic-gate 	 */
35540Sstevel@tonic-gate 	if (msglen == 0) {
35550Sstevel@tonic-gate 		freemsg(mp);
35560Sstevel@tonic-gate 		return (NULL);
35570Sstevel@tonic-gate 	}
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 	/*
35600Sstevel@tonic-gate 	 * Copy as much of the cipher text as possible into
35610Sstevel@tonic-gate 	 * the new msgb (c_msg).
35620Sstevel@tonic-gate 	 *
35630Sstevel@tonic-gate 	 * Logic:  if we got some bytes (msglen) and we still
35640Sstevel@tonic-gate 	 * 	"need" some bytes (len-rcvd), get them here.
35650Sstevel@tonic-gate 	 */
35660Sstevel@tonic-gate 	ASSERT(tmi->rcmd_state.c_msg != NULL);
35670Sstevel@tonic-gate 	if (msglen > 0 &&
35680Sstevel@tonic-gate 	    (tmi->rcmd_state.cd_len > MBLKL(tmi->rcmd_state.c_msg))) {
35690Sstevel@tonic-gate 		mblk_t *bp, *nextp;
35700Sstevel@tonic-gate 		size_t n;
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 		/*
35730Sstevel@tonic-gate 		 * Walk the mblks and copy just as many bytes as we need
35740Sstevel@tonic-gate 		 * for this particular block of cipher text.
35750Sstevel@tonic-gate 		 */
35760Sstevel@tonic-gate 		bp = mp;
35770Sstevel@tonic-gate 		while (bp != NULL) {
35780Sstevel@tonic-gate 			size_t needed;
35790Sstevel@tonic-gate 			size_t tocopy;
35800Sstevel@tonic-gate 			n = MBLKL(bp);
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 			needed = tmi->rcmd_state.cd_len -
35830Sstevel@tonic-gate 				MBLKL(tmi->rcmd_state.c_msg);
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 			tocopy = (needed >= n ? n : needed);
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 			ASSERT(bp->b_rptr + tocopy <= DB_LIM(bp));
35880Sstevel@tonic-gate 			ASSERT(tmi->rcmd_state.c_msg->b_wptr + tocopy <=
35890Sstevel@tonic-gate 				DB_LIM(tmi->rcmd_state.c_msg));
35900Sstevel@tonic-gate 
35910Sstevel@tonic-gate 			/* Copy to end of new mblk */
35920Sstevel@tonic-gate 			bcopy(bp->b_rptr, tmi->rcmd_state.c_msg->b_wptr,
35930Sstevel@tonic-gate 				tocopy);
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 			tmi->rcmd_state.c_msg->b_wptr += tocopy;
35960Sstevel@tonic-gate 
35970Sstevel@tonic-gate 			bp->b_rptr += tocopy;
35980Sstevel@tonic-gate 
35990Sstevel@tonic-gate 			nextp = bp->b_cont;
36000Sstevel@tonic-gate 
36010Sstevel@tonic-gate 			/*
36020Sstevel@tonic-gate 			 * If we used this whole block, free it and
36030Sstevel@tonic-gate 			 * move on.
36040Sstevel@tonic-gate 			 */
36050Sstevel@tonic-gate 			if (!MBLKL(bp)) {
36060Sstevel@tonic-gate 				freeb(bp);
36070Sstevel@tonic-gate 				bp = NULL;
36080Sstevel@tonic-gate 			}
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 			/* If we got what we needed, stop the loop */
36110Sstevel@tonic-gate 			if (MBLKL(tmi->rcmd_state.c_msg) ==
36120Sstevel@tonic-gate 			    tmi->rcmd_state.cd_len) {
36130Sstevel@tonic-gate 				/*
36140Sstevel@tonic-gate 				 * If there is more data in the message,
36150Sstevel@tonic-gate 				 * its for another block of cipher text,
36160Sstevel@tonic-gate 				 * put it back in the queue for next time.
36170Sstevel@tonic-gate 				 */
36180Sstevel@tonic-gate 				if (bp) {
36190Sstevel@tonic-gate 					if (!putbq(q, bp))
36200Sstevel@tonic-gate 						freemsg(bp);
36210Sstevel@tonic-gate 				} else if (nextp != NULL) {
36220Sstevel@tonic-gate 					/*
36230Sstevel@tonic-gate 					 * If there is more, put it back in the
36240Sstevel@tonic-gate 					 * queue for another pass thru.
36250Sstevel@tonic-gate 					 */
36260Sstevel@tonic-gate 					if (!putbq(q, nextp))
36270Sstevel@tonic-gate 						freemsg(nextp);
36280Sstevel@tonic-gate 				}
36290Sstevel@tonic-gate 				break;
36300Sstevel@tonic-gate 			}
36310Sstevel@tonic-gate 			bp = nextp;
36320Sstevel@tonic-gate 		}
36330Sstevel@tonic-gate 	}
36340Sstevel@tonic-gate 	/*
36350Sstevel@tonic-gate 	 * Finally, if we received all the cipher text data for
36360Sstevel@tonic-gate 	 * this message, decrypt it into a new msg and send it up
36370Sstevel@tonic-gate 	 * to the app.
36380Sstevel@tonic-gate 	 */
36390Sstevel@tonic-gate 	if (tmi->rcmd_state.pt_len > 0 &&
36400Sstevel@tonic-gate 	    MBLKL(tmi->rcmd_state.c_msg) == tmi->rcmd_state.cd_len) {
36410Sstevel@tonic-gate 		mblk_t *bp;
36420Sstevel@tonic-gate 		mblk_t *newbp;
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 		/*
36450Sstevel@tonic-gate 		 * Now we can use our msg that we created when the
36460Sstevel@tonic-gate 		 * initial message boundary was detected.
36470Sstevel@tonic-gate 		 */
36480Sstevel@tonic-gate 		bp = tmi->rcmd_state.c_msg;
36490Sstevel@tonic-gate 		tmi->rcmd_state.c_msg = NULL;
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate 		newbp = do_decrypt(q, bp);
36520Sstevel@tonic-gate 		if (newbp != NULL) {
36530Sstevel@tonic-gate 			bp = newbp;
36540Sstevel@tonic-gate 			/*
36550Sstevel@tonic-gate 			 * If using RCMD_MODE_V2 ("new" mode),
36560Sstevel@tonic-gate 			 * look at the 4 byte plaintext length that
36570Sstevel@tonic-gate 			 * was just decrypted and compare with the
36580Sstevel@tonic-gate 			 * original pt_len value that was received.
36590Sstevel@tonic-gate 			 */
36600Sstevel@tonic-gate 			if (tmi->dec_data.option_mask &
36610Sstevel@tonic-gate 			    CRYPTOPT_RCMD_MODE_V2) {
36620Sstevel@tonic-gate 				uint32_t pt_len2;
36630Sstevel@tonic-gate 
36640Sstevel@tonic-gate 				pt_len2 = *(uint32_t *)bp->b_rptr;
36650Sstevel@tonic-gate 				pt_len2 = ntohl(pt_len2);
36660Sstevel@tonic-gate 				/*
36670Sstevel@tonic-gate 				 * Make sure the 2 pt len fields agree.
36680Sstevel@tonic-gate 				 */
36690Sstevel@tonic-gate 				if (pt_len2 != tmi->rcmd_state.pt_len) {
36700Sstevel@tonic-gate 					cmn_err(CE_WARN,
36710Sstevel@tonic-gate 						"Inconsistent length fields"
36720Sstevel@tonic-gate 						" received %d != %d",
36730Sstevel@tonic-gate 						(int)tmi->rcmd_state.pt_len,
36740Sstevel@tonic-gate 						(int)pt_len2);
36750Sstevel@tonic-gate 					bp->b_datap->db_type = M_ERROR;
36760Sstevel@tonic-gate 					bp->b_rptr = bp->b_datap->db_base;
36770Sstevel@tonic-gate 					*bp->b_rptr = EIO;
36780Sstevel@tonic-gate 					bp->b_wptr = bp->b_rptr + sizeof (char);
36790Sstevel@tonic-gate 					freemsg(bp->b_cont);
36800Sstevel@tonic-gate 					bp->b_cont = NULL;
36810Sstevel@tonic-gate 					tmi->rcmd_state.cd_len = 0;
36820Sstevel@tonic-gate 					qreply(WR(q), bp);
36830Sstevel@tonic-gate 					return (NULL);
36840Sstevel@tonic-gate 				}
36850Sstevel@tonic-gate 				bp->b_rptr += sizeof (uint32_t);
36860Sstevel@tonic-gate 			}
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 			/*
36890Sstevel@tonic-gate 			 * Trim the decrypted block the length originally
36900Sstevel@tonic-gate 			 * indicated by the sender.  This is to remove any
36910Sstevel@tonic-gate 			 * padding bytes that the sender added to satisfy
36920Sstevel@tonic-gate 			 * requirements of the crypto algorithm.
36930Sstevel@tonic-gate 			 */
36940Sstevel@tonic-gate 			bp->b_wptr = bp->b_rptr + tmi->rcmd_state.pt_len;
36950Sstevel@tonic-gate 
36960Sstevel@tonic-gate 			newmp = bp;
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 			/*
36990Sstevel@tonic-gate 			 * Reset our state to indicate we are ready
37000Sstevel@tonic-gate 			 * for a new message.
37010Sstevel@tonic-gate 			 */
37020Sstevel@tonic-gate 			tmi->rcmd_state.pt_len = 0;
37030Sstevel@tonic-gate 			tmi->rcmd_state.cd_len = 0;
37040Sstevel@tonic-gate 		} else {
37050Sstevel@tonic-gate #ifdef DEBUG
37060Sstevel@tonic-gate 			cmn_err(CE_WARN,
37070Sstevel@tonic-gate 				"decrypt_rcmd: do_decrypt on %d bytes failed",
37080Sstevel@tonic-gate 				(int)tmi->rcmd_state.cd_len);
37090Sstevel@tonic-gate #endif
37100Sstevel@tonic-gate 			/*
37110Sstevel@tonic-gate 			 * do_decrypt already handled failures, just
37120Sstevel@tonic-gate 			 * return NULL.
37130Sstevel@tonic-gate 			 */
37140Sstevel@tonic-gate 			tmi->rcmd_state.pt_len = 0;
37150Sstevel@tonic-gate 			tmi->rcmd_state.cd_len = 0;
37160Sstevel@tonic-gate 			return (NULL);
37170Sstevel@tonic-gate 		}
37180Sstevel@tonic-gate 	}
37190Sstevel@tonic-gate 
37200Sstevel@tonic-gate 	/*
37210Sstevel@tonic-gate 	 * return the new message with the 'length' fields removed
37220Sstevel@tonic-gate 	 */
37230Sstevel@tonic-gate 	return (newmp);
37240Sstevel@tonic-gate }
37250Sstevel@tonic-gate 
37260Sstevel@tonic-gate /*
37270Sstevel@tonic-gate  * cryptmodrsrv
37280Sstevel@tonic-gate  *
37290Sstevel@tonic-gate  * Read queue service routine
37300Sstevel@tonic-gate  * Necessary because if the ready flag is not set
37310Sstevel@tonic-gate  * (via CRYPTIOCSTOP/CRYPTIOCSTART ioctls) then the data
37320Sstevel@tonic-gate  * must remain on queue and not be passed along.
37330Sstevel@tonic-gate  */
37340Sstevel@tonic-gate static int
37350Sstevel@tonic-gate cryptmodrsrv(queue_t *q)
37360Sstevel@tonic-gate {
37370Sstevel@tonic-gate 	mblk_t *mp, *bp;
37380Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
37390Sstevel@tonic-gate 
37400Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
37410Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
37420Sstevel@tonic-gate 		case M_DATA:
37430Sstevel@tonic-gate 			if (canputnext(q) && tmi->ready & CRYPT_READ_READY) {
37440Sstevel@tonic-gate 				/*
37450Sstevel@tonic-gate 				 * Process "rcmd" messages differently because
37460Sstevel@tonic-gate 				 * they contain a 4 byte plaintext length
37470Sstevel@tonic-gate 				 * id that needs to be removed.
37480Sstevel@tonic-gate 				 */
37490Sstevel@tonic-gate 				if (tmi->dec_data.method != CRYPT_METHOD_NONE &&
37500Sstevel@tonic-gate 				    (tmi->dec_data.option_mask &
37510Sstevel@tonic-gate 				    (CRYPTOPT_RCMD_MODE_V1 |
37520Sstevel@tonic-gate 				    CRYPTOPT_RCMD_MODE_V2))) {
37530Sstevel@tonic-gate 					mp = decrypt_rcmd_mblks(q, mp);
37540Sstevel@tonic-gate 					if (mp)
37550Sstevel@tonic-gate 						putnext(q, mp);
37560Sstevel@tonic-gate 					continue;
37570Sstevel@tonic-gate 				}
37580Sstevel@tonic-gate 				if ((bp = msgpullup(mp, -1)) != NULL) {
37590Sstevel@tonic-gate 					freemsg(mp);
37600Sstevel@tonic-gate 					if (MBLKL(bp) > 0) {
37610Sstevel@tonic-gate 						mp = do_decrypt(q, bp);
37620Sstevel@tonic-gate 						if (mp != NULL)
37630Sstevel@tonic-gate 							putnext(q, mp);
37640Sstevel@tonic-gate 					}
37650Sstevel@tonic-gate 				}
37660Sstevel@tonic-gate 			} else {
37670Sstevel@tonic-gate 				if (!putbq(q, mp)) {
37680Sstevel@tonic-gate 					freemsg(mp);
37690Sstevel@tonic-gate 				}
37700Sstevel@tonic-gate 				return (0);
37710Sstevel@tonic-gate 			}
37720Sstevel@tonic-gate 			break;
37730Sstevel@tonic-gate 		default:
37740Sstevel@tonic-gate 			/*
37750Sstevel@tonic-gate 			 * rput does not queue anything > QPCTL, so we don't
37760Sstevel@tonic-gate 			 * need to check for it here.
37770Sstevel@tonic-gate 			 */
37780Sstevel@tonic-gate 			if (!canputnext(q)) {
37790Sstevel@tonic-gate 				if (!putbq(q, mp))
37800Sstevel@tonic-gate 					freemsg(mp);
37810Sstevel@tonic-gate 				return (0);
37820Sstevel@tonic-gate 			}
37830Sstevel@tonic-gate 			putnext(q, mp);
37840Sstevel@tonic-gate 			break;
37850Sstevel@tonic-gate 		}
37860Sstevel@tonic-gate 	}
37870Sstevel@tonic-gate 	return (0);
37880Sstevel@tonic-gate }
37890Sstevel@tonic-gate 
37900Sstevel@tonic-gate 
37910Sstevel@tonic-gate /*
37920Sstevel@tonic-gate  * Read-side put procedure.
37930Sstevel@tonic-gate  */
37940Sstevel@tonic-gate static void
37950Sstevel@tonic-gate cryptmodrput(queue_t *rq, mblk_t *mp)
37960Sstevel@tonic-gate {
37970Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
37980Sstevel@tonic-gate 	case M_DATA:
37990Sstevel@tonic-gate 		if (!putq(rq, mp)) {
38000Sstevel@tonic-gate 			freemsg(mp);
38010Sstevel@tonic-gate 		}
38020Sstevel@tonic-gate 		break;
38030Sstevel@tonic-gate 	case M_FLUSH:
38040Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
38050Sstevel@tonic-gate 			flushq(rq, FLUSHALL);
38060Sstevel@tonic-gate 		}
38070Sstevel@tonic-gate 		putnext(rq, mp);
38080Sstevel@tonic-gate 		break;
38090Sstevel@tonic-gate 	default:
38100Sstevel@tonic-gate 		if (queclass(mp) < QPCTL) {
38110Sstevel@tonic-gate 			if (rq->q_first != NULL || !canputnext(rq)) {
38120Sstevel@tonic-gate 				if (!putq(rq, mp))
38130Sstevel@tonic-gate 					freemsg(mp);
38140Sstevel@tonic-gate 				return;
38150Sstevel@tonic-gate 			}
38160Sstevel@tonic-gate 		}
38170Sstevel@tonic-gate 		putnext(rq, mp);
38180Sstevel@tonic-gate 		break;
38190Sstevel@tonic-gate 	}
38200Sstevel@tonic-gate }
3821