xref: /onnv-gate/usr/src/uts/common/io/cryptmod.c (revision 7227:2b2431af037c)
10Sstevel@tonic-gate /*
2*7227Sps57422  * Copyright 2008 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,
179*7227Sps57422 	"STREAMS encryption module",
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 
1999*7227Sps57422 	bzero(&indata, sizeof (indata));
2000*7227Sps57422 
20010Sstevel@tonic-gate 	/* The usage constant is 1026 for all "old" rcmd mode operations */
20020Sstevel@tonic-gate 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V1)
20030Sstevel@tonic-gate 		usage = RCMDV1_USAGE;
20040Sstevel@tonic-gate 	else
20050Sstevel@tonic-gate 		usage = ARCFOUR_ENCRYPT_USAGE;
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	mech.cm_type = tmi->enc_data.mech_type;
20080Sstevel@tonic-gate 	mech.cm_param = NULL;
20090Sstevel@tonic-gate 	mech.cm_param_len = 0;
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	/*
20120Sstevel@tonic-gate 	 * The size at this point should be the size of
20130Sstevel@tonic-gate 	 * all the plaintext plus the optional plaintext length
20140Sstevel@tonic-gate 	 * needed for RCMD V2 mode.  There should also be room
20150Sstevel@tonic-gate 	 * at the head of the mblk for the confounder and hash info.
20160Sstevel@tonic-gate 	 */
20170Sstevel@tonic-gate 	inlen = (size_t)MBLKL(mp);
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	cipherlen = encrypt_size(&tmi->enc_data, inlen);
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mp) >= cipherlen);
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 	/*
20240Sstevel@tonic-gate 	 * Shift the rptr back enough to insert
20250Sstevel@tonic-gate 	 * the confounder and hash.
20260Sstevel@tonic-gate 	 */
20270Sstevel@tonic-gate 	mp->b_rptr -= (hash->confound_len + hash->hash_len);
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	/* zero out the hash area */
20300Sstevel@tonic-gate 	bzero(mp->b_rptr, (size_t)hash->hash_len);
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 	if (cipherlen > inlen) {
20330Sstevel@tonic-gate 		bzero(mp->b_wptr, MBLKTAIL(mp));
20340Sstevel@tonic-gate 	}
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
20370Sstevel@tonic-gate 		bcopy(ARCFOUR_EXP_SALT, saltdata, strlen(ARCFOUR_EXP_SALT));
20380Sstevel@tonic-gate 		saltdata[9] = 0;
20390Sstevel@tonic-gate 		saltdata[10] = usage & 0xff;
20400Sstevel@tonic-gate 		saltdata[11] = (usage >> 8) & 0xff;
20410Sstevel@tonic-gate 		saltdata[12] = (usage >> 16) & 0xff;
20420Sstevel@tonic-gate 		saltdata[13] = (usage >> 24) & 0xff;
20430Sstevel@tonic-gate 		saltlen = 14;
20440Sstevel@tonic-gate 	} else {
20450Sstevel@tonic-gate 		saltdata[0] = usage & 0xff;
20460Sstevel@tonic-gate 		saltdata[1] = (usage >> 8) & 0xff;
20470Sstevel@tonic-gate 		saltdata[2] = (usage >> 16) & 0xff;
20480Sstevel@tonic-gate 		saltdata[3] = (usage >> 24) & 0xff;
20490Sstevel@tonic-gate 		saltlen = 4;
20500Sstevel@tonic-gate 	}
20510Sstevel@tonic-gate 	/*
20520Sstevel@tonic-gate 	 * Use the salt value to create a key to be used
20530Sstevel@tonic-gate 	 * for subsequent HMAC operations.
20540Sstevel@tonic-gate 	 */
20550Sstevel@tonic-gate 	result = do_hmac(md5_hmac_mech,
20560Sstevel@tonic-gate 			tmi->enc_data.ckey,
20570Sstevel@tonic-gate 			(char *)saltdata, saltlen,
20580Sstevel@tonic-gate 			(char *)k1data, sizeof (k1data));
20590Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
20600Sstevel@tonic-gate 		cmn_err(CE_WARN,
20610Sstevel@tonic-gate 			"arcfour_hmac_md5_encrypt:  do_hmac(k1)"
20620Sstevel@tonic-gate 			"failed - error %0x", result);
20630Sstevel@tonic-gate 		goto cleanup;
20640Sstevel@tonic-gate 	}
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	bcopy(k1data, k2data, sizeof (k2data));
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	/*
20690Sstevel@tonic-gate 	 * For the neutered MS RC4 encryption type,
20700Sstevel@tonic-gate 	 * set the trailing 9 bytes to 0xab per the
20710Sstevel@tonic-gate 	 * RC4-HMAC spec.
20720Sstevel@tonic-gate 	 */
20730Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
20740Sstevel@tonic-gate 		bcopy((void *)&k1data[7], ms_exp, sizeof (ms_exp));
20750Sstevel@tonic-gate 	}
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	/*
20780Sstevel@tonic-gate 	 * Get the confounder bytes.
20790Sstevel@tonic-gate 	 */
20800Sstevel@tonic-gate 	(void) random_get_pseudo_bytes(
20810Sstevel@tonic-gate 			(uint8_t *)(mp->b_rptr + hash->hash_len),
20820Sstevel@tonic-gate 			(size_t)hash->confound_len);
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	k2.ck_data = k2data;
20850Sstevel@tonic-gate 	k2.ck_format = CRYPTO_KEY_RAW;
20860Sstevel@tonic-gate 	k2.ck_length = sizeof (k2data) * 8;
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	/*
20890Sstevel@tonic-gate 	 * This writes the HMAC to the hash area in the
20900Sstevel@tonic-gate 	 * mblk.  The key used is the one just created by
20910Sstevel@tonic-gate 	 * the previous HMAC operation.
20920Sstevel@tonic-gate 	 * The data being processed is the confounder bytes
20930Sstevel@tonic-gate 	 * PLUS the input plaintext.
20940Sstevel@tonic-gate 	 */
20950Sstevel@tonic-gate 	result = do_hmac(md5_hmac_mech, &k2,
20960Sstevel@tonic-gate 			(char *)mp->b_rptr + hash->hash_len,
20970Sstevel@tonic-gate 			hash->confound_len + inlen,
20980Sstevel@tonic-gate 			(char *)mp->b_rptr, hash->hash_len);
20990Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
21000Sstevel@tonic-gate 		cmn_err(CE_WARN,
21010Sstevel@tonic-gate 			"arcfour_hmac_md5_encrypt:  do_hmac(k2)"
21020Sstevel@tonic-gate 			"failed - error %0x", result);
21030Sstevel@tonic-gate 		goto cleanup;
21040Sstevel@tonic-gate 	}
21050Sstevel@tonic-gate 	/*
21060Sstevel@tonic-gate 	 * Because of the odd way that MIT uses RC4 keys
21070Sstevel@tonic-gate 	 * on the rlogin stream, we only need to create
21080Sstevel@tonic-gate 	 * this key once.
21090Sstevel@tonic-gate 	 * However, if using "old" rcmd mode, we need to do
21100Sstevel@tonic-gate 	 * it every time.
21110Sstevel@tonic-gate 	 */
21120Sstevel@tonic-gate 	if (tmi->enc_data.ctx == NULL ||
21130Sstevel@tonic-gate 	    (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V1)) {
21140Sstevel@tonic-gate 		crypto_key_t *key = &tmi->enc_data.d_encr_key;
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 		k1.ck_data = k1data;
21170Sstevel@tonic-gate 		k1.ck_format = CRYPTO_KEY_RAW;
21180Sstevel@tonic-gate 		k1.ck_length = sizeof (k1data) * 8;
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 		key->ck_format = CRYPTO_KEY_RAW;
21210Sstevel@tonic-gate 		key->ck_length = k1.ck_length;
21220Sstevel@tonic-gate 		if (key->ck_data == NULL)
21230Sstevel@tonic-gate 			key->ck_data = kmem_zalloc(
21240Sstevel@tonic-gate 				CRYPT_ARCFOUR_KEYBYTES, KM_SLEEP);
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 		/*
21270Sstevel@tonic-gate 		 * The final HMAC operation creates the encryption
21280Sstevel@tonic-gate 		 * key to be used for the encrypt operation.
21290Sstevel@tonic-gate 		 */
21300Sstevel@tonic-gate 		result = do_hmac(md5_hmac_mech, &k1,
21310Sstevel@tonic-gate 			(char *)mp->b_rptr, hash->hash_len,
21320Sstevel@tonic-gate 			(char *)key->ck_data, CRYPT_ARCFOUR_KEYBYTES);
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
21350Sstevel@tonic-gate 			cmn_err(CE_WARN,
21360Sstevel@tonic-gate 				"arcfour_hmac_md5_encrypt:  do_hmac(k3)"
21370Sstevel@tonic-gate 				"failed - error %0x", result);
21380Sstevel@tonic-gate 			goto cleanup;
21390Sstevel@tonic-gate 		}
21400Sstevel@tonic-gate 	}
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 	/*
21430Sstevel@tonic-gate 	 * If the context has not been initialized, do it now.
21440Sstevel@tonic-gate 	 */
21450Sstevel@tonic-gate 	if (tmi->enc_data.ctx == NULL &&
21460Sstevel@tonic-gate 	    (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)) {
21470Sstevel@tonic-gate 		/*
21480Sstevel@tonic-gate 		 * Only create a template if we are doing
21490Sstevel@tonic-gate 		 * chaining from block to block.
21500Sstevel@tonic-gate 		 */
21510Sstevel@tonic-gate 		result = crypto_create_ctx_template(&mech,
21520Sstevel@tonic-gate 				&tmi->enc_data.d_encr_key,
21530Sstevel@tonic-gate 				&tmi->enc_data.enc_tmpl,
21540Sstevel@tonic-gate 				KM_SLEEP);
21550Sstevel@tonic-gate 		if (result == CRYPTO_NOT_SUPPORTED) {
21560Sstevel@tonic-gate 			tmi->enc_data.enc_tmpl = NULL;
21570Sstevel@tonic-gate 		} else if (result != CRYPTO_SUCCESS) {
21580Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to create enc template "
21590Sstevel@tonic-gate 				"for RC4 encrypt: %0x", result);
21600Sstevel@tonic-gate 			goto cleanup;
21610Sstevel@tonic-gate 		}
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 		result = crypto_encrypt_init(&mech,
21640Sstevel@tonic-gate 					&tmi->enc_data.d_encr_key,
21650Sstevel@tonic-gate 					tmi->enc_data.enc_tmpl,
21660Sstevel@tonic-gate 					&tmi->enc_data.ctx, NULL);
21670Sstevel@tonic-gate 		if (result != CRYPTO_SUCCESS) {
21680Sstevel@tonic-gate 			cmn_err(CE_WARN, "crypto_encrypt_init failed:"
21690Sstevel@tonic-gate 				" %0x", result);
21700Sstevel@tonic-gate 			goto cleanup;
21710Sstevel@tonic-gate 		}
21720Sstevel@tonic-gate 	}
21730Sstevel@tonic-gate 	v1.iov_base = (char *)mp->b_rptr + hash->hash_len;
21740Sstevel@tonic-gate 	v1.iov_len = hash->confound_len + inlen;
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	indata.cd_format = CRYPTO_DATA_RAW;
21770Sstevel@tonic-gate 	indata.cd_offset = 0;
21780Sstevel@tonic-gate 	indata.cd_length = hash->confound_len + inlen;
21790Sstevel@tonic-gate 	indata.cd_raw = v1;
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
21820Sstevel@tonic-gate 		result = crypto_encrypt_update(tmi->enc_data.ctx,
21830Sstevel@tonic-gate 			&indata, NULL, NULL);
21840Sstevel@tonic-gate 	else
21850Sstevel@tonic-gate 		result = crypto_encrypt(&mech, &indata,
21860Sstevel@tonic-gate 			&tmi->enc_data.d_encr_key, NULL,
21870Sstevel@tonic-gate 			NULL, NULL);
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
21900Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_encrypt_update failed: 0x%0x",
21910Sstevel@tonic-gate 			result);
21920Sstevel@tonic-gate 	}
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate cleanup:
21950Sstevel@tonic-gate 	bzero(k1data, sizeof (k1data));
21960Sstevel@tonic-gate 	bzero(k2data, sizeof (k2data));
21970Sstevel@tonic-gate 	bzero(saltdata, sizeof (saltdata));
21980Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
21990Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
22000Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
22010Sstevel@tonic-gate 		*mp->b_rptr = EIO;
22020Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
22030Sstevel@tonic-gate 		freemsg(mp->b_cont);
22040Sstevel@tonic-gate 		mp->b_cont = NULL;
22050Sstevel@tonic-gate 		qreply(WR(q), mp);
22060Sstevel@tonic-gate 		return (NULL);
22070Sstevel@tonic-gate 	}
22080Sstevel@tonic-gate 	return (mp);
22090Sstevel@tonic-gate }
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate /*
22120Sstevel@tonic-gate  * DES-CBC-[HASH] encrypt
22130Sstevel@tonic-gate  *
22140Sstevel@tonic-gate  * Needed to support userland apps that must support Kerberos V5
22150Sstevel@tonic-gate  * encryption DES-CBC encryption modes.
22160Sstevel@tonic-gate  *
22170Sstevel@tonic-gate  * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
22180Sstevel@tonic-gate  *
22190Sstevel@tonic-gate  * format of ciphertext for DES-CBC functions, per RFC1510 is:
22200Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
22210Sstevel@tonic-gate  *  |confounder |  cksum   |   msg-data  | pad |
22220Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
22230Sstevel@tonic-gate  *
22240Sstevel@tonic-gate  * format of ciphertext when using DES3-SHA1-HMAC
22250Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
22260Sstevel@tonic-gate  *  |confounder |  msg-data  |   hmac    | pad |
22270Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
22280Sstevel@tonic-gate  *
22290Sstevel@tonic-gate  *  The confounder is 8 bytes of random data.
22300Sstevel@tonic-gate  *  The cksum depends on the hash being used.
22310Sstevel@tonic-gate  *   4 bytes for CRC32
22320Sstevel@tonic-gate  *  16 bytes for MD5
22330Sstevel@tonic-gate  *  20 bytes for SHA1
22340Sstevel@tonic-gate  *   0 bytes for RAW
22350Sstevel@tonic-gate  *
22360Sstevel@tonic-gate  */
22370Sstevel@tonic-gate static mblk_t *
22380Sstevel@tonic-gate des_cbc_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, hash_info_t *hash)
22390Sstevel@tonic-gate {
22400Sstevel@tonic-gate 	int result;
22410Sstevel@tonic-gate 	size_t cipherlen;
22420Sstevel@tonic-gate 	size_t inlen;
22430Sstevel@tonic-gate 	size_t plainlen;
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	/*
22460Sstevel@tonic-gate 	 * The size at this point should be the size of
22470Sstevel@tonic-gate 	 * all the plaintext plus the optional plaintext length
22480Sstevel@tonic-gate 	 * needed for RCMD V2 mode.  There should also be room
22490Sstevel@tonic-gate 	 * at the head of the mblk for the confounder and hash info.
22500Sstevel@tonic-gate 	 */
22510Sstevel@tonic-gate 	inlen = (size_t)MBLKL(mp);
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	/*
22540Sstevel@tonic-gate 	 * The output size will be a multiple of 8 because this algorithm
22550Sstevel@tonic-gate 	 * only works on 8 byte chunks.
22560Sstevel@tonic-gate 	 */
22570Sstevel@tonic-gate 	cipherlen = encrypt_size(&tmi->enc_data, inlen);
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mp) >= cipherlen);
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	if (cipherlen > inlen) {
22620Sstevel@tonic-gate 		bzero(mp->b_wptr, MBLKTAIL(mp));
22630Sstevel@tonic-gate 	}
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	/*
22660Sstevel@tonic-gate 	 * Shift the rptr back enough to insert
22670Sstevel@tonic-gate 	 * the confounder and hash.
22680Sstevel@tonic-gate 	 */
22690Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
22700Sstevel@tonic-gate 		mp->b_rptr -= hash->confound_len;
22710Sstevel@tonic-gate 	} else {
22720Sstevel@tonic-gate 		mp->b_rptr -= (hash->confound_len + hash->hash_len);
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 		/* zero out the hash area */
22750Sstevel@tonic-gate 		bzero(mp->b_rptr + hash->confound_len, (size_t)hash->hash_len);
22760Sstevel@tonic-gate 	}
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	/* get random confounder from our friend, the 'random' module */
22790Sstevel@tonic-gate 	if (hash->confound_len > 0) {
22800Sstevel@tonic-gate 		(void) random_get_pseudo_bytes((uint8_t *)mp->b_rptr,
22810Sstevel@tonic-gate 				    (size_t)hash->confound_len);
22820Sstevel@tonic-gate 	}
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	/*
22850Sstevel@tonic-gate 	 * For 3DES we calculate an HMAC later.
22860Sstevel@tonic-gate 	 */
22870Sstevel@tonic-gate 	if (tmi->enc_data.method != CRYPT_METHOD_DES3_CBC_SHA1) {
22880Sstevel@tonic-gate 		/* calculate chksum of confounder + input */
22890Sstevel@tonic-gate 		if (hash->hash_len > 0 && hash->hashfunc != NULL) {
22900Sstevel@tonic-gate 			uchar_t cksum[MAX_CKSUM_LEN];
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 			result = hash->hashfunc(cksum, mp->b_rptr,
22930Sstevel@tonic-gate 				cipherlen);
22940Sstevel@tonic-gate 			if (result != CRYPTO_SUCCESS) {
22950Sstevel@tonic-gate 				goto failure;
22960Sstevel@tonic-gate 			}
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 			/* put hash in place right after the confounder */
22990Sstevel@tonic-gate 			bcopy(cksum, (mp->b_rptr + hash->confound_len),
23000Sstevel@tonic-gate 			    (size_t)hash->hash_len);
23010Sstevel@tonic-gate 		}
23020Sstevel@tonic-gate 	}
23030Sstevel@tonic-gate 	/*
23040Sstevel@tonic-gate 	 * In order to support the "old" Kerberos RCMD protocol,
23050Sstevel@tonic-gate 	 * we must use the IVEC 3 different ways:
23060Sstevel@tonic-gate 	 *   IVEC_REUSE = keep using the same IV each time, this is
23070Sstevel@tonic-gate 	 *		ugly and insecure, but necessary for
23080Sstevel@tonic-gate 	 *		backwards compatibility with existing MIT code.
23090Sstevel@tonic-gate 	 *   IVEC_ONETIME = Use the ivec as initialized when the crypto
23100Sstevel@tonic-gate 	 *		was setup (see setup_crypto routine).
23110Sstevel@tonic-gate 	 *   IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
23120Sstevel@tonic-gate 	 */
23130Sstevel@tonic-gate 	if (tmi->enc_data.ivec_usage == IVEC_NEVER) {
23140Sstevel@tonic-gate 		bzero(tmi->enc_data.block, tmi->enc_data.blocklen);
23150Sstevel@tonic-gate 	} else if (tmi->enc_data.ivec_usage == IVEC_REUSE) {
23160Sstevel@tonic-gate 		bcopy(tmi->enc_data.ivec, tmi->enc_data.block,
23170Sstevel@tonic-gate 		    tmi->enc_data.blocklen);
23180Sstevel@tonic-gate 	}
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
23210Sstevel@tonic-gate 		/*
23220Sstevel@tonic-gate 		 * The input length already included the hash size,
23230Sstevel@tonic-gate 		 * don't include this in the plaintext length
23240Sstevel@tonic-gate 		 * calculations.
23250Sstevel@tonic-gate 		 */
23260Sstevel@tonic-gate 		plainlen = cipherlen - hash->hash_len;
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + plainlen;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 		result = kef_encr_hmac(&tmi->enc_data,
23310Sstevel@tonic-gate 			(void *)mp, (size_t)plainlen,
23320Sstevel@tonic-gate 			(char *)(mp->b_rptr + plainlen),
23330Sstevel@tonic-gate 			hash->hash_len);
23340Sstevel@tonic-gate 	} else {
23350Sstevel@tonic-gate 		ASSERT(mp->b_rptr + cipherlen <= DB_LIM(mp));
23360Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + cipherlen;
23370Sstevel@tonic-gate 		result = kef_crypt(&tmi->enc_data, (void *)mp,
23380Sstevel@tonic-gate 			CRYPTO_DATA_MBLK, (size_t)cipherlen,
23390Sstevel@tonic-gate 			CRYPT_ENCRYPT);
23400Sstevel@tonic-gate 	}
23410Sstevel@tonic-gate failure:
23420Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
23430Sstevel@tonic-gate #ifdef DEBUG
23440Sstevel@tonic-gate 		cmn_err(CE_WARN,
23450Sstevel@tonic-gate 			"des_cbc_encrypt: kef_crypt encrypt "
23460Sstevel@tonic-gate 			"failed (len: %ld) - error %0x",
23470Sstevel@tonic-gate 			cipherlen, result);
23480Sstevel@tonic-gate #endif
23490Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
23500Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
23510Sstevel@tonic-gate 		*mp->b_rptr = EIO;
23520Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
23530Sstevel@tonic-gate 		freemsg(mp->b_cont);
23540Sstevel@tonic-gate 		mp->b_cont = NULL;
23550Sstevel@tonic-gate 		qreply(WR(q), mp);
23560Sstevel@tonic-gate 		return (NULL);
23570Sstevel@tonic-gate 	} else if (tmi->enc_data.ivec_usage == IVEC_ONETIME) {
23580Sstevel@tonic-gate 		/*
23590Sstevel@tonic-gate 		 * Because we are using KEF, we must manually
23600Sstevel@tonic-gate 		 * update our IV.
23610Sstevel@tonic-gate 		 */
23620Sstevel@tonic-gate 		bcopy(mp->b_wptr - tmi->enc_data.ivlen,
23630Sstevel@tonic-gate 			tmi->enc_data.block, tmi->enc_data.ivlen);
23640Sstevel@tonic-gate 	}
23650Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
23660Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + cipherlen;
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	return (mp);
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate /*
23730Sstevel@tonic-gate  * des_cbc_decrypt
23740Sstevel@tonic-gate  *
23750Sstevel@tonic-gate  *
23760Sstevel@tonic-gate  * Needed to support userland apps that must support Kerberos V5
23770Sstevel@tonic-gate  * encryption DES-CBC decryption modes.
23780Sstevel@tonic-gate  *
23790Sstevel@tonic-gate  * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
23800Sstevel@tonic-gate  *
23810Sstevel@tonic-gate  * format of ciphertext for DES-CBC functions, per RFC1510 is:
23820Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
23830Sstevel@tonic-gate  *  |confounder |  cksum   |   msg-data  | pad |
23840Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
23850Sstevel@tonic-gate  *
23860Sstevel@tonic-gate  * format of ciphertext when using DES3-SHA1-HMAC
23870Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
23880Sstevel@tonic-gate  *  |confounder |  msg-data  |   hmac    | pad |
23890Sstevel@tonic-gate  *  +-----------+----------+-------------+-----+
23900Sstevel@tonic-gate  *
23910Sstevel@tonic-gate  *  The confounder is 8 bytes of random data.
23920Sstevel@tonic-gate  *  The cksum depends on the hash being used.
23930Sstevel@tonic-gate  *   4 bytes for CRC32
23940Sstevel@tonic-gate  *  16 bytes for MD5
23950Sstevel@tonic-gate  *  20 bytes for SHA1
23960Sstevel@tonic-gate  *   0 bytes for RAW
23970Sstevel@tonic-gate  *
23980Sstevel@tonic-gate  */
23990Sstevel@tonic-gate static mblk_t *
24000Sstevel@tonic-gate des_cbc_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, hash_info_t *hash)
24010Sstevel@tonic-gate {
24020Sstevel@tonic-gate 	uint_t inlen, datalen;
24030Sstevel@tonic-gate 	int result = 0;
24040Sstevel@tonic-gate 	uchar_t *optr = NULL;
24050Sstevel@tonic-gate 	uchar_t cksum[MAX_CKSUM_LEN], newcksum[MAX_CKSUM_LEN];
24060Sstevel@tonic-gate 	uchar_t nextiv[DEFAULT_DES_BLOCKLEN];
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	/* Compute adjusted size */
24090Sstevel@tonic-gate 	inlen = MBLKL(mp);
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 	optr = mp->b_rptr;
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 	/*
24140Sstevel@tonic-gate 	 * In order to support the "old" Kerberos RCMD protocol,
24150Sstevel@tonic-gate 	 * we must use the IVEC 3 different ways:
24160Sstevel@tonic-gate 	 *   IVEC_REUSE = keep using the same IV each time, this is
24170Sstevel@tonic-gate 	 *		ugly and insecure, but necessary for
24180Sstevel@tonic-gate 	 *		backwards compatibility with existing MIT code.
24190Sstevel@tonic-gate 	 *   IVEC_ONETIME = Use the ivec as initialized when the crypto
24200Sstevel@tonic-gate 	 *		was setup (see setup_crypto routine).
24210Sstevel@tonic-gate 	 *   IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
24220Sstevel@tonic-gate 	 */
24230Sstevel@tonic-gate 	if (tmi->dec_data.ivec_usage == IVEC_NEVER)
24240Sstevel@tonic-gate 		bzero(tmi->dec_data.block, tmi->dec_data.blocklen);
24250Sstevel@tonic-gate 	else if (tmi->dec_data.ivec_usage == IVEC_REUSE)
24260Sstevel@tonic-gate 		bcopy(tmi->dec_data.ivec, tmi->dec_data.block,
24270Sstevel@tonic-gate 		    tmi->dec_data.blocklen);
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate 	if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
24300Sstevel@tonic-gate 		/*
24310Sstevel@tonic-gate 		 * Do not decrypt the HMAC at the end
24320Sstevel@tonic-gate 		 */
24330Sstevel@tonic-gate 		int decrypt_len = inlen - hash->hash_len;
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 		/*
24360Sstevel@tonic-gate 		 * Move the wptr so the mblk appears to end
24370Sstevel@tonic-gate 		 * BEFORE the HMAC section.
24380Sstevel@tonic-gate 		 */
24390Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + decrypt_len;
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 		/*
24420Sstevel@tonic-gate 		 * Because we are using KEF, we must manually update our
24430Sstevel@tonic-gate 		 * IV.
24440Sstevel@tonic-gate 		 */
24450Sstevel@tonic-gate 		if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
24460Sstevel@tonic-gate 			bcopy(mp->b_rptr + decrypt_len - tmi->dec_data.ivlen,
24470Sstevel@tonic-gate 				nextiv, tmi->dec_data.ivlen);
24480Sstevel@tonic-gate 		}
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 		result = kef_decr_hmac(&tmi->dec_data, mp, decrypt_len,
24510Sstevel@tonic-gate 			(char *)newcksum, hash->hash_len);
24520Sstevel@tonic-gate 	} else {
24530Sstevel@tonic-gate 		/*
24540Sstevel@tonic-gate 		 * Because we are using KEF, we must manually update our
24550Sstevel@tonic-gate 		 * IV.
24560Sstevel@tonic-gate 		 */
24570Sstevel@tonic-gate 		if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
24580Sstevel@tonic-gate 			bcopy(mp->b_wptr - tmi->enc_data.ivlen, nextiv,
24590Sstevel@tonic-gate 				tmi->dec_data.ivlen);
24600Sstevel@tonic-gate 		}
24610Sstevel@tonic-gate 		result = kef_crypt(&tmi->dec_data, (void *)mp,
24620Sstevel@tonic-gate 			CRYPTO_DATA_MBLK, (size_t)inlen, CRYPT_DECRYPT);
24630Sstevel@tonic-gate 	}
24640Sstevel@tonic-gate 	if (result != CRYPTO_SUCCESS) {
24650Sstevel@tonic-gate #ifdef DEBUG
24660Sstevel@tonic-gate 		cmn_err(CE_WARN,
24670Sstevel@tonic-gate 			"des_cbc_decrypt: kef_crypt decrypt "
24680Sstevel@tonic-gate 			"failed - error %0x", result);
24690Sstevel@tonic-gate #endif
24700Sstevel@tonic-gate 		mp->b_datap->db_type = M_ERROR;
24710Sstevel@tonic-gate 		mp->b_rptr = mp->b_datap->db_base;
24720Sstevel@tonic-gate 		*mp->b_rptr = EIO;
24730Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
24740Sstevel@tonic-gate 		freemsg(mp->b_cont);
24750Sstevel@tonic-gate 		mp->b_cont = NULL;
24760Sstevel@tonic-gate 		qreply(WR(q), mp);
24770Sstevel@tonic-gate 		return (NULL);
24780Sstevel@tonic-gate 	}
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 	/*
24810Sstevel@tonic-gate 	 * Manually update the IV, KEF does not track this for us.
24820Sstevel@tonic-gate 	 */
24830Sstevel@tonic-gate 	if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
24840Sstevel@tonic-gate 		bcopy(nextiv, tmi->dec_data.block, tmi->dec_data.ivlen);
24850Sstevel@tonic-gate 	}
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	/* Verify the checksum(if necessary) */
24880Sstevel@tonic-gate 	if (hash->hash_len > 0) {
24890Sstevel@tonic-gate 		if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
24900Sstevel@tonic-gate 			bcopy(mp->b_rptr + inlen - hash->hash_len, cksum,
24910Sstevel@tonic-gate 				hash->hash_len);
24920Sstevel@tonic-gate 		} else {
24930Sstevel@tonic-gate 			bcopy(optr + hash->confound_len, cksum, hash->hash_len);
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate 			/* zero the cksum in the buffer */
24960Sstevel@tonic-gate 			ASSERT(optr + hash->confound_len + hash->hash_len <=
24970Sstevel@tonic-gate 				DB_LIM(mp));
24980Sstevel@tonic-gate 			bzero(optr + hash->confound_len, hash->hash_len);
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 			/* calculate MD5 chksum of confounder + input */
25010Sstevel@tonic-gate 			if (hash->hashfunc) {
25020Sstevel@tonic-gate 				(void) hash->hashfunc(newcksum, optr, inlen);
25030Sstevel@tonic-gate 			}
25040Sstevel@tonic-gate 		}
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 		if (bcmp(cksum, newcksum, hash->hash_len)) {
25070Sstevel@tonic-gate #ifdef DEBUG
25080Sstevel@tonic-gate 			cmn_err(CE_WARN, "des_cbc_decrypt: checksum "
25090Sstevel@tonic-gate 				"verification failed");
25100Sstevel@tonic-gate #endif
25110Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
25120Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
25130Sstevel@tonic-gate 			*mp->b_rptr = EIO;
25140Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
25150Sstevel@tonic-gate 			freemsg(mp->b_cont);
25160Sstevel@tonic-gate 			mp->b_cont = NULL;
25170Sstevel@tonic-gate 			qreply(WR(q), mp);
25180Sstevel@tonic-gate 			return (NULL);
25190Sstevel@tonic-gate 		}
25200Sstevel@tonic-gate 	}
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 	datalen = inlen - hash->confound_len - hash->hash_len;
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 	/* Move just the decrypted input into place if necessary */
25250Sstevel@tonic-gate 	if (hash->confound_len > 0 || hash->hash_len > 0) {
25260Sstevel@tonic-gate 		if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1)
25270Sstevel@tonic-gate 			mp->b_rptr += hash->confound_len;
25280Sstevel@tonic-gate 		else
25290Sstevel@tonic-gate 			mp->b_rptr += hash->confound_len + hash->hash_len;
25300Sstevel@tonic-gate 	}
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	ASSERT(mp->b_rptr + datalen <= DB_LIM(mp));
25330Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + datalen;
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	return (mp);
25360Sstevel@tonic-gate }
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate static mblk_t *
25390Sstevel@tonic-gate do_decrypt(queue_t *q, mblk_t *mp)
25400Sstevel@tonic-gate {
25410Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
25420Sstevel@tonic-gate 	mblk_t *outmp;
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 	switch (tmi->dec_data.method) {
25450Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CFB:
25460Sstevel@tonic-gate 		outmp = des_cfb_decrypt(q, tmi, mp);
25470Sstevel@tonic-gate 		break;
25480Sstevel@tonic-gate 	case CRYPT_METHOD_NONE:
25490Sstevel@tonic-gate 		outmp = mp;
25500Sstevel@tonic-gate 		break;
25510Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_NULL:
25520Sstevel@tonic-gate 		outmp = des_cbc_decrypt(q, tmi, mp, &null_hash);
25530Sstevel@tonic-gate 		break;
25540Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_MD5:
25550Sstevel@tonic-gate 		outmp = des_cbc_decrypt(q, tmi, mp, &md5_hash);
25560Sstevel@tonic-gate 		break;
25570Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_CRC:
25580Sstevel@tonic-gate 		outmp = des_cbc_decrypt(q, tmi, mp, &crc32_hash);
25590Sstevel@tonic-gate 		break;
25600Sstevel@tonic-gate 	case CRYPT_METHOD_DES3_CBC_SHA1:
25610Sstevel@tonic-gate 		outmp = des_cbc_decrypt(q, tmi, mp, &sha1_hash);
25620Sstevel@tonic-gate 		break;
25630Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
25640Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
25650Sstevel@tonic-gate 		outmp = arcfour_hmac_md5_decrypt(q, tmi, mp, &md5_hash);
25660Sstevel@tonic-gate 		break;
25670Sstevel@tonic-gate 	case CRYPT_METHOD_AES128:
25680Sstevel@tonic-gate 	case CRYPT_METHOD_AES256:
25690Sstevel@tonic-gate 		outmp = aes_decrypt(q, tmi, mp, &sha1_hash);
25700Sstevel@tonic-gate 		break;
25710Sstevel@tonic-gate 	}
25720Sstevel@tonic-gate 	return (outmp);
25730Sstevel@tonic-gate }
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate /*
25760Sstevel@tonic-gate  * do_encrypt
25770Sstevel@tonic-gate  *
25780Sstevel@tonic-gate  * Generic encryption routine for a single message block.
25790Sstevel@tonic-gate  * The input mblk may be replaced by some encrypt routines
25800Sstevel@tonic-gate  * because they add extra data in some cases that may exceed
25810Sstevel@tonic-gate  * the input mblk_t size limit.
25820Sstevel@tonic-gate  */
25830Sstevel@tonic-gate static mblk_t *
25840Sstevel@tonic-gate do_encrypt(queue_t *q, mblk_t *mp)
25850Sstevel@tonic-gate {
25860Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
25870Sstevel@tonic-gate 	mblk_t *outmp;
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	switch (tmi->enc_data.method) {
25900Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CFB:
25910Sstevel@tonic-gate 		outmp = des_cfb_encrypt(q, tmi, mp);
25920Sstevel@tonic-gate 		break;
25930Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_NULL:
25940Sstevel@tonic-gate 		outmp = des_cbc_encrypt(q, tmi, mp, &null_hash);
25950Sstevel@tonic-gate 		break;
25960Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_MD5:
25970Sstevel@tonic-gate 		outmp = des_cbc_encrypt(q, tmi, mp, &md5_hash);
25980Sstevel@tonic-gate 		break;
25990Sstevel@tonic-gate 	case CRYPT_METHOD_DES_CBC_CRC:
26000Sstevel@tonic-gate 		outmp = des_cbc_encrypt(q, tmi, mp, &crc32_hash);
26010Sstevel@tonic-gate 		break;
26020Sstevel@tonic-gate 	case CRYPT_METHOD_DES3_CBC_SHA1:
26030Sstevel@tonic-gate 		outmp = des_cbc_encrypt(q, tmi, mp, &sha1_hash);
26040Sstevel@tonic-gate 		break;
26050Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
26060Sstevel@tonic-gate 	case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
26070Sstevel@tonic-gate 		outmp = arcfour_hmac_md5_encrypt(q, tmi, mp, &md5_hash);
26080Sstevel@tonic-gate 		break;
26090Sstevel@tonic-gate 	case CRYPT_METHOD_AES128:
26100Sstevel@tonic-gate 	case CRYPT_METHOD_AES256:
26110Sstevel@tonic-gate 		outmp = aes_encrypt(q, tmi, mp, &sha1_hash);
26120Sstevel@tonic-gate 		break;
26130Sstevel@tonic-gate 	case CRYPT_METHOD_NONE:
26140Sstevel@tonic-gate 		outmp = mp;
26150Sstevel@tonic-gate 		break;
26160Sstevel@tonic-gate 	}
26170Sstevel@tonic-gate 	return (outmp);
26180Sstevel@tonic-gate }
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate /*
26210Sstevel@tonic-gate  * setup_crypto
26220Sstevel@tonic-gate  *
26230Sstevel@tonic-gate  * This takes the data from the CRYPTIOCSETUP ioctl
26240Sstevel@tonic-gate  * and sets up a cipher_data_t structure for either
26250Sstevel@tonic-gate  * encryption or decryption.  This is where the
26260Sstevel@tonic-gate  * key and initialization vector data get stored
26270Sstevel@tonic-gate  * prior to beginning any crypto functions.
26280Sstevel@tonic-gate  *
26290Sstevel@tonic-gate  * Special note:
26300Sstevel@tonic-gate  *   Some applications(e.g. telnetd) have ability to switch
26310Sstevel@tonic-gate  * crypto on/off periodically.  Thus, the application may call
26320Sstevel@tonic-gate  * the CRYPTIOCSETUP ioctl many times for the same stream.
26330Sstevel@tonic-gate  * If the CRYPTIOCSETUP is called with 0 length key or ivec fields
26340Sstevel@tonic-gate  * assume that the key, block, and saveblock fields that are already
26350Sstevel@tonic-gate  * set from a previous CRIOCSETUP call are still valid.  This helps avoid
26360Sstevel@tonic-gate  * a rekeying error that could occur if we overwrite these fields
26370Sstevel@tonic-gate  * with each CRYPTIOCSETUP call.
26380Sstevel@tonic-gate  *   In short, sometimes, CRYPTIOCSETUP is used to simply toggle on/off
26390Sstevel@tonic-gate  * without resetting the original crypto parameters.
26400Sstevel@tonic-gate  *
26410Sstevel@tonic-gate  */
26420Sstevel@tonic-gate static int
26430Sstevel@tonic-gate setup_crypto(struct cr_info_t *ci, struct cipher_data_t *cd, int encrypt)
26440Sstevel@tonic-gate {
26450Sstevel@tonic-gate 	uint_t newblocklen;
26460Sstevel@tonic-gate 	uint32_t enc_usage = 0, dec_usage = 0;
26470Sstevel@tonic-gate 	int rv;
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 	/*
26500Sstevel@tonic-gate 	 * Initial sanity checks
26510Sstevel@tonic-gate 	 */
26520Sstevel@tonic-gate 	if (!CR_METHOD_OK(ci->crypto_method)) {
26530Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal crypto method (%d)",
26540Sstevel@tonic-gate 			ci->crypto_method);
26550Sstevel@tonic-gate 		return (EINVAL);
26560Sstevel@tonic-gate 	}
26570Sstevel@tonic-gate 	if (!CR_OPTIONS_OK(ci->option_mask)) {
26580Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal crypto options (%d)",
26590Sstevel@tonic-gate 			ci->option_mask);
26600Sstevel@tonic-gate 		return (EINVAL);
26610Sstevel@tonic-gate 	}
26620Sstevel@tonic-gate 	if (!CR_IVUSAGE_OK(ci->ivec_usage)) {
26630Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal ivec usage value (%d)",
26640Sstevel@tonic-gate 			ci->ivec_usage);
26650Sstevel@tonic-gate 		return (EINVAL);
26660Sstevel@tonic-gate 	}
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 	cd->method = ci->crypto_method;
26690Sstevel@tonic-gate 	cd->bytes = 0;
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate 	if (ci->keylen > 0) {
26720Sstevel@tonic-gate 		if (cd->key != NULL) {
26730Sstevel@tonic-gate 			kmem_free(cd->key, cd->keylen);
26740Sstevel@tonic-gate 			cd->key = NULL;
26750Sstevel@tonic-gate 			cd->keylen = 0;
26760Sstevel@tonic-gate 		}
26770Sstevel@tonic-gate 		/*
26780Sstevel@tonic-gate 		 * cd->key holds the copy of the raw key bytes passed in
26790Sstevel@tonic-gate 		 * from the userland app.
26800Sstevel@tonic-gate 		 */
26810Sstevel@tonic-gate 		cd->key = (char *)kmem_alloc((size_t)ci->keylen, KM_SLEEP);
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate 		cd->keylen = ci->keylen;
26840Sstevel@tonic-gate 		bcopy(ci->key, cd->key, (size_t)ci->keylen);
26850Sstevel@tonic-gate 	}
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	/*
26880Sstevel@tonic-gate 	 * Configure the block size based on the type of cipher.
26890Sstevel@tonic-gate 	 */
26900Sstevel@tonic-gate 	switch (cd->method) {
26910Sstevel@tonic-gate 		case CRYPT_METHOD_NONE:
26920Sstevel@tonic-gate 			newblocklen = 0;
26930Sstevel@tonic-gate 			break;
26940Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CFB:
26950Sstevel@tonic-gate 			newblocklen = DEFAULT_DES_BLOCKLEN;
26960Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_DES_ECB);
26970Sstevel@tonic-gate 			break;
26980Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CBC_NULL:
26990Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CBC_MD5:
27000Sstevel@tonic-gate 		case CRYPT_METHOD_DES_CBC_CRC:
27010Sstevel@tonic-gate 			newblocklen = DEFAULT_DES_BLOCKLEN;
27020Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_DES_CBC);
27030Sstevel@tonic-gate 			break;
27040Sstevel@tonic-gate 		case CRYPT_METHOD_DES3_CBC_SHA1:
27050Sstevel@tonic-gate 			newblocklen = DEFAULT_DES_BLOCKLEN;
27060Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_DES3_CBC);
27070Sstevel@tonic-gate 			/* 3DES always uses the old usage constant */
27080Sstevel@tonic-gate 			enc_usage = RCMDV1_USAGE;
27090Sstevel@tonic-gate 			dec_usage = RCMDV1_USAGE;
27100Sstevel@tonic-gate 			break;
27110Sstevel@tonic-gate 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
27120Sstevel@tonic-gate 		case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
27130Sstevel@tonic-gate 			newblocklen = 0;
27140Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_RC4);
27150Sstevel@tonic-gate 			break;
27160Sstevel@tonic-gate 		case CRYPT_METHOD_AES128:
27170Sstevel@tonic-gate 		case CRYPT_METHOD_AES256:
27180Sstevel@tonic-gate 			newblocklen = DEFAULT_AES_BLOCKLEN;
27190Sstevel@tonic-gate 			cd->mech_type = crypto_mech2id(SUN_CKM_AES_ECB);
27200Sstevel@tonic-gate 			enc_usage = AES_ENCRYPT_USAGE;
27210Sstevel@tonic-gate 			dec_usage = AES_DECRYPT_USAGE;
27220Sstevel@tonic-gate 			break;
27230Sstevel@tonic-gate 	}
27240Sstevel@tonic-gate 	if (cd->mech_type == CRYPTO_MECH_INVALID) {
27250Sstevel@tonic-gate 		return (CRYPTO_FAILED);
27260Sstevel@tonic-gate 	}
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate 	/*
27290Sstevel@tonic-gate 	 * If RC4, initialize the master crypto key used by
27300Sstevel@tonic-gate 	 * the RC4 algorithm to derive the final encrypt and decrypt keys.
27310Sstevel@tonic-gate 	 */
27320Sstevel@tonic-gate 	if (cd->keylen > 0 && IS_RC4_METHOD(cd->method)) {
27330Sstevel@tonic-gate 		/*
27340Sstevel@tonic-gate 		 * cd->ckey is a kernel crypto key structure used as the
27350Sstevel@tonic-gate 		 * master key in the RC4-HMAC crypto operations.
27360Sstevel@tonic-gate 		 */
27370Sstevel@tonic-gate 		if (cd->ckey == NULL) {
27380Sstevel@tonic-gate 			cd->ckey = (crypto_key_t *)kmem_zalloc(
27390Sstevel@tonic-gate 				sizeof (crypto_key_t), KM_SLEEP);
27400Sstevel@tonic-gate 		}
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 		cd->ckey->ck_format = CRYPTO_KEY_RAW;
27430Sstevel@tonic-gate 		cd->ckey->ck_data = cd->key;
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 		/* key length for EF is measured in bits */
27460Sstevel@tonic-gate 		cd->ckey->ck_length = cd->keylen * 8;
27470Sstevel@tonic-gate 	}
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	/*
27500Sstevel@tonic-gate 	 * cd->block and cd->saveblock are used as temporary storage for
27510Sstevel@tonic-gate 	 * data that must be carried over between encrypt/decrypt operations
27520Sstevel@tonic-gate 	 * in some of the "feedback" modes.
27530Sstevel@tonic-gate 	 */
27540Sstevel@tonic-gate 	if (newblocklen != cd->blocklen) {
27550Sstevel@tonic-gate 		if (cd->block != NULL) {
27560Sstevel@tonic-gate 			kmem_free(cd->block, cd->blocklen);
27570Sstevel@tonic-gate 			cd->block = NULL;
27580Sstevel@tonic-gate 		}
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 		if (cd->saveblock != NULL) {
27610Sstevel@tonic-gate 			kmem_free(cd->saveblock, cd->blocklen);
27620Sstevel@tonic-gate 			cd->saveblock = NULL;
27630Sstevel@tonic-gate 		}
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 		cd->blocklen = newblocklen;
27660Sstevel@tonic-gate 		if (cd->blocklen) {
27670Sstevel@tonic-gate 			cd->block = (char *)kmem_zalloc((size_t)cd->blocklen,
27680Sstevel@tonic-gate 				KM_SLEEP);
27690Sstevel@tonic-gate 		}
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 		if (cd->method == CRYPT_METHOD_DES_CFB)
27720Sstevel@tonic-gate 			cd->saveblock = (char *)kmem_zalloc(cd->blocklen,
27730Sstevel@tonic-gate 						KM_SLEEP);
27740Sstevel@tonic-gate 		else
27750Sstevel@tonic-gate 			cd->saveblock = NULL;
27760Sstevel@tonic-gate 	}
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	if (ci->iveclen != cd->ivlen) {
27790Sstevel@tonic-gate 		if (cd->ivec != NULL) {
27800Sstevel@tonic-gate 			kmem_free(cd->ivec, cd->ivlen);
27810Sstevel@tonic-gate 			cd->ivec = NULL;
27820Sstevel@tonic-gate 		}
27830Sstevel@tonic-gate 		if (ci->ivec_usage != IVEC_NEVER && ci->iveclen > 0) {
27840Sstevel@tonic-gate 			cd->ivec = (char *)kmem_zalloc((size_t)ci->iveclen,
27850Sstevel@tonic-gate 						KM_SLEEP);
27860Sstevel@tonic-gate 			cd->ivlen = ci->iveclen;
27870Sstevel@tonic-gate 		} else {
27880Sstevel@tonic-gate 			cd->ivlen = 0;
27890Sstevel@tonic-gate 			cd->ivec = NULL;
27900Sstevel@tonic-gate 		}
27910Sstevel@tonic-gate 	}
27920Sstevel@tonic-gate 	cd->option_mask = ci->option_mask;
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 	/*
27950Sstevel@tonic-gate 	 * Old protocol requires a static 'usage' value for
27960Sstevel@tonic-gate 	 * deriving keys.  Yuk.
27970Sstevel@tonic-gate 	 */
27980Sstevel@tonic-gate 	if (cd->option_mask & CRYPTOPT_RCMD_MODE_V1) {
27990Sstevel@tonic-gate 		enc_usage = dec_usage = RCMDV1_USAGE;
28000Sstevel@tonic-gate 	}
28010Sstevel@tonic-gate 
28020Sstevel@tonic-gate 	if (cd->ivlen > cd->blocklen) {
28030Sstevel@tonic-gate 		cmn_err(CE_WARN, "setup_crypto: IV longer than block size");
28040Sstevel@tonic-gate 		return (EINVAL);
28050Sstevel@tonic-gate 	}
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 	/*
28080Sstevel@tonic-gate 	 * If we are using an IVEC "correctly" (i.e. set it once)
28090Sstevel@tonic-gate 	 * copy it here.
28100Sstevel@tonic-gate 	 */
28110Sstevel@tonic-gate 	if (ci->ivec_usage == IVEC_ONETIME && cd->block != NULL)
28120Sstevel@tonic-gate 		bcopy(ci->ivec, cd->block, (size_t)cd->ivlen);
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate 	cd->ivec_usage = ci->ivec_usage;
28150Sstevel@tonic-gate 	if (cd->ivec != NULL) {
28160Sstevel@tonic-gate 		/* Save the original IVEC in case we need it later */
28170Sstevel@tonic-gate 		bcopy(ci->ivec, cd->ivec, (size_t)cd->ivlen);
28180Sstevel@tonic-gate 	}
28190Sstevel@tonic-gate 	/*
28200Sstevel@tonic-gate 	 * Special handling for 3DES-SHA1-HMAC and AES crypto:
28210Sstevel@tonic-gate 	 * generate derived keys and context templates
28220Sstevel@tonic-gate 	 * for better performance.
28230Sstevel@tonic-gate 	 */
28240Sstevel@tonic-gate 	if (cd->method == CRYPT_METHOD_DES3_CBC_SHA1 ||
28250Sstevel@tonic-gate 	    IS_AES_METHOD(cd->method)) {
28260Sstevel@tonic-gate 		crypto_mechanism_t enc_mech;
28270Sstevel@tonic-gate 		crypto_mechanism_t hmac_mech;
28280Sstevel@tonic-gate 
28290Sstevel@tonic-gate 		if (cd->d_encr_key.ck_data != NULL) {
28300Sstevel@tonic-gate 			bzero(cd->d_encr_key.ck_data, cd->keylen);
28310Sstevel@tonic-gate 			kmem_free(cd->d_encr_key.ck_data, cd->keylen);
28320Sstevel@tonic-gate 		}
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 		if (cd->d_hmac_key.ck_data != NULL) {
28350Sstevel@tonic-gate 			bzero(cd->d_hmac_key.ck_data, cd->keylen);
28360Sstevel@tonic-gate 			kmem_free(cd->d_hmac_key.ck_data, cd->keylen);
28370Sstevel@tonic-gate 		}
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 		if (cd->enc_tmpl != NULL)
28400Sstevel@tonic-gate 			(void) crypto_destroy_ctx_template(cd->enc_tmpl);
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 		if (cd->hmac_tmpl != NULL)
28430Sstevel@tonic-gate 			(void) crypto_destroy_ctx_template(cd->hmac_tmpl);
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 		enc_mech.cm_type = cd->mech_type;
28460Sstevel@tonic-gate 		enc_mech.cm_param = cd->ivec;
28470Sstevel@tonic-gate 		enc_mech.cm_param_len = cd->ivlen;
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 		hmac_mech.cm_type = sha1_hmac_mech;
28500Sstevel@tonic-gate 		hmac_mech.cm_param = NULL;
28510Sstevel@tonic-gate 		hmac_mech.cm_param_len = 0;
28520Sstevel@tonic-gate 
28530Sstevel@tonic-gate 		/*
28540Sstevel@tonic-gate 		 * Create the derived keys.
28550Sstevel@tonic-gate 		 */
28560Sstevel@tonic-gate 		rv = create_derived_keys(cd,
28570Sstevel@tonic-gate 			(encrypt ? enc_usage : dec_usage),
28580Sstevel@tonic-gate 			&cd->d_encr_key, &cd->d_hmac_key);
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
28610Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to create derived "
28620Sstevel@tonic-gate 				"keys: %0x", rv);
28630Sstevel@tonic-gate 			return (CRYPTO_FAILED);
28640Sstevel@tonic-gate 		}
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 		rv = crypto_create_ctx_template(&enc_mech,
28670Sstevel@tonic-gate 					&cd->d_encr_key,
28680Sstevel@tonic-gate 					&cd->enc_tmpl, KM_SLEEP);
28690Sstevel@tonic-gate 		if (rv == CRYPTO_MECH_NOT_SUPPORTED) {
28700Sstevel@tonic-gate 			cd->enc_tmpl = NULL;
28710Sstevel@tonic-gate 		} else if (rv != CRYPTO_SUCCESS) {
28720Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to create enc template "
28730Sstevel@tonic-gate 				"for d_encr_key: %0x", rv);
28740Sstevel@tonic-gate 			return (CRYPTO_FAILED);
28750Sstevel@tonic-gate 		}
28760Sstevel@tonic-gate 
28770Sstevel@tonic-gate 		rv = crypto_create_ctx_template(&hmac_mech,
28780Sstevel@tonic-gate 				&cd->d_hmac_key,
28790Sstevel@tonic-gate 				&cd->hmac_tmpl, KM_SLEEP);
28800Sstevel@tonic-gate 		if (rv == CRYPTO_MECH_NOT_SUPPORTED) {
28810Sstevel@tonic-gate 			cd->hmac_tmpl = NULL;
28820Sstevel@tonic-gate 		} else if (rv != CRYPTO_SUCCESS) {
28830Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to create hmac template:"
28840Sstevel@tonic-gate 				" %0x", rv);
28850Sstevel@tonic-gate 			return (CRYPTO_FAILED);
28860Sstevel@tonic-gate 		}
28870Sstevel@tonic-gate 	} else if (IS_RC4_METHOD(cd->method)) {
28880Sstevel@tonic-gate 		bzero(&cd->d_encr_key, sizeof (crypto_key_t));
28890Sstevel@tonic-gate 		bzero(&cd->d_hmac_key, sizeof (crypto_key_t));
28900Sstevel@tonic-gate 		cd->ctx = NULL;
28910Sstevel@tonic-gate 		cd->enc_tmpl = NULL;
28920Sstevel@tonic-gate 		cd->hmac_tmpl = NULL;
28930Sstevel@tonic-gate 	}
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate 	/* Final sanity checks, make sure no fields are NULL */
28960Sstevel@tonic-gate 	if (cd->method != CRYPT_METHOD_NONE) {
28970Sstevel@tonic-gate 		if (cd->block == NULL && cd->blocklen > 0) {
28980Sstevel@tonic-gate #ifdef DEBUG
28990Sstevel@tonic-gate 			cmn_err(CE_WARN,
29000Sstevel@tonic-gate 				"setup_crypto: IV block not allocated");
29010Sstevel@tonic-gate #endif
29020Sstevel@tonic-gate 			return (ENOMEM);
29030Sstevel@tonic-gate 		}
29040Sstevel@tonic-gate 		if (cd->key == NULL && cd->keylen > 0) {
29050Sstevel@tonic-gate #ifdef DEBUG
29060Sstevel@tonic-gate 			cmn_err(CE_WARN,
29070Sstevel@tonic-gate 				"setup_crypto: key block not allocated");
29080Sstevel@tonic-gate #endif
29090Sstevel@tonic-gate 			return (ENOMEM);
29100Sstevel@tonic-gate 		}
29110Sstevel@tonic-gate 		if (cd->method == CRYPT_METHOD_DES_CFB &&
29120Sstevel@tonic-gate 		    cd->saveblock == NULL && cd->blocklen > 0) {
29130Sstevel@tonic-gate #ifdef DEBUG
29140Sstevel@tonic-gate 			cmn_err(CE_WARN,
29150Sstevel@tonic-gate 				"setup_crypto: save block not allocated");
29160Sstevel@tonic-gate #endif
29170Sstevel@tonic-gate 			return (ENOMEM);
29180Sstevel@tonic-gate 		}
29190Sstevel@tonic-gate 		if (cd->ivec == NULL && cd->ivlen > 0) {
29200Sstevel@tonic-gate #ifdef DEBUG
29210Sstevel@tonic-gate 			cmn_err(CE_WARN,
29220Sstevel@tonic-gate 				"setup_crypto: IV not allocated");
29230Sstevel@tonic-gate #endif
29240Sstevel@tonic-gate 			return (ENOMEM);
29250Sstevel@tonic-gate 		}
29260Sstevel@tonic-gate 	}
29270Sstevel@tonic-gate 	return (0);
29280Sstevel@tonic-gate }
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate /*
29310Sstevel@tonic-gate  * RCMDS require a 4 byte, clear text
29320Sstevel@tonic-gate  * length field before each message.
29330Sstevel@tonic-gate  * Add it now.
29340Sstevel@tonic-gate  */
29350Sstevel@tonic-gate static mblk_t *
29360Sstevel@tonic-gate mklenmp(mblk_t *bp, uint32_t len)
29370Sstevel@tonic-gate {
29380Sstevel@tonic-gate 	mblk_t *lenmp;
29390Sstevel@tonic-gate 	uchar_t *ucp;
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 	if (bp->b_rptr - 4 < DB_BASE(bp) || DB_REF(bp) > 1) {
29420Sstevel@tonic-gate 		lenmp = allocb(4, BPRI_MED);
29430Sstevel@tonic-gate 		if (lenmp != NULL) {
29440Sstevel@tonic-gate 			lenmp->b_rptr = lenmp->b_wptr = DB_LIM(lenmp);
29450Sstevel@tonic-gate 			linkb(lenmp, bp);
29460Sstevel@tonic-gate 			bp = lenmp;
29470Sstevel@tonic-gate 		}
29480Sstevel@tonic-gate 	}
29490Sstevel@tonic-gate 	ucp = bp->b_rptr;
29500Sstevel@tonic-gate 	*--ucp = len;
29510Sstevel@tonic-gate 	*--ucp = len >> 8;
29520Sstevel@tonic-gate 	*--ucp = len >> 16;
29530Sstevel@tonic-gate 	*--ucp = len >> 24;
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 	bp->b_rptr = ucp;
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 	return (bp);
29580Sstevel@tonic-gate }
29590Sstevel@tonic-gate 
29603518Spk193450 static mblk_t *
29613518Spk193450 encrypt_block(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, size_t plainlen)
29623518Spk193450 {
29633518Spk193450 	mblk_t *newmp;
29643518Spk193450 	size_t headspace;
29653518Spk193450 
29663518Spk193450 	mblk_t *cbp;
29673518Spk193450 	size_t cipherlen;
29683518Spk193450 	size_t extra = 0;
29693518Spk193450 	uint32_t ptlen = (uint32_t)plainlen;
29703518Spk193450 	/*
29713518Spk193450 	 * If we are using the "NEW" RCMD mode,
29723518Spk193450 	 * add 4 bytes to the plaintext for the
29733518Spk193450 	 * plaintext length that gets prepended
29743518Spk193450 	 * before encrypting.
29753518Spk193450 	 */
29763518Spk193450 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
29773518Spk193450 		ptlen += 4;
29783518Spk193450 
29793518Spk193450 	cipherlen = encrypt_size(&tmi->enc_data, (size_t)ptlen);
29803518Spk193450 
29813518Spk193450 	/*
29823518Spk193450 	 * if we must allocb, then make sure its enough
29833518Spk193450 	 * to hold the length field so we dont have to allocb
29843518Spk193450 	 * again down below in 'mklenmp'
29853518Spk193450 	 */
29863518Spk193450 	if (ANY_RCMD_MODE(tmi->enc_data.option_mask)) {
29873518Spk193450 		extra = sizeof (uint32_t);
29883518Spk193450 	}
29893518Spk193450 
29903518Spk193450 	/*
29913518Spk193450 	 * Calculate how much space is needed in front of
29923518Spk193450 	 * the data.
29933518Spk193450 	 */
29943518Spk193450 	headspace = plaintext_offset(&tmi->enc_data);
29953518Spk193450 
29963518Spk193450 	/*
29973518Spk193450 	 * If the current block is too small, reallocate
29983518Spk193450 	 * one large enough to hold the hdr, tail, and
29993518Spk193450 	 * ciphertext.
30003518Spk193450 	 */
30013518Spk193450 	if ((cipherlen + extra >= MBLKSIZE(mp)) || DB_REF(mp) > 1) {
30023518Spk193450 		int sz = P2ROUNDUP(cipherlen+extra, 8);
30033518Spk193450 
30043518Spk193450 		cbp = allocb_tmpl(sz, mp);
30053518Spk193450 		if (cbp == NULL) {
30063518Spk193450 			cmn_err(CE_WARN,
30073518Spk193450 				"allocb (%d bytes) failed", sz);
30083518Spk193450 				return (NULL);
30093518Spk193450 		}
30103518Spk193450 
30113518Spk193450 		cbp->b_cont = mp->b_cont;
30123518Spk193450 
30133518Spk193450 		/*
30143518Spk193450 		 * headspace includes the length fields needed
30153518Spk193450 		 * for the RCMD modes (v1 == 4 bytes, V2 = 8)
30163518Spk193450 		 */
30173518Spk193450 		cbp->b_rptr = DB_BASE(cbp) + headspace;
30183518Spk193450 
30193518Spk193450 		ASSERT(cbp->b_rptr + P2ROUNDUP(plainlen, 8)
30203518Spk193450 			<= DB_LIM(cbp));
30213518Spk193450 
30223518Spk193450 		bcopy(mp->b_rptr, cbp->b_rptr, plainlen);
30233518Spk193450 		cbp->b_wptr = cbp->b_rptr + plainlen;
30243518Spk193450 
30253518Spk193450 		freeb(mp);
30263518Spk193450 	} else {
30273518Spk193450 		size_t extra = 0;
30283518Spk193450 		cbp = mp;
30293518Spk193450 
30303518Spk193450 		/*
30313518Spk193450 		 * Some ciphers add HMAC after the final block
30323518Spk193450 		 * of the ciphertext, not at the beginning like the
30333518Spk193450 		 * 1-DES ciphers.
30343518Spk193450 		 */
30353518Spk193450 		if (tmi->enc_data.method ==
30363518Spk193450 			CRYPT_METHOD_DES3_CBC_SHA1 ||
30373518Spk193450 		    IS_AES_METHOD(tmi->enc_data.method)) {
30383518Spk193450 			extra = sha1_hash.hash_len;
30393518Spk193450 		}
30403518Spk193450 
30413518Spk193450 		/*
30423518Spk193450 		 * Make sure the rptr is positioned correctly so that
30433518Spk193450 		 * routines later do not have to shift this data around
30443518Spk193450 		 */
3045*7227Sps57422 		if ((cbp->b_rptr + P2ROUNDUP(cipherlen + extra, 8) >
30463518Spk193450 			DB_LIM(cbp)) ||
30473518Spk193450 			(cbp->b_rptr - headspace < DB_BASE(cbp))) {
30483518Spk193450 			ovbcopy(cbp->b_rptr, DB_BASE(cbp) + headspace,
30493518Spk193450 				plainlen);
30503518Spk193450 			cbp->b_rptr = DB_BASE(cbp) + headspace;
30513518Spk193450 			cbp->b_wptr = cbp->b_rptr + plainlen;
30523518Spk193450 		}
30533518Spk193450 	}
30543518Spk193450 
30553518Spk193450 	ASSERT(cbp->b_rptr - headspace >= DB_BASE(cbp));
30563518Spk193450 	ASSERT(cbp->b_wptr <= DB_LIM(cbp));
30573518Spk193450 
30583518Spk193450 	/*
30593518Spk193450 	 * If using RCMD_MODE_V2 (new rcmd mode), prepend
30603518Spk193450 	 * the plaintext length before the actual plaintext.
30613518Spk193450 	 */
30623518Spk193450 	if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2) {
30633518Spk193450 		cbp->b_rptr -= RCMD_LEN_SZ;
30643518Spk193450 
30653518Spk193450 		/* put plaintext length at head of buffer */
30663518Spk193450 		*(cbp->b_rptr + 3) = (uchar_t)(plainlen & 0xff);
30673518Spk193450 		*(cbp->b_rptr + 2) = (uchar_t)((plainlen >> 8) & 0xff);
30683518Spk193450 		*(cbp->b_rptr + 1) = (uchar_t)((plainlen >> 16) & 0xff);
30693518Spk193450 		*(cbp->b_rptr) = (uchar_t)((plainlen >> 24) & 0xff);
30703518Spk193450 	}
30713518Spk193450 
30723518Spk193450 	newmp = do_encrypt(q, cbp);
30733518Spk193450 
30743518Spk193450 	if (newmp != NULL &&
30753518Spk193450 	    (tmi->enc_data.option_mask &
30763518Spk193450 	    (CRYPTOPT_RCMD_MODE_V1 | CRYPTOPT_RCMD_MODE_V2))) {
30773518Spk193450 		mblk_t *lp;
30783518Spk193450 		/*
30793518Spk193450 		 * Add length field, required when this is
30803518Spk193450 		 * used to encrypt "r*" commands(rlogin, rsh)
30813518Spk193450 		 * with Kerberos.
30823518Spk193450 		 */
30833518Spk193450 		lp = mklenmp(newmp, plainlen);
30843518Spk193450 
30853518Spk193450 		if (lp == NULL) {
30863518Spk193450 			freeb(newmp);
30873518Spk193450 			return (NULL);
30883518Spk193450 		} else {
30893518Spk193450 			newmp = lp;
30903518Spk193450 		}
30913518Spk193450 	}
30923518Spk193450 	return (newmp);
30933518Spk193450 }
30943518Spk193450 
30950Sstevel@tonic-gate /*
30960Sstevel@tonic-gate  * encrypt_msgb
30970Sstevel@tonic-gate  *
30980Sstevel@tonic-gate  * encrypt a single message. This routine adds the
30990Sstevel@tonic-gate  * RCMD overhead bytes when necessary.
31000Sstevel@tonic-gate  */
31010Sstevel@tonic-gate static mblk_t *
31020Sstevel@tonic-gate encrypt_msgb(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
31030Sstevel@tonic-gate {
31043518Spk193450 	size_t plainlen, outlen;
31053518Spk193450 	mblk_t *newmp = NULL;
31063518Spk193450 
31073518Spk193450 	/* If not encrypting, do nothing */
31080Sstevel@tonic-gate 	if (tmi->enc_data.method == CRYPT_METHOD_NONE) {
31090Sstevel@tonic-gate 		return (mp);
31100Sstevel@tonic-gate 	}
31110Sstevel@tonic-gate 
31123518Spk193450 	plainlen = MBLKL(mp);
31133518Spk193450 	if (plainlen == 0)
31143518Spk193450 		return (NULL);
31153518Spk193450 
31160Sstevel@tonic-gate 	/*
31173518Spk193450 	 * If the block is too big, we encrypt in 4K chunks so that
31183518Spk193450 	 * older rlogin clients do not choke on the larger buffers.
31190Sstevel@tonic-gate 	 */
31203518Spk193450 	while ((plainlen = MBLKL(mp)) > MSGBUF_SIZE) {
31213518Spk193450 		mblk_t *mp1 = NULL;
31223518Spk193450 		outlen = MSGBUF_SIZE;
31230Sstevel@tonic-gate 		/*
31243518Spk193450 		 * Allocate a new buffer that is only 4K bytes, the
31253518Spk193450 		 * extra bytes are for crypto overhead.
31260Sstevel@tonic-gate 		 */
31273518Spk193450 		mp1 = allocb(outlen + CONFOUNDER_BYTES, BPRI_MED);
31283518Spk193450 		if (mp1 == NULL) {
31293518Spk193450 			cmn_err(CE_WARN,
31303518Spk193450 				"allocb (%d bytes) failed",
31313518Spk193450 				(int)(outlen + CONFOUNDER_BYTES));
31323518Spk193450 			return (NULL);
31330Sstevel@tonic-gate 		}
31343518Spk193450 		/* Copy the next 4K bytes from the old block. */
31353518Spk193450 		bcopy(mp->b_rptr, mp1->b_rptr, outlen);
31363518Spk193450 		mp1->b_wptr = mp1->b_rptr + outlen;
31373518Spk193450 		/* Advance the old block. */
31383518Spk193450 		mp->b_rptr += outlen;
31393518Spk193450 
31403518Spk193450 		/* encrypt the new block */
31413518Spk193450 		newmp = encrypt_block(q, tmi, mp1, outlen);
31423518Spk193450 		if (newmp == NULL)
31433518Spk193450 			return (NULL);
31443518Spk193450 
31453518Spk193450 		putnext(q, newmp);
31460Sstevel@tonic-gate 	}
31473518Spk193450 	newmp = NULL;
31483518Spk193450 	/* If there is data left (< MSGBUF_SIZE), encrypt it. */
31493532Spk193450 	if ((plainlen = MBLKL(mp)) > 0)
31503518Spk193450 		newmp = encrypt_block(q, tmi, mp, plainlen);
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 	return (newmp);
31530Sstevel@tonic-gate }
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate /*
31560Sstevel@tonic-gate  * cryptmodwsrv
31570Sstevel@tonic-gate  *
31580Sstevel@tonic-gate  * Service routine for the write queue.
31590Sstevel@tonic-gate  *
31600Sstevel@tonic-gate  * Because data may be placed in the queue to hold between
31610Sstevel@tonic-gate  * the CRYPTIOCSTOP and CRYPTIOCSTART ioctls, the service routine is needed.
31620Sstevel@tonic-gate  */
31630Sstevel@tonic-gate static int
31640Sstevel@tonic-gate cryptmodwsrv(queue_t *q)
31650Sstevel@tonic-gate {
31660Sstevel@tonic-gate 	mblk_t *mp;
31670Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
31680Sstevel@tonic-gate 
31690Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
31700Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
31710Sstevel@tonic-gate 		default:
31720Sstevel@tonic-gate 			/*
31730Sstevel@tonic-gate 			 * wput does not queue anything > QPCTL
31740Sstevel@tonic-gate 			 */
31750Sstevel@tonic-gate 			if (!canputnext(q) ||
31760Sstevel@tonic-gate 			    !(tmi->ready & CRYPT_WRITE_READY)) {
31770Sstevel@tonic-gate 				if (!putbq(q, mp)) {
31780Sstevel@tonic-gate 					freemsg(mp);
31790Sstevel@tonic-gate 				}
31800Sstevel@tonic-gate 				return (0);
31810Sstevel@tonic-gate 			}
31820Sstevel@tonic-gate 			putnext(q, mp);
31830Sstevel@tonic-gate 			break;
31840Sstevel@tonic-gate 		case M_DATA:
31850Sstevel@tonic-gate 			if (canputnext(q) && (tmi->ready & CRYPT_WRITE_READY)) {
31860Sstevel@tonic-gate 				mblk_t *bp;
31870Sstevel@tonic-gate 				mblk_t *newmsg = NULL;
31880Sstevel@tonic-gate 
31890Sstevel@tonic-gate 				/*
31900Sstevel@tonic-gate 				 * If multiple msgs, concat into 1
31910Sstevel@tonic-gate 				 * to minimize crypto operations later.
31920Sstevel@tonic-gate 				 */
31930Sstevel@tonic-gate 				if (mp->b_cont != NULL) {
31940Sstevel@tonic-gate 					bp = msgpullup(mp, -1);
31950Sstevel@tonic-gate 					if (bp != NULL) {
31960Sstevel@tonic-gate 						freemsg(mp);
31970Sstevel@tonic-gate 						mp = bp;
31980Sstevel@tonic-gate 					}
31990Sstevel@tonic-gate 				}
32000Sstevel@tonic-gate 				newmsg = encrypt_msgb(q, tmi, mp);
32010Sstevel@tonic-gate 				if (newmsg != NULL)
32020Sstevel@tonic-gate 					putnext(q, newmsg);
32030Sstevel@tonic-gate 			} else {
32040Sstevel@tonic-gate 				if (!putbq(q, mp)) {
32050Sstevel@tonic-gate 					freemsg(mp);
32060Sstevel@tonic-gate 				}
32070Sstevel@tonic-gate 				return (0);
32080Sstevel@tonic-gate 			}
32090Sstevel@tonic-gate 			break;
32100Sstevel@tonic-gate 		}
32110Sstevel@tonic-gate 	}
32120Sstevel@tonic-gate 	return (0);
32130Sstevel@tonic-gate }
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate static void
32160Sstevel@tonic-gate start_stream(queue_t *wq, mblk_t *mp, uchar_t dir)
32170Sstevel@tonic-gate {
32180Sstevel@tonic-gate 	mblk_t *newmp = NULL;
32190Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)wq->q_ptr;
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 	if (dir == CRYPT_ENCRYPT) {
32220Sstevel@tonic-gate 		tmi->ready |= CRYPT_WRITE_READY;
32230Sstevel@tonic-gate 		(void) (STRLOG(CRYPTMOD_ID, 0, 5, SL_TRACE|SL_NOTE,
32240Sstevel@tonic-gate 				"start_stream: restart ENCRYPT/WRITE q"));
32250Sstevel@tonic-gate 
32260Sstevel@tonic-gate 		enableok(wq);
32270Sstevel@tonic-gate 		qenable(wq);
32280Sstevel@tonic-gate 	} else if (dir == CRYPT_DECRYPT) {
32290Sstevel@tonic-gate 		/*
32300Sstevel@tonic-gate 		 * put any extra data in the RD
32310Sstevel@tonic-gate 		 * queue to be processed and
32320Sstevel@tonic-gate 		 * sent back up.
32330Sstevel@tonic-gate 		 */
32340Sstevel@tonic-gate 		newmp = mp->b_cont;
32350Sstevel@tonic-gate 		mp->b_cont = NULL;
32360Sstevel@tonic-gate 
32370Sstevel@tonic-gate 		tmi->ready |= CRYPT_READ_READY;
32380Sstevel@tonic-gate 		(void) (STRLOG(CRYPTMOD_ID, 0, 5,
32390Sstevel@tonic-gate 				SL_TRACE|SL_NOTE,
32400Sstevel@tonic-gate 				"start_stream: restart "
32410Sstevel@tonic-gate 				"DECRYPT/READ q"));
32420Sstevel@tonic-gate 
32430Sstevel@tonic-gate 		if (newmp != NULL)
32440Sstevel@tonic-gate 			if (!putbq(RD(wq), newmp))
32450Sstevel@tonic-gate 				freemsg(newmp);
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 		enableok(RD(wq));
32480Sstevel@tonic-gate 		qenable(RD(wq));
32490Sstevel@tonic-gate 	}
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate 	miocack(wq, mp, 0, 0);
32520Sstevel@tonic-gate }
32530Sstevel@tonic-gate 
32540Sstevel@tonic-gate /*
32550Sstevel@tonic-gate  * Write-side put procedure.  Its main task is to detect ioctls and
32560Sstevel@tonic-gate  * FLUSH operations.  Other message types are passed on through.
32570Sstevel@tonic-gate  */
32580Sstevel@tonic-gate static void
32590Sstevel@tonic-gate cryptmodwput(queue_t *wq, mblk_t *mp)
32600Sstevel@tonic-gate {
32610Sstevel@tonic-gate 	struct iocblk *iocp;
32620Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)wq->q_ptr;
32630Sstevel@tonic-gate 	int ret, err;
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
32660Sstevel@tonic-gate 	case M_DATA:
32670Sstevel@tonic-gate 		if (wq->q_first == NULL && canputnext(wq) &&
32680Sstevel@tonic-gate 		    (tmi->ready & CRYPT_WRITE_READY) &&
32690Sstevel@tonic-gate 		    tmi->enc_data.method == CRYPT_METHOD_NONE) {
32700Sstevel@tonic-gate 			putnext(wq, mp);
32710Sstevel@tonic-gate 			return;
32720Sstevel@tonic-gate 		}
32730Sstevel@tonic-gate 		/* else, put it in the service queue */
32740Sstevel@tonic-gate 		if (!putq(wq, mp)) {
32750Sstevel@tonic-gate 			freemsg(mp);
32760Sstevel@tonic-gate 		}
32770Sstevel@tonic-gate 		break;
32780Sstevel@tonic-gate 	case M_FLUSH:
32790Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
32800Sstevel@tonic-gate 			flushq(wq, FLUSHDATA);
32810Sstevel@tonic-gate 		}
32820Sstevel@tonic-gate 		putnext(wq, mp);
32830Sstevel@tonic-gate 		break;
32840Sstevel@tonic-gate 	case M_IOCTL:
32850Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
32860Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
32870Sstevel@tonic-gate 		case CRYPTIOCSETUP:
32880Sstevel@tonic-gate 			ret = 0;
32890Sstevel@tonic-gate 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
32900Sstevel@tonic-gate 					SL_TRACE | SL_NOTE,
32910Sstevel@tonic-gate 					"wput: got CRYPTIOCSETUP "
32920Sstevel@tonic-gate 					"ioctl(%d)", iocp->ioc_cmd));
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 			if ((err = miocpullup(mp,
32950Sstevel@tonic-gate 					sizeof (struct cr_info_t))) != 0) {
32960Sstevel@tonic-gate 				cmn_err(CE_WARN,
32970Sstevel@tonic-gate 				"wput: miocpullup failed for cr_info_t");
32980Sstevel@tonic-gate 				miocnak(wq, mp, 0, err);
32990Sstevel@tonic-gate 			} else {
33000Sstevel@tonic-gate 				struct cr_info_t *ci;
33010Sstevel@tonic-gate 				ci = (struct cr_info_t *)mp->b_cont->b_rptr;
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 				if (ci->direction_mask & CRYPT_ENCRYPT) {
33040Sstevel@tonic-gate 				    ret = setup_crypto(ci, &tmi->enc_data, 1);
33050Sstevel@tonic-gate 				}
33060Sstevel@tonic-gate 
33070Sstevel@tonic-gate 				if (ret == 0 &&
33080Sstevel@tonic-gate 				    (ci->direction_mask & CRYPT_DECRYPT)) {
33090Sstevel@tonic-gate 				    ret = setup_crypto(ci, &tmi->dec_data, 0);
33100Sstevel@tonic-gate 				}
33110Sstevel@tonic-gate 				if (ret == 0 &&
33120Sstevel@tonic-gate 				    (ci->direction_mask & CRYPT_DECRYPT) &&
33130Sstevel@tonic-gate 				    ANY_RCMD_MODE(tmi->dec_data.option_mask)) {
33140Sstevel@tonic-gate 					bzero(&tmi->rcmd_state,
33150Sstevel@tonic-gate 					    sizeof (tmi->rcmd_state));
33160Sstevel@tonic-gate 				}
33170Sstevel@tonic-gate 				if (ret == 0) {
33180Sstevel@tonic-gate 					miocack(wq, mp, 0, 0);
33190Sstevel@tonic-gate 				} else {
33200Sstevel@tonic-gate 					cmn_err(CE_WARN,
33210Sstevel@tonic-gate 						"wput: setup_crypto failed");
33220Sstevel@tonic-gate 					miocnak(wq, mp, 0, ret);
33230Sstevel@tonic-gate 				}
33240Sstevel@tonic-gate 				(void) (STRLOG(CRYPTMOD_ID, 0, 5,
33250Sstevel@tonic-gate 						SL_TRACE|SL_NOTE,
33260Sstevel@tonic-gate 						"wput: done with SETUP "
33270Sstevel@tonic-gate 						"ioctl"));
33280Sstevel@tonic-gate 			}
33290Sstevel@tonic-gate 			break;
33300Sstevel@tonic-gate 		case CRYPTIOCSTOP:
33310Sstevel@tonic-gate 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
33320Sstevel@tonic-gate 					SL_TRACE|SL_NOTE,
33330Sstevel@tonic-gate 					"wput: got CRYPTIOCSTOP "
33340Sstevel@tonic-gate 					"ioctl(%d)", iocp->ioc_cmd));
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate 			if ((err = miocpullup(mp, sizeof (uint32_t))) != 0) {
33370Sstevel@tonic-gate 				cmn_err(CE_WARN,
33380Sstevel@tonic-gate 					"wput: CRYPTIOCSTOP ioctl wrong "
33390Sstevel@tonic-gate 					"size (%d should be %d)",
33400Sstevel@tonic-gate 					(int)iocp->ioc_count,
33410Sstevel@tonic-gate 					(int)sizeof (uint32_t));
33420Sstevel@tonic-gate 				miocnak(wq, mp, 0, err);
33430Sstevel@tonic-gate 			} else {
33440Sstevel@tonic-gate 				uint32_t *stopdir;
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 				stopdir = (uint32_t *)mp->b_cont->b_rptr;
33470Sstevel@tonic-gate 				if (!CR_DIRECTION_OK(*stopdir)) {
33480Sstevel@tonic-gate 					miocnak(wq, mp, 0, EINVAL);
33490Sstevel@tonic-gate 					return;
33500Sstevel@tonic-gate 				}
33510Sstevel@tonic-gate 
33520Sstevel@tonic-gate 				/* disable the queues until further notice */
33530Sstevel@tonic-gate 				if (*stopdir & CRYPT_ENCRYPT) {
33540Sstevel@tonic-gate 					noenable(wq);
33550Sstevel@tonic-gate 					tmi->ready &= ~CRYPT_WRITE_READY;
33560Sstevel@tonic-gate 				}
33570Sstevel@tonic-gate 				if (*stopdir & CRYPT_DECRYPT) {
33580Sstevel@tonic-gate 					noenable(RD(wq));
33590Sstevel@tonic-gate 					tmi->ready &= ~CRYPT_READ_READY;
33600Sstevel@tonic-gate 				}
33610Sstevel@tonic-gate 
33620Sstevel@tonic-gate 				miocack(wq, mp, 0, 0);
33630Sstevel@tonic-gate 			}
33640Sstevel@tonic-gate 			break;
33650Sstevel@tonic-gate 		case CRYPTIOCSTARTDEC:
33660Sstevel@tonic-gate 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
33670Sstevel@tonic-gate 					SL_TRACE|SL_NOTE,
33680Sstevel@tonic-gate 					"wput: got CRYPTIOCSTARTDEC "
33690Sstevel@tonic-gate 					"ioctl(%d)", iocp->ioc_cmd));
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate 			start_stream(wq, mp, CRYPT_DECRYPT);
33720Sstevel@tonic-gate 			break;
33730Sstevel@tonic-gate 		case CRYPTIOCSTARTENC:
33740Sstevel@tonic-gate 			(void) (STRLOG(CRYPTMOD_ID, 0, 5,
33750Sstevel@tonic-gate 					SL_TRACE|SL_NOTE,
33760Sstevel@tonic-gate 					"wput: got CRYPTIOCSTARTENC "
33770Sstevel@tonic-gate 					"ioctl(%d)", iocp->ioc_cmd));
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate 			start_stream(wq, mp, CRYPT_ENCRYPT);
33800Sstevel@tonic-gate 			break;
33810Sstevel@tonic-gate 		default:
33820Sstevel@tonic-gate 			putnext(wq, mp);
33830Sstevel@tonic-gate 			break;
33840Sstevel@tonic-gate 		}
33850Sstevel@tonic-gate 		break;
33860Sstevel@tonic-gate 	default:
33870Sstevel@tonic-gate 		if (queclass(mp) < QPCTL) {
33880Sstevel@tonic-gate 			if (wq->q_first != NULL || !canputnext(wq)) {
33890Sstevel@tonic-gate 				if (!putq(wq, mp))
33900Sstevel@tonic-gate 					freemsg(mp);
33910Sstevel@tonic-gate 				return;
33920Sstevel@tonic-gate 			}
33930Sstevel@tonic-gate 		}
33940Sstevel@tonic-gate 		putnext(wq, mp);
33950Sstevel@tonic-gate 		break;
33960Sstevel@tonic-gate 	}
33970Sstevel@tonic-gate }
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate /*
34000Sstevel@tonic-gate  * decrypt_rcmd_mblks
34010Sstevel@tonic-gate  *
34020Sstevel@tonic-gate  * Because kerberized r* commands(rsh, rlogin, etc)
34030Sstevel@tonic-gate  * use a 4 byte length field to indicate the # of
34040Sstevel@tonic-gate  * PLAINTEXT bytes that are encrypted in the field
34050Sstevel@tonic-gate  * that follows, we must parse out each message and
34060Sstevel@tonic-gate  * break out the length fields prior to sending them
34070Sstevel@tonic-gate  * upstream to our Solaris r* clients/servers which do
34080Sstevel@tonic-gate  * NOT understand this format.
34090Sstevel@tonic-gate  *
34100Sstevel@tonic-gate  * Kerberized/encrypted message format:
34110Sstevel@tonic-gate  * -------------------------------
34120Sstevel@tonic-gate  * | XXXX | N bytes of ciphertext|
34130Sstevel@tonic-gate  * -------------------------------
34140Sstevel@tonic-gate  *
34150Sstevel@tonic-gate  * Where: XXXX = number of plaintext bytes that were encrypted in
34160Sstevel@tonic-gate  *               to make the ciphertext field.  This is done
34170Sstevel@tonic-gate  *               because we are using a cipher that pads out to
34180Sstevel@tonic-gate  *               an 8 byte boundary.  We only want the application
34190Sstevel@tonic-gate  *               layer to see the correct number of plain text bytes,
34200Sstevel@tonic-gate  *               not plaintext + pad.  So, after we decrypt, we
34210Sstevel@tonic-gate  *               must trim the output block down to the intended
34220Sstevel@tonic-gate  *               plaintext length and eliminate the pad bytes.
34230Sstevel@tonic-gate  *
34240Sstevel@tonic-gate  * This routine takes the entire input message, breaks it into
34250Sstevel@tonic-gate  * a new message that does not contain these length fields and
34260Sstevel@tonic-gate  * returns a message consisting of mblks filled with just ciphertext.
34270Sstevel@tonic-gate  *
34280Sstevel@tonic-gate  */
34290Sstevel@tonic-gate static mblk_t *
34300Sstevel@tonic-gate decrypt_rcmd_mblks(queue_t *q, mblk_t *mp)
34310Sstevel@tonic-gate {
34320Sstevel@tonic-gate 	mblk_t *newmp = NULL;
34330Sstevel@tonic-gate 	size_t msglen;
34340Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 	msglen = msgsize(mp);
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 	/*
34390Sstevel@tonic-gate 	 * If we need the length field, get it here.
34400Sstevel@tonic-gate 	 * Test the "plaintext length" indicator.
34410Sstevel@tonic-gate 	 */
34420Sstevel@tonic-gate 	if (tmi->rcmd_state.pt_len == 0) {
34430Sstevel@tonic-gate 		uint32_t elen;
34440Sstevel@tonic-gate 		int tocopy;
34450Sstevel@tonic-gate 		mblk_t *nextp;
34460Sstevel@tonic-gate 
34470Sstevel@tonic-gate 		/*
34480Sstevel@tonic-gate 		 * Make sure we have recieved all 4 bytes of the
34490Sstevel@tonic-gate 		 * length field.
34500Sstevel@tonic-gate 		 */
34510Sstevel@tonic-gate 		while (mp != NULL) {
34520Sstevel@tonic-gate 			ASSERT(tmi->rcmd_state.cd_len < sizeof (uint32_t));
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 			tocopy = sizeof (uint32_t) -
34550Sstevel@tonic-gate 				tmi->rcmd_state.cd_len;
34560Sstevel@tonic-gate 			if (tocopy > msglen)
34570Sstevel@tonic-gate 				tocopy = msglen;
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 			ASSERT(mp->b_rptr + tocopy <= DB_LIM(mp));
34600Sstevel@tonic-gate 			bcopy(mp->b_rptr,
34610Sstevel@tonic-gate 				(char *)(&tmi->rcmd_state.next_len +
34620Sstevel@tonic-gate 					tmi->rcmd_state.cd_len), tocopy);
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 			tmi->rcmd_state.cd_len += tocopy;
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 			if (tmi->rcmd_state.cd_len >= sizeof (uint32_t)) {
34670Sstevel@tonic-gate 				tmi->rcmd_state.next_len =
34680Sstevel@tonic-gate 					ntohl(tmi->rcmd_state.next_len);
34690Sstevel@tonic-gate 				break;
34700Sstevel@tonic-gate 			}
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate 			nextp = mp->b_cont;
34730Sstevel@tonic-gate 			mp->b_cont = NULL;
34740Sstevel@tonic-gate 			freeb(mp);
34750Sstevel@tonic-gate 			mp = nextp;
34760Sstevel@tonic-gate 		}
34770Sstevel@tonic-gate 
34780Sstevel@tonic-gate 		if (mp == NULL) {
34790Sstevel@tonic-gate 			return (NULL);
34800Sstevel@tonic-gate 		}
34810Sstevel@tonic-gate 		/*
34820Sstevel@tonic-gate 		 * recalculate the msglen now that we've read the
34830Sstevel@tonic-gate 		 * length and adjusted the bufptr (b_rptr).
34840Sstevel@tonic-gate 		 */
34850Sstevel@tonic-gate 		msglen -= tocopy;
34860Sstevel@tonic-gate 		mp->b_rptr += tocopy;
34870Sstevel@tonic-gate 
34880Sstevel@tonic-gate 		tmi->rcmd_state.pt_len = tmi->rcmd_state.next_len;
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate 		if (tmi->rcmd_state.pt_len <= 0) {
34910Sstevel@tonic-gate 			/*
34920Sstevel@tonic-gate 			 * Return an IO error to break the connection. there
34930Sstevel@tonic-gate 			 * is no way to recover from this.  Usually it means
34940Sstevel@tonic-gate 			 * the app has incorrectly requested decryption on
34950Sstevel@tonic-gate 			 * a non-encrypted stream, thus the "pt_len" field
34960Sstevel@tonic-gate 			 * is negative.
34970Sstevel@tonic-gate 			 */
34980Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
34990Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
35000Sstevel@tonic-gate 			*mp->b_rptr = EIO;
35010Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
35020Sstevel@tonic-gate 
35030Sstevel@tonic-gate 			freemsg(mp->b_cont);
35040Sstevel@tonic-gate 			mp->b_cont = NULL;
35050Sstevel@tonic-gate 			qreply(WR(q), mp);
35060Sstevel@tonic-gate 			tmi->rcmd_state.cd_len = tmi->rcmd_state.pt_len = 0;
35070Sstevel@tonic-gate 			return (NULL);
35080Sstevel@tonic-gate 		}
35090Sstevel@tonic-gate 
35100Sstevel@tonic-gate 		/*
35110Sstevel@tonic-gate 		 * If this is V2 mode, then the encrypted data is actually
35120Sstevel@tonic-gate 		 * 4 bytes bigger than the indicated len because the plaintext
35130Sstevel@tonic-gate 		 * length is encrypted for an additional security check, but
35140Sstevel@tonic-gate 		 * its not counted as part of the overall length we just read.
35150Sstevel@tonic-gate 		 * Strange and confusing, but true.
35160Sstevel@tonic-gate 		 */
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 		if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
35190Sstevel@tonic-gate 			elen = tmi->rcmd_state.pt_len + 4;
35200Sstevel@tonic-gate 		else
35210Sstevel@tonic-gate 			elen = tmi->rcmd_state.pt_len;
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate 		tmi->rcmd_state.cd_len  = encrypt_size(&tmi->dec_data, elen);
35240Sstevel@tonic-gate 
35250Sstevel@tonic-gate 		/*
35260Sstevel@tonic-gate 		 * Allocate an mblk to hold the cipher text until it is
35270Sstevel@tonic-gate 		 * all ready to be processed.
35280Sstevel@tonic-gate 		 */
35290Sstevel@tonic-gate 		tmi->rcmd_state.c_msg = allocb(tmi->rcmd_state.cd_len,
35300Sstevel@tonic-gate 						BPRI_HI);
35310Sstevel@tonic-gate 		if (tmi->rcmd_state.c_msg == NULL) {
35320Sstevel@tonic-gate #ifdef DEBUG
35330Sstevel@tonic-gate 			cmn_err(CE_WARN, "decrypt_rcmd_msgb: allocb failed "
35340Sstevel@tonic-gate 				"for %d bytes",
35350Sstevel@tonic-gate 				(int)tmi->rcmd_state.cd_len);
35360Sstevel@tonic-gate #endif
35370Sstevel@tonic-gate 			/*
35380Sstevel@tonic-gate 			 * Return an IO error to break the connection.
35390Sstevel@tonic-gate 			 */
35400Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
35410Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
35420Sstevel@tonic-gate 			*mp->b_rptr = EIO;
35430Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
35440Sstevel@tonic-gate 			freemsg(mp->b_cont);
35450Sstevel@tonic-gate 			mp->b_cont = NULL;
35460Sstevel@tonic-gate 			tmi->rcmd_state.cd_len = tmi->rcmd_state.pt_len = 0;
35470Sstevel@tonic-gate 			qreply(WR(q), mp);
35480Sstevel@tonic-gate 			return (NULL);
35490Sstevel@tonic-gate 		}
35500Sstevel@tonic-gate 	}
35510Sstevel@tonic-gate 
35520Sstevel@tonic-gate 	/*
35530Sstevel@tonic-gate 	 * If this entire message was just the length field,
35540Sstevel@tonic-gate 	 * free and return.  The actual data will probably be next.
35550Sstevel@tonic-gate 	 */
35560Sstevel@tonic-gate 	if (msglen == 0) {
35570Sstevel@tonic-gate 		freemsg(mp);
35580Sstevel@tonic-gate 		return (NULL);
35590Sstevel@tonic-gate 	}
35600Sstevel@tonic-gate 
35610Sstevel@tonic-gate 	/*
35620Sstevel@tonic-gate 	 * Copy as much of the cipher text as possible into
35630Sstevel@tonic-gate 	 * the new msgb (c_msg).
35640Sstevel@tonic-gate 	 *
35650Sstevel@tonic-gate 	 * Logic:  if we got some bytes (msglen) and we still
35660Sstevel@tonic-gate 	 * 	"need" some bytes (len-rcvd), get them here.
35670Sstevel@tonic-gate 	 */
35680Sstevel@tonic-gate 	ASSERT(tmi->rcmd_state.c_msg != NULL);
35690Sstevel@tonic-gate 	if (msglen > 0 &&
35700Sstevel@tonic-gate 	    (tmi->rcmd_state.cd_len > MBLKL(tmi->rcmd_state.c_msg))) {
35710Sstevel@tonic-gate 		mblk_t *bp, *nextp;
35720Sstevel@tonic-gate 		size_t n;
35730Sstevel@tonic-gate 
35740Sstevel@tonic-gate 		/*
35750Sstevel@tonic-gate 		 * Walk the mblks and copy just as many bytes as we need
35760Sstevel@tonic-gate 		 * for this particular block of cipher text.
35770Sstevel@tonic-gate 		 */
35780Sstevel@tonic-gate 		bp = mp;
35790Sstevel@tonic-gate 		while (bp != NULL) {
35800Sstevel@tonic-gate 			size_t needed;
35810Sstevel@tonic-gate 			size_t tocopy;
35820Sstevel@tonic-gate 			n = MBLKL(bp);
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate 			needed = tmi->rcmd_state.cd_len -
35850Sstevel@tonic-gate 				MBLKL(tmi->rcmd_state.c_msg);
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 			tocopy = (needed >= n ? n : needed);
35880Sstevel@tonic-gate 
35890Sstevel@tonic-gate 			ASSERT(bp->b_rptr + tocopy <= DB_LIM(bp));
35900Sstevel@tonic-gate 			ASSERT(tmi->rcmd_state.c_msg->b_wptr + tocopy <=
35910Sstevel@tonic-gate 				DB_LIM(tmi->rcmd_state.c_msg));
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 			/* Copy to end of new mblk */
35940Sstevel@tonic-gate 			bcopy(bp->b_rptr, tmi->rcmd_state.c_msg->b_wptr,
35950Sstevel@tonic-gate 				tocopy);
35960Sstevel@tonic-gate 
35970Sstevel@tonic-gate 			tmi->rcmd_state.c_msg->b_wptr += tocopy;
35980Sstevel@tonic-gate 
35990Sstevel@tonic-gate 			bp->b_rptr += tocopy;
36000Sstevel@tonic-gate 
36010Sstevel@tonic-gate 			nextp = bp->b_cont;
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 			/*
36040Sstevel@tonic-gate 			 * If we used this whole block, free it and
36050Sstevel@tonic-gate 			 * move on.
36060Sstevel@tonic-gate 			 */
36070Sstevel@tonic-gate 			if (!MBLKL(bp)) {
36080Sstevel@tonic-gate 				freeb(bp);
36090Sstevel@tonic-gate 				bp = NULL;
36100Sstevel@tonic-gate 			}
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate 			/* If we got what we needed, stop the loop */
36130Sstevel@tonic-gate 			if (MBLKL(tmi->rcmd_state.c_msg) ==
36140Sstevel@tonic-gate 			    tmi->rcmd_state.cd_len) {
36150Sstevel@tonic-gate 				/*
36160Sstevel@tonic-gate 				 * If there is more data in the message,
36170Sstevel@tonic-gate 				 * its for another block of cipher text,
36180Sstevel@tonic-gate 				 * put it back in the queue for next time.
36190Sstevel@tonic-gate 				 */
36200Sstevel@tonic-gate 				if (bp) {
36210Sstevel@tonic-gate 					if (!putbq(q, bp))
36220Sstevel@tonic-gate 						freemsg(bp);
36230Sstevel@tonic-gate 				} else if (nextp != NULL) {
36240Sstevel@tonic-gate 					/*
36250Sstevel@tonic-gate 					 * If there is more, put it back in the
36260Sstevel@tonic-gate 					 * queue for another pass thru.
36270Sstevel@tonic-gate 					 */
36280Sstevel@tonic-gate 					if (!putbq(q, nextp))
36290Sstevel@tonic-gate 						freemsg(nextp);
36300Sstevel@tonic-gate 				}
36310Sstevel@tonic-gate 				break;
36320Sstevel@tonic-gate 			}
36330Sstevel@tonic-gate 			bp = nextp;
36340Sstevel@tonic-gate 		}
36350Sstevel@tonic-gate 	}
36360Sstevel@tonic-gate 	/*
36370Sstevel@tonic-gate 	 * Finally, if we received all the cipher text data for
36380Sstevel@tonic-gate 	 * this message, decrypt it into a new msg and send it up
36390Sstevel@tonic-gate 	 * to the app.
36400Sstevel@tonic-gate 	 */
36410Sstevel@tonic-gate 	if (tmi->rcmd_state.pt_len > 0 &&
36420Sstevel@tonic-gate 	    MBLKL(tmi->rcmd_state.c_msg) == tmi->rcmd_state.cd_len) {
36430Sstevel@tonic-gate 		mblk_t *bp;
36440Sstevel@tonic-gate 		mblk_t *newbp;
36450Sstevel@tonic-gate 
36460Sstevel@tonic-gate 		/*
36470Sstevel@tonic-gate 		 * Now we can use our msg that we created when the
36480Sstevel@tonic-gate 		 * initial message boundary was detected.
36490Sstevel@tonic-gate 		 */
36500Sstevel@tonic-gate 		bp = tmi->rcmd_state.c_msg;
36510Sstevel@tonic-gate 		tmi->rcmd_state.c_msg = NULL;
36520Sstevel@tonic-gate 
36530Sstevel@tonic-gate 		newbp = do_decrypt(q, bp);
36540Sstevel@tonic-gate 		if (newbp != NULL) {
36550Sstevel@tonic-gate 			bp = newbp;
36560Sstevel@tonic-gate 			/*
36570Sstevel@tonic-gate 			 * If using RCMD_MODE_V2 ("new" mode),
36580Sstevel@tonic-gate 			 * look at the 4 byte plaintext length that
36590Sstevel@tonic-gate 			 * was just decrypted and compare with the
36600Sstevel@tonic-gate 			 * original pt_len value that was received.
36610Sstevel@tonic-gate 			 */
36620Sstevel@tonic-gate 			if (tmi->dec_data.option_mask &
36630Sstevel@tonic-gate 			    CRYPTOPT_RCMD_MODE_V2) {
36640Sstevel@tonic-gate 				uint32_t pt_len2;
36650Sstevel@tonic-gate 
36660Sstevel@tonic-gate 				pt_len2 = *(uint32_t *)bp->b_rptr;
36670Sstevel@tonic-gate 				pt_len2 = ntohl(pt_len2);
36680Sstevel@tonic-gate 				/*
36690Sstevel@tonic-gate 				 * Make sure the 2 pt len fields agree.
36700Sstevel@tonic-gate 				 */
36710Sstevel@tonic-gate 				if (pt_len2 != tmi->rcmd_state.pt_len) {
36720Sstevel@tonic-gate 					cmn_err(CE_WARN,
36730Sstevel@tonic-gate 						"Inconsistent length fields"
36740Sstevel@tonic-gate 						" received %d != %d",
36750Sstevel@tonic-gate 						(int)tmi->rcmd_state.pt_len,
36760Sstevel@tonic-gate 						(int)pt_len2);
36770Sstevel@tonic-gate 					bp->b_datap->db_type = M_ERROR;
36780Sstevel@tonic-gate 					bp->b_rptr = bp->b_datap->db_base;
36790Sstevel@tonic-gate 					*bp->b_rptr = EIO;
36800Sstevel@tonic-gate 					bp->b_wptr = bp->b_rptr + sizeof (char);
36810Sstevel@tonic-gate 					freemsg(bp->b_cont);
36820Sstevel@tonic-gate 					bp->b_cont = NULL;
36830Sstevel@tonic-gate 					tmi->rcmd_state.cd_len = 0;
36840Sstevel@tonic-gate 					qreply(WR(q), bp);
36850Sstevel@tonic-gate 					return (NULL);
36860Sstevel@tonic-gate 				}
36870Sstevel@tonic-gate 				bp->b_rptr += sizeof (uint32_t);
36880Sstevel@tonic-gate 			}
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate 			/*
36910Sstevel@tonic-gate 			 * Trim the decrypted block the length originally
36920Sstevel@tonic-gate 			 * indicated by the sender.  This is to remove any
36930Sstevel@tonic-gate 			 * padding bytes that the sender added to satisfy
36940Sstevel@tonic-gate 			 * requirements of the crypto algorithm.
36950Sstevel@tonic-gate 			 */
36960Sstevel@tonic-gate 			bp->b_wptr = bp->b_rptr + tmi->rcmd_state.pt_len;
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 			newmp = bp;
36990Sstevel@tonic-gate 
37000Sstevel@tonic-gate 			/*
37010Sstevel@tonic-gate 			 * Reset our state to indicate we are ready
37020Sstevel@tonic-gate 			 * for a new message.
37030Sstevel@tonic-gate 			 */
37040Sstevel@tonic-gate 			tmi->rcmd_state.pt_len = 0;
37050Sstevel@tonic-gate 			tmi->rcmd_state.cd_len = 0;
37060Sstevel@tonic-gate 		} else {
37070Sstevel@tonic-gate #ifdef DEBUG
37080Sstevel@tonic-gate 			cmn_err(CE_WARN,
37090Sstevel@tonic-gate 				"decrypt_rcmd: do_decrypt on %d bytes failed",
37100Sstevel@tonic-gate 				(int)tmi->rcmd_state.cd_len);
37110Sstevel@tonic-gate #endif
37120Sstevel@tonic-gate 			/*
37130Sstevel@tonic-gate 			 * do_decrypt already handled failures, just
37140Sstevel@tonic-gate 			 * return NULL.
37150Sstevel@tonic-gate 			 */
37160Sstevel@tonic-gate 			tmi->rcmd_state.pt_len = 0;
37170Sstevel@tonic-gate 			tmi->rcmd_state.cd_len = 0;
37180Sstevel@tonic-gate 			return (NULL);
37190Sstevel@tonic-gate 		}
37200Sstevel@tonic-gate 	}
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate 	/*
37230Sstevel@tonic-gate 	 * return the new message with the 'length' fields removed
37240Sstevel@tonic-gate 	 */
37250Sstevel@tonic-gate 	return (newmp);
37260Sstevel@tonic-gate }
37270Sstevel@tonic-gate 
37280Sstevel@tonic-gate /*
37290Sstevel@tonic-gate  * cryptmodrsrv
37300Sstevel@tonic-gate  *
37310Sstevel@tonic-gate  * Read queue service routine
37320Sstevel@tonic-gate  * Necessary because if the ready flag is not set
37330Sstevel@tonic-gate  * (via CRYPTIOCSTOP/CRYPTIOCSTART ioctls) then the data
37340Sstevel@tonic-gate  * must remain on queue and not be passed along.
37350Sstevel@tonic-gate  */
37360Sstevel@tonic-gate static int
37370Sstevel@tonic-gate cryptmodrsrv(queue_t *q)
37380Sstevel@tonic-gate {
37390Sstevel@tonic-gate 	mblk_t *mp, *bp;
37400Sstevel@tonic-gate 	struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
37430Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
37440Sstevel@tonic-gate 		case M_DATA:
37450Sstevel@tonic-gate 			if (canputnext(q) && tmi->ready & CRYPT_READ_READY) {
37460Sstevel@tonic-gate 				/*
37470Sstevel@tonic-gate 				 * Process "rcmd" messages differently because
37480Sstevel@tonic-gate 				 * they contain a 4 byte plaintext length
37490Sstevel@tonic-gate 				 * id that needs to be removed.
37500Sstevel@tonic-gate 				 */
37510Sstevel@tonic-gate 				if (tmi->dec_data.method != CRYPT_METHOD_NONE &&
37520Sstevel@tonic-gate 				    (tmi->dec_data.option_mask &
37530Sstevel@tonic-gate 				    (CRYPTOPT_RCMD_MODE_V1 |
37540Sstevel@tonic-gate 				    CRYPTOPT_RCMD_MODE_V2))) {
37550Sstevel@tonic-gate 					mp = decrypt_rcmd_mblks(q, mp);
37560Sstevel@tonic-gate 					if (mp)
37570Sstevel@tonic-gate 						putnext(q, mp);
37580Sstevel@tonic-gate 					continue;
37590Sstevel@tonic-gate 				}
37600Sstevel@tonic-gate 				if ((bp = msgpullup(mp, -1)) != NULL) {
37610Sstevel@tonic-gate 					freemsg(mp);
37620Sstevel@tonic-gate 					if (MBLKL(bp) > 0) {
37630Sstevel@tonic-gate 						mp = do_decrypt(q, bp);
37640Sstevel@tonic-gate 						if (mp != NULL)
37650Sstevel@tonic-gate 							putnext(q, mp);
37660Sstevel@tonic-gate 					}
37670Sstevel@tonic-gate 				}
37680Sstevel@tonic-gate 			} else {
37690Sstevel@tonic-gate 				if (!putbq(q, mp)) {
37700Sstevel@tonic-gate 					freemsg(mp);
37710Sstevel@tonic-gate 				}
37720Sstevel@tonic-gate 				return (0);
37730Sstevel@tonic-gate 			}
37740Sstevel@tonic-gate 			break;
37750Sstevel@tonic-gate 		default:
37760Sstevel@tonic-gate 			/*
37770Sstevel@tonic-gate 			 * rput does not queue anything > QPCTL, so we don't
37780Sstevel@tonic-gate 			 * need to check for it here.
37790Sstevel@tonic-gate 			 */
37800Sstevel@tonic-gate 			if (!canputnext(q)) {
37810Sstevel@tonic-gate 				if (!putbq(q, mp))
37820Sstevel@tonic-gate 					freemsg(mp);
37830Sstevel@tonic-gate 				return (0);
37840Sstevel@tonic-gate 			}
37850Sstevel@tonic-gate 			putnext(q, mp);
37860Sstevel@tonic-gate 			break;
37870Sstevel@tonic-gate 		}
37880Sstevel@tonic-gate 	}
37890Sstevel@tonic-gate 	return (0);
37900Sstevel@tonic-gate }
37910Sstevel@tonic-gate 
37920Sstevel@tonic-gate 
37930Sstevel@tonic-gate /*
37940Sstevel@tonic-gate  * Read-side put procedure.
37950Sstevel@tonic-gate  */
37960Sstevel@tonic-gate static void
37970Sstevel@tonic-gate cryptmodrput(queue_t *rq, mblk_t *mp)
37980Sstevel@tonic-gate {
37990Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
38000Sstevel@tonic-gate 	case M_DATA:
38010Sstevel@tonic-gate 		if (!putq(rq, mp)) {
38020Sstevel@tonic-gate 			freemsg(mp);
38030Sstevel@tonic-gate 		}
38040Sstevel@tonic-gate 		break;
38050Sstevel@tonic-gate 	case M_FLUSH:
38060Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
38070Sstevel@tonic-gate 			flushq(rq, FLUSHALL);
38080Sstevel@tonic-gate 		}
38090Sstevel@tonic-gate 		putnext(rq, mp);
38100Sstevel@tonic-gate 		break;
38110Sstevel@tonic-gate 	default:
38120Sstevel@tonic-gate 		if (queclass(mp) < QPCTL) {
38130Sstevel@tonic-gate 			if (rq->q_first != NULL || !canputnext(rq)) {
38140Sstevel@tonic-gate 				if (!putq(rq, mp))
38150Sstevel@tonic-gate 					freemsg(mp);
38160Sstevel@tonic-gate 				return;
38170Sstevel@tonic-gate 			}
38180Sstevel@tonic-gate 		}
38190Sstevel@tonic-gate 		putnext(rq, mp);
38200Sstevel@tonic-gate 		break;
38210Sstevel@tonic-gate 	}
38220Sstevel@tonic-gate }
3823