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